Sha256: 173f0a6972d311c6ca7e1f66ab8bdcccb96afa02a7cf05be53b28a2fcc3477f1

Contents?: true

Size: 1.71 KB

Versions: 23

Compression:

Stored size: 1.71 KB

Contents

module Regexp::Expression
  module Conditional
    class TooManyBranches < Regexp::Parser::Error
      def initialize
        super('The conditional expression has more than 2 branches')
      end
    end

    class Condition < Regexp::Expression::Base
      attr_accessor :referenced_expression

      # Name or number of the referenced capturing group that determines state.
      # Returns a String if reference is by name, Integer if by number.
      def reference
        ref = text.tr("'<>()", "")
        ref =~ /\D/ ? ref : Integer(ref)
      end

      def initialize_copy(orig)
        self.referenced_expression = orig.referenced_expression.dup
        super
      end
    end

    class Branch < Regexp::Expression::Sequence; end

    class Expression < Regexp::Expression::Subexpression
      attr_accessor :referenced_expression

      def <<(exp)
        expressions.last << exp
      end

      def add_sequence(active_opts = {})
        raise TooManyBranches.new if branches.length == 2
        params = { conditional_level: conditional_level + 1 }
        Branch.add_to(self, params, active_opts)
      end
      alias :branch :add_sequence

      def condition=(exp)
        expressions.delete(condition)
        expressions.unshift(exp)
      end

      def condition
        find { |subexp| subexp.is_a?(Condition) }
      end

      def branches
        select { |subexp| subexp.is_a?(Sequence) }
      end

      def reference
        condition.reference
      end

      def to_s(format = :full)
        "#{text}#{condition}#{branches.join('|')})#{quantifier_affix(format)}"
      end

      def initialize_copy(orig)
        self.referenced_expression = orig.referenced_expression.dup
        super
      end
    end
  end
end

Version data entries

23 entries across 19 versions & 8 rubygems

Version Path
scrapbook-0.3.2 vendor/ruby/2.7.0/gems/regexp_parser-2.2.1/lib/regexp_parser/expression/classes/conditional.rb
scrapbook-0.3.1 vendor/ruby/2.7.0/gems/regexp_parser-2.2.1/lib/regexp_parser/expression/classes/conditional.rb
regexp_parser-2.3.1 lib/regexp_parser/expression/classes/conditional.rb
regexp_parser-2.3.0 lib/regexp_parser/expression/classes/conditional.rb
op_connect-0.1.2 vendor/bundle/ruby/3.1.0/gems/regexp_parser-2.2.1/lib/regexp_parser/expression/classes/conditional.rb
tdiary-5.2.1 vendor/bundle/ruby/3.1.0/gems/regexp_parser-2.2.1/lib/regexp_parser/expression/classes/conditional.rb
regexp_parser-2.2.1 lib/regexp_parser/expression/classes/conditional.rb
phillipug-foodie-0.1.0 .vendor/ruby/3.0.0/gems/regexp_parser-2.2.0/lib/regexp_parser/expression/classes/conditional.rb
regexp_parser-2.2.0 lib/regexp_parser/expression/classes/conditional.rb
tdiary-5.2.0 vendor/bundle/ruby/3.0.0/gems/regexp_parser-2.1.1/lib/regexp_parser/expression/classes/conditional.rb
tdiary-5.2.0 vendor/bundle/ruby/2.7.0/gems/regexp_parser-2.1.1/lib/regexp_parser/expression/classes/conditional.rb
date_n_time_picker_activeadmin-0.1.2 vendor/bundle/ruby/2.6.0/gems/regexp_parser-2.1.1/lib/regexp_parser/expression/classes/conditional.rb
date_n_time_picker_activeadmin-0.1.1 vendor/bundle/ruby/2.6.0/gems/regexp_parser-2.1.1/lib/regexp_parser/expression/classes/conditional.rb
tdiary-5.1.7 vendor/bundle/ruby/2.7.0/gems/regexp_parser-2.1.1/lib/regexp_parser/expression/classes/conditional.rb
tdiary-5.1.7 vendor/bundle/ruby/3.0.0/gems/regexp_parser-2.1.1/lib/regexp_parser/expression/classes/conditional.rb
rails_mini_profiler-0.2.0 vendor/bundle/ruby/3.0.0/gems/regexp_parser-2.1.1/lib/regexp_parser/expression/classes/conditional.rb
tdiary-5.1.6 vendor/bundle/ruby/3.0.0/gems/regexp_parser-2.1.1/lib/regexp_parser/expression/classes/conditional.rb
tdiary-5.1.6 vendor/bundle/ruby/2.7.0/gems/regexp_parser-2.1.1/lib/regexp_parser/expression/classes/conditional.rb
tdiary-5.1.6 vendor/bundle/ruby/2.7.0/gems/tdiary-5.1.5/vendor/bundle/ruby/2.7.0/gems/regexp_parser-2.1.1/lib/regexp_parser/expression/classes/conditional.rb
cocRb-0.1.0 .bundle/ruby/3.0.0/gems/regexp_parser-2.1.1/lib/regexp_parser/expression/classes/conditional.rb