Class: Yaparc::Satisfy

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

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

Constructor Details

#initialize(predicate) ⇒ Satisfy

Returns a new instance of Satisfy.



103
104
105
106
107
108
109
110
111
112
113
# File 'lib/yaparc.rb', line 103

def initialize(predicate)
  raise unless predicate.instance_of?(Proc)
  @parser = lambda do |input|
    result = Item.new.parse(input)
    if result.instance_of?(Result::OK) and predicate.call(result.value)
      Succeed.new(result.value, result.input)
    else
      Fail.new
    end
  end
end

Instance Method Details

#parse(input) ⇒ Object



115
116
117
118
119
120
121
122
123
124
# File 'lib/yaparc.rb', line 115

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