-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcapturing.go
229 lines (197 loc) · 4.86 KB
/
capturing.go
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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
package peg
import (
"fmt"
"strings"
)
// Underlying types implemented Pattern interface.
type (
patternLet struct {
pat Pattern
vars map[string]Pattern
}
patternCaptureVariable struct {
varname string
cons NonTerminalConstructor
}
patternCaptureToken struct {
pat Pattern
toktype int
cons TerminalConstructor
}
patternCaptureCons struct {
pat Pattern
cons NonTerminalConstructor
}
patternCaptureTerm struct {
pat Pattern
cons TerminalConstructor
}
)
// Let enters given namespace then invokes the entry pattern.
//
// Panics if any variable definition is nil.
func Let(vars map[string]Pattern, entry Pattern) Pattern {
for name, pat := range vars {
if pat == nil {
panic(errorUndefinedVar(name))
}
}
return &patternLet{pat: entry, vars: vars}
}
// V invokes a defined variable without capturing.
//
// A runtime error occurs when variable is undefined.
func V(varname string) Pattern {
return &patternCaptureVariable{
varname: varname,
cons: nil,
}
}
// CV invokes and captures a defined variable, the parse capture would be
// stored into a Variable-typed non-terminal when the pattern is matched.
//
// A runtime error occurs when variable is undefined.
func CV(varname string) Pattern {
return &patternCaptureVariable{
varname: varname,
cons: newVariableConstructor(varname),
}
}
// CK constructs a Token-typed terminal from the matched text.
func CK(toktype int, pat Pattern) Pattern {
return &patternCaptureToken{
pat: pat,
toktype: toktype,
cons: newTokenConstructor(toktype),
}
}
// CC constructs a customed non-terminal using user defined constructor.
func CC(cons NonTerminalConstructor, pat Pattern) Pattern {
return &patternCaptureCons{pat: pat, cons: cons}
}
// CT constructs a customed terminal using user defined constructor.
func CT(cons TerminalConstructor, pat Pattern) Pattern {
return &patternCaptureTerm{pat: pat, cons: cons}
}
func newVariableConstructor(name string) NonTerminalConstructor {
return func(subs []Capture) (Capture, error) {
return &Variable{Name: name, Subs: subs}, nil
}
}
func newTokenConstructor(toktype int) TerminalConstructor {
return func(span string, pos Position) (Capture, error) {
return &Token{Type: toktype, Value: span, Position: pos}, nil
}
}
// Setups variables.
func (pat *patternLet) match(ctx *context) error {
if !ctx.justReturned() {
// enter namespace
ctx.enter(pat.vars)
return ctx.call(pat.pat)
}
// leave namespace.
ret := ctx.ret
ctx.leave()
return ctx.returns(ret)
}
// Invokes vaiable and optionally captures it.
func (pat *patternCaptureVariable) match(ctx *context) error {
// lookup and invoke
if !ctx.justReturned() {
callee := ctx.lookup(pat.varname)
if callee == nil {
return errorUndefinedVar(pat.varname)
}
if pat.cons == nil {
// won't capture the variable
return ctx.execute(callee)
}
ctx.begin(pat.cons)
return ctx.call(callee)
}
// finish capturing
ret := ctx.ret
err := ctx.end(ret.ok)
if err != nil {
return err
}
return ctx.returns(ret)
}
// Captures text to construct a token.
func (pat *patternCaptureToken) match(ctx *context) error {
if !ctx.justReturned() {
return ctx.call(pat.pat)
}
ret := ctx.ret
if !ret.ok {
return ctx.predicates(false)
}
head := ctx.tell()
ctx.consume(ret.n)
term, err := pat.cons(ctx.span(), head)
if err != nil {
return err
}
err = ctx.push(term)
if err != nil {
return err
}
return ctx.commit()
}
// Captures using customed non-terminal constructor.
func (pat *patternCaptureCons) match(ctx *context) error {
if !ctx.justReturned() {
ctx.begin(pat.cons)
return ctx.call(pat.pat)
}
ret := ctx.ret
err := ctx.end(ret.ok)
if err != nil {
return err
}
return ctx.returns(ret)
}
// Captures text to construct terminal using customed constructor.
func (pat *patternCaptureTerm) match(ctx *context) error {
if !ctx.justReturned() {
return ctx.call(pat.pat)
}
ret := ctx.ret
if !ret.ok {
return ctx.predicates(false)
}
head := ctx.tell()
ctx.consume(ret.n)
term, err := pat.cons(ctx.span(), head)
if err != nil {
return err
}
err = ctx.push(term)
if err != nil {
return err
}
return ctx.commit()
}
func (pat *patternLet) String() string {
strs := make([]string, 0, len(pat.vars))
for name, value := range pat.vars {
strs = append(strs, fmt.Sprintf("$%s := %s", name, value))
}
return fmt.Sprintf("let (%s) in %s", strings.Join(strs, "; "), pat.pat)
}
func (pat *patternCaptureVariable) String() string {
if pat.cons == nil {
return fmt.Sprintf("$%s", pat.varname)
}
return fmt.Sprintf("${%s}", pat.varname)
}
func (pat *patternCaptureCons) String() string {
return fmt.Sprintf("cons_%p{%s}", pat.cons, pat.pat)
}
func (pat *patternCaptureToken) String() string {
return fmt.Sprintf("token_%d{%s}", pat.toktype, pat.pat)
}
func (pat *patternCaptureTerm) String() string {
return fmt.Sprintf("term_%p{%s}", pat.cons, pat.pat)
}