Implement PCRE Matching (prefer left-branch) #2
23
regex/nfa.go
23
regex/nfa.go
@@ -329,9 +329,6 @@ func kleene(s1 *nfaState) (*nfaState, error) {
|
|||||||
toReturn.isAlternation = true
|
toReturn.isAlternation = true
|
||||||
toReturn.content = newContents(epsilon)
|
toReturn.content = newContents(epsilon)
|
||||||
toReturn.splitState = s1
|
toReturn.splitState = s1
|
||||||
for i := range s1.output {
|
|
||||||
s1.output[i].next = toReturn
|
|
||||||
}
|
|
||||||
|
|
||||||
// toReturn := &nfaState{}
|
// toReturn := &nfaState{}
|
||||||
// toReturn.transitions = make(map[int][]*nfaState)
|
// toReturn.transitions = make(map[int][]*nfaState)
|
||||||
@@ -373,14 +370,20 @@ func alternate(s1 *nfaState, s2 *nfaState) *nfaState {
|
|||||||
return toReturn
|
return toReturn
|
||||||
}
|
}
|
||||||
|
|
||||||
func question(s1 *nfaState) *nfaState { // Use the fact that ab? == a(b|)
|
func question(s1 *nfaState) (*nfaState, error) { // Use the fact that ab? == a(b|)
|
||||||
s2 := &nfaState{}
|
if s1.isEmpty && s1.assert != noneAssert {
|
||||||
|
return nil, fmt.Errorf("previous token is not quantifiable")
|
||||||
|
}
|
||||||
|
toReturn := &nfaState{}
|
||||||
|
toReturn.isEmpty = true
|
||||||
|
toReturn.isAlternation = true
|
||||||
|
toReturn.isQuestion = true
|
||||||
|
toReturn.content = newContents(epsilon)
|
||||||
|
toReturn.splitState = s1
|
||||||
|
toReturn.output = append([]*nfaState{}, toReturn)
|
||||||
|
toReturn.output = append(toReturn.output, s1.output...)
|
||||||
// s2.transitions = make(map[int][]*nfaState)
|
// s2.transitions = make(map[int][]*nfaState)
|
||||||
s2.content = newContents(epsilon)
|
return toReturn, nil
|
||||||
s2.output = append(s2.output, s2)
|
|
||||||
s2.isEmpty = true
|
|
||||||
s3 := alternate(s1, s2)
|
|
||||||
return s3
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Creates and returns a new state with the 'default' values.
|
// Creates and returns a new state with the 'default' values.
|
||||||
|
Reference in New Issue
Block a user