-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathparse_test.go
176 lines (149 loc) · 2.83 KB
/
parse_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
package parse
import (
"errors"
"fmt"
"io"
"testing"
)
type myEOF struct {
}
func (eof *myEOF) ParseValue(str []byte, loc int) (int, error) {
if loc < len(str) {
fmt.Printf("[ERROR] ##### EOF checker!\n")
return -1, errors.New("Waiting for end of file")
}
fmt.Printf("[OK] ##### EOF checker!\n")
return 0, nil
}
func (eof *myEOF) WriteValue(out io.Writer) error {
fmt.Printf("##### EOF writer!\n")
return nil
}
type tmp struct {
A string `regexp:"[hH]ello"`
_ string `literal:","`
_ *string `set:"SetW" parse:"?" regexp:"[wW]orld"`
Loc int `parse:"#"`
w string
EOF myEOF
}
func (t *tmp) SetW(v *string) error {
t.w = *v
fmt.Printf("SET: %s\n", t.w)
return nil
}
/* Simple arithmetic expressions grammar:
EXPR <- MUL ([+-] MUL)*
MUL <- ATOM ([/%*] ATOM)*
ATOM <- '(' EXPR ')' / NUMBER
NUMBER <- [1-9][0-9]*
*/
type bracedExpression struct {
_ string `regexp:"\\("`
Expr *expression
_ string `regexp:"\\)"`
}
type atom struct {
FirstOf
Expr bracedExpression
Number int64
}
func (a atom) p() {
if a.Field == "Number" {
print(a.Number)
} else {
a.Expr.Expr.p()
}
}
type mexpression struct {
First atom
Rest []struct {
Op string `regexp:"[*%/]"`
Arg atom
} `parse:"*"`
}
func (m mexpression) p() {
m.First.p()
print(" ")
for i := 0; i < len(m.Rest); i++ {
m.Rest[i].Arg.p()
print(" ")
print(m.Rest[i].Op)
}
}
type expression struct {
First mexpression
Rest []struct {
Op string `regexp:"[-+]"`
Arg mexpression
} `parse:"*"`
}
func (e expression) p() {
e.First.p()
print(" ")
for i := 0; i < len(e.Rest); i++ {
print(" ")
e.Rest[i].Arg.p()
print(" ")
print(e.Rest[i].Op)
}
}
/* Example from Wikipedia:
S ← &(A 'c') 'a'+ B !('a'/'b'/'c')
A ← 'a' A? 'b'
B ← 'b' B? 'c'
*/
type abcS struct {
_ struct {
A abcA
C string `regexp:"c"`
} `parse:"&"`
A []struct {
A string `regexp:"a"`
} `parse:"+"`
B abcB
_ struct {
FirstOf
A string `regexp:"a"`
B string `regexp:"b"`
C string `regexp:"c"`
} `parse:"!"`
}
type abcA struct {
A string `regexp:"a"`
A1 *abcA `parse:"?"`
B string `regexp:"b"`
}
type abcB struct {
B string `regexp:"b"`
B1 *abcB `parse:"?"`
C string `regexp:"c"`
}
func TestParse(t *testing.T) {
var x tmp
l, e := Parse(&x, []byte("Hello , \n\tworld"), nil)
if e != nil {
fmt.Printf("Error: %v\n", e)
} else {
fmt.Printf("New location: %d\n", l)
fmt.Println(x)
}
var ex expression
l, e = Parse(&ex, []byte("12 + (56 * 3) % 10"), nil)
if e != nil {
fmt.Printf("Error: %v\n", e)
} else {
fmt.Printf("New location: %d\n", l)
fmt.Println(ex)
ex.p()
println("")
}
for _, s := range []string{"aabbcc", "", "abc", "aabbc", "aabcc"} {
var g abcS
_, e = Parse(&g, []byte(s), &Options{PackratEnabled: true})
fmt.Printf("EXPR: %s\n", s)
if e != nil {
fmt.Printf("ERROR: %s\n", e.Error())
}
}
}