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
plaid-14.13.0 vendor/bundle/ruby/3.0.0/gems/regexp_parser-1.8.0/lib/regexp_parser/expression/classes/conditional.rb
plaid-14.12.1 vendor/bundle/ruby/3.0.0/gems/regexp_parser-1.8.0/lib/regexp_parser/expression/classes/conditional.rb
plaid-14.12.0 vendor/bundle/ruby/2.6.0/gems/regexp_parser-1.8.0/lib/regexp_parser/expression/classes/conditional.rb
plaid-14.11.1 vendor/bundle/ruby/2.6.0/gems/regexp_parser-1.8.0/lib/regexp_parser/expression/classes/conditional.rb
plaid-14.10.0 vendor/bundle/ruby/2.6.0/gems/regexp_parser-1.8.0/lib/regexp_parser/expression/classes/conditional.rb
plaid-14.7.0 vendor/bundle/ruby/2.6.0/gems/regexp_parser-1.8.0/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/3.0.0/gems/regexp_parser-2.0.3/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/3.0.0/gems/tdiary-5.1.4/vendor/bundle/ruby/2.7.0/gems/regexp_parser-1.8.2/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/3.0.0/gems/regexp_parser-1.8.2/lib/regexp_parser/expression/classes/conditional.rb
tdiary-5.1.5 vendor/bundle/ruby/3.0.0/gems/tdiary-5.1.4/vendor/bundle/ruby/2.7.0/gems/regexp_parser-1.8.2/lib/regexp_parser/expression/classes/conditional.rb
tdiary-5.1.5 vendor/bundle/ruby/3.0.0/gems/regexp_parser-1.8.2/lib/regexp_parser/expression/classes/conditional.rb
tdiary-5.1.5 vendor/bundle/ruby/3.0.0/gems/regexp_parser-2.0.3/lib/regexp_parser/expression/classes/conditional.rb
regexp_parser-2.0.3 lib/regexp_parser/expression/classes/conditional.rb
regexp_parser-2.0.2 lib/regexp_parser/expression/classes/conditional.rb
regexp_parser-2.0.1 lib/regexp_parser/expression/classes/conditional.rb
tdiary-5.1.4 vendor/bundle/ruby/2.7.0/gems/regexp_parser-1.8.2/lib/regexp_parser/expression/classes/conditional.rb
regexp_parser-2.0.0 lib/regexp_parser/expression/classes/conditional.rb
regexp_parser-1.8.2 lib/regexp_parser/expression/classes/conditional.rb
regexp_parser-1.8.1 lib/regexp_parser/expression/classes/conditional.rb
regexp_parser-1.8.0 lib/regexp_parser/expression/classes/conditional.rb