2014-08-05 16:17:40 -04:00
|
|
|
// This package implements a parser and parse tree dumper for Dockerfiles.
|
|
|
|
package parser
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bufio"
|
|
|
|
"io"
|
|
|
|
"regexp"
|
|
|
|
"strings"
|
2014-10-02 19:58:53 -04:00
|
|
|
"unicode"
|
2015-02-12 02:54:41 -05:00
|
|
|
|
|
|
|
"github.com/docker/docker/builder/command"
|
2014-08-05 16:17:40 -04:00
|
|
|
)
|
|
|
|
|
2014-08-07 01:56:44 -04:00
|
|
|
// Node is a structure used to represent a parse tree.
|
2014-08-05 16:17:40 -04:00
|
|
|
//
|
2014-08-07 01:56:44 -04:00
|
|
|
// In the node there are three fields, Value, Next, and Children. Value is the
|
|
|
|
// current token's string value. Next is always the next non-child token, and
|
|
|
|
// children contains all the children. Here's an example:
|
2014-08-05 16:17:40 -04:00
|
|
|
//
|
2014-08-07 01:56:44 -04:00
|
|
|
// (value next (child child-next child-next-next) next-next)
|
2014-08-05 16:17:40 -04:00
|
|
|
//
|
2014-08-07 01:56:44 -04:00
|
|
|
// This data structure is frankly pretty lousy for handling complex languages,
|
|
|
|
// but lucky for us the Dockerfile isn't very complicated. This structure
|
|
|
|
// works a little more effectively than a "proper" parse tree for our needs.
|
2014-08-05 16:17:40 -04:00
|
|
|
//
|
|
|
|
type Node struct {
|
2014-08-13 06:07:41 -04:00
|
|
|
Value string // actual content
|
|
|
|
Next *Node // the next item in the current sexp
|
|
|
|
Children []*Node // the children of this sexp
|
|
|
|
Attributes map[string]bool // special attributes for this node
|
2014-10-13 16:14:35 -04:00
|
|
|
Original string // original line used before parsing
|
2014-08-05 16:17:40 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
var (
|
2014-08-13 06:07:41 -04:00
|
|
|
dispatch map[string]func(string) (*Node, map[string]bool, error)
|
2014-08-05 18:41:09 -04:00
|
|
|
TOKEN_WHITESPACE = regexp.MustCompile(`[\t\v\f\r ]+`)
|
2015-01-08 14:30:08 -05:00
|
|
|
TOKEN_LINE_CONTINUATION = regexp.MustCompile(`\\[ \t]*$`)
|
2014-08-05 16:17:40 -04:00
|
|
|
TOKEN_COMMENT = regexp.MustCompile(`^#.*$`)
|
|
|
|
)
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
// Dispatch Table. see line_parsers.go for the parse functions.
|
|
|
|
// The command is parsed and mapped to the line parser. The line parser
|
|
|
|
// recieves the arguments but not the command, and returns an AST after
|
|
|
|
// reformulating the arguments according to the rules in the parser
|
2015-02-03 21:47:37 -05:00
|
|
|
// functions. Errors are propagated up by Parse() and the resulting AST can
|
2014-08-05 16:17:40 -04:00
|
|
|
// be incorporated directly into the existing AST as a next.
|
2014-08-13 06:07:41 -04:00
|
|
|
dispatch = map[string]func(string) (*Node, map[string]bool, error){
|
2015-02-12 02:54:41 -05:00
|
|
|
command.User: parseString,
|
|
|
|
command.Onbuild: parseSubCommand,
|
|
|
|
command.Workdir: parseString,
|
|
|
|
command.Env: parseEnv,
|
2015-02-17 10:20:06 -05:00
|
|
|
command.Label: parseLabel,
|
2015-02-12 02:54:41 -05:00
|
|
|
command.Maintainer: parseString,
|
|
|
|
command.From: parseString,
|
|
|
|
command.Add: parseMaybeJSONToList,
|
|
|
|
command.Copy: parseMaybeJSONToList,
|
|
|
|
command.Run: parseMaybeJSON,
|
|
|
|
command.Cmd: parseMaybeJSON,
|
|
|
|
command.Entrypoint: parseMaybeJSON,
|
|
|
|
command.Expose: parseStringsWhitespaceDelimited,
|
|
|
|
command.Volume: parseMaybeJSONToList,
|
|
|
|
command.Insert: parseIgnore,
|
2014-08-05 16:17:40 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-08-07 01:56:44 -04:00
|
|
|
// parse a line and return the remainder.
|
2014-08-05 16:17:40 -04:00
|
|
|
func parseLine(line string) (string, *Node, error) {
|
|
|
|
if line = stripComments(line); line == "" {
|
|
|
|
return "", nil, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
if TOKEN_LINE_CONTINUATION.MatchString(line) {
|
|
|
|
line = TOKEN_LINE_CONTINUATION.ReplaceAllString(line, "")
|
|
|
|
return line, nil, nil
|
|
|
|
}
|
|
|
|
|
2014-10-14 23:33:11 -04:00
|
|
|
cmd, args, err := splitCommand(line)
|
|
|
|
if err != nil {
|
|
|
|
return "", nil, err
|
|
|
|
}
|
2014-08-05 16:17:40 -04:00
|
|
|
|
2014-08-10 07:01:10 -04:00
|
|
|
node := &Node{}
|
2014-08-05 16:17:40 -04:00
|
|
|
node.Value = cmd
|
|
|
|
|
2014-08-13 06:07:41 -04:00
|
|
|
sexp, attrs, err := fullDispatch(cmd, args)
|
2014-08-05 16:17:40 -04:00
|
|
|
if err != nil {
|
|
|
|
return "", nil, err
|
|
|
|
}
|
|
|
|
|
2015-01-03 00:40:43 -05:00
|
|
|
node.Next = sexp
|
2014-10-23 20:23:25 -04:00
|
|
|
node.Attributes = attrs
|
|
|
|
node.Original = line
|
|
|
|
|
2014-08-05 16:17:40 -04:00
|
|
|
return "", node, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// The main parse routine. Handles an io.ReadWriteCloser and returns the root
|
|
|
|
// of the AST.
|
|
|
|
func Parse(rwc io.Reader) (*Node, error) {
|
2014-08-10 07:01:10 -04:00
|
|
|
root := &Node{}
|
2014-08-05 16:17:40 -04:00
|
|
|
scanner := bufio.NewScanner(rwc)
|
|
|
|
|
|
|
|
for scanner.Scan() {
|
2014-10-15 04:44:14 -04:00
|
|
|
scannedLine := strings.TrimLeftFunc(scanner.Text(), unicode.IsSpace)
|
|
|
|
line, child, err := parseLine(scannedLine)
|
2014-08-05 16:17:40 -04:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if line != "" && child == nil {
|
2014-08-19 07:14:21 -04:00
|
|
|
for scanner.Scan() {
|
2014-10-02 19:58:53 -04:00
|
|
|
newline := scanner.Text()
|
2014-08-05 16:17:40 -04:00
|
|
|
|
2014-10-15 04:44:14 -04:00
|
|
|
if stripComments(strings.TrimSpace(newline)) == "" {
|
2014-08-05 16:17:40 -04:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
line, child, err = parseLine(line + newline)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if child != nil {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
2014-09-25 22:28:24 -04:00
|
|
|
if child == nil && line != "" {
|
|
|
|
line, child, err = parseLine(line)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
2014-08-05 16:17:40 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
if child != nil {
|
|
|
|
root.Children = append(root.Children, child)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return root, nil
|
|
|
|
}
|