Skip to content

improve errors around around dots #4176

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 2 commits into from
Sep 4, 2024
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
12 changes: 10 additions & 2 deletions src/language/__tests__/lexer-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,13 @@ describe('Lexer', () => {
});
});

it('reports unexpected characters', () => {
expectSyntaxError('.').to.deep.equal({
message: 'Syntax Error: Unexpected character: ".".',
locations: [{ line: 1, column: 1 }],
});
});

it('errors respect whitespace', () => {
let caughtError;
try {
Expand Down Expand Up @@ -852,7 +859,8 @@ describe('Lexer', () => {
});

expectSyntaxError('.123').to.deep.equal({
message: 'Syntax Error: Unexpected character: ".".',
message:
'Syntax Error: Invalid number, expected digit before ".", did you mean "0.123"?',
locations: [{ line: 1, column: 1 }],
});

Expand Down Expand Up @@ -1030,7 +1038,7 @@ describe('Lexer', () => {

it('lex reports useful unknown character error', () => {
expectSyntaxError('..').to.deep.equal({
message: 'Syntax Error: Unexpected character: ".".',
message: 'Syntax Error: Unexpected "..", did you mean "..."?',
locations: [{ line: 1, column: 1 }],
});

Expand Down
27 changes: 22 additions & 5 deletions src/language/lexer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -258,14 +258,31 @@ function readNextToken(lexer: Lexer, start: number): Token {
return createToken(lexer, TokenKind.PAREN_L, position, position + 1);
case 0x0029: // )
return createToken(lexer, TokenKind.PAREN_R, position, position + 1);
case 0x002e: // .
if (
body.charCodeAt(position + 1) === 0x002e &&
body.charCodeAt(position + 2) === 0x002e
) {
case 0x002e: {
// .
const nextCode = body.charCodeAt(position + 1);
if (nextCode === 0x002e && body.charCodeAt(position + 2) === 0x002e) {
return createToken(lexer, TokenKind.SPREAD, position, position + 3);
}
if (nextCode === 0x002e) {
throw syntaxError(
lexer.source,
position,
'Unexpected "..", did you mean "..."?',
);
} else if (isDigit(nextCode)) {
const digits = lexer.source.body.slice(
position + 1,
readDigits(lexer, position + 1, nextCode),
);
throw syntaxError(
lexer.source,
position,
`Invalid number, expected digit before ".", did you mean "0.${digits}"?`,
);
}
break;
}
case 0x003a: // :
return createToken(lexer, TokenKind.COLON, position, position + 1);
case 0x003d: // =
Expand Down
Loading