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
|
package grammar
import "testing"
type testSymbolGenerator func(text string) symbol
func newTestSymbolGenerator(t *testing.T, symTab *symbolTableReader) testSymbolGenerator {
return func(text string) symbol {
t.Helper()
sym, ok := symTab.toSymbol(text)
if !ok {
t.Fatalf("symbol was not found: %v", text)
}
return sym
}
}
type testProductionGenerator func(lhs string, rhs ...string) *production
func newTestProductionGenerator(t *testing.T, genSym testSymbolGenerator) testProductionGenerator {
return func(lhs string, rhs ...string) *production {
t.Helper()
rhsSym := []symbol{}
for _, text := range rhs {
rhsSym = append(rhsSym, genSym(text))
}
prod, err := newProduction(genSym(lhs), rhsSym)
if err != nil {
t.Fatalf("failed to create a production: %v", err)
}
return prod
}
}
type testLR0ItemGenerator func(lhs string, dot int, rhs ...string) *lrItem
func newTestLR0ItemGenerator(t *testing.T, genProd testProductionGenerator) testLR0ItemGenerator {
return func(lhs string, dot int, rhs ...string) *lrItem {
t.Helper()
prod := genProd(lhs, rhs...)
item, err := newLR0Item(prod, dot)
if err != nil {
t.Fatalf("failed to create a LR0 item: %v", err)
}
return item
}
}
func withLookAhead(item *lrItem, lookAhead ...symbol) *lrItem {
if item.lookAhead.symbols == nil {
item.lookAhead.symbols = map[symbol]struct{}{}
}
for _, a := range lookAhead {
item.lookAhead.symbols[a] = struct{}{}
}
return item
}
|