Sha256: c9ff188b4e0d2442dd0334598727b40a5c7bb841658bc6407614394949afd24c

Contents?: true

Size: 1.46 KB

Versions: 35

Compression:

Stored size: 1.46 KB

Contents

module Regexp::Expression
  module Conditional
    class TooManyBranches < StandardError
      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
    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
    end
  end
end

Version data entries

35 entries across 28 versions & 6 rubygems

Version Path
grape-extra_validators-2.0.0 vendor/bundle/ruby/2.6.0/gems/regexp_parser-1.7.1/lib/regexp_parser/expression/classes/conditional.rb
tdiary-5.1.3 vendor/bundle/ruby/2.7.0/gems/regexp_parser-1.7.1/lib/regexp_parser/expression/classes/conditional.rb
tdiary-5.1.3 vendor/bundle/ruby/2.6.0/gems/regexp_parser-1.7.0/lib/regexp_parser/expression/classes/conditional.rb
files.com-1.0.1 vendor/bundle/ruby/2.5.0/gems/regexp_parser-1.7.1/lib/regexp_parser/expression/classes/conditional.rb
regexp_parser-1.7.1 lib/regexp_parser/expression/classes/conditional.rb
tdiary-5.1.2 vendor/bundle/ruby/2.7.0/gems/regexp_parser-1.7.0/lib/regexp_parser/expression/classes/conditional.rb
tdiary-5.1.2 vendor/bundle/ruby/2.6.0/gems/regexp_parser-1.6.0/lib/regexp_parser/expression/classes/conditional.rb
tdiary-5.1.1 vendor/bundle/ruby/2.6.0/gems/regexp_parser-1.6.0/lib/regexp_parser/expression/classes/conditional.rb
tdiary-5.1.1 vendor/bundle/ruby/2.7.0/gems/regexp_parser-1.7.0/lib/regexp_parser/expression/classes/conditional.rb
regexp_parser-1.7.0 lib/regexp_parser/expression/classes/conditional.rb
tdiary-5.1.0 vendor/bundle/gems/regexp_parser-1.6.0/lib/regexp_parser/expression/classes/conditional.rb
chatops-rpc-0.0.2 fixtures/chatops-controller-example/vendor/bundle/ruby/2.5.0/gems/regexp_parser-1.6.0/lib/regexp_parser/expression/classes/conditional.rb
chatops-rpc-0.0.1 fixtures/chatops-controller-example/vendor/bundle/ruby/2.5.0/gems/regexp_parser-1.6.0/lib/regexp_parser/expression/classes/conditional.rb
regexp_parser-1.6.0 lib/regexp_parser/expression/classes/conditional.rb
regexp_parser-1.5.1 lib/regexp_parser/expression/classes/conditional.rb