Class: PlaceholderPattern
- Inherits:
-
PatternBase
- Object
- PatternBase
- PlaceholderPattern
- 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
-
#evaluate(groups = nil) ⇒ String
evaluates the pattern into a string suitable for inserting into a grammar or constructing a Regexp.
-
#initialize(placeholder, deep_clone = nil, original_arguments = nil) ⇒ PlaceholderPattern
constructor
Constructs a new placeholder pattern.
-
#resolve!(repository) ⇒ self
Resolves the placeholder.
-
#to_s(depth = 0, top_level = true) ⇒ String
Displays the PatternBase as you would write it in code.
-
#to_tag ⇒ Hash
converts a PatternBase to a Hash representing a textmate rule.
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
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
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.
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
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
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_tag ⇒ Hash
this raises a runtime error if the pattern has not been resolved
converts a PatternBase to a Hash representing a textmate 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 |