parser.go 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. // This package implements a parser and parse tree dumper for Dockerfiles.
  2. package parser
  3. import (
  4. "bufio"
  5. "io"
  6. "regexp"
  7. "strings"
  8. "unicode"
  9. )
  10. // Node is a structure used to represent a parse tree.
  11. //
  12. // In the node there are three fields, Value, Next, and Children. Value is the
  13. // current token's string value. Next is always the next non-child token, and
  14. // children contains all the children. Here's an example:
  15. //
  16. // (value next (child child-next child-next-next) next-next)
  17. //
  18. // This data structure is frankly pretty lousy for handling complex languages,
  19. // but lucky for us the Dockerfile isn't very complicated. This structure
  20. // works a little more effectively than a "proper" parse tree for our needs.
  21. //
  22. type Node struct {
  23. Value string // actual content
  24. Next *Node // the next item in the current sexp
  25. Children []*Node // the children of this sexp
  26. Attributes map[string]bool // special attributes for this node
  27. Original string // original line used before parsing
  28. }
  29. var (
  30. dispatch map[string]func(string) (*Node, map[string]bool, error)
  31. TOKEN_WHITESPACE = regexp.MustCompile(`[\t\v\f\r ]+`)
  32. TOKEN_LINE_CONTINUATION = regexp.MustCompile(`\\\s*$`)
  33. TOKEN_COMMENT = regexp.MustCompile(`^#.*$`)
  34. )
  35. func init() {
  36. // Dispatch Table. see line_parsers.go for the parse functions.
  37. // The command is parsed and mapped to the line parser. The line parser
  38. // recieves the arguments but not the command, and returns an AST after
  39. // reformulating the arguments according to the rules in the parser
  40. // functions. Errors are propogated up by Parse() and the resulting AST can
  41. // be incorporated directly into the existing AST as a next.
  42. dispatch = map[string]func(string) (*Node, map[string]bool, error){
  43. "user": parseString,
  44. "onbuild": parseSubCommand,
  45. "workdir": parseString,
  46. "env": parseEnv,
  47. "maintainer": parseString,
  48. "from": parseString,
  49. "add": parseStringsWhitespaceDelimited,
  50. "copy": parseStringsWhitespaceDelimited,
  51. "run": parseMaybeJSON,
  52. "cmd": parseMaybeJSON,
  53. "entrypoint": parseMaybeJSON,
  54. "expose": parseStringsWhitespaceDelimited,
  55. "volume": parseMaybeJSONToList,
  56. "insert": parseIgnore,
  57. }
  58. }
  59. // parse a line and return the remainder.
  60. func parseLine(line string) (string, *Node, error) {
  61. if line = stripComments(line); line == "" {
  62. return "", nil, nil
  63. }
  64. if TOKEN_LINE_CONTINUATION.MatchString(line) {
  65. line = TOKEN_LINE_CONTINUATION.ReplaceAllString(line, "")
  66. return line, nil, nil
  67. }
  68. cmd, args, err := splitCommand(line)
  69. if err != nil {
  70. return "", nil, err
  71. }
  72. node := &Node{}
  73. node.Value = cmd
  74. sexp, attrs, err := fullDispatch(cmd, args)
  75. if err != nil {
  76. return "", nil, err
  77. }
  78. if sexp.Value != "" || sexp.Next != nil || sexp.Children != nil {
  79. node.Next = sexp
  80. }
  81. node.Attributes = attrs
  82. node.Original = line
  83. return "", node, nil
  84. }
  85. // The main parse routine. Handles an io.ReadWriteCloser and returns the root
  86. // of the AST.
  87. func Parse(rwc io.Reader) (*Node, error) {
  88. root := &Node{}
  89. scanner := bufio.NewScanner(rwc)
  90. for scanner.Scan() {
  91. scannedLine := strings.TrimLeftFunc(scanner.Text(), unicode.IsSpace)
  92. if stripComments(scannedLine) == "" {
  93. continue
  94. }
  95. line, child, err := parseLine(scannedLine)
  96. if err != nil {
  97. return nil, err
  98. }
  99. if line != "" && child == nil {
  100. for scanner.Scan() {
  101. newline := scanner.Text()
  102. if stripComments(strings.TrimSpace(newline)) == "" {
  103. continue
  104. }
  105. line, child, err = parseLine(line + newline)
  106. if err != nil {
  107. return nil, err
  108. }
  109. if child != nil {
  110. break
  111. }
  112. }
  113. }
  114. if child != nil {
  115. root.Children = append(root.Children, child)
  116. }
  117. }
  118. return root, nil
  119. }