Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

#1059: Add support for floating numbers separator #1093

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion NOTICE
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ under the licensing terms detailed in LICENSE:
* jhwgh1968 <jhwgh1968@protonmail.com>
* Jeffrey Charles <jeffreycharles@gmail.com>
* Vladimir Tikhonov <reg@tikhonov.by>
* Nikolai Mavrenkov <koluch@koluch.ru>

Portions of this software are derived from third-party works licensed under
the following terms:
Expand All @@ -44,6 +45,6 @@ the following terms:
The 3-Clause BSD License (https://opensource.org/licenses/BSD-3-Clause)

* Arm Optimized Routines: https://github.com/ARM-software/optimized-routines

Copyright (c) Arm Limited
The MIT License (https://opensource.org/licenses/MIT)
19 changes: 13 additions & 6 deletions src/tokenizer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@ import {
isIdentifierPart,
isDecimalDigit,
isOctalDigit,
isKeywordCharacter
isKeywordCharacter,
isDecimalSeparator,
} from "./util";

/** Named token types. */
Expand Down Expand Up @@ -1499,16 +1500,20 @@ export class Tokenizer extends DiagnosticEmitter {
}

readDecimalFloat(): f64 {
// TODO: numeric separators (parseFloat can't handle these)
var start = this.pos;
var end = this.end;
var text = this.source.text;
while (this.pos < end && isDecimalDigit(text.charCodeAt(this.pos))) {

while (this.pos < end) {
let ch = text.charCodeAt(this.pos);
if (!(isDecimalDigit(ch) || isDecimalSeparator(ch))) break;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks like it would accept ___123___123___.___123___. Is that valid?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Any thoughts on this? :)

++this.pos;
}
if (this.pos < end && text.charCodeAt(this.pos) == CharCode.DOT) {
++this.pos;
while (this.pos < end && isDecimalDigit(text.charCodeAt(this.pos))) {
while (this.pos < end) {
let ch = text.charCodeAt(this.pos);
if (!(isDecimalDigit(ch) || isDecimalSeparator(ch))) break;
++this.pos;
}
}
Expand All @@ -1522,12 +1527,14 @@ export class Tokenizer extends DiagnosticEmitter {
) {
++this.pos;
}
while (this.pos < end && isDecimalDigit(text.charCodeAt(this.pos))) {
while (this.pos < end) {
let ch = text.charCodeAt(this.pos);
if (!(isDecimalDigit(ch) || isDecimalSeparator(ch))) break;
++this.pos;
}
}
}
return parseFloat(text.substring(start, this.pos));
return parseFloat(text.substring(start, this.pos).replace(/_/g, ''));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also we try to avoid RegExp

}

readHexFloat(): f64 {
Expand Down
6 changes: 5 additions & 1 deletion src/util/charcode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,11 @@ export function isWhiteSpace(c: i32): bool {

/** Tests if the specified character code is a valid decimal digit. */
export function isDecimalDigit(c: i32): bool {
return c >= CharCode._0 && c <= CharCode._9;
return c >= CharCode._0 && c <= CharCode._9 ;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: unnecessary space between semicolon and _9

}

export function isDecimalSeparator(c: i32): bool {
return c === CharCode._;
}

/** Tests if the specified character code is a valid octal digit. */
Expand Down
1 change: 1 addition & 0 deletions tests/parser/literals.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,3 +60,4 @@
"1\"23";
"1\"2\\3";
"\0\n\\n\r";
1000_000.1234_1234;
1 change: 1 addition & 0 deletions tests/parser/literals.ts.fixture.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,3 +60,4 @@
"1\"23";
"1\"2\\3";
"\0\n\\n\r";
1000000.12341234;