Skip to content

Commit

Permalink
Fix parsing of negative 0x, 0b, 0o long literals
Browse files Browse the repository at this point in the history
  • Loading branch information
wendigo committed Dec 31, 2024
1 parent bb80ff8 commit e7557fe
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -107,12 +107,21 @@ private static long parse(String value)
if (value.startsWith("0x") || value.startsWith("0X")) {
return Long.parseLong(value.substring(2), 16);
}
else if (value.startsWith("-0x") || value.startsWith("-0X")) {
return Long.parseLong("-" + value.substring(3), 16);
}
else if (value.startsWith("0b") || value.startsWith("0B")) {
return Long.parseLong(value.substring(2), 2);
}
else if (value.startsWith("-0b") || value.startsWith("-0B")) {
return Long.parseLong("-" + value.substring(3), 2);
}
else if (value.startsWith("0o") || value.startsWith("0O")) {
return Long.parseLong(value.substring(2), 8);
}
else if (value.startsWith("-0o") || value.startsWith("-0O")) {
return Long.parseLong("-" + value.substring(3), 8);
}
else {
return Long.parseLong(value);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -486,6 +486,14 @@ public void testNumbers()
.isEqualTo(new LongLiteral(new NodeLocation(1, 1), "0X123_ABC_DEF"))
.satisfies(value -> assertThat(((LongLiteral) value).getParsedValue()).isEqualTo(4893429231L));

assertThat(expression("-0x123_abc_def"))
.isEqualTo(new LongLiteral(new NodeLocation(1, 1), "-0x123_abc_def"))
.satisfies(value -> assertThat(((LongLiteral) value).getParsedValue()).isEqualTo(-4893429231L));

assertThat(expression("-0X123_ABC_DEF"))
.isEqualTo(new LongLiteral(new NodeLocation(1, 1), "-0X123_ABC_DEF"))
.satisfies(value -> assertThat(((LongLiteral) value).getParsedValue()).isEqualTo(-4893429231L));

assertThatThrownBy(() -> SQL_PARSER.createExpression("0x123_ABC_DEF_"))
.isInstanceOf(ParsingException.class);

Expand All @@ -497,6 +505,14 @@ public void testNumbers()
.isEqualTo(new LongLiteral(new NodeLocation(1, 1), "0o012_345"))
.satisfies(value -> assertThat(((LongLiteral) value).getParsedValue()).isEqualTo(5349L));

assertThat(expression("-0O012_345"))
.isEqualTo(new LongLiteral(new NodeLocation(1, 1), "-0O012_345"))
.satisfies(value -> assertThat(((LongLiteral) value).getParsedValue()).isEqualTo(-5349L));

assertThat(expression("-0o012_345"))
.isEqualTo(new LongLiteral(new NodeLocation(1, 1), "-0o012_345"))
.satisfies(value -> assertThat(((LongLiteral) value).getParsedValue()).isEqualTo(-5349L));

assertThatThrownBy(() -> SQL_PARSER.createExpression("0o012_345_"))
.isInstanceOf(ParsingException.class);

Expand All @@ -508,6 +524,14 @@ public void testNumbers()
.isEqualTo(new LongLiteral(new NodeLocation(1, 1), "0b110_010"))
.satisfies(value -> assertThat(((LongLiteral) value).getParsedValue()).isEqualTo(50L));

assertThat(expression("-0B110_010"))
.isEqualTo(new LongLiteral(new NodeLocation(1, 1), "-0B110_010"))
.satisfies(value -> assertThat(((LongLiteral) value).getParsedValue()).isEqualTo(-50L));

assertThat(expression("-0b110_010"))
.isEqualTo(new LongLiteral(new NodeLocation(1, 1), "-0b110_010"))
.satisfies(value -> assertThat(((LongLiteral) value).getParsedValue()).isEqualTo(-50L));

assertThatThrownBy(() -> SQL_PARSER.createExpression("0b110_010_"))
.isInstanceOf(ParsingException.class);
}
Expand Down

0 comments on commit e7557fe

Please sign in to comment.