From 3ec662c34841bb5bcf05166d1b9efd800b1e9ea3 Mon Sep 17 00:00:00 2001 From: Ryo Nihei Date: Sat, 11 Dec 2021 19:42:32 +0900 Subject: Add tests of compiler/parser package --- compiler/parser/error.go | 1 + compiler/parser/parser.go | 8 ++++---- compiler/parser/parser_test.go | 8 ++++++++ 3 files changed, 13 insertions(+), 4 deletions(-) (limited to 'compiler/parser') diff --git a/compiler/parser/error.go b/compiler/parser/error.go index 8be6600..be81da4 100644 --- a/compiler/parser/error.go +++ b/compiler/parser/error.go @@ -15,6 +15,7 @@ var ( // syntax errors synErrUnexpectedToken = fmt.Errorf("unexpected token") synErrNullPattern = fmt.Errorf("a pattern must be a non-empty byte sequence") + synErrUnmatchablePattern = fmt.Errorf("a pattern cannot match any characters") synErrAltLackOfOperand = fmt.Errorf("an alternation expression must have operands") synErrRepNoTarget = fmt.Errorf("a repeat expression must have an operand") synErrGroupNoElem = fmt.Errorf("a grouping expression must include at least one character") diff --git a/compiler/parser/parser.go b/compiler/parser/parser.go index 70d1ce6..fd85fab 100644 --- a/compiler/parser/parser.go +++ b/compiler/parser/parser.go @@ -205,7 +205,7 @@ func (p *parser) parseSingleChar() CPTree { } inverse := exclude(elem, genAnyCharAST()) if inverse == nil { - panic(fmt.Errorf("a pattern that isn't matching any symbols")) + p.raiseParseError(synErrUnmatchablePattern, "") } for { elem := p.parseBExpElem() @@ -214,7 +214,7 @@ func (p *parser) parseSingleChar() CPTree { } inverse = exclude(elem, inverse) if inverse == nil { - panic(fmt.Errorf("a pattern that isn't matching any symbols")) + p.raiseParseError(synErrUnmatchablePattern, "") } } if p.consume(tokenKindEOF) { @@ -355,12 +355,12 @@ func (p *parser) parseCharProp() CPTree { r := cpRanges[0] alt = exclude(newRangeSymbolNode(r.From, r.To), genAnyCharAST()) if alt == nil { - panic(fmt.Errorf("a pattern that isn't matching any symbols")) + p.raiseParseError(synErrUnmatchablePattern, "") } for _, r := range cpRanges[1:] { alt = exclude(newRangeSymbolNode(r.From, r.To), alt) if alt == nil { - panic(fmt.Errorf("a pattern that isn't matching any symbols")) + p.raiseParseError(synErrUnmatchablePattern, "") } } } else { diff --git a/compiler/parser/parser_test.go b/compiler/parser/parser_test.go index 1fa0489..bad6404 100644 --- a/compiler/parser/parser_test.go +++ b/compiler/parser/parser_test.go @@ -471,6 +471,14 @@ func TestParse(t *testing.T) { pattern: "[^\\p{Lu}-\\p{Ll}]", syntaxError: synErrRangePropIsUnavailable, }, + { + pattern: "[^\\u{0000}-\\u{10FFFF}]", + syntaxError: synErrUnmatchablePattern, + }, + { + pattern: "[^\\u{0000}-\\u{FFFF}\\u{010000}-\\u{10FFFF}]", + syntaxError: synErrUnmatchablePattern, + }, { pattern: "[^]", ast: newSymbolNode('^'), -- cgit v1.2.3