diff --git a/regex/nfa.go b/regex/nfa.go index d051a25..d7ac1af 100644 --- a/regex/nfa.go +++ b/regex/nfa.go @@ -329,9 +329,6 @@ func kleene(s1 *nfaState) (*nfaState, error) { toReturn.isAlternation = true toReturn.content = newContents(epsilon) toReturn.splitState = s1 - for i := range s1.output { - s1.output[i].next = toReturn - } // toReturn := &nfaState{} // toReturn.transitions = make(map[int][]*nfaState) @@ -373,14 +370,20 @@ func alternate(s1 *nfaState, s2 *nfaState) *nfaState { return toReturn } -func question(s1 *nfaState) *nfaState { // Use the fact that ab? == a(b|) - s2 := &nfaState{} +func question(s1 *nfaState) (*nfaState, error) { // Use the fact that ab? == a(b|) + 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.content = newContents(epsilon) - s2.output = append(s2.output, s2) - s2.isEmpty = true - s3 := alternate(s1, s2) - return s3 + return toReturn, nil } // Creates and returns a new state with the 'default' values.