class ParserCombinator::ParsedSeq

Public Class Methods

empty() click to toggle source
# File lib/parser_combinator.rb, line 74
def self.empty
  new([])
end
new(seq) click to toggle source
# File lib/parser_combinator.rb, line 62
def initialize(seq)
  @seq = seq
end

Public Instance Methods

[](key) click to toggle source
# File lib/parser_combinator.rb, line 82
def [](key)
  case key
  when Integer
    if 0 <= key && key < @seq.length
      @seq[key][:entity]
    else
      raise "out of bounds for ParsedSeq"
    end
  else
    if e = @seq.find{|e| e[:name] == key}
      e[:entity]
    else
      raise "key #{key} is not found in ParsedSeq"
    end
  end
end
cons(entity, name) click to toggle source
# File lib/parser_combinator.rb, line 78
def cons(entity, name)
  self.class.new([{:entity => entity, :name => name}] + @seq)
end
to_a() click to toggle source
# File lib/parser_combinator.rb, line 66
def to_a
  @seq.map{|e| e[:entity]}
end
to_h() click to toggle source
# File lib/parser_combinator.rb, line 70
def to_h
  Hash[@seq.select{|e| e[:name]}.map{|e| [e[:name], e[:entity]]}]
end