Class: TDParser::ChoiceParser

Inherits:
CompositeParser show all
Defined in:
lib/tdp.rb

Instance Attribute Summary

Attributes inherited from CompositeParser

#parsers

Instance Method Summary collapse

Methods inherited from CompositeParser

#==, #same?

Methods inherited from Parser

#%, #*, #+, #-, #/, #==, #>, #>>, #do, #parse, #peek, #same?, #to_proc, #|, #~@

Methods included from TDParser

#any_rule, #backref, #chainl, #chainr, #condition_rule, define, #empty_rule, #fail_rule, #leftrec, #none_rule, #rightrec, #rule, #stackref, #state, #token

Methods included from BufferUtils

#prepare, #recover

Constructor Details

#initialize(r1, r2) ⇒ ChoiceParser

Returns a new instance of ChoiceParser.



400
401
402
# File 'lib/tdp.rb', line 400

def initialize(r1, r2)
  super(r1, r2)
end

Instance Method Details

#call(tokens, buff) ⇒ Object



404
405
406
407
408
409
410
411
412
413
# File 'lib/tdp.rb', line 404

def call(tokens, buff)
  b = prepare(buff)
  if( (x = @parsers[0].call(tokens, b)).nil? )
    recover(b, tokens)
    @parsers[1].call(tokens, buff)
  else
    buff.insert(0, *b)
    x
  end
end

#optimize(default = false) ⇒ Object



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

def optimize(default=false)
  r1 = @parsers[0]
  r2 = @parsers[1]
  if (r1.is_a?(ActionParser))
    act1 = r1.action
    r1 = r1.parsers[0]
  end
  if (r2.is_a?(ActionParser))
    act2 = r2.action
    r2 = r2.parsers[0]
  end
  share,r12,r22, = shared_sequence(r1, r2)
  if (share)
    r = share - (r12 + r22)
    if (act1)
      if (act2)
        r = r >> Proc.new{|x|
          y0,y1,*ys = x.pop()
          if (y0)
            act1.call(x.push(*y0))
          else
            act2.call(x.push(*y1))
          end
        }
      else
        r = r >> Proc.new{|x|
          y0,y1,*ys = x.pop()
          if (y0)
            act1.call(x.push(*y0))
          end
        }
      end
    else
      if (act2)
        r = r >> Proc.new{|x|
          y0,y1,*ys = x.pop()
          if (y1)
            act2.call(x.push(*y1))
          end
        }
      end
    end
    return r
  end
  if (default)
    self.dup()
  else
    super(default)
  end
end

#shared_sequence(r1, r2) ⇒ Object



419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
# File 'lib/tdp.rb', line 419

def shared_sequence(r1, r2)
  if (r1.is_a?(ConcatParser) && r2.is_a?(ConcatParser))
    r11 = r1.parsers[0]
    r12 = r1.parsers[1]
    r21 = r2.parsers[0]
    r22 = r2.parsers[1]
    if (r11.same?(r21))
      share,r12,r22, = shared_sequence(r12, r22)
      if (share)
        return [r11 - share, r12, r22]
      else
        return [r11, r12, r22]
      end
    end
  end
  [nil, r1, r2]
end

#to_sObject



415
416
417
# File 'lib/tdp.rb', line 415

def to_s()
  "(#{@parsers[0].to_s()} | #{@parsers[1].to_s()})"
end