line_parsers.go 6.4 KB

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