aboutsummaryrefslogtreecommitdiff
path: root/spec/parser_test.go
diff options
context:
space:
mode:
authorRyo Nihei <nihei.dev@gmail.com>2021-06-15 19:44:51 +0900
committerRyo Nihei <nihei.dev@gmail.com>2021-06-15 19:44:51 +0900
commit434ee1561016d0fd5099c755b150365b7a528c6b (patch)
tree682b822a8b30df0bcab5e132068232dce88360a4 /spec/parser_test.go
parentAdd spec parser (diff)
downloadcotia-434ee1561016d0fd5099c755b150365b7a528c6b.tar.gz
cotia-434ee1561016d0fd5099c755b150365b7a528c6b.tar.xz
Add production syntax
Diffstat (limited to 'spec/parser_test.go')
-rw-r--r--spec/parser_test.go58
1 files changed, 47 insertions, 11 deletions
diff --git a/spec/parser_test.go b/spec/parser_test.go
index 483c0b4..e061cc9 100644
--- a/spec/parser_test.go
+++ b/spec/parser_test.go
@@ -17,6 +17,11 @@ func TestParse(t *testing.T) {
Elements: elems,
}
}
+ id := func(id string) *ElementNode {
+ return &ElementNode{
+ ID: id,
+ }
+ }
pattern := func(p string) *ElementNode {
return &ElementNode{
Pattern: p,
@@ -41,15 +46,51 @@ func TestParse(t *testing.T) {
{
caption: "multiple productions are a valid grammar",
src: `
-a: "a";
-b: "b";
-c: "c";
+e: e "\+|-" t | t;
+t: t "\*|/" f | f;
+f: "\(" e ")" | id;
+id: "[A-Za-z_][0-9A-Za-z_]*";
`,
ast: &RootNode{
Productions: []*ProductionNode{
- production("a", alternative(pattern("a"))),
- production("b", alternative(pattern("b"))),
- production("c", alternative(pattern("c"))),
+ production("e",
+ alternative(id("e"), pattern(`\+|-`), id("t")),
+ alternative(id("t")),
+ ),
+ production("t",
+ alternative(id("t"), pattern(`\*|/`), id("f")),
+ alternative(id("f")),
+ ),
+ production("f",
+ alternative(pattern(`\(`), id("e"), pattern(`)`)),
+ alternative(id("id")),
+ ),
+ production("id",
+ alternative(pattern(`[A-Za-z_][0-9A-Za-z_]*`)),
+ ),
+ },
+ },
+ },
+ {
+ caption: "productions can contain the empty alternative",
+ src: `
+a: "foo" | ;
+b: | "bar";
+c: ;
+`,
+ ast: &RootNode{
+ Productions: []*ProductionNode{
+ production("a",
+ alternative(pattern(`foo`)),
+ alternative(),
+ ),
+ production("b",
+ alternative(),
+ alternative(pattern(`bar`)),
+ ),
+ production("c",
+ alternative(),
+ ),
},
},
},
@@ -74,11 +115,6 @@ c: "c";
synErr: synErrNoColon,
},
{
- caption: "an alternative must have at least one element",
- src: `a:;`,
- synErr: synErrNoElement,
- },
- {
caption: "';' must follow a production",
src: `a: "a"`,
synErr: synErrNoSemicolon,