public
Description: Library for parsing and compiling OMeta grammars to ruby
Clone URL: git://github.com/aquasync/ruby-ometa.git
Search Repo:
ruby-ometa / runtime.rb
100644 491 lines (413 sloc) 8.2 kb
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
UNDEFINED = Object.new
 
class Fail < StandardError
  attr_accessor :matcher
  attr_accessor :failPos
end
 
class Character < String
  undef_method :each
 
  class StringWrapper
    include Enumerable
 
    def initialize str
      @str = str
    end
 
    def length
      @str.length
    end
 
    def [] i
      Character.new @str[i..i]
    end
 
    def each
      length.times { |i| yield self[i] }
    end
 
    def to_s
      @str.to_s
    end
 
    def inspect
      @str.inspect
    end
  end
 
  # make them display with single quotes
  def inspect
    "'" + super[1..-2] + "'"
  end
end
 
class Stream
  def copy
    self
  end
end
 
class ReadStream < Stream
  attr_reader :pos
  def initialize obj
    if String === obj
#      puts 'note, wrapping string in a string wrapper'
      obj = Character::StringWrapper.new obj
    end
    @src = obj
    @pos = 0
  end
 
  def eof?
    @pos >= @src.length
  end
 
  def next
    @src[@pos]
  ensure
    @pos += 1
  end
end
 
class LazyStream < Stream
  def self.new head, tail, stream
    if stream and stream.eof?
      LazyInputStreamEnd.new stream
    else
      LazyInputStream.new head, tail, stream
    end
  end
end
 
class LazyInputStream < Stream
  attr_reader :memo
  def initialize head=UNDEFINED, tail=UNDEFINED, stream=nil
    @head = head
    @tail = tail
    @stream = stream
    @memo = {}
  end
 
  def head
    if @head == UNDEFINED
      @head = @stream.next
    end
    @head
  end
 
  def tail
    if @tail == UNDEFINED
      @tail = LazyStream.new UNDEFINED, UNDEFINED, @stream
    end
    @tail
  end
 
  # also interesting, due to backtracking, would possibly be to store
  # @stream.pos in initialize, which would presumably be the actual stream
  # pos at which any potential error occurred.
  def realPos
    @stream.pos
  end
end
 
class LazyInputStreamEnd < Stream
  attr_reader :memo
 
  def initialize stream
    @stream = stream
    @memo = {}
  end
 
  def realPos
    @stream.pos
  end
 
  def head
    raise Fail #EOFError
  end
 
  def tail
    raise NoMethodError
  end
end
 
class LazyStreamWrapper < Stream
  attr_reader :memo, :stream
  def initialize stream
    @stream = stream
    @memo = {}
  end
 
  def realPos
    @stream.pos
  end
 
  def head
    @stream.head
  end
 
  def tail
    self.class.new @stream.tail
  end
end
 
class LeftRecursion
  attr_accessor :detected
end
 
#// the OMeta "class" and basic functionality
#// TODO: make apply support indirect left recursion
 
