Class: PatternRange
- Inherits:
-
PatternBase
- Object
- PatternBase
- PatternRange
- Defined in:
- lib/ruby_grammar_builder/pattern_variations/pattern_range.rb
Overview
Provides the ability to create begin/end and begin/while rules
Instance Attribute Summary collapse
-
#start_pattern ⇒ Object
readonly
Returns the value of attribute start_pattern.
Attributes inherited from PatternBase
#arguments, #match, #next_pattern, #original_arguments
Instance Method Summary collapse
-
#__deep_clone__ ⇒ PatternBase
Deeply clone self.
-
#do_evaluate_self(*_ignored) ⇒ void
Raises an error to prevent use inside a pattern list.
-
#evaluate(*_ignored) ⇒ void
Raises an error to prevent use inside a pattern list.
-
#initialize(arguments) ⇒ PatternRange
constructor
Creates a new PatternRange.
-
#inspect ⇒ String
Displays the Pattern for inspection.
-
#map!(map_includes = false) {|self| ... } ⇒ self
Uses a block to transform all Patterns in the list.
-
#run_tests ⇒ Boolean
Runs the unit tests, recursively.
-
#to_s ⇒ String
Displays this pattern range as source code that would generate it.
-
#to_tag ⇒ Hash
Generate a Textmate rule from the PatternRange.
-
#transform_includes {|PatternBase, Symbol, Regexp, String| ... } ⇒ PatternBase
Uses block to recursively transform includes.
Methods inherited from PatternBase
#==, #__deep_clone_self__, #collect_group_attributes, #convert_group_attributes_to_captures, #convert_includes_to_patterns, #do_add_attributes, #do_collect_self_groups, #do_get_to_s_name, #each, #eql?, #evaluate_operator, #fixup_regex_references, #groupless, #groupless?, #hash, #insert, #insert!, #lookAheadFor, #lookAheadToAvoid, #lookAround, #lookBehindFor, #lookBehindToAvoid, #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, #self_scramble_references, #single_entity?, #then, #to_r, #transform_tag_as, #zeroOrMoreOf
Constructor Details
#initialize(arguments) ⇒ PatternRange
exactly one of :end_pattern or :while_pattern is required
Creates a new PatternRange
Plugins may add additional options
27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 |
# File 'lib/ruby_grammar_builder/pattern_variations/pattern_range.rb', line 27 def initialize(arguments) @original_arguments = arguments @match = nil @next_pattern = nil raise "PatternRange.new() expects a hash" unless arguments.is_a? Hash # ensure end_pattern: XOR while_pattern: is provided if arguments[:end_pattern].nil? && arguments[:while_pattern].nil? raise "one of `while_pattern:` or `end_pattern` must be supplied" end if !arguments[:end_pattern].nil? && !arguments[:while_pattern].nil? raise "only one of `while_pattern:` or `end_pattern` must be supplied" end @start_pattern = arguments[:start_pattern] @stop_pattern = arguments[:end_pattern] || arguments[:while_pattern] # convert to patterns if needed @start_pattern = PatternBase.new(@start_pattern) unless @start_pattern.is_a? PatternBase @stop_pattern = PatternBase.new(@stop_pattern) unless @stop_pattern.is_a? PatternBase # store originals for to_s @original_start_pattern = @start_pattern @original_stop_pattern = @stop_pattern if arguments[:tag_start_as] @start_pattern = PatternBase.new( match: @start_pattern, tag_as: arguments[:tag_start_as], ) end tag_stop_as = arguments[:tag_end_as] || arguments[:tag_while_as] if tag_stop_as @stop_pattern = PatternBase.new( match: @stop_pattern, tag_as: tag_stop_as, ) end raise "match: is not supported in a PatternRange" if arguments[:match] @stop_type = arguments[:end_pattern] ? :end_pattern : :while_pattern arguments.delete(:start_pattern) arguments.delete(:end_pattern) arguments.delete(:while_pattern) # ensure that includes is either nil or a flat array if arguments[:includes] arguments[:includes] = [arguments[:includes]] unless arguments[:includes].is_a? Array arguments[:includes] = arguments[:includes].flatten end #canonize end_pattern_last arguments[:end_pattern_last] = arguments[:apply_end_pattern_last] if arguments[:apply_end_pattern_last] arguments[:end_pattern_last] = arguments[:applyEndPatternLast] if arguments[:applyEndPatternLast] arguments.delete(:apply_end_pattern_last) arguments.delete(:applyEndPatternLast) @arguments = arguments end |
Instance Attribute Details
#start_pattern ⇒ Object (readonly)
Returns the value of attribute start_pattern.
9 10 11 |
# File 'lib/ruby_grammar_builder/pattern_variations/pattern_range.rb', line 9 def start_pattern @start_pattern end |
Instance Method Details
#__deep_clone__ ⇒ PatternBase
Deeply clone self
97 98 99 100 101 102 103 104 105 106 |
# File 'lib/ruby_grammar_builder/pattern_variations/pattern_range.rb', line 97 def __deep_clone__ = @arguments.__deep_clone__ [:start_pattern] = @original_start_pattern.__deep_clone__ if @stop_type == :end_pattern [:end_pattern] = @original_stop_pattern.__deep_clone__ else [:while_pattern] = @original_stop_pattern.__deep_clone__ end self.class.new() end |
#do_evaluate_self(*_ignored) ⇒ void
This method returns an undefined value.
Raises an error to prevent use inside a pattern list
126 127 128 |
# File 'lib/ruby_grammar_builder/pattern_variations/pattern_range.rb', line 126 def do_evaluate_self(*_ignored) raise "PatternRange cannot be used as a part of a Pattern" end |
#evaluate(*_ignored) ⇒ void
This method returns an undefined value.
Raises an error to prevent use inside a pattern list
115 116 117 |
# File 'lib/ruby_grammar_builder/pattern_variations/pattern_range.rb', line 115 def evaluate(*_ignored) raise "PatternRange cannot be used as a part of a Pattern" end |
#inspect ⇒ String
Displays the Pattern for inspection
230 231 232 |
# File 'lib/ruby_grammar_builder/pattern_variations/pattern_range.rb', line 230 def inspect super.split(" ")[0] + " start_pattern:" + @start_pattern.inspect + ">" end |
#map!(map_includes = false) {|self| ... } ⇒ self
Uses a block to transform all Patterns in the list
196 197 198 199 200 201 202 203 204 |
# File 'lib/ruby_grammar_builder/pattern_variations/pattern_range.rb', line 196 def map!(map_includes = false, &block) yield self @start_pattern.map!(map_includes, &block) @stop_pattern.map!(map_includes, &block) map_includes!(&block) if map_includes self end |
#run_tests ⇒ Boolean
Runs the unit tests, recursively
221 222 223 224 225 |
# File 'lib/ruby_grammar_builder/pattern_variations/pattern_range.rb', line 221 def run_tests s = @start_pattern.run_tests e = @stop_pattern.run_tests s && e end |
#to_s ⇒ String
Displays this pattern range as source code that would generate it
174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 |
# File 'lib/ruby_grammar_builder/pattern_variations/pattern_range.rb', line 174 def to_s start_pattern = @original_start_pattern.to_s(2, true) stop_pattern = @original_stop_pattern.to_s(2, true) output = "PatternRange.new(" output += "\n start_pattern: " + start_pattern.lstrip output += ",\n #{@stop_type}: " + stop_pattern.lstrip [:tag_as, :tag_content_as, :tag_start_as, :tag_end_as, :tag_while_as].each do |tag| next if @arguments[tag].nil? output += ",\n #{tag}: \"" + @arguments[tag] + "\"" if @arguments[tag].is_a? String end output += ",\n includes: " + @arguments[:includes].to_s if @arguments[:includes] output += ",\n end_pattern_last: #{@arguments[:end_pattern_last]}" if @arguments[:end_pattern_last] output += ",\n)" output end |
#to_tag ⇒ Hash
Generate a Textmate rule from the PatternRange
135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 |
# File 'lib/ruby_grammar_builder/pattern_variations/pattern_range.rb', line 135 def to_tag match_key = { end_pattern: "end", while_pattern: "while" }[@stop_type] capture_key = { end_pattern: "endCaptures", while_pattern: "whileCaptures" }[@stop_type] start_groups = @start_pattern.collect_group_attributes stop_groups = @stop_pattern.collect_group_attributes output = { "begin" => @start_pattern.evaluate, # this is supposed to be start_groups as back references in end and while # refer to the start pattern match_key => @stop_pattern.evaluate(start_groups, fixup_refereces: true), "beginCaptures" => convert_group_attributes_to_captures(start_groups), capture_key => convert_group_attributes_to_captures(stop_groups), } output[:name] = @arguments[:tag_as] unless @arguments[:tag_as].nil? output[:contentName] = @arguments[:tag_content_as] unless @arguments[:tag_content_as].nil? output["begin"] = output["begin"][1..-2] if @start_pattern.optimize_outer_group? output[match_key] = output[match_key][1..-2] if @stop_pattern.optimize_outer_group? if @arguments[:includes].is_a? Array output[:patterns] = convert_includes_to_patterns(@arguments[:includes]) elsif !@arguments[:includes].nil? output[:patterns] = convert_includes_to_patterns([@arguments[:includes]]) end # end_pattern_last output["applyEndPatternLast"] = 1 if @arguments[:end_pattern_last] output end |
#transform_includes {|PatternBase, Symbol, Regexp, String| ... } ⇒ PatternBase
Uses block to recursively transform includes
209 210 211 212 213 214 215 216 |
# File 'lib/ruby_grammar_builder/pattern_variations/pattern_range.rb', line 209 def transform_includes(&block) copy = __deep_clone__ copy.arguments[:includes].map!(&block) if copy.arguments[:includes].is_a? Array copy.map!(true) do |s| s.arguments[:includes].map!(&block) if s.arguments[:includes].is_a? Array end.freeze end |