<- Back to shtanton's homepage
summaryrefslogtreecommitdiff
path: root/main/regexstate.go
blob: 16d5581a4b3f0424bf2ba4a17a2ccd43fd9b8762 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
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()
}