This repository has been archived by the owner on Feb 3, 2021. It is now read-only.
/
hll-grammar.pir
469 lines (373 loc) · 11.9 KB
/
hll-grammar.pir
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
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
# Copyright (C) 2009 Patrick R. Michaud
=head1 TITLE
src/cheats/hll-grammar.pir -- Additional HLL::Grammar methods
=head2 Methods
=over 4
=cut
.include 'cclass.pasm'
.include 'src/Regex/constants.pir'
.namespace ['HLL';'Grammar']
.sub '' :load :init
load_bytecode 'P6Regex.pbc'
.local pmc brackets
brackets = box unicode:"<>[](){}\xab\xbb\u0f3a\u0f3b\u0f3c\u0f3d\u169b\u169c\u2045\u2046\u207d\u207e\u208d\u208e\u2329\u232a\u2768\u2769\u276a\u276b\u276c\u276d\u276e\u276f\u2770\u2771\u2772\u2773\u2774\u2775\u27c5\u27c6\u27e6\u27e7\u27e8\u27e9\u27ea\u27eb\u2983\u2984\u2985\u2986\u2987\u2988\u2989\u298a\u298b\u298c\u298d\u298e\u298f\u2990\u2991\u2992\u2993\u2994\u2995\u2996\u2997\u2998\u29d8\u29d9\u29da\u29db\u29fc\u29fd\u3008\u3009\u300a\u300b\u300c\u300d\u300e\u300f\u3010\u3011\u3014\u3015\u3016\u3017\u3018\u3019\u301a\u301b\u301d\u301e\ufd3e\ufd3f\ufe17\ufe18\ufe35\ufe36\ufe37\ufe38\ufe39\ufe3a\ufe3b\ufe3c\ufe3d\ufe3e\ufe3f\ufe40\ufe41\ufe42\ufe43\ufe44\ufe47\ufe48\ufe59\ufe5a\ufe5b\ufe5c\ufe5d\ufe5e\uff08\uff09\uff3b\uff3d\uff5b\uff5d\uff5f\uff60\uff62\uff63"
set_global '$!brackets', brackets
.end
.include 'src/gen/hll-grammar.pir'
.include 'src/gen/hll-actions.pir'
.namespace ['HLL';'Grammar']
=item O(spec [, save])
This subrule attaches operator precedence information to
a match object (such as an operator token). A typical
invocation for the subrule might be:
token infix:sym<+> { <sym> <O( q{ %additive, :pirop<add> } )> }
This says to add all of the attribute of the C<%addition> hash
(described below) and a C<pirop> entry into the match object
returned by the C<< infix:sym<+> >> token (as the C<O> named
capture). Note that this is a alphabetic 'O", not a digit zero.
Currently the C<O> subrule accepts a string argument describing
the hash to be stored. (Note the C< q{ ... } > above. Eventually
it may be possible to omit the 'q' such that an actual (constant)
hash constructor is passed as an argument to C<O>.
The hash built via the string argument to C<O> is cached, so that
subsequent parses of the same token re-use the hash built from
previous parses of the token, rather than building a new hash
on each invocation.
The C<save> argument is used to build "hash" aggregates that can
be referred to by subsequent calls to C<O>. For example,
NQP::Grammar.O(':prec<t=>, :assoc<left>', '%additive' );
specifies the values to be associated with later references to
"%additive". Eventually it will likely be possible to use true
hashes from a package namespace, but this works for now.
Currently the only pairs recognized have the form C< :pair >,
C< :!pair >, and C<< :pair<strval> >>.
=cut
.sub 'O' :method
.param string spec
.param string save :optional
.param int has_save :opt_flag
# First, get the hash cache. Right now we have one
# cache for all grammars; eventually we may need a way to
# separate them out by cursor type.
.local pmc ohash
ohash = get_global '%!ohash'
unless null ohash goto have_ohash
ohash = new ['Hash']
set_global '%!ohash', ohash
have_ohash:
# See if we've already created a Hash for the current
# specification string -- if so, use that.
.local pmc hash
hash = ohash[spec]
unless null hash goto hash_done
# Otherwise, we need to build a new one.
hash = new ['Hash']
.local int pos, eos
pos = 0
eos = length spec
spec_loop:
pos = find_not_cclass .CCLASS_WHITESPACE, spec, pos, eos
if pos >= eos goto spec_done
$S0 = substr spec, pos, 1
if $S0 == ',' goto spec_comma
if $S0 == ':' goto spec_pair
# If whatever we found doesn't start with a colon, treat it
# as a lookup of a previously saved hash to be merged in.
.local string lookup
.local int lpos
# Find the first whitespace or comma
lpos = find_cclass .CCLASS_WHITESPACE, spec, pos, eos
$I0 = index spec, ',', pos
if $I0 < 0 goto have_lookup_lpos
if $I0 >= lpos goto have_lookup_lpos
lpos = $I0
have_lookup_lpos:
$I0 = lpos - pos
lookup = substr spec, pos, $I0
.local pmc lhash, lhash_it
lhash = ohash[lookup]
if null lhash goto err_lookup
lhash_it = iter lhash
lhash_loop:
unless lhash_it goto lhash_done
$S0 = shift lhash_it
$P0 = lhash[$S0]
hash[$S0] = $P0
goto lhash_loop
lhash_done:
pos = lpos
goto spec_loop
# We just ignore commas between elements for now.
spec_comma:
inc pos
goto spec_loop
# If we see a colon, then we want to parse whatever
# comes next like a pair.
spec_pair:
# eat colon
inc pos
.local string name
.local pmc value
value = new ['Boolean']
# If the pair is of the form :!name, then reverse the value
# and skip the colon.
$S0 = substr spec, pos, 1
$I0 = iseq $S0, '!'
pos += $I0
$I0 = not $I0
value = $I0
# Get the name of the pair.
lpos = find_not_cclass .CCLASS_WORD, spec, pos, eos
$I0 = lpos - pos
name = substr spec, pos, $I0
pos = lpos
# Look for a <...> that follows.
$S0 = substr spec, pos, 1
unless $S0 == '<' goto have_value
inc pos
lpos = index spec, '>', pos
$I0 = lpos - pos
$S0 = substr spec, pos, $I0
value = box $S0
pos = lpos + 1
have_value:
# Done processing the pair, store it in the hash.
hash[name] = value
goto spec_loop
spec_done:
# Done processing the spec string, cache the hash for later.
ohash[spec] = hash
hash_done:
# If we've been called as a subrule, then build a pass-cursor
# to indicate success and set the hash as the subrule's match object.
if has_save goto save_hash
($P0, $I0) = self.'!cursor_start'()
$P0.'!cursor_pass'($I0, '')
setattribute $P0, '$!match', hash
.return ($P0)
# save the hash under a new entry
save_hash:
ohash[save] = hash
.return (self)
err_lookup:
self.'panic'('Unknown operator precedence specification "', lookup, '"')
.end
=item panic([args :slurpy])
Throw an exception at the current cursor location. If the message
doesn't end with a newline, also output the line number and offset
of the match.
=cut
.sub 'panic' :method
.param pmc args :slurpy
.local int pos
.local pmc target
pos = self.'pos'()
target = getattribute self, '$!target'
$I1 = target.'lineof'(pos)
push args, ' at line '
push args, $I1
$S0 = target
$S0 = substr $S0, pos, 10
$S0 = escape $S0
push args, ', near "'
push args, $S0
push args, '"'
.local string message
message = join '', args
die message
.end
=item peek_delimiters(target, pos)
Return the start/stop delimiter pair based on peeking at C<target>
position C<pos>.
=cut
.sub 'peek_delimiters' :method
.param string target
.param int pos
.local string brackets, start, stop
$P0 = get_global '$!brackets'
brackets = $P0
# peek at the next character
start = substr target, pos, 1
# colon and word characters aren't valid delimiters
if start == ':' goto err_colon_delim
$I0 = is_cclass .CCLASS_WORD, start, 0
if $I0 goto err_word_delim
$I0 = is_cclass .CCLASS_WHITESPACE, start, 0
if $I0 goto err_ws_delim
# assume stop delim is same as start, for the moment
stop = start
# see if we have an opener or closer
$I0 = index brackets, start
if $I0 < 0 goto bracket_end
# if it's a closing bracket, that's an error also
$I1 = $I0 % 2
if $I1 goto err_close
# it's an opener, so get the closing bracket
inc $I0
stop = substr brackets, $I0, 1
# see if the opening bracket is repeated
.local int len
len = 0
bracket_loop:
inc pos
inc len
$S0 = substr target, pos, 1
if $S0 == start goto bracket_loop
if len == 1 goto bracket_end
start = repeat start, len
stop = repeat stop, len
bracket_end:
.return (start, stop)
err_colon_delim:
self.'panic'('Colons may not be used to delimit quoting constructs')
err_word_delim:
self.'panic'('Alphanumeric character is not allowed as a delimiter')
err_ws_delim:
self.'panic'('Whitespace character is not allowed as a delimiter')
err_close:
self.'panic'('Use of a closing delimiter for an opener is reserved')
.end
.sub 'quote_EXPR' :method
.param pmc args :slurpy
.local pmc cur
.local string target
.local int pos
(cur, pos, target) = self.'!cursor_start'()
.local pmc start, stop
(start, stop) = self.'peek_delimiters'(target, pos)
.lex '$*QUOTE_START', start
.lex '$*QUOTE_STOP', stop
$P10 = cur.'quote_delimited'()
unless $P10 goto fail
cur.'!mark_push'(0, CURSOR_FAIL, 0, $P10)
$P10.'!cursor_names'('quote_delimited')
pos = $P10.'pos'()
cur.'!cursor_pass'(pos, 'quote_EXPR')
fail:
.return (cur)
.end
.sub 'starter' :method
.local pmc cur
.local string target, start
.local int pos
(cur, pos, target) = self.'!cursor_start'()
$P0 = find_dynamic_lex '$*QUOTE_START'
if null $P0 goto fail
start = $P0
$I0 = length start
$S0 = substr target, pos, $I0
unless $S0 == start goto fail
pos += $I0
cur.'!cursor_pass'(pos, 'starter')
fail:
.return (cur)
.end
.sub 'stopper' :method
.local pmc cur
.local string target, start
.local int pos
(cur, pos, target) = self.'!cursor_start'()
$P0 = find_dynamic_lex '$*QUOTE_STOP'
if null $P0 goto fail
start = $P0
$I0 = length start
$S0 = substr target, pos, $I0
unless $S0 == start goto fail
pos += $I0
cur.'!cursor_pass'(pos, 'stopper')
fail:
.return (cur)
.end
=item EXPR(...)
An operator precedence parser.
=cut
.sub 'EXPR' :method
.const 'Sub' reduce = 'EXPR_reduce'
.local string termish
termish = 'termish'
.local pmc opstack, termstack
opstack = new ['ResizablePMCArray']
.lex '@opstack', opstack
termstack = new ['ResizablePMCArray']
.lex '@termstack', termstack
.local pmc here, from, pos
(here, pos) = self.'!cursor_start'()
term_loop:
here = here.termish()
unless here goto fail
.local pmc term
term = here.'MATCH'()
push termstack, term
.local pmc infixcur, infix
here = here.'ws'()
infixcur = here.'infixish'()
unless infixcur goto term_done
infix = infixcur.'MATCH'()
unless opstack goto reduce_done
.local string inprec, inassoc, opprec
$P0 = infix['O']
inprec = $P0['prec']
inassoc = $P0['assoc']
unless inprec goto err_inprec
reduce_loop:
$P0 = opstack[-1]
$P0 = $P0['O']
opprec = $P0['prec']
unless opprec > inprec goto reduce_gt_done
capture_lex reduce
self.reduce()
goto reduce_loop
reduce_gt_done:
unless opprec == inprec goto reduce_done
# equal precedence, use associativity to decide
unless inassoc == 'left' goto reduce_done
# left associative, reduce immediately
capture_lex reduce
self.reduce()
reduce_done:
push opstack, infix # The Shift
here = infixcur.'ws'()
goto term_loop
term_done:
opstack_loop:
unless opstack goto opstack_done
capture_lex reduce
self.reduce()
goto opstack_loop
opstack_done:
expr_done:
term = pop termstack
pos = here.'pos'()
here = self.'!cursor_start'()
setattribute here, '$!pos', pos
setattribute here, '$!match', term
here.'!reduce'('EXPR')
fail:
.return (here)
err_internal:
$I0 = termstack
here.'panic'('Internal operator parser error, @termstack == ', $I0)
err_inprec:
infixcur.'panic'('Missing infixish operator precedence')
.end
.sub 'EXPR_reduce' :method :anon :outer('EXPR')
.local pmc termstack, opstack
termstack = find_lex '@termstack'
opstack = find_lex '@opstack'
.local pmc op
op = pop opstack
op_infix:
.local pmc right, left
right = pop termstack
left = pop termstack
op[0] = left
op[1] = right
self.'!reduce'('EXPR', 'INFIX', op)
push termstack, op
.end
=cut
# Local Variables:
# mode: pir
# fill-column: 100
# End:
# vim: expandtab shiftwidth=4 ft=pir: