-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathparser_test.go
196 lines (178 loc) · 3.55 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
package parser
import (
"math"
"reflect"
"strconv"
"strings"
"testing"
)
type Node interface {
Value() int
}
type BinOp struct {
Op1 Node
Op string
Op2 Node
}
func (bo BinOp) Value() int {
switch bo.Op {
case "+":
return bo.Op1.Value() + bo.Op2.Value()
case "-":
return bo.Op1.Value() - bo.Op2.Value()
case "*":
return bo.Op1.Value() * bo.Op2.Value()
case "/":
return bo.Op1.Value() / bo.Op2.Value()
case "^":
return int(math.Pow(float64(bo.Op1.Value()), float64(bo.Op2.Value())))
}
panic("Unknown operator")
}
type Num int
func (n Num) Value() int {
return int(n)
}
func ParseParen(sr StatefulReader) (Node, error) {
_, err := Lit("(")(sr)
if err != nil {
return nil, err
}
n, err := ParseExpr(sr)
if err != nil {
return nil, err
}
_, err = Lit(")")(sr)
if err != nil {
return nil, err
}
return n, err
}
func ParseExpr(sr StatefulReader) (Node, error) {
return ParseBinOp(
ParseBinOp(
ParseBinOp(
Or(ParseParen, ParseNum),
"^",
),
"*", "/",
),
"+", "-",
)(sr)
}
func ParseBinOp(opType func(StatefulReader) (Node, error), ops ...string) func(StatefulReader) (Node, error) {
parseOps := []func(StatefulReader) (string, error){}
for _, op := range ops {
parseOps = append(parseOps, Lit(op))
}
return func(sr StatefulReader) (Node, error) {
n, err := opType(sr)
if err != nil {
return nil, err
}
for {
s := sr.State()
op, err := Or(parseOps...)(sr)
if err != nil {
sr.Restore(s)
break
}
n2, err := opType(sr)
if err != nil {
sr.Restore(s)
break
}
n = BinOp{Op1: n, Op: op, Op2: n2}
}
return n, nil
}
}
var ParseNum = Convert(And(Optional(Lit("-")), Convert(Mult(1, 0, Set("0-9")), func(s []string) (string, error) {
return strings.Join(s, ""), nil
})), func(s []string) (Node, error) {
n, err := strconv.Atoi(strings.Join(s, ""))
if err != nil {
return nil, err
}
return Num(n), nil
})
func TestLit(t *testing.T) {
t.Parallel()
p := Lit("foo")
out, err := parse("foo", p)
if err != nil {
t.Error(err)
}
if out != "foo" {
t.Errorf("Expected %s, got %s", "foo", out)
}
}
func TestMult(t *testing.T) {
t.Parallel()
p := Mult(0, 3, Lit("foo"))
out, err := parse("foo", p)
if err != nil {
t.Error(err)
}
assert(t, out, []string{"foo"})
out, err = parse("foofoofoo", p)
if err != nil {
t.Error(err)
}
assert(t, out, []string{"foo", "foo", "foo"})
}
func TestMultOr(t *testing.T) {
t.Parallel()
p := Mult(0, 3, Or(Lit("bar"), Lit("foo")))
out, err := parse("foo", p)
if err != nil {
t.Error(err)
}
assert(t, out, []string{"foo"})
out, err = parse("foobarfoo", p)
if err != nil {
t.Error(err)
}
assert(t, out, []string{"foo", "bar", "foo"})
}
func TestExpr(t *testing.T) {
t.Parallel()
tests := []struct {
in string
out int
}{
{"1+2", 3},
{"1+2*3", 7},
{"1+2*3-4", 3},
{"1+2*3-4*5", -13},
{"2-1", 1},
{"(1+2)*3", 9},
{"(1+2)*(3-4)", -3},
{"2^3", 8},
{"1+2^2", 5},
{"1+2^2+1", 6},
{"(1+2)^2", 9},
}
for _, test := range tests {
out, err := parse(test.in, ParseExpr)
if err != nil {
t.Error(err)
}
assertSrc(t, out, out.Value(), test.out)
}
}
func assert[T any](t *testing.T, got, expected T) {
t.Helper()
if !reflect.DeepEqual(got, expected) {
t.Errorf("Expected %v, got %v", expected, got)
}
}
func assertSrc[T any](t *testing.T, src any, got, expected T) {
t.Helper()
if !reflect.DeepEqual(got, expected) {
t.Errorf("Expected (%v) %v, got %v", src, expected, got)
}
}
func parse[T any](s string, p func(StatefulReader) (T, error)) (T, error) {
return p(SimpleReader{strings.NewReader(s)})
}