Class: Yaparc::Seq

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

Overview

class SeqParser

Constant Summary

Constants included from Parsable

Parsable::IS_ALPHANUM, 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

#eval, included, #parse

Constructor Details

#initialize(*parsers, &block) ⇒ Seq

Returns a new instance of Seq.



187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
# File 'lib/yaparc.rb', line 187

def initialize(*parsers, &block)
  @parser = lambda do |input|
    args = []
    initial_result = Result::OK.new(:input => input)
    final_result = parsers.inject(initial_result) do |subsequent, parser|
      case result = parser.parse(subsequent.input)
      when Result::Fail
        break Result::Fail.new(:input => subsequent.input)
#           when Result::Error
#             break Result::Error.new(:input => subsequent.input)
      else
        args << result.value
        result
      end
    end

    case final_result
    when Result::Fail
      Result::Fail.new(:input => final_result.input)
    when Result::OK
      final_value = if block_given?
                      yield(*args)
                    else
                      args.last
                    end
      Result::OK.new(:value => final_value, :input => final_result.input)
    else
      raise
    end
  end
end