Sha256: 2b14ae34b802057aa16cb7a15712c4e2e3d467bc9aa9a5dd97371247381e9952

Contents?: true

Size: 1.24 KB

Versions: 16

Compression:

Stored size: 1.24 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; end
    class Branch    < Regexp::Expression::Sequence; end

    class Expression < Regexp::Expression::Subexpression
      def initialize(token)
        super(token)

        @condition = nil
        @branches  = []
      end

      def condition(exp = nil)
        return @condition unless exp
        @condition = exp
        @expressions << exp
      end

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

      def branch(exp = nil)
        raise TooManyBranches.new if @branches.length == 2

        sequence = Branch.new(level, set_level, conditional_level + 1)

        @expressions << sequence
        @branches << @expressions.last
      end

      def branches
        @branches
      end

      def quantify(token, text, min = nil, max = nil, mode = :greedy)
        branches.last.last.quantify(token, text, min, max, mode)
      end

      def to_s
        s = @text.dup
        s << @condition.text
        s << branches.map{|e| e.to_s}.join('|')
        s << ')'
      end
    end
  end

end

Version data entries

16 entries across 16 versions & 1 rubygems

Version Path
regexp_parser-0.4.6 lib/regexp_parser/expression/classes/conditional.rb
regexp_parser-0.4.5 lib/regexp_parser/expression/classes/conditional.rb
regexp_parser-0.4.4 lib/regexp_parser/expression/classes/conditional.rb
regexp_parser-0.4.3 lib/regexp_parser/expression/classes/conditional.rb
regexp_parser-0.4.2 lib/regexp_parser/expression/classes/conditional.rb
regexp_parser-0.4.1 lib/regexp_parser/expression/classes/conditional.rb
regexp_parser-0.4.0 lib/regexp_parser/expression/classes/conditional.rb
regexp_parser-0.3.6 lib/regexp_parser/expression/classes/conditional.rb
regexp_parser-0.3.5 lib/regexp_parser/expression/classes/conditional.rb
regexp_parser-0.3.4 lib/regexp_parser/expression/classes/conditional.rb
regexp_parser-0.3.3 lib/regexp_parser/expression/classes/conditional.rb
regexp_parser-0.3.2 lib/regexp_parser/expression/classes/conditional.rb
regexp_parser-0.3.1 lib/regexp_parser/expression/classes/conditional.rb
regexp_parser-0.3.0 lib/regexp_parser/expression/classes/conditional.rb
regexp_parser-0.2.1 lib/regexp_parser/expression/classes/conditional.rb
regexp_parser-0.2.0 lib/regexp_parser/expression/classes/conditional.rb