<- Back to shtanton's homepage
summaryrefslogtreecommitdiff
path: root/main/subexast.go
blob: 7e2f33ca136013510936d106fecc9cdeea0492ae (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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
package main

import (
	"fmt"
)

type SubexAST interface {
	compileWith(next SubexState) SubexState
}

type SubexASTConcat struct {
	first, second SubexAST
}
func (ast SubexASTConcat) compileWith(next SubexState) SubexState {
	return ast.first.compileWith(ast.second.compileWith(next))
}
func (ast SubexASTConcat) String() string {
	return fmt.Sprintf("(%v)(%v)", ast.first, ast.second)
}

type SubexASTStore struct {
	match RegexAST
	slot rune
}
func (ast SubexASTStore) compileWith(next SubexState) SubexState {
	return SubexStoreState {
		match: ast.match.compileWith(RegexNoneState{}),
		slot: ast.slot,
		next: next,
	}
}
func (ast SubexASTStore) String() string {
	return fmt.Sprintf("$%c(%v)", ast.slot, ast.match)
}

type SubexASTOr struct {
	first, second SubexAST
}
func (ast SubexASTOr) compileWith(next SubexState) SubexState {
	return SubexGroupState {
		ast.first.compileWith(next),
		ast.second.compileWith(next),
	}
}

type SubexASTMaximise struct {
	content SubexAST
}
func (ast SubexASTMaximise) compileWith(next SubexState) SubexState {
	state := &SubexGroupState {
		nil,
		next,
	}
	state.first = ast.content.compileWith(state)
	return state
}
func (ast SubexASTMaximise) String() string {
	return fmt.Sprintf("(%v)*", ast.content)
}

type SubexASTMinimise struct {
	content SubexAST
}
func (ast SubexASTMinimise) compileWith(next SubexState) SubexState {
	state := &SubexGroupState {
		next,
		nil,
	}
	state.second = ast.content.compileWith(state)
	return state
}
func (ast SubexASTMinimise) String() string {
	return fmt.Sprintf("(%v)-", ast.content)
}

type SubexASTRepeat struct {
	content SubexAST
	min, max int
}
func (ast SubexASTRepeat) compileWith(next SubexState) SubexState {
	for i := ast.min; i < ast.max; i += 1 {
		next = SubexGroupState {
			ast.content.compileWith(next),
			next,
		}
	}
	for i := 0; i < ast.min; i += 1 {
		next = ast.content.compileWith(next)
	}
	return next
}

type SubexASTCopyRune rune
func (ast SubexASTCopyRune) compileWith(next SubexState) SubexState {
	return SubexCopyRuneState{
		rune: rune(ast),
		next: next,
	}
}

type SubexASTCopyAny struct {}
func (ast SubexASTCopyAny) compileWith(next SubexState) SubexState {
	return SubexCopyAnyState{next}
}
func (ast SubexASTCopyAny) String() string {
	return "."
}

type SubexASTOutput struct {
	replacement []TransducerOutput
}
func (ast SubexASTOutput) compileWith(next SubexState) SubexState {
	return SubexOutputState{
		content: ast.replacement,
		next: next,
	}
}