From 2dd098d1e16bd0b8786ca97ccc7d3b06fa6bc3d1 Mon Sep 17 00:00:00 2001 From: Ryo Nihei Date: Sun, 12 Jun 2022 00:40:28 +0900 Subject: Prohibit using a pattern in an alternative When a syntax error occurs, the parser must provide a user with the names of expected tokens. However, if a pattern appears directly in an alternative, Vartan cannot assign an appropriate name to the pattern. Therefore, this commit prohibits alternatives from containing patterns. --- spec/grammar/parser_test.go | 85 +++++++++++++++++++++++++++++++++++++++------ 1 file changed, 75 insertions(+), 10 deletions(-) (limited to 'spec/grammar/parser_test.go') diff --git a/spec/grammar/parser_test.go b/spec/grammar/parser_test.go index dcacd7d..4fd7e9f 100644 --- a/spec/grammar/parser_test.go +++ b/spec/grammar/parser_test.go @@ -272,23 +272,25 @@ func TestParse(t *testing.T) { { caption: "multiple productions are a valid grammar", src: ` -e: e "\+|-" t | t; -t: t "\*|/" f | f; -f: "\(" e ")" | id; +e: e '+' t | e '-' t | t; +t: t '*' f | t '/' f | f; +f: '(' e ')' | id; id: "[A-Za-z_][0-9A-Za-z_]*"; `, ast: &RootNode{ Productions: []*ProductionNode{ prod("e", - alt(id("e"), pat(`\+|-`), id("t")), + alt(id("e"), pat(`+`), id("t")), + alt(id("e"), pat(`-`), id("t")), alt(id("t")), ), prod("t", - alt(id("t"), pat(`\*|/`), id("f")), + alt(id("t"), pat(`*`), id("f")), + alt(id("t"), pat(`/`), id("f")), alt(id("f")), ), prod("f", - alt(pat(`\(`), id("e"), pat(`)`)), + alt(pat(`(`), id("e"), pat(`)`)), alt(id("id")), ), }, @@ -302,8 +304,8 @@ id: "[A-Za-z_][0-9A-Za-z_]*"; { caption: "productions can contain the empty alternative", src: ` -a: "foo" | ; -b: | "bar"; +a: 'foo' | ; +b: | 'bar'; c: ; `, ast: &RootNode{ @@ -329,6 +331,69 @@ a: $x; `, synErr: synErrNoSemicolon, }, + { + caption: "an alternative can contain a string literal without a terminal symbol", + src: ` +s + : 'foo' bar + ; + +bar + : 'bar'; +`, + ast: &RootNode{ + Productions: []*ProductionNode{ + prod("s", + alt(pat(`foo`), id("bar")), + ), + }, + LexProductions: []*ProductionNode{ + prod("bar", + alt(pat(`bar`)), + ), + }, + }, + }, + { + caption: "an alternative cannot contain a pattern directly", + src: ` +s + : "foo" bar + ; + +bar + : "bar"; +`, + synErr: synErrPatternInAlt, + }, + { + caption: "a terminal symbol can be defined using a string literal", + src: ` +foo + : 'foo'; +`, + ast: &RootNode{ + LexProductions: []*ProductionNode{ + prod("foo", + alt(pat(`foo`)), + ), + }, + }, + }, + { + caption: "a terminal symbol can be defined using a pattern", + src: ` +foo + : "foo"; +`, + ast: &RootNode{ + LexProductions: []*ProductionNode{ + prod("foo", + alt(pat(`foo`)), + ), + }, + }, + }, { caption: "`fragment` is a reserved word", src: `fragment: 'fragment';`, @@ -656,7 +721,7 @@ a caption: "an AST has node positions", src: ` exp - : exp "\+" id #ast exp id + : exp '+' id #ast exp id | id ; @@ -678,7 +743,7 @@ fragment number withAltDir( alt( withElemPos(id("exp"), newPos(3)), - withElemPos(pat(`\+`), newPos(3)), + withElemPos(pat(`+`), newPos(3)), withElemPos(id("id"), newPos(3)), ), withDirPos( -- cgit v1.2.3