Flowdock
identify_number() public

No documentation

This method has no description. You can help the Ruby community by adding new notes.

Hide source
# File lib/irb/ruby-lex.rb, line 949
  def identify_number
    @lex_state = EXPR_END

    if peek(0) == "0" && peek(1) !~ /[.eE]/
      getc
      case peek(0)
      when /[xX]/
        ch = getc
        match = /[0-9a-fA-F_]/
      when /[bB]/
        ch = getc
        match = /[01_]/
      when /[oO]/
        ch = getc
        match = /[0-7_]/
      when /[dD]/
        ch = getc
        match = /[0-9_]/
      when /[0-7]/
        match = /[0-7_]/
      when /[89]/
        RubyLex.fail SyntaxError, "Illegal octal digit"
      else 
        return Token(TkINTEGER)
      end
      
      len0 = true
      non_digit = false
      while ch = getc
        if match =~ ch
          if ch == "_"
            if non_digit
              RubyLex.fail SyntaxError, "trailing `#{ch}' in number"
            else
              non_digit = ch
            end
          else
            non_digit = false
            len0 = false
          end
        else
          ungetc
          if len0
            RubyLex.fail SyntaxError, "numeric literal without digits"
          end
          if non_digit
            RubyLex.fail SyntaxError, "trailing `#{non_digit}' in number"
          end
          break
        end
      end
      return Token(TkINTEGER)
    end
    
    type = TkINTEGER
    allow_point = true
    allow_e = true
    non_digit = false
    while ch = getc
      case ch
      when /[0-9]/
        non_digit = false
      when "_"
        non_digit = ch
      when allow_point && "."
        if non_digit
          RubyLex.fail SyntaxError, "trailing `#{non_digit}' in number"
        end
        type = TkFLOAT
        if peek(0) !~ /[0-9]/
          type = TkINTEGER
          ungetc
          break
        end
        allow_point = false
      when allow_e && "e", allow_e && "E"
        if non_digit
          RubyLex.fail SyntaxError, "trailing `#{non_digit}' in number"
        end
        type = TkFLOAT
        if peek(0) =~ /[+-]/
          getc
        end
        allow_e = false
        allow_point = false
        non_digit = ch
      else
        if non_digit
          RubyLex.fail SyntaxError, "trailing `#{non_digit}' in number"
        end
        ungetc
        break
      end
    end
    Token(type)
  end
Register or log in to add new notes.