Skip to content

Commit

Permalink
JS: support parsing binary/octal/hexadecimal integers with big int su…
Browse files Browse the repository at this point in the history
…ffix, fixes #116
  • Loading branch information
tdewolff committed Jan 11, 2024
1 parent 64705f8 commit 3ced090
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 1 deletion.
9 changes: 9 additions & 0 deletions js/lex.go
Original file line number Diff line number Diff line change
Expand Up @@ -538,6 +538,9 @@ func (l *Lexer) consumeNumericToken() TokenType {
if l.consumeHexDigit() {
for l.consumeHexDigit() || l.consumeNumericSeparator(l.consumeHexDigit) {
}
if l.r.Peek(0) == 'n' {
l.r.Move(1)
}
return HexadecimalToken
}
l.err = parse.NewErrorLexer(l.r, "invalid hexadecimal number")
Expand All @@ -547,6 +550,9 @@ func (l *Lexer) consumeNumericToken() TokenType {
if l.consumeBinaryDigit() {
for l.consumeBinaryDigit() || l.consumeNumericSeparator(l.consumeBinaryDigit) {
}
if l.r.Peek(0) == 'n' {
l.r.Move(1)
}
return BinaryToken
}
l.err = parse.NewErrorLexer(l.r, "invalid binary number")
Expand All @@ -556,6 +562,9 @@ func (l *Lexer) consumeNumericToken() TokenType {
if l.consumeOctalDigit() {
for l.consumeOctalDigit() || l.consumeNumericSeparator(l.consumeOctalDigit) {
}
if l.r.Peek(0) == 'n' {
l.r.Move(1)
}
return OctalToken
}
l.err = parse.NewErrorLexer(l.r, "invalid octal number")
Expand Down
2 changes: 1 addition & 1 deletion js/lex_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ func TestTokens(t *testing.T) {
{"5.2 .04 1. 2.e3 0x0F 5e99", TTs{DecimalToken, DecimalToken, DecimalToken, DecimalToken, HexadecimalToken, DecimalToken}},
{"2_3 5_4.1_2 1_1n 0o2_3 0b1_1 0xF_F", TTs{DecimalToken, DecimalToken, BigIntToken, OctalToken, BinaryToken, HexadecimalToken}},
{"0o22 0b11", TTs{OctalToken, BinaryToken}},
{"0n 2345n 435.333n", TTs{BigIntToken, BigIntToken, DecimalToken, ErrorToken}},
{"0n 2345n 0o5n 0b1n 0x5n 435.333n", TTs{IntegerToken, IntegerToken, OctalToken, BinaryToken, HexadecimalToken, DecimalToken, ErrorToken}},
{"a = 'string'", TTs{IdentifierToken, EqToken, StringToken}},
{"/*comment*/ //comment", TTs{CommentToken, CommentToken}},
{"{ } ( ) [ ]", TTs{OpenBraceToken, CloseBraceToken, OpenParenToken, CloseParenToken, OpenBracketToken, CloseBracketToken}},
Expand Down

0 comments on commit 3ced090

Please sign in to comment.