Class: KPeg::Choice

Inherits:
Operator show all
Defined in:
lib/kpeg/grammar.rb

Instance Attribute Summary collapse

Attributes inherited from Operator

#action

Instance Method Summary collapse

Methods inherited from Operator

#detect_tags, #inspect_type, #prune_values, #set_action

Constructor Details

#initialize(*many) ⇒ Choice

Returns a new instance of Choice.



204
205
206
207
# File 'lib/kpeg/grammar.rb', line 204

def initialize(*many)
  super()
  @ops = many
end

Instance Attribute Details

#opsObject (readonly)

Returns the value of attribute ops.



209
210
211
# File 'lib/kpeg/grammar.rb', line 209

def ops
  @ops
end

Instance Method Details

#==(obj) ⇒ Object



230
231
232
233
234
235
236
237
# File 'lib/kpeg/grammar.rb', line 230

def ==(obj)
  case obj
  when Choice
    @ops == obj.ops
  else
    super
  end
end

#inspectObject



239
240
241
# File 'lib/kpeg/grammar.rb', line 239

def inspect
  inspect_type "any", @ops.map { |i| i.inspect }.join(' | ')
end

#match(x) ⇒ Object



216
217
218
219
220
221
222
223
224
225
226
227
228
# File 'lib/kpeg/grammar.rb', line 216

def match(x)
  pos = x.pos

  @ops.each do |c|
    if m = c.match(x)
      return m
    end

    x.pos = pos
  end

  return nil
end

#|(other) ⇒ Object



211
212
213
214
# File 'lib/kpeg/grammar.rb', line 211

def |(other)
  @ops << Grammar.resolve(other)
  self
end