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

View as plain text