module Regexp::Lexer

def self.lex(input, syntax = "ruby/#{RUBY_VERSION}", &block)

def self.lex(input, syntax = "ruby/#{RUBY_VERSION}", &block)
  syntax = Regexp::Syntax.new(syntax)
  @tokens = []
  @nesting, @set_nesting, @conditional_nesting = 0, 0, 0
  last = nil
  Regexp::Scanner.scan(input) do |type, token, text, ts, te|
    type, token = *syntax.normalize(type, token)
    syntax.check! type, token
    ascend(type, token)
    break_literal(last) if type == :quantifier and
      last and last.type == :literal
    current = Regexp::Token.new(type, token, text, ts, te,
              @nesting, @set_nesting, @conditional_nesting)
    current = merge_literal(current) if type == :literal and
      last and last.type == :literal
    current = merge_condition(current) if type == :conditional and
      [:condition, :condition_close].include?(token)
    last.next(current) if last
    current.previous(last) if last
    @tokens << current
    last = current
    descend(type, token)
  end
  if block_given?
    @tokens.map {|t| block.call(t)}
  else
    @tokens
  end
end