-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpackrat_test.go
183 lines (157 loc) · 3.09 KB
/
packrat_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
package parse
import (
"fmt"
"testing"
)
/* Left recursion:
Expression <- Expression ([+-] MultiplicativeExpression)? / MultiplicativeExpression
MultiplicativeExpression <- MultiplicativeExpression ([*%/] Atom)? / Atom
Atom <- '(' Expression ')' / Int64
*/
type BracedExpression struct {
_ string `regexp:"\\("`
Expr Expression
_ string `regexp:"\\)"`
}
type Atom struct {
FirstOf
Expr BracedExpression
Val int64
}
type Mul1 struct {
Mul MultiplicativeExpression
Arg *struct {
Op string `regexp:"[/%*]"`
Atom Atom
} `parse:"?"`
}
type MultiplicativeExpression struct {
FirstOf
Mul *Mul1
Atom Atom
}
func printMul(m *MultiplicativeExpression) {
if m.Field == "Mul" {
if m.Mul.Arg != nil {
fmt.Printf("%s(", m.Mul.Arg.Op)
printMul(&m.Mul.Mul)
if m.Mul.Arg.Atom.Field == "Val" {
fmt.Printf("%d ", m.Mul.Arg.Atom.Val)
} else {
printExpression(&m.Mul.Arg.Atom.Expr.Expr)
}
fmt.Print(")")
} else {
printMul(&m.Mul.Mul)
}
} else {
if m.Atom.Field == "Val" {
fmt.Printf("%d ", m.Atom.Val)
} else {
printExpression(&m.Atom.Expr.Expr)
}
}
}
type Expression1 struct {
Expr Expression
Arg *struct {
Op string `regexp:"[-+]"`
Mul MultiplicativeExpression
} `parse:"?"`
}
type Expression struct {
FirstOf
Expr *Expression1
Mul *MultiplicativeExpression
}
func printExpression(e *Expression) {
if e.Field == "Expr" {
if e.Expr.Arg != nil {
fmt.Printf("%s(", e.Expr.Arg.Op)
printExpression(&e.Expr.Expr)
printMul(&e.Expr.Arg.Mul)
fmt.Print(")")
} else {
printExpression(&e.Expr.Expr)
}
} else {
printMul(e.Mul)
}
}
/* Direct recursion test: */
type Rt struct {
FirstOf
A *struct {
A Rt
_ string `regexp:"-"`
N string `regexp:"[0-9]+"`
}
N string `regexp:"[0-9]+"`
}
func (r Rt) Print() {
if r.Field == "A" {
r.A.A.Print()
fmt.Print("-", r.A.N)
} else {
fmt.Print(r.N)
}
}
/* Indirect recursion test: */
type Xt struct {
E *Et
}
type Et struct {
FirstOf
M struct {
X Xt
_ string `regexp:"-"`
N string `regexp:"[0-9]+"`
}
N string `regexp:"[0-9]+"`
}
func (x Xt) Print() {
x.E.Print()
}
func (e Et) Print() {
if e.Field == "M" {
e.M.X.Print()
fmt.Print("-", e.M.N)
} else {
fmt.Print(e.N)
}
}
func TestPackrat(t *testing.T) {
params := NewOptions()
params.PackratEnabled = true
params.Debug = true
var l int
var e error
if true {
var expr Expression
l, e = Parse(&expr, []byte(" 10 + 5 - 3 * 2 % 2"), params)
fmt.Printf("New location: %d, error: %v\n", l, e)
printExpression(&expr)
println("")
}
if true {
var expr Expression
l, e = Parse(&expr, []byte("1 * 2 * 3 * 4 * 5 + 2 * 3 * 4 * 5 * 6 + 3 * 4 * 5 * 6 * 7 + 4 * 5 * 6 * 7 * 8 + 5 * 6 * 7 * 8 * 9"), params)
fmt.Printf("New location: %d, error: %v\n", l, e)
printExpression(&expr)
println("")
}
if true {
var x Xt
Parse(&x, []byte(" 1 - 2 - 3 - 4 - 5"), params)
fmt.Printf("New location: %d, error: %v\n", l, e)
x.Print()
fmt.Println("")
}
if true {
var r Rt
Parse(&r, []byte("1 - 2 - 3 - 4 - 5"), params)
fmt.Printf("New location: %d, error: %v\n", l, e)
r.Print()
fmt.Println("")
}
}