Skip to content
Permalink

Comparing changes

Choose two branches to see what’s changed or to start a new pull request. If you need to, you can also or learn more about diff comparisons.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also . Learn more about diff comparisons here.
base repository: jruby/jruby
Failed to load repositories. Confirm that selected base ref is valid, then try again.
Loading
base: 3e995ac7a317
Choose a base ref
...
head repository: jruby/jruby
Failed to load repositories. Confirm that selected head ref is valid, then try again.
Loading
compare: 0f9c9171945c
Choose a head ref
  • 2 commits
  • 1 file changed
  • 1 contributor

Commits on Nov 28, 2014

  1. Copy the full SHA
    fc7c0b1 View commit details
  2. Let RubyLexer.getInteger() return the right token for imaginary numbers.

    * Tokens.tINTEGER must still have the Node return type for FixnumNode vs BignumNode.
    eregon committed Nov 28, 2014
    Copy the full SHA
    0f9c917 View commit details
Showing with 48 additions and 53 deletions.
  1. +48 −53 core/src/main/java/org/jruby/lexer/yacc/RubyLexer.java
101 changes: 48 additions & 53 deletions core/src/main/java/org/jruby/lexer/yacc/RubyLexer.java
Original file line number Diff line number Diff line change
@@ -130,42 +130,6 @@ public Encoding getEncoding() {
return encoding;
}

private int considerComplex(int token, int suffix) {
if ((suffix & SUFFIX_I) == 0) {
return token;
} else {
yaccValue = newComplexNode((Node) yaccValue);
return RubyParser.tIMAGINARY;
}
}

private int getFloatToken(String number, int suffix) {
if ((suffix & SUFFIX_R) != 0) {
BigDecimal bd = new BigDecimal(number);
BigDecimal denominator = BigDecimal.ONE.scaleByPowerOfTen(bd.scale());
BigDecimal numerator = bd.multiply(denominator);

try {
yaccValue = new RationalNode(getPosition(), numerator.longValueExact(), denominator.longValueExact());
} catch (ArithmeticException ae) {
// FIXME: Rational supports Bignum numerator and denominator
throw new SyntaxException(PID.RATIONAL_OUT_OF_RANGE, getPosition(), getCurrentLine(), "Rational (" + numerator + "/" + denominator + ") out of range.");
}
return considerComplex(Tokens.tRATIONAL, suffix);
}

double d;
try {
d = SafeDoubleParser.parseDouble(number);
} catch (NumberFormatException e) {
warnings.warn(ID.FLOAT_OUT_OF_RANGE, getPosition(), "Float " + number + " out of range.");

d = number.startsWith("-") ? Double.NEGATIVE_INFINITY : Double.POSITIVE_INFINITY;
}
yaccValue = new FloatNode(getPosition(), d);
return considerComplex(Tokens.tFLOAT, suffix);
}

private BignumNode newBignumNode(String value, int radix) {
return new BignumNode(getPosition(), new BigInteger(value, radix));
}
@@ -561,9 +525,45 @@ private void determineExpressionState() {
break;
}
}

