shevek / jcpp

The C Preprocessor as a Java library
http://www.anarres.org/projects/jcpp/
Apache License 2.0
106 stars 36 forks source link

False float number parsing #3

Closed yuriy-chumak closed 10 years ago

yuriy-chumak commented 11 years ago

Float number "0.1" parsed as three tokens: octal numeric "0", dot ".", numeric "0" intead of float numeric "0.1".

yuriy-chumak commented 11 years ago

And one more: token.getText() for "1." number returns "1" instead of "1." or "1.0".

risingPhil commented 11 years ago

I'm not sure if this is the same issue, but I encountered float problems as well.

Consider the following string: ANGLE = 0.6108652381980154;

After preprocessing, my lexer receives the following string: ANGLE = 06108652381980154;

After spending the whole morning researching this issue, I came up with the following fix.

One of the problems is that you consider every value that starts with a zero as either an octal int or a hexadecimal value (and not a float). I fixed this by changing the part at line 826 in LexerSource.cpp (the function token) into this:

        case '0':
            /* decimal, octal or hex */
            d = read();
            if (d == 'x' || d == 'X')
                tok = number_hex((char)d);
            else if(d == '.')
            {
                unread(d);
                unread(c);
                tok = number_decimal();
            }
            else{
                unread(d);
                tok = number_octal();
            }
            break;

Secondly, you forgot to add a '.' to your stringbuilder with the name "text" in the function number_decimal. This is the fixed function:

private Token number_decimal()
                    throws IOException,
                            LexerException {
    StringBuilder   text = new StringBuilder();
    String          integer = _number_part(text, 10);
    NumericValue    value = new NumericValue(10, integer);
    int             d = read();
    if (d == '.') {
        text.append('.');
        String      fraction = _number_part(text, 10);
        value.setFractionalPart(fraction);
        d = read();
    }
    if (d == 'E' || d == 'e') {
        String      exponent = _number_part(text, 10);
        value.setExponent(exponent);
        d = read();
    }
    // XXX Make sure it's got enough parts
    return _number_suffix(text, value, d);
}
shevek commented 10 years ago

Same bug existed for exponents. Fixed in github, will make 1.4.1. Thank you.