src/sys/dotnet/fanx/serial/Tokenizer.cs
/// <summary>
/// Parse a number literal token.
/// </summary>
Parse a number literal token.
Parse a number literal token .
private int number(bool neg)
    {
      if (cur == '0' && peek == 'x')
        return hex();
      StringBuilder s = null;
      long whole = 0;
      int wholeCount = 0;
      while (curt == DIGIT)
      {
        if (s != null)
        {
          s.Append((char)cur);
        }
        else
        {
          whole = whole*10 + (cur - '0');
          wholeCount++;
          if (wholeCount >= 18) { s = new StringBuilder(32); if (neg) s.Append('-'); s.Append(whole); }
        }
        consume();
        if (cur == '_') consume();
      }
      bool floating = false;
      if (cur == '.' && peekt == DIGIT)
      {
        floating = true;
        if (s == null) { s = new StringBuilder(32); if (neg) s.Append('-'); s.Append(whole); }
        s.Append('.');
        consume();
        while (curt == DIGIT)
        {
          s.Append((char)cur);
          consume();
          if (cur == '_') consume();
        }
      }
      if (cur == 'e' || cur == 'E')
      {
        floating = true;
        if (s == null) { s = new StringBuilder(32); if (neg) s.Append('-'); s.Append(whole); }
        s.Append('e');
        consume();
        if (cur == '-' || cur == '+') { s.Append((char)cur); consume(); }
        if (curt != DIGIT) throw err("Expected exponent digits");
        while (curt == DIGIT)
        {
          s.Append((char)cur);
          consume();
          if (cur == '_') consume();
        }
      }
      if (neg) whole = -whole;
      bool floatSuffix  = false;
      bool decimalSuffix = false;
      long dur = -1;
      if ('d' <= cur && cur <= 's')
      {
        if (cur == 'n' && peek == 's') { consume(); consume(); dur = 1L; }
        if (cur == 'm' && peek == 's') { consume(); consume(); dur = 1000000L; }
        if (cur == 's' && peek == 'e') { consume(); consume(); if (cur != 'c') throw err("Expected 'sec' in Duration literal"); consume(); dur = 1000000000L; }
        if (cur == 'm' && peek == 'i') { consume(); consume(); if (cur != 'n') throw err("Expected 'min' in Duration literal"); consume(); dur = 60000000000L; }
        if (cur == 'h' && peek == 'r') { consume(); consume(); dur = 3600000000000L; }
        if (cur == 'd' && peek == 'a') { consume(); consume(); if (cur != 'y') throw err("Expected 'day' in Duration literal"); consume(); dur = 86400000000000L; }
      }
      if (cur == 'f' || cur == 'F')
      {
        consume();
        floatSuffix = true;
      }
      else if (cur == 'd' || cur == 'D')
      {
        consume();
        decimalSuffix = true;
      }
      try
      {
        if (floatSuffix)
        {
          if (s == null)
            this.m_val = Double.valueOf((double)whole);
          else
            this.m_val = Double.valueOf(s.ToString());
          return Token.FLOAT_LITERAL;
        }
        if (decimalSuffix || floating)
        {
          BigDecimal dnum = (s == null) ?
            BigDecimal.valueOf(whole) :
            BigDecimal.valueOf(s.ToString());
          if (dur > 0)
          {
            this.m_val = Duration.make(dnum.multiply(BigDecimal.valueOf(dur)).longValue());
            return Token.DURATION_LITERAL;
          }
          else
          {
            this.m_val = dnum;
            return Token.DECIMAL_LITERAL;
          }
        }
        long num = (s == null) ? whole : BigDecimal.valueOf(s.ToString()).longValue();
        if (dur > 0)
        {
          this.m_val = Duration.make(num*dur);
          return Token.DURATION_LITERAL;
        }
        else
        {
          this.m_val = Long.valueOf(num);
          return Token.INT_LITERAL;
        }
      }
      catch (System.Exception)
      {
        throw err("Invalid numeric literal: " + s);
      }
    }
