parser.go 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  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. "github.com/docker/docker/builder/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. }
  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 propagated 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. command.User: parseString,
  45. command.Onbuild: parseSubCommand,
  46. command.Workdir: parseString,
  47. command.Env: parseEnv,
  48. command.Label: parseLabel,
  49. command.Maintainer: parseString,
  50. command.From: parseString,
  51. command.Add: parseMaybeJSONToList,
  52. command.Copy: parseMaybeJSONToList,
  53. command.Run: parseMaybeJSON,
  54. command.Cmd: parseMaybeJSON,
  55. command.Entrypoint: parseMaybeJSON,
  56. command.Expose: parseStringsWhitespaceDelimited,
  57. command.Volume: parseMaybeJSONToList,
  58. command.Insert: parseIgnore,
  59. }
  60. }
  61. // parse a line and return the remainder.
  62. func parseLine(line string) (string, *Node, error) {
  63. if line = stripComments(line); line == "" {
  64. return "", nil, nil
  65. }
  66. if TOKEN_LINE_CONTINUATION.MatchString(line) {
  67. line = TOKEN_LINE_CONTINUATION.ReplaceAllString(line, "")
  68. return line, nil, nil
  69. }
  70. cmd, args, err := splitCommand(line)
  71. if err != nil {
  72. return "", nil, err
  73. }
  74. node := &Node{}
  75. node.Value = cmd
  76. sexp, attrs, err := fullDispatch(cmd, args)
  77. if err != nil {
  78. return "", nil, err
  79. }
  80. node.Next = sexp
  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. line, child, err := parseLine(scannedLine)
  93. if err != nil {
  94. return nil, err
  95. }
  96. if line != "" && child == nil {
  97. for scanner.Scan() {
  98. newline := scanner.Text()
  99. if stripComments(strings.TrimSpace(newline)) == "" {
  100. continue
  101. }
  102. line, child, err = parseLine(line + newline)
  103. if err != nil {
  104. return nil, err
  105. }
  106. if child != nil {
  107. break
  108. }
  109. }
  110. if child == nil && line != "" {
  111. line, child, err = parseLine(line)
  112. if err != nil {
  113. return nil, err
  114. }
  115. }
  116. }
  117. if child != nil {
  118. root.Children = append(root.Children, child)
  119. }
  120. }
  121. return root, nil
  122. }