<- Back to shtanton's homepage
aboutsummaryrefslogtreecommitdiff
path: root/subex/subexast.go
blob: cec75f7f8f54d9167d68102e5f52391071a7b17e (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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
package subex

import (
	"fmt"
	"main/walk"
)

// A node in the AST of a subex
type SubexAST interface {
	compileWith(next SubexState) SubexState
}

// Process the first subex, then the second, splitting the input text in two
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)
}

// Processing a subex and storing the output in a slot instead of outputting it
type SubexASTStore struct {
	match SubexAST
	slot rune
}
func (ast SubexASTStore) compileWith(next SubexState) SubexState {
	return &SubexStoreBeginState {
		next: ast.match.compileWith(&SubexStoreEndState {
			slot: ast.slot,
			next: next,
		}),
	}
}
func (ast SubexASTStore) String() string {
	return fmt.Sprintf("$%c(%v)", ast.slot, ast.match)
}

// Try to run the first subex, if it fails then backtrack and use the second
type SubexASTOr struct {
	first, second SubexAST
}
func (ast SubexASTOr) compileWith(next SubexState) SubexState {
	return &SubexGroupState {
		ast.first.compileWith(next),
		ast.second.compileWith(next),
	}
}

type ConvexRange struct {
	start, end int
}
func (cr ConvexRange) minmax() (int, int) {
		if cr.start == -1 {
			return cr.end, -1
		} else if cr.end == -1 {
			return cr.start, -1
		} else if cr.start < cr.end {
			return cr.start, cr.end
		} else {
			return cr.end, cr.start
		}
}
func (cr ConvexRange) decrement() ConvexRange {
	if cr.start == -1 {
		return ConvexRange{-1, cr.end - 1}
	} else if cr.end == -1 {
		return ConvexRange{cr.start - 1, -1}
	} else {
		return ConvexRange{cr.start - 1, cr.end - 1}
	}
}
func (cr ConvexRange) compile(content SubexAST, next SubexState) SubexState {
	min, _ := cr.minmax()
	if min != 0 {
		return content.compileWith(cr.decrement().compile(content, next))
	}
	if cr.start == -1 {
		state := &SubexGroupState {nil, next}
		state.first = content.compileWith(state)
		return state
	}
	if cr.end == -1 {
		state := &SubexGroupState {next, nil}
		state.second = content.compileWith(state)
		return state
	}

	if cr.end == 0 {
		state := next;
		for i := 0; i < cr.start; i += 1 {
			state = &SubexGroupState {
				content.compileWith(state),
				next,
			}
		}
		return state
	} else {
		state := next;
		for i := 0; i < cr.end; i += 1 {
			state = &SubexGroupState {
				next,
				content.compileWith(state),
			}
		}
		return state
	}
}

// Try to run the subex a number of times that is one of the numbers in the acceptable range
// Prioritising the left
type SubexASTRepeat struct {
	content SubexAST
	acceptable []ConvexRange
}
func (ast SubexASTRepeat) compileWith(next SubexState) SubexState {
	var state SubexState = &SubexDeadState{}
	for _, convex := range ast.acceptable {
		state = SubexGroupState {state, convex.compile(ast.content, next)}
	}
	return state
}

// Read in a single specific Atom and output it unchanged
type SubexASTCopyAtom struct {
	atom walk.Atom
}
func (ast SubexASTCopyAtom) compileWith(next SubexState) SubexState {
	return &SubexCopyAtomState{
		atom: ast.atom,
		next: next,
	}
}

// Read in any single Atom and output it unchanged
type SubexASTCopyAny struct {}
func (ast SubexASTCopyAny) compileWith(next SubexState) SubexState {
	return &SubexCopyAnyState{next}
}
func (ast SubexASTCopyAny) String() string {
	return "."
}

// Output a series of Atoms without reading anything from input
type SubexASTOutput struct {
	replacement []OutputContent
}
func (ast SubexASTOutput) compileWith(next SubexState) SubexState {
	return &SubexOutputState{
		content: ast.replacement,
		next: next,
	}
}

// Read in a repeated subex separated by a delimiter. Greedy
type SubexASTJoin struct {
	content, delimiter SubexAST
}
func (ast SubexASTJoin) compileWith(next SubexState) SubexState {
	afterContentState := &SubexGroupState {
		nil,
		next,
	}
	manyContentsState := ast.content.compileWith(afterContentState)
	afterContentState.first = ast.delimiter.compileWith(manyContentsState)
	return &SubexGroupState {
		manyContentsState,
		next,
	}
}

// Run each input Atom through a map to produce an output Atom
// Atoms not in the map cause this to not match
type SubexASTRange struct {
	parts map[walk.Atom]walk.Atom
}
func (ast SubexASTRange) compileWith(next SubexState) SubexState {
	return &SubexRangeState {
		parts: ast.parts,
		next: next,
	}
}

// Run content and then assume that output is a series of numbers, sum them and output the total
// Will cast strings, booleans and null to numbers
type SubexASTSum struct {
	content SubexAST
}
func (ast SubexASTSum) compileWith(next SubexState) SubexState {
	return &SubexSumBeginState {
		next: ast.content.compileWith(&SubexSumEndState {
			next: next,
		}),
	}
}