parser.go 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. // Package parser 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. "github.com/docker/docker/builder/dockerfile/command"
  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. Flags []string // only top Node should have this set
  30. }
  31. var (
  32. dispatch map[string]func(string) (*Node, map[string]bool, error)
  33. tokenWhitespace = regexp.MustCompile(`[\t\v\f\r ]+`)
  34. tokenLineContinuation = regexp.MustCompile(`\\[ \t]*$`)
  35. tokenComment = regexp.MustCompile(`^#.*$`)
  36. )
  37. func init() {
  38. // Dispatch Table. see line_parsers.go for the parse functions.
  39. // The command is parsed and mapped to the line parser. The line parser
  40. // receives the arguments but not the command, and returns an AST after
  41. // reformulating the arguments according to the rules in the parser
  42. // functions. Errors are propagated up by Parse() and the resulting AST can
  43. // be incorporated directly into the existing AST as a next.
  44. dispatch = map[string]func(string) (*Node, map[string]bool, error){
  45. command.User: parseString,
  46. command.Onbuild: parseSubCommand,
  47. command.Workdir: parseString,
  48. command.Env: parseEnv,
  49. command.Label: parseLabel,
  50. command.Maintainer: parseString,
  51. command.From: parseString,
  52. command.Add: parseMaybeJSONToList,
  53. command.Copy: parseMaybeJSONToList,
  54. command.Run: parseMaybeJSON,
  55. command.Cmd: parseMaybeJSON,
  56. command.Entrypoint: parseMaybeJSON,
  57. command.Expose: parseStringsWhitespaceDelimited,
  58. command.Volume: parseMaybeJSONToList,
  59. command.StopSignal: parseString,
  60. command.Arg: parseNameOrNameVal,
  61. }
  62. }
  63. // parse a line and return the remainder.
  64. func parseLine(line string) (string, *Node, error) {
  65. if line = stripComments(line); line == "" {
  66. return "", nil, nil
  67. }
  68. if tokenLineContinuation.MatchString(line) {
  69. line = tokenLineContinuation.ReplaceAllString(line, "")
  70. return line, nil, nil
  71. }
  72. cmd, flags, args, err := splitCommand(line)
  73. if err != nil {
  74. return "", nil, err
  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. node.Next = sexp
  83. node.Attributes = attrs
  84. node.Original = line
  85. node.Flags = flags
  86. return "", node, nil
  87. }
  88. // Parse is the main parse routine.
  89. // It handles an io.ReadWriteCloser and returns the root of the AST.
  90. func Parse(rwc io.Reader) (*Node, error) {
  91. root := &Node{}
  92. scanner := bufio.NewScanner(rwc)
  93. for scanner.Scan() {
  94. scannedLine := strings.TrimLeftFunc(scanner.Text(), unicode.IsSpace)
  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. if child == nil && line != "" {
  114. line, child, err = parseLine(line)
  115. if err != nil {
  116. return nil, err
  117. }
  118. }
  119. }
  120. if child != nil {
  121. root.Children = append(root.Children, child)
  122. }
  123. }
  124. return root, nil
  125. }