<- Back to shtanton's homepage
summaryrefslogtreecommitdiff
path: root/main
diff options
context:
space:
mode:
Diffstat (limited to 'main')
-rw-r--r--main/parse.go51
-rw-r--r--main/regexast.go92
-rw-r--r--main/regexstate.go48
3 files changed, 0 insertions, 191 deletions
diff --git a/main/parse.go b/main/parse.go
index 18941f2..59104c1 100644
--- a/main/parse.go
+++ b/main/parse.go
@@ -21,57 +21,6 @@ func parseReplacement(l *RuneReader) (output []TransducerOutput) {
return output
}
-func parseRegex(l *RuneReader, minPower int) RegexAST {
- var lhs RegexAST
- r := l.next()
- switch r {
- case eof:
- return nil
- case ')', '*', '-', '|', '?', '!':
- l.rewind()
- return nil
- case '(':
- lhs = parseRegex(l, 0)
- if !l.accept(")") {
- panic("Missing matching )")
- }
- case '.':
- lhs = RegexASTAny{}
- default:
- lhs = RegexASTRune(r)
- }
- loop: for {
- if minPower <= 0 {
- next := parseRegex(l, 1)
- if next != nil {
- lhs = RegexASTConcat{lhs, next}
- continue loop
- }
- }
- r := l.next()
- switch {
- case r == '*' && minPower <= 4:
- lhs = RegexASTMaximise{lhs}
- case r == '-' && minPower <= 4:
- lhs = RegexASTMinimise{lhs}
- case r == '!' && minPower <= 4:
- lhs = RegexASTTry{lhs}
- case r == '?' && minPower <= 4:
- lhs = RegexASTMaybe{lhs}
- case r == '|' && minPower <= 2:
- rhs := parseRegex(l, 3)
- if rhs == nil {
- panic("Missing regex after |")
- }
- lhs = RegexASTOr{lhs, rhs}
- default:
- l.rewind()
- break loop
- }
- }
- return lhs
-}
-
func parseSubex(l *RuneReader, minPower int) SubexAST {
var lhs SubexAST
r := l.next()
diff --git a/main/regexast.go b/main/regexast.go
deleted file mode 100644
index a5a60c4..0000000
--- a/main/regexast.go
+++ /dev/null
@@ -1,92 +0,0 @@
-package main
-
-import (
- "fmt"
-)
-
-type RegexAST interface {
- compileWith(next RegexState) RegexState
-}
-
-type RegexASTRune rune
-func (ast RegexASTRune) compileWith(next RegexState) RegexState {
- return RegexRuneState{
- rune: rune(ast),
- next: next,
- }
-}
-func (ast RegexASTRune) String() string {
- return string(rune(ast))
-}
-
-type RegexASTAny struct {}
-func (ast RegexASTAny) compileWith(next RegexState) RegexState {
- return RegexAnyState{next}
-}
-func (ast RegexASTAny) String() string {
- return "."
-}
-
-type RegexASTConcat struct {
- first, second RegexAST
-}
-func (ast RegexASTConcat) compileWith(next RegexState) RegexState {
- return ast.first.compileWith(ast.second.compileWith(next))
-}
-func (ast RegexASTConcat) String() string {
- return fmt.Sprintf("Concat{%v, %v}", ast.first, ast.second)
-}
-
-type RegexASTOr struct {
- first, second RegexAST
-}
-func (ast RegexASTOr) compileWith(next RegexState) RegexState {
- return RegexGroupState{
- ast.first.compileWith(next),
- ast.second.compileWith(next),
- }
-}
-
-type RegexASTMaximise struct {
- content RegexAST
-}
-func (ast RegexASTMaximise) compileWith(next RegexState) RegexState {
- state := &RegexGroupState{
- nil,
- next,
- }
- state.first = ast.content.compileWith(state)
- return state
-}
-
-type RegexASTMinimise struct {
- content RegexAST
-}
-func (ast RegexASTMinimise) compileWith(next RegexState) RegexState {
- state := &RegexGroupState{
- next,
- nil,
- }
- state.second = ast.content.compileWith(state)
- return state
-}
-
-type RegexASTTry struct {
- content RegexAST
-}
-func (ast RegexASTTry) compileWith(next RegexState) RegexState {
- return RegexGroupState{
- ast.content.compileWith(next),
- next,
- }
-}
-
-type RegexASTMaybe struct {
- content RegexAST
-}
-func (ast RegexASTMaybe) compileWith(next RegexState) RegexState {
- return RegexGroupState {
- next,
- ast.content.compileWith(next),
- }
-}
diff --git a/main/regexstate.go b/main/regexstate.go
deleted file mode 100644
index 16d5581..0000000
--- a/main/regexstate.go
+++ /dev/null
@@ -1,48 +0,0 @@
-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()
-}