Skip to content

RFC: Number lexer lookahead restriction #2164

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

Merged
merged 1 commit into from
Sep 15, 2019
Merged
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
35 changes: 35 additions & 0 deletions src/language/__tests__/lexer-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -708,6 +708,41 @@ describe('Lexer', () => {
);
});

it('lex does not allow name-start after a number', () => {
expectSyntaxError('0xF1', 'Invalid number, expected digit but got: "x".', {
line: 1,
column: 2,
});
expectSyntaxError('0b10', 'Invalid number, expected digit but got: "b".', {
line: 1,
column: 2,
});
expectSyntaxError(
'123abc',
'Invalid number, expected digit but got: "a".',
{ line: 1, column: 4 },
);
expectSyntaxError('1_234', 'Invalid number, expected digit but got: "_".', {
line: 1,
column: 2,
});
expect(() => lexSecond('1ß')).to.throw(
'Syntax Error: Cannot parse the unexpected character "\\u00DF".',
);
expectSyntaxError('1.23f', 'Invalid number, expected digit but got: "f".', {
line: 1,
column: 5,
});
expectSyntaxError(
'1.234_5',
'Invalid number, expected digit but got: "_".',
{ line: 1, column: 6 },
);
expect(() => lexSecond('1.2ß')).to.throw(
'Syntax Error: Cannot parse the unexpected character "\\u00DF".',
);
});

it('lexes punctuation', () => {
expect(lexOne('!')).to.contain({
kind: TokenKind.BANG,
Expand Down
11 changes: 9 additions & 2 deletions src/language/lexer.js
Original file line number Diff line number Diff line change
Expand Up @@ -447,8 +447,8 @@ function readNumber(source, start, firstCode, line, col, prev): Token {
code = body.charCodeAt(position);
}

// Numbers cannot be followed by . or e
if (code === 46 || code === 69 || code === 101) {
// Numbers cannot be followed by . or NameStart
if (code === 46 || isNameStart(code)) {
throw syntaxError(
source,
position,
Expand Down Expand Up @@ -738,3 +738,10 @@ function readName(source, start, line, col, prev): Token {
body.slice(start, position),
);
}

// _ A-Z a-z
function isNameStart(code): boolean {
return (
code === 95 || (code >= 65 && code <= 90) || (code >= 97 && code <= 122)
);
}