@ -22,18 +22,18 @@ const (
alwaysTrueAssert // An assertion that is always true
alwaysTrueAssert // An assertion that is always true
)
)
type S tate struct {
type s tate struct {
content stateContents // Contents of current state
content stateContents // Contents of current state
isEmpty bool // If it is empty - Union operator and Kleene star states will be empty
isEmpty bool // If it is empty - Union operator and Kleene star states will be empty
isLast bool // If it is the last state (acept state)
isLast bool // If it is the last state (acept state)
output [ ] * S tate // The outputs of the current state ie. the 'outward arrows'. A union operator state will have more than one of these.
output [ ] * s tate // The outputs of the current state ie. the 'outward arrows'. A union operator state will have more than one of these.
transitions map [ int ] [ ] * S tate // 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)
transitions map [ int ] [ ] * s tate // 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
isKleene bool // Identifies whether current node is a 0-state representing Kleene star
assert assertType // Type of assertion of current node - NONE means that the node doesn't assert anything
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
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.
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 * S tate // Holds the NFA of the lookaroundRegex - if it exists
lookaroundNFA * s tate // Holds the NFA of the lookaroundRegex - if it exists
lookaroundNumCaptureGroups int // Number of capturing groups in lookaround regex if current node is a lookaround
lookaroundNumCaptureGroups int // Number of capturing groups in lookaround regex if current node is a lookaround
groupBegin bool // Whether or not the node starts a capturing group
groupBegin bool // Whether or not the node starts a capturing group
groupEnd bool // Whether or not the node ends a capturing group
groupEnd bool // Whether or not the node ends a capturing group
@ -44,14 +44,14 @@ type State struct {
}
}
// Clones the NFA starting from the given state.
// Clones the NFA starting from the given state.
func cloneState ( start * State) * S tate {
func cloneState ( start * state) * s tate {
return cloneStateHelper ( start , make ( map [ * State] * S tate) )
return cloneStateHelper ( start , make ( map [ * state] * s tate) )
}
}
// Helper function for clone. The map is used to keep track of which states have
// Helper function for clone. The map is used to keep track of which states have
// already been copied, and which ones haven't.
// already been copied, and which ones haven't.
// This function was created using output from Llama3.1:405B.
// This function was created using output from Llama3.1:405B.
func cloneStateHelper ( stateToClone * State, cloneMap map [ * State ] * State ) * S tate {
func cloneStateHelper ( stateToClone * state, cloneMap map [ * state ] * state ) * s tate {
// Base case - if the clone exists in our map, return it.
// Base case - if the clone exists in our map, return it.
if clone , exists := cloneMap [ stateToClone ] ; exists {
if clone , exists := cloneMap [ stateToClone ] ; exists {
return clone
return clone
@ -61,12 +61,12 @@ func cloneStateHelper(stateToClone *State, cloneMap map[*State]*State) *State {
}
}
// Recursive case - if the clone doesn't exist, create it, add it to the map,
// Recursive case - if the clone doesn't exist, create it, add it to the map,
// and recursively call for each of the transition states.
// and recursively call for each of the transition states.
clone := & S tate{
clone := & s tate{
content : append ( [ ] int { } , stateToClone . content ... ) ,
content : append ( [ ] int { } , stateToClone . content ... ) ,
isEmpty : stateToClone . isEmpty ,
isEmpty : stateToClone . isEmpty ,
isLast : stateToClone . isLast ,
isLast : stateToClone . isLast ,
output : make ( [ ] * S tate, len ( stateToClone . output ) ) ,
output : make ( [ ] * s tate, len ( stateToClone . output ) ) ,
transitions : make ( map [ int ] [ ] * S tate) ,
transitions : make ( map [ int ] [ ] * s tate) ,
isKleene : stateToClone . isKleene ,
isKleene : stateToClone . isKleene ,
assert : stateToClone . assert ,
assert : stateToClone . assert ,
zeroMatchFound : stateToClone . zeroMatchFound ,
zeroMatchFound : stateToClone . zeroMatchFound ,
@ -86,7 +86,7 @@ func cloneStateHelper(stateToClone *State, cloneMap map[*State]*State) *State {
}
}
}
}
for k , v := range stateToClone . transitions {
for k , v := range stateToClone . transitions {
clone . transitions [ k ] = make ( [ ] * S tate, len ( v ) )
clone . transitions [ k ] = make ( [ ] * s tate, len ( v ) )
for i , s := range v {
for i , s := range v {
if s == stateToClone {
if s == stateToClone {
clone . transitions [ k ] [ i ] = clone
clone . transitions [ k ] [ i ] = clone
@ -104,7 +104,7 @@ func cloneStateHelper(stateToClone *State, cloneMap map[*State]*State) *State {
// Checks if the given state's assertion is true. Returns true if the given
// Checks if the given state's assertion is true. Returns true if the given
// state doesn't have an assertion.
// state doesn't have an assertion.
func ( s S tate) checkAssertion ( str [ ] rune , idx int ) bool {
func ( s s tate) checkAssertion ( str [ ] rune , idx int ) bool {
if s . assert == alwaysTrueAssert {
if s . assert == alwaysTrueAssert {
return true
return true
}
}
@ -171,7 +171,7 @@ func (s State) checkAssertion(str []rune, idx int) bool {
}
}
// Returns true if the contents of 's' contain the value at the given index of the given string
// Returns true if the contents of 's' contain the value at the given index of the given string
func ( s S tate) contentContains ( str [ ] rune , idx int ) bool {
func ( s s tate) contentContains ( str [ ] rune , idx int ) bool {
if s . assert != noneAssert {
if s . assert != noneAssert {
return s . checkAssertion ( str , idx )
return s . checkAssertion ( str , idx )
}
}
@ -182,19 +182,19 @@ func (s State) contentContains(str []rune, idx int) bool {
return slices . Contains ( s . content , int ( str [ idx ] ) )
return slices . Contains ( s . content , int ( str [ idx ] ) )
}
}
func ( s S tate) isLookaround ( ) bool {
func ( s s tate) isLookaround ( ) bool {
return s . assert == plaAssert || s . assert == plbAssert || s . assert == nlaAssert || s . assert == nlbAssert
return s . assert == plaAssert || s . assert == plbAssert || s . assert == nlaAssert || s . assert == nlbAssert
}
}
// Returns the matches for the character at the given index of the given string.
// Returns the matches for the character at the given index of the given string.
// Also returns the number of matches. Returns -1 if an assertion failed.
// Also returns the number of matches. Returns -1 if an assertion failed.
func ( s S tate) matchesFor ( str [ ] rune , idx int ) ( [ ] * S tate, int ) {
func ( s s tate) matchesFor ( str [ ] rune , idx int ) ( [ ] * s tate, int ) {
// Assertions can be viewed as 'checks'. If the check fails, we return
// Assertions can be viewed as 'checks'. If the check fails, we return
// an empty array and 0.
// an empty array and 0.
// If it passes, we treat it like any other state, and return all the transitions.
// If it passes, we treat it like any other state, and return all the transitions.
if s . assert != noneAssert {
if s . assert != noneAssert {
if s . checkAssertion ( str , idx ) == false {
if s . checkAssertion ( str , idx ) == false {
return make ( [ ] * S tate, 0 ) , - 1
return make ( [ ] * s tate, 0 ) , - 1
}
}
}
}
listTransitions := s . transitions [ int ( str [ idx ] ) ]
listTransitions := s . transitions [ int ( str [ idx ] ) ]
@ -211,39 +211,39 @@ func (s State) matchesFor(str []rune, idx int) ([]*State, int) {
}
}
// verifyLastStatesHelper performs the depth-first recursion needed for verifyLastStates
// verifyLastStatesHelper performs the depth-first recursion needed for verifyLastStates
func verifyLastStatesHelper ( st ate * State , visited map [ * S tate] bool ) {
func verifyLastStatesHelper ( st * state , visited map [ * s tate] bool ) {
if len ( st ate . transitions ) == 0 {
if len ( st . transitions ) == 0 {
st ate . isLast = true
st . isLast = true
return
return
}
}
// if len(state.transitions) == 1 && len(state.transitions[state.content]) == 1 && state.transitions[state.content][0] == state { // Eg. a*
// if len(state.transitions) == 1 && len(state.transitions[state.content]) == 1 && state.transitions[state.content][0] == state { // Eg. a*
if len ( st ate . transitions ) == 1 { // Eg. a*
if len ( st . transitions ) == 1 { // Eg. a*
var moreThanOneTrans bool // Dummy variable, check if all the transitions for the current's state's contents have a length of one
var moreThanOneTrans bool // Dummy variable, check if all the transitions for the current's state's contents have a length of one
for _ , c := range st ate . content {
for _ , c := range st . content {
if len ( st ate . transitions [ c ] ) != 1 || st ate . transitions [ c ] [ 0 ] != st ate {
if len ( st . transitions [ c ] ) != 1 || st . transitions [ c ] [ 0 ] != st {
moreThanOneTrans = true
moreThanOneTrans = true
}
}
}
}
st ate . isLast = ! moreThanOneTrans
st . isLast = ! moreThanOneTrans
}
}
if st ate . isKleene { // A State representing a Kleene Star has transitions going out, which loop back to it. If all those transitions point to the same (single) state, then it must be a last state
if st . isKleene { // A State representing a Kleene Star has transitions going out, which loop back to it. If all those transitions point to the same (single) state, then it must be a last state
transitionDests := make ( [ ] * S tate, 0 )
transitionDests := make ( [ ] * s tate, 0 )
for _ , v := range st ate . transitions {
for _ , v := range st . transitions {
transitionDests = append ( transitionDests , v ... )
transitionDests = append ( transitionDests , v ... )
}
}
if allEqual ( transitionDests ... ) {
if allEqual ( transitionDests ... ) {
st ate . isLast = true
st . isLast = true
return
return
}
}
}
}
if visited [ st ate ] == true {
if visited [ st ] == true {
return
return
}
}
visited [ st ate ] = true
visited [ st ] = true
for _ , states := range st ate . transitions {
for _ , states := range st . transitions {
for i := range states {
for i := range states {
if states [ i ] != st ate {
if states [ i ] != st {
verifyLastStatesHelper ( states [ i ] , visited )
verifyLastStatesHelper ( states [ i ] , visited )
}
}
}
}
@ -251,12 +251,12 @@ func verifyLastStatesHelper(state *State, visited map[*State]bool) {
}
}
// verifyLastStates enables the 'isLast' flag for the leaf nodes (last states)
// verifyLastStates enables the 'isLast' flag for the leaf nodes (last states)
func verifyLastStates ( start [ ] * S tate) {
func verifyLastStates ( start [ ] * s tate) {
verifyLastStatesHelper ( start [ 0 ] , make ( map [ * S tate] bool ) )
verifyLastStatesHelper ( start [ 0 ] , make ( map [ * s tate] bool ) )
}
}
// Concatenates s1 and s2, returns the start of the concatenation.
// Concatenates s1 and s2, returns the start of the concatenation.
func concatenate ( s1 * State, s2 * State ) * S tate {
func concatenate ( s1 * state, s2 * state ) * s tate {
if s1 == nil {
if s1 == nil {
return s2
return s2
}
}
@ -269,13 +269,13 @@ func concatenate(s1 *State, s2 *State) *State {
return s1
return s1
}
}
func kleene ( s1 State) ( * S tate, error ) {
func kleene ( s1 state) ( * s tate, error ) {
if s1 . isEmpty && s1 . assert != noneAssert {
if s1 . isEmpty && s1 . assert != noneAssert {
return nil , fmt . Errorf ( "previous token is not quantifiable" )
return nil , fmt . Errorf ( "previous token is not quantifiable" )
}
}
toReturn := & S tate{ }
toReturn := & s tate{ }
toReturn . transitions = make ( map [ int ] [ ] * S tate)
toReturn . transitions = make ( map [ int ] [ ] * s tate)
toReturn . content = newContents ( EPSILON )
toReturn . content = newContents ( EPSILON )
toReturn . isEmpty = true
toReturn . isEmpty = true
toReturn . isKleene = true
toReturn . isKleene = true
@ -291,9 +291,9 @@ func kleene(s1 State) (*State, error) {
return toReturn , nil
return toReturn , nil
}
}
func alternate ( s1 * State, s2 * State ) * S tate {
func alternate ( s1 * state, s2 * state ) * s tate {
toReturn := & S tate{ }
toReturn := & s tate{ }
toReturn . transitions = make ( map [ int ] [ ] * S tate)
toReturn . transitions = make ( map [ int ] [ ] * s tate)
toReturn . output = append ( toReturn . output , s1 . output ... )
toReturn . output = append ( toReturn . output , s1 . output ... )
toReturn . output = append ( toReturn . output , s2 . output ... )
toReturn . output = append ( toReturn . output , s2 . output ... )
// Unique append is used here (and elsewhere) to ensure that,
// Unique append is used here (and elsewhere) to ensure that,
@ -313,9 +313,9 @@ func alternate(s1 *State, s2 *State) *State {
return toReturn
return toReturn
}
}
func question ( s1 * State) * S tate { // Use the fact that ab? == a(b|)
func question ( s1 * state) * s tate { // Use the fact that ab? == a(b|)
s2 := & S tate{ }
s2 := & s tate{ }
s2 . transitions = make ( map [ int ] [ ] * S tate)
s2 . transitions = make ( map [ int ] [ ] * s tate)
s2 . content = newContents ( EPSILON )
s2 . content = newContents ( EPSILON )
s2 . output = append ( s2 . output , s2 )
s2 . output = append ( s2 . output , s2 )
s2 . isEmpty = true
s2 . isEmpty = true
@ -324,10 +324,10 @@ func question(s1 *State) *State { // Use the fact that ab? == a(b|)
}
}
// Creates and returns a new state with the 'default' values.
// Creates and returns a new state with the 'default' values.
func newState ( ) S tate {
func newState ( ) s tate {
ret := S tate{
ret := s tate{
output : make ( [ ] * S tate, 0 ) ,
output : make ( [ ] * s tate, 0 ) ,
transitions : make ( map [ int ] [ ] * S tate) ,
transitions : make ( map [ int ] [ ] * s tate) ,
assert : noneAssert ,
assert : noneAssert ,
except : append ( [ ] rune { } , 0 ) ,
except : append ( [ ] rune { } , 0 ) ,
lookaroundRegex : "" ,
lookaroundRegex : "" ,
@ -339,7 +339,7 @@ func newState() State {
}
}
// Creates and returns a state that _always_ has a zero-length match.
// Creates and returns a state that _always_ has a zero-length match.
func zeroLengthMatchState ( ) S tate {
func zeroLengthMatchState ( ) s tate {
start := newState ( )
start := newState ( )
start . content = newContents ( EPSILON )
start . content = newContents ( EPSILON )
start . isEmpty = true
start . isEmpty = true