Skip to content

Commit ff7f201

Browse files
committed
Throw error when unable to tokenize the string
1 parent d2915e0 commit ff7f201

File tree

2 files changed

+14
-3
lines changed

2 files changed

+14
-3
lines changed

src/core/Tokenizer.ts

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,9 @@ export default class Tokenizer {
128128
if (input.length) {
129129
// Get the next token and the token type
130130
token = this.getNextToken(input, token);
131+
if (!token) {
132+
throw new Error(`Parse error: Unexpected "${input.slice(0, 100)}"`);
133+
}
131134
// Advance the string
132135
input = input.substring(token.value.length);
133136

@@ -154,8 +157,9 @@ export default class Tokenizer {
154157
});
155158

156159
/** Attempts to match next token from input string, tests RegExp patterns in decreasing priority */
157-
getNextToken(input: string, previousToken?: Token) {
158-
return (this.matchToken(TokenType.LINE_COMMENT)(input) ||
160+
getNextToken(input: string, previousToken?: Token): Token | undefined {
161+
return (
162+
this.matchToken(TokenType.LINE_COMMENT)(input) ||
159163
this.matchToken(TokenType.BLOCK_COMMENT)(input) ||
160164
this.matchToken(TokenType.STRING)(input) ||
161165
this.matchToken(TokenType.BLOCK_START)(input) ||
@@ -164,7 +168,8 @@ export default class Tokenizer {
164168
this.matchToken(TokenType.NUMBER)(input) ||
165169
this.getReservedWordToken(input, previousToken) ||
166170
this.matchToken(TokenType.WORD)(input) ||
167-
this.matchToken(TokenType.OPERATOR)(input)) as Token;
171+
this.matchToken(TokenType.OPERATOR)(input)
172+
);
168173
}
169174

170175
/**

test/sqlFormatter.test.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,10 @@ describe('sqlFormatter', () => {
66
format('SELECT *', { language: 'blah' as SqlLanguage });
77
}).toThrow('Unsupported SQL dialect: blah');
88
});
9+
10+
it('throws error when encountering unsupported characters', () => {
11+
expect(() => {
12+
format('SELECT «weird-stuff»');
13+
}).toThrow('Parse error: Unexpected "«weird-stuff»"');
14+
});
915
});

0 commit comments

Comments
 (0)