...

Source file src/github.com/noirbizarre/gonja/parser/expression.go

Documentation: github.com/noirbizarre/gonja/parser

     1  package parser
     2  
     3  import (
     4  	"github.com/noirbizarre/gonja/nodes"
     5  	"github.com/noirbizarre/gonja/tokens"
     6  	log "github.com/sirupsen/logrus"
     7  )
     8  
     9  // ParseFilterExpression parses an optionnal filter chain for a node
    10  func (p *Parser) ParseFilterExpression(expr nodes.Expression) (nodes.Expression, error) {
    11  	log.WithFields(log.Fields{
    12  		"current": p.Current(),
    13  	}).Trace("ParseFilterExpression")
    14  
    15  	if p.Peek(tokens.Pipe) != nil {
    16  
    17  		filtered := &nodes.FilteredExpression{
    18  			Expression: expr,
    19  		}
    20  		for p.Match(tokens.Pipe) != nil {
    21  			// Parse one single filter
    22  			filter, err := p.ParseFilter()
    23  			if err != nil {
    24  				return nil, err
    25  			}
    26  
    27  			// Check sandbox filter restriction
    28  			// if _, isBanned := p.template.set.bannedFilters[filter.name]; isBanned {
    29  			// 	return nil, p.Error(fmt.Sprintf("Usage of filter '%s' is not allowed (sandbox restriction active).", filter.name), nil)
    30  			// }
    31  
    32  			filtered.Filters = append(filtered.Filters, filter)
    33  		}
    34  		expr = filtered
    35  	}
    36  
    37  	log.WithFields(log.Fields{
    38  		"expr": expr,
    39  	}).Trace("ParseFilterExpression return")
    40  	return expr, nil
    41  }
    42  
    43  // ParseExpression parses an expression with optionnal filters
    44  // Nested expression shoulds call this method
    45  func (p *Parser) ParseExpression() (nodes.Expression, error) {
    46  	log.WithFields(log.Fields{
    47  		"current": p.Current(),
    48  	}).Trace("ParseExpression")
    49  	var expr nodes.Expression
    50  
    51  	expr, err := p.ParseLogicalExpression()
    52  	if err != nil {
    53  		return nil, err
    54  	}
    55  
    56  	expr, err = p.ParseFilterExpression(expr)
    57  	if err != nil {
    58  		return nil, err
    59  	}
    60  
    61  	log.WithFields(log.Fields{
    62  		"expr": expr,
    63  	}).Trace("ParseExpression return")
    64  	return expr, nil
    65  }
    66  
    67  func (p *Parser) ParseExpressionNode() (nodes.Node, error) {
    68  	log.WithFields(log.Fields{
    69  		"current": p.Current(),
    70  	}).Trace("ParseExpressionNode")
    71  
    72  	tok := p.Match(tokens.VariableBegin)
    73  	if tok == nil {
    74  		return nil, p.Error("'{{' expected here", p.Current())
    75  	}
    76  
    77  	node := &nodes.Output{
    78  		Start: tok,
    79  		Trim: &nodes.Trim{
    80  			Left: tok.Val[len(tok.Val)-1] == '-',
    81  		},
    82  	}
    83  
    84  	expr, err := p.ParseExpression()
    85  	if err != nil {
    86  		return nil, err
    87  	}
    88  	if expr == nil {
    89  		return nil, p.Error("Expected an expression.", p.Current())
    90  	}
    91  	node.Expression = expr
    92  
    93  	tok = p.Match(tokens.VariableEnd)
    94  	if tok == nil {
    95  		return nil, p.Error("'}}' expected here", p.Current())
    96  	}
    97  	node.End = tok
    98  	node.Trim.Right = tok.Val[0] == '-'
    99  
   100  	log.WithFields(log.Fields{
   101  		"node": node,
   102  	}).Trace("parseExpressionNode return")
   103  	return node, nil
   104  }
   105  

View as plain text