@ -5,74 +5,133 @@ import (
"sort"
"sort"
)
)
// a MatchIndex represents a match/group. It contains the start index and end index of the match
// a Match stores a slice of all the capturing groups in a match.
type MatchIndex struct {
type Match [ ] Group
// a Group represents a group. It contains the start index and end index of the match
type Group struct {
startIdx int
startIdx int
endIdx int
endIdx int
}
}
// Converts the MatchIndex into a string representation:
func newMatch ( size int ) Match {
func ( idx MatchIndex ) toString ( ) string {
toRet := make ( [ ] Group , size )
for i := range toRet {
toRet [ i ] . startIdx = - 1
toRet [ i ] . endIdx = - 1
}
return toRet
}
// Returns the number of valid groups in the match
func ( m Match ) numValidGroups ( ) int {
numValid := 0
for _ , g := range m {
if g . startIdx >= 0 && g . endIdx >= 0 {
numValid ++
}
}
return numValid
}
// Returns a string containing the indices of all (valid) groups in the match
func ( m Match ) toString ( ) string {
var toRet string
for i , g := range m {
if g . isValid ( ) {
toRet += fmt . Sprintf ( "Group %d\n" , i )
toRet += g . toString ( )
toRet += "\n"
}
}
return toRet
}
// Converts the Group into a string representation:
func ( idx Group ) toString ( ) string {
return fmt . Sprintf ( "%d\t%d" , idx . startIdx , idx . endIdx )
return fmt . Sprintf ( "%d\t%d" , idx . startIdx , idx . endIdx )
}
}
// Returns whether a group contains valid indices
func ( g Group ) isValid ( ) bool {
return g . startIdx >= 0 && g . endIdx >= 0
}
// takeZeroState takes the 0-state (if such a transition exists) for all states in the
// takeZeroState takes the 0-state (if such a transition exists) for all states in the
// given slice. It returns the resulting states. If any of the resulting states is a 0-state,
// given slice. It returns the resulting states. If any of the resulting states is a 0-state,
// the second parameter is true.
// the second ret val is true.
func takeZeroState ( states [ ] * State ) ( rtv [ ] * State , isZero bool ) {
// The third ret val is a list of all the group numbers of all the opening parentheses we crossed,
// and the fourth is a list of all the closing parentheses we passed
func takeZeroState ( states [ ] * State ) ( rtv [ ] * State , isZero bool , openParenGroups [ ] int , closeParenGroups [ ] int ) {
for _ , state := range states {
for _ , state := range states {
if len ( state . transitions [ EPSILON ] ) > 0 {
if len ( state . transitions [ EPSILON ] ) > 0 {
for _ , s := range state . transitions [ EPSILON ] {
if s . groupBegin {
openParenGroups = append ( openParenGroups , s . groupNum )
}
if s . groupEnd {
closeParenGroups = append ( closeParenGroups , s . groupNum )
}
}
rtv = append ( rtv , state . transitions [ EPSILON ] ... )
rtv = append ( rtv , state . transitions [ EPSILON ] ... )
}
}
}
}
for _ , state := range rtv {
for _ , state := range rtv {
if len ( state . transitions [ EPSILON ] ) > 0 {
if len ( state . transitions [ EPSILON ] ) > 0 {
return rtv , true
return rtv , true , openParenGroups , closeParenGroups
}
}
}
}
return rtv , false
return rtv , false , openParenGroups , closeParenGroups
}
}
// zeroMatchPossible returns true if a zero-length match is possible
// zeroMatchPossible returns true if a zero-length match is possible
// from any of the given states, given the string and our position in it.
// from any of the given states, given the string and our position in it.
// It uses the same algorithm to find zero-states as the one inside the loop,
// It uses the same algorithm to find zero-states as the one inside the loop,
// so I should probably put it in a function.
// so I should probably put it in a function.
func zeroMatchPossible ( str [ ] rune , idx int , states ... * State ) bool {
// It also returns all the capturing groups that both begin and end at the current index.
zerostates , iszero := takeZeroState ( states )
// This is because, by definition, zero-states don't move forward in the string.
tempstates := make ( [ ] * State , 0 , len ( zerostates ) + len ( states ) )
func zeroMatchPossible ( str [ ] rune , idx int , states ... * State ) ( bool , [ ] int , [ ] int ) {
allOpenParenGroups := make ( [ ] int , 0 )
allCloseParenGroups := make ( [ ] int , 0 )
zeroStates , isZero , openParenGroups , closeParenGroups := takeZeroState ( states )
allOpenParenGroups = append ( allOpenParenGroups , openParenGroups ... )
allCloseParenGroups = append ( allCloseParenGroups , closeParenGroups ... )
tempstates := make ( [ ] * State , 0 , len ( zeroStates ) + len ( states ) )
tempstates = append ( tempstates , states ... )
tempstates = append ( tempstates , states ... )
tempstates = append ( tempstates , zerostates ... )
tempstates = append ( tempstates , zero S tates... )
num_appended := 0 // number of unique states addded to tempstates
num_appended := 0 // number of unique states addded to tempstates
for iszero == true {
for isZero == true {
zerostates , iszero = takeZeroState ( tempstates )
zeroStates , isZero , openParenGroups , closeParenGroups = takeZeroState ( tempstates )
tempstates , num_appended = unique_append ( tempstates , zerostates ... )
allOpenParenGroups = append ( allOpenParenGroups , openParenGroups ... )
allCloseParenGroups = append ( allCloseParenGroups , closeParenGroups ... )
tempstates , num_appended = unique_append ( tempstates , zeroStates ... )
if num_appended == 0 { // break if we haven't appended any more unique values
if num_appended == 0 { // break if we haven't appended any more unique values
break
break
}
}
}
}
for _ , state := range tempstates {
for _ , state := range tempstates {
if state . isEmpty && ( state . assert == NONE || state . checkAssertion ( str , idx ) ) && state . isLast {
if state . isEmpty && ( state . assert == NONE || state . checkAssertion ( str , idx ) ) && state . isLast {
return true
return true , allOpenParenGroups , allCloseParenGroups
}
}
}
}
return false
return false , allOpenParenGroups , allCloseParenGroups
}
}
// Prunes the slice by removing overlapping indices.
// Prunes the slice by removing overlapping indices.
func pruneIndices ( indices [ ] Match Index ) [ ] Match Index {
func pruneIndices ( indices [ ] Match ) [ ] Match {
// First, sort the slice by the start indices
// First, sort the slice by the start indices
sort . Slice ( indices , func ( i , j int ) bool {
sort . Slice ( indices , func ( i , j int ) bool {
return indices [ i ] .startIdx < indices [ j ] . startIdx
return indices [ i ] [0 ] .startIdx < indices [ j ] [ 0 ] . startIdx
} )
} )
toRet := make ( [ ] Match Index , 0 , len ( indices ) )
toRet := make ( [ ] Match , 0 , len ( indices ) )
current := indices [ 0 ]
current := indices [ 0 ]
for _ , idx := range indices [ 1 : ] {
for _ , idx := range indices [ 1 : ] {
// idx doesn't overlap with current (starts after current ends), so add current to result
// idx doesn't overlap with current (starts after current ends), so add current to result
// and update the current.
// and update the current.
if idx .startIdx >= current . endIdx {
if idx [0 ] .startIdx >= current [ 0 ] . endIdx {
toRet = append ( toRet , current )
toRet = append ( toRet , current )
current = idx
current = idx
} else if idx .endIdx > current . endIdx {
} else if idx [0 ] .endIdx > current [ 0 ] . endIdx {
// idx overlaps, but it is longer, so update current
// idx overlaps, but it is longer, so update current
current = idx
current = idx
}
}
@ -84,46 +143,63 @@ func pruneIndices(indices []MatchIndex) []MatchIndex {
// findAllMatches tries to find all matches of the regex represented by given start-state, with
// findAllMatches tries to find all matches of the regex represented by given start-state, with
// the given string
// the given string
func findAllMatches ( start * State , str [ ] rune ) [ ] Match Index {
func findAllMatches ( start * State , str [ ] rune , numGroups int ) [ ] Match {
idx := 0
idx := 0
var matchFound bool
var matchFound bool
var matchIdx Match Index
var matchIdx Match
indices := new_uniq_arr [ MatchIndex ] ( )
indices := make ( [ ] Match , 0 )
for idx <= len ( str ) {
for idx <= len ( str ) {
matchFound , matchIdx , idx = findAllMatchesHelper ( start , str , idx )
matchFound , matchIdx , idx = findAllMatchesHelper ( start , str , idx , numGroups )
if matchFound {
if matchFound {
indices . add ( matchIdx )
indices = append ( indices , matchIdx )
}
}
}
}
toReturn := indices . values ( )
if len ( indices ) > 0 {
if len ( toReturn ) > 0 {
return pruneIndices ( indices )
return pruneIndices ( toReturn )
}
}
return toReturn
return indices
}
}
// Helper for findAllMatches. Returns whether it found a match, the
// Helper for findAllMatches. Returns whether it found a match, the
// first matchIndex it finds, and how far it got into the string ie. where
// first Match it finds, and how far it got into the string ie. where
// the next search should start from.
// the next search should start from.
//
//
// Might return duplicates or overlapping indices, so care must be taken to prune the resulting array.
// Might return duplicates or overlapping indices, so care must be taken to prune the resulting array.
func findAllMatchesHelper ( start * State , str [ ] rune , offset int ) ( bool , Match Index , int ) {
func findAllMatchesHelper ( start * State , str [ ] rune , offset int , numGroups int ) ( bool , Match , int ) {
// Base case - exit if offset exceeds string's length
// Base case - exit if offset exceeds string's length
if offset > len ( str ) {
if offset > len ( str ) {
// The first value here shouldn't be used, because we should exit when the secon d return value is > than len(str)
// The second value here shouldn't be used, because we should exit when the thir d return value is > than len(str)
return false , MatchIndex { } , offset
return false , [ ] Group { } , offset
}
}
// 'Base case' - if we are at the end of the string, check if we can add a zero-length match
// 'Base case' - if we are at the end of the string, check if we can add a zero-length match
if offset == len ( str ) {
if offset == len ( str ) {
// Get all zero-state matches. If we can get to a zero-state without matching anything, we
// Get all zero-state matches. If we can get to a zero-state without matching anything, we
// can add a zero-length match. This is all true only if the start state itself matches nothing.
// can add a zero-length match. This is all true only if the start state itself matches nothing.
if start . isEmpty {
if start . isEmpty {
if zeroMatchPossible ( str , offset , start ) {
to_return := newMatch ( numGroups + 1 )
return true , MatchIndex { offset , offset } , offset + 1
if start . groupBegin {
to_return [ start . groupNum ] . startIdx = offset
}
if ok , openGrps , closeGrps := zeroMatchPossible ( str , offset , start ) ; ok {
for _ , gIdx := range openGrps {
to_return [ gIdx ] . startIdx = offset
}
}
for _ , gIdx := range closeGrps {
to_return [ gIdx ] . endIdx = offset
}
}
return false , MatchIndex { } , offset + 1
to_return [ 0 ] = Group { offset , offset }
return true , to_return , offset + 1
}
}
}
return false , [ ] Group { } , offset + 1
}
// Hold a list of match indices for the current run. When we
// can no longer find a match, the match with the largest range is
// chosen as the match for the entire string.
// This allows us to pick the longest possible match (which is how greedy matching works).
// COMMENT ABOVE IS CURRENTLY NOT UP-TO-DATE
tempIndices := newMatch ( numGroups + 1 )
foundPath := false
foundPath := false
startIdx := offset
startIdx := offset
@ -138,7 +214,7 @@ func findAllMatchesHelper(start *State, str []rune, offset int) (bool, MatchInde
if start . assert != NONE {
if start . assert != NONE {
if start . checkAssertion ( str , offset ) == false {
if start . checkAssertion ( str , offset ) == false {
i ++
i ++
return false , MatchIndex { } , i
return false , [ ] Group { } , i
}
}
}
}
// Increment until we hit a character matching the start state (assuming not 0-state)
// Increment until we hit a character matching the start state (assuming not 0-state)
@ -150,13 +226,14 @@ func findAllMatchesHelper(start *State, str []rune, offset int) (bool, MatchInde
startingFrom = i
startingFrom = i
i ++ // Advance to next character (if we aren't at a 0-state, which doesn't match anything), so that we can check for transitions. If we advance at a 0-state, we will never get a chance to match the first character
i ++ // Advance to next character (if we aren't at a 0-state, which doesn't match anything), so that we can check for transitions. If we advance at a 0-state, we will never get a chance to match the first character
}
}
// Check if the start state begins a group - if so, add the start index to our list
if start . groupBegin {
tempIndices [ start . groupNum ] . startIdx = i
}
currentStates = append ( currentStates , start )
currentStates = append ( currentStates , start )
// Hold a list of match indices for the current run. When we
// can no longer find a match, the match with the largest range is
// chosen as the match for the entire string.
// This allows us to pick the longest possible match (which is how greedy matching works).
tempIndices := make ( [ ] MatchIndex , 0 )
// Main loop
// Main loop
for i < len ( str ) {
for i < len ( str ) {
foundPath = false
foundPath = false
@ -164,11 +241,23 @@ func findAllMatchesHelper(start *State, str []rune, offset int) (bool, MatchInde
zeroStates := make ( [ ] * State , 0 )
zeroStates := make ( [ ] * State , 0 )
// Keep taking zero-states, until there are no more left to take
// Keep taking zero-states, until there are no more left to take
// Objective: If any of our current states have transitions to 0-states, replace them with the 0-state. Do this until there are no more transitions to 0-states, or there are no more unique 0-states to take.
// Objective: If any of our current states have transitions to 0-states, replace them with the 0-state. Do this until there are no more transitions to 0-states, or there are no more unique 0-states to take.
zeroStates , isZero := takeZeroState ( currentStates )
zeroStates , isZero , openParenGroups , closeParenGroups := takeZeroState ( currentStates )
for _ , val := range openParenGroups {
tempIndices [ val ] . startIdx = i
}
for _ , val := range closeParenGroups {
tempIndices [ val ] . endIdx = i
}
tempStates = append ( tempStates , zeroStates ... )
tempStates = append ( tempStates , zeroStates ... )
num_appended := 0
num_appended := 0
for isZero == true {
for isZero == true {
zeroStates , isZero = takeZeroState ( tempStates )
zeroStates , isZero , openParenGroups , closeParenGroups = takeZeroState ( tempStates )
for _ , val := range openParenGroups {
tempIndices [ val ] . startIdx = i
}
for _ , val := range closeParenGroups {
tempIndices [ val ] . endIdx = i
}
tempStates , num_appended = unique_append ( tempStates , zeroStates ... )
tempStates , num_appended = unique_append ( tempStates , zeroStates ... )
if num_appended == 0 { // Break if we haven't appended any more unique values
if num_appended == 0 { // Break if we haven't appended any more unique values
break
break
@ -217,18 +306,20 @@ func findAllMatchesHelper(start *State, str []rune, offset int) (bool, MatchInde
if i == startingFrom {
if i == startingFrom {
i ++
i ++
}
}
return false , MatchIndex { } , i
return false , [ ] Group { } , i
}
}
}
}
if lastStateInList { // A last-state was in the list of states. add the matchIndex to our MatchIndex list
if lastStateInList { // A last-state was in the list of states. add the matchIndex to our MatchIndex list
endIdx = i
endIdx = i
tempIndices , _ = unique_append ( tempIndices , MatchIndex { startIdx , endIdx } )
tempIndices [0 ] = Group { startIdx , endIdx }
}
}
// Check if we can find a zero-length match
// Check if we can find a zero-length match
if foundPath == false {
if foundPath == false {
if zeroMatchPossible ( str , i , currentStates ... ) {
if ok , _ , _ := zeroMatchPossible ( str , i , currentStates ... ) ; ok {
tempIndices , _ = unique_append ( tempIndices , MatchIndex { startIdx , startIdx } )
if tempIndices [ 0 ] . isValid ( ) == false {
tempIndices [ 0 ] = Group { startIdx , startIdx }
}
}
}
// If we haven't moved in the string, increment the counter by 1
// If we haven't moved in the string, increment the counter by 1
// to ensure we don't keep trying the same string over and over.
// to ensure we don't keep trying the same string over and over.
@ -236,23 +327,14 @@ func findAllMatchesHelper(start *State, str []rune, offset int) (bool, MatchInde
startIdx ++
startIdx ++
// i++
// i++
// }
// }
// Get the maximum index-range from the list
if tempIndices . numValidGroups ( ) > 0 && tempIndices [ 0 ] . isValid ( ) {
if len ( tempIndices ) > 0 {
if tempIndices [ 0 ] . startIdx == tempIndices [ 0 ] . endIdx { // If we have a zero-length match, we have to shift the index at which we start. Otherwise we keep looking at the same paert of the string over and over.
indexToAdd := Reduce ( tempIndices , func ( i1 MatchIndex , i2 MatchIndex ) MatchIndex {
return true , tempIndices , tempIndices [ 0 ] . endIdx + 1
r1 := i1 . endIdx - i1 . startIdx
r2 := i2 . endIdx - i2 . startIdx
if r1 >= r2 {
return i1
}
return i2
} )
if indexToAdd . startIdx == indexToAdd . endIdx { // If we have a zero-length match, we have to shift the index at which we start. Otherwise we keep looking at the same paert of the string over and over.
return true , indexToAdd , indexToAdd . endIdx + 1
} else {
} else {
return true , indexToAdd, indexToAdd . endIdx
return true , tempIndices , tempIndices [ 0 ] . endIdx
}
}
}
}
return false , MatchIndex { } , startIdx
return false , [ ] Group { } , startIdx
}
}
currentStates = make ( [ ] * State , len ( tempStates ) )
currentStates = make ( [ ] * State , len ( tempStates ) )
copy ( currentStates , tempStates )
copy ( currentStates , tempStates )
@ -263,11 +345,23 @@ func findAllMatchesHelper(start *State, str []rune, offset int) (bool, MatchInde
// End-of-string reached. Go to any 0-states, until there are no more 0-states to go to. Then check if any of our states are in the end position.
// End-of-string reached. Go to any 0-states, until there are no more 0-states to go to. Then check if any of our states are in the end position.
// This is the exact same algorithm used inside the loop, so I should probably put it in a function.
// This is the exact same algorithm used inside the loop, so I should probably put it in a function.
zeroStates , isZero := takeZeroState ( currentStates )
zeroStates , isZero , openParenGroups , closeParenGroups := takeZeroState ( currentStates )
for _ , val := range openParenGroups {
tempIndices [ val ] . startIdx = i
}
for _ , val := range closeParenGroups {
tempIndices [ val ] . endIdx = i
}
tempStates = append ( tempStates , zeroStates ... )
tempStates = append ( tempStates , zeroStates ... )
num_appended := 0 // Number of unique states addded to tempStates
num_appended := 0 // Number of unique states addded to tempStates
for isZero == true {
for isZero == true {
zeroStates , isZero = takeZeroState ( tempStates )
zeroStates , isZero , openParenGroups , closeParenGroups = takeZeroState ( tempStates )
for _ , val := range openParenGroups {
tempIndices [ val ] . startIdx = i
}
for _ , val := range closeParenGroups {
tempIndices [ val ] . endIdx = i
}
tempStates , num_appended = unique_append ( tempStates , zeroStates ... )
tempStates , num_appended = unique_append ( tempStates , zeroStates ... )
if num_appended == 0 { // Break if we haven't appended any more unique values
if num_appended == 0 { // Break if we haven't appended any more unique values
break
break
@ -283,28 +377,20 @@ func findAllMatchesHelper(start *State, str []rune, offset int) (bool, MatchInde
if state . isLast && startIdx < len ( str ) {
if state . isLast && startIdx < len ( str ) {
if state . assert == NONE || state . checkAssertion ( str , i ) {
if state . assert == NONE || state . checkAssertion ( str , i ) {
endIdx = i
endIdx = i
tempIndices , _ = unique_append ( tempIndices , MatchIndex { startIdx , endIdx } )
tempIndices [0 ] = Group { startIdx , endIdx }
}
}
}
}
}
}
// Get the maximum index-range from the list
if len ( tempIndices ) > 0 {
if tempIndices . numValidGroups ( ) > 0 {
indexToAdd := Reduce ( tempIndices , func ( i1 MatchIndex , i2 MatchIndex ) MatchIndex {
if tempIndices [ 0 ] . startIdx == tempIndices [ 0 ] . endIdx { // If we have a zero-length match, we have to shift the index at which we start. Otherwise we keep looking at the same paert of the string over and over.
r1 := i1 . endIdx - i1 . startIdx
return true , tempIndices , tempIndices [ 0 ] . endIdx + 1
r2 := i2 . endIdx - i2 . startIdx
if r1 >= r2 {
return i1
}
return i2
} )
if indexToAdd . endIdx == indexToAdd . startIdx { // Same statement occurs above, see reasoning there
return true , indexToAdd , indexToAdd . endIdx + 1
} else {
} else {
return true , indexToAdd, indexToAdd . endIdx
return true , tempIndices , tempIndices [ 0 ] . endIdx
}
}
}
}
if startIdx == startingFrom { // Increment starting index if we haven't moved in the string. Prevents us from matching the same part of the string over and over.
if startIdx == startingFrom { // Increment starting index if we haven't moved in the string. Prevents us from matching the same part of the string over and over.
startIdx ++
startIdx ++
}
}
return false , MatchIndex { } , startIdx
return false , [ ] Group { } , startIdx
}
}