This repository has been archived by the owner on Jun 9, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
grammar51.pir
624 lines (539 loc) · 14.6 KB
/
grammar51.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
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
# Copyright (C) 2006-2009, Parrot Foundation.
# $Id$
=head1 elements of grammar for Lua 5.1
=head2 Description
Used by F<languages/lua/lua.pir> and F<languages/lua/test_lex.pir>.
=cut
.namespace [ 'Lua';'Grammar' ]
=head3 Functions
Some grammar routines are handly written in PIR.
See "Lua 5.1 Reference Manual", section 2.1 "Lexical Conventions",
L<http://www.lua.org/manual/5.1/manual.html#2.1>.
=over 4
=item C<syntaxerror (match, [message])>
=cut
.sub 'syntaxerror' :method
.param string message :optional
.param pmc adv :slurpy :named
unless null message goto L1
message = 'syntax error'
L1:
lexerror(self, message)
.end
.sub 'lexerror' :anon
.param pmc mob
.param string message
.local int lineno
# .local pmc infile
# infile = get_hll_global ['TGE';'Compiler'], '$!infile'
# $S0 = infile
# $S0 .= ':'
$S0 = '_._:'
$P0 = get_hll_global ['PGE';'Util'], 'line_number'
lineno = mob.$P0()
inc lineno
$S1 = lineno
$S0 .= $S1
$S0 .= ': '
$S0 .= message
$S1 = mob.'Str'()
if $S1 == '' goto L1
$S0 .= " near '"
$S0 .= $S1
$S0 .= "'"
L1:
die $S0
.end
=item C<name>
I<Names> (also called I<identifiers>) in Lua can be any string of letters,
digits, and underscores, not beginning with a digit. This coincides with the
definition of names in most languages. (The definition of letter depends on
the current locale: any character considered alphabetic by the current locale
can be used in an identifier.) Identifiers are used to name variables and
table fields.
The following keywords are reserved and cannot be used as names:
and break do else elseif
end false for function if
in local nil not or
repeat return then true until while
Lua is a case-sensitive language: C<and> is a reserved word, but C<And> and
C<AND> are two different, valid names. As a convention, names starting with
an underscore followed by uppercase letters (such as C<_VERSION>) are reserved
for internal global variables used by Lua.
=cut
.include 'cclass.pasm'
.sub 'Name'
.param pmc tgt
.param pmc adverbs :slurpy :named
.local string target
.local int pos, lastpos
.local pmc mob
$P0 = get_hll_global ['PGE'], 'Match'
(mob, pos, target) = $P0.'new'(tgt)
lastpos = length target
$S0 = substr target, pos, 1
if $S0 == '_' goto L1
$I0 = is_cclass .CCLASS_ALPHABETIC, target, pos
if $I0 == 0 goto L2
L1:
$I0 = pos
pos = find_not_cclass .CCLASS_WORD, target, pos, lastpos
$I1 = pos - $I0
$S0 = substr target, $I0, $I1
.local pmc kw
kw = get_global 'keyword'
unless null kw goto L3
kw = _const_keyword()
set_global 'keyword', kw
L3:
$I0 = exists kw[$S0]
if $I0 goto L2
mob.'to'(pos)
L2:
.return (mob)
.end
.sub _const_keyword :anon
.local pmc kw
new kw, 'Hash'
kw['and'] = 1
kw['break'] = 1
kw['do'] = 1
kw['else'] = 1
kw['elseif'] = 1
kw['end'] = 1
kw['false'] = 1
kw['for'] = 1
kw['function'] = 1
kw['if'] = 1
kw['in'] = 1
kw['local'] = 1
kw['nil'] = 1
kw['not'] = 1
kw['or'] = 1
kw['repeat'] = 1
kw['return'] = 1
kw['then'] = 1
kw['true'] = 1
kw['until'] = 1
kw['while'] = 1
.return (kw)
.end
=item C<number>
A I<numerical constant> may be written with an optional decimal part and an
optional decimal exponent. Lua also accepts integer hexadecimal constants,
by prefixing them with C<0x>. Examples of valid numerical constants are
3 3.0 3.1416 314.16e-2 0.31416E1 0xff 0x56
=cut
.sub 'number'
.param pmc mob
.param pmc params :slurpy
mob = read_numeral(mob)
unless mob goto L1
.local string target
.local int pos, lastpos
target = mob.'Str'()
lastpos = length target
L_alt1: # 0 [Xx] <xdigit>+
pos = 0
$S0 = substr target, pos, 1
unless $S0 == '0' goto L_alt2
inc pos
$S0 = substr target, pos, 1
$I0 = index 'Xx', $S0
if $I0 < 0 goto L_alt2
inc pos
$I0 = is_cclass .CCLASS_HEXADECIMAL, target, pos
if $I0 == 0 goto L_alt2
pos = find_not_cclass .CCLASS_HEXADECIMAL, target, pos, lastpos
goto L_end
L_alt2: # <digit>+ [\.]?
pos = 0
$I0 = is_cclass .CCLASS_NUMERIC, target, pos
if $I0 == 0 goto L_alt3
pos = find_not_cclass .CCLASS_NUMERIC, target, pos, lastpos
$S0 = substr target, pos, 1
unless $S0 == '.' goto L_opt2
inc pos
goto L_opt1
L_alt3: # \. <digit>
pos = 0
$S0 = substr target, pos, 1
unless $S0 == '.' goto L_end
inc pos
$I0 = is_cclass .CCLASS_NUMERIC, target, pos
if $I0 == 0 goto L_end
L_opt1: # <digit>*
pos = find_not_cclass .CCLASS_NUMERIC, target, pos, lastpos
L_opt2: # [Ee] [+\-]? <digit>+
.local int pos2
pos2 = pos
$S0 = substr target, pos2, 1
$I0 = index 'Ee', $S0
if $I0 < 0 goto L_end
inc pos2
$S0 = substr target, pos2, 1
$I0 = index '+-', $S0
if $I0 < 0 goto L_opt3
inc pos2
L_opt3:
$I0 = is_cclass .CCLASS_NUMERIC, target, pos2
if $I0 == 0 goto L_end
pos = find_not_cclass .CCLASS_NUMERIC, target, pos2, lastpos
L_end:
if pos == lastpos goto L1
lexerror(mob, 'malformed number')
L1:
.return (mob)
.end
.sub 'read_numeral' :anon
.param pmc tgt
.param pmc adverbs :slurpy :named
.local string target
.local int pos, lastpos
.local pmc mob
$P0 = get_hll_global ['PGE'], 'Match'
(mob, pos, target) = $P0.'new'(tgt)
lastpos = length target
$S0 = substr target, pos, 1
unless $S0 == '.' goto L1
inc pos
L1:
$I0 = is_cclass .CCLASS_NUMERIC, target, pos
if $I0 == 0 goto L2
inc pos
L3:
$I0 = is_cclass .CCLASS_NUMERIC, target, pos
if $I0 == 0 goto L4
pos = find_not_cclass .CCLASS_NUMERIC, target, pos, lastpos
$S0 = substr target, pos, 1
unless $S0 == '.' goto L5
inc pos
goto L3
L4:
$S0 = substr target, pos, 1
unless $S0 == '.' goto L5
inc pos
goto L3
L5:
$S0 = substr target, pos, 1
$I0 = index 'Ee', $S0
if $I0 < 0 goto L6
inc pos
$S0 = substr target, pos, 1
$I0 = index '+-', $S0
if $I0 < 0 goto L6
inc pos
L6:
$I0 = .CCLASS_NUMERIC | .CCLASS_WORD
pos = find_not_cclass $I0, target, pos, lastpos
mob.'to'(pos)
L2:
.return (mob)
.end
=item C<quoted_literal>
I<Literal strings> can be delimited by matching single or double quotes,
and can contain the following C-like escape sequences: C<'\a'> (bell),
C<'\b'> (backspace), C<'\f'> (form feed), C<'\n'> (newline), C<'\r'>
(carriage return), C<'\t'> (horizontal tab), C<'\v'> (vertical tab),
C<'\\'> (backslash), C<'\"'> (quotation mark [double quote]),
and C<'\''> (apostrophe [single quote]). Moreover, a backslash followed by
a real newline results in a newline in the string. A character in a string
may also be specified by its numerical value using the escape sequence C<\ddd>,
where I<ddd> is a sequence of up to three decimal digits. (Note that if a
numerical escape is to be followed by a digit, it must be expressed using
exactly three digits.) Strings in Lua may contain any 8-bit value, including
embedded zeros, which can be specified as C<'\0'>.
To put a double (single) quote, a newline, a backslash, or an embedded zero
inside a literal string enclosed by double (single) quotes you must use an
escape sequence. Any other character may be directly inserted into the literal.
(Some control characters may cause problems for the file system, but Lua has
no problem with them.)
=cut
.sub 'quoted_literal'
.param pmc tgt
.param string delim
.param pmc adv :slurpy :named
.local string target
.local int pos, lastpos
.local pmc mob
$P0 = get_hll_global ['PGE'], 'Match'
(mob, pos, target) = $P0.'new'(tgt)
lastpos = length target
.local string literal
literal = ''
LOOP:
if pos < lastpos goto L1
mob.'to'(pos)
lexerror(mob, "unfinished string")
L1:
$S0 = substr target, pos, 1
if $S0 != delim goto L2
mob.'!make'(literal)
mob.'to'(pos)
.return (mob)
L2:
$I0 = index "\n\r", $S0
if $I0 < 0 goto L3
mob.'to'(pos)
lexerror(mob, "unfinished string")
L3:
if $S0 != "\\" goto CONCAT
inc pos
if pos == lastpos goto LOOP # error
$S0 = substr target, pos, 1
$I0 = index 'abfnrtv', $S0
if $I0 < 0 goto L4
$S0 = substr "\a\b\f\n\r\t\x0b", $I0, 1
goto CONCAT
L4:
$I0 = index "\n\r", $S0
if $I0 < 0 goto L5
$S0 = "\n"
goto CONCAT
L5:
$I0 = index '0123456789', $S0
if $I0 < 0 goto CONCAT
inc pos
$S0 = substr target, pos, 1
$I1 = index '0123456789', $S0
if $I1 < 0 goto L6
$I0 *= 10
$I0 += $I1
inc pos
$S0 = substr target, pos, 1
$I1 = index '0123456789', $S0
if $I1 < 0 goto L6
$I0 *= 10
$I0 += $I1
goto L7
L6:
dec pos
L7:
if $I0 < 256 goto L8
mob.'to'(pos)
lexerror(mob, "escape sequence too large")
L8:
$S0 = chr $I0
CONCAT:
concat literal, $S0
inc pos
goto LOOP
.end
=item C<long_string>
Literal strings can also be defined using a long format enclosed by
I<long brackets>. We define an I<opening long bracket of level n> as an
opening square bracket followed by I<n> equal signs followed by another
opening square bracket. So, an opening long bracket of level 0 is written
as C<[[>, an opening long bracket of level 1 is written as C<[=[>, and so on.
A I<closing long bracket> is defined similarly; for instance, a closing long
bracket of level 4 is written as C<]====]>. A long string starts with an
opening long bracket of any level and ends at the first closing long bracket
of the same level. Literals in this bracketed form may run for several lines,
do not interpret any escape sequences, and ignore long brackets of any other
level. They may contain anything except a closing bracket of the proper level.
For convenience, when the opening long bracket is immediately followed by
a newline, the newline is not included in the string. As an example, in
a system using ASCII (in which C<'a'> is coded as 97, newline is coded as 10,
and C<'1'> is coded as 49), the five literals below denote the same string:
a = 'alo\n123"'
a = "alo\n123\""
a = '\97lo\10\04923"'
a = [[alo
123"]]
a = [==[
alo
123"]==]
=cut
.sub 'long_string'
.param pmc tgt
.param pmc adv :slurpy :named
.local string target
.local int pos, lastpos
.local pmc mob
$P0 = get_hll_global ['PGE'], 'Match'
(mob, pos, target) = $P0.'new'(tgt)
lastpos = length target
.local int sep
sep = 0
$S0 = substr target, pos, 1
if $S0 != '[' goto END
inc pos
(pos, sep) = _skip_sep(target, pos, '[')
if sep >= 0 goto L1
if sep == -1 goto END
mob.'to'(pos)
lexerror(mob, "invalid long string delimiter")
L1:
inc pos
$S0 = substr target, pos, 1
$I0 = index "\n\r", $S0
if $I0 < 0 goto L2
inc pos
L2:
.local string literal
literal = ''
LOOP:
if pos < lastpos goto L3
lexerror(mob, "unfinished long string")
L3:
$S0 = substr target, pos, 1
if $S0 != '[' goto L4
inc pos
($I0, $I1) = _skip_sep(target, pos, '[')
if $I1 != sep goto L5
if sep != 0 goto L5
inc pos
mob.'to'(pos)
lexerror(mob, "nesting of [[...]] is deprecated")
L5:
dec pos
goto CONCAT
L4:
if $S0 != ']' goto L6
inc pos
($I0, $I1) = _skip_sep(target, pos, ']')
if $I1 != sep goto L7
pos = $I0 + 1
mob.'!make'(literal)
mob.'to'(pos)
goto END
L7:
dec pos
goto CONCAT
L6:
if $S0 != "\r" goto L8
$S0 = ''
goto CONCAT
L8:
CONCAT:
concat literal, $S0
inc pos
goto LOOP
END:
.return (mob)
.end
=item C<long_comment>
A I<comment> starts with a double hyphen (C<-->) anywhere outside a string.
If the text immediately after C<--> is not an opening long bracket,
the comment is a I<short comment>, which runs until the end of the line.
Otherwise, it is a I<long comment>, which runs until the corresponding closing
long bracket. Long comments are frequently used to disable code temporarily.
=cut
.sub 'long_comment'
.param pmc tgt
.param pmc adv :slurpy :named
.local string target
.local int pos, lastpos
.local pmc mob
$P0 = get_hll_global ['PGE'], 'Match'
(mob, pos, target) = $P0.'new'(tgt)
lastpos = length target
.local int sep
sep = 0
$S0 = substr target, pos, 1
if $S0 != '[' goto END
inc pos
(pos, sep) = _skip_sep(target, pos, '[')
if sep < 0 goto END
inc pos
# $S0 = substr target, pos, 1
# $I0 = index "\n\r", $S0
# if $I0 < 0 goto L2
# inc pos
# L2:
# .local string literal
# literal = ''
LOOP:
if pos < lastpos goto L3
lexerror(mob, "unfinished long comment")
L3:
$S0 = substr target, pos, 1
if $S0 != '[' goto L4
inc pos
($I0, $I1) = _skip_sep(target, pos, '[')
if $I1 != sep goto L5
if sep != 0 goto L5
inc pos
mob.'to'(pos)
lexerror(mob, "nesting of [[...]] is deprecated")
L5:
dec pos
goto CONCAT
L4:
if $S0 != ']' goto L6
inc pos
($I0, $I1) = _skip_sep(target, pos, ']')
if $I1 != sep goto L7
pos = $I0 + 1
# mob.'!make'(literal)
mob.'to'(pos)
goto END
L7:
dec pos
goto CONCAT
L6:
# if $S0 != "\r" goto L8
# $S0 = ''
# goto CONCAT
# L8:
CONCAT:
# concat literal, $S0
inc pos
goto LOOP
END:
.return (mob)
.end
.sub '_skip_sep' :anon
.param string target
.param int pos
.param string delim
.local int count
count = 0
L1:
$S0 = substr target, pos, 1
if $S0 != '=' goto L2
inc count
inc pos
goto L1
L2:
if $S0 == delim goto L3
neg count
dec count
L3:
.return (pos, count)
.end
=item C<unexpected>
=cut
.sub 'unexpected'
.param pmc tgt
.param pmc adverbs :slurpy :named
.local string target
.local int pos, lastpos
.local pmc mob
$P0 = get_hll_global ['PGE'], 'Match'
(mob, pos, target) = $P0.'new'(tgt)
$I0 = index target, ';', pos
unless $I0 > 1 goto L1
lastpos = length target
$I1 = find_not_cclass .CCLASS_WORD, target, pos, lastpos
$I2 = $I1 - pos
unless $I2 goto L2
$S0 = substr target, pos, $I2
if $S0 == 'end' goto L1
if $S0 == 'else' goto L1
if $S0 == 'elseif' goto L1
if $S0 == 'until' goto L1
$I0 = $I1
L2:
mob.'to'($I0)
lexerror(mob, "unexpected symbol")
L1:
.return (mob)
.end
=back
=cut
# Local Variables:
# mode: pir
# fill-column: 100
# End:
# vim: expandtab shiftwidth=4 ft=pir: