Class: TDParser::IterationParser

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

Instance Attribute Summary collapse

Attributes inherited from CompositeParser

#parsers

Instance Method Summary collapse

Methods inherited from CompositeParser

#optimize, #same?

Methods inherited from Parser

#%, #*, #+, #-, #/, #>, #>>, #do, #optimize, #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(parser, n, range) ⇒ IterationParser

Returns a new instance of IterationParser.



512
513
514
515
516
# File 'lib/tdp.rb', line 512

def initialize(parser, n, range)
  @min = n
  @range = range
  super(parser)
end

Instance Attribute Details

#minObject (readonly)

Returns the value of attribute min.



511
512
513
# File 'lib/tdp.rb', line 511

def min
  @min
end

#rangeObject (readonly)

Returns the value of attribute range.



511
512
513
# File 'lib/tdp.rb', line 511

def range
  @range
end

Instance Method Details

#==(r) ⇒ Object



574
575
576
577
578
# File 'lib/tdp.rb', line 574

def ==(r)
  super(r) &&
  (@min == r.min) &&
  (@range == r.range)
end

#call(ts, buff) ⇒ Object



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
556
557
558
559
560
561
562
563
564
565
566
567
568
# File 'lib/tdp.rb', line 518

def call(ts, buff)
  r = @parsers[0]
  n = @min
  x  = true
  xs = []
  while( n > 0 )
    n -= 1
    b = prepare(buff)
    if( (x = r.call(ts, b)).nil? )
      recover(b, ts)
      break
    else
      buff.insert(0, *b)
      xs.push(x)
    end
  end
  if ( x.nil? )
    nil
  else
    if( range )
      range.each{
        while( true )
          y = x
          b = prepare(buff)
          if( (x = r.call(ts, b)).nil? )
            recover(b, ts)
            x = y
            break
          else
            buff.insert(0, *b)
            xs.push(x)
          end
        end
      }
    else
      while( true )
        y = x
        b = prepare(buff)
        if( (x = r.call(ts, b)).nil? )
          recover(b, ts)
          x = y
          break
        else
          buff.insert(0, *b)
          xs.push(x)
        end
      end
    end
    Sequence[xs]
  end
end

#to_sObject



570
571
572
# File 'lib/tdp.rb', line 570

def to_s()
  "(#{@parsers[0].to_s()})*#{@range ? @range.to_s : @min.to_s}"
end