From ad0f7d0178da53e69a69103df3d10f7d556b792a Mon Sep 17 00:00:00 2001 From: Aadhavan Srinivasan Date: Mon, 3 Feb 2025 14:05:53 -0500 Subject: [PATCH] Added new state fields to tell if a state is a question or alternation --- regex/nfa.go | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/regex/nfa.go b/regex/nfa.go index 4ff15fe..8f63eb0 100644 --- a/regex/nfa.go +++ b/regex/nfa.go @@ -31,6 +31,8 @@ type nfaState struct { output []*nfaState // The outputs of the current state ie. the 'outward arrows'. A union operator state will have more than one of these. transitions map[int][]*nfaState // Transitions to different states (maps a character (int representation) to a _list of states. This is useful if one character can lead multiple states eg. ab|aa) isKleene bool // Identifies whether current node is a 0-state representing Kleene star + isQuestion bool // Identifies whether current node is a 0-state representing the question operator + isAlternation bool // Identifies whether current node is a 0-state representing an alternation assert assertType // Type of assertion of current node - NONE means that the node doesn't assert anything allChars bool // Whether or not the state represents all characters (eg. a 'dot' metacharacter). A 'dot' node doesn't store any contents directly, as it would take up too much space except []rune // Only valid if allChars is true - match all characters _except_ the ones in this block. Useful for inverting character classes. @@ -70,6 +72,8 @@ func cloneStateHelper(stateToClone *nfaState, cloneMap map[*nfaState]*nfaState) output: make([]*nfaState, len(stateToClone.output)), transitions: make(map[int][]*nfaState), isKleene: stateToClone.isKleene, + isQuestion: stateToClone.isQuestion, + isAlternation: stateToClone.isAlternation, assert: stateToClone.assert, zeroMatchFound: stateToClone.zeroMatchFound, allChars: stateToClone.allChars, @@ -341,6 +345,7 @@ func alternate(s1 *nfaState, s2 *nfaState) *nfaState { } toReturn.content = newContents(epsilon) toReturn.isEmpty = true + toReturn.isAlternation = true return toReturn } @@ -351,6 +356,7 @@ func question(s1 *nfaState) *nfaState { // Use the fact that ab? == a(b|) s2.content = newContents(epsilon) s2.output = append(s2.output, s2) s2.isEmpty = true + s2.isQuestion = true s3 := alternate(s1, s2) return s3 }