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.

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

.[](ref_label) ⇒ Object



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

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

.parsed(results, *_) ⇒ Object



22
23
24
# File 'lib/rattler/parsers/back_reference.rb', line 22

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

Instance Method Details

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



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

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

#re_expr(scope) ⇒ Object



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

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

#re_source(scope) ⇒ Object



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

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



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

def with_ws(ws)
  ws.skip & self
end