Started working on '+' operator
This commit is contained in:
6
main.go
6
main.go
@@ -11,7 +11,7 @@ import (
|
||||
const CONCAT rune = '~'
|
||||
|
||||
func isOperator(c rune) bool {
|
||||
if c == '*' || c == '|' || c == CONCAT {
|
||||
if c == '+' || c == '*' || c == '|' || c == CONCAT {
|
||||
return true
|
||||
}
|
||||
return false
|
||||
@@ -19,7 +19,7 @@ func isOperator(c rune) bool {
|
||||
|
||||
/* priority returns the priority of the given operator */
|
||||
func priority(op rune) int {
|
||||
precedence := []rune{'|', CONCAT, '*'}
|
||||
precedence := []rune{'|', CONCAT, '+', '*'}
|
||||
return slices.Index(precedence, op)
|
||||
}
|
||||
|
||||
@@ -138,6 +138,8 @@ func thompson(re string) *State {
|
||||
}
|
||||
stateToAdd.transitions[s1.content] = append(stateToAdd.transitions[s1.content], s1)
|
||||
nfa = append(nfa, stateToAdd)
|
||||
case '+':
|
||||
|
||||
case '|':
|
||||
s1 := pop(&nfa)
|
||||
s2 := pop(&nfa)
|
||||
|
@@ -40,15 +40,16 @@ func findAllMatchesHelper(start *State, str string, indices []matchIndex, offset
|
||||
currentStates := make([]*State, 0)
|
||||
tempStates := make([]*State, 0) // Used to store states that should be used in next loop iteration
|
||||
i := 0 // Index in string
|
||||
startingFrom := i // Store starting index
|
||||
// Increment until we hit a character matching the start state (assuming not 0-state)
|
||||
if start.isEmpty == false {
|
||||
for i < len(str) && int(str[i]) != start.content {
|
||||
i++
|
||||
}
|
||||
startIdx = 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
|
||||
}
|
||||
startingFrom := i
|
||||
currentStates = append(currentStates, start)
|
||||
|
||||
// Main loop
|
||||
@@ -57,6 +58,7 @@ func findAllMatchesHelper(start *State, str string, indices []matchIndex, offset
|
||||
|
||||
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
|
||||
zeroStates, isZero := takeZeroState(currentStates)
|
||||
tempStates = append(tempStates, zeroStates...)
|
||||
for isZero == true {
|
||||
@@ -105,7 +107,8 @@ func findAllMatchesHelper(start *State, str string, indices []matchIndex, offset
|
||||
tempStates = nil
|
||||
|
||||
for _, state := range currentStates {
|
||||
if state.isLast {
|
||||
// Only add the match if we the start index is in bounds
|
||||
if state.isLast && startIdx+offset < len(str)+offset {
|
||||
endIdx = i
|
||||
indices = append(indices, matchIndex{startIdx + offset, endIdx + offset})
|
||||
}
|
||||
|
Reference in New Issue
Block a user