aboutsummaryrefslogtreecommitdiff
path: root/spec
diff options
context:
space:
mode:
authorRyo Nihei <nihei.dev@gmail.com>2022-06-12 00:40:28 +0900
committerRyo Nihei <nihei.dev@gmail.com>2022-06-12 09:44:00 +0900
commit2dd098d1e16bd0b8786ca97ccc7d3b06fa6bc3d1 (patch)
tree2e5a9ffa712ffe553d5ecafb35e27a7a3cd1fa11 /spec
parentSupport the underscore symbol matching any symbols in vartan-test command (diff)
downloadcotia-2dd098d1e16bd0b8786ca97ccc7d3b06fa6bc3d1.tar.gz
cotia-2dd098d1e16bd0b8786ca97ccc7d3b06fa6bc3d1.tar.xz
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.
Diffstat (limited to 'spec')
-rw-r--r--spec/grammar/parser.go17
-rw-r--r--spec/grammar/parser_test.go85
-rw-r--r--spec/grammar/syntax_error.go1
3 files changed, 92 insertions, 11 deletions
diff --git a/spec/grammar/parser.go b/spec/grammar/parser.go
index 2bb6a4a..946d877 100644
--- a/spec/grammar/parser.go
+++ b/spec/grammar/parser.go
@@ -338,12 +338,27 @@ func (p *parser) parseProduction() *ProductionNode {
}
}
- return &ProductionNode{
+ prod := &ProductionNode{
Directives: dirs,
LHS: lhs,
RHS: rhs,
Pos: lhsPos,
}
+
+ // Vartan's driver must provide a user with the names of expected tokens when a syntax error occurs.
+ // However, if a pattern appears directly in an alternative, Vartan's compiler cannot assign an appropriate
+ // name to the pattern. Therefore, this code prohibits alternatives from containing patterns.
+ if !prod.isLexical() {
+ for _, alt := range prod.RHS {
+ for _, elem := range alt.Elements {
+ if elem.Pattern != "" && !elem.Literally {
+ raiseSyntaxError(elem.Pos.Row, synErrPatternInAlt)
+ }
+ }
+ }
+ }
+
+ return prod
}
func (p *parser) parseAlternative() *AlternativeNode {
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{
@@ -330,6 +332,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';`,
synErr: synErrNoProductionName,
@@ -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(
diff --git a/spec/grammar/syntax_error.go b/spec/grammar/syntax_error.go
index 1fec801..1f9664b 100644
--- a/spec/grammar/syntax_error.go
+++ b/spec/grammar/syntax_error.go
@@ -37,6 +37,7 @@ var (
synErrNoDirectiveName = newSyntaxError("a directive needs a name")
synErrNoOrderedSymbolName = newSyntaxError("an ordered symbol name is missing")
synErrUnclosedDirGroup = newSyntaxError("a directive group must be closed by )")
+ synErrPatternInAlt = newSyntaxError("a pattern literal cannot appear directly in an alternative. instead, please define a terminal symbol with the pattern literal")
synErrStrayExpOp = newSyntaxError("an expansion operator ... must be preceded by an identifier")
synErrInvalidExpOperand = newSyntaxError("an expansion operator ... can be applied to only an identifier")
synErrSemicolonNoNewline = newSyntaxError("a semicolon must be followed by a newline")