/
parser_test.go
218 lines (185 loc) · 5.99 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
214
215
216
217
218
package main
import (
"bytes"
"testing"
"github.com/stretchr/testify/assert"
)
func newParser(s string) *Parser {
return NewParser(NewScanner(bytes.NewReader([]byte(s))))
}
func TestParser(t *testing.T) {
t.Parallel()
t.Run("Skip comment", func(t *testing.T) {
p := newParser("//\n \tfoo")
str := p.ident()
assert.Equal(t, "foo", str)
})
t.Run("Parse nil", func(t *testing.T) {
p := newParser("")
result, _ := p.Parse()
assert.Equal(t, 0, len(result.Stmts))
})
t.Run("Test parse recovery", func(t *testing.T) {
p := newParser("42")
_, err := p.Parse()
assert.NotNil(t, err)
})
t.Run("Parse identifier", func(t *testing.T) {
p := newParser("foo")
str := p.ident()
assert.Equal(t, "foo", str)
})
t.Run("Parse identifier error", func(t *testing.T) {
p := newParser("42")
assert.Panics(t, func() {
p.ident()
})
})
t.Run("Parse parenthesized term", func(t *testing.T) {
p := newParser("(42)")
node := p.term().(*AstNumberNode)
assert.Equal(t, 42.0, node.Value)
})
t.Run("Parse signed term", func(t *testing.T) {
p := newParser("-42")
node := p.term().(*AstNegativeNode)
assert.Equal(t, 42.0, node.Term.(*AstNumberNode).Value)
})
t.Run("Parse cast", func(t *testing.T) {
p := newParser("foo:string")
node := p.castExpr().(*AstCastNode)
assert.Equal(t, "string", node.Cast)
assert.Equal(t, "foo", node.Term.(*AstVariableNode).Name)
})
t.Run("Parse func call term", func(t *testing.T) {
p := newParser("foo()")
node := p.term().(*AstFuncCallNode)
assert.Equal(t, "foo", node.Name)
assert.Equal(t, 0, len(node.Args))
})
t.Run("Parse term func call with args", func(t *testing.T) {
p := newParser("foo(bar, 42, \"baz\")")
node := p.term().(*AstFuncCallNode)
assert.Equal(t, "foo", node.Name)
assert.Equal(t, "bar", node.Args[0].(*AstVariableNode).Name)
assert.Equal(t, 42.0, node.Args[1].(*AstNumberNode).Value)
assert.Equal(t, "baz", node.Args[2].(*AstStringNode).Value)
})
t.Run("Parse empty braced statement list", func(t *testing.T) {
p := newParser("{}")
node := p.braceStmtList()
assert.Equal(t, 0, len(node))
})
t.Run("Parse braced statement list", func(t *testing.T) {
p := newParser("{foo := 42 bar := 73}")
node := p.braceStmtList()
assert.Equal(t, "foo", node[0].(*AstAssignNode).Name)
assert.Equal(t, 42.0, node[0].(*AstAssignNode).Expr.(*AstNumberNode).Value)
assert.Equal(t, "bar", node[1].(*AstAssignNode).Name)
assert.Equal(t, 73.0, node[1].(*AstAssignNode).Expr.(*AstNumberNode).Value)
})
t.Run("Parse braced statement list with statement error", func(t *testing.T) {
p := newParser("{foo := 42\n bar 73")
assert.Panics(t, func() {
p.braceStmtList()
})
})
t.Run("Parse braced statement list with brace error", func(t *testing.T) {
p := newParser("{foo := 42\n")
assert.Panics(t, func() {
p.braceStmtList()
})
})
t.Run("Parse function def", func(t *testing.T) {
p := newParser("func foo {}")
node := p.stmt().(*AstFuncDefNode)
assert.Equal(t, "foo", node.Name)
assert.Equal(t, 0, len(node.Args))
assert.Equal(t, 0, len(node.Body))
})
t.Run("Parse function def with one parameter", func(t *testing.T) {
p := newParser("func foo bar {}")
node := p.stmt().(*AstFuncDefNode)
assert.Equal(t, "foo", node.Name)
assert.Equal(t, "bar", node.Args[0])
})
t.Run("Parse function def with many parameters", func(t *testing.T) {
p := newParser("func foo bar baz {}")
node := p.stmt().(*AstFuncDefNode)
assert.Equal(t, "foo", node.Name)
assert.Equal(t, "bar", node.Args[0])
assert.Equal(t, "baz", node.Args[1])
})
t.Run("Parse if statement", func(t *testing.T) {
p := newParser("if true {foo := 42}")
node := p.stmt().(*AstIfNode)
assert.Equal(t, true, node.Cond.(*AstBoolNode).Value)
assert.Equal(t, "foo", node.Body[0].(*AstAssignNode).Name)
})
t.Run("Parse if statement with expression error", func(t *testing.T) {
p := newParser("if foo = {")
assert.Panics(t, func() {
p.stmt()
})
})
t.Run("Parse if statement with brace error", func(t *testing.T) {
p := newParser("if true foo :=")
assert.Panics(t, func() {
p.stmt()
})
})
t.Run("Parse if statement with else", func(t *testing.T) {
p := newParser("if false {} else false {} else {}")
node := p.stmt().(*AstIfNode)
assert.Equal(t, false, node.Cond.(*AstBoolNode).Value)
assert.Equal(t, false, node.Else[0].(*AstIfNode).Cond.(*AstBoolNode).Value)
})
t.Run("Parse return statement", func(t *testing.T) {
p := newParser("return 42\n")
node := p.stmt().(*AstReturnNode)
assert.Equal(t, 42.0, node.Expr.(*AstNumberNode).Value)
})
t.Run("Parse return statement without expression", func(t *testing.T) {
p := newParser("return }")
node := p.stmt()
assert.Nil(t, node.(*AstReturnNode).Expr)
})
t.Run("Parse while statement", func(t *testing.T) {
p := newParser("while foo = true {bar := 42}")
node := p.stmt().(*AstWhileNode)
assert.Equal(t, "foo", node.Cond.(*AstEqualNode).Left.(*AstVariableNode).Name)
assert.Equal(t, true, node.Cond.(*AstEqualNode).Right.(*AstBoolNode).Value)
assert.Equal(t, "bar", node.Body[0].(*AstAssignNode).Name)
})
t.Run("Parse while statement with expression error", func(t *testing.T) {
p := newParser("while foo = {")
assert.Panics(t, func() {
p.stmt()
})
})
t.Run("Test parse statement error", func(t *testing.T) {
p := newParser("\n")
assert.Panics(t, func() {
p.stmt()
})
})
t.Run("Parse assignment statement", func(t *testing.T) {
p := newParser("foo := 42 + 73\n")
node := p.stmt().(*AstAssignNode)
assert.Equal(t, "foo", node.Name)
assert.Equal(t, 42.0, node.Expr.(*AstAddNode).Left.(*AstNumberNode).Value)
assert.Equal(t, 73.0, node.Expr.(*AstAddNode).Right.(*AstNumberNode).Value)
})
t.Run("Parse assignment statement with expression error", func(t *testing.T) {
p := newParser("foo := 42 +")
assert.Panics(t, func() {
p.stmt()
})
})
t.Run("Parse function call", func(t *testing.T) {
p := newParser("foo()\n")
node := p.stmt().(*AstFuncCallNode)
assert.Equal(t, "foo", node.Name)
assert.Equal(t, 0, len(node.Args))
})
}