aboutsummaryrefslogtreecommitdiff
path: root/grammar/test_helper_test.go
diff options
context:
space:
mode:
authorRyo Nihei <nihei.dev@gmail.com>2021-06-18 22:56:00 +0900
committerRyo Nihei <nihei.dev@gmail.com>2021-06-18 22:56:00 +0900
commit4f681885516651e1a85ba98527f80700c605f333 (patch)
tree669a2af1fd112b6744a7f175d820a5943d84c8d5 /grammar/test_helper_test.go
parentAdd production set (diff)
downloadurubu-4f681885516651e1a85ba98527f80700c605f333.tar.gz
urubu-4f681885516651e1a85ba98527f80700c605f333.tar.xz
Add SLR parsing table generator
Diffstat (limited to 'grammar/test_helper_test.go')
-rw-r--r--grammar/test_helper_test.go52
1 files changed, 52 insertions, 0 deletions
diff --git a/grammar/test_helper_test.go b/grammar/test_helper_test.go
new file mode 100644
index 0000000..8f5c372
--- /dev/null
+++ b/grammar/test_helper_test.go
@@ -0,0 +1,52 @@
+package grammar
+
+import "testing"
+
+type testSymbolGenerator func(text string) symbol
+
+func newTestSymbolGenerator(t *testing.T, symTab *symbolTable) 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) *lr0Item
+
+func newTestLR0ItemGenerator(t *testing.T, genProd testProductionGenerator) testLR0ItemGenerator {
+ return func(lhs string, dot int, rhs ...string) *lr0Item {
+ 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
+ }
+}