Class: PatternRange

Inherits:
PatternBase show all
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

Attributes inherited from PatternBase

#arguments, #match, #next_pattern, #original_arguments

Instance Method Summary collapse

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

Note:

exactly one of :end_pattern or :while_pattern is required

Creates a new PatternRange

Plugins may add additional options

Parameters:

  • arguments (Hash)

    options

Options Hash (arguments):

  • :start_pattern (PatternBase, Regexp, String)

    the start pattern

  • :end_pattern (PatternBase, Regexp, String)

    the end pattern

  • :while_pattern (PatternBase, Regexp, String)

    the while pattern

  • :tag_as (String)

    the tag for this pattern

  • :tag_contents_as (String)

    the tag for contents of this pattern

  • :tag_start_as (String)

    the tag for the start pattern

  • :tag_end_as (String)

    the tag for the end pattern

  • :tag_while_as (String)

    the tag for the continuation pattern



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_patternObject (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

Returns:



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__
    options = @arguments.__deep_clone__
    options[:start_pattern] = @original_start_pattern.__deep_clone__
    if @stop_type == :end_pattern
        options[:end_pattern] = @original_stop_pattern.__deep_clone__
    else
        options[:while_pattern] = @original_stop_pattern.__deep_clone__
    end
    self.class.new(options)
end

#do_evaluate_self(*_ignored) ⇒ void

This method returns an undefined value.

Raises an error to prevent use inside a pattern list

Parameters:

  • _ignored

    ignored



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

Parameters:

  • _ignored

    ignored



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

#inspectString

Displays the Pattern for inspection

Returns:

  • (String)

    A representation of the pattern



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

Parameters:

  • map_includes (Boolean) (defaults to: false)

    should include patterns be mapped?

Yields:

  • (self)

    invokes the block with self for modification

Returns:

  • (self)


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_testsBoolean

Runs the unit tests, recursively

Returns:

  • (Boolean)

    If all test passed return true, otherwise false



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_sString

Displays this pattern range as source code that would generate it

Returns:

  • (String)

    The PatternRange as source code



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_tagHash

Generate a Textmate rule from the PatternRange

Returns:

  • (Hash)

    The Textmate rule



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

Yields:

  • (PatternBase, Symbol, Regexp, String)

    invokes the block with each include to transform

Returns:

  • (PatternBase)

    a copy of self with transformed 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