diff options
Diffstat (limited to 'spec/parser_test.go')
-rw-r--r-- | spec/parser_test.go | 58 |
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, |