mirror of
https://github.com/odoo/owl.git
synced 2025-10-06 19:59:41 +07:00
[IMP] compiler: improve error message for tokenization errors
This commit is contained in:
committed by
Géry Debongnie
parent
e4fdd32f22
commit
14d2328c88
@@ -199,24 +199,30 @@ const TOKENIZERS = [
|
||||
export function tokenize(expr: string): Token[] {
|
||||
const result: Token[] = [];
|
||||
let token: boolean | Token = true;
|
||||
let error: any;
|
||||
let current = expr;
|
||||
|
||||
while (token) {
|
||||
expr = expr.trim();
|
||||
if (expr) {
|
||||
for (let tokenizer of TOKENIZERS) {
|
||||
token = tokenizer(expr);
|
||||
if (token) {
|
||||
result.push(token);
|
||||
expr = expr.slice(token.size || token.value.length);
|
||||
break;
|
||||
try {
|
||||
while (token) {
|
||||
current = current.trim();
|
||||
if (current) {
|
||||
for (let tokenizer of TOKENIZERS) {
|
||||
token = tokenizer(current);
|
||||
if (token) {
|
||||
result.push(token);
|
||||
current = current.slice(token.size || token.value.length);
|
||||
break;
|
||||
}
|
||||
}
|
||||
} else {
|
||||
token = false;
|
||||
}
|
||||
} else {
|
||||
token = false;
|
||||
}
|
||||
} catch (e) {
|
||||
error = e; // Silence all errors and throw a generic error below
|
||||
}
|
||||
if (expr.length) {
|
||||
throw new Error(`Tokenizer error: could not tokenize "${expr}"`);
|
||||
if (current.length || error) {
|
||||
throw new Error(`Tokenizer error: could not tokenize \`${expr}\``);
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
@@ -65,14 +65,14 @@ describe("tokenizer", () => {
|
||||
});
|
||||
|
||||
test("strings", () => {
|
||||
expect(() => tokenize("'")).toThrow("Invalid expression");
|
||||
expect(() => tokenize("'\\")).toThrow("Invalid expression");
|
||||
expect(() => tokenize("'\\'")).toThrow("Invalid expression");
|
||||
expect(() => tokenize("'")).toThrow("Tokenizer error: could not tokenize `'`");
|
||||
expect(() => tokenize("'\\")).toThrow("Tokenizer error: could not tokenize `'\\`");
|
||||
expect(() => tokenize("'\\'")).toThrow("Tokenizer error: could not tokenize `'\\'`");
|
||||
expect(tokenize("'hello ged'")).toEqual([{ type: "VALUE", value: "'hello ged'" }]);
|
||||
expect(tokenize("'hello \\'ged\\''")).toEqual([{ type: "VALUE", value: "'hello \\'ged\\''" }]);
|
||||
|
||||
expect(() => tokenize('"')).toThrow("Invalid expression");
|
||||
expect(() => tokenize('"\\"')).toThrow("Invalid expression");
|
||||
expect(() => tokenize('"')).toThrow('Tokenizer error: could not tokenize `"`');
|
||||
expect(() => tokenize('"\\"')).toThrow('Tokenizer error: could not tokenize `"\\"`');
|
||||
expect(tokenize('"hello ged"')).toEqual([{ type: "VALUE", value: '"hello ged"' }]);
|
||||
expect(tokenize('"hello ged"}')).toEqual([
|
||||
{ type: "VALUE", value: '"hello ged"' },
|
||||
|
||||
Reference in New Issue
Block a user