2014-08-05 16:17:40 -04:00
|
|
|
package parser
|
|
|
|
|
2014-08-07 01:56:44 -04:00
|
|
|
// line parsers are dispatch calls that parse a single unit of text into a
|
|
|
|
// Node object which contains the whole statement. Dockerfiles have varied
|
|
|
|
// (but not usually unique, see ONBUILD for a unique example) parsing rules
|
|
|
|
// per-command, and these unify the processing in a way that makes it
|
|
|
|
// manageable.
|
|
|
|
|
2014-08-05 16:17:40 -04:00
|
|
|
import (
|
|
|
|
"encoding/json"
|
2014-08-07 03:42:10 -04:00
|
|
|
"errors"
|
2014-08-31 08:39:36 -04:00
|
|
|
"fmt"
|
2014-08-05 16:17:40 -04:00
|
|
|
"strings"
|
2014-09-25 22:28:24 -04:00
|
|
|
"unicode"
|
2014-08-05 16:17:40 -04:00
|
|
|
)
|
|
|
|
|
2014-08-07 03:42:10 -04:00
|
|
|
var (
|
2015-01-03 00:40:43 -05:00
|
|
|
errDockerfileNotStringArray = errors.New("When using JSON array syntax, arrays must be comprised of strings only.")
|
2014-08-07 03:42:10 -04:00
|
|
|
)
|
|
|
|
|
2014-08-05 16:17:40 -04:00
|
|
|
// ignore the current argument. This will still leave a command parsed, but
|
|
|
|
// will not incorporate the arguments into the ast.
|
2014-08-13 06:07:41 -04:00
|
|
|
func parseIgnore(rest string) (*Node, map[string]bool, error) {
|
|
|
|
return &Node{}, nil, nil
|
2014-08-05 16:17:40 -04:00
|
|
|
}
|
|
|
|
|
2014-08-07 01:56:44 -04:00
|
|
|
// used for onbuild. Could potentially be used for anything that represents a
|
|
|
|
// statement with sub-statements.
|
|
|
|
//
|
|
|
|
// ONBUILD RUN foo bar -> (onbuild (run foo bar))
|
|
|
|
//
|
2014-08-13 06:07:41 -04:00
|
|
|
func parseSubCommand(rest string) (*Node, map[string]bool, error) {
|
2015-02-04 12:34:25 -05:00
|
|
|
if rest == "" {
|
|
|
|
return nil, nil, nil
|
|
|
|
}
|
|
|
|
|
2016-04-23 15:59:17 -04:00
|
|
|
_, child, err := ParseLine(rest)
|
2014-08-05 16:17:40 -04:00
|
|
|
if err != nil {
|
2014-08-13 06:07:41 -04:00
|
|
|
return nil, nil, err
|
2014-08-05 16:17:40 -04:00
|
|
|
}
|
|
|
|
|
2014-08-13 06:07:41 -04:00
|
|
|
return &Node{Children: []*Node{child}}, nil, nil
|
2014-08-05 16:17:40 -04:00
|
|
|
}
|
|
|
|
|
2014-11-14 13:59:14 -05:00
|
|
|
// helper to parse words (i.e space delimited or quoted strings) in a statement.
|
|
|
|
// The quotes are preserved as part of this function and they are stripped later
|
|
|
|
// as part of processWords().
|
|
|
|
func parseWords(rest string) []string {
|
2014-09-25 22:28:24 -04:00
|
|
|
const (
|
|
|
|
inSpaces = iota // looking for start of a word
|
|
|
|
inWord
|
|
|
|
inQuote
|
|
|
|
)
|
|
|
|
|
|
|
|
words := []string{}
|
|
|
|
phase := inSpaces
|
|
|
|
word := ""
|
|
|
|
quote := '\000'
|
|
|
|
blankOK := false
|
|
|
|
var ch rune
|
|
|
|
|
|
|
|
for pos := 0; pos <= len(rest); pos++ {
|
|
|
|
if pos != len(rest) {
|
|
|
|
ch = rune(rest[pos])
|
|
|
|
}
|
|
|
|
|
|
|
|
if phase == inSpaces { // Looking for start of word
|
|
|
|
if pos == len(rest) { // end of input
|
|
|
|
break
|
|
|
|
}
|
|
|
|
if unicode.IsSpace(ch) { // skip spaces
|
|
|
|
continue
|
|
|
|
}
|
2016-02-22 14:22:20 -05:00
|
|
|
phase = inWord // found it, fall through
|
2014-09-25 22:28:24 -04:00
|
|
|
}
|
|
|
|
if (phase == inWord || phase == inQuote) && (pos == len(rest)) {
|
|
|
|
if blankOK || len(word) > 0 {
|
|
|
|
words = append(words, word)
|
|
|
|
}
|
|
|
|
break
|
|
|
|
}
|
|
|
|
if phase == inWord {
|
|
|
|
if unicode.IsSpace(ch) {
|
|
|
|
phase = inSpaces
|
|
|
|
if blankOK || len(word) > 0 {
|
|
|
|
words = append(words, word)
|
|
|
|
}
|
|
|
|
word = ""
|
|
|
|
blankOK = false
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if ch == '\'' || ch == '"' {
|
|
|
|
quote = ch
|
|
|
|
blankOK = true
|
|
|
|
phase = inQuote
|
|
|
|
}
|
2016-04-22 18:04:46 -04:00
|
|
|
if ch == tokenEscape {
|
2014-09-25 22:28:24 -04:00
|
|
|
if pos+1 == len(rest) {
|
2016-04-22 18:04:46 -04:00
|
|
|
continue // just skip an escape token at end of line
|
2014-09-25 22:28:24 -04:00
|
|
|
}
|
2016-04-22 18:04:46 -04:00
|
|
|
// If we're not quoted and we see an escape token, then always just
|
|
|
|
// add the escape token plus the char to the word, even if the char
|
2015-01-28 21:28:48 -05:00
|
|
|
// is a quote.
|
|
|
|
word += string(ch)
|
2014-09-25 22:28:24 -04:00
|
|
|
pos++
|
|
|
|
ch = rune(rest[pos])
|
|
|
|
}
|
|
|
|
word += string(ch)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if phase == inQuote {
|
|
|
|
if ch == quote {
|
|
|
|
phase = inWord
|
|
|
|
}
|
2016-04-22 18:04:46 -04:00
|
|
|
// The escape token is special except for ' quotes - can't escape anything for '
|
|
|
|
if ch == tokenEscape && quote != '\'' {
|
2014-09-25 22:28:24 -04:00
|
|
|
if pos+1 == len(rest) {
|
|
|
|
phase = inWord
|
2016-04-22 18:04:46 -04:00
|
|
|
continue // just skip the escape token at end
|
2014-09-25 22:28:24 -04:00
|
|
|
}
|
|
|
|
pos++
|
2015-01-28 21:28:48 -05:00
|
|
|
nextCh := rune(rest[pos])
|
|
|
|
word += string(ch)
|
|
|
|
ch = nextCh
|
2014-09-25 22:28:24 -04:00
|
|
|
}
|
|
|
|
word += string(ch)
|
|
|
|
}
|
|
|
|
}
|
2014-08-31 08:39:36 -04:00
|
|
|
|
2014-11-14 13:59:14 -05:00
|
|
|
return words
|
|
|
|
}
|
|
|
|
|
|
|
|
// parse environment like statements. Note that this does *not* handle
|
|
|
|
// variable interpolation, which will be handled in the evaluator.
|
|
|
|
func parseNameVal(rest string, key string) (*Node, map[string]bool, error) {
|
|
|
|
// This is kind of tricky because we need to support the old
|
|
|
|
// variant: KEY name value
|
|
|
|
// as well as the new one: KEY name=value ...
|
|
|
|
// The trigger to know which one is being used will be whether we hit
|
|
|
|
// a space or = first. space ==> old, "=" ==> new
|
|
|
|
|
|
|
|
words := parseWords(rest)
|
2014-09-25 22:28:24 -04:00
|
|
|
if len(words) == 0 {
|
2015-03-17 01:23:40 -04:00
|
|
|
return nil, nil, nil
|
2014-08-31 08:39:36 -04:00
|
|
|
}
|
|
|
|
|
2014-09-25 22:28:24 -04:00
|
|
|
var rootnode *Node
|
|
|
|
|
2014-11-14 13:59:14 -05:00
|
|
|
// Old format (KEY name value)
|
2014-09-25 22:28:24 -04:00
|
|
|
if !strings.Contains(words[0], "=") {
|
|
|
|
node := &Node{}
|
|
|
|
rootnode = node
|
2015-07-22 01:29:03 -04:00
|
|
|
strs := tokenWhitespace.Split(rest, 2)
|
2014-09-25 22:28:24 -04:00
|
|
|
|
|
|
|
if len(strs) < 2 {
|
2015-02-17 10:20:06 -05:00
|
|
|
return nil, nil, fmt.Errorf(key + " must have two arguments")
|
2014-09-25 22:28:24 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
node.Value = strs[0]
|
|
|
|
node.Next = &Node{}
|
|
|
|
node.Next.Value = strs[1]
|
|
|
|
} else {
|
|
|
|
var prevNode *Node
|
|
|
|
for i, word := range words {
|
|
|
|
if !strings.Contains(word, "=") {
|
|
|
|
return nil, nil, fmt.Errorf("Syntax error - can't find = in %q. Must be of the form: name=value", word)
|
|
|
|
}
|
|
|
|
parts := strings.SplitN(word, "=", 2)
|
|
|
|
|
|
|
|
name := &Node{}
|
|
|
|
value := &Node{}
|
|
|
|
|
|
|
|
name.Next = value
|
|
|
|
name.Value = parts[0]
|
|
|
|
value.Value = parts[1]
|
|
|
|
|
|
|
|
if i == 0 {
|
|
|
|
rootnode = name
|
|
|
|
} else {
|
|
|
|
prevNode.Next = name
|
|
|
|
}
|
|
|
|
prevNode = value
|
|
|
|
}
|
|
|
|
}
|
2014-08-05 16:17:40 -04:00
|
|
|
|
2014-08-13 06:07:41 -04:00
|
|
|
return rootnode, nil, nil
|
2014-08-05 16:17:40 -04:00
|
|
|
}
|
|
|
|
|
2015-02-17 10:20:06 -05:00
|
|
|
func parseEnv(rest string) (*Node, map[string]bool, error) {
|
|
|
|
return parseNameVal(rest, "ENV")
|
|
|
|
}
|
|
|
|
|
|
|
|
func parseLabel(rest string) (*Node, map[string]bool, error) {
|
|
|
|
return parseNameVal(rest, "LABEL")
|
|
|
|
}
|
|
|
|
|
2014-11-14 13:59:14 -05:00
|
|
|
// parses a statement containing one or more keyword definition(s) and/or
|
|
|
|
// value assignments, like `name1 name2= name3="" name4=value`.
|
|
|
|
// Note that this is a stricter format than the old format of assignment,
|
|
|
|
// allowed by parseNameVal(), in a way that this only allows assignment of the
|
|
|
|
// form `keyword=[<value>]` like `name2=`, `name3=""`, and `name4=value` above.
|
|
|
|
// In addition, a keyword definition alone is of the form `keyword` like `name1`
|
|
|
|
// above. And the assignments `name2=` and `name3=""` are equivalent and
|
|
|
|
// assign an empty value to the respective keywords.
|
|
|
|
func parseNameOrNameVal(rest string) (*Node, map[string]bool, error) {
|
|
|
|
words := parseWords(rest)
|
|
|
|
if len(words) == 0 {
|
|
|
|
return nil, nil, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
var (
|
|
|
|
rootnode *Node
|
|
|
|
prevNode *Node
|
|
|
|
)
|
|
|
|
for i, word := range words {
|
|
|
|
node := &Node{}
|
|
|
|
node.Value = word
|
|
|
|
if i == 0 {
|
|
|
|
rootnode = node
|
|
|
|
} else {
|
|
|
|
prevNode.Next = node
|
|
|
|
}
|
|
|
|
prevNode = node
|
|
|
|
}
|
|
|
|
|
|
|
|
return rootnode, nil, nil
|
|
|
|
}
|
|
|
|
|
2014-08-05 16:17:40 -04:00
|
|
|
// parses a whitespace-delimited set of arguments. The result is effectively a
|
|
|
|
// linked list of string arguments.
|
2014-08-13 06:07:41 -04:00
|
|
|
func parseStringsWhitespaceDelimited(rest string) (*Node, map[string]bool, error) {
|
2015-02-04 12:34:25 -05:00
|
|
|
if rest == "" {
|
|
|
|
return nil, nil, nil
|
|
|
|
}
|
|
|
|
|
2014-08-10 07:01:10 -04:00
|
|
|
node := &Node{}
|
2014-08-05 16:17:40 -04:00
|
|
|
rootnode := node
|
2014-08-05 18:41:09 -04:00
|
|
|
prevnode := node
|
2015-07-22 01:29:03 -04:00
|
|
|
for _, str := range tokenWhitespace.Split(rest, -1) { // use regexp
|
2014-08-05 18:41:09 -04:00
|
|
|
prevnode = node
|
|
|
|
node.Value = str
|
2014-08-10 07:01:10 -04:00
|
|
|
node.Next = &Node{}
|
2014-08-05 16:17:40 -04:00
|
|
|
node = node.Next
|
|
|
|
}
|
|
|
|
|
2014-08-05 18:41:09 -04:00
|
|
|
// XXX to get around regexp.Split *always* providing an empty string at the
|
|
|
|
// end due to how our loop is constructed, nil out the last node in the
|
|
|
|
// chain.
|
|
|
|
prevnode.Next = nil
|
|
|
|
|
2014-08-13 06:07:41 -04:00
|
|
|
return rootnode, nil, nil
|
2014-08-05 16:17:40 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// parsestring just wraps the string in quotes and returns a working node.
|
2014-08-13 06:07:41 -04:00
|
|
|
func parseString(rest string) (*Node, map[string]bool, error) {
|
2015-02-04 12:34:25 -05:00
|
|
|
if rest == "" {
|
|
|
|
return nil, nil, nil
|
|
|
|
}
|
2014-08-13 06:07:41 -04:00
|
|
|
n := &Node{}
|
|
|
|
n.Value = rest
|
|
|
|
return n, nil, nil
|
2014-08-05 16:17:40 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// parseJSON converts JSON arrays to an AST.
|
2014-08-13 06:07:41 -04:00
|
|
|
func parseJSON(rest string) (*Node, map[string]bool, error) {
|
2015-08-17 13:22:57 -04:00
|
|
|
rest = strings.TrimLeftFunc(rest, unicode.IsSpace)
|
|
|
|
if !strings.HasPrefix(rest, "[") {
|
2015-08-17 13:22:57 -04:00
|
|
|
return nil, nil, fmt.Errorf(`Error parsing "%s" as a JSON array`, rest)
|
2015-08-17 13:22:57 -04:00
|
|
|
}
|
|
|
|
|
2015-07-22 01:29:03 -04:00
|
|
|
var myJSON []interface{}
|
|
|
|
if err := json.NewDecoder(strings.NewReader(rest)).Decode(&myJSON); err != nil {
|
2014-08-13 06:07:41 -04:00
|
|
|
return nil, nil, err
|
2014-08-05 16:17:40 -04:00
|
|
|
}
|
|
|
|
|
2015-01-03 00:40:43 -05:00
|
|
|
var top, prev *Node
|
2015-07-22 01:29:03 -04:00
|
|
|
for _, str := range myJSON {
|
2015-03-23 15:21:37 -04:00
|
|
|
s, ok := str.(string)
|
|
|
|
if !ok {
|
2015-01-03 00:40:43 -05:00
|
|
|
return nil, nil, errDockerfileNotStringArray
|
2015-03-23 15:21:37 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
node := &Node{Value: s}
|
|
|
|
if prev == nil {
|
|
|
|
top = node
|
2015-01-03 00:40:43 -05:00
|
|
|
} else {
|
2015-03-23 15:21:37 -04:00
|
|
|
prev.Next = node
|
2014-08-05 16:17:40 -04:00
|
|
|
}
|
2015-03-23 15:21:37 -04:00
|
|
|
prev = node
|
2014-08-05 16:17:40 -04:00
|
|
|
}
|
|
|
|
|
2015-01-03 00:40:43 -05:00
|
|
|
return top, map[string]bool{"json": true}, nil
|
2014-08-05 16:17:40 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// parseMaybeJSON determines if the argument appears to be a JSON array. If
|
|
|
|
// so, passes to parseJSON; if not, quotes the result and returns a single
|
|
|
|
// node.
|
2014-08-13 06:07:41 -04:00
|
|
|
func parseMaybeJSON(rest string) (*Node, map[string]bool, error) {
|
2015-02-04 12:34:25 -05:00
|
|
|
if rest == "" {
|
|
|
|
return nil, nil, nil
|
|
|
|
}
|
2014-08-05 16:17:40 -04:00
|
|
|
|
2014-08-19 07:14:21 -04:00
|
|
|
node, attrs, err := parseJSON(rest)
|
2014-08-08 16:44:57 -04:00
|
|
|
|
2014-08-19 07:14:21 -04:00
|
|
|
if err == nil {
|
|
|
|
return node, attrs, nil
|
|
|
|
}
|
2015-01-03 00:40:43 -05:00
|
|
|
if err == errDockerfileNotStringArray {
|
2014-08-19 07:14:21 -04:00
|
|
|
return nil, nil, err
|
2014-08-05 16:17:40 -04:00
|
|
|
}
|
|
|
|
|
2014-08-19 07:14:21 -04:00
|
|
|
node = &Node{}
|
2014-08-05 18:41:09 -04:00
|
|
|
node.Value = rest
|
2014-08-13 06:07:41 -04:00
|
|
|
return node, nil, nil
|
2014-08-05 16:17:40 -04:00
|
|
|
}
|
2014-09-11 09:27:51 -04:00
|
|
|
|
|
|
|
// parseMaybeJSONToList determines if the argument appears to be a JSON array. If
|
2015-04-10 22:34:21 -04:00
|
|
|
// so, passes to parseJSON; if not, attempts to parse it as a whitespace
|
2014-09-11 09:27:51 -04:00
|
|
|
// delimited string.
|
|
|
|
func parseMaybeJSONToList(rest string) (*Node, map[string]bool, error) {
|
|
|
|
node, attrs, err := parseJSON(rest)
|
|
|
|
|
|
|
|
if err == nil {
|
|
|
|
return node, attrs, nil
|
|
|
|
}
|
2015-01-03 00:40:43 -05:00
|
|
|
if err == errDockerfileNotStringArray {
|
2014-09-11 09:27:51 -04:00
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return parseStringsWhitespaceDelimited(rest)
|
|
|
|
}
|
2016-04-18 05:48:13 -04:00
|
|
|
|
|
|
|
// The HEALTHCHECK command is like parseMaybeJSON, but has an extra type argument.
|
|
|
|
func parseHealthConfig(rest string) (*Node, map[string]bool, error) {
|
|
|
|
// Find end of first argument
|
|
|
|
var sep int
|
|
|
|
for ; sep < len(rest); sep++ {
|
|
|
|
if unicode.IsSpace(rune(rest[sep])) {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
next := sep
|
|
|
|
for ; next < len(rest); next++ {
|
|
|
|
if !unicode.IsSpace(rune(rest[next])) {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if sep == 0 {
|
|
|
|
return nil, nil, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
typ := rest[:sep]
|
|
|
|
cmd, attrs, err := parseMaybeJSON(rest[next:])
|
|
|
|
if err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return &Node{Value: typ, Next: cmd, Attributes: attrs}, nil, err
|
|
|
|
}
|