-
-
Notifications
You must be signed in to change notification settings - Fork 74
BigInt & numeric separator support #204
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
Changes from 1 commit
a1784dc
0a57267
18307e2
2fbe694
3cf3080
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,16 @@ | ||
| using System.Numerics; | ||
|
|
||
| namespace Esprima.Ast | ||
| { | ||
| public sealed class BigIntLiteral : Literal | ||
| { | ||
| public readonly string BigInt; | ||
|
|
||
| public BigInteger? BigIntValue => (BigInteger?) Value; | ||
|
|
||
| public BigIntLiteral(BigInteger value, string raw) : base(TokenType.BigIntLiteral, value, raw) | ||
| { | ||
| BigInt = raw; | ||
| } | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,6 +1,7 @@ | ||
| using System; | ||
| using System.Collections.Generic; | ||
| using System.Globalization; | ||
| using System.Numerics; | ||
| using System.Runtime.CompilerServices; | ||
| using System.Text; | ||
| using System.Text.RegularExpressions; | ||
|
|
@@ -888,19 +889,7 @@ static string SafeSubstring(string s, int startIndex, int length) | |
|
|
||
| public Token ScanHexLiteral(int start) | ||
| { | ||
| var index = Index; | ||
|
|
||
| while (!Eof()) | ||
| { | ||
| if (!Character.IsHexDigit(Source.CharCodeAt(Index))) | ||
| { | ||
| break; | ||
| } | ||
|
|
||
| Index++; | ||
| } | ||
|
|
||
| var number = Source.Substring(index, Index - index); | ||
| var number = this.ScanLiteralPart(Character.IsHexDigit); | ||
|
|
||
| if (number.Length == 0) | ||
| { | ||
|
|
@@ -957,23 +946,10 @@ public Token ScanHexLiteral(int start) | |
| } | ||
|
|
||
| public Token ScanBinaryLiteral(int start) | ||
| { | ||
| { | ||
| char ch; | ||
| var index = Index; | ||
|
|
||
| while (!Eof()) | ||
| { | ||
| ch = Source[Index]; | ||
| if (ch != '0' && ch != '1') | ||
| { | ||
| break; | ||
| } | ||
|
|
||
| Index++; | ||
| } | ||
|
|
||
| var number = Source.Substring(index, Index - index); | ||
|
|
||
| var number = this.ScanLiteralPart(c => c == '0' || c == '1'); | ||
|
|
||
| if (number.Length == 0) | ||
| { | ||
| // only 0b or 0B | ||
|
|
@@ -1015,18 +991,9 @@ public Token ScanOctalLiteral(char prefix, int start) | |
| else | ||
| { | ||
| ++Index; | ||
| } | ||
|
|
||
| while (!Eof()) | ||
| { | ||
| if (!Character.IsOctalDigit(Source.CharCodeAt(Index))) | ||
| { | ||
| break; | ||
| } | ||
|
|
||
| sb.Append(Source[Index++]); | ||
| } | ||
|
|
||
| } | ||
|
|
||
| sb.Append(this.ScanLiteralPart(Character.IsOctalDigit)); | ||
| var number = sb.ToString(); | ||
|
|
||
| if (!octal && number.Length == 0) | ||
|
|
@@ -1084,6 +1051,27 @@ public bool IsImplicitOctalLiteral() | |
| return true; | ||
| } | ||
|
|
||
| private string ScanLiteralPart(Func<char, bool> check) | ||
| { | ||
| string num = ""; | ||
jogibear9988 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| if (Source.CharCodeAt(Index) == '_') | ||
jogibear9988 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| ThrowUnexpectedToken(Messages.NumericSeperatorNotAllowedHere); | ||
|
|
||
| while (!Eof() && (check(Source.CharCodeAt(Index)) || Source.CharCodeAt(Index) == '_')) { | ||
|
||
| if (Source.CharCodeAt(Index) != '_') | ||
| num += Source.CharCodeAt(Index); | ||
| Index++; | ||
| if (Source.CharCodeAt(Index-1) == '_' && Source.CharCodeAt(Index) == '_') | ||
| ThrowUnexpectedToken(Messages.NumericSeperatorOneUnderscore); | ||
| } | ||
|
|
||
| if (Source.CharCodeAt(Index-1) == '_') | ||
| ThrowUnexpectedToken(Messages.NumericSeperatorNotAllowedHere); | ||
|
|
||
| return num; | ||
| } | ||
|
|
||
| public Token ScanNumericLiteral() | ||
| { | ||
| var sb = GetStringBuilder(); | ||
|
|
@@ -1095,7 +1083,6 @@ public Token ScanNumericLiteral() | |
| if (ch != '.') | ||
| { | ||
| var first = Source[Index++]; | ||
| sb.Append(first); | ||
| ch = Source.CharCodeAt(Index); | ||
|
|
||
| // Hex number starts with '0x'. | ||
|
|
@@ -1130,21 +1117,15 @@ public Token ScanNumericLiteral() | |
| } | ||
| } | ||
|
|
||
| while (Character.IsDecimalDigit(Source.CharCodeAt(Index))) | ||
| { | ||
| sb.Append(Source[Index++]); | ||
| } | ||
|
|
||
| --Index; | ||
| sb.Append(this.ScanLiteralPart(Character.IsDecimalDigit)); | ||
| ch = Source.CharCodeAt(Index); | ||
| } | ||
|
|
||
| if (ch == '.') | ||
| { | ||
| sb.Append(Source[Index++]); | ||
| while (Character.IsDecimalDigit(Source.CharCodeAt(Index))) | ||
| { | ||
| sb.Append(Source[Index++]); | ||
| } | ||
| sb.Append(Source[Index++]); | ||
| sb.Append(this.ScanLiteralPart(Character.IsDecimalDigit)); | ||
|
|
||
| ch = Source.CharCodeAt(Index); | ||
| } | ||
|
|
@@ -1161,15 +1142,27 @@ public Token ScanNumericLiteral() | |
|
|
||
| if (Character.IsDecimalDigit(Source.CharCodeAt(Index))) | ||
| { | ||
| while (Character.IsDecimalDigit(Source.CharCodeAt(Index))) | ||
| { | ||
| sb.Append(Source[Index++]); | ||
| } | ||
| sb.Append(this.ScanLiteralPart(Character.IsDecimalDigit)); | ||
| } | ||
| else | ||
| { | ||
| ThrowUnexpectedToken(); | ||
| } | ||
| } | ||
| else if (ch == 'n') | ||
| { | ||
| Index++; | ||
| var bigInt = BigInteger.Parse(sb.ToString()); | ||
| return new Token | ||
| { | ||
| Type = TokenType.BigIntLiteral, | ||
| Value = bigInt, | ||
| BigIntValue = bigInt, | ||
| LineNumber = LineNumber, | ||
| LineStart = LineStart, | ||
| Start = start, | ||
| End = Index | ||
| }; | ||
| } | ||
|
|
||
| if (Character.IsIdentifierStart(Source.CharCodeAt(Index))) | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
do we really need to add this as Literal already has that, I think it should be quite cheap to call even with the type check
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I did it to match the Tree spec: https://github.com/estree/estree/blob/master/es2020.md#bigintliteral
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Cause I think the AST should be nearly the same in all Javascript Parsers