class Rattler::Parsers::BackReference

BackReference matches the labeled result of an earlier match.

Public Class Methods

[](ref_label) click to toggle source

@param [Symbol,String] ref_label the label referencing the earlier match @return [BackReference] a new parser that matches the value an earlier

match whose result is labeled by +ref_label+
# File lib/rattler/parsers/back_reference.rb, line 11
def self.[](ref_label)
  self.new(:ref_label => ref_label.to_sym)
end
parsed(results, *_) click to toggle source

@private

# File lib/rattler/parsers/back_reference.rb, line 16
def self.parsed(results, *_)
  self[results.first[1..-1]]
end

Public Instance Methods

parse(scanner, rules, scope = ParserScope.empty) click to toggle source

If the earlier referenced match result appears again at the parse position, match that string, otherwise return a false value.

@param (see Match#parse)

@return the matched string, or nil

# 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) click to toggle source

@param [ParserScope] scope the scope of captured results return [String] ruby code for a Regexp that matches the earlier

referenced match result
# File lib/rattler/parsers/back_reference.rb, line 33
def re_expr(scope)
  "/#{re_source scope}/"
end
re_source(scope) click to toggle source

@param [ParserScope] scope the scope of captured results return [String] the source of a Regexp that matches the earlier

referenced match result
# File lib/rattler/parsers/back_reference.rb, line 40
def re_source(scope)
  '#{' + Regexp.escape(scope[ref_label].to_s) + '}'
end
with_ws(ws) click to toggle source

(see Parser#with_ws)

# File lib/rattler/parsers/back_reference.rb, line 45
def with_ws(ws)
  ws.skip & self
end