Class: Rattler::Parsers::BackReference

Inherits:
Parser show all
Defined in:
lib/rattler/parsers/back_reference.rb

Overview

BackReference matches the labeled result of an earlier match.

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Parser

#&, #>>, #capturing?, #capturing_decidable?, #labeled?, #list, #one_or_more, #optional, #repeat, #semantic?, #sequence?, #skip, #variable_capture_count?, #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

.[](ref_label) ⇒ BackReference

Returns a new parser that matches the value an earlier match whose result is labeled by ref_label.

Parameters:

  • ref_label (Symbol, String)

    the label referencing the earlier match

Returns:

  • (BackReference)

    a new parser that matches the value an earlier match whose result is labeled by ref_label



11
12
13
# File 'lib/rattler/parsers/back_reference.rb', line 11

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

.parsed(results, *_) ⇒ Object



16
17
18
# File 'lib/rattler/parsers/back_reference.rb', line 16

def self.parsed(results, *_)
  self[results.first[1..-1]]
end

Instance Method Details

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

If the earlier referenced match result appears again at the parse position, match that 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



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

def parse(scanner, rules, scope = ParserScope.empty)
  scanner.scan Regexp.compile(Regexp.escape scope[ref_label])
end

#re_expr(scope) ⇒ Object

return [String] ruby code for a Regexp that matches the earlier

referenced match result

Parameters:

  • scope (ParserScope)

    the scope of captured results



33
34
35
# File 'lib/rattler/parsers/back_reference.rb', line 33

def re_expr(scope)
  "/#{re_source scope}/"
end

#re_source(scope) ⇒ Object

return [String] the source of a Regexp that matches the earlier

referenced match result

Parameters:

  • scope (ParserScope)

    the scope of captured results



40
41
42
# File 'lib/rattler/parsers/back_reference.rb', line 40

def re_source(scope)
  '#{' + Regexp.escape(scope[ref_label].to_s) + '}'
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



45
46
47
# File 'lib/rattler/parsers/back_reference.rb', line 45

def with_ws(ws)
  ws.skip & self
end