Added fields to state, to determine capturing group information. 0th group refers to entire match
This commit is contained in:
13
nfa.go
13
nfa.go
@@ -33,6 +33,10 @@ type State struct {
|
|||||||
except []rune // Only valid if allChars is true - match all characters _except_ the ones in this block. Useful for inverting character classes.
|
except []rune // Only valid if allChars is true - match all characters _except_ the ones in this block. Useful for inverting character classes.
|
||||||
lookaroundRegex string // Only for lookaround states - Contents of the regex that the lookaround state holds
|
lookaroundRegex string // Only for lookaround states - Contents of the regex that the lookaround state holds
|
||||||
lookaroundNFA *State // Holds the NFA of the lookaroundRegex - if it exists
|
lookaroundNFA *State // Holds the NFA of the lookaroundRegex - if it exists
|
||||||
|
lookaroundNumCaptureGroups int // Number of capturing groups if current node is a lookaround
|
||||||
|
groupBegin bool // Whether or not the node starts a capturing group
|
||||||
|
groupEnd bool // Whether or not the node ends a capturing group
|
||||||
|
groupNum int // Which capturing group the node starts / ends
|
||||||
}
|
}
|
||||||
|
|
||||||
// Clones the NFA starting from the given state.
|
// Clones the NFA starting from the given state.
|
||||||
@@ -65,6 +69,9 @@ func cloneStateHelper(state *State, cloneMap map[*State]*State) *State {
|
|||||||
allChars: state.allChars,
|
allChars: state.allChars,
|
||||||
except: append([]rune{}, state.except...),
|
except: append([]rune{}, state.except...),
|
||||||
lookaroundRegex: state.lookaroundRegex,
|
lookaroundRegex: state.lookaroundRegex,
|
||||||
|
groupEnd: state.groupEnd,
|
||||||
|
groupBegin: state.groupBegin,
|
||||||
|
groupNum: state.groupNum,
|
||||||
}
|
}
|
||||||
cloneMap[state] = clone
|
cloneMap[state] = clone
|
||||||
for i, s := range state.output {
|
for i, s := range state.output {
|
||||||
@@ -113,17 +120,17 @@ func (s State) checkAssertion(str []rune, idx int) bool {
|
|||||||
// 2. Run it on the test string.
|
// 2. Run it on the test string.
|
||||||
// 3. Based on the kind of lookaround (and the indices we get), determine what action to take.
|
// 3. Based on the kind of lookaround (and the indices we get), determine what action to take.
|
||||||
startState := s.lookaroundNFA
|
startState := s.lookaroundNFA
|
||||||
matchIndices := findAllMatches(startState, str)
|
matchIndices := findAllMatches(startState, str, startState.lookaroundNumCaptureGroups)
|
||||||
|
|
||||||
numMatchesFound := 0
|
numMatchesFound := 0
|
||||||
for _, matchIdx := range matchIndices {
|
for _, matchIdx := range matchIndices {
|
||||||
if s.assert == PLA || s.assert == NLA { // Lookahead - return true (or false) if at least one match starts at the current index
|
if s.assert == PLA || s.assert == NLA { // Lookahead - return true (or false) if at least one match starts at the current index
|
||||||
if matchIdx.startIdx == idx {
|
if matchIdx[0].startIdx == idx {
|
||||||
numMatchesFound++
|
numMatchesFound++
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if s.assert == PLB || s.assert == NLB { // Lookbehind - return true (or false) if at least one match _ends_ at the current index.
|
if s.assert == PLB || s.assert == NLB { // Lookbehind - return true (or false) if at least one match _ends_ at the current index.
|
||||||
if matchIdx.endIdx == idx {
|
if matchIdx[0].endIdx == idx {
|
||||||
numMatchesFound++
|
numMatchesFound++
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user