Module: Raabro::ModuleMethods

Included in:
Raabro
Defined in:
lib/raabro.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#lastObject

Returns the value of attribute last.



529
530
531
# File 'lib/raabro.rb', line 529

def last
  @last
end

Class Method Details

.included(target) ⇒ Object



596
597
598
599
600
601
602
# File 'lib/raabro.rb', line 596

def self.included(target)

  target.instance_eval do
    extend ::Raabro::ModuleMethods
    extend self
  end
end

Instance Method Details

#_match(name, input, parter, regex_or_string) ⇒ Object



242
243
244
245
246
247
248
249
250
251
252
253
# File 'lib/raabro.rb', line 242

def _match(name, input, parter, regex_or_string)

  r = Raabro::Tree.new(name, parter, input)

  if l = input.match(regex_or_string)
    r.result = 1
    r.length = l
    input.offset += l
  end

  r
end

#_narrow(parser) ⇒ Object



279
280
281
282
283
284
# File 'lib/raabro.rb', line 279

def _narrow(parser)

  fail ArgumentError.new("lone quantifier #{parser}") if _quantify(parser)

  method(parser.to_sym)
end

#_parse(parser, input) ⇒ Object



286
287
288
289
290
291
292
293
# File 'lib/raabro.rb', line 286

def _parse(parser, input)

  #p [ caller.length, parser, input.tring ]
  #r = _narrow(parser).call(input)
  #p [ caller.length, parser, input.tring, r.to_a(children: false) ]
  #r
  _narrow(parser).call(input)
end

#_quantify(parser) ⇒ Object



265
266
267
268
269
270
271
272
273
274
275
276
277
# File 'lib/raabro.rb', line 265

def _quantify(parser)

  return nil if parser.is_a?(Symbol) && respond_to?(parser)
    # so that :plus and co can be overriden

  case parser
  when '?', :q, :qmark then [ 0, 1 ]
  when '*', :s, :star then [ 0, 0 ]
  when '+', :p, :plus then [ 1, 0 ]
  when '!' then :bang
  else nil
  end
end

#all(name, input, parser) ⇒ Object



438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
# File 'lib/raabro.rb', line 438

def all(name, input, parser)

  start = input.offset
  length = input.string.length - input.offset

  r = ::Raabro::Tree.new(name, :all, input)
  c = _parse(parser, input)
  r.children << c

  if c.length < length
    input.offset = start
  else
    r.result = 1
    r.length = c.length
  end

  r
end

#alt(name, input, *parsers) ⇒ Object



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
# File 'lib/raabro.rb', line 333

def alt(name, input, *parsers)

  greedy =
    if parsers.last == true || parsers.last == false
      parsers.pop
    else
      false
    end

  r = ::Raabro::Tree.new(name, greedy ? :altg : :alt, input)

  start = input.offset
  c = nil

  parsers.each do |pa|

    cc = _parse(pa, input)
    r.children << cc

    input.offset = start

    if greedy
      if cc.result == 1 && cc.length >= (c ? c.length : -1)
        c.result = 0 if c
        c = cc
      else
        cc.result = 0
      end
    else
      c = cc
      break if c.result == 1
    end
  end

  if c && c.result == 1
    r.result = 1
    r.length = c.length
    input.offset = start + r.length
  end

  r.prune! if input.options[:prune]

  r
end

#altg(name, input, *parsers) ⇒ Object



378
379
380
381
# File 'lib/raabro.rb', line 378

def altg(name, input, *parsers)

  alt(name, input, *parsers, true)
end

#eseq(name, input, startpa, eltpa, seppa = nil, endpa = nil) ⇒ Object Also known as: jseq



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
# File 'lib/raabro.rb', line 457

def eseq(name, input, startpa, eltpa, seppa=nil, endpa=nil)

  jseq = false

  if seppa.nil? && endpa.nil?
    jseq = true
    seppa = eltpa; eltpa = startpa; startpa = nil
  end

  start = input.offset
  r = ::Raabro::Tree.new(name, jseq ? :jseq : :eseq, input)
  r.result = 1
  c = nil

  if startpa
    c = _parse(startpa, input)
    r.children << c
    r.result = 0 if c.result != 1
  end

  if r.result == 1

    on_elt = false
    count = 0
    empty_stack = 0

    loop do

      on_elt = ! on_elt

      cr = _parse(on_elt ? eltpa : seppa, input)

      empty_stack = cr.empty? ? empty_stack + 1 : 0
      cr.result = 0 if empty_stack > 1
        #
        # prevent "no progress"

      r.children.push(cr)

      if cr.result != 1
        if on_elt && count > 0
          lsep = r.children[-2]
          lsep.result = 0
          input.offset = lsep.offset
        end
        break
      end

      count += 1
    end

    r.result = 0 if jseq && count < 1
  end

  if r.result == 1 && endpa
    c = _parse(endpa, input)
    r.children << c
    r.result = 0 if c.result != 1
  end

  if r.result == 1
    r.length = input.offset - start
  else
    input.offset = start
  end

  r.prune! if input.options[:prune]

  r
