Class: Rattler::Parsers::Match

Inherits:
Parser show all
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 inherited from Parser

#&, #capturing?, #labeled?, #one_or_more, #optional, #skip, #variable_capture_count?, #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



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

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

.parsed(results, *_) ⇒ Object



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

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



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

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

#with_ws(ws) ⇒ Parser

Returns a new parser that uses ws to skip whitespace.

Parameters:

  • ws (Parser)

    the parser used to skip whitespace

Returns:

  • (Parser)

    a new parser that uses ws to skip whitespace



47
48
49
# File 'lib/rattler/parsers/match.rb', line 47

def with_ws(ws)
  ws.skip & self
end