FileDocCategorySizeDatePackage
NumberParser.javaAPI DocphoneME MR2 API (J2ME)10430Wed May 02 18:00:36 BST 2007com.sun.perseus.parser

NumberParser

public class NumberParser extends AbstractParser
This class represents a parser with support for numbers. Note that the parameter-less form of the parseNumber methods is meant for use by subclasses (e.g., TransformListParser).
version
$Id: NumberParser.java,v 1.2 2006/04/21 06:40:35 st125089 Exp $

Fields Summary
private static final float[]
POW_10
Array of powers of ten.
Constructors Summary
Methods Summary
public static floatbuildFloat(int mant, int exp)
Computes a float from mantissa and exponent.

param
mant the mantissa for the floating point value to create
param
exp the exponent for the floating point value to create
return
a single precision floating point value corresponding to the input mantissa/exponent pair.

        if (exp < -125 || mant == 0) {
            return 0f;
        }

        if (exp >  128) {
            if (mant > 0) {
                return Float.POSITIVE_INFINITY;
            } else {
                return Float.NEGATIVE_INFINITY;
            }
        }

        if (exp == 0) {
            return mant;
        }
          
        if (mant >= 1 << 26) {
            mant++;  // round up trailing bits if they will be dropped.
        }

        if (exp > 0) {
            return mant * POW_10[exp];
        } else {
            return mant / POW_10[-exp];
        }
    
public floatparseNumber(java.lang.String numberString)
Parses the content of the input String and converts it to a float.

param
numberString the value to parse
return
the corresponding single precision floating point value.

        setString(numberString);
        return parseNumber(true);
    
public floatparseNumber()
Parses a float value from the current position in the string

return
floating point value corresponding to the parsed string.

        return parseNumber(false);
    
public floatparseNumber(boolean eos)
Parses the next float value in the string.

param
eos If eos is set to true, then there should be no more characters at the end of the string.
return
floating point value corresponding to the parsed string. An IllegalArgumentException is thrown if the next number in the string does not have a valid number syntax or if eos is true and there are more characters in the string after the number.

        int     mant     = 0;
        int     mantDig  = 0;
        boolean mantPos  = true;
        boolean mantRead = false;

        int     exp      = 0;
        int     expDig   = 0;
        int     expAdj   = 0;
        boolean expPos   = true;

        // Only read the next character if the
        // current one is -1
        if (current == -1) {
            current  = read();
        }

        // Parse the initial +/- sign if any
        switch (current) {
        case '-":
            mantPos = false;
        case '+":
            current = read();
        default:
            // nothing
        }

        // Now, parse the mantisse
        m1: switch (current) {
        default:
            throw new IllegalArgumentException("" + (char) current);

        case '.":
            break;

        case '0":
            mantRead = true;
            l: for (;;) {
                current = read();
                switch (current) {
                case '1": case '2": case '3": case '4":
                case '5": case '6": case '7": case '8": case '9":
                    break l;
                case '.": case 'e": case 'E":
                    break m1;
                case -1:
                    break m1;
                default:
                    if (eos) {
                        throw new IllegalArgumentException
                            (">" + (char) current + "<");
                    } else {
                        return 0;
                    }
                case '0": // <!>
                }
            }

        case '1": case '2": case '3": case '4":
        case '5": case '6": case '7": case '8": case '9":
            mantRead = true;
            l: for (;;) {
                if (mantDig < 9) {
                    mantDig++;
                    mant = mant * 10 + (current - '0");
                } else {
                    expAdj++;
                }
                current = read();
                switch (current) {
                default:
                    break l;
                case '0": case '1": case '2": case '3": case '4":
                case '5": case '6": case '7": case '8": case '9":
                }              
            }
        }
      
        // If we hit a point, parse the fractional part
        if (current == '.") {
            current = read();
            m2: switch (current) {
            default:
            case 'e": case 'E":
                if (!mantRead) {
                    throw new IllegalArgumentException();
                }
                break;

            case '0":
                if (mantDig == 0) {
                    l: for (;;) {
                        current = read();
                        expAdj--;
                        switch (current) {
                        case '1": case '2": case '3": case '4":
                        case '5": case '6": case '7": case '8": case '9":
                            break l;
                        default:
                            break m2;
                        case '0":
                        }
                    }
                }
            case '1": case '2": case '3": case '4":
            case '5": case '6": case '7": case '8": case '9":
                l: for (;;) {
                    if (mantDig < 9) {
                        mantDig++;
                        mant = mant * 10 + (current - '0");
                        expAdj--;
                    }
                    current = read();
                    switch (current) {
                    default:
                        break l;
                    case '0": case '1": case '2": case '3": case '4":
                    case '5": case '6": case '7": case '8": case '9":
                    }
                }
            }
        }

        // Parse the exponent
        switch (current) {
        case 'e": case 'E":
            current = read();
            switch (current) {
            default:
                throw new IllegalArgumentException();
            case '-":
                expPos = false;
            case '+":
                current = read();
                switch (current) {
                default:
                    throw new IllegalArgumentException();
                case '0": case '1": case '2": case '3": case '4":
                case '5": case '6": case '7": case '8": case '9":
                }
            case '0": case '1": case '2": case '3": case '4":
            case '5": case '6": case '7": case '8": case '9":
            }
          
            en: switch (current) {
            case '0":
                l: for (;;) {
                    current = read();
                    switch (current) {
                    case '1": case '2": case '3": case '4":
                    case '5": case '6": case '7": case '8": case '9":
                        break l;
                    default:
                        break en;
                    case '0":
                    }
                }

            case '1": case '2": case '3": case '4":
            case '5": case '6": case '7": case '8": case '9":
                l: for (;;) {
                    if (expDig < 3) {
                        expDig++;
                        exp = exp * 10 + (current - '0");
                    }
                    current = read();
                    switch (current) {
                    default:
                        break l;
                    case '0": case '1": case '2": case '3": case '4":
                    case '5": case '6": case '7": case '8": case '9":
                    }
                }
            }
        default:
        }

        if (eos && current != -1) {
            throw new IllegalArgumentException();
        }

        if (!expPos) {
            exp = -exp;
        }
        exp += expAdj;
        if (!mantPos) {
            mant = -mant;
        }

        return buildFloat(mant, exp);