line_parsers.go 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294
  1. package parser
  2. // line parsers are dispatch calls that parse a single unit of text into a
  3. // Node object which contains the whole statement. Dockerfiles have varied
  4. // (but not usually unique, see ONBUILD for a unique example) parsing rules
  5. // per-command, and these unify the processing in a way that makes it
  6. // manageable.
  7. import (
  8. "encoding/json"
  9. "errors"
  10. "fmt"
  11. "strings"
  12. "unicode"
  13. )
  14. var (
  15. errDockerfileNotStringArray = errors.New("When using JSON array syntax, arrays must be comprised of strings only.")
  16. )
  17. // ignore the current argument. This will still leave a command parsed, but
  18. // will not incorporate the arguments into the ast.
  19. func parseIgnore(rest string) (*Node, map[string]bool, error) {
  20. return &Node{}, nil, nil
  21. }
  22. // used for onbuild. Could potentially be used for anything that represents a
  23. // statement with sub-statements.
  24. //
  25. // ONBUILD RUN foo bar -> (onbuild (run foo bar))
  26. //
  27. func parseSubCommand(rest string) (*Node, map[string]bool, error) {
  28. if rest == "" {
  29. return nil, nil, nil
  30. }
  31. _, child, err := parseLine(rest)
  32. if err != nil {
  33. return nil, nil, err
  34. }
  35. return &Node{Children: []*Node{child}}, nil, nil
  36. }
  37. // parse environment like statements. Note that this does *not* handle
  38. // variable interpolation, which will be handled in the evaluator.
  39. func parseNameVal(rest string, key string) (*Node, map[string]bool, error) {
  40. // This is kind of tricky because we need to support the old
  41. // variant: KEY name value
  42. // as well as the new one: KEY name=value ...
  43. // The trigger to know which one is being used will be whether we hit
  44. // a space or = first. space ==> old, "=" ==> new
  45. const (
  46. inSpaces = iota // looking for start of a word
  47. inWord
  48. inQuote
  49. )
  50. words := []string{}
  51. phase := inSpaces
  52. word := ""
  53. quote := '\000'
  54. blankOK := false
  55. var ch rune
  56. for pos := 0; pos <= len(rest); pos++ {
  57. if pos != len(rest) {
  58. ch = rune(rest[pos])
  59. }
  60. if phase == inSpaces { // Looking for start of word
  61. if pos == len(rest) { // end of input
  62. break
  63. }
  64. if unicode.IsSpace(ch) { // skip spaces
  65. continue
  66. }
  67. phase = inWord // found it, fall thru
  68. }
  69. if (phase == inWord || phase == inQuote) && (pos == len(rest)) {
  70. if blankOK || len(word) > 0 {
  71. words = append(words, word)
  72. }
  73. break
  74. }
  75. if phase == inWord {
  76. if unicode.IsSpace(ch) {
  77. phase = inSpaces
  78. if blankOK || len(word) > 0 {
  79. words = append(words, word)
  80. // Look for = and if not there assume
  81. // we're doing the old stuff and
  82. // just read the rest of the line
  83. if !strings.Contains(word, "=") {
  84. word = strings.TrimSpace(rest[pos:])
  85. words = append(words, word)
  86. break
  87. }
  88. }
  89. word = ""
  90. blankOK = false
  91. continue
  92. }
  93. if ch == '\'' || ch == '"' {
  94. quote = ch
  95. blankOK = true
  96. phase = inQuote
  97. }
  98. if ch == '\\' {
  99. if pos+1 == len(rest) {
  100. continue // just skip \ at end
  101. }
  102. // If we're not quoted and we see a \, then always just
  103. // add \ plus the char to the word, even if the char
  104. // is a quote.
  105. word += string(ch)
  106. pos++
  107. ch = rune(rest[pos])
  108. }
  109. word += string(ch)
  110. continue
  111. }
  112. if phase == inQuote {
  113. if ch == quote {
  114. phase = inWord
  115. }
  116. // \ is special except for ' quotes - can't escape anything for '
  117. if ch == '\\' && quote != '\'' {
  118. if pos+1 == len(rest) {
  119. phase = inWord
  120. continue // just skip \ at end
  121. }
  122. pos++
  123. nextCh := rune(rest[pos])
  124. word += string(ch)
  125. ch = nextCh
  126. }
  127. word += string(ch)
  128. }
  129. }
  130. if len(words) == 0 {
  131. return nil, nil, nil
  132. }
  133. // Old format (KEY name value)
  134. var rootnode *Node
  135. if !strings.Contains(words[0], "=") {
  136. node := &Node{}
  137. rootnode = node
  138. strs := TOKEN_WHITESPACE.Split(rest, 2)
  139. if len(strs) < 2 {
  140. return nil, nil, fmt.Errorf(key + " must have two arguments")
  141. }
  142. node.Value = strs[0]
  143. node.Next = &Node{}
  144. node.Next.Value = strs[1]
  145. } else {
  146. var prevNode *Node
  147. for i, word := range words {
  148. if !strings.Contains(word, "=") {
  149. return nil, nil, fmt.Errorf("Syntax error - can't find = in %q. Must be of the form: name=value", word)
  150. }
  151. parts := strings.SplitN(word, "=", 2)
  152. name := &Node{}
  153. value := &Node{}
  154. name.Next = value
  155. name.Value = parts[0]
  156. value.Value = parts[1]
  157. if i == 0 {
  158. rootnode = name
  159. } else {
  160. prevNode.Next = name
  161. }
  162. prevNode = value
  163. }
  164. }
  165. return rootnode, nil, nil
  166. }
  167. func parseEnv(rest string) (*Node, map[string]bool, error) {
  168. return parseNameVal(rest, "ENV")
  169. }
  170. func parseLabel(rest string) (*Node, map[string]bool, error) {
  171. return parseNameVal(rest, "LABEL")
  172. }
  173. // parses a whitespace-delimited set of arguments. The result is effectively a
  174. // linked list of string arguments.
  175. func parseStringsWhitespaceDelimited(rest string) (*Node, map[string]bool, error) {
  176. if rest == "" {
  177. return nil, nil, nil
  178. }
  179. node := &Node{}
  180. rootnode := node
  181. prevnode := node
  182. for _, str := range TOKEN_WHITESPACE.Split(rest, -1) { // use regexp
  183. prevnode = node
  184. node.Value = str
  185. node.Next = &Node{}
  186. node = node.Next
  187. }
  188. // XXX to get around regexp.Split *always* providing an empty string at the
  189. // end due to how our loop is constructed, nil out the last node in the
  190. // chain.
  191. prevnode.Next = nil
  192. return rootnode, nil, nil
  193. }
  194. // parsestring just wraps the string in quotes and returns a working node.
  195. func parseString(rest string) (*Node, map[string]bool, error) {
  196. if rest == "" {
  197. return nil, nil, nil
  198. }
  199. n := &Node{}
  200. n.Value = rest
  201. return n, nil, nil
  202. }
  203. // parseJSON converts JSON arrays to an AST.
  204. func parseJSON(rest string) (*Node, map[string]bool, error) {
  205. var myJson []interface{}
  206. if err := json.Unmarshal([]byte(rest), &myJson); err != nil {
  207. return nil, nil, err
  208. }
  209. var top, prev *Node
  210. for _, str := range myJson {
  211. if s, ok := str.(string); !ok {
  212. return nil, nil, errDockerfileNotStringArray
  213. } else {
  214. node := &Node{Value: s}
  215. if prev == nil {
  216. top = node
  217. } else {
  218. prev.Next = node
  219. }
  220. prev = node
  221. }
  222. }
  223. return top, map[string]bool{"json": true}, nil
  224. }
  225. // parseMaybeJSON determines if the argument appears to be a JSON array. If
  226. // so, passes to parseJSON; if not, quotes the result and returns a single
  227. // node.
  228. func parseMaybeJSON(rest string) (*Node, map[string]bool, error) {
  229. if rest == "" {
  230. return nil, nil, nil
  231. }
  232. node, attrs, err := parseJSON(rest)
  233. if err == nil {
  234. return node, attrs, nil
  235. }
  236. if err == errDockerfileNotStringArray {
  237. return nil, nil, err
  238. }
  239. node = &Node{}
  240. node.Value = rest
  241. return node, nil, nil
  242. }
  243. // parseMaybeJSONToList determines if the argument appears to be a JSON array. If
  244. // so, passes to parseJSON; if not, attmpts to parse it as a whitespace
  245. // delimited string.
  246. func parseMaybeJSONToList(rest string) (*Node, map[string]bool, error) {
  247. node, attrs, err := parseJSON(rest)
  248. if err == nil {
  249. return node, attrs, nil
  250. }
  251. if err == errDockerfileNotStringArray {
  252. return nil, nil, err
  253. }
  254. return parseStringsWhitespaceDelimited(rest)
  255. }