Sha256: 26b6a70178de1d8d692acc4b5755ec31e7b61b1ecc932c33c66653a26f2d2af4

Contents?: true

Size: 1.68 KB

Versions: 13

Compression:

Stored size: 1.68 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 parts
        [text.dup, condition, *intersperse(branches, '|'), ')']
      end

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

Version data entries

13 entries across 13 versions & 4 rubygems

Version Path
cm-admin-1.5.22 vendor/bundle/ruby/3.3.0/gems/regexp_parser-2.6.1/lib/regexp_parser/expression/classes/conditional.rb
cm-admin-1.5.21 vendor/bundle/ruby/3.3.0/gems/regexp_parser-2.6.1/lib/regexp_parser/expression/classes/conditional.rb
cm-admin-1.5.20 vendor/bundle/ruby/3.3.0/gems/regexp_parser-2.6.1/lib/regexp_parser/expression/classes/conditional.rb
scrapbook-0.3.2 vendor/ruby/2.7.0/gems/regexp_parser-2.5.0/lib/regexp_parser/expression/classes/conditional.rb
scrapbook-0.3.1 vendor/ruby/2.7.0/gems/regexp_parser-2.5.0/lib/regexp_parser/expression/classes/conditional.rb
regexp_parser-2.6.2 lib/regexp_parser/expression/classes/conditional.rb
tdiary-5.2.4 vendor/bundle/ruby/3.1.0/gems/regexp_parser-2.5.0/lib/regexp_parser/expression/classes/conditional.rb
regexp_parser-2.6.1 lib/regexp_parser/expression/classes/conditional.rb
regexp_parser-2.6.0 lib/regexp_parser/expression/classes/conditional.rb
tdiary-5.2.3 vendor/bundle/ruby/3.1.0/gems/regexp_parser-2.5.0/lib/regexp_parser/expression/classes/conditional.rb
tdiary-5.2.2 vendor/bundle/ruby/3.1.0/gems/regexp_parser-2.5.0/lib/regexp_parser/expression/classes/conditional.rb
regexp_parser-2.5.0 lib/regexp_parser/expression/classes/conditional.rb
regexp_parser-2.4.0 lib/regexp_parser/expression/classes/conditional.rb