class OMeta
  attr_reader :input
 
  def _apply(rule)
    #p rule
    memoRec = @input.memo[rule]
    if not memoRec
      oldInput = @input
      lr = LeftRecursion.new
      @input.memo[rule] = memoRec = lr
      # should these be copies too?
      @input.memo[rule] = memoRec = {:ans => method(rule).call, :nextInput => @input}
      if lr.detected
        sentinel = @input
        while true
          begin
            @input = oldInput
            ans = method(rule).call
            if @input == sentinel
              raise Fail
            end
            oldInput.memo[rule] = memoRec = {:ans => ans, :nextInput => @input}
          rescue Fail
            break
          end
        end
      end
    elsif LeftRecursion === memoRec
      memoRec.detected = true
      raise Fail
    end
    @input = memoRec[:nextInput]
    return memoRec[:ans]
  end
 
  def _applyWithArgs(rule, *args)
    #p :app_wit_arg => [rule, args]
    args.reverse_each do |arg|
      @input = LazyStream.new arg, @input, nil
    end
    send rule
  end
 
  def _superApplyWithArgs(rule, *args)
    #for (var idx = arguments.length - 1; idx > 1; idx--)
    # $elf.input = makeOMInputStream(arguments[idx], $elf.input, null)
    #return this[rule].apply($elf)
    # would probably be easier to use realsuper in the caller, rather than do this
    ##classes = self.class.ancestors.select { |a| Class === a }
    #methods = classes.map { |a| a.instance_method rule rescue nil }.compact
    #method = methods.first.bind(self)
    args.reverse_each do |arg|
      @input = LazyStream.new arg, @input, nil
    end
    # we leverage the inbuild ruby super, by means of the block passed to this function
    # which calls super
    yield
    #method.call
  end
 
  def _pred(b)
    if (b)
      return true
    end
    raise Fail
  end
 
  def _not(x)
    oldInput = @input.copy
    begin
      x.call
    rescue Fail
      @input = oldInput
      return true
    end
    raise Fail
  end
 
  def _lookahead(x)
    oldInput = @input.copy
    r = x.call
    @input = oldInput
    return r
  end
 
  def _or(*args)
    oldInput = @input.copy
    args.each do |arg|
      begin
        @input = oldInput
        return arg.call
      rescue Fail
      end
    end
    raise Fail
  end
 
  def _many(x, *ans)
    while true
      oldInput = @input.copy
      begin
        ans << x.call
      rescue Fail
        @input = oldInput
        break
      end
    end
    return ans
  end
 
  def _many1(x)
    _many x, x.call
  end
 
  def _form(x)
    v = _apply "anything"
    unless v.respond_to? :each
      raise Fail
    end
    oldInput = @input
    @input = LazyStream.new UNDEFINED, UNDEFINED, ReadStream.new(v)
    r = x.call
    _apply "end"
    @input = oldInput
    return v
  end
 
  #// some basic rules
  def anything
    r = @input.head
    @input = @input.tail
    return r
  end
 
  def end
    _not proc { return _apply("anything") }
  end
 
  def empty
    return true
  end
 
  def apply
    _apply _apply('anything')
  end
 
  def foreign
    g = _apply("anything")
    r = _apply("anything")
    fis = LazyStreamWrapper.new @input
    gi = g.new(fis)
    ans = gi._apply(r)
    @input = gi.input.stream
    #p :foreign => ans
    return ans
  end
 
  #// some useful "derived" rules
  def exactly
    wanted = _apply("anything")
    if wanted == _apply("anything")
      return wanted
    end
    raise Fail
  end
 
  def char
    r = _apply("anything")
    _pred(Character === r)
    return r
  end
 
  def space
    r = _apply("char")
    _pred(r[0] <= 32)
    return r
  end
 
  def spaces
    _many proc{ _apply("space") }
  end
 
  def digit
    r = _apply("char")
    _pred(r =~ /[0-9]/)
    return r
  end
 
  def lower
    r = _apply("char")
    _pred(r =~ /[a-z]/)
    return r
  end
 
  def upper
    r = _apply("char")
    _pred(r =~ /[A-Z]/)
    return r
  end
 
  def letter
    _or(
        proc { _apply 'lower' },
        proc { _apply 'upper' })
  end
 
  def letterOrDigit
    _or(
        proc { _apply 'letter' },
        proc { _apply 'digit' })
  end
 
  def firstAndRest
    first = _apply 'anything'
    rest = _apply 'anything'
    _many proc { _apply rest }, _apply(first)
  end
 
  def seq
    xs = _apply 'anything'
    if String === xs
      xs = Character::StringWrapper.new xs
    end
    xs.each { |obj| _applyWithArgs 'exactly', obj }
    xs
  end
 
  def notLast
    rule = _apply("anything")
    r = _apply(rule)
    _lookahead(proc { return _apply(rule) })
    return r
  end
 
  def initialize(input)
    @input = input
    initialize_hook
  end
  
  def initialize_hook
  end
 
  // #match:with: and #matchAll:with: are a grammar's "public interface"
  def self.genericMatch(input, rule, *args)
    m = new(input)
    begin
      if args.empty?
        m._apply(rule)
      else
        m._applyWithArgs(rule, *args)
      end
    rescue Fail
      $!.matcher = m
      raise
    end
  end
 
  def self.matchwith(obj, rule, *args)
    genericMatch LazyStream.new(UNDEFINED, UNDEFINED, ReadStream.new([obj])), rule, *args
  end
 
  def self.matchAllwith(listyObj, rule, *args)
    genericMatch LazyStream.new(UNDEFINED, UNDEFINED, ReadStream.new(listyObj)), rule, *args
  end
 
  # ----
 
  def listOf
    rule = _apply("anything")
    delim = _apply("anything")
    _or(proc {
      r = _apply(rule)
      _many(proc {
        _applyWithArgs("token", delim)
        _apply(rule)
        },
        r)
      },
      proc { [] }
    )
  end
 
  def token
    cs = _apply("anything")
    _apply("spaces")
    return _applyWithArgs("seq", cs)
  end
 
  def parse
    rule = _apply("anything"),
    ans = _apply(rule)
    _apply("end")
    return ans
  end
 
  NICER_FAILURE_METHODS = {
    '_or' => 'No matching alternative',
    nil => 'Failure'
  }
 
  def self.parsewith(text, rule)
    begin
      return matchAllwith(text, rule)
    rescue Fail => e
      e.failPos = e.matcher.input.realPos() - 1
      cause = NICER_FAILURE_METHODS[$@.first[/`(.*?)'/, 1]] || NICER_FAILURE_METHODS[nil]
      rule = $@.find { |l| l !~ /runtime/ }[/`(.*?)'/, 1]
      lines = text[0, e.failPos].to_a
      lines = [''] if lines.empty?
      message = "#{cause} in rule #{rule.inspect}, at line #{lines.length} character #{lines.last.length + 1}"
      raise e, message #, $@
    end
  end
end
 
__END__
 
current process:
 
g = File.read('ometa_parser.ometa'); puts g
 
begin; data = OMetaParser.matchAllwith(g, 'grammar'); rescue; $x = $!; $y = $@; end
 
str = RubyOMetaTranslator.matchwith(data, 'trans')
 
eval str
 
...