-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathast.go
109 lines (92 loc) · 2.55 KB
/
ast.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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
package parser
import (
"fmt"
"strings"
)
type Statement struct {
Fragments []Fragment `parser:"@@+"`
}
type Fragment struct {
IfStatement *IfStatement `parser:"'#{' @@ '}' "`
Variable *string `parser:"| '${' @Ident '}'"`
Literal *string `parser:"| @!(Ternary Variable)"`
}
type IfStatement struct {
Cond Expr `parser:"@@"`
Then Term `parser:"'?' @@"`
Else Term `parser:"':' @@"`
}
type Expr struct {
Left *Cond `parser:"( @@"`
Sub *Expr `parser:"| '(' @@ ')' )"`
Right []LogicExpr `parser:"@@*"`
}
type LogicExpr struct {
Operator LogicOperator `parser:"@LogicOperator"`
Cond *Cond `parser:"( @@"`
Sub *Expr `parser:"| '(' @@ ')' )"`
}
type Cond struct {
Left Term `parser:"@@"`
Operator ComparisonOperator `parser:"( @ComparisonOperator"`
Right *Term `parser:"@@ )?"`
}
type Term struct {
Variable *string `parser:"'${' @Ident '}'"`
Value *Value `parser:"| @@"`
}
type Value struct {
Number *float64 `parser:"@Number"`
String *string `parser:"| @String"`
Boolean *Boolean `parser:"| @Boolean"`
Array []Value `parser:"| '[' @@ (',' @@)* ']'"`
}
type Boolean bool
func (b *Boolean) Capture(s []string) error {
switch strings.ToUpper(s[0]) {
case "TRUE":
*b = true
case "FALSE":
*b = false
default:
return fmt.Errorf("unexpected string: %s", s[0])
}
return nil
}
type LogicOperator string
const (
AndLogicOperator LogicOperator = "AND"
OrLogicOperator LogicOperator = "OR"
)
func (operator *LogicOperator) Capture(s []string) error {
switch strings.ToUpper(s[0]) {
case "AND", "&&":
*operator = AndLogicOperator
case "OR", "||":
*operator = OrLogicOperator
default:
return fmt.Errorf("unexpected string: %s", s[0])
}
return nil
}
type ComparisonOperator string
const (
EqualComparisonOperator ComparisonOperator = "=="
NotEqualComparisonOperator ComparisonOperator = "!="
GreaterThanComparisonOperator ComparisonOperator = ">"
GreaterThanEqualComparisonOperator ComparisonOperator = ">="
LessThanComparisonOperator ComparisonOperator = "<"
LessThanEqualComparisonOperator ComparisonOperator = "<="
InComparisonOperator ComparisonOperator = "IN"
)
func comparisonOperatorKeywords() []string {
return []string{
string(EqualComparisonOperator),
string(NotEqualComparisonOperator),
string(GreaterThanComparisonOperator),
string(GreaterThanEqualComparisonOperator),
string(LessThanComparisonOperator),
string(LessThanEqualComparisonOperator),
string(InComparisonOperator),
}
}