class FixRepeatedTagAs

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

Public Class Methods

display_options(indent, options) click to toggle source

Displays the state of the options

@return (see GrammarPlugin.display_options)

# 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
options() click to toggle source

Contributes the option :preserve_references?

:preserve_references? disables the scrambling of references

@return (see GrammarPlugin.options)

# File lib/ruby_grammar_builder/transforms/fix_repeated_tag_as.rb, line 61
def self.options
    [:preserve_references?]
end

Public Instance Methods

pre_transform(pattern, options) click to toggle source

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

# 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) click to toggle source

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

@param [PatternBase, String] pattern the pattern to check

@return [Boolean] if any of the patterns have a tag_as

# 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