private Object getInteger(String value, int radix, int suffix) {
Node literalValue = null;

private int considerComplex(int token, int suffix) {
if ((suffix & SUFFIX_I) == 0) {
return token;
} else {
yaccValue = newComplexNode((Node) yaccValue);
return RubyParser.tIMAGINARY;
}
}

private int getFloatToken(String number, int suffix) {
if ((suffix & SUFFIX_R) != 0) {
BigDecimal bd = new BigDecimal(number);
BigDecimal denominator = BigDecimal.ONE.scaleByPowerOfTen(bd.scale());
BigDecimal numerator = bd.multiply(denominator);

try {
yaccValue = new RationalNode(getPosition(), numerator.longValueExact(), denominator.longValueExact());
} catch (ArithmeticException ae) {
// FIXME: Rational supports Bignum numerator and denominator
throw new SyntaxException(PID.RATIONAL_OUT_OF_RANGE, getPosition(), getCurrentLine(), "Rational (" + numerator + "/" + denominator + ") out of range.");
}
return considerComplex(Tokens.tRATIONAL, suffix);
}

double d;
try {
d = SafeDoubleParser.parseDouble(number);
} catch (NumberFormatException e) {
warnings.warn(ID.FLOAT_OUT_OF_RANGE, getPosition(), "Float " + number + " out of range.");

d = number.startsWith("-") ? Double.NEGATIVE_INFINITY : Double.POSITIVE_INFINITY;
}
yaccValue = new FloatNode(getPosition(), d);
return considerComplex(Tokens.tFLOAT, suffix);
}

private int getIntegerToken(String value, int radix, int suffix) {
Node literalValue;

if ((suffix & SUFFIX_R) != 0) {
literalValue = newRationalNode(value, radix);
@@ -574,8 +574,9 @@ private Object getInteger(String value, int radix, int suffix) {
literalValue = newBignumNode(value, radix);
}
}

return (suffix & SUFFIX_I) != 0 ? newComplexNode(literalValue) : literalValue;

yaccValue = literalValue;
return considerComplex(Tokens.tINTEGER, suffix);
}

/**
@@ -2342,8 +2343,7 @@ private int parseNumber(int c) throws IOException {
throw new SyntaxException(PID.TRAILING_UNDERSCORE_IN_NUMBER,
getPosition(), getCurrentLine(), "Trailing '_' in number.");
}
yaccValue = getInteger(tokenBuffer.toString(), 16, numberLiteralSuffix(SUFFIX_ALL));
return Tokens.tINTEGER;
return getIntegerToken(tokenBuffer.toString(), 16, numberLiteralSuffix(SUFFIX_ALL));
case 'b' :
case 'B' : // binary
c = src.read();
@@ -2369,8 +2369,7 @@ private int parseNumber(int c) throws IOException {
throw new SyntaxException(PID.TRAILING_UNDERSCORE_IN_NUMBER,
getPosition(), getCurrentLine(), "Trailing '_' in number.");
}
yaccValue = getInteger(tokenBuffer.toString(), 2, numberLiteralSuffix(SUFFIX_ALL));
return Tokens.tINTEGER;
return getIntegerToken(tokenBuffer.toString(), 2, numberLiteralSuffix(SUFFIX_ALL));
case 'd' :
case 'D' : // decimal
c = src.read();
@@ -2396,8 +2395,7 @@ private int parseNumber(int c) throws IOException {
throw new SyntaxException(PID.TRAILING_UNDERSCORE_IN_NUMBER, getPosition(),
getCurrentLine(), "Trailing '_' in number.");
}
yaccValue = getInteger(tokenBuffer.toString(), 10, numberLiteralSuffix(SUFFIX_ALL));
return Tokens.tINTEGER;
return getIntegerToken(tokenBuffer.toString(), 10, numberLiteralSuffix(SUFFIX_ALL));
case 'o':
case 'O':
c = src.read();
@@ -2423,8 +2421,7 @@ private int parseNumber(int c) throws IOException {
getPosition(), getCurrentLine(), "Trailing '_' in number.");
}

yaccValue = getInteger(tokenBuffer.toString(), 8, numberLiteralSuffix(SUFFIX_ALL));
return Tokens.tINTEGER;
return getIntegerToken(tokenBuffer.toString(), 8, numberLiteralSuffix(SUFFIX_ALL));
}
case '8' :
case '9' :
@@ -2477,8 +2474,7 @@ private int parseNumber(int c) throws IOException {
// Enebo: c can never be antrhign but '.'
// Why did I put this here?
} else {
yaccValue = getInteger(tokenBuffer.toString(), 10, numberLiteralSuffix(SUFFIX_ALL));
return Tokens.tINTEGER;
return getIntegerToken(tokenBuffer.toString(), 10, numberLiteralSuffix(SUFFIX_ALL));
}
} else {
tokenBuffer.append('.');
@@ -2532,8 +2528,7 @@ private int getNumberToken(String number, boolean seen_e, boolean seen_point, in
int suffix = numberLiteralSuffix(seen_e ? SUFFIX_I : SUFFIX_ALL);
return getFloatToken(number, suffix);
}
yaccValue = getInteger(number, 10, numberLiteralSuffix(SUFFIX_ALL));
return Tokens.tINTEGER;
return getIntegerToken(number, 10, numberLiteralSuffix(SUFFIX_ALL));
}

// Note: parser_tokadd_utf8 variant just for regexp literal parsing. This variant is to be