-
Notifications
You must be signed in to change notification settings - Fork 0
/
ifnotequal.go
78 lines (62 loc) · 1.92 KB
/
ifnotequal.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
package statements
import (
"fmt"
"github.com/aisbergg/gonja/pkg/gonja/errors"
"github.com/aisbergg/gonja/pkg/gonja/parse"
)
type IfNotEqualStmt struct {
Location *parse.Token
var1, var2 parse.Expression
thenWrapper *parse.WrapperNode
elseWrapper *parse.WrapperNode
}
func (stmt *IfNotEqualStmt) Position() *parse.Token { return stmt.Location }
func (stmt *IfNotEqualStmt) String() string {
t := stmt.Position()
return fmt.Sprintf("IfNotEqualStmt(Line=%d Col=%d)", t.Line, t.Col)
}
// func (node *IfNotEqualStmt) Execute(ctx *ExecutionContext, writer TemplateWriter) *Error {
// r.Current = stmt
// r1, err := node.var1.Evaluate(ctx)
// if err != nil {
// return err
// }
// r2, err := node.var2.Evaluate(ctx)
// if err != nil {
// return err
// }
// result := !r1.EqualValueTo(r2)
// if result {
// return node.thenWrapper.Execute(ctx, writer)
// }
// if node.elseWrapper != nil {
// return node.elseWrapper.Execute(ctx, writer)
// }
// }
func ifNotEqualParser(p *parse.Parser, args *parse.Parser) parse.Statement {
ifnotequalNode := &IfNotEqualStmt{}
// Parse two expressions
ifnotequalNode.var1 = args.ParseExpression()
ifnotequalNode.var2 = args.ParseExpression()
if !args.End() {
errors.ThrowSyntaxError(args.Current().ErrorToken(), "ifequal only takes 2 args")
}
// Wrap then/else-blocks
wrapper, endargs := p.WrapUntil("else", "endifnotequal")
ifnotequalNode.thenWrapper = wrapper
if !endargs.End() {
errors.ThrowSyntaxError(endargs.Current().ErrorToken(), "arguments not allowed here")
}
if wrapper.EndTag == "else" {
// if there's an else in the if-statement, we need the else-Block as well
wrapper, endargs = p.WrapUntil("endifnotequal")
ifnotequalNode.elseWrapper = wrapper
if !endargs.End() {
errors.ThrowSyntaxError(endargs.Current().ErrorToken(), "arguments not allowed here")
}
}
return ifnotequalNode
}
func init() {
All.MustRegister("ifnotequal", ifNotEqualParser)
}