Class: ExcelFormulaParser::DFA

Inherits:
Object
  • Object
show all
Defined in:
lib/surpass/ExcelFormulaParser.rb

Instance Method Summary collapse

Constructor Details

#initialize(eot, eof, min, max, accept, special, transition) ⇒ DFA

Returns a new instance of DFA.



506
507
508
509
510
511
512
513
514
# File 'lib/surpass/ExcelFormulaParser.rb', line 506

def initialize(eot, eof, min, max, accept, special, transition)
    @eot = eot
    @eof = eof
    @min = min
    @max = max
    @accept = accept
    @special = special
    @transition = transition
end

Instance Method Details

#predict(parser, input) ⇒ Object



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

def predict(parser, input)
    mark = input.mark()
    s = 0 # we always start at s0
    begin
        loop do
            special_state = @special[s]
            if special_state >= 0
                s = parser.special_state_transition(special_state)
                input.consume()
                next
            end

            if @accept[s] >= 1
                return @accept[s]
            end

            # look for a normal char transition
            c = input.look_ahead(1).to_i
            if c != :EOF && c >= @min[s] && c <= @max[s]
                next_state = @transition[s][c - @min[s]] # move to next state
                if next_state < 0
                    # was in range but not a normal transition
                    # must check EOT, which is like the else clause.
                    # eot[s]>=0 indicates that an EOT edge goes to another
                    # state.
                    if @eot[s] >= 0  # EOT Transition to accept state?
                        s = @eot[s]
                        input.consume()
                        next
                    end
                    raise "No viable alt"
                end
                s = next_state
                input.consume()
                next
            end
            if @eot[s] >= 0   # EOT Transition?
                s = @eot[s]
                input.consume()
                next
            end
            if c == :EOF && @eof[s] >= 0   # EOF Transition to accept state?
                return @accept[@eof[s]]
            end

            # not in range and not EOF/EOT, must be invalid symbol
            raise "No viable alt"
        end
    ensure
        input.rewind(mark)
    end
end