Class: Yaparc::ManyOne

Inherits:
Object
  • Object
show all
Includes:
Parsable
Defined in:
lib/yaparc.rb

Overview

requires at least one successfull application of parser.

Constant Summary

Constants included from Parsable

Parsable::IS_ALPHANUM, Parsable::IS_CR, Parsable::IS_DIGIT, Parsable::IS_LOWER, Parsable::IS_SPACE, Parsable::IS_WHITESPACE

Instance Attribute Summary

Attributes included from Parsable

#tree

Instance Method Summary collapse

Methods included from Parsable

#parse

Constructor Details

#initialize(parser, identity = []) ⇒ ManyOne

Returns a new instance of ManyOne.



316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
# File 'lib/yaparc.rb', line 316

def initialize(parser, identity = [])
  @parser = lambda do |input|
    Seq.new(parser, Many.new(parser, identity)) do |head, tail|
      case head
      when ::String
        if tail.instance_of?(::String)
          head + tail
        else
          raise "Incompatible type: head => #{head.inspect}, tail => #{tail.inspect}"
        end
      when ::Array
        if tail.instance_of?(Array)
          head + tail
        else
          raise "Incompatible type: head => #{head.inspect}, tail => #{tail.inspect}"
        end
      when ::Hash
        if tail.instance_of?(Hash)
          head.merge(tail)
        else
          raise "Incompatible type: head => #{head.inspect}, tail => #{tail.inspect}"
        end
      when ::Integer
        if tail.kind_of?(Integer)
          head + tail
        else
          raise "Incompatible type: head => #{head.inspect}, tail => #{tail.inspect}"
        end
      else
        if tail.nil?
          head
        else
          [head] + tail
        end
      end
    end
  end
end