diff options
author | Charlie Stanton <charlie@shtanton.xyz> | 2022-09-21 21:05:34 +0100 |
---|---|---|
committer | Charlie Stanton <charlie@shtanton.xyz> | 2022-09-21 21:05:34 +0100 |
commit | 0a8690993d572a50b95dd4f1c1903ed00ddb9c2b (patch) | |
tree | 2ab207544c88ff19308e22c8b79c3ea349c97faa /main/regexstate.go | |
download | subex-0a8690993d572a50b95dd4f1c1903ed00ddb9c2b.tar |
Initial commit
Parses and executes substitute expressions (subexes)
So far subex has the following operations:
- Concatenation of a and b with ab
- Or with |
- Repeat maximally with *
- Repeat minimally with -
- Copy a specific character 'a'
- Copy any character '.'
- Store text matching a regex into slot 's': `$s(regex)`
- Output text in "" including loading from slots with '$'
Regexes support all the same operations as subexes minus storing and outputting
This first implementation gives very little thought to efficiency
Example:
./main 'according to all known laws of aviation' '$1(.-)$m(( .* )| ).*"$m$1"'
This swaps the first and last words of the input string
Diffstat (limited to 'main/regexstate.go')
-rw-r--r-- | main/regexstate.go | 48 |
1 files changed, 48 insertions, 0 deletions
diff --git a/main/regexstate.go b/main/regexstate.go new file mode 100644 index 0000000..16d5581 --- /dev/null +++ b/main/regexstate.go @@ -0,0 +1,48 @@ +package main + +type RegexState interface { + eat(char rune) []RegexState + accepting() bool +} + +type RegexNoneState struct {} +func (state RegexNoneState) eat(char rune) []RegexState { + return nil +} +func (state RegexNoneState) accepting() bool { + return true +} + +type RegexAnyState struct { + next RegexState +} +func (state RegexAnyState) eat(char rune) []RegexState { + return []RegexState{state.next} +} +func (state RegexAnyState) accepting() bool { + return false +} + +type RegexRuneState struct { + rune rune + next RegexState +} +func (state RegexRuneState) eat(char rune) []RegexState { + if char == state.rune { + return []RegexState{state.next} + } + return nil +} +func (state RegexRuneState) accepting() bool { + return false +} + +type RegexGroupState struct { + first, second RegexState +} +func (state RegexGroupState) eat(char rune) []RegexState { + return append(state.first.eat(char), state.second.eat(char)...) +} +func (state RegexGroupState) accepting() bool { + return state.first.accepting() || state.second.accepting() +} |