depager 0.2.3 → 0.3.0.b20250423
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/.rubocop.yml +44 -0
- data/.simplecov +5 -0
- data/Gemfile +12 -0
- data/LICENSE.gpl +339 -0
- data/Manifest.txt +73 -0
- data/README.en +7 -21
- data/README.ja +19 -99
- data/Rakefile +31 -0
- data/bin/depager +7 -45
- data/examples/action_pl0d/pl0d.action.dr +421 -0
- data/examples/action_pl0d/test.pl0ds +48 -0
- data/examples/c89/c89.dr +493 -496
- data/examples/c89/test.c89 +10 -10
- data/examples/extension/astdf.rb +9 -0
- data/examples/extension/atree.dr +55 -0
- data/examples/{sample_calc → extension}/calc.atree.dr +42 -43
- data/examples/{sample_calc/calc.action.dr → extension/calc.simple_action.dr} +33 -33
- data/examples/extension/paction.dr +16 -15
- data/examples/extension/pactiontest.dr +14 -14
- data/examples/extension/simple_action.rb +46 -0
- data/examples/pl0d/pl0ds.dr +337 -334
- data/examples/pl0d/test.pl0ds +33 -33
- data/examples/rie_calc/calc.rie.dr +57 -0
- data/examples/rie_calc/test.calc +4 -0
- data/examples/rie_dcuse/dcuse.rie.dr +71 -0
- data/examples/rie_dcuse/test.dcuse +1 -0
- data/examples/rie_pl0/orig_ex/exerrdg.pl0 +44 -0
- data/examples/rie_pl0/orig_ex/exerrm.pl0 +19 -0
- data/examples/rie_pl0/orig_ex/exerrmre.pl0 +20 -0
- data/examples/rie_pl0/orig_ex/exerrtok.pl0 +18 -0
- data/examples/rie_pl0/orig_ex/exmdg.pl0 +40 -0
- data/examples/rie_pl0/orig_ex/exmdgwwl.pl0 +43 -0
- data/examples/rie_pl0/orig_ex/exmrw.pl0 +22 -0
- data/examples/rie_pl0/orig_ex/exmwwl.pl0 +18 -0
- data/examples/rie_pl0/orig_ex/exnorw.pl0 +17 -0
- data/examples/rie_pl0/pl0.rie.dr +450 -0
- data/examples/rie_pl0/test.pl0 +10 -0
- data/examples/slex_test/divreg.slex.dr +29 -29
- data/examples/slex_test/ljoin.slex.dr +36 -36
- data/examples/slex_test/test.divreg +1 -1
- data/examples/slex_test/test.ljoin +3 -3
- data/examples/{sample_calc/calc.nvaction.dr → tiny_calc/calc.action.dr} +33 -33
- data/examples/{sample_calc → tiny_calc}/calc.ast.action.dr +76 -66
- data/examples/{sample_calc → tiny_calc}/calc.ast.dr +67 -55
- data/examples/tiny_calc/calc.cst.dr +50 -0
- data/examples/{sample_calc → tiny_calc}/calc.dr +43 -43
- data/examples/{sample_calc → tiny_calc}/calc.lex.dr +29 -29
- data/examples/{sample_calc/calc_prec.nvaction.dr → tiny_calc/calc_prec.action.dr} +31 -31
- data/lib/depager/cli.rb +44 -0
- data/lib/depager/grammar.rb +253 -291
- data/lib/depager/lr.rb +589 -579
- data/lib/depager/parser.rb +269 -277
- data/lib/depager/plugins/_rie_debug.rb +63 -0
- data/lib/depager/plugins/action.rb +47 -0
- data/lib/depager/plugins/ast.dr +367 -0
- data/lib/depager/plugins/ast.rb +1329 -0
- data/lib/depager/{ruby/plugins → plugins}/cst.dr +174 -180
- data/lib/depager/plugins/cst.rb +591 -0
- data/lib/depager/{ruby/plugins → plugins}/lex.dr +85 -89
- data/lib/depager/plugins/lex.rb +313 -0
- data/lib/depager/plugins/rie.dr +725 -0
- data/lib/depager/plugins/rie.rb +1614 -0
- data/lib/depager/{ruby/plugins → plugins}/slex.dr +201 -200
- data/lib/depager/plugins/slex.rb +769 -0
- data/lib/depager/plugins/srp.rb +46 -0
- data/lib/depager/ruby/templates/extension_lalr_master.erb +40 -51
- data/lib/depager/ruby/templates/extension_lalr_slave.erb +113 -107
- data/lib/depager/ruby/templates/single_lalr_parser.erb +124 -117
- data/lib/depager/utils.rb +158 -318
- data/lib/depager/version.rb +3 -3
- data/lib/depager.rb +572 -670
- metadata +77 -80
- data/ChangeLog +0 -16
- data/data/depager/pre-setup.rb +0 -3
- data/examples/c89/c89.tab.rb +0 -7127
- data/examples/pl0d/pl0ds.tab.rb +0 -2698
- data/examples/sample_calc/calc.action.tab.rb +0 -457
- data/examples/sample_calc/calc.ast.action.tab.rb +0 -749
- data/examples/sample_calc/calc.ast.tab.rb +0 -665
- data/examples/sample_calc/calc.astdf.dr +0 -54
- data/examples/sample_calc/calc.astdf.tab.rb +0 -672
- data/examples/sample_calc/calc.atree.tab.rb +0 -451
- data/examples/sample_calc/calc.cst.dr +0 -45
- data/examples/sample_calc/calc.cst.tab.rb +0 -644
- data/examples/sample_calc/calc.lex.tab.rb +0 -374
- data/examples/sample_calc/calc.nvaction.tab.rb +0 -465
- data/examples/sample_calc/calc.tab.rb +0 -365
- data/examples/sample_calc/calc_prec.nvaction.tab.rb +0 -431
- data/examples/slex_test/divreg.slex.tab.rb +0 -303
- data/examples/slex_test/ljoin.slex.tab.rb +0 -370
- data/lib/depager/ruby/plugins/_ast_tmpl.rb +0 -73
- data/lib/depager/ruby/plugins/action.rb +0 -43
- data/lib/depager/ruby/plugins/ast.dr +0 -269
- data/lib/depager/ruby/plugins/ast.rb +0 -1308
- data/lib/depager/ruby/plugins/astdf.rb +0 -6
- data/lib/depager/ruby/plugins/atree.dr +0 -55
- data/lib/depager/ruby/plugins/atree.rb +0 -347
- data/lib/depager/ruby/plugins/cst.rb +0 -626
- data/lib/depager/ruby/plugins/lex.rb +0 -336
- data/lib/depager/ruby/plugins/nvaction.rb +0 -19
- data/lib/depager/ruby/plugins/slex.rb +0 -817
- data/lib/depager/ruby/plugins/srp.rb +0 -51
- data/lib/depager/ruby/templates/simple.erb +0 -23
- data/setup.rb +0 -1585
- /data/examples/{sample_calc → tiny_calc}/test.calc +0 -0
data/lib/depager.rb
CHANGED
@@ -1,670 +1,572 @@
|
|
1
|
-
require
|
2
|
-
require "depager/parser
|
3
|
-
require "depager/grammar
|
4
|
-
require "depager/utils
|
5
|
-
|
6
|
-
module Depager
|
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
|
-
def
|
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
|
-
end
|
141
|
-
|
142
|
-
|
143
|
-
|
144
|
-
|
145
|
-
@
|
146
|
-
|
147
|
-
end
|
148
|
-
|
149
|
-
|
150
|
-
|
151
|
-
|
152
|
-
|
153
|
-
|
154
|
-
|
155
|
-
|
156
|
-
|
157
|
-
|
158
|
-
|
159
|
-
|
160
|
-
|
161
|
-
|
162
|
-
|
163
|
-
|
164
|
-
|
165
|
-
|
166
|
-
def
|
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
|
-
do_hook :
|
198
|
-
|
199
|
-
|
200
|
-
|
201
|
-
|
202
|
-
|
203
|
-
|
204
|
-
|
205
|
-
def
|
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
|
-
val = @terms[s] ||=
|
247
|
-
when
|
248
|
-
|
249
|
-
|
250
|
-
|
251
|
-
val =
|
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
|
-
def
|
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
|
-
else
|
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
|
-
end
|
566
|
-
end
|
567
|
-
|
568
|
-
|
569
|
-
|
570
|
-
|
571
|
-
|
572
|
-
|
573
|
-
def out_code g_parser
|
574
|
-
ERB.new(File.read("#{tmpldir}/#{tmplf}")).result(binding)
|
575
|
-
end
|
576
|
-
def parse_decl
|
577
|
-
until eof?
|
578
|
-
line = getline
|
579
|
-
case line
|
580
|
-
when /^%%\s*$/
|
581
|
-
g_parser = GrammarParser.new(@d_parser)
|
582
|
-
g_parser.extend_paser @ext, @options
|
583
|
-
g_parser.parse(@d_parser.target_name, @mixin, @precs)
|
584
|
-
@optmain.push parse_block
|
585
|
-
return out_code(g_parser)
|
586
|
-
else
|
587
|
-
parse_common(line)
|
588
|
-
end
|
589
|
-
end
|
590
|
-
return nil
|
591
|
-
end
|
592
|
-
end
|
593
|
-
|
594
|
-
class ExtensionGenerator < Generator
|
595
|
-
TmplfileMaster = '**NOTHING**'
|
596
|
-
TmplfileSlave = '**NOTHING**'
|
597
|
-
attr_accessor :regs, :paramkey
|
598
|
-
def initialize d_parser
|
599
|
-
super
|
600
|
-
@parsing_method = Simple
|
601
|
-
@basis_name = 'self'
|
602
|
-
@paramkey = nil
|
603
|
-
@regs = { }
|
604
|
-
end
|
605
|
-
|
606
|
-
def parse_decl
|
607
|
-
until eof?
|
608
|
-
line = getline
|
609
|
-
case line
|
610
|
-
when /^%hook\s*(.*?)\s*$/
|
611
|
-
hs = $1
|
612
|
-
mtsk = if hs =~ /\/(([^\/\\]+|\\.)*)\/\s*(skip)?$/
|
613
|
-
hs = $`
|
614
|
-
[$1, $3 ? true : false]
|
615
|
-
end
|
616
|
-
hook = hs.split(' ')
|
617
|
-
hookname = hook.join('__')
|
618
|
-
hook.each do |i| i = i.to_sym
|
619
|
-
raise unless GrammarParser.hook_name? i
|
620
|
-
@regs[i] = "#{@d_parser.target_name}_#{hookname}"
|
621
|
-
end
|
622
|
-
@optouter.push(parse_hook(hookname, mtsk))
|
623
|
-
when /^%param\s*(.*)\s*$/
|
624
|
-
@paramkey = $1
|
625
|
-
else
|
626
|
-
parse_common(line)
|
627
|
-
end
|
628
|
-
end
|
629
|
-
return out_master_code
|
630
|
-
end
|
631
|
-
|
632
|
-
def parse_hook hookname, mtsk
|
633
|
-
inner = ''
|
634
|
-
precs = []
|
635
|
-
banner = nil
|
636
|
-
mixin = [].concat(@mixin)
|
637
|
-
target_name = "#{@d_parser.target_name}_#{hookname}"
|
638
|
-
until eof?
|
639
|
-
line = getline
|
640
|
-
case line
|
641
|
-
when /^%banner\s*'(([^'\\]+|\\.)*)'\s*$/
|
642
|
-
banner = $1
|
643
|
-
when /^%inner\s*\{\s*$/
|
644
|
-
inner = parse_block
|
645
|
-
when /^%mixin\s*(.+?)\s*(\((.+)\)\s*)?$/
|
646
|
-
mixin.push $1
|
647
|
-
@req.push $3 if $3
|
648
|
-
when /^%prec\s*(.*?)\s*$/
|
649
|
-
precs = parse_prec
|
650
|
-
when /^%%\s*$/
|
651
|
-
g_parser = GrammarParser.new(@d_parser)
|
652
|
-
g_parser.extend_paser @ext, @options
|
653
|
-
g_parser.parse(target_name, mixin, precs)
|
654
|
-
g_parser.optinner.push inner
|
655
|
-
return out_slave_code(g_parser, mixin, mtsk, banner)
|
656
|
-
else
|
657
|
-
warning "syntax error(declaration).\n> #{line}", lineno
|
658
|
-
end
|
659
|
-
end
|
660
|
-
end
|
661
|
-
|
662
|
-
def out_slave_code g_parser, mixin, mtsk, banner
|
663
|
-
ERB.new(File.read(self.class::TmplfileSlave)).result(binding)
|
664
|
-
end
|
665
|
-
def out_master_code
|
666
|
-
ERB.new(File.read(self.class::TmplfileMaster)).result(binding)
|
667
|
-
end
|
668
|
-
end
|
669
|
-
end
|
670
|
-
end
|
1
|
+
require "erb"
|
2
|
+
require "depager/parser"
|
3
|
+
require "depager/grammar"
|
4
|
+
require "depager/utils"
|
5
|
+
|
6
|
+
module Depager
|
7
|
+
def self.configuration
|
8
|
+
@configuration ||= {}
|
9
|
+
end
|
10
|
+
|
11
|
+
def self.debug_mode?(flag = "")
|
12
|
+
value = configuration[:debug] or return false
|
13
|
+
!!value.match(/#{flag}/)
|
14
|
+
end
|
15
|
+
|
16
|
+
def self.verbose_mode?
|
17
|
+
configuration[:verbose]
|
18
|
+
end
|
19
|
+
|
20
|
+
class DeclarationPartParser
|
21
|
+
include Depager::Utils::CommonMethods
|
22
|
+
|
23
|
+
attr_reader :file, :target_namespace, :target_name, :g_parser, :generator
|
24
|
+
|
25
|
+
def parse(file)
|
26
|
+
@file = file
|
27
|
+
@g_parser = GrammarPartParser.new(self)
|
28
|
+
|
29
|
+
until file.eof?
|
30
|
+
case file.gets
|
31
|
+
when /^\s*(#.*)?$/
|
32
|
+
# skip space and comment.
|
33
|
+
when /^%class\s+(\S+)(\s+based_on\s+(\S+)(\s+\(\s*'(.+)'\s*\))?)?\s*$/
|
34
|
+
@target_name = $1
|
35
|
+
if $2
|
36
|
+
require $5 if $5
|
37
|
+
nesting = $3.split("::").tap { |it| it[-1] = "#{it[-1]}Generator" }
|
38
|
+
generator_class = nesting.inject(Object) { |c, name| c.const_get(name) }
|
39
|
+
else
|
40
|
+
require "depager/lr"
|
41
|
+
generator_class = LALR::ParserGenerator
|
42
|
+
end
|
43
|
+
|
44
|
+
if @target_name =~ /(.+)::([^:]+)/
|
45
|
+
@target_namespace = $1
|
46
|
+
@target_name = $2
|
47
|
+
else
|
48
|
+
error_exit "Namespace required"
|
49
|
+
end
|
50
|
+
|
51
|
+
@generator = generator_class.new(self)
|
52
|
+
break
|
53
|
+
when /^%defext\s+(\S+)\s*$/
|
54
|
+
require "depager/lr"
|
55
|
+
|
56
|
+
@target_namespace = $1
|
57
|
+
@target_name = "Master"
|
58
|
+
|
59
|
+
@generator = LALR::ExtensionGenerator.new(self)
|
60
|
+
break
|
61
|
+
else
|
62
|
+
error_exit "%class not found."
|
63
|
+
end
|
64
|
+
end
|
65
|
+
@generator.parse
|
66
|
+
end
|
67
|
+
|
68
|
+
def parsing_method
|
69
|
+
@generator.parsing_method
|
70
|
+
end
|
71
|
+
end
|
72
|
+
|
73
|
+
class GrammarPartParser
|
74
|
+
include Depager::Utils::CommonMethods
|
75
|
+
|
76
|
+
Token = Struct.new(:tag, :value, :name, :lineno)
|
77
|
+
|
78
|
+
HOOK_KEYS = %i[
|
79
|
+
pre_rule_list post_rule_list
|
80
|
+
pre_rule post_rule
|
81
|
+
post_lhs
|
82
|
+
post_rhs_list pre_rhs_list
|
83
|
+
pre_rhs post_rhs
|
84
|
+
post_symbol
|
85
|
+
].freeze # :nodoc:
|
86
|
+
|
87
|
+
attr_accessor :rules, :terms, :nonterms, :precs, :table, :lhs, :rhs, :nrhs, :rhs_names, :nrules, :line,
|
88
|
+
:original_line, :old_line, :token, :outer_code, :inner_code, :mixins
|
89
|
+
attr_reader :d_parser, :target_namespace, :target_name, :hooks
|
90
|
+
|
91
|
+
def initialize(d_parser)
|
92
|
+
@d_parser = d_parser
|
93
|
+
end
|
94
|
+
|
95
|
+
# parse and make LALR(1) table
|
96
|
+
def parse(full_target_name, precs = [], extensions = [], options = {})
|
97
|
+
@original_line = @old_line = @line = ""
|
98
|
+
@inner_code = ""
|
99
|
+
@outer_code = ""
|
100
|
+
|
101
|
+
full_target_name.match(/::([^:]+)$/) or raise
|
102
|
+
@target_namespace = $`
|
103
|
+
@target_name = $1
|
104
|
+
|
105
|
+
extend_parser(extensions, options)
|
106
|
+
init_grammar
|
107
|
+
parse_grammar
|
108
|
+
check_grammar
|
109
|
+
make_grammar precs
|
110
|
+
|
111
|
+
grammar = d_parser.parsing_method::Grammar.new(@rules, @terms, @nonterms, @precs)
|
112
|
+
@table = d_parser.parsing_method::Table.new(grammar)
|
113
|
+
@table.check_table d_parser
|
114
|
+
@extensions.each { |o| o.send :term_extension }
|
115
|
+
@table
|
116
|
+
end
|
117
|
+
|
118
|
+
def hook_name?(s)
|
119
|
+
HOOK_KEYS.include? s.to_sym
|
120
|
+
end
|
121
|
+
|
122
|
+
def update_context(line)
|
123
|
+
@line = line
|
124
|
+
scan_token
|
125
|
+
end
|
126
|
+
|
127
|
+
def int_to_sym(n)
|
128
|
+
@int_to_sym[n]
|
129
|
+
end
|
130
|
+
|
131
|
+
def lhs_name
|
132
|
+
int_to_sym(lhs)
|
133
|
+
end
|
134
|
+
|
135
|
+
def add_nonterm(sym)
|
136
|
+
sym = sym.to_s.intern
|
137
|
+
isym = @nonterms[sym] = @nonterms.size
|
138
|
+
@int_to_sym[isym] = sym
|
139
|
+
isym
|
140
|
+
end
|
141
|
+
|
142
|
+
def add_rule(lhs, rhs, prec = nil, rhs_names = nil)
|
143
|
+
@lhs_syms[lhs] = true
|
144
|
+
rule = d_parser.parsing_method::Rule[lhs, rhs, prec, rhs_names]
|
145
|
+
@rules.push rule
|
146
|
+
rule
|
147
|
+
end
|
148
|
+
|
149
|
+
def name_to_rhs_index(name)
|
150
|
+
@rhs_names.index(name)
|
151
|
+
end
|
152
|
+
|
153
|
+
def insert_sym_to_rhs(pos, token_value, token_name)
|
154
|
+
if pos < 0
|
155
|
+
@rhs.push token_value
|
156
|
+
@rhs_names[@rhs.size - 1] = token_name
|
157
|
+
else
|
158
|
+
@rhs.insert(pos, token_value)
|
159
|
+
@rhs_names.insert(pos, token_name)
|
160
|
+
end
|
161
|
+
@rhs_syms[token_value] = true
|
162
|
+
end
|
163
|
+
|
164
|
+
private
|
165
|
+
|
166
|
+
def parse_grammar
|
167
|
+
scan_token
|
168
|
+
do_hook :pre_rule_list
|
169
|
+
parse_rulelist
|
170
|
+
do_hook :post_rule_list
|
171
|
+
return if @token.tag.nil?
|
172
|
+
|
173
|
+
error_exit "syntax error(grammar). unexpected '#{@token.tag}' expect '%%'"
|
174
|
+
end
|
175
|
+
|
176
|
+
def parse_rulelist
|
177
|
+
do_hook :pre_rule
|
178
|
+
parse_rule
|
179
|
+
do_hook :post_rule
|
180
|
+
return unless @token.tag == :NT
|
181
|
+
|
182
|
+
parse_rulelist
|
183
|
+
end
|
184
|
+
|
185
|
+
def parse_rule
|
186
|
+
return unless @token[0] == :NT
|
187
|
+
|
188
|
+
@lhs = @token.value
|
189
|
+
|
190
|
+
scan_token
|
191
|
+
do_hook :post_lhs
|
192
|
+
|
193
|
+
error_exit "syntax error(grammar). unexpected '#{@token.tag}' expecting ':'" unless @token.tag == ":"
|
194
|
+
scan_token
|
195
|
+
|
196
|
+
@nrhs = 0
|
197
|
+
do_hook :pre_rhs_list
|
198
|
+
parse_rhslist
|
199
|
+
do_hook :post_rhs_list
|
200
|
+
|
201
|
+
error_exit "syntax error(grammar). unexpected '#{@token.tag}' expecting ';'" unless @token.tag == ";"
|
202
|
+
scan_token
|
203
|
+
end
|
204
|
+
|
205
|
+
def parse_rhslist
|
206
|
+
@rhs = []
|
207
|
+
@rhs_names = []
|
208
|
+
@prec = nil
|
209
|
+
do_hook :pre_rhs
|
210
|
+
parse_syms
|
211
|
+
add_rule(@lhs, @rhs, @prec, @rhs_names)
|
212
|
+
do_hook :post_rhs
|
213
|
+
@nrhs += 1
|
214
|
+
|
215
|
+
return unless @token.tag == "|"
|
216
|
+
|
217
|
+
scan_token
|
218
|
+
parse_rhslist
|
219
|
+
end
|
220
|
+
|
221
|
+
def parse_syms
|
222
|
+
return unless @token.tag == :NT || @token.tag == :T
|
223
|
+
|
224
|
+
insert_sym_to_rhs(-1, @token.value, @token.name)
|
225
|
+
scan_token
|
226
|
+
|
227
|
+
do_hook :post_symbol
|
228
|
+
parse_syms
|
229
|
+
end
|
230
|
+
|
231
|
+
def make_token(tag, value, name = nil)
|
232
|
+
@token = Token.new(tag, value, name, file.lineno)
|
233
|
+
end
|
234
|
+
|
235
|
+
def scan_token
|
236
|
+
while !@line.empty? || !file.eof?
|
237
|
+
@original_line = @old_line = @line = file.gets if @line.empty?
|
238
|
+
|
239
|
+
tag = s = name = val = nil
|
240
|
+
until @line.empty?
|
241
|
+
case @line
|
242
|
+
when /^\s+/, /^\#.*/
|
243
|
+
# skip
|
244
|
+
when /^=([a-zA-Z]+)/
|
245
|
+
@prec = s = $1.intern
|
246
|
+
val = @terms[s] ||= - (@terms.size + 1)
|
247
|
+
when /^([a-z][a-z0-9_]*)(-([a-z][_a-z0-9]*))?/
|
248
|
+
tag = :NT
|
249
|
+
s = $1.intern
|
250
|
+
name = $3 || $1
|
251
|
+
val = @nonterms[s] ||= @nonterms.size
|
252
|
+
when /^([A-Z][A-Z0-9_]*)(-([a-z][_a-z0-9]*))?/
|
253
|
+
tag = :T
|
254
|
+
s = $1.intern
|
255
|
+
name = $3 || $1
|
256
|
+
val = @terms[s] ||= - (@terms.size + 1)
|
257
|
+
when /^'(.+?)'/
|
258
|
+
tag = :T
|
259
|
+
name = s = $1
|
260
|
+
val = @terms[s] ||= - (@terms.size + 1)
|
261
|
+
when /^%%/
|
262
|
+
return make_token(nil, nil)
|
263
|
+
when /^([:;|])/
|
264
|
+
tag = $&
|
265
|
+
val = $&
|
266
|
+
else
|
267
|
+
return make_token(:UNKNOWN, @line[0].chr)
|
268
|
+
end
|
269
|
+
@old_line = @line
|
270
|
+
@line = $'
|
271
|
+
@int_to_sym[val] ||= s if val && s
|
272
|
+
return make_token(tag, val, name) if tag
|
273
|
+
end
|
274
|
+
end
|
275
|
+
make_token(nil, nil)
|
276
|
+
end
|
277
|
+
|
278
|
+
def extend_parser(extensions, options)
|
279
|
+
init_hook
|
280
|
+
@extensions = []
|
281
|
+
extensions.each do |f, m|
|
282
|
+
if f&.match(%r{^\./})
|
283
|
+
require File.expand_path(f, File.dirname(file.path))
|
284
|
+
elsif f
|
285
|
+
require f
|
286
|
+
end
|
287
|
+
|
288
|
+
nesting = m.split("::").tap { |it| it[-1] = "#{it[-1]}Extension" }
|
289
|
+
extension = nesting.inject(Object) { |c, name| c.const_get(name) }.new
|
290
|
+
options[m]&.each do |k, v|
|
291
|
+
extension.send(:"#{k}=", v)
|
292
|
+
rescue NoMethodError
|
293
|
+
warning "no such a option '#{m}.#{k}'."
|
294
|
+
end
|
295
|
+
@extensions << extension
|
296
|
+
extension.extension_registered self
|
297
|
+
extension.init_extension
|
298
|
+
end
|
299
|
+
end
|
300
|
+
|
301
|
+
def init_grammar
|
302
|
+
@rules = [d_parser.parsing_method::Rule[0, [1]]]
|
303
|
+
@terms = { nil => -1, false => -2 }
|
304
|
+
@nonterms = { "$start" => 0 }
|
305
|
+
@int_to_sym = {}
|
306
|
+
@rhs_syms = {}
|
307
|
+
@lhs_syms = {}
|
308
|
+
end
|
309
|
+
|
310
|
+
def make_grammar(precs)
|
311
|
+
ts = @nonterms.size - 1
|
312
|
+
@terms.each { |k, v| @terms[k] = ts - v }
|
313
|
+
|
314
|
+
@precs = {}
|
315
|
+
precs.each_with_index do |p, x|
|
316
|
+
p[1].each do |i|
|
317
|
+
if @terms[i]
|
318
|
+
@precs[@terms[i]] = [p[0], x]
|
319
|
+
else
|
320
|
+
error_exit "prec error '#{i}'."
|
321
|
+
end
|
322
|
+
end
|
323
|
+
end
|
324
|
+
@rules.each do |rule|
|
325
|
+
rule.rhs.each_with_index { |i, x| rule.rhs[x] = ts - i if i < 0 }
|
326
|
+
rule.prec &&= @precs[@terms[rule.prec]]
|
327
|
+
end
|
328
|
+
end
|
329
|
+
|
330
|
+
def check_grammar
|
331
|
+
lhs_warns = []
|
332
|
+
@lhs_syms.each_key do |s|
|
333
|
+
lhs_warns << s if (s != 1) && !@rhs_syms[s]
|
334
|
+
end
|
335
|
+
rhs_warns = []
|
336
|
+
@rhs_syms.each_key do |s|
|
337
|
+
rhs_warns << s if (s > 0) && !@lhs_syms[s]
|
338
|
+
end
|
339
|
+
lhs_warns.uniq.each do |s|
|
340
|
+
warning "the lhs '#{@int_to_sym[s]}' is not used"
|
341
|
+
end
|
342
|
+
rhs_warns.uniq.each do |s|
|
343
|
+
warning "the symbol '#{@int_to_sym[s]}' is undefined"
|
344
|
+
end
|
345
|
+
end
|
346
|
+
|
347
|
+
def init_hook
|
348
|
+
@hooks = HOOK_KEYS.each_with_object({}) do |i, r|
|
349
|
+
r[i] = []
|
350
|
+
end
|
351
|
+
end
|
352
|
+
|
353
|
+
def do_hook(hook)
|
354
|
+
@hooks[hook].each { |o, m| o.send m }
|
355
|
+
end
|
356
|
+
end
|
357
|
+
|
358
|
+
class Generator
|
359
|
+
include Depager::Utils::CommonMethods
|
360
|
+
|
361
|
+
TEMPLATES_DIR = File.expand_path("depager/ruby/templates", __dir__)
|
362
|
+
|
363
|
+
attr_reader :d_parser, :g_parser, :parsing_method
|
364
|
+
attr_accessor :outer_code, :inner_code, :setup_code, :decorators, :requirements, :options
|
365
|
+
|
366
|
+
def initialize(d_parser)
|
367
|
+
@d_parser = d_parser
|
368
|
+
@g_parser = d_parser.g_parser
|
369
|
+
@parsing_method = nil
|
370
|
+
|
371
|
+
@options = {}
|
372
|
+
@decorators = []
|
373
|
+
@requirements = []
|
374
|
+
@extensions = []
|
375
|
+
@inner_code = ""
|
376
|
+
@outer_code = ""
|
377
|
+
@setup_code = ""
|
378
|
+
@precs = []
|
379
|
+
@mixins = []
|
380
|
+
end
|
381
|
+
|
382
|
+
def generate_code(template)
|
383
|
+
ERB.new(template || "", trim_mode: "-").result(binding)
|
384
|
+
end
|
385
|
+
|
386
|
+
def parse_prec
|
387
|
+
precs = []
|
388
|
+
until file.eof?
|
389
|
+
case line = file.gets
|
390
|
+
when /^\s*$/, /^\s*#.*$/
|
391
|
+
# skip
|
392
|
+
when /^\s*(left|right|nonassoc)\s+(.*)$/
|
393
|
+
dir = $1.upcase.intern
|
394
|
+
syms = $2.split.map { |i| i =~ /'(.+?)'/ ? $1 : i.intern }
|
395
|
+
precs.push [dir, syms]
|
396
|
+
when /^%\}\s*$/
|
397
|
+
break
|
398
|
+
else
|
399
|
+
error_exit "syntax error(prec).\n> #{line}"
|
400
|
+
end
|
401
|
+
end
|
402
|
+
precs
|
403
|
+
end
|
404
|
+
|
405
|
+
def parse_block(raw: false)
|
406
|
+
lineno = file.lineno
|
407
|
+
val = ""
|
408
|
+
until file.eof?
|
409
|
+
line = file.gets
|
410
|
+
break if /^%\}/.match?(line)
|
411
|
+
|
412
|
+
val.concat line
|
413
|
+
end
|
414
|
+
return val if raw
|
415
|
+
|
416
|
+
delimiter = expanded_code_delimiter
|
417
|
+
<<~EXPANDED_CODE
|
418
|
+
module_eval <<'#{delimiter}', '#{file.path}', #{lineno + 1}
|
419
|
+
#{val}
|
420
|
+
#{delimiter}
|
421
|
+
EXPANDED_CODE
|
422
|
+
end
|
423
|
+
|
424
|
+
def parse_common(line)
|
425
|
+
case line
|
426
|
+
when /^\s*(#.*)?$/
|
427
|
+
# skip space and comment.
|
428
|
+
when /^%decorate\s+(.*?)\s*\((.*)\)\s*$/
|
429
|
+
@decorators.push $1
|
430
|
+
@requirements.push $2
|
431
|
+
when /^%decorate\s+(.*?)\s*$/
|
432
|
+
@decorators.push $1
|
433
|
+
when /^%extend\s+(.*?)\s*\(\s*'(.*)'\s*\)\s*$/
|
434
|
+
@extensions.push [$2.strip, $1] unless @extensions.find { |i| i[1] == $1 }
|
435
|
+
when /^%option\s+([\w:]+).(\w+)\s+(.*)\s*$/
|
436
|
+
if $1 == "Generator"
|
437
|
+
Depager.configuration[$2.to_sym] = $3
|
438
|
+
else
|
439
|
+
@options[$1] ||= []
|
440
|
+
@options[$1] << [$2, $3]
|
441
|
+
end
|
442
|
+
when /^%require\s+'(.+?)'\s*$/
|
443
|
+
@requirements.push "'#{$1}'"
|
444
|
+
when /^%mixin\s+(\S+)\s*(\((.+)\)\s*)?$/
|
445
|
+
@mixins.push $1
|
446
|
+
@requirements.push $3 if $3
|
447
|
+
when /^%inner\s*\{\s*$/
|
448
|
+
@inner_code << parse_block
|
449
|
+
when /^%prec\s*\{\s*$/
|
450
|
+
@precs = parse_prec
|
451
|
+
when /^%expanded_code_delimiter\s+(\S+)\s*$/
|
452
|
+
Depager.configuration[:expanded_code_delimiter] = $1
|
453
|
+
when /^%abort_behavior\s+(pending\s+)?(.*)$/
|
454
|
+
@inner_code << " def do_abort_driver; end\n" if $1
|
455
|
+
@inner_code << " def abort_driver; #{$2}; end\n" unless $2.strip.empty?
|
456
|
+
else
|
457
|
+
warning "syntax error(declaration).\n> #{line}"
|
458
|
+
end
|
459
|
+
end
|
460
|
+
end
|
461
|
+
|
462
|
+
class ParserGenerator < Generator
|
463
|
+
def parse
|
464
|
+
until file.eof?
|
465
|
+
line = file.gets
|
466
|
+
case line
|
467
|
+
when /^%%\s*$/
|
468
|
+
g_parser.parse(@d_parser.full_target_name, @precs, @extensions, @options)
|
469
|
+
@outer_code << parse_block(raw: true)
|
470
|
+
return generate_code parser_code_template
|
471
|
+
when /^%setup((\s*{\s*)|(\s.*))$/
|
472
|
+
@setup_code << ($2 ? parse_block : $3.lstrip)
|
473
|
+
else
|
474
|
+
parse_common(line)
|
475
|
+
end
|
476
|
+
end
|
477
|
+
nil
|
478
|
+
end
|
479
|
+
|
480
|
+
def parser_code_template; end
|
481
|
+
end
|
482
|
+
|
483
|
+
class ExtensionGenerator < Generator
|
484
|
+
attr_accessor :slaves
|
485
|
+
|
486
|
+
def initialize(d_parser)
|
487
|
+
super
|
488
|
+
@slaves = {}
|
489
|
+
end
|
490
|
+
|
491
|
+
def parse
|
492
|
+
until file.eof?
|
493
|
+
line = file.gets
|
494
|
+
case line
|
495
|
+
when %r{^%hook\s+([\w \t]+?)(/((?:[^/\\]+|\\.)+)/(\s+skip)?)?\s*$}
|
496
|
+
hooks = $1.split
|
497
|
+
@banner = hooks.join(",")
|
498
|
+
@do_parse_re = $3
|
499
|
+
@do_parse_skip = $4
|
500
|
+
|
501
|
+
parts = hooks.map do |i|
|
502
|
+
error_exit "Unknown hook: #{i}" unless g_parser.hook_name? i
|
503
|
+
i.split("_").map(&:capitalize).join
|
504
|
+
end
|
505
|
+
target_name = "#{d_parser.target_namespace}::#{parts.join}#{slaves.size}::Parser"
|
506
|
+
@slaves[target_name] = hooks
|
507
|
+
@outer_code << parse_hook(target_name)
|
508
|
+
when /^%%\s*$/
|
509
|
+
@outer_code << parse_block(raw: true)
|
510
|
+
else
|
511
|
+
parse_common(line)
|
512
|
+
end
|
513
|
+
end
|
514
|
+
generate_code master_code_template
|
515
|
+
end
|
516
|
+
|
517
|
+
def parse_hook(target_name)
|
518
|
+
inner = ""
|
519
|
+
precs = []
|
520
|
+
@slave_mixins = @mixins.dup
|
521
|
+
until file.eof?
|
522
|
+
line = file.gets
|
523
|
+
case line
|
524
|
+
when /^%banner\s*'(([^'\\]+|\\.)*)'\s*$/
|
525
|
+
@banner = $1
|
526
|
+
when /^%inner\s*\{\s*$/
|
527
|
+
inner = parse_block
|
528
|
+
when /^%mixin\s*(.+?)\s*(\((.+)\)\s*)?$/
|
529
|
+
@slave_mixins << $1
|
530
|
+
@requirements.push $3 if $3
|
531
|
+
when /^%prec\s*(.*?)\s*$/
|
532
|
+
precs = parse_prec
|
533
|
+
when /^%%\s*$/
|
534
|
+
g_parser.parse(target_name, precs, @extensions, @options)
|
535
|
+
g_parser.inner_code << inner
|
536
|
+
return generate_code slave_code_template
|
537
|
+
else
|
538
|
+
warning "syntax error(declaration).\n> #{line}", file.lineno
|
539
|
+
end
|
540
|
+
end
|
541
|
+
end
|
542
|
+
|
543
|
+
def slave_code_template; end
|
544
|
+
|
545
|
+
def master_code_template; end
|
546
|
+
end
|
547
|
+
|
548
|
+
class Extension
|
549
|
+
include Depager::Utils::CommonMethods
|
550
|
+
include Depager::Utils::CodeGeneratorMethods
|
551
|
+
|
552
|
+
attr_reader :d_parser, :g_parser
|
553
|
+
|
554
|
+
def init_extension; end
|
555
|
+
|
556
|
+
def term_extension; end
|
557
|
+
|
558
|
+
def extension_registered(g_parser)
|
559
|
+
@g_parser = g_parser
|
560
|
+
@d_parser = g_parser.d_parser
|
561
|
+
|
562
|
+
methods.sort.each do |m|
|
563
|
+
m = m.to_sym
|
564
|
+
g_parser.hooks[m].push [self, m] if g_parser.hook_name? m
|
565
|
+
end
|
566
|
+
end
|
567
|
+
|
568
|
+
def decorator_name
|
569
|
+
self.class.name.split("::").last.sub(/Extension$/, "")
|
570
|
+
end
|
571
|
+
end
|
572
|
+
end
|