class RDParser::Rule

Constants

Match

Public Class Methods

new(name, parser) click to toggle source
# File lib/asciitracker/rdparser.rb, line 102
def initialize(name, parser)
  @name = name
  @parser = parser
  @matches = []
  @lrmatches = []
end

Public Instance Methods

add_match(pattern, block) click to toggle source
# File lib/asciitracker/rdparser.rb, line 109
def add_match(pattern, block)
  match = Match.new(pattern, block)
  if pattern[0] == @name
    pattern.shift
    @lrmatches << match
  else
    @matches << match
  end
end
parse() click to toggle source
# File lib/asciitracker/rdparser.rb, line 119
def parse
  match_result = try_matches(@matches)
  return nil unless match_result
  loop do
    result = try_matches(@lrmatches, match_result)
    return match_result unless result
    match_result = result
  end
end

Private Instance Methods

try_matches(matches, pre_result = nil) click to toggle source
# File lib/asciitracker/rdparser.rb, line 131
def try_matches(matches, pre_result = nil)
  match_result = nil
  start = @parser.pos
  matches.each do |match|
    r = pre_result ? [pre_result] : []
    match.pattern.each do |token|
      if @parser.rules[token]
        r << @parser.rules[token].parse
        unless r.last
          r = nil
          break
        end
      else
        nt = @parser.expect(token)
        if nt
          r << nt
        else
          r = nil
          break
        end
      end
    end
    if r
      if match.block
        match_result = match.block.call(*r)
      else
        match_result = r[0]
      end
      break
    else
      @parser.pos = start
    end
  end
  return match_result
end