parser.go 3.5 KB

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