github.com/eikeon/docker@v1.5.0-rc4/builder/parser/utils.go (about) 1 package parser 2 3 import ( 4 "fmt" 5 "strconv" 6 "strings" 7 ) 8 9 // dumps the AST defined by `node` as a list of sexps. Returns a string 10 // suitable for printing. 11 func (node *Node) Dump() string { 12 str := "" 13 str += node.Value 14 15 for _, n := range node.Children { 16 str += "(" + n.Dump() + ")\n" 17 } 18 19 if node.Next != nil { 20 for n := node.Next; n != nil; n = n.Next { 21 if len(n.Children) > 0 { 22 str += " " + n.Dump() 23 } else { 24 str += " " + strconv.Quote(n.Value) 25 } 26 } 27 } 28 29 return strings.TrimSpace(str) 30 } 31 32 // performs the dispatch based on the two primal strings, cmd and args. Please 33 // look at the dispatch table in parser.go to see how these dispatchers work. 34 func fullDispatch(cmd, args string) (*Node, map[string]bool, error) { 35 fn := dispatch[cmd] 36 37 // Ignore invalid Dockerfile instructions 38 if fn == nil { 39 fn = parseIgnore 40 } 41 42 sexp, attrs, err := fn(args) 43 if err != nil { 44 return nil, nil, err 45 } 46 47 return sexp, attrs, nil 48 } 49 50 // splitCommand takes a single line of text and parses out the cmd and args, 51 // which are used for dispatching to more exact parsing functions. 52 func splitCommand(line string) (string, string, error) { 53 cmdline := TOKEN_WHITESPACE.Split(line, 2) 54 55 if len(cmdline) != 2 { 56 return "", "", fmt.Errorf("We do not understand this file. Please ensure it is a valid Dockerfile. Parser error at %q", line) 57 } 58 59 cmd := strings.ToLower(cmdline[0]) 60 // the cmd should never have whitespace, but it's possible for the args to 61 // have trailing whitespace. 62 return cmd, strings.TrimSpace(cmdline[1]), nil 63 } 64 65 // covers comments and empty lines. Lines should be trimmed before passing to 66 // this function. 67 func stripComments(line string) string { 68 // string is already trimmed at this point 69 if TOKEN_COMMENT.MatchString(line) { 70 return TOKEN_COMMENT.ReplaceAllString(line, "") 71 } 72 73 return line 74 }