Skip to content

Commit

Permalink
RFC: Number lexer lookahead restriction
Browse files Browse the repository at this point in the history
Implements and adds the tests described by graphql/graphql-spec#601
  • Loading branch information
leebyron committed Sep 12, 2019
1 parent c68acd8 commit ee06056
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 2 deletions.
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)
);
}

0 comments on commit ee06056

Please sign in to comment.