Class: Rattler::Parsers::BackReference
- Inherits:
-
Parser
- Object
- Util::Node
- Parser
- Rattler::Parsers::BackReference
- Defined in:
- lib/rattler/parsers/back_reference.rb
Overview
BackReference
matches the labeled result of an earlier match.
Class Method Summary collapse
-
.[](ref_label) ⇒ BackReference
A new parser that matches the value an earlier match whose result is labeled by
ref_label
. - .parsed(results, *_) ⇒ Object
Instance Method Summary collapse
-
#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.
-
#re_expr(scope) ⇒ Object
return [String] ruby code for a
Regexp
that matches the earlier referenced match result. -
#re_source(scope) ⇒ Object
return [String] the source of a
Regexp
that matches the earlier referenced match result. -
#with_ws(ws) ⇒ Parser
A new parser that uses
ws
to skip whitespace.
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
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
.
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.
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
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
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.
45 46 47 |
# File 'lib/rattler/parsers/back_reference.rb', line 45 def with_ws(ws) ws.skip & self end |