forked from aquasync/ruby-ometa
/
bootstrap.rb
555 lines (418 loc) · 14 KB
/
bootstrap.rb
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
# Automatically generated. DO NOT MODIFY.
OMeta = Class.new(OMetaCore) do
@name = "OMeta"
def _dot_
_apply("anything")
end
def regch
regex = c = nil
(regex = _apply("anything");c = _apply("char");_pred(Regexp.new("[#{regex}]").match(c));c)
end
def end
_xnot { _apply("_dot_") }
end
def empty
true
end
def endline
_or(proc { (_applyWithArgs("token", "\r");_applyWithArgs("token", "\n")) }, proc { _applyWithArgs("token", "\r") }, proc { _applyWithArgs("token", "\n") })
end
def char
c = nil
(c = _apply("_dot_");_pred(Character === c );c)
end
def space
_applyWithArgs("regch", " \t\r\n\f")
end
def spaces
_xmany { _apply("space") }
end
def digit
_applyWithArgs("regch", "0-9")
end
def lower
_applyWithArgs("regch", "a-z")
end
def upper
_applyWithArgs("regch", "A-Z")
end
def letter
_or(proc { _apply("lower") }, proc { _apply("upper") })
end
def letterOrDigit
_or(proc { _apply("letter") }, proc { _apply("digit") })
end
def alpha
_apply("letter")
end
def alnum
_apply("letterOrDigit")
end
def xdigit
_applyWithArgs("regch", "0-9a-fA-F")
end
def word
_or(proc { _apply("alpha") }, proc { _applyWithArgs("exactly", "_") })
end
def exactly
wanted = got = nil
(wanted = _apply("anything");got = _apply("anything");wanted == got ? wanted : (raise Fail))
end
def notLast
rule = r = nil
(rule = _apply("anything");r = _applyWithArgs("apply", rule);_xlookahead { _applyWithArgs("apply", rule) };r)
end
def token
cs = nil
(cs = _apply("anything");_apply("spaces");_applyWithArgs("seq", cs))
end
def firstAndRest
first = rest = f = r = nil
(first = _apply("anything");rest = _apply("anything");f = _applyWithArgs("apply", first);r = _xmany { _applyWithArgs("apply", rest) };r.unshift(f))
end
def listOf
rule = delim = f = r = nil
(rule = _apply("anything");delim = _apply("anything");_or(proc { (f = _applyWithArgs("apply", rule);r = _xmany { (_applyWithArgs("apply", delim);_applyWithArgs("apply", rule)) };r.unshift(f)) }, proc { (_apply("empty");[]) }))
end
end
NullOptimizer = Class.new(OMeta) do
@name = "NullOptimizer"
def setHelped
@_didSomething = true
end
def helped
_pred(@_didSomething)
end
def trans
t = ans = nil
_or(proc { (_xform { (t = _apply("anything");_pred(respond_to?(t));ans = _applyWithArgs("apply", t)) }; ans ) }, proc { _apply("anything") })
end
def optimize
x = nil
(x = _apply("trans");_apply("helped"); x )
end
def Or
xs = nil
(xs = _xmany { _apply("trans") }; ['Or', *xs] )
end
def And
xs = nil
(xs = _xmany { _apply("trans") }; ['And', *xs] )
end
def Many
x = nil
(x = _apply("trans"); ['Many', x] )
end
def Many1
x = nil
(x = _apply("trans"); ['Many1', x] )
end
def Set
n = v = nil
(n = _apply("anything");v = _apply("trans"); ['Set', n, v] )
end
def Not
x = nil
(x = _apply("trans"); ['Not', x] )
end
def Lookahead
x = nil
(x = _apply("trans"); ['Lookahead', x] )
end
def Form
x = nil
(x = _apply("trans"); ['Form', x] )
end
def Rule
name = ls = ar = body = nil
(name = _apply("anything");ls = _apply("anything");ar = _apply("anything");body = _apply("trans"); ['Rule', name, ls,ar, body] )
end
def initialize_hook
@_didSomething = false
end
end
AndOrOptimizer = Class.new(NullOptimizer) do
@name = "AndOrOptimizer"
def And
x = xs = nil
_or(proc { (x = _apply("trans");_apply("end");_apply("setHelped");x ) }, proc { (xs = _applyWithArgs("transInside", "And");['And', *xs] ) })
end
def Or
x = xs = nil
_or(proc { (x = _apply("trans");_apply("end");_apply("setHelped");x ) }, proc { (xs = _applyWithArgs("transInside", "Or");['Or', *xs] ) })
end
def transInside
t = xs = ys = x = xs = nil
(t = _apply("anything");_or(proc { (_xform { (_applyWithArgs("exactly", t);xs = _applyWithArgs("transInside", t)) };ys = _applyWithArgs("transInside", t);_apply("setHelped");xs + ys ) }, proc { (x = _apply("trans");xs = _applyWithArgs("transInside", t);[x, *xs] ) }, proc { [] }))
end
end
OMetaOptimizer = Class.new(OMeta) do
@name = "OMetaOptimizer"
def optimizeGrammar
n = sn = rs = nil
(_xform { (_applyWithArgs("exactly", "Grammar");n = _apply("anything");sn = _apply("anything");rs = _xmany { _apply("optimizeRule") }) }; ['Grammar', n, sn, *rs] )
end
def optimizeRule
r = r = nil
(r = _apply("anything");_xmany { r = _applyWithArgs("foreign", AndOrOptimizer, "optimize", r) }; r )
end
end
BSRubyParser = Class.new(OMeta) do
@name = "BSRubyParser"
def eChar
c = nil
_or(proc { (_applyWithArgs("exactly", "\\");c = _apply("char"); unescapeChar c ) }, proc { _apply("char") })
end
def tsString
xs = nil
(_applyWithArgs("exactly", "'");xs = _xmany { (_xnot { _applyWithArgs("exactly", "'") };_apply("eChar")) };_applyWithArgs("exactly", "'"); xs.join('') )
end
def nonBraChar
(_xnot { _applyWithArgs("exactly", "(") };_xnot { _applyWithArgs("exactly", ")") };_apply("char"))
end
def insideBra
o = nil
_or(proc { (o = _apply("omproc");[o]) }, proc { _apply("innerBra") }, proc { _apply("nonBraChar") })
end
def innerBra
xs = nil
(_applyWithArgs("exactly", "(");xs = _xmany { _apply("insideBra") };_applyWithArgs("exactly", ")");"("+xs*"" +")" )
end
def outerBra
xs = nil
(_applyWithArgs("exactly", "(");xs = _xmany { _apply("insideBra") };_applyWithArgs("exactly", ")"); [ xs] )
end
def expr
_apply("outerBra")
end
def semAction1
xs = nil
(_apply("spaces");xs = _xmany { (_xnot { _applyWithArgs("seq", "\n") };_apply("anything")) }; xs.join('') )
end
def nonBraceChar
(_xnot { _applyWithArgs("exactly", "{") };_xnot { _applyWithArgs("exactly", "}") };_apply("char"))
end
def inside
_or(proc { _apply("innerBraces") }, proc { _apply("nonBraceChar") })
end
def innerBraces
xs = nil
(_applyWithArgs("exactly", "{");xs = _xmany { _apply("inside") };_applyWithArgs("exactly", "}"); "{#{xs.join('')}}" )
end
def outerBraces
xs = nil
(_applyWithArgs("exactly", "{");xs = _xmany { _apply("inside") };_applyWithArgs("exactly", "}"); xs.join('') )
end
def semAction2
(_apply("spaces");_apply("outerBraces"))
end
def semAction
_or(proc { _apply("semAction2") }, proc { _apply("semAction1") })
end
def omproc
x = nil
(_applyWithArgs("exactly", "`");x = _applyWithArgs("foreign", OMetaParser, "expr");_applyWithArgs("exactly", "`");x)
end
end
BSRubyTranslator = Class.new(OMeta) do
@name = "BSRubyTranslator"
def trans
_apply("anything")
end
end
OMetaParser = Class.new(OMeta) do
@name = "OMetaParser"
def nameFirst
_or(proc { _applyWithArgs("regch", "_$.^") }, proc { _apply("letter") })
end
def nameRest
_or(proc { _apply("nameFirst") }, proc { _apply("digit") })
end
def tsName
xs = nil
(xs = _applyWithArgs("firstAndRest", "nameFirst","nameRest");leterize(xs.join('')) )
end
def name
(_apply("spaces");_apply("tsName"))
end
def eChar
c = nil
_or(proc { (_applyWithArgs("exactly", "\\");c = _apply("char");unescapeChar c ) }, proc { _apply("char") })
end
def tsString
xs = nil
(_applyWithArgs("exactly", "'");xs = _xmany { (_xnot { _applyWithArgs("exactly", "'") };_apply("eChar")) };_applyWithArgs("exactly", "'");xs.join('') )
end
def characters
xs = nil
(_applyWithArgs("exactly", "'");_applyWithArgs("exactly", "'");xs = _xmany { (_xnot { (_applyWithArgs("exactly", "'");_applyWithArgs("exactly", "'")) };_apply("eChar")) };_applyWithArgs("exactly", "'");_applyWithArgs("exactly", "'");['App', 'seq', xs.join('').inspect] )
end
def sCharacters
xs = nil
(_applyWithArgs("exactly", "\"");xs = _xmany { (_xnot { _applyWithArgs("exactly", "\"") };_apply("eChar")) };_applyWithArgs("exactly", "\"");['App', 'token', xs.join('').inspect] )
end
def string
xs = nil
(xs = _or(proc { (_applyWithArgs("exactly", "#");_apply("tsName")) }, proc { _apply("tsString") });['App', 'exactly', xs.inspect] )
end
def number
sign = ds = nil
(sign = _or(proc { _applyWithArgs("exactly", "-") }, proc { (_apply("empty"); '' ) });ds = _xmany1 { _apply("digit") };['App', 'exactly', sign + ds.join('')] )
end
def keyword
xs = nil
(xs = _apply("anything");_applyWithArgs("token", xs);_xnot { _apply("letterOrDigit") };xs )
end
def hostExpr
r = nil
(r = _applyWithArgs("foreign", BSRubyParser, "expr");_applyWithArgs("foreign", BSRubyTranslator, "trans", r))
end
def atomicHostExpr
r = nil
(r = _applyWithArgs("foreign", BSRubyParser, "semAction");_applyWithArgs("foreign", BSRubyTranslator, "trans", r))
end
def inlineHostExpr
r = nil
(r = _applyWithArgs("foreign", BSRubyParser, "semAction2");_applyWithArgs("foreign", BSRubyTranslator, "trans", r))
end
def args
xs = nil
_or(proc { (_xnot { _apply("space") };xs = _apply("hostExpr");xs ) }, proc { (_apply("empty");"" ) })
end
def application
klas = rule = as = rule = as = nil
_or(proc { (klas = _apply("name");_applyWithArgs("token", "::");rule = _apply("name");as = _apply("args");['App', 'foreign',klas,rule.inspect, *as] ) }, proc { (rule = _apply("name");as = _apply("args");['App', rule, *as] ) })
end
def or
_applyWithArgs("token", "|")
end
def expr
xs = nil
(xs = _applyWithArgs("listOf", "expr4", "or");(xs.size==1 ? xs[0] : ['Or', *xs] ))
end
def expr4
xs = nil
(xs = _xmany { _apply("expr3") };(xs.size==1 ? xs[0] : ['And', *xs] ))
end
def optIter
x = nil
(x = _apply("anything");_or(proc { (_applyWithArgs("token", "*");['Many', x]) }, proc { (_applyWithArgs("token", "+");['Many1', x] ) }, proc { (_applyWithArgs("token", "?");_xnot { _apply("inlineHostExpr") };['Or', x,['App','empty']] ) }, proc { (_apply("empty");x ) }))
end
def binding
x = n = nil
(x = _apply("anything");_applyWithArgs("exactly", ":");n = _apply("name");_or(proc { (_applyWithArgs("exactly", "[");_applyWithArgs("exactly", "]");@arrays << n; ['Append', n, x] ) }, proc { (_apply("empty");@locals << n; ['Set', n, x]) }))
end
def expr3
x = x = nil
_or(proc { (x = _apply("expr2");x = _applyWithArgs("optIter", x);_or(proc { _applyWithArgs("binding", x) }, proc { (_apply("empty");x) })) }, proc { (_apply("spaces"); x=['App','anything'];_applyWithArgs("binding", x)) })
end
def expr2
x = x = nil
_or(proc { (_applyWithArgs("token", "~");x = _apply("expr2");['Not', x] ) }, proc { (_applyWithArgs("token", "&");_xnot { _apply("inlineHostExpr") };x = _apply("expr1");['Lookahead', x] ) }, proc { _apply("expr1") })
end
def expr1
x = x = x = x = x = nil
_or(proc { _apply("application") }, proc { (_applyWithArgs("token", "->");x = _apply("atomicHostExpr");['Act', x]) }, proc { (_applyWithArgs("token", "&");x = _apply("inlineHostExpr");['Pred', x]) }, proc { (_apply("spaces");_or(proc { _apply("characters") }, proc { _apply("sCharacters") }, proc { _apply("string") }, proc { _apply("number") })) }, proc { (_applyWithArgs("token", "[");x = _apply("expr");_applyWithArgs("token", "]");['Form', x] ) }, proc { (_applyWithArgs("token", "<");x = _xmany1 { (_xnot { _applyWithArgs("token", ">") };_apply("eChar")) };_applyWithArgs("token", ">");['App', 'regch', x.join('').inspect] ) }, proc { (_applyWithArgs("token", "(");x = _apply("expr");_applyWithArgs("token", ")");x ) })
end
def ruleName
_or(proc { _apply("name") }, proc { (_apply("spaces");_apply("tsString")) })
end
def rule
n = x = xs = nil
(_xlookahead { n = _apply("ruleName") };@locals = []; @arrays = [];x = _applyWithArgs("rulePart", n);xs = _xmany { (_apply("ruleSep");_applyWithArgs("rulePart", n)) };['Rule', n, @locals,@arrays, ['Or', x, *xs]] )
end
def rulePart
rn = n = b1 = b2 = nil
(rn = _apply("anything");n = _apply("ruleName");_pred(n == rn);b1 = _apply("expr4");_or(proc { (_applyWithArgs("token", "=");b2 = _apply("expr");['And', b1, b2] ) }, proc { (_apply("empty");b1 ) }))
end
def grammar
n = sn = rs = nil
(_applyWithArgs("keyword", "ometa");n = _apply("name");sn = _or(proc { (_applyWithArgs("token", "<:");_apply("name")) }, proc { (_apply("empty"); 'OMeta' ) });_applyWithArgs("token", "{");rs = _applyWithArgs("listOf", "rule", "ruleSep");_applyWithArgs("token", "}");['Grammar', n, sn, *rs])
end
def ruleSep
_applyWithArgs("token", ",")
end
end
RubyOMetaTranslator = Class.new(OMeta) do
@name = "RubyOMetaTranslator"
def trans
t = ans = nil
(_xform { (t = _apply("anything");ans = _applyWithArgs("apply", t)) }; ans )
end
def App
args = rule = args = rule = nil
_or(proc { (_applyWithArgs("exactly", "super");args = _xmany1 { _apply("arg") }; "_superApplyWithArgs(#{args*", "})" ) }, proc { (rule = _apply("anything");args = _xmany1 { _apply("arg") }; "_applyWithArgs(#{rule.inspect}, #{args*", "})" ) }, proc { (rule = _apply("anything"); "_apply(#{rule.inspect})" ) })
end
def arg
m = s = nil
_or(proc { (_xform { m = _xmany { _apply("ag") } };m*"") }, proc { (s = _apply("anything");_pred(s.is_a? String);s) })
end
def ag
t = nil
_or(proc { _apply("char") }, proc { (_xform { t = _apply("transFn") };t) })
end
def Act
expr = nil
(expr = _apply("anything"); expr )
end
def Pred
expr = nil
(expr = _apply("anything"); "_pred(#{expr})" )
end
def Or
xs = nil
(xs = _xmany { _apply("transFn") }; "_or(#{xs * ', '})" )
end
def And
xs = y = nil
_or(proc { (xs = _xmany { _applyWithArgs("notLast", "trans") };y = _apply("trans"); "(#{(xs + [y]) * ';'})" ) }, proc { "proc {}" })
end
def Many
x = nil
(x = _apply("trans"); "_xmany { #{x} }" )
end
def Many1
x = nil
(x = _apply("trans"); "_xmany1 { #{x} }" )
end
def Set
n = v = nil
(n = _apply("anything");v = _apply("trans"); "#{n} = #{v}" )
end
def Append
n = v = nil
(n = _apply("anything");v = _apply("trans"); "#{n} << #{v}" )
end
def Not
x = nil
(x = _apply("trans"); "_xnot { #{x} }" )
end
def Lookahead
x = nil
(x = _apply("trans"); "_xlookahead { #{x} }" )
end
def Form
x = nil
(x = _apply("trans"); "_xform { #{x} }" )
end
def Rule
name = ls = ars = body = nil
(name = _apply("anything");ls = _apply("locals");ars = _apply("arrays");body = _apply("trans"); "def #{name}\n#{ls}\n#{body}\nend\n" )
end
def Grammar
name = sName = rules = nil
(name = _apply("anything");sName = _apply("anything");rules = _xmany { _apply("trans") }; "Class.new(#{sName}) do\n@name = #{name.inspect}\n#{rules * "\n"}end" )
end
def locals
vs = nil
_or(proc { (_xform { vs = _xmany1 { _apply("anything") } }; vs.map { |v| "#{v} = " }.join('') + 'nil' ) }, proc { (_xform { proc {} }; '' ) })
end
def arrays
vs = nil
_or(proc { (_xform { vs = _xmany1 { _apply("anything") } }; vs.map { |v| "#{v} = " }.join('') + '[]' ) }, proc { (_xform { proc {} }; '' ) })
end
def transFn
x = nil
(x = _apply("trans"); "proc { #{x} }" )
end
end