Class: RecursivelyMatchPattern

Inherits:
PatternBase show all
Defined in:
lib/ruby_grammar_builder/pattern_extensions/recursively_match.rb

Overview

Implements subexp calls for some group N whose reference is “foo” recursivelyMatch(“foo”) results in the pattern /gN/

Instance Attribute Summary

Attributes inherited from PatternBase

#arguments, #match, #next_pattern, #original_arguments

Instance Method Summary collapse

Methods inherited from PatternBase

#==, #__deep_clone__, #__deep_clone_self__, #collect_group_attributes, #convert_group_attributes_to_captures, #convert_includes_to_patterns, #do_add_attributes, #do_collect_self_groups, #do_evaluate_self, #do_get_to_s_name, #each, #eql?, #evaluate, #evaluate_operator, #fixup_regex_references, #groupless, #groupless?, #hash, #insert, #insert!, #inspect, #lookAheadFor, #lookAheadToAvoid, #lookAround, #lookBehindFor, #lookBehindToAvoid, #map, #map!, #map_includes!, #matchResultOf, #maybe, #name, #needs_to_capture?, #oneOf, #oneOrMoreOf, #optimize_outer_group?, #or, #placeholder, #raise_if_regex_has_capture_group, #reTag, #recursivelyMatch, #resolve, #run_self_tests, #run_tests, #start_pattern, #then, #to_r, #to_tag, #transform_includes, #transform_tag_as, #zeroOrMoreOf

Constructor Details

#initialize(reference, deep_clone = nil, original_arguments = nil) ⇒ RecursivelyMatchPattern

Returns a new instance of RecursivelyMatchPattern.



9
10
11
12
13
14
15
16
17
18
19
# File 'lib/ruby_grammar_builder/pattern_extensions/recursively_match.rb', line 9

def initialize(reference, deep_clone = nil, original_arguments = nil)
    if reference.is_a? String
        super(
            match: Regexp.new("(?#[:subroutine:#{reference}:])"),
            subroutine_key: reference
        )
    else
        # most likely __deep_clone__ was called, just call the super initializer
        super(reference, deep_clone, original_arguments)
    end
end

Instance Method Details

#self_capture_group_rematchtrue

RecursivelyMatchPattern can cause its capture groups to be rematched

Returns:

  • (true)

    rematching is possible



55
56
57
# File 'lib/ruby_grammar_builder/pattern_extensions/recursively_match.rb', line 55

def self_capture_group_rematch
    true
end

#self_scramble_referencesvoid

This method returns an undefined value.

Scrambles references of self This method provides a way to rename all references both actual references and references to references will be scrambled in some one to one mapping, all references that were unique before remain unique

This must be idempotent, calling this repeatedly must have references be as if it was called only once, even if the pattern is cloned between calls

this is because it may be called a different number of times depending on the nest
level of the patterns


36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/ruby_grammar_builder/pattern_extensions/recursively_match.rb', line 36

def self_scramble_references
    scramble = lambda do |name|
        return name if name.start_with?("__scrambled__")

        "__scrambled__" + name
    end

    key = @arguments[:subroutine_key]
    scrambled = scramble.call(key)

    @match = @match.sub(key, scrambled)
    @arguments[:subroutine_key] = scrambled
end

#single_entity?true

Returns:

  • (true)


31
32
33
# File 'lib/ruby_grammar_builder/pattern_extensions/recursively_match.rb', line 31

def single_entity?
    true
end

#to_s(depth = 0, top_level = true) ⇒ String

Displays the PatternBase as you would write it in code

Parameters:

  • depth (Integer) (defaults to: 0)

    the current nesting depth

  • top_level (Boolean) (defaults to: true)

    is this a top level pattern or is it being chained

Returns:

  • (String)

    The pattern as a string



22
23
24
25
26
27
# File 'lib/ruby_grammar_builder/pattern_extensions/recursively_match.rb', line 22

def to_s(depth = 0, top_level = true)
    output = top_level ? "recursivelyMatch(" : ".recursivelyMatch("
    output += "\"#{@arguments[:subroutine_key]}\")"
    output += @next_pattern.to_s(depth, false).lstrip if @next_pattern
    output
end