...

Source file src/github.com/noirbizarre/gonja/builtins/statements/if.go

Documentation: github.com/noirbizarre/gonja/builtins/statements

     1  package statements
     2  
     3  import (
     4  	"fmt"
     5  
     6  	log "github.com/sirupsen/logrus"
     7  
     8  	"github.com/noirbizarre/gonja/exec"
     9  	"github.com/noirbizarre/gonja/nodes"
    10  	"github.com/noirbizarre/gonja/parser"
    11  	"github.com/noirbizarre/gonja/tokens"
    12  )
    13  
    14  type IfStmt struct {
    15  	Location   *tokens.Token
    16  	conditions []nodes.Expression
    17  	wrappers   []*nodes.Wrapper
    18  }
    19  
    20  func (stmt *IfStmt) Position() *tokens.Token { return stmt.Location }
    21  func (stmt *IfStmt) String() string {
    22  	t := stmt.Position()
    23  	return fmt.Sprintf("IfStmt(Line=%d Col=%d)", t.Line, t.Col)
    24  }
    25  
    26  func (node *IfStmt) Execute(r *exec.Renderer, tag *nodes.StatementBlock) error {
    27  	for i, condition := range node.conditions {
    28  		result := r.Eval(condition)
    29  		if result.IsError() {
    30  			return result
    31  		}
    32  
    33  		if result.IsTrue() {
    34  			return r.ExecuteWrapper(node.wrappers[i])
    35  		}
    36  		// Last condition?
    37  		if len(node.conditions) == i+1 && len(node.wrappers) > i+1 {
    38  			return r.ExecuteWrapper(node.wrappers[i+1])
    39  		}
    40  	}
    41  	return nil
    42  }
    43  
    44  func ifParser(p *parser.Parser, args *parser.Parser) (nodes.Statement, error) {
    45  	log.WithFields(log.Fields{
    46  		"arg":     args.Current(),
    47  		"current": p.Current(),
    48  	}).Trace("ParseIf")
    49  	ifNode := &IfStmt{
    50  		Location: args.Current(),
    51  	}
    52  
    53  	// Parse first and main IF condition
    54  	condition, err := args.ParseExpression()
    55  	if err != nil {
    56  		return nil, err
    57  	}
    58  	ifNode.conditions = append(ifNode.conditions, condition)
    59  
    60  	if !args.End() {
    61  		return nil, args.Error("If-condition is malformed.", nil)
    62  	}
    63  
    64  	// Check the rest
    65  	for {
    66  		wrapper, tagArgs, err := p.WrapUntil("elif", "else", "endif")
    67  		if err != nil {
    68  			return nil, err
    69  		}
    70  		ifNode.wrappers = append(ifNode.wrappers, wrapper)
    71  
    72  		if wrapper.EndTag == "elif" {
    73  			// elif can take a condition
    74  			condition, err = tagArgs.ParseExpression()
    75  			if err != nil {
    76  				return nil, err
    77  			}
    78  			ifNode.conditions = append(ifNode.conditions, condition)
    79  
    80  			if !tagArgs.End() {
    81  				return nil, tagArgs.Error("Elif-condition is malformed.", nil)
    82  			}
    83  		} else {
    84  			if !tagArgs.End() {
    85  				// else/endif can't take any conditions
    86  				return nil, tagArgs.Error("Arguments not allowed here.", nil)
    87  			}
    88  		}
    89  
    90  		if wrapper.EndTag == "endif" {
    91  			break
    92  		}
    93  	}
    94  
    95  	return ifNode, nil
    96  }
    97  
    98  func init() {
    99  	All.Register("if", ifParser)
   100  }
   101  

View as plain text