Skip to content

Commit 4afe35b

Browse files
Use custom TranslatorErrors
1 parent a3af20b commit 4afe35b

File tree

2 files changed

+29
-2
lines changed

2 files changed

+29
-2
lines changed

src/errors.ts

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -215,4 +215,30 @@ export namespace ResolverErrors {
215215
this.name = "NameReassignmentError";
216216
}
217217
}
218+
}
219+
220+
export namespace TranslatorErrors {
221+
export class BaseTranslatorError extends SyntaxError {
222+
line: number;
223+
col: number;
224+
loc: Position;
225+
226+
constructor(message: string, line: number, col: number) {
227+
super(`BaseTranslatorError at line ${line} column ${col-1}
228+
${message}`);
229+
this.line = line;
230+
this.col = col;
231+
this.name = "BaseTranslatorError";
232+
this.loc = toEstreeLocation(line, col, 0);
233+
}
234+
}
235+
export class UnsupportedOperator extends BaseTranslatorError {
236+
constructor(line: number, col: number, source: string, start: number) {
237+
let msg = getFullLine(source, start) + "\n";
238+
let hint = `^ This operator is not yet supported by us.`;
239+
hint = hint.padStart(hint.length + col - MAGIC_OFFSET, " ");
240+
super(msg + hint, line, col);
241+
this.name = "UnsupportedOperator";
242+
}
243+
}
218244
}

src/translator.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ import {
3939
VariableDeclarator,
4040
WhileStatement
4141
} from "estree";
42+
import {TranslatorErrors} from "./errors";
4243

4344
export interface EstreePosition {
4445
line: number;
@@ -435,7 +436,7 @@ export class Translator implements StmtNS.Visitor<BaseNode>, ExprNS.Visitor<Base
435436
// @TODO double slash and power needs to convert to math exponent/floor divide
436437
case TokenType.DOUBLESLASH:
437438
case TokenType.DOUBLESTAR:
438-
throw new Error("This operator is not yet supported");
439+
throw new TranslatorErrors.UnsupportedOperator(expr.operator.line, expr.operator.col, this.source, expr.operator.indexInSource);
439440
default:
440441
throw new Error("Unreachable binary code path in translator");
441442
}
@@ -476,7 +477,7 @@ export class Translator implements StmtNS.Visitor<BaseNode>, ExprNS.Visitor<Base
476477
case TokenType.ISNOT:
477478
case TokenType.IN:
478479
case TokenType.NOTIN:
479-
throw new Error("This operator is not yet supported");
480+
throw new TranslatorErrors.UnsupportedOperator(expr.operator.line, expr.operator.col, this.source, expr.operator.indexInSource);
480481
default:
481482
throw new Error("Unreachable binary code path in translator");
482483
}

0 commit comments

Comments
 (0)