Class: PlaceholderPattern

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

Overview

Implements using a pattern that has not been defined

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_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, #self_scramble_references, #single_entity?, #start_pattern, #then, #to_r, #transform_includes, #transform_tag_as, #zeroOrMoreOf

Constructor Details

#initialize(placeholder) ⇒ PlaceholderPattern #initialize(opts, deep_clone, original) ⇒ PlaceholderPattern

Constructs a new placeholder pattern

Overloads:

  • #initialize(placeholder) ⇒ PlaceholderPattern

    Parameters:

    • placeholder (Symbol)

      the name to replace with



15
16
17
18
19
20
21
22
23
24
# File 'lib/ruby_grammar_builder/pattern_extensions/placeholder.rb', line 15

def initialize(placeholder, deep_clone = nil, original_arguments = nil)
    if deep_clone == :deep_clone
        super(placeholder, deep_clone, original_arguments)
    else
        super(
            match: Regexp.new("placeholder(?##{placeholder})"),
            placeholder: placeholder
        )
    end
end

Instance Method Details

#evaluate(groups = nil) ⇒ String

Note:

this raises a runtime error if the pattern has not been resolved

evaluates the pattern into a string suitable for inserting into a grammar or constructing a Regexp.

Parameters:

  • groups (Hash) (defaults to: nil)

    if groups is nil consider this PatternBase to be the top_level when a pattern is top_level, group numbers and back references are relative to that pattern

Returns:

  • (String)

    the complete pattern



49
50
51
52
53
54
55
# File 'lib/ruby_grammar_builder/pattern_extensions/placeholder.rb', line 49

def evaluate(groups = nil)
    if @match.is_a?(String) && @match.start_with?("placeholder")
        raise "Attempting to evaluate an unresolved placeholder `:#{@arguments[:placeholder]}'"
    end

    super(groups)
end

#resolve!(repository) ⇒ self

Resolves the placeholder

Parameters:

  • repository (Hash)

    the repository to use to resolve the placeholder

Returns:

  • (self)


64
65
66
67
68
69
70
71
72
# File 'lib/ruby_grammar_builder/pattern_extensions/placeholder.rb', line 64

def resolve!(repository)
    unless repository[@arguments[:placeholder]].is_a? PatternBase
        raise ":#{@arguments[:placeholder]} is not a pattern and cannot be substituted"
    end

    @match = repository[@arguments[:placeholder]].__deep_clone__
    self
    # repository[@arguments[:placeholder]].resolve(repository)
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



27
28
29
30
31
32
33
34
# File 'lib/ruby_grammar_builder/pattern_extensions/placeholder.rb', line 27

def to_s(depth = 0, top_level = true)
    return super unless @match == "placeholder"

    output = top_level ? "placeholder(" : ".placeholder("
    output += ":#{@arguments[:placeholder]})"
    output += @next_pattern.to_s(depth, false).lstrip if @next_pattern
    output
end

#to_tagHash

Note:

this raises a runtime error if the pattern has not been resolved

converts a PatternBase to a Hash representing a textmate rule

Returns:

  • (Hash)

    The pattern as a textmate grammar rule



38
39
40
41
42
43
44
45
# File 'lib/ruby_grammar_builder/pattern_extensions/placeholder.rb', line 38

def to_tag
    if @match.is_a?(String) && @match.start_with?("placeholder")
        placeholder = @arguments[:placeholder]
        raise "Attempting to create a tag from an unresolved placeholder `:#{placeholder}'"
    end

    super()
end