Sha256: 55f0af81b07468efc222ddc4dabdcdabb9317b9e8b8356f51d9d45b1ef78d67d

Contents?: true

Size: 1.24 KB

Versions: 7

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, options = {})
        super

        @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

7 entries across 7 versions & 1 rubygems

Version Path
regexp_parser-0.4.13 lib/regexp_parser/expression/classes/conditional.rb
regexp_parser-0.4.12 lib/regexp_parser/expression/classes/conditional.rb
regexp_parser-0.4.11 lib/regexp_parser/expression/classes/conditional.rb
regexp_parser-0.4.10 lib/regexp_parser/expression/classes/conditional.rb
regexp_parser-0.4.9 lib/regexp_parser/expression/classes/conditional.rb
regexp_parser-0.4.8 lib/regexp_parser/expression/classes/conditional.rb
regexp_parser-0.4.7 lib/regexp_parser/expression/classes/conditional.rb