-
Notifications
You must be signed in to change notification settings - Fork 0
/
grammar_test.go
188 lines (163 loc) · 5 KB
/
grammar_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
package lr0
import (
"testing"
"github.com/vovan-ve/go-lr0-parser/internal/testutils"
)
func TestGrammar(t *testing.T) {
var (
terminals = []Terminal{
NewTerm(tInt, "integer").FuncByte(isDigit, bytesToInt),
NewTerm(tPlus, "plus").Hide().Str("+"),
NewTerm(tMinus, "minus").Hide().Str("-"),
}
)
t.Run("panic: multiple main rules", func(t *testing.T) {
defer testutils.ExpectPanicError(t, ErrDefine, func(t *testing.T, err error) {
if err.Error() != "another rule Sum2 has Main flag too, previous was Sum1: invalid definition" {
t.Fatal("wrong error message:", err)
}
})
newGrammar(terminals, []NonTerminalDefinition{
NewNT(nVal, "Value").Is(tInt),
NewNT(nSum, "Sum1").Main().Is(nVal),
NewNT(nSum, "Sum2").Main().Is(nSum, tPlus, nVal).Do(calc2IntSum),
NewNT(nSum, "Sum3").Main().Is(nSum, tMinus, nVal).Do(calc2IntSub),
})
})
t.Run("panic: rule subject is Terminal", func(t *testing.T) {
defer testutils.ExpectPanicError(t, ErrDefine, func(t *testing.T, err error) {
if err.Error() != "Non-Terminal Plus is Terminal: invalid definition" {
t.Fatal("wrong error message:", err)
}
})
newGrammar(terminals, []NonTerminalDefinition{
NewNT(nVal, "Value").Is(tInt),
NewNT(tPlus, "Plus").Is(nVal),
NewNT(nSum, "Sum").
Is(nSum, tPlus, nVal).Do(calc2IntSum).
Is(nSum, tMinus, nVal).Do(calc2IntSub),
})
})
t.Run("panic: non-terminal without rule", func(t *testing.T) {
defer testutils.ExpectPanicError(t, ErrDefine, func(t *testing.T, err error) {
const (
expected = `undefined non-terminals without rules:
- #10 in NT Sum rules[0] (Sum : #10) definitions[0]
: invalid definition`
)
if err.Error() != expected {
t.Fatal("wrong error message:", err)
}
})
newGrammar(terminals, []NonTerminalDefinition{
NewNT(nSum, "Sum").
Is(nVal).
Is(nSum, tPlus, nVal).Do(calc2IntSum).
Is(nSum, tMinus, nVal).Do(calc2IntSub),
})
})
t.Run("panic: no main rule", func(t *testing.T) {
defer testutils.ExpectPanicError(t, ErrDefine, func(t *testing.T, err error) {
if err.Error() != "no main rule with EOF flag: invalid definition" {
t.Fatal("wrong error message:", err)
}
})
newGrammar(terminals, []NonTerminalDefinition{
NewNT(nVal, "Value").Is(tInt),
NewNT(nSum, "Sum").
Is(nVal).
Is(nSum, tPlus, nVal).Do(calc2IntSum).
Is(nSum, tMinus, nVal).Do(calc2IntSub),
NewNT(nGoal, "Goal").Is(nSum),
})
})
t.Run("panic: unused terminals", func(t *testing.T) {
defer testutils.ExpectPanicError(t, ErrDefine, func(t *testing.T, err error) {
const (
expected = `following Terminals are not used in any Rule:
- div
- MUL
: invalid definition`
)
if err.Error() != expected {
t.Fatal("wrong error message:", err)
}
})
terminals2 := append(
terminals,
NewTerm(tDiv, "div").Str("/"),
NewTerm(tMul, "MUL").Str("*"),
)
newGrammar(terminals2, []NonTerminalDefinition{
NewNT(nVal, "Value").Is(tInt),
NewNT(nSum, "Sum").
Is(nVal).
Is(nSum, tPlus, nVal).Do(calc2IntSum).
Is(nSum, tMinus, nVal).Do(calc2IntSub),
NewNT(nGoal, "Goal").Main().Is(nSum),
})
})
g := newGrammar(terminals, []NonTerminalDefinition{
NewNT(nVal, "Value").Is(tInt),
NewNT(nSum, "Sum").
Is(nVal).
Is(nSum, tPlus, nVal).Do(calc2IntSum).
Is(nSum, tMinus, nVal).Do(calc2IntSub),
NewNT(nGoal, "Goal").Main().Is(nSum),
})
t.Run("SymbolRegistry", func(t *testing.T) {
var reg SymbolRegistry = g
if s := reg.SymbolName(tInt); s != "integer" {
t.Error("name of tInt: ", s)
}
if s := reg.SymbolName(nSum); s != "Sum" {
t.Error("name of nSum: ", s)
}
if s := dumpId(tInt, g); s != "integer" {
t.Error("name of tInt: ", s)
}
if s := dumpId(nSum, g); s != "Sum" {
t.Error("name of nSum: ", s)
}
})
t.Run("main rule", func(t *testing.T) {
mr := g.MainRule()
if mr == nil {
t.Fatal("no main rule")
}
if mr.Subject() != nGoal || !mr.HasEOF() {
t.Errorf("incorrect main rule: %#v", mr)
}
if mrd := mr.Definition(); len(mrd) != 1 || mrd[0] != nSum {
t.Errorf("incorrect main rule definition: %#v", mrd)
}
})
t.Run("RulesFor", func(t *testing.T) {
if rv := g.RulesFor(nVal); len(rv) != 1 || rv[0].Definition()[0] != tInt {
t.Fatalf("incorrect rules for nValue: %#v", rv)
}
rs := g.RulesFor(nSum)
if len(rs) != 3 {
t.Fatalf("incorrect rules for nSum: %#v", rs)
}
if rg := g.RulesFor(nGoal); len(rg) != 1 || rg[0].Definition()[0] != nSum {
t.Fatalf("incorrect rules for nGoal: %#v", rg)
}
t.Run("panic: for terminal", func(t *testing.T) {
defer testutils.ExpectPanicError(t, ErrDefine, func(t *testing.T, err error) {
if err.Error() != "no rule - integer is Terminal: invalid definition" {
t.Fatal("wrong error message:", err)
}
})
g.RulesFor(tInt)
})
t.Run("panic: unknown symbol", func(t *testing.T) {
defer testutils.ExpectPanicError(t, ErrDefine, func(t *testing.T, err error) {
if err.Error() != "no rule for #6: invalid definition" {
t.Fatal("wrong error message:", err)
}
})
g.RulesFor(tMul)
})
})
}