private int number ( bool neg ) { if ( cur == ' ' && peek == ' ' ) return hex ( ) ; StringBuilder s = null ; long whole = 0 ; int wholeCount = 0 ; while ( curt == DIGIT ) { if ( s != null ) { s . Append ( ( char ) cur ) ; } else { whole = whole * 10 + ( cur - ' ' ) ; wholeCount ++ ; if ( wholeCount >= 18 ) { s = new StringBuilder ( 32 ) ; if ( neg ) s . Append ( ' ' ) ; s . Append ( whole ) ; } } consume ( ) ; if ( cur == ' ' ) consume ( ) ; } bool floating = false ; if ( cur == ' ' && peekt == DIGIT ) { floating = true ; if ( s == null ) { s = new StringBuilder ( 32 ) ; if ( neg ) s . Append ( ' ' ) ; s . Append ( whole ) ; } s . Append ( ' ' ) ; consume ( ) ; while ( curt == DIGIT ) { s . Append ( ( char ) cur ) ; consume ( ) ; if ( cur == ' ' ) consume ( ) ; } } if ( cur == ' ' || cur == ' ' ) { floating = true ; if ( s == null ) { s = new StringBuilder ( 32 ) ; if ( neg ) s . Append ( ' ' ) ; s . Append ( whole ) ; } s . Append ( ' ' ) ; consume ( ) ; if ( cur == ' ' || cur == ' ' ) { s . Append ( ( char ) cur ) ; consume ( ) ; } if ( curt != DIGIT ) throw err ( " " ) ; while ( curt == DIGIT ) { s . Append ( ( char ) cur ) ; consume ( ) ; if ( cur == ' ' ) consume ( ) ; } } if ( neg ) whole = - whole ; bool floatSuffix = false ; bool decimalSuffix = false ; long dur = - 1 ; if ( ' ' <= cur && cur <= ' ' ) { if ( cur == ' ' && peek == ' ' ) { consume ( ) ; consume ( ) ; dur = 1L ; } if ( cur == ' ' && peek == ' ' ) { consume ( ) ; consume ( ) ; dur = 1000000L ; } if ( cur == ' ' && peek == ' ' ) { consume ( ) ; consume ( ) ; if ( cur != ' ' ) throw err ( " " ) ; consume ( ) ; dur = 1000000000L ; } if ( cur == ' ' && peek == ' ' ) { consume ( ) ; consume ( ) ; if ( cur != ' ' ) throw err ( " " ) ; consume ( ) ; dur = 60000000000L ; } if ( cur == ' ' && peek == ' ' ) { consume ( ) ; consume ( ) ; dur = 3600000000000L ; } if ( cur == ' ' && peek == ' ' ) { consume ( ) ; consume ( ) ; if ( cur != ' ' ) throw err ( " " ) ; consume ( ) ; dur = 86400000000000L ; } } if ( cur == ' ' || cur == ' ' ) { consume ( ) ; floatSuffix = true ; } else if ( cur == ' ' || cur == ' ' ) { consume ( ) ; decimalSuffix = true ; } try { if ( floatSuffix ) { if ( s == null ) this . m_val = Double . valueOf ( ( double ) whole ) ; else this . m_val = Double . valueOf ( s . ToString ( ) ) ; return Token . FLOAT_LITERAL ; } if ( decimalSuffix || floating ) { BigDecimal dnum = ( s == null ) ? BigDecimal . valueOf ( whole ) : BigDecimal . valueOf ( s . ToString ( ) ) ; if ( dur > 0 ) { this . m_val = Duration . make ( dnum . multiply ( BigDecimal . valueOf ( dur ) ) . longValue ( ) ) ; return Token . DURATION_LITERAL ; } else { this . m_val = dnum ; return Token . DECIMAL_LITERAL ; } } long num = ( s == null ) ? whole : BigDecimal . valueOf ( s . ToString ( ) ) . longValue ( ) ; if ( dur > 0 ) { this . m_val = Duration . make ( num * dur ) ; return Token . DURATION_LITERAL ; } else { this . m_val = Long . valueOf ( num ) ; return Token . INT_LITERAL ; } } catch ( System . Exception ) { throw err ( " " + s ) ; } }
Parse a number literal token.
Parse a number literal token .
// check for hex value // read whole part // fraction part // exponent // check for suffixes // float literal // decimal literal (or duration) // int literal (or duration)
neg
bool