From 4547ba74f0161fa961cfbe09fe27fecfffd2ac0e Mon Sep 17 00:00:00 2001 From: Aadhavan Srinivasan Date: Tue, 28 Jan 2025 09:40:19 -0500 Subject: [PATCH] Throw error if a quantifier is quantified eg. 'a**'; throw error if start of character range is greater than the end eg. '[b-a]' --- compile.go | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/compile.go b/compile.go index 7a6669b..907bb19 100644 --- a/compile.go +++ b/compile.go @@ -469,6 +469,10 @@ func shuntingYard(re string, flags ...ReFlag) ([]postfixNode, error) { outQueue = append(outQueue, newPostfixNode(to_append)) topStack, _ = peek(opStack) } + outQueueFinalElement, _ := peek(outQueue) + if (c == '*' && outQueueFinalElement.nodetype == KLEENE) || (c == '+' && outQueueFinalElement.nodetype == PLUS) { // You cannot apply a quantifier to a quantifier in this way + return nil, fmt.Errorf("illegal use of token '%c'", c) + } opStack = append(opStack, c) } } @@ -654,6 +658,9 @@ func shuntingYard(re string, flags ...ReFlag) ([]postfixNode, error) { // We have established that they both have a length of 1 startRangeRune := startRangePostfixNode.contents[0] endRangeRune := endRangePostfixNode.contents[0] + if startRangeRune > endRangeRune { + return nil, fmt.Errorf("character range syntax is [a-b], not [b-a]") + } chars = append(chars, newPostfixCharNode(genRange(startRangeRune, endRangeRune+1)...)) }