Fixed kleene star behavior, which used to behave like a '+'

This commit is contained in:
2024-10-23 08:51:40 -04:00
parent 2cd43bf2a1
commit 9d3bc2b804
3 changed files with 34 additions and 12 deletions

View File

@@ -8,13 +8,14 @@ func match(start *State, str string) (startIdx int, endIdx int, matched bool) {
i := 0 // Index in string
// Increment until we hit a character matching the start state
if start.isEmpty == false {
for int(str[i]) != start.content {
for i < len(str) && int(str[i]) != start.content {
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
}
currentStates = append(currentStates, start)
startIdx = i
i++ // Advance to next character so that we can check for transitions
// Main loop
for i < len(str) {
// If there are any 0-transitions, take those
@@ -45,7 +46,15 @@ func match(start *State, str string) (startIdx int, endIdx int, matched bool) {
i++
}
// End-of-string reached. Check if any of our states is in the end position.
// End-of-string reached. Go to any 0-states. Then check if any of our states are in the end position.
for _, state := range currentStates {
if len(state.transitions[EPSILON]) > 0 {
tempStates = append(tempStates, state.transitions[EPSILON]...)
}
}
copy(currentStates, tempStates)
tempStates = nil
for _, state := range currentStates {
if state.isLast {
endIdx = i