line_parsers.go 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  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 no 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. continue
  98. }
  99. if ch == '\\' {
  100. if pos+1 == len(rest) {
  101. continue // just skip \ at end
  102. }
  103. pos++
  104. ch = rune(rest[pos])
  105. }
  106. word += string(ch)
  107. continue
  108. }
  109. if phase == inQuote {
  110. if ch == quote {
  111. phase = inWord
  112. continue
  113. }
  114. if ch == '\\' {
  115. if pos+1 == len(rest) {
  116. phase = inWord
  117. continue // just skip \ at end
  118. }
  119. pos++
  120. ch = rune(rest[pos])
  121. }
  122. word += string(ch)
  123. }
  124. }
  125. if len(words) == 0 {
  126. return nil, nil, nil
  127. }
  128. // Old format (KEY name value)
  129. var rootnode *Node
  130. if !strings.Contains(words[0], "=") {
  131. node := &Node{}
  132. rootnode = node
  133. strs := TOKEN_WHITESPACE.Split(rest, 2)
  134. if len(strs) < 2 {
  135. return nil, nil, fmt.Errorf(key + " must have two arguments")
  136. }
  137. node.Value = strs[0]
  138. node.Next = &Node{}
  139. node.Next.Value = strs[1]
  140. } else {
  141. var prevNode *Node
  142. for i, word := range words {
  143. if !strings.Contains(word, "=") {
  144. return nil, nil, fmt.Errorf("Syntax error - can't find = in %q. Must be of the form: name=value", word)
  145. }
  146. parts := strings.SplitN(word, "=", 2)
  147. name := &Node{}
  148. value := &Node{}
  149. name.Next = value
  150. name.Value = parts[0]
  151. value.Value = parts[1]
  152. if i == 0 {
  153. rootnode = name
  154. } else {
  155. prevNode.Next = name
  156. }
  157. prevNode = value
  158. }
  159. }
  160. return rootnode, nil, nil
  161. }
  162. func parseEnv(rest string) (*Node, map[string]bool, error) {
  163. return parseNameVal(rest, "ENV")
  164. }
  165. func parseLabel(rest string) (*Node, map[string]bool, error) {
  166. return parseNameVal(rest, "LABEL")
  167. }
  168. // parses a whitespace-delimited set of arguments. The result is effectively a
  169. // linked list of string arguments.
  170. func parseStringsWhitespaceDelimited(rest string) (*Node, map[string]bool, error) {
  171. if rest == "" {
  172. return nil, nil, nil
  173. }
  174. node := &Node{}
  175. rootnode := node
  176. prevnode := node
  177. for _, str := range TOKEN_WHITESPACE.Split(rest, -1) { // use regexp
  178. prevnode = node
  179. node.Value = str
  180. node.Next = &Node{}
  181. node = node.Next
  182. }
  183. // XXX to get around regexp.Split *always* providing an empty string at the
  184. // end due to how our loop is constructed, nil out the last node in the
  185. // chain.
  186. prevnode.Next = nil
  187. return rootnode, nil, nil
  188. }
  189. // parsestring just wraps the string in quotes and returns a working node.
  190. func parseString(rest string) (*Node, map[string]bool, error) {
  191. if rest == "" {
  192. return nil, nil, nil
  193. }
  194. n := &Node{}
  195. n.Value = rest
  196. return n, nil, nil
  197. }
  198. // parseJSON converts JSON arrays to an AST.
  199. func parseJSON(rest string) (*Node, map[string]bool, error) {
  200. var myJson []interface{}
  201. if err := json.Unmarshal([]byte(rest), &myJson); err != nil {
  202. return nil, nil, err
  203. }
  204. var top, prev *Node
  205. for _, str := range myJson {
  206. if s, ok := str.(string); !ok {
  207. return nil, nil, errDockerfileNotStringArray
  208. } else {
  209. node := &Node{Value: s}
  210. if prev == nil {
  211. top = node
  212. } else {
  213. prev.Next = node
  214. }
  215. prev = node
  216. }
  217. }
  218. return top, map[string]bool{"json": true}, nil
  219. }
  220. // parseMaybeJSON determines if the argument appears to be a JSON array. If
  221. // so, passes to parseJSON; if not, quotes the result and returns a single
  222. // node.
  223. func parseMaybeJSON(rest string) (*Node, map[string]bool, error) {
  224. if rest == "" {
  225. return nil, nil, nil
  226. }
  227. node, attrs, err := parseJSON(rest)
  228. if err == nil {
  229. return node, attrs, nil
  230. }
  231. if err == errDockerfileNotStringArray {
  232. return nil, nil, err
  233. }
  234. node = &Node{}
  235. node.Value = rest
  236. return node, nil, nil
  237. }
  238. // parseMaybeJSONToList determines if the argument appears to be a JSON array. If
  239. // so, passes to parseJSON; if not, attmpts to parse it as a whitespace
  240. // delimited string.
  241. func parseMaybeJSONToList(rest string) (*Node, map[string]bool, error) {
  242. node, attrs, err := parseJSON(rest)
  243. if err == nil {
  244. return node, attrs, nil
  245. }
  246. if err == errDockerfileNotStringArray {
  247. return nil, nil, err
  248. }
  249. return parseStringsWhitespaceDelimited(rest)
  250. }