-
Notifications
You must be signed in to change notification settings - Fork 0
/
parser_test.go
213 lines (171 loc) · 4.37 KB
/
parser_test.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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
/**
* @begin 2020-03-04
* @author <a href="mailto:[email protected]">Giuseppe Greco</a>
* @copyright 2020 <a href="skeeterhealth.com">Skeeter</a>
*/
package espressopp
import (
"fmt"
"strconv"
"strings"
"testing"
)
// TestParse tests the parsing of Espresso++ expressions.
func TestParse(t *testing.T) {
parser := newParser()
for _, item := range getTestDataItems() {
r := strings.NewReader(item.input)
grammar, _ := parser.parse(r)
result := emitGrammar(grammar)
if result != strings.Split(item.input, " //")[0] {
t.Errorf("Parser with input '%v' : FAILED, expected '%v' but got '%v'", item.input, item.input, result)
} else {
t.Logf("Parser with input '%v' : PASSED, expected '%v' and got '%v'", item.input, item.input, result)
}
}
}
// emitGrammars renders the expressions in g.
func emitGrammar(g *Grammar) string {
var sb strings.Builder
for _, e := range g.Expressions {
sb.WriteString(emitExpression(e))
}
return sb.String()
}
// emitExpression renders e.
func emitExpression(e *Expression) string {
var s string
if e.Op != nil {
s = fmt.Sprintf(" %s ", *e.Op)
} else if e.SubExpression != nil {
s = emitSubExpression(e.SubExpression)
} else if e.Comparison != nil {
s = emitComparison(e.Comparison)
} else if e.Equality != nil {
s = emitEquality(e.Equality)
} else if e.Range != nil {
s = emitRange(e.Range)
} else if e.Match != nil {
s = emitMatch(e.Match)
} else if e.Is != nil {
s = emitIs(e.Is)
}
return s
}
// emitSubExpression renders se.
func emitSubExpression(se *SubExpression) string {
var sb strings.Builder
if se.Not {
sb.WriteString("not ")
}
sb.WriteString("(")
for _, e := range se.Expressions {
sb.WriteString(emitExpression(e))
}
sb.WriteString(")")
return sb.String()
}
// emitComparison renders c.
func emitComparison(c *Comparison) string {
t1 := emitTermOrMath(c.TermOrMath1)
t2 := emitTermOrMath(c.TermOrMath2)
return fmt.Sprintf("%s %s %s", t1, c.Op, t2)
}
// emitEquality renders e.
func emitEquality(e *Equality) string {
t1 := emitTermOrMath(e.TermOrMath1)
t2 := emitTermOrMath(e.TermOrMath2)
return fmt.Sprintf("%s %s %s", t1, e.Op, t2)
}
// emitRange renders r.
func emitRange(r *Range) string {
t1 := emitTermOrMath(r.TermOrMath1)
t2 := emitTermOrMath(r.TermOrMath2)
t3 := emitTermOrMath(r.TermOrMath3)
return fmt.Sprintf("%s %s %s %s %s", t1, r.Between, t2, r.And, t3)
}
// emitMatch renders m.
func emitMatch(m *Match) string {
t1 := emitTerm(m.Term1)
t2 := emitTerm(m.Term2)
return fmt.Sprintf("%s %s %s", t1, m.Op, t2)
}
// emitIs renders i.
func emitIs(i *Is) string {
var sb strings.Builder
if i.IsWithExplicitValue != nil {
sb.WriteString(i.IsWithExplicitValue.Ident)
sb.WriteString(" is ")
if i.IsWithExplicitValue.Not {
sb.WriteString("not ")
}
sb.WriteString(i.IsWithExplicitValue.Value)
} else if i.IsWithImplicitValue != nil {
sb.WriteString("is ")
if i.IsWithImplicitValue.Not {
sb.WriteString("not ")
}
sb.WriteString(i.IsWithImplicitValue.Ident)
}
return sb.String()
}
// emitMacro renders m.
func emitMacro(m *Macro) string {
var sb strings.Builder
sb.WriteString(m.Name)
if m.Args != nil {
sb.WriteString("(")
var s string
for i, a := range m.Args {
s = emitTerm(a)
if i > 0 {
sb.WriteString(", ")
}
sb.WriteString(s)
}
sb.WriteString(")")
}
return sb.String()
}
// emitMath renders m.
func emitMath(m *Math) string {
t1 := emitTerm(m.Term1)
t2 := emitTerm(m.Term2)
return fmt.Sprintf("%s %s %s", t1, m.Op, t2)
}
// emitTerm renders t.
func emitTerm(t *Term) string {
var s string
if t.Identifier != nil {
s = *t.Identifier
} else if t.Integer != nil {
s = strconv.Itoa(*t.Integer)
} else if t.Decimal != nil {
s = strconv.FormatFloat(*t.Decimal, 'f', -1, 64)
} else if t.String != nil {
s = fmt.Sprintf("'%s'", *t.String)
} else if t.Date != nil {
s = fmt.Sprintf("'%s'", *t.Date)
} else if t.Time != nil {
s = fmt.Sprintf("'%s'", *t.Time)
} else if t.DateTime != nil {
s = fmt.Sprintf("'%s'", *t.DateTime)
} else if t.Bool != nil {
s = *t.Bool
} else if t.Macro != nil {
s = emitMacro(t.Macro)
}
return s
}
// emitTermOrMath renders tm.
func emitTermOrMath(tm *TermOrMath) string {
var s string
if tm.Math != nil {
s = emitMath(tm.Math)
} else if tm.SubMath != nil {
s = fmt.Sprintf("(%s)", emitMath(tm.SubMath))
} else if tm.Term != nil {
s = emitTerm(tm.Term)
}
return s
}