Throw error if non-greedy operator is attempted

master
Aadhavan Srinivasan 4 days ago
parent 5bb06900cc
commit 5e6435d8a7

@ -480,6 +480,9 @@ func shuntingYard(re string, flags ...ReFlag) ([]postfixNode, error) {
if (c == '*' && outQueueFinalElement.nodetype == KLEENE) || (c == '+' && outQueueFinalElement.nodetype == PLUS) { // You cannot apply a quantifier to a quantifier in this way 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) return nil, fmt.Errorf("illegal use of token '%c'", c)
} }
if c == '?' && slices.Contains([]NodeType{KLEENE, PLUS, QUESTION}, outQueueFinalElement.nodetype) {
return nil, fmt.Errorf("non-greedy operators not supported")
}
opStack = append(opStack, c) opStack = append(opStack, c)
} }
} }
@ -1001,6 +1004,9 @@ func thompson(re []postfixNode) (Reg, error) {
if err != nil { if err != nil {
return Reg{}, fmt.Errorf("error applying kleene star") return Reg{}, fmt.Errorf("error applying kleene star")
} }
if s1.isEmpty && s1.assert != NONE {
return Reg{}, fmt.Errorf("previous token is not quantifiable")
}
stateToAdd := kleene(*s1) stateToAdd := kleene(*s1)
nfa = append(nfa, stateToAdd) nfa = append(nfa, stateToAdd)
case PLUS: // a+ is equivalent to aa* case PLUS: // a+ is equivalent to aa*

Loading…
Cancel
Save