<- Back to shtanton's homepage
summaryrefslogtreecommitdiff
path: root/main/regexast.go
blob: 0aab053452574fb7754eaf4e7da3e2d04d7fc0c0 (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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
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
}