class StatefulParser

Constants

VERSION

Public Class Methods

new(normalizer: nil) click to toggle source
# File lib/stateful_parser.rb, line 8
def initialize(normalizer: nil)
  @state = :none
  @states = []
  @normalizer = normalizer
end

Public Instance Methods

parse(line) { |state, line| ... } click to toggle source
# File lib/stateful_parser.rb, line 18
def parse(line)
  normalized_line = @normalizer ? @normalizer.call(line) : line
  matchers = @states.select { |s| s.last.include?(@state) || s.last.include?(:any) }
  new_state = nil
  matchers.each do |(name, match, _applies_to)|
    next unless normalized_line =~ match
    if new_state
      raise "multiple state transitions match: #{[new_state, name].inspect}"
    end

    new_state = name
  end
  @state = new_state if new_state
  yield @state, line
end
parse_string(string, &block) click to toggle source
# File lib/stateful_parser.rb, line 34
def parse_string(string, &block)
  string.each_line do |line|
    parse(line, &block)
  end
end
state(name, match, applies_to = [:none]) click to toggle source
# File lib/stateful_parser.rb, line 14
def state(name, match, applies_to = [:none])
  @states << [name, match, applies_to]
end