Class: FixRepeatedTagAs

Inherits:
GrammarTransform show all
Defined in:
lib/ruby_grammar_builder/transforms/fix_repeated_tag_as.rb

Overview

tag_as: inside a quantifier does not work as expected, this fixes it

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from GrammarTransform

#post_transform

Class Method Details

.display_options(indent, options) ⇒ String

Displays the state of the options

Returns:

  • (String)

    the options as a string



70
71
72
# File 'lib/ruby_grammar_builder/transforms/fix_repeated_tag_as.rb', line 70

def self.display_options(indent, options)
    ",\n#{indent}preserve_references?: #{options[:preserve_references?]}"
end

.optionsArray<Symbol>

Contributes the option :preserve_references?

:preserve_references? disables the scrambling of references

Returns:

  • (Array<Symbol>)

    a list of symbols that represent keys that can be read by the plugin



61
62
63
# File 'lib/ruby_grammar_builder/transforms/fix_repeated_tag_as.rb', line 61

def self.options
    [:preserve_references?]
end

Instance Method Details

#pre_transform(pattern, options) ⇒ Object

fixes tag_as when it is inside a quantifier see github.com/jeff-hykin/cpp-textmate-grammar/issues/339#issuecomment-543285390 for an explanation of why and how



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/ruby_grammar_builder/transforms/fix_repeated_tag_as.rb', line 29

def pre_transform(pattern, options)
    return pattern.map { |v| pre_transform(v, options) } if pattern.is_a? Array
    return pattern unless pattern.is_a? PatternBase
    return pattern if pattern.is_a? PatternRange

    pattern.map do |pat|
        next pat unless pat.respond_to? :self_capture_group_rematch
        next pat unless pat.self_capture_group_rematch
        next pat unless tag_as?(pat.match)

        unless pat.arguments[:includes].nil? || pat.arguments[:includes].empty?
            raise "Cannot transform a Repeated pattern that has non empty includes"
        end

        pat.arguments[:includes] = [pat.match.__deep_clone__]
        pat.match.map! do |pm|
            pm.arguments.delete(:tag_as)
            pm.arguments.delete(:includes)
            next unless options[:preserve_references?] || pm.arguments[:preserve_references?]

            pm.self_scramble_references
        end
    end
end

#tag_as?(pattern) ⇒ Boolean

Does pattern or any of its children / siblings have a tag_as

Parameters:

Returns:

  • (Boolean)

    if any of the patterns have a tag_as



14
15
16
17
18
19
20
21
22
# File 'lib/ruby_grammar_builder/transforms/fix_repeated_tag_as.rb', line 14

def tag_as?(pattern)
    return false unless pattern.is_a? PatternBase

    pattern.each do |s|
        return true if s.arguments[:tag_as]
    end

    false
end