Class: Yaparc::Regex

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

Overview

class RegexParser

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(regex, &block) ⇒ Regex

Returns a new instance of Regex.



290
291
292
293
294
295
296
297
298
299
300
301
302
303
# File 'lib/yaparc.rb', line 290

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

Instance Method Details

#parse_with_parameter(input) ⇒ Object



305
306
307
308
309
310
311
312
313
314
315
# File 'lib/yaparc.rb', line 305

def parse_with_parameter(input)
  if match = Regexp.new(@regex).match(input)
    if block_given?
      yield match.to_a[1..match.to_a.length]
    else
      Result::OK.new(:value => match, :input => match.post_match)
    end
  else
    Result::Fail.new(:input => input)
  end
end