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.go | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) (limited to 'spec/grammar/parser.go') 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 { -- cgit v1.2.3