parser.go 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  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. }
  26. var (
  27. dispatch map[string]func(string) (*Node, error)
  28. TOKEN_WHITESPACE = regexp.MustCompile(`[\t\v\f\r ]+`)
  29. TOKEN_LINE_CONTINUATION = regexp.MustCompile(`\\$`)
  30. TOKEN_COMMENT = regexp.MustCompile(`^#.*$`)
  31. )
  32. func init() {
  33. // Dispatch Table. see line_parsers.go for the parse functions.
  34. // The command is parsed and mapped to the line parser. The line parser
  35. // recieves the arguments but not the command, and returns an AST after
  36. // reformulating the arguments according to the rules in the parser
  37. // functions. Errors are propogated up by Parse() and the resulting AST can
  38. // be incorporated directly into the existing AST as a next.
  39. dispatch = map[string]func(string) (*Node, error){
  40. "user": parseString,
  41. "onbuild": parseSubCommand,
  42. "workdir": parseString,
  43. "env": parseEnv,
  44. "maintainer": parseString,
  45. "docker-version": parseString,
  46. "from": parseString,
  47. "add": parseStringsWhitespaceDelimited,
  48. "copy": parseStringsWhitespaceDelimited,
  49. "run": parseMaybeJSON,
  50. "cmd": parseMaybeJSON,
  51. "entrypoint": parseMaybeJSON,
  52. "expose": parseStringsWhitespaceDelimited,
  53. "volume": parseMaybeJSON,
  54. "insert": parseIgnore,
  55. }
  56. }
  57. // empty node. Useful for managing structure.
  58. func blankNode() *Node {
  59. return &Node{"", nil, []*Node{}}
  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 := splitCommand(line)
  71. node := blankNode()
  72. node.Value = cmd
  73. sexp, err := fullDispatch(cmd, args)
  74. if err != nil {
  75. return "", nil, err
  76. }
  77. node.Next = sexp
  78. return "", node, nil
  79. }
  80. // The main parse routine. Handles an io.ReadWriteCloser and returns the root
  81. // of the AST.
  82. func Parse(rwc io.Reader) (*Node, error) {
  83. var child *Node
  84. var line string
  85. var err error
  86. root := blankNode()
  87. scanner := bufio.NewScanner(rwc)
  88. for scanner.Scan() {
  89. line, child, err = parseLine(strings.TrimSpace(scanner.Text()))
  90. if err != nil {
  91. return nil, err
  92. }
  93. if line != "" && child == nil {
  94. for {
  95. scanner.Scan()
  96. newline := strings.TrimSpace(scanner.Text())
  97. if newline == "" {
  98. continue
  99. }
  100. line, child, err = parseLine(line + newline)
  101. if err != nil {
  102. return nil, err
  103. }
  104. if child != nil {
  105. break
  106. }
  107. }
  108. }
  109. if child != nil {
  110. root.Children = append(root.Children, child)
  111. }
  112. }
  113. return root, nil
  114. }