end

#make_includableObject



594
595
596
597
598
599
600
601
602
603
# File 'lib/raabro.rb', line 594

def make_includable

  def self.included(target)

    target.instance_eval do
      extend ::Raabro::ModuleMethods
      extend self
    end
  end
end

#method_added(name) ⇒ Object



531
532
533
534
535
536
537
538
# File 'lib/raabro.rb', line 531

def method_added(name)

  m = method(name)
  return unless m.arity == 1
  return unless m.parameters[0][1] == :i || m.parameters[0][1] == :input

  @last = name.to_sym
end

#nott(name, input, parser) ⇒ Object



422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
# File 'lib/raabro.rb', line 422

def nott(name, input, parser)

  start = input.offset

  r = ::Raabro::Tree.new(name, :nott, input)
  c = _parse(parser, input)
  r.children << c

  r.length = 0
  r.result = c.result == 1 ? 0 : 1

  input.offset = start

  r
end

#parse(input, opts = {}) ⇒ Object



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
# File 'lib/raabro.rb', line 540

def parse(input, opts={})

  d = opts[:debug].to_i
  opts[:rewrite] = false if d > 0
  opts[:all] = false if d > 1
  opts[:prune] = false if d > 2

  opts[:prune] = true unless opts.has_key?(:prune)

  root = self.respond_to?(:root) ? :root : @last

  t =
    if opts[:all] == false
      _parse(root, Raabro::Input.new(input, opts))
    else
      all(nil, Raabro::Input.new(input, opts), root)
    end

  return reparse_for_error(input, opts, t) if opts[:error] && t.result != 1
  return nil if opts[:prune] != false && t.result != 1

  t = t.children.first if t.parter == :all

  return rewrite(t) if opts[:rewrite] != false

  t
end

#ren(name, input, parser) ⇒ Object Also known as: rename



413
414
415
416
417
418
419
# File 'lib/raabro.rb', line 413

def ren(name, input, parser)

  r = _parse(parser, input)
  r.name = name

  r
end

#rep(name, input, parser, min, max = 0) ⇒ Object



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
# File 'lib/raabro.rb', line 383

def rep(name, input, parser, min, max=0)

  min = 0 if min == nil || min < 0
  max = nil if max.nil? || max < 1

  r = ::Raabro::Tree.new(name, :rep, input)
  start = input.offset
  count = 0

  loop do
    c = _parse(parser, input)
    r.children << c
    break if c.result != 1
    count += 1
    break if c.length < 1
    break if max && count == max
  end

  if count >= min && (max == nil || count <= max)
    r.result = 1
    r.length = input.offset - start
  else
    input.offset = start
  end

  r.prune! if input.options[:prune]

  r
end

#reparse_for_error(input, opts, t) ⇒ Object



568
569
570
571
572
573
574
575
576
577
# File 'lib/raabro.rb', line 568

def reparse_for_error(input, opts, t)

  t =
    opts[:prune] == false ?
    t :
    parse(input, opts.merge(error: false, rewrite: false, prune: false))
#Raabro.pp(t, colours: true)

  t.extract_error
end

#rewrite(tree) ⇒ Object



586
587
588
589
590
591
592
# File 'lib/raabro.rb', line 586

def rewrite(tree)

  return !! methods.find { |m| m.to_s.start_with?('rewrite_') } if tree == 0
    # return true when "rewrite_xxx" methods seem to have been provided

  send("rewrite_#{tree.name}", tree)
end

#rewrite_(tree) ⇒ Object



579
580
581
582
583
584
# File 'lib/raabro.rb', line 579

def rewrite_(tree)

  t = tree.lookup(nil)

  t ? rewrite(t) : nil
end

#rex(name, input, regex_or_string) ⇒ Object



260
261
262
263
# File 'lib/raabro.rb', line 260

def rex(name, input, regex_or_string)

  _match(name, input, :rex, Regexp.new(regex_or_string))
end

#seq(name, input, *parsers) ⇒ Object



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
# File 'lib/raabro.rb', line 295

def seq(name, input, *parsers)

  r = ::Raabro::Tree.new(name, :seq, input)

  start = input.offset
  c = nil

  loop do

    pa = parsers.shift
    break unless pa

    if parsers.first == '!'
      parsers.shift
      c = nott(nil, input, pa)
      r.children << c
    elsif q = _quantify(parsers.first)
      parsers.shift
      c = rep(nil, input, pa, *q)
      r.children.concat(c.children)
    else
      c = _parse(pa, input)
      r.children << c
    end

    break if c.result != 1
  end

  if c && c.result == 1
    r.result = 1
    r.length = input.offset - start
  else
    input.offset = start
  end

  r
end

#str(name, input, string) ⇒ Object



255
256
257
258
# File 'lib/raabro.rb', line 255

def str(name, input, string)

  _match(name, input, :str, string)
end