Fixed bug where the regex '(()|.)(b)' wouldn't compile

posixStyleMatching
Aadhavan Srinivasan 1 month ago
parent 3cfc2a6854
commit 6a96c98d04

@ -949,7 +949,9 @@ func thompson(re []postfixNode) (Reg, error) {
// and added back in. // and added back in.
// If the middle node doesn't exist (ie. something like '()' ), that's fine, I just connect the LPAREN // If the middle node doesn't exist (ie. something like '()' ), that's fine, I just connect the LPAREN
// and RPAREN nodes. // and RPAREN nodes.
// If neither node exists, that's a problem so I return an error. // If the middle node exists but is itself the start of a group, then that _must_ be the opening paren for
// the closing paren that I'm on. I put the third node back (because it isn't involved in the capturing group), then
// I concatenate those two and add them. If neither node exists, that's a problem so I return an error.
if c.nodetype == rparenNode { if c.nodetype == rparenNode {
s.groupEnd = true s.groupEnd = true
middleNode, err1 := pop(&nfa) middleNode, err1 := pop(&nfa)
@ -964,6 +966,11 @@ func thompson(re []postfixNode) (Reg, error) {
s.groupNum = lparenNode.groupNum s.groupNum = lparenNode.groupNum
to_add := concatenate(lparenNode, s) to_add := concatenate(lparenNode, s)
nfa = append(nfa, to_add) nfa = append(nfa, to_add)
} else if middleNode.groupBegin && len(middleNode.transitions) == 0 { // The middle node is a lone lparen - something like '(())', and I'm looking at the first closing parentheses
nfa = append(nfa, lparenNode)
s.groupNum = middleNode.groupNum // In this case, the 'middle' node is actually a paren node
to_add := concatenate(middleNode, s)
nfa = append(nfa, to_add)
} else { } else {
// At this point, we assume all three nodes are valid ('lparenNode', 'middleNode' and 's') // At this point, we assume all three nodes are valid ('lparenNode', 'middleNode' and 's')
if lparenNode.groupBegin { if lparenNode.groupBegin {

Loading…
Cancel
Save