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() }