/
lex_test.go
171 lines (145 loc) · 3.58 KB
/
lex_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
package purple
import (
"fmt"
. "github.com/smartystreets/goconvey/convey"
"io"
"testing"
)
func TestInputStream(t *testing.T) {
Convey("With an inputstream", t, func() {
i1 := NewInputStream("01234")
v, w, err := i1.Peek(0)
So(err, ShouldBeNil)
So(w, ShouldEqual, 1)
So(v, ShouldEqual, byte('0'))
v, w, err = i1.Peek(4)
So(err, ShouldBeNil)
So(v, ShouldEqual, byte('4'))
v, w, err = i1.Peek(5)
So(err, ShouldNotBeNil)
So(v, ShouldEqual, byte(0))
for _, b := range []byte("01234") {
v, w, err := i1.Consume()
fmt.Println(string(v), string(b))
So(err, ShouldBeNil)
So(v, ShouldEqual, b)
So(w, ShouldEqual, 1)
}
// Consume from empty input stream should return a zero-width
v, w, err = i1.Consume()
So(err, ShouldNotBeNil)
So(w, ShouldEqual, 0)
So(v, ShouldEqual, byte(0))
})
}
func TestLiteral(t *testing.T) {
Convey("With an inputstream", t, func() {
i1 := NewInputStream("01234")
l1 := &Literal{"0123"}
m, err := l1.Match(i1, false)
So(m, ShouldEqual, 4)
So(err, ShouldBeNil)
b, w, err := i1.Peek(0)
So(b, ShouldEqual, '0')
So(w, ShouldEqual, 1)
So(err, ShouldBeNil)
m, err = l1.Match(i1, true)
So(m, ShouldEqual, 4)
So(err, ShouldBeNil)
b, w, err = i1.Peek(0)
fmt.Println("got [", string(b), "]")
So(b, ShouldEqual, '4')
So(w, ShouldEqual, 1)
})
}
func TestOr(t *testing.T) {
Convey("With an inputstream", t, func() {
i1 := NewInputStream("foo bar baz")
i2 := NewInputStream("blah bar baz")
i3 := NewInputStream("cow bar baz")
o1 := &Or{[]Lexeme{&Literal{"foo"}, &Literal{"blah"}}}
m, err := o1.Match(i1, false)
So(m, ShouldEqual, 3)
So(err, ShouldBeNil)
m, err = o1.Match(i2, false)
So(m, ShouldEqual, 4)
So(err, ShouldBeNil)
m, err = o1.Match(i3, false)
So(m, ShouldEqual, -1)
So(err, ShouldBeNil)
})
}
func TestThen(t *testing.T) {
Convey("With an inputstream", t, func() {
i1 := NewInputStream("foo bar baz")
i2 := NewInputStream("foo bah baz")
//i3 := NewInputStream("cow bar baz")
t1 := &Then{[]Lexeme{&Literal{"foo "}, &Literal{"bar"}}}
m, err := t1.Match(i1, true)
So(m, ShouldEqual, 7)
So(err, ShouldBeNil)
m, err = t1.Match(i2, true)
So(m, ShouldEqual, -1)
So(err, ShouldBeNil)
})
}
func TestOneOrMore(t *testing.T) {
Convey("With an inputstream", t, func() {
i1 := NewInputStream("baz123")
t1 := &OneOrMore{Alpha}
m, err := t1.Match(i1, true)
So(m, ShouldEqual, 3)
So(err, ShouldBeNil)
t2 := &OneOrMore{Digit}
m, err = t2.Match(i1, true)
So(m, ShouldEqual, -1)
})
}
func TestZeroOrMore(t *testing.T) {
Convey("With an inputstream", t, func() {
i1 := NewInputStream("baz123")
t1 := &ZeroOrMore{Alpha}
m, err := t1.Match(i1, false)
So(m, ShouldEqual, 3)
So(err, ShouldBeNil)
t2 := &ZeroOrMore{Digit}
m, err = t2.Match(i1, false)
So(err, ShouldBeNil)
So(m, ShouldEqual, 0)
})
}
func TestLexerTest(t *testing.T) {
ts := []Token{
{Literal{"("}, "LPAREN"},
{Literal{")"}, "RPAREN"},
{OneOrMore{Digit}, "number"},
{&SetLexeme{"*+-/"}, "operator"},
{ZeroOrMore{Whitespace}, "whitespace"},
}
is := NewInputStream("((1+2)/(5*9)) - 1")
outerloop:
for {
fmt.Println("isis: ", is.debug())
fmt.Println("offset is", is.offset)
v := 0
var err error
for _, t := range ts {
v, err = t.Match(is, true)
if err == io.EOF {
break outerloop
}
if err != nil {
panic(err)
}
if v <= 0 {
continue
}
fmt.Println("got token", t.Name, v)
fmt.Println("token text is", is.data[is.offset-v:is.offset])
fmt.Println("offset is now", is.offset, "\n\n")
continue outerloop
}
//is.offset += v
panic("no match")
}
}