From 00902944f62d3ea7ec42f312de5221397c0d9b27 Mon Sep 17 00:00:00 2001 From: Aadhavan Srinivasan Date: Mon, 9 Dec 2024 01:28:18 -0500 Subject: [PATCH] Added code to match capturing groups and store into a Group (used to be MatchIndex) --- matching.go | 248 +++++++++++++++++++++++++++++++++++----------------- 1 file changed, 167 insertions(+), 81 deletions(-) diff --git a/matching.go b/matching.go index 62e7131..4f0ae21 100644 --- a/matching.go +++ b/matching.go @@ -5,74 +5,133 @@ import ( "sort" ) -// a MatchIndex represents a match/group. It contains the start index and end index of the match -type MatchIndex struct { +// a Match stores a slice of all the capturing groups in a match. +type Match []Group + +// a Group represents a group. It contains the start index and end index of the match +type Group struct { startIdx int endIdx int } -// Converts the MatchIndex into a string representation: -func (idx MatchIndex) toString() string { +func newMatch(size int) Match { + 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) } +// 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 // given slice. It returns the resulting states. If any of the resulting states is a 0-state, -// the second parameter is true. -func takeZeroState(states []*State) (rtv []*State, isZero bool) { +// the second ret val is true. +// 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 { 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]...) } } for _, state := range rtv { 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 // 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, // so I should probably put it in a function. -func zeroMatchPossible(str []rune, idx int, states ...*State) bool { - zerostates, iszero := takeZeroState(states) - tempstates := make([]*State, 0, len(zerostates)+len(states)) +// It also returns all the capturing groups that both begin and end at the current index. +// This is because, by definition, zero-states don't move forward in the string. +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, zerostates...) + tempstates = append(tempstates, zeroStates...) num_appended := 0 // number of unique states addded to tempstates - for iszero == true { - zerostates, iszero = takeZeroState(tempstates) - tempstates, num_appended = unique_append(tempstates, zerostates...) + for isZero == true { + zeroStates, isZero, openParenGroups, closeParenGroups = takeZeroState(tempstates) + 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 break } } for _, state := range tempstates { 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. -func pruneIndices(indices []MatchIndex) []MatchIndex { +func pruneIndices(indices []Match) []Match { // First, sort the slice by the start indices 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([]MatchIndex, 0, len(indices)) + toRet := make([]Match, 0, len(indices)) current := indices[0] for _, idx := range indices[1:] { // idx doesn't overlap with current (starts after current ends), so add current to result // and update the current. - if idx.startIdx >= current.endIdx { + if idx[0].startIdx >= current[0].endIdx { toRet = append(toRet, current) 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 current = idx } @@ -84,47 +143,64 @@ func pruneIndices(indices []MatchIndex) []MatchIndex { // findAllMatches tries to find all matches of the regex represented by given start-state, with // the given string -func findAllMatches(start *State, str []rune) []MatchIndex { +func findAllMatches(start *State, str []rune, numGroups int) []Match { idx := 0 var matchFound bool - var matchIdx MatchIndex - indices := new_uniq_arr[MatchIndex]() + var matchIdx Match + indices := make([]Match, 0) for idx <= len(str) { - matchFound, matchIdx, idx = findAllMatchesHelper(start, str, idx) + matchFound, matchIdx, idx = findAllMatchesHelper(start, str, idx, numGroups) if matchFound { - indices.add(matchIdx) + indices = append(indices, matchIdx) } } - toReturn := indices.values() - if len(toReturn) > 0 { - return pruneIndices(toReturn) + if len(indices) > 0 { + return pruneIndices(indices) } - return toReturn + return indices } // 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. // // 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, MatchIndex, int) { +func findAllMatchesHelper(start *State, str []rune, offset int, numGroups int) (bool, Match, int) { // Base case - exit if offset exceeds string's length if offset > len(str) { - // The first value here shouldn't be used, because we should exit when the second return value is > than len(str) - return false, MatchIndex{}, offset + // The second value here shouldn't be used, because we should exit when the third return value is > than len(str) + return false, []Group{}, offset } // 'Base case' - if we are at the end of the string, check if we can add a zero-length match if offset == len(str) { // 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. if start.isEmpty { - if zeroMatchPossible(str, offset, start) { - return true, MatchIndex{offset, offset}, offset + 1 + to_return := newMatch(numGroups + 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 + } + to_return[0] = Group{offset, offset} + return true, to_return, offset + 1 } } - return false, MatchIndex{}, 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 startIdx := offset endIdx := offset @@ -138,7 +214,7 @@ func findAllMatchesHelper(start *State, str []rune, offset int) (bool, MatchInde if start.assert != NONE { if start.checkAssertion(str, offset) == false { i++ - return false, MatchIndex{}, i + return false, []Group{}, i } } // 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 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) - // 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 for i < len(str) { foundPath = false @@ -164,11 +241,23 @@ func findAllMatchesHelper(start *State, str []rune, offset int) (bool, MatchInde zeroStates := make([]*State, 0) // 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. - 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...) num_appended := 0 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...) if num_appended == 0 { // Break if we haven't appended any more unique values break @@ -217,18 +306,20 @@ func findAllMatchesHelper(start *State, str []rune, offset int) (bool, MatchInde if i == startingFrom { 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 endIdx = i - tempIndices, _ = unique_append(tempIndices, MatchIndex{startIdx, endIdx}) + tempIndices[0] = Group{startIdx, endIdx} } // Check if we can find a zero-length match if foundPath == false { - if zeroMatchPossible(str, i, currentStates...) { - tempIndices, _ = unique_append(tempIndices, MatchIndex{startIdx, startIdx}) + if ok, _, _ := zeroMatchPossible(str, i, currentStates...); ok { + if tempIndices[0].isValid() == false { + tempIndices[0] = Group{startIdx, startIdx} + } } // 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. @@ -236,23 +327,14 @@ func findAllMatchesHelper(start *State, str []rune, offset int) (bool, MatchInde startIdx++ // i++ // } - // Get the maximum index-range from the list - if len(tempIndices) > 0 { - indexToAdd := Reduce(tempIndices, func(i1 MatchIndex, i2 MatchIndex) MatchIndex { - 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 + if tempIndices.numValidGroups() > 0 && tempIndices[0].isValid() { + 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. + return true, tempIndices, tempIndices[0].endIdx + 1 } 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)) 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. // 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...) num_appended := 0 // Number of unique states addded to tempStates 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...) if num_appended == 0 { // Break if we haven't appended any more unique values break @@ -283,28 +377,20 @@ func findAllMatchesHelper(start *State, str []rune, offset int) (bool, MatchInde if state.isLast && startIdx < len(str) { if state.assert == NONE || state.checkAssertion(str, 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 { - indexToAdd := Reduce(tempIndices, func(i1 MatchIndex, i2 MatchIndex) MatchIndex { - r1 := i1.endIdx - i1.startIdx - 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 + + if tempIndices.numValidGroups() > 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. + return true, tempIndices, tempIndices[0].endIdx + 1 } 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. startIdx++ } - return false, MatchIndex{}, startIdx + return false, []Group{}, startIdx }