summaryrefslogtreecommitdiff
path: root/test/ambig1.lm
blob: 91195593aedd1be52190e4bc955447938d775ddf (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
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
##### LM #####
#
# Grammar
#

# The items in this scanner may have newline in front of them.
lex
	# Reserved Words.
	literal '__LINE__', '__FILE__', '__ENCODING__', 'BEGIN', 'END', 'alias',
		'and', 'begin', 'break', 'case', 'class', 'def', 'defined?', 'do',
		'else', 'elsif', 'end', 'ensure', 'false', 'for', 'in', 'module',
		'next', 'nil', 'not', 'or', 'redo', 'rescue', 'retry', 'return',
		'self', 'super', 'then', 'true', 'undef', 'when', 'yield', 'if',
		'unless', 'while', 'until'

	token tNTH_REF /'$' [0-9]+/
	token tBACK_REF /'$' ( '&' | '`' | '\'' | '+' ) /

	literal ')', ',', ']'
	literal '{', '}', ':'
	literal '.', '::'
	literal '->'
	
	# Unary operators.
	literal '!', '~'
	token tUPLUS /'+'/
	token tUMINUS /'-'/

	token tLBRACK /'['/
	token tLPAREN /'('/
	token tSTAR /'*'/
	token tBAR /'|'/
	token tAMPER /'&'/

	token tIDENTIFIER /[a-z][a-zA-Z_]*/
	token tFID /[a-z][a-zA-Z_]* ('!'|'?')/
	token tCONSTANT /[A-Z][a-zA-Z_]*/
	token tGVAR /'$' [a-zA-Z_]+/
	token tIVAR /'@' [a-zA-Z_]+/
	token tCVAR /'@@' [a-zA-Z_]+/

	token tINTEGER /[0-9]+/
	token tFLOAT /[0-9]+ '.' [0-9]+/

	token tDSTRING_BEG /'"'/
	token tSSTRING_BEG /'\''/
	token tXSTRING_BEG /'`'/

	ignore /[ \t\n]+/
	ignore comment /'#' [^\n]* '\n'/
end

# These items cannot appear at the beginning of a line (except maybe the first).
lex
	ignore /[\t ]+/

	literal '+', '-', '*', '**', '/', '%', '^'
	literal '|', '&', '||', '&&'
	literal '[', '('
	literal '='
	literal '<<', '>>'
	literal '?'
	literal '<=>'
	literal '=>'
	literal '[]', '[]='
	literal '=~', '!~'
	literal '<', '>', '>=', '<='
	literal '!=', '==', '==='
	literal '..', '...'
end

lex
	ignore /[\t ]+/
	ignore /'#' [^\n]*/
	literal ';'
	literal '\n'
end


lex
	token dstring_contents /[^"]+/
	token tDSTRING_END /'"'/
end

lex
	token sstring_contents /[^']+/
	token tSSTRING_END /'\''/
end

lex
	token xstring_contents /[^`]+/
	token tXSTRING_END /'`'/
end

def ruby
	[compstmt]

def compstmt
	[stmts opt_terms]

def bodystmt
	[compstmt opt_rescue opt_else opt_ensure]

def opt_rescue
#	['rescue' exc_list exc_var then compstmt opt_rescue] |
	[]

def then
	[term]
|	['then']
|	[term 'then']

def do
	[term]
|	['do']

def if_tail
	[opt_else]
|	['elsif' expr_value then compstmt if_tail]

def opt_else
	['else' compstmt]
|	[]

def opt_ensure
	['ensure' compstmt]
|	[]

def stmts
	[stmts terms stmt]
|	[stmt]
|	[]

def opt_terms
	[terms]
|	[]

def terms
	[term]
|	[terms ';']

def term
	[';']
|	['\n']

def stmt
	['alias' fitem fitem]
|	['undef' undef_list]
|	[stmt 'if' expr_value]
|	[stmt 'unless' expr_value]
|	[stmt 'while' expr_value]
|	[stmt 'until' expr_value]
|	[stmt 'rescue' stmt]
|	['BEGIN' '{' compstmt '}']
|	['END' '{' compstmt '}']
|	[lhs '=' mrhs]
|	[mlhs '=' arg_value]
|	[mlhs '=' mrhs]
|	[expr]

def mlhs
	[mlhs_basic]
|	[tLPAREN mlhs ')']

def mlhs_basic
	[mlhs_head]

def mlhs_head
	[mlhs_item ',' mlhs_head]
|	[mlhs_item]

def mlhs_item
	[variable]
|	['*' mlhs_item]
|	['*']
|	[primary_value '[' opt_call_args ']']
|	[primary_value '.' tIDENTIFIER]
|	[primary_value '.' tCONSTANT]
|	[primary_value '::' tIDENTIFIER]
|	[primary_value '::' tCONSTANT]
|	['::' tCONSTANT]
|	[backref]
|	[tLPAREN mlhs ')']

def lhs
	[variable]
|	[primary_value '[' opt_call_args ']']
|	[primary_value '.' tIDENTIFIER]
|	[primary_value '.' tCONSTANT]
|	[primary_value '::' tIDENTIFIER]
|	[primary_value '::' tCONSTANT]
|	['::' tCONSTANT]
|	[backref]

def mrhs
	[args ',' arg_value]
|	[args ',' '*' arg_value]
|	['*' arg_value]

def expr
	[expr 'and' expr]
|	[expr 'or' expr]
|	['not' expr]
|	[arg]

def expr_value
	[expr]

def opt_brace_block
	[brace_block]
|	[]

def block_param_def
	[tBAR opt_bv_decl tBAR]
|	[tBAR block_param opt_bv_decl tBAR]

def block_param
	[block_arg_list]
|	[]

def block_arg_list
	[block_arg_list ',' block_arg_item]
|	[block_arg_item]

def block_arg_item
	[f_norm_arg]
|	[f_rest_arg]
|	[f_block_arg]
|	['(' f_args ')']

def opt_bv_decl
	[';' bv_decls]
|	[]

def bv_decls
	[bvar]
|	[bv_decls ',' bvar]

def bvar
	[tIDENTIFIER]

def opt_block_param
	[block_param_def]
|	[]

def operation
	[tIDENTIFIER]
|	[tCONSTANT]
|	[tFID]

def operation2
	[tIDENTIFIER]
|	[tCONSTANT]
|	[tFID]
|	[op]

def operation3
	[tIDENTIFIER]
|	[tFID]
|	[op]

def op
	['|'] | ['^'] | ['&'] | ['<=>'] | ['=='] | ['==='] | ['=~'] | ['!~'] |
	['>'] | ['>='] | ['<'] | ['<='] | ['!='] | ['<<'] | ['>>'] | ['+'] |
	['-'] | ['*'] | ['/'] | ['%'] | ['**'] | ['!'] | ['~'] | ['[]'] | ['[]='] |
	[tXSTRING_BEG]

def opt_call_args
	[call_args]
|	[]

def call_args
	[args opt_block_arg]
|	[assocs opt_block_arg]
|	[args ',' assocs opt_block_arg]
|	[block_arg]

def args
	[arg_value]
|	['*' arg_value]
|	[args ',' arg_value]
|	[args ',' '*' arg_value]

def arg_value
	[arg]

def opt_block_arg	
	[',' block_arg]
|	[]

def block_arg
	[tAMPER arg_value]

right '='
left 'rescue'
right '?', ':'
nonassoc '..', '...'
left '||'
left '&&'
nonassoc '<=>', '==', '===', '!=', '=~', '!~'
left '>', '>=', '<', '<='
left '|', '^'
left '&'
left '<<', '>>'
left '+', '-'
left '*', '/', '%'
#right tUMINUS_NUM tUMINUS
right tUMINUS
right '**'
right '!', '~', tUPLUS

def arg 
	[lhs '=' arg]
|	[lhs '=' arg 'rescue' arg]
|	[arg '?' arg ':' arg]
|	[arg '..' arg]
|	[arg '...' arg]
|	[arg '||' arg]
|	[arg '&&' arg]
|	[arg '<=>' arg]
|	[arg '==' arg]
|	[arg '===' arg]
|	[arg '!=' arg]
|	[arg '=~' arg]
|	[arg '!~' arg]
|	[arg '>' arg]
|	[arg '>=' arg]
|	[arg '<' arg]
|	[arg '<=' arg]
|	[arg '|' arg]
|	[arg '^' arg]
|	[arg '&' arg]
|	[arg '<<' arg]
|	[arg '>>' arg]
|	[arg '+' arg]
|	[arg '-' arg]
|	[arg '*' arg]
|	[arg '/' arg]
|	[arg '%' arg]
|	[arg '**' arg]
|	['!' primary]
|	['~' primary]
|	[tUMINUS primary]
|	[tUPLUS primary]
|	['defined?' arg]
|	[primary]

def primary_value 
	[primary]

def primary
	[pliteral]
|	[strings]
|	[xstring]
#|	[regexp]
#|	[words]
#|	[qwords]
|	[var_ref]
|	[backref]
|	[tFID]
|	['begin' bodystmt 'end']
|	[tLPAREN compstmt ')']
|	[primary_value '::' tCONSTANT]
|	['::' tCONSTANT]
|	[tLBRACK aref_args ']']
|	['{' assoc_list '}']
|	['defined?' '(' expr ')']
|	[operation brace_block]
|	[method_call]
|	[method_call brace_block]
|	['->' lambda]
|	['if' expr_value then compstmt if_tail 'end']
|	['unless' expr_value then compstmt opt_else 'end']
|	['while' expr_value do compstmt 'end']
|	['until' expr_value do compstmt 'end']
#|	['case' expr_value opt_terms case_body 'end']
#|	['case' opt_terms case_body 'end']
|	['for' for_var 'in' expr_value do compstmt 'end']
|	['class' cpath superclass bodystmt 'end']
|	['class' '<<' expr term bodystmt 'end']
|	['module' cpath bodystmt 'end']
|	['def' fname f_arglist bodystmt 'end']
|	['def' singleton dot_or_colon fname f_arglist bodystmt 'end']
|	['break']
|	['next']
|	['redo']
|	['retry']

def for_var
	[lhs]
|	[mlhs]

def lambda
	[f_larglist lambda_body]

def f_larglist
	['(' f_args opt_bv_decl ')']
|	[f_args opt_bv_decl]

def lambda_body
	['{' compstmt '}']
|	['do' compstmt 'end']

def assoc_list
	[assocs trailer]
|	[]

def assocs
	[assocs ',' assoc]
|	[assoc]

def assoc
	[arg_value '=>' arg_value]
|	[':' arg_value]

def singleton
	[var_ref]
|	['(' expr ')']

def dot_or_colon
	['.']
|	['::']

def aref_args
	[args trailer]
|	[args ',' assocs trailer]
|	[assocs trailer]
|	[]

def trailer		
	[',']
|	[]

def brace_block
	['{' opt_block_param compstmt '}']
|	['do' opt_block_param compstmt 'end']

def f_arglist
	['(' f_args ')']
|	[f_args term]

def f_args
	[f_arg_list]
|	[]

def f_arg_list
	[f_arg_list ',' f_arg_item]
|	[f_arg_item]

def f_arg_item
	[f_norm_arg]
|	[f_opt]
|	[f_rest_arg]
|	[f_block_arg]
|	['(' f_args ')']

def f_opt
	[tIDENTIFIER '=' arg_value]

def f_rest_arg
	['*' tIDENTIFIER]
|	['*']

def f_block_arg
	[tAMPER tIDENTIFIER]

def f_norm_arg
	[tIDENTIFIER]

def backref
	[tNTH_REF] | [tBACK_REF]

def superclass	
	[term]
|	['<' expr_value term]

def cpath
	['::' cname]
|	[cname]
|	[primary_value '::' cname]

def fname
	[tIDENTIFIER]
|	[tCONSTANT]
|	[tFID]
|	[op]
|	[reswords]

def reswords
	['__LINE__'] |	['__FILE__'] |	['__ENCODING__'] |	['BEGIN'] |	['END'] |
	['alias'] |	['and'] |	['begin'] |	['break'] |	['case'] |	['class'] |
	['def'] |	['defined?'] |	['do'] |	['else'] |	['elsif'] |	['end'] |
	['ensure'] |	['false'] |	['for'] |	['in'] |	['module'] |
	['next'] |	['nil'] |	['not'] |	['or'] |	['redo'] |	['rescue'] |
	['retry'] |	['return'] |	['self'] |	['super'] |	['then'] |	['true'] |
	['undef'] |	['when'] |	['yield'] |	['if'] |	['unless'] |	['while'] |
	['until']

def cname
	[tIDENTIFIER]
|	[tCONSTANT]

def pliteral
	[numeric]
|	[symbol]
#|	[dsym]

def strings
	[string]

def string
#	[tCHAR]
	[string1]
|	[string string1]

def string1
	[tSSTRING_BEG sstring_contents? tSSTRING_END]
|	[tDSTRING_BEG dstring_contents? tDSTRING_END]

def xstring
	[tXSTRING_BEG xstring_contents? tXSTRING_END]

def numeric
	[tINTEGER]
|	[tFLOAT]

def symbol
	[':' sym]

def sym
	[fname]
|	[tIVAR]
|	[tGVAR]
|	[tCVAR]

def fitem
	[fsym]
#|	[dsym]

def undef_list
	[fitem]
|	[undef_list ',' fitem]

def fsym
	[fname]
|	[symbol]

#def dsym
#	[':' xstring_contents tDSTRING_END]

def var_ref
	[variable]

def variable
	[tIDENTIFIER] | [tIVAR] | [tGVAR] | [tCONSTANT] | [tCVAR] | ['nil'] |
	['self'] | ['true'] | ['false'] | ['__FILE__'] | ['__LINE__'] |
	['__ENCODING__']


# Required whitespace, but newline is not allowed.
token ws_no_nl
	/[ \t]+[^ \t\n]/
	{
		input.push( make_token( typeid<ws_no_nl> input.pull(match_length-1) ) )
	}

def method_call
	[operation paren_args]
|	[operation ws_no_nl call_args]
|	[primary_value '.' operation2 opt_paren_args]
|	[primary_value '.' operation2 ws_no_nl call_args]
|	[primary_value '::' operation2 opt_paren_args]
|	[primary_value '::' operation2 ws_no_nl call_args]
|	[primary_value '.' paren_args]
|	[primary_value '::' paren_args]
|	['super' paren_args]
|	['super' ws_no_nl call_args]
|	['super']
|	['yield' paren_args]
|	['yield' ws_no_nl call_args]
|	['yield']
|	['return' call_args]
|	['return']
|	[primary_value '[' opt_call_args ']']

def opt_paren_args
	[paren_args]
|	[]

def paren_args
	['(' opt_call_args ')']

#
# Grammar finished
#

parse R: ruby[stdin]

print_xml( R )
print( '\n' )

#for T: primary in R
#	print_xml( T, '\n\n' )
##### IN #####
##### EXP #####
<ruby><compstmt><stmts></stmts><opt_terms></opt_terms></compstmt></ruby>