Class: Yaparc::Satisfy

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

Overview

class SatisfyParser

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

Constructor Details

#initialize(predicate) ⇒ Satisfy

Returns a new instance of Satisfy.



134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
# File 'lib/yaparc.rb', line 134

def initialize(predicate)
  raise unless predicate.instance_of?(Proc)

  @parser = lambda do |input|
    case result = Item.new.parse(input)
    when Result::OK
      parser = if predicate.call(result.value)
                 Succeed.new(result.value, result.input)
               else
                 Fail.new
               end
    else # Result::Fail or Result::Error
      Fail.new
    end
  end
end

Instance Method Details

#parse(input) ⇒ Object



151
152
153
154
155
156
157
158
159
160
# File 'lib/yaparc.rb', line 151

def parse(input)
  case parser = @parser.call(input)
  when Succeed
    parser.parse(parser.remaining)
  when Fail
    parser.parse(input)
  else
    raise
  end
end