Class: Yaparc::Regex

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

Methods included from Parsable

#parse

Constructor Details

#initialize(regex, &block) ⇒ Regex

Returns a new instance of Regex.



268
269
270
271
272
273
274
275
276
277
278
279
280
281
# File 'lib/yaparc.rb', line 268

def initialize(regex, &block)
  @regex = regex
  @parser = lambda do |input|
    if match = Regexp.new(regex).match(input)
      if block_given?
        Succeed.new(yield(*match.to_a[1..match.to_a.length])).parse(match.post_match)
      else
        Result::OK.new(:value => match[0], :input => match.post_match)
      end
    else
      Result::Fail.new(:input => input)
    end
  end
end