Class: Yaparc::Regex

Inherits:
Object
  • Object
show all
Includes:
Parsable
Defined in:
lib/yaparc/regex.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 Method Summary collapse

Methods included from Parsable

#parse

Constructor Details

#initialize(regex) ⇒ Regex

Returns a new instance of Regex.



7
8
9
10
11
12
13
14
15
16
17
18
19
20
# File 'lib/yaparc/regex.rb', line 7

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