github.com/ralexstokes/docker@v1.6.2/builder/parser/utils.go (about)

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