Renamed variable to avoid conflicting with type name

master
Aadhavan Srinivasan 3 days ago
parent af5b6ebe08
commit 7e88b8a4b0

@ -815,10 +815,10 @@ func thompson(re []postfixNode) (Reg, error) {
for _, c := range re { for _, c := range re {
if c.nodetype == characterNode || c.nodetype == assertionNode { if c.nodetype == characterNode || c.nodetype == assertionNode {
state := State{} stateToAdd := State{}
state.transitions = make(map[int][]*State) stateToAdd.transitions = make(map[int][]*State)
if c.allChars { if c.allChars {
state.allChars = true stateToAdd.allChars = true
if len(c.except) != 0 { if len(c.except) != 0 {
// For each node that I am 'excepting' (eg. in an inverted character class): // For each node that I am 'excepting' (eg. in an inverted character class):
// - If the node itself has exceptions, then the exceptions cancel out. // - If the node itself has exceptions, then the exceptions cancel out.
@ -827,7 +827,7 @@ func thompson(re []postfixNode) (Reg, error) {
// - If the node doesn't have exceptions (allChars == false) then the contents of the node are added to the except list. // - If the node doesn't have exceptions (allChars == false) then the contents of the node are added to the except list.
for _, node := range c.except { for _, node := range c.except {
if node.allChars { if node.allChars {
state.allChars = false stateToAdd.allChars = false
// For each postfixNode in node.except, extract the contents of the postfixNode. Concatenate them all, // For each postfixNode in node.except, extract the contents of the postfixNode. Concatenate them all,
// and them to the state's _content_. As mentioned above, if the exception has exceptions, then we can match // and them to the state's _content_. As mentioned above, if the exception has exceptions, then we can match
// those. // those.
@ -840,7 +840,7 @@ func thompson(re []postfixNode) (Reg, error) {
} }
return nodeContents return nodeContents
})...) })...)
state.content = rune2Contents(nodeExceptChars) stateToAdd.content = rune2Contents(nodeExceptChars)
} else { } else {
charsToAdd := node.contents charsToAdd := node.contents
if caseInsensitive { if caseInsensitive {
@ -848,7 +848,7 @@ func thompson(re []postfixNode) (Reg, error) {
return allCases(r, caseInsensitive) return allCases(r, caseInsensitive)
})...) })...)
} }
state.except = append(state.except, charsToAdd...) stateToAdd.except = append(stateToAdd.except, charsToAdd...)
} }
} }
} }
@ -861,43 +861,43 @@ func thompson(re []postfixNode) (Reg, error) {
return allCases(r, caseInsensitive) return allCases(r, caseInsensitive)
})...) })...)
} }
state.content = stateContents(append([]int(state.content), []int(rune2Contents(runesToAdd))...)) stateToAdd.content = stateContents(append([]int(stateToAdd.content), []int(rune2Contents(runesToAdd))...))
state.output = make([]*State, 0) stateToAdd.output = make([]*State, 0)
state.output = append(state.output, &state) stateToAdd.output = append(stateToAdd.output, &stateToAdd)
state.isEmpty = false stateToAdd.isEmpty = false
if c.nodetype == assertionNode { if c.nodetype == assertionNode {
state.isEmpty = true // This is a little weird. A lookaround has the 'isEmpty' flag set, even though it _isn't_ empty (the contents are the regex). But, there's so much error-checking that relies on this flag that it's better to keep it this way. stateToAdd.isEmpty = true // This is a little weird. A lookaround has the 'isEmpty' flag set, even though it _isn't_ empty (the contents are the regex). But, there's so much error-checking that relies on this flag that it's better to keep it this way.
state.content = newContents(EPSILON) // Ideally, an assertion shouldn't have any content, since it doesn't say anything about the content of string stateToAdd.content = newContents(EPSILON) // Ideally, an assertion shouldn't have any content, since it doesn't say anything about the content of string
if c.lookaroundDir == 0 || c.lookaroundSign == 0 { if c.lookaroundDir == 0 || c.lookaroundSign == 0 {
switch c.contents[0] { switch c.contents[0] {
case '^': case '^':
state.assert = sosAssert stateToAdd.assert = sosAssert
case '$': case '$':
state.assert = eosAssert stateToAdd.assert = eosAssert
case 'b': case 'b':
state.assert = wboundAssert stateToAdd.assert = wboundAssert
case 'B': case 'B':
state.assert = nonwboundAssert stateToAdd.assert = nonwboundAssert
} }
} else { // Lookaround } else { // Lookaround
state.lookaroundRegex = string(c.contents) stateToAdd.lookaroundRegex = string(c.contents)
if c.lookaroundDir == lookahead { if c.lookaroundDir == lookahead {
if c.lookaroundSign == positive { if c.lookaroundSign == positive {
state.assert = plaAssert stateToAdd.assert = plaAssert
} }
if c.lookaroundSign == negative { if c.lookaroundSign == negative {
state.assert = nlaAssert stateToAdd.assert = nlaAssert
} }
} }
if c.lookaroundDir == lookbehind { if c.lookaroundDir == lookbehind {
if c.lookaroundSign == positive { if c.lookaroundSign == positive {
state.assert = plbAssert stateToAdd.assert = plbAssert
} }
if c.lookaroundSign == negative { if c.lookaroundSign == negative {
state.assert = nlbAssert stateToAdd.assert = nlbAssert
} }
} }
tmpRe, err := shuntingYard(state.lookaroundRegex) tmpRe, err := shuntingYard(stateToAdd.lookaroundRegex)
if err != nil { if err != nil {
return Reg{}, fmt.Errorf("error parsing lookaround: %w", err) return Reg{}, fmt.Errorf("error parsing lookaround: %w", err)
} }
@ -905,17 +905,17 @@ func thompson(re []postfixNode) (Reg, error) {
if err != nil { if err != nil {
return Reg{}, fmt.Errorf("error compiling lookaround: %w", err) return Reg{}, fmt.Errorf("error compiling lookaround: %w", err)
} }
state.lookaroundNFA = reg.start stateToAdd.lookaroundNFA = reg.start
state.lookaroundNumCaptureGroups = reg.numGroups stateToAdd.lookaroundNumCaptureGroups = reg.numGroups
} }
} }
// Replace ESC_BACKSLASH with actual backslash, so that we can actually check if we encounter it // Replace ESC_BACKSLASH with actual backslash, so that we can actually check if we encounter it
replaceByValue([]int(state.content), int(ESC_BACKSLASH), '\\') replaceByValue([]int(stateToAdd.content), int(ESC_BACKSLASH), '\\')
replaceByValue(state.except, ESC_BACKSLASH, '\\') replaceByValue(stateToAdd.except, ESC_BACKSLASH, '\\')
nfa = append(nfa, &state) nfa = append(nfa, &stateToAdd)
} }
if c.nodetype == lparenNode || c.nodetype == rparenNode { if c.nodetype == lparenNode || c.nodetype == rparenNode {
s := &State{} s := &State{}

Loading…
Cancel
Save