Class: ParserCombinator::ParsedSeq

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

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(seq) ⇒ ParsedSeq

Returns a new instance of ParsedSeq.



98
99
100
# File 'lib/parser_combinator.rb', line 98

def initialize(seq)
  @seq = seq
end

Class Method Details

.emptyObject



110
111
112
# File 'lib/parser_combinator.rb', line 110

def self.empty
  new([])
end

Instance Method Details

#[](key) ⇒ Object



118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
# File 'lib/parser_combinator.rb', line 118

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) ⇒ Object



114
115
116
# File 'lib/parser_combinator.rb', line 114

def cons(entity, name)
  self.class.new([{:entity => entity, :name => name}] + @seq)
end

#to_aObject



102
103
104
# File 'lib/parser_combinator.rb', line 102

def to_a
  @seq.map{|e| e[:entity]}
end

#to_hObject



106
107
108
# File 'lib/parser_combinator.rb', line 106

def to_h
  Hash[@seq.select{|e| e[:name]}.map{|e| [e[:name], e[:entity]]}]
end