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.

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Atomic

#with_ws

Methods inherited from Parser

#&, #>>, #capturing?, #capturing_decidable?, #labeled?, #list, #one_or_more, #optional, #repeat, #semantic?, #sequence?, #skip, #variable_capture_count?, #with_ws, #zero_or_more, #|

Methods included from Runtime::ParserHelper

#select_captures

Methods inherited from Util::Node

#==, #[], #attrs, #can_equal?, #child, #children, #each, #empty?, #eql?, #initialize, #inspect, #method_missing, #name, #pretty_print, #pretty_print_cycle, #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

Returns a new match parser that matches with re.

Parameters:

  • re (Regexp)

    the pattern to match

Returns:

  • (Match)

    a new match parser that matches with re



13
14
15
# File 'lib/rattler/parsers/match.rb', line 13

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

.parsed(results, *_) ⇒ Object



18
19
20
# File 'lib/rattler/parsers/match.rb', line 18

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

Instance Method Details

#parse(scanner, rules, scope = ParserScope.empty) ⇒ Object

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

Parameters:

  • scanner (StringScanner)

    the scanner for the current parse

  • rules (RuleSet)

    the grammar rules being used for the current parse

  • scope (ParserScope) (defaults to: ParserScope.empty)

    the scope of captured results

Returns:

  • the matched string, or nil



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

def parse(scanner, rules, scope = ParserScope.empty)
  scanner.scan re
end