Sha256: 99b94c867a3039a863fe914ee151f70cceb351186f244f94415d7d273076665f

Contents?: true

Size: 1.96 KB

Versions: 4

Compression:

Stored size: 1.96 KB

Contents

# frozen_string_literal: true

module Mutant
  class AST
    module Regexp
      class Transformer
        # Transformer for option groups
        class OptionsGroup < self
          register :regexp_options_group
          register :regexp_options_switch_group

          # Mapper from `Regexp::Expression` to `Parser::AST::Node`
          class ExpressionToAST < Transformer::ExpressionToAST

            # Transform options group into node
            #
            # @return [Parser::AST::Node]
            def call
              quantify(ast(expression.option_changes, *children))
            end
          end # ExpressionToAST

          # Mapper from `Parser::AST::Node` to `Regexp::Expression`
          class ASTToExpression < Transformer::ASTToExpression
            include NamedChildren

            children :option_changes

          private

            def transform
              options_group.tap do |expression|
                expression.expressions = subexpressions
              end
            end

            def subexpressions
              remaining_children.map(&Regexp.public_method(:to_expression))
            end

            def options_group
              ::Regexp::Expression::Group::Options.new(
                ::Regexp::Token.new(:group, type, text)
              )
            end

            def type
              {
                regexp_options_group:        :options,
                regexp_options_switch_group: :options_switch
              }.fetch(node.type)
            end

            def text
              pos, neg = option_changes.partition { |_opt, val| val }.map do |arr|
                arr.map(&:first).join
              end
              neg_opt_sep = '-' unless neg.empty?
              content_sep = ':' unless type.equal?(:options_switch)

              "(?#{pos}#{neg_opt_sep}#{neg}#{content_sep}"
            end
          end # ASTToExpression
        end # OptionsGroup
      end # Transformer
    end # Regexp
  end # AST
end # Mutant

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
mutant-0.11.17 lib/mutant/ast/regexp/transformer/options_group.rb
mutant-0.11.16 lib/mutant/ast/regexp/transformer/options_group.rb
mutant-0.11.15 lib/mutant/ast/regexp/transformer/options_group.rb
mutant-0.11.14 lib/mutant/ast/regexp/transformer/options_group.rb