-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathparser-functions.dylan
349 lines (296 loc) · 13.4 KB
/
parser-functions.dylan
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
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
module: peg-parser
synopsis: PEG parser cached and uncached parser functions.
// See parser-rules.dylan for a full explanation of rule parsers. Basically,
// rule parsers parse a stream in a given context and return a value or sequence
// of values called the "product."
// This auxiliary macro generates the cached or uncached parser function.
define macro parser-function
{ parser-function cached,
#key ?token-name:name, ?token-type:expression, ?label:expression = #f,
??parser-attr:*, #all-keys;
end
} => {
define function "parse-" ## ?token-name
(stream :: <positionable-stream>, context :: <parse-context>)
=> (value :: false-or(?token-type), success? :: <boolean>, extent :: <parse-extent>)
let start-pos = stream.stream-position;
let start-pos-index = as(<integer>, start-pos);
indent-trace();
let pos-cache = element(context.cache, start-pos-index, default: #f);
let cached-result = pos-cache & element(pos-cache, ?#"token-name", default: #f);
let (value, success?, extent) =
if (cached-result)
// Result cached. Return appropriate values.
// Note a hit.
if (*parser-cache-hits*)
context.parser-cache-hits[?#"token-name"] :=
element(context.parser-cache-hits, ?#"token-name", default: 0) + 1;
end if;
if (cached-result.success-pos)
stream.stream-position := cached-result.success-pos;
let end-pos-index = as(<integer>, cached-result.success-pos);
format-trace("%s (cached) matched stream pos %x-%x",
?"token-name", start-pos-index, end-pos-index);
values(cached-result.semantic-value, #t,
cached-result.parse-extent);
else
let err = cached-result.parse-extent;
let fail-pos-index = as(<integer>, err.parse-position);
format-trace("%s (cached) no match, exp. %s at stream pos %x",
?"token-name", err.parse-expected, fail-pos-index);
values(cached-result.semantic-value, #f, err);
end if;
else
// Result not cached. Call parser function.
let parser-label = ?label;
format-trace("%s...", ?"token-name");
// Set up grammar attributes.
dynamic-bind (??parser-attr, ...)
// Call parser rule to get product.
let (prod, succ? :: <boolean>, ext :: <parse-extent>) =
?token-name ## "-parser-rule" (stream, context);
// Compute proposed semantic value.
let prop-value :: false-or(?token-type) =
succ? & ?token-name ## "-parser-value"
(context, prod, start-pos, stream.stream-position);
// Call user-defined afterwards clause, which may cause failure.
let after-error :: false-or(<parse-failure>) =
if (succ?)
let end-pos-index = as(<integer>, stream.stream-position);
"after-" ## ?token-name
(context, prod, prop-value, start-pos-index, end-pos-index)
end if;
if (after-error)
after-error.parse-position := after-error.parse-position | start-pos;
if (after-error.empty-failure-lists?)
after-error.parse-expected-list :=
list(format-to-string("valid %s", parser-label | "input"));
end if;
succ? := #f;
ext := after-error;
end if;
// Consolidate lower level extent descriptions into a better
// description for this parser. Best description of extents
// below this parser is after-error or this parser's own label.
// Sibling descriptions will be resolved by higher-level choice
// operators. If no better description, leave ext alone.
if (~after-error & parser-label)
if (instance?(ext, <parse-success>))
ext.parse-success-list := list(parser-label);
else
ext.parse-expected-list := list(parser-label);
ext.parse-expected-other-than-list := #();
end if;
end if;
// Earlier logic that assumes later errors are always better,
// in case I am wrong above.
// if (~succ? & err.parse-position = start-pos)
// if (parser-label & ~after-error)
// err.parse-expected-list := list(parser-label);
// err.parse-expected-other-than-list := #();
// end if;
// end if;
// Log results of parsing.
if (succ?)
let end-pos-index = as(<integer>, stream.stream-position);
format-trace("%s matched stream pos %x-%x",
?"token-name", start-pos-index, end-pos-index);
else
let fail-pos-index = as(<integer>, ext.parse-position);
format-trace("%s no match, exp. %s at stream pos %x",
?"token-name", ext.parse-expected, fail-pos-index);
end if;
// Compute actual semantic value.
let value :: false-or(?token-type) = succ? & prop-value;
// Call user-defined cleanup clause.
"cleanup-" ## ?token-name (context, value, succ?, ext);
// Store in cache. Get pos-cache again, because lower productions
// may have changed the cache at this position.
let pos-cache = element(context.cache, start-pos-index, default: #f)
| make(<table>);
context.cache[start-pos-index] := pos-cache;
pos-cache[?#"token-name"] :=
make(<parse-result>, value: value, extent: ext,
success-pos: succ? & stream.stream-position);
// Return values.
values(value, succ?, ext);
end dynamic-bind;
end if;
outdent-trace();
values(value, success?, extent);
end function
}
{ parser-function uncached,
#key ?token-name:name, ?token-type:expression, ?label:expression = #f,
??parser-attr:*, #all-keys;
end
} => {
define function "parse-" ## ?token-name
(stream :: <positionable-stream>, context :: <parse-context>)
=> (value :: false-or(?token-type), success? :: <boolean>, extent :: <parse-extent>)
let start-pos = stream.stream-position;
let start-pos-index = as(<integer>, start-pos);
indent-trace();
let parser-label = ?label;
format-trace("%s...", ?"token-name");
// Set up grammar attributes.
dynamic-bind (??parser-attr, ...)
// Call parser rule to get product.
let (prod, succ? :: <boolean>, ext :: <parse-extent>) =
?token-name ## "-parser-rule" (stream, context);
// Compute proposed semantic value.
let prop-value :: false-or(?token-type) =
succ? & ?token-name ## "-parser-value"
(context, prod, start-pos, stream.stream-position);
// Call user-defined afterwards clause, which may cause failure.
let after-error :: false-or(<parse-failure>) =
if (succ?)
let end-pos-index = as(<integer>, stream.stream-position);
"after-" ## ?token-name
(context, prod, prop-value, start-pos-index, end-pos-index)
end if;
if (after-error)
after-error.parse-position := after-error.parse-position | start-pos;
if (after-error.empty-failure-lists?)
after-error.parse-expected-list :=
list(format-to-string("valid %s", parser-label | "input"));
end if;
succ? := #f;
ext := after-error;
end if;
// Consolidate lower level extent descriptions into a better
// description for this parser. Best description of extents
// below this parser is after-error or this parser's own label.
// Sibling descriptions will be resolved by higher-level choice
// operators. If no better description, leave ext alone.
if (~after-error & parser-label)
if (instance?(ext, <parse-success>))
ext.parse-success-list := list(parser-label);
else
ext.parse-expected-list := list(parser-label);
ext.parse-expected-other-than-list := #();
end if;
end if;
// Earlier logic that assumes later errors are always better,
// in case I am wrong above.
// if (~succ? & err.parse-position = start-pos)
// if (parser-label & ~after-error)
// err.parse-expected-list := list(parser-label);
// err.parse-expected-other-than-list := #();
// end if;
// end if;
// Log results of parsing.
if (succ?)
let end-pos-index = as(<integer>, stream.stream-position);
format-trace("%s matched stream pos %x-%x",
?"token-name", start-pos-index, end-pos-index);
else
let fail-pos-index = as(<integer>, ext.parse-position);
format-trace("%s no match, exp. %s at stream pos %x",
?"token-name", ext.parse-expected, fail-pos-index);
end if;
// Compute actual semantic value.
let value :: false-or(?token-type) = succ? & prop-value;
// Call user-defined cleanup clause.
"cleanup-" ## ?token-name (context, value, succ?, ext);
// Return values.
outdent-trace();
values(value, succ?, ext);
end dynamic-bind;
end function
}
end macro;
// This auxiliary macro turns slot clauses into a class declaration.
define macro class-specifier
{ class-specifier
#key ?token-type:name, ??super:name = <token>, ??slot, #all-keys;
end
} => {
define class ?token-type (??super, ...)
??slot; ...
end class
}
slot:
{ ?:variable } => { slot ?variable }
{ ?:variable = ?:expression } => { slot ?variable }
{ inherited ?:name = ?:expression } => { inherited slot ?name }
end macro;
// This auxiliary macro turns slot clauses into an initialize function. The
// initialization expressions will be in terms of ?product-name and ?context-name.
define macro initialize-specifier
{ initialize-specifier
#key ?token-type:name, ?product-name:name, ?product-type:expression,
?context-name:name, ?context-type:expression, ??slot, #all-keys;
end
} => {
define method initialize
(token :: ?token-type, #next next-method,
#key ?product-name :: type-union(?product-type, singleton(unsupplied()))
= unsupplied(),
?context-name :: type-union(?context-type, singleton(unsupplied()))
= unsupplied())
next-method(token);
if (supplied?(?product-name) & supplied?(?context-name))
??slot; ...
end if;
end method;
}
slot:
{ ?slot-name:name :: ?slot-type:expression = ?:expression }
=> { token.?slot-name := ?expression }
{ inherited ?slot-name:name = ?:expression }
=> { token.?slot-name := ?expression }
{ ?slot-name:name :: ?slot-type:expression }
=> { #f /* because Gwydion Dylan prefers ";#f;" to ";;" */ }
end macro;
// These auxiliary macro generates the after- and cleanup- functions. These
// functions are not inlined for clearer debugging.
define macro after-function
{ after-function
#key ?token-name:name, ?after-ctxt:variable, ?after-prod:variable,
?after-value:variable, ?after-start:variable, ?after-end:variable,
?after-body:expression, ?after-fail:name,
#all-keys;
end
} => {
define function "after-" ## ?token-name
(?after-ctxt, ?after-prod, ?after-value, ?after-start, ?after-end)
=> (new-err :: false-or(<parse-failure>))
block (?after-fail)
?after-body; #f
end block
end function
}
{ after-function
#key ?token-name:name, #all-keys;
end
} => {
define constant "after-" ## ?token-name :: <function>
= do-nothing
}
end macro;
define macro cleanup-function
{ cleanup-function
#key ?token-name:name, ?cleanup-ctxt:variable, ?cleanup-value:variable,
?cleanup-succ:variable, ?cleanup-ext:variable,
?cleanup-body:expression,
#all-keys;
end
} => {
define function "cleanup-" ## ?token-name
(?cleanup-ctxt, ?cleanup-value, ?cleanup-succ, ?cleanup-ext)
=> ()
?cleanup-body
end function
}
{ cleanup-function
#key ?token-name:name, #all-keys;
end
} => {
define constant "cleanup-" ## ?token-name :: <function>
= do-nothing
}
end macro;
define function do-nothing (#rest anything) => (false :: singleton(#f))
#f
end function;