Class: Rattler::Parsers::Match

Inherits:
Parser show all
Includes:
Atomic
Defined in:
lib/rattler/parsers/match.rb

Overview

Match parses by matching with a Regexp. If the Regexp matches at the parse position the entire matched string is returned, otherwise the parse fails.

Author:

  • Jason Arhart

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Atomic

#with_ws

Methods inherited from Parser

#&, #capturing?, #labeled?, #one_or_more, #optional, #skip, #variable_capture_count?, #with_ws, #zero_or_more, #|

Methods inherited from Util::Node

#==, #[], #attrs, #can_equal?, #child, #children, #each, #empty?, #eql?, #initialize, #inspect, #method_missing, #name, #respond_to?, #same_contents?, #to_graphviz, #with_attrs, #with_attrs!, #with_children

Constructor Details

This class inherits a constructor from Rattler::Util::Node

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class Rattler::Util::Node

Class Method Details

.[](re) ⇒ Match

Create a new parser that matches with re.

Parameters:

  • re (Regexp)

    the pattern to match

Returns:

  • (Match)

    a new match parser that matches with re



27
28
29
# File 'lib/rattler/parsers/match.rb', line 27

def self.[](re)
  self.new(:re => re)
end

.parsed(results, *_) ⇒ Object



32
33
34
# File 'lib/rattler/parsers/match.rb', line 32

def self.parsed(results, *_) #:nodoc:
  self[eval(results.first)]
end

Instance Method Details

#parse(scanner, rules, scope = {}) ⇒ Object

If the Regexp matches at the parse position, return the matched string, otherwise return a false value.

Returns:

  • the matched string, or nil



42
43
44
# File 'lib/rattler/parsers/match.rb', line 42

def parse(scanner, rules, scope={})
  scanner.scan re
end