parser.go 3.9 KB

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