...
1 package statements
2
3 import (
4 "fmt"
5
6 "github.com/noirbizarre/gonja/nodes"
7 "github.com/noirbizarre/gonja/parser"
8 "github.com/noirbizarre/gonja/tokens"
9 )
10
11 type IfEqualStmt struct {
12 Location *tokens.Token
13 var1, var2 nodes.Expression
14 thenWrapper *nodes.Wrapper
15 elseWrapper *nodes.Wrapper
16 }
17
18 func (stmt *IfEqualStmt) Position() *tokens.Token { return stmt.Location }
19 func (stmt *IfEqualStmt) String() string {
20 t := stmt.Position()
21 return fmt.Sprintf("IfEqualStmt(Line=%d Col=%d)", t.Line, t.Col)
22 }
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45 func ifEqualParser(p *parser.Parser, args *parser.Parser) (nodes.Statement, error) {
46 ifequalNode := &IfEqualStmt{}
47
48
49 var1, err := args.ParseExpression()
50 if err != nil {
51 return nil, err
52 }
53 var2, err := args.ParseExpression()
54 if err != nil {
55 return nil, err
56 }
57 ifequalNode.var1 = var1
58 ifequalNode.var2 = var2
59
60 if !args.End() {
61 return nil, args.Error("ifequal only takes 2 args.", nil)
62 }
63
64
65 wrapper, endargs, err := p.WrapUntil("else", "endifequal")
66 if err != nil {
67 return nil, err
68 }
69 ifequalNode.thenWrapper = wrapper
70
71 if !endargs.End() {
72 return nil, endargs.Error("Arguments not allowed here.", nil)
73 }
74
75 if wrapper.EndTag == "else" {
76
77 wrapper, endargs, err = p.WrapUntil("endifequal")
78 if err != nil {
79 return nil, err
80 }
81 ifequalNode.elseWrapper = wrapper
82
83 if !endargs.End() {
84 return nil, endargs.Error("Arguments not allowed here.", nil)
85 }
86 }
87
88 return ifequalNode, nil
89 }
90
91 func init() {
92 All.Register("ifequal", ifEqualParser)
93 }
94
View as plain text