diff options
author | Charlie Stanton <charlie@shtanton.xyz> | 2022-12-22 15:23:28 +0000 |
---|---|---|
committer | Charlie Stanton <charlie@shtanton.xyz> | 2022-12-22 15:23:28 +0000 |
commit | fb1f51d1da22f34509cf3fe1d174296d36a0f0ad (patch) | |
tree | f07260b169af51288e76ae095b3dce3a21f01148 /main/subexstate.go | |
parent | f3911888915f6aa96e6b28bd2a98a662faf20f47 (diff) | |
download | subex-fb1f51d1da22f34509cf3fe1d174296d36a0f0ad.tar |
Expressions inside stores are now subexes instead of regexes
This simplifies things by no longer needing a regex implementation
It also enables transforming text as it is being read into a slot
Diffstat (limited to 'main/subexstate.go')
-rw-r--r-- | main/subexstate.go | 32 |
1 files changed, 17 insertions, 15 deletions
diff --git a/main/subexstate.go b/main/subexstate.go index cc697f0..00b9e75 100644 --- a/main/subexstate.go +++ b/main/subexstate.go @@ -21,25 +21,25 @@ func (state SubexGroupState) accepting(store Store) []string { } type SubexStoreState struct { - match RegexState + match SubexState slot rune next SubexState - input string + toStore string } -func (state SubexStoreState) eat(store Store, char rune) []SubexBranch { - var nextStates []SubexBranch - if state.match.accepting() { - store[state.slot] = state.input - nextStates = state.next.eat(store, char) +func (state SubexStoreState) eat(store Store, char rune) (nextStates []SubexBranch) { + acceptedOutputs := state.match.accepting(store) + for _, acceptedOutput := range acceptedOutputs { + nextStore := store.withValue(state.slot, state.toStore + acceptedOutput) + nextStates = append(nextStates, state.next.eat(nextStore.clone(), char)...) } - nextRegexStates := state.match.eat(char) - for _, regexState := range nextRegexStates { + nextMatchStates := state.match.eat(store.clone(), char) + for _, matchState := range nextMatchStates { nextStates = append(nextStates, SubexBranch { state: SubexStoreState { - match: regexState, + match: matchState.state, slot: state.slot, next: state.next, - input: state.input + string(char), + toStore: state.toStore + matchState.output, }, output: "", store: store.clone(), @@ -47,11 +47,13 @@ func (state SubexStoreState) eat(store Store, char rune) []SubexBranch { } return nextStates } -func (state SubexStoreState) accepting(store Store) []string { - if state.match.accepting() { - return state.next.accepting(store) +func (state SubexStoreState) accepting(store Store) (outputs []string) { + acceptedOutputs := state.match.accepting(store) + for _, acceptedOutput := range acceptedOutputs { + nextStore := store.withValue(state.slot, state.toStore + acceptedOutput) + outputs = append(outputs, state.next.accepting(nextStore)...) } - return nil + return outputs } type SubexOutputState struct { |