Source file src/text/template/parse/parse.go

     1  // Copyright 2011 The Go Authors. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  // Package parse builds parse trees for templates as defined by text/template
     6  // and html/template. Clients should use those packages to construct templates
     7  // rather than this one, which provides shared internal data structures not
     8  // intended for general use.
     9  package parse
    10  
    11  import (
    12  	"bytes"
    13  	"fmt"
    14  	"runtime"
    15  	"strconv"
    16  	"strings"
    17  )
    18  
    19  // Tree is the representation of a single parsed template.
    20  type Tree struct {
    21  	Name      string    // name of the template represented by the tree.
    22  	ParseName string    // name of the top-level template during parsing, for error messages.
    23  	Root      *ListNode // top-level root of the tree.
    24  	Mode      Mode      // parsing mode.
    25  	text      string    // text parsed to create the template (or its parent)
    26  	// Parsing only; cleared after parse.
    27  	funcs      []map[string]any
    28  	lex        *lexer
    29  	token      [3]item // three-token lookahead for parser.
    30  	peekCount  int
    31  	vars       []string // variables defined at the moment.
    32  	treeSet    map[string]*Tree
    33  	actionLine int // line of left delim starting action
    34  	rangeDepth int
    35  	stackDepth int // depth of nested parenthesized expressions
    36  }
    37  
    38  // A mode value is a set of flags (or 0). Modes control parser behavior.
    39  type Mode uint
    40  
    41  const (
    42  	ParseComments Mode = 1 << iota // parse comments and add them to AST
    43  	SkipFuncCheck                  // do not check that functions are defined
    44  )
    45  
    46  // maxStackDepth is the maximum depth permitted for nested
    47  // parenthesized expressions.
    48  var maxStackDepth = 10000
    49  
    50  // init reduces maxStackDepth for WebAssembly due to its smaller stack size.
    51  func init() {
    52  	if runtime.GOARCH == "wasm" {
    53  		maxStackDepth = 1000
    54  	}
    55  }
    56  
    57  // Copy returns a copy of the [Tree]. Any parsing state is discarded.
    58  func (t *Tree) Copy() *Tree {
    59  	if t == nil {
    60  		return nil
    61  	}
    62  	return &Tree{
    63  		Name:      t.Name,
    64  		ParseName: t.ParseName,
    65  		Root:      t.Root.CopyList(),
    66  		text:      t.text,
    67  	}
    68  }
    69  
    70  // Parse returns a map from template name to [Tree], created by parsing the
    71  // templates described in the argument string. The top-level template will be
    72  // given the specified name. If an error is encountered, parsing stops and an
    73  // empty map is returned with the error.
    74  func Parse(name, text, leftDelim, rightDelim string, funcs ...map[string]any) (map[string]*Tree, error) {
    75  	treeSet := make(map[string]*Tree)
    76  	t := New(name)
    77  	t.text = text
    78  	_, err := t.Parse(text, leftDelim, rightDelim, treeSet, funcs...)
    79  	return treeSet, err
    80  }
    81  
    82  // next returns the next token.
    83  func (t *Tree) next() item {
    84  	if t.peekCount > 0 {
    85  		t.peekCount--
    86  	} else {
    87  		t.token[0] = t.lex.nextItem()
    88  	}
    89  	return t.token[t.peekCount]
    90  }
    91  
    92  // backup backs the input stream up one token.
    93  func (t *Tree) backup() {
    94  	t.peekCount++
    95  }
    96  
    97  // backup2 backs the input stream up two tokens.
    98  // The zeroth token is already there.
    99  func (t *Tree) backup2(t1 item) {
   100  	t.token[1] = t1
   101  	t.peekCount = 2
   102  }
   103  
   104  // backup3 backs the input stream up three tokens
   105  // The zeroth token is already there.
   106  func (t *Tree) backup3(t2, t1 item) { // Reverse order: we're pushing back.
   107  	t.token[1] = t1
   108  	t.token[2] = t2
   109  	t.peekCount = 3
   110  }
   111  
   112  // peek returns but does not consume the next token.
   113  func (t *Tree) peek() item {
   114  	if t.peekCount > 0 {
   115  		return t.token[t.peekCount-1]
   116  	}
   117  	t.peekCount = 1
   118  	t.token[0] = t.lex.nextItem()
   119  	return t.token[0]
   120  }
   121  
   122  // nextNonSpace returns the next non-space token.
   123  func (t *Tree) nextNonSpace() (token item) {
   124  	for {
   125  		token = t.next()
   126  		if token.typ != itemSpace {
   127  			break
   128  		}
   129  	}
   130  	return token
   131  }
   132  
   133  // peekNonSpace returns but does not consume the next non-space token.
   134  func (t *Tree) peekNonSpace() item {
   135  	token := t.nextNonSpace()
   136  	t.backup()
   137  	return token
   138  }
   139  
   140  // Parsing.
   141  
   142  // New allocates a new parse tree with the given name.
   143  func New(name string, funcs ...map[string]any) *Tree {
   144  	return &Tree{
   145  		Name:  name,
   146  		funcs: funcs,
   147  	}
   148  }
   149  
   150  // ErrorContext returns a textual representation of the location of the node in the input text.
   151  // The receiver is only used when the node does not have a pointer to the tree inside,
   152  // which can occur in old code.
   153  func (t *Tree) ErrorContext(n Node) (location, context string) {
   154  	pos := int(n.Position())
   155  	tree := n.tree()
   156  	if tree == nil {
   157  		tree = t
   158  	}
   159  	text := tree.text[:pos]
   160  	byteNum := strings.LastIndex(text, "\n")
   161  	if byteNum == -1 {
   162  		byteNum = pos // On first line.
   163  	} else {
   164  		byteNum++ // After the newline.
   165  		byteNum = pos - byteNum
   166  	}
   167  	lineNum := 1 + strings.Count(text, "\n")
   168  	context = n.String()
   169  	return fmt.Sprintf("%s:%d:%d", tree.ParseName, lineNum, byteNum), context
   170  }
   171  
   172  // errorf formats the error and terminates processing.
   173  func (t *Tree) errorf(format string, args ...any) {
   174  	t.Root = nil
   175  	format = fmt.Sprintf("template: %s:%d: %s", t.ParseName, t.token[0].line, format)
   176  	panic(fmt.Errorf(format, args...))
   177  }
   178  
   179  // error terminates processing.
   180  func (t *Tree) error(err error) {
   181  	t.errorf("%s", err)
   182  }
   183  
   184  // expect consumes the next token and guarantees it has the required type.
   185  func (t *Tree) expect(expected itemType, context string) item {
   186  	token := t.nextNonSpace()
   187  	if token.typ != expected {
   188  		t.unexpected(token, context)
   189  	}
   190  	return token
   191  }
   192  
   193  // expectOneOf consumes the next token and guarantees it has one of the required types.
   194  func (t *Tree) expectOneOf(expected1, expected2 itemType, context string) item {
   195  	token := t.nextNonSpace()
   196  	if token.typ != expected1 && token.typ != expected2 {
   197  		t.unexpected(token, context)
   198  	}
   199  	return token
   200  }
   201  
   202  // unexpected complains about the token and terminates processing.
   203  func (t *Tree) unexpected(token item, context string) {
   204  	if token.typ == itemError {
   205  		extra := ""
   206  		if t.actionLine != 0 && t.actionLine != token.line {
   207  			extra = fmt.Sprintf(" in action started at %s:%d", t.ParseName, t.actionLine)
   208  			if strings.HasSuffix(token.val, " action") {
   209  				extra = extra[len(" in action"):] // avoid "action in action"
   210  			}
   211  		}
   212  		t.errorf("%s%s", token, extra)
   213  	}
   214  	t.errorf("unexpected %s in %s", token, context)
   215  }
   216  
   217  // recover is the handler that turns panics into returns from the top level of Parse.
   218  func (t *Tree) recover(errp *error) {
   219  	e := recover()
   220  	if e != nil {
   221  		if _, ok := e.(runtime.Error); ok {
   222  			panic(e)
   223  		}
   224  		if t != nil {
   225  			t.stopParse()
   226  		}
   227  		*errp = e.(error)
   228  	}
   229  }
   230  
   231  // startParse initializes the parser, using the lexer.
   232  func (t *Tree) startParse(funcs []map[string]any, lex *lexer, treeSet map[string]*Tree) {
   233  	t.Root = nil
   234  	t.lex = lex
   235  	t.vars = []string{"$"}
   236  	t.funcs = funcs
   237  	t.treeSet = treeSet
   238  	t.stackDepth = 0
   239  	lex.options = lexOptions{
   240  		emitComment: t.Mode&ParseComments != 0,
   241  		breakOK:     !t.hasFunction("break"),
   242  		continueOK:  !t.hasFunction("continue"),
   243  	}
   244  }
   245  
   246  // stopParse terminates parsing.
   247  func (t *Tree) stopParse() {
   248  	t.lex = nil
   249  	t.vars = nil
   250  	t.funcs = nil
   251  	t.treeSet = nil
   252  }
   253  
   254  // Parse parses the template definition string to construct a representation of
   255  // the template for execution. If either action delimiter string is empty, the
   256  // default ("{{" or "}}") is used. Embedded template definitions are added to
   257  // the treeSet map.
   258  func (t *Tree) Parse(text, leftDelim, rightDelim string, treeSet map[string]*Tree, funcs ...map[string]any) (tree *Tree, err error) {
   259  	defer t.recover(&err)
   260  	t.ParseName = t.Name
   261  	lexer := lex(t.Name, text, leftDelim, rightDelim)
   262  	t.startParse(funcs, lexer, treeSet)
   263  	t.text = text
   264  	t.parse()
   265  	t.add()
   266  	t.stopParse()
   267  	return t, nil
   268  }
   269  
   270  // add adds tree to t.treeSet.
   271  func (t *Tree) add() {
   272  	tree := t.treeSet[t.Name]
   273  	if tree == nil || IsEmptyTree(tree.Root) {
   274  		t.treeSet[t.Name] = t
   275  		return
   276  	}
   277  	if !IsEmptyTree(t.Root) {
   278  		t.errorf("template: multiple definition of template %q", t.Name)
   279  	}
   280  }
   281  
   282  // IsEmptyTree reports whether this tree (node) is empty of everything but space or comments.
   283  func IsEmptyTree(n Node) bool {
   284  	switch n := n.(type) {
   285  	case nil:
   286  		return true
   287  	case *ActionNode:
   288  	case *CommentNode:
   289  		return true
   290  	case *IfNode:
   291  	case *ListNode:
   292  		for _, node := range n.Nodes {
   293  			if !IsEmptyTree(node) {
   294  				return false
   295  			}
   296  		}
   297  		return true
   298  	case *RangeNode:
   299  	case *TemplateNode:
   300  	case *TextNode:
   301  		return len(bytes.TrimSpace(n.Text)) == 0
   302  	case *WithNode:
   303  	default:
   304  		panic("unknown node: " + n.String())
   305  	}
   306  	return false
   307  }
   308  
   309  // parse is the top-level parser for a template, essentially the same
   310  // as itemList except it also parses {{define}} actions.
   311  // It runs to EOF.
   312  func (t *Tree) parse() {
   313  	t.Root = t.newList(t.peek().pos)
   314  	for t.peek().typ != itemEOF {
   315  		if t.peek().typ == itemLeftDelim {
   316  			delim := t.next()
   317  			if t.nextNonSpace().typ == itemDefine {
   318  				newT := New("definition") // name will be updated once we know it.
   319  				newT.text = t.text
   320  				newT.Mode = t.Mode
   321  				newT.ParseName = t.ParseName
   322  				newT.startParse(t.funcs, t.lex, t.treeSet)
   323  				newT.parseDefinition()
   324  				continue
   325  			}
   326  			t.backup2(delim)
   327  		}
   328  		switch n := t.textOrAction(); n.Type() {
   329  		case nodeEnd, nodeElse:
   330  			t.errorf("unexpected %s", n)
   331  		default:
   332  			t.Root.append(n)
   333  		}
   334  	}
   335  }
   336  
   337  // parseDefinition parses a {{define}} ...  {{end}} template definition and
   338  // installs the definition in t.treeSet. The "define" keyword has already
   339  // been scanned.
   340  func (t *Tree) parseDefinition() {
   341  	const context = "define clause"
   342  	name := t.expectOneOf(itemString, itemRawString, context)
   343  	var err error
   344  	t.Name, err = strconv.Unquote(name.val)
   345  	if err != nil {
   346  		t.error(err)
   347  	}
   348  	t.expect(itemRightDelim, context)
   349  	var end Node
   350  	t.Root, end = t.itemList()
   351  	if end.Type() != nodeEnd {
   352  		t.errorf("unexpected %s in %s", end, context)
   353  	}
   354  	t.add()
   355  	t.stopParse()
   356  }
   357  
   358  // itemList:
   359  //
   360  //	textOrAction*
   361  //
   362  // Terminates at {{end}} or {{else}}, returned separately.
   363  func (t *Tree) itemList() (list *ListNode, next Node) {
   364  	list = t.newList(t.peekNonSpace().pos)
   365  	for t.peekNonSpace().typ != itemEOF {
   366  		n := t.textOrAction()
   367  		switch n.Type() {
   368  		case nodeEnd, nodeElse:
   369  			return list, n
   370  		}
   371  		list.append(n)
   372  	}
   373  	t.errorf("unexpected EOF")
   374  	return
   375  }
   376  
   377  // textOrAction:
   378  //
   379  //	text | comment | action
   380  func (t *Tree) textOrAction() Node {
   381  	switch token := t.nextNonSpace(); token.typ {
   382  	case itemText:
   383  		return t.newText(token.pos, token.val)
   384  	case itemLeftDelim:
   385  		t.actionLine = token.line
   386  		defer t.clearActionLine()
   387  		return t.action()
   388  	case itemComment:
   389  		return t.newComment(token.pos, token.val)
   390  	default:
   391  		t.unexpected(token, "input")
   392  	}
   393  	return nil
   394  }
   395  
   396  func (t *Tree) clearActionLine() {
   397  	t.actionLine = 0
   398  }
   399  
   400  // Action:
   401  //
   402  //	control
   403  //	command ("|" command)*
   404  //
   405  // Left delim is past. Now get actions.
   406  // First word could be a keyword such as range.
   407  func (t *Tree) action() (n Node) {
   408  	switch token := t.nextNonSpace(); token.typ {
   409  	case itemBlock:
   410  		return t.blockControl()
   411  	case itemBreak:
   412  		return t.breakControl(token.pos, token.line)
   413  	case itemContinue:
   414  		return t.continueControl(token.pos, token.line)
   415  	case itemElse:
   416  		return t.elseControl()
   417  	case itemEnd:
   418  		return t.endControl()
   419  	case itemIf:
   420  		return t.ifControl()
   421  	case itemRange:
   422  		return t.rangeControl()
   423  	case itemTemplate:
   424  		return t.templateControl()
   425  	case itemWith:
   426  		return t.withControl()
   427  	}
   428  	t.backup()
   429  	token := t.peek()
   430  	// Do not pop variables; they persist until "end".
   431  	return t.newAction(token.pos, token.line, t.pipeline("command", itemRightDelim))
   432  }
   433  
   434  // Break:
   435  //
   436  //	{{break}}
   437  //
   438  // Break keyword is past.
   439  func (t *Tree) breakControl(pos Pos, line int) Node {
   440  	if token := t.nextNonSpace(); token.typ != itemRightDelim {
   441  		t.unexpected(token, "{{break}}")
   442  	}
   443  	if t.rangeDepth == 0 {
   444  		t.errorf("{{break}} outside {{range}}")
   445  	}
   446  	return t.newBreak(pos, line)
   447  }
   448  
   449  // Continue:
   450  //
   451  //	{{continue}}
   452  //
   453  // Continue keyword is past.
   454  func (t *Tree) continueControl(pos Pos, line int) Node {
   455  	if token := t.nextNonSpace(); token.typ != itemRightDelim {
   456  		t.unexpected(token, "{{continue}}")
   457  	}
   458  	if t.rangeDepth == 0 {
   459  		t.errorf("{{continue}} outside {{range}}")
   460  	}
   461  	return t.newContinue(pos, line)
   462  }
   463  
   464  // Pipeline:
   465  //
   466  //	declarations? command ('|' command)*
   467  func (t *Tree) pipeline(context string, end itemType) (pipe *PipeNode) {
   468  	token := t.peekNonSpace()
   469  	pipe = t.newPipeline(token.pos, token.line, nil)
   470  	// Are there declarations or assignments?
   471  decls:
   472  	if v := t.peekNonSpace(); v.typ == itemVariable {
   473  		t.next()
   474  		// Since space is a token, we need 3-token look-ahead here in the worst case:
   475  		// in "$x foo" we need to read "foo" (as opposed to ":=") to know that $x is an
   476  		// argument variable rather than a declaration. So remember the token
   477  		// adjacent to the variable so we can push it back if necessary.
   478  		tokenAfterVariable := t.peek()
   479  		next := t.peekNonSpace()
   480  		switch {
   481  		case next.typ == itemAssign, next.typ == itemDeclare:
   482  			pipe.IsAssign = next.typ == itemAssign
   483  			t.nextNonSpace()
   484  			pipe.Decl = append(pipe.Decl, t.newVariable(v.pos, v.val))
   485  			t.vars = append(t.vars, v.val)
   486  		case next.typ == itemChar && next.val == ",":
   487  			t.nextNonSpace()
   488  			pipe.Decl = append(pipe.Decl, t.newVariable(v.pos, v.val))
   489  			t.vars = append(t.vars, v.val)
   490  			if context == "range" && len(pipe.Decl) < 2 {
   491  				switch t.peekNonSpace().typ {
   492  				case itemVariable, itemRightDelim, itemRightParen:
   493  					// second initialized variable in a range pipeline
   494  					goto decls
   495  				default:
   496  					t.errorf("range can only initialize variables")
   497  				}
   498  			}
   499  			t.errorf("too many declarations in %s", context)
   500  		case tokenAfterVariable.typ == itemSpace:
   501  			t.backup3(v, tokenAfterVariable)
   502  		default:
   503  			t.backup2(v)
   504  		}
   505  	}
   506  	for {
   507  		switch token := t.nextNonSpace(); token.typ {
   508  		case end:
   509  			// At this point, the pipeline is complete
   510  			t.checkPipeline(pipe, context)
   511  			return
   512  		case itemBool, itemCharConstant, itemComplex, itemDot, itemField, itemIdentifier,
   513  			itemNumber, itemNil, itemRawString, itemString, itemVariable, itemLeftParen:
   514  			t.backup()
   515  			pipe.append(t.command())
   516  		default:
   517  			t.unexpected(token, context)
   518  		}
   519  	}
   520  }
   521  
   522  func (t *Tree) checkPipeline(pipe *PipeNode, context string) {
   523  	// Reject empty pipelines
   524  	if len(pipe.Cmds) == 0 {
   525  		t.errorf("missing value for %s", context)
   526  	}
   527  	// Only the first command of a pipeline can start with a non executable operand
   528  	for i, c := range pipe.Cmds[1:] {
   529  		switch c.Args[0].Type() {
   530  		case NodeBool, NodeDot, NodeNil, NodeNumber, NodeString:
   531  			// With A|B|C, pipeline stage 2 is B
   532  			t.errorf("non executable command in pipeline stage %d", i+2)
   533  		}
   534  	}
   535  }
   536  
   537  func (t *Tree) parseControl(context string) (pos Pos, line int, pipe *PipeNode, list, elseList *ListNode) {
   538  	defer t.popVars(len(t.vars))
   539  	pipe = t.pipeline(context, itemRightDelim)
   540  	if context == "range" {
   541  		t.rangeDepth++
   542  	}
   543  	var next Node
   544  	list, next = t.itemList()
   545  	if context == "range" {
   546  		t.rangeDepth--
   547  	}
   548  	switch next.Type() {
   549  	case nodeEnd: //done
   550  	case nodeElse:
   551  		// Special case for "else if" and "else with".
   552  		// If the "else" is followed immediately by an "if" or "with",
   553  		// the elseControl will have left the "if" or "with" token pending. Treat
   554  		//	{{if a}}_{{else if b}}_{{end}}
   555  		//  {{with a}}_{{else with b}}_{{end}}
   556  		// as
   557  		//	{{if a}}_{{else}}{{if b}}_{{end}}{{end}}
   558  		//  {{with a}}_{{else}}{{with b}}_{{end}}{{end}}.
   559  		// To do this, parse the "if" or "with" as usual and stop at it {{end}};
   560  		// the subsequent{{end}} is assumed. This technique works even for long if-else-if chains.
   561  		if context == "if" && t.peek().typ == itemIf {
   562  			t.next() // Consume the "if" token.
   563  			elseList = t.newList(next.Position())
   564  			elseList.append(t.ifControl())
   565  		} else if context == "with" && t.peek().typ == itemWith {
   566  			t.next()
   567  			elseList = t.newList(next.Position())
   568  			elseList.append(t.withControl())
   569  		} else {
   570  			elseList, next = t.itemList()
   571  			if next.Type() != nodeEnd {
   572  				t.errorf("expected end; found %s", next)
   573  			}
   574  		}
   575  	}
   576  	return pipe.Position(), pipe.Line, pipe, list, elseList
   577  }
   578  
   579  // If:
   580  //
   581  //	{{if pipeline}} itemList {{end}}
   582  //	{{if pipeline}} itemList {{else}} itemList {{end}}
   583  //
   584  // If keyword is past.
   585  func (t *Tree) ifControl() Node {
   586  	return t.newIf(t.parseControl("if"))
   587  }
   588  
   589  // Range:
   590  //
   591  //	{{range pipeline}} itemList {{end}}
   592  //	{{range pipeline}} itemList {{else}} itemList {{end}}
   593  //
   594  // Range keyword is past.
   595  func (t *Tree) rangeControl() Node {
   596  	r := t.newRange(t.parseControl("range"))
   597  	return r
   598  }
   599  
   600  // With:
   601  //
   602  //	{{with pipeline}} itemList {{end}}
   603  //	{{with pipeline}} itemList {{else}} itemList {{end}}
   604  //
   605  // If keyword is past.
   606  func (t *Tree) withControl() Node {
   607  	return t.newWith(t.parseControl("with"))
   608  }
   609  
   610  // End:
   611  //
   612  //	{{end}}
   613  //
   614  // End keyword is past.
   615  func (t *Tree) endControl() Node {
   616  	return t.newEnd(t.expect(itemRightDelim, "end").pos)
   617  }
   618  
   619  // Else:
   620  //
   621  //	{{else}}
   622  //
   623  // Else keyword is past.
   624  func (t *Tree) elseControl() Node {
   625  	peek := t.peekNonSpace()
   626  	// The "{{else if ... " and "{{else with ..." will be
   627  	// treated as "{{else}}{{if ..." and "{{else}}{{with ...".
   628  	// So return the else node here.
   629  	if peek.typ == itemIf || peek.typ == itemWith {
   630  		return t.newElse(peek.pos, peek.line)
   631  	}
   632  	token := t.expect(itemRightDelim, "else")
   633  	return t.newElse(token.pos, token.line)
   634  }
   635  
   636  // Block:
   637  //
   638  //	{{block stringValue pipeline}}
   639  //
   640  // Block keyword is past.
   641  // The name must be something that can evaluate to a string.
   642  // The pipeline is mandatory.
   643  func (t *Tree) blockControl() Node {
   644  	const context = "block clause"
   645  
   646  	token := t.nextNonSpace()
   647  	name := t.parseTemplateName(token, context)
   648  	pipe := t.pipeline(context, itemRightDelim)
   649  
   650  	block := New(name) // name will be updated once we know it.
   651  	block.text = t.text
   652  	block.Mode = t.Mode
   653  	block.ParseName = t.ParseName
   654  	block.startParse(t.funcs, t.lex, t.treeSet)
   655  	var end Node
   656  	block.Root, end = block.itemList()
   657  	if end.Type() != nodeEnd {
   658  		t.errorf("unexpected %s in %s", end, context)
   659  	}
   660  	block.add()
   661  	block.stopParse()
   662  
   663  	return t.newTemplate(token.pos, token.line, name, pipe)
   664  }
   665  
   666  // Template:
   667  //
   668  //	{{template stringValue pipeline}}
   669  //
   670  // Template keyword is past. The name must be something that can evaluate
   671  // to a string.
   672  func (t *Tree) templateControl() Node {
   673  	const context = "template clause"
   674  	token := t.nextNonSpace()
   675  	name := t.parseTemplateName(token, context)
   676  	var pipe *PipeNode
   677  	if t.nextNonSpace().typ != itemRightDelim {
   678  		t.backup()
   679  		// Do not pop variables; they persist until "end".
   680  		pipe = t.pipeline(context, itemRightDelim)
   681  	}
   682  	return t.newTemplate(token.pos, token.line, name, pipe)
   683  }
   684  
   685  func (t *Tree) parseTemplateName(token item, context string) (name string) {
   686  	switch token.typ {
   687  	case itemString, itemRawString:
   688  		s, err := strconv.Unquote(token.val)
   689  		if err != nil {
   690  			t.error(err)
   691  		}
   692  		name = s
   693  	default:
   694  		t.unexpected(token, context)
   695  	}
   696  	return
   697  }
   698  
   699  // command:
   700  //
   701  //	operand (space operand)*
   702  //
   703  // space-separated arguments up to a pipeline character or right delimiter.
   704  // we consume the pipe character but leave the right delim to terminate the action.
   705  func (t *Tree) command() *CommandNode {
   706  	cmd := t.newCommand(t.peekNonSpace().pos)
   707  	for {
   708  		t.peekNonSpace() // skip leading spaces.
   709  		operand := t.operand()
   710  		if operand != nil {
   711  			cmd.append(operand)
   712  		}
   713  		switch token := t.next(); token.typ {
   714  		case itemSpace:
   715  			continue
   716  		case itemRightDelim, itemRightParen:
   717  			t.backup()
   718  		case itemPipe:
   719  			// nothing here; break loop below
   720  		default:
   721  			t.unexpected(token, "operand")
   722  		}
   723  		break
   724  	}
   725  	if len(cmd.Args) == 0 {
   726  		t.errorf("empty command")
   727  	}
   728  	return cmd
   729  }
   730  
   731  // operand:
   732  //
   733  //	term .Field*
   734  //
   735  // An operand is a space-separated component of a command,
   736  // a term possibly followed by field accesses.
   737  // A nil return means the next item is not an operand.
   738  func (t *Tree) operand() Node {
   739  	node := t.term()
   740  	if node == nil {
   741  		return nil
   742  	}
   743  	if t.peek().typ == itemField {
   744  		chain := t.newChain(t.peek().pos, node)
   745  		for t.peek().typ == itemField {
   746  			chain.Add(t.next().val)
   747  		}
   748  		// Compatibility with original API: If the term is of type NodeField
   749  		// or NodeVariable, just put more fields on the original.
   750  		// Otherwise, keep the Chain node.
   751  		// Obvious parsing errors involving literal values are detected here.
   752  		// More complex error cases will have to be handled at execution time.
   753  		switch node.Type() {
   754  		case NodeField:
   755  			node = t.newField(chain.Position(), chain.String())
   756  		case NodeVariable:
   757  			node = t.newVariable(chain.Position(), chain.String())
   758  		case NodeBool, NodeString, NodeNumber, NodeNil, NodeDot:
   759  			t.errorf("unexpected . after term %q", node.String())
   760  		default:
   761  			node = chain
   762  		}
   763  	}
   764  	return node
   765  }
   766  
   767  // term:
   768  //
   769  //	literal (number, string, nil, boolean)
   770  //	function (identifier)
   771  //	.
   772  //	.Field
   773  //	$
   774  //	'(' pipeline ')'
   775  //
   776  // A term is a simple "expression".
   777  // A nil return means the next item is not a term.
   778  func (t *Tree) term() Node {
   779  	switch token := t.nextNonSpace(); token.typ {
   780  	case itemIdentifier:
   781  		checkFunc := t.Mode&SkipFuncCheck == 0
   782  		if checkFunc && !t.hasFunction(token.val) {
   783  			t.errorf("function %q not defined", token.val)
   784  		}
   785  		return NewIdentifier(token.val).SetTree(t).SetPos(token.pos)
   786  	case itemDot:
   787  		return t.newDot(token.pos)
   788  	case itemNil:
   789  		return t.newNil(token.pos)
   790  	case itemVariable:
   791  		return t.useVar(token.pos, token.val)
   792  	case itemField:
   793  		return t.newField(token.pos, token.val)
   794  	case itemBool:
   795  		return t.newBool(token.pos, token.val == "true")
   796  	case itemCharConstant, itemComplex, itemNumber:
   797  		number, err := t.newNumber(token.pos, token.val, token.typ)
   798  		if err != nil {
   799  			t.error(err)
   800  		}
   801  		return number
   802  	case itemLeftParen:
   803  		if t.stackDepth >= maxStackDepth {
   804  			t.errorf("max expression depth exceeded")
   805  		}
   806  		t.stackDepth++
   807  		defer func() { t.stackDepth-- }()
   808  		return t.pipeline("parenthesized pipeline", itemRightParen)
   809  	case itemString, itemRawString:
   810  		s, err := strconv.Unquote(token.val)
   811  		if err != nil {
   812  			t.error(err)
   813  		}
   814  		return t.newString(token.pos, token.val, s)
   815  	}
   816  	t.backup()
   817  	return nil
   818  }
   819  
   820  // hasFunction reports if a function name exists in the Tree's maps.
   821  func (t *Tree) hasFunction(name string) bool {
   822  	for _, funcMap := range t.funcs {
   823  		if funcMap == nil {
   824  			continue
   825  		}
   826  		if funcMap[name] != nil {
   827  			return true
   828  		}
   829  	}
   830  	return false
   831  }
   832  
   833  // popVars trims the variable list to the specified length
   834  func (t *Tree) popVars(n int) {
   835  	t.vars = t.vars[:n]
   836  }
   837  
   838  // useVar returns a node for a variable reference. It errors if the
   839  // variable is not defined.
   840  func (t *Tree) useVar(pos Pos, name string) Node {
   841  	v := t.newVariable(pos, name)
   842  	for _, varName := range t.vars {
   843  		if varName == v.Ident[0] {
   844  			return v
   845  		}
   846  	}
   847  	t.errorf("undefined variable %q", v.Ident[0])
   848  	return nil
   849  }
   850  

View as plain text