aboutsummaryrefslogtreecommitdiff
path: root/grammar/test_helper_test.go
blob: 297a9a3da89b7ada60a48051230a23dd45ec725e (plain) (blame)
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
package grammar

import (
	"testing"

	"github.com/nihei9/vartan/grammar/symbol"
)

type testSymbolGenerator func(text string) symbol.Symbol

func newTestSymbolGenerator(t *testing.T, symTab *symbol.SymbolTableReader) testSymbolGenerator {
	return func(text string) symbol.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.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.Symbol) *lrItem {
	if item.lookAhead.symbols == nil {
		item.lookAhead.symbols = map[symbol.Symbol]struct{}{}
	}

	for _, a := range lookAhead {
		item.lookAhead.symbols[a] = struct{}{}
	}

	return item
}