Skip to content

Commit

Permalink
JS: revert ignoring error when ident after number to be on the safe side
Browse files Browse the repository at this point in the history
  • Loading branch information
tdewolff committed Jan 12, 2024
1 parent 5691480 commit 4b768d6
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 8 deletions.
8 changes: 7 additions & 1 deletion js/lex.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,9 @@ func (l *Lexer) Next() (TokenType, []byte) {
prevLineTerminator := l.prevLineTerminator
l.prevLineTerminator = false

prevNumericLiteral := l.prevNumericLiteral
l.prevNumericLiteral = false

// study on 50x jQuery shows:
// spaces: 20k
// alpha: 16k
Expand Down Expand Up @@ -190,7 +193,10 @@ func (l *Lexer) Next() (TokenType, []byte) {
}
default:
if l.consumeIdentifierToken() {
if keyword, ok := Keywords[string(l.r.Lexeme())]; ok {
if prevNumericLiteral {
l.err = parse.NewErrorLexer(l.r, "unexpected identifier after number")
return ErrorToken, nil
} else if keyword, ok := Keywords[string(l.r.Lexeme())]; ok {
return keyword, l.r.Shift()
}
return IdentifierToken, l.r.Shift()
Expand Down
11 changes: 5 additions & 6 deletions 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{IntegerToken, DecimalToken, IntegerToken, OctalToken, BinaryToken, HexadecimalToken}},
{"0o22 0b11", TTs{OctalToken, BinaryToken}},
{"0n 2345n 0o5n 0b1n 0x5n 435.333n", TTs{IntegerToken, IntegerToken, OctalToken, BinaryToken, HexadecimalToken, DecimalToken, IdentifierToken}},
{"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 Expand Up @@ -90,13 +90,12 @@ func TestTokens(t *testing.T) {
{"`template\\\x00`return", TTs{TemplateToken, ReturnToken}},

// numbers
{"0xg", TTs{IntegerToken, IdentifierToken}},
{"0.f", TTs{DecimalToken, IdentifierToken}},
{"0bg", TTs{IntegerToken, IdentifierToken}},
{"0og", TTs{IntegerToken, IdentifierToken}},
{"0xg", TTs{IntegerToken, ErrorToken}},
{"0bg", TTs{IntegerToken, ErrorToken}},
{"0og", TTs{IntegerToken, ErrorToken}},
{"010", TTs{ErrorToken}}, // Decimal(0) Decimal(10) Identifier(xF)
{"50e+-0", TTs{ErrorToken}},
{"5.a", TTs{DecimalToken, IdentifierToken}},
{"5.a", TTs{DecimalToken, ErrorToken}},
{"5..a", TTs{DecimalToken, DotToken, IdentifierToken}},

// coverage
Expand Down
2 changes: 1 addition & 1 deletion js/parse_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -442,7 +442,7 @@ func TestParseError(t *testing.T) {
js string
err string
}{
{"5a", "unexpected a in expression"},
{"5a", "unexpected identifier after number"},
{"{a", "unexpected EOF"},
{"if", "expected ( instead of EOF in if statement"},
{"if(a", "expected ) instead of EOF in if statement"},
Expand Down

0 comments on commit 4b768d6

Please sign in to comment.