parser.go 3.5 KB

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