summaryrefslogtreecommitdiff
path: root/src/compiler/utils/error.ts
diff options
context:
space:
mode:
authorGravatar Matthew Phillips <matthew@matthewphillips.info> 2021-03-16 16:08:11 -0400
committerGravatar GitHub <noreply@github.com> 2021-03-16 16:08:11 -0400
commit588b086a4dc45e717341e18728fff65a90ddf46c (patch)
treefbabfff761add5217963a3d341d68392bea2cccf /src/compiler/utils/error.ts
parent174fc1d669fea1cf8be0d873fdb232fd0169abe6 (diff)
downloadastro-588b086a4dc45e717341e18728fff65a90ddf46c.tar.gz
astro-588b086a4dc45e717341e18728fff65a90ddf46c.tar.zst
astro-588b086a4dc45e717341e18728fff65a90ddf46c.zip
Bring compiler into Astro (#4)
* include source compiler * Import from JS * Conditionally use the instance contents Co-authored-by: Fred K. Schott <fkschott@gmail.com>
Diffstat (limited to 'src/compiler/utils/error.ts')
-rw-r--r--src/compiler/utils/error.ts42
1 files changed, 42 insertions, 0 deletions
diff --git a/src/compiler/utils/error.ts b/src/compiler/utils/error.ts
new file mode 100644
index 000000000..5c2cf3732
--- /dev/null
+++ b/src/compiler/utils/error.ts
@@ -0,0 +1,42 @@
+// @ts-nocheck
+
+import { locate } from 'locate-character';
+import get_code_frame from './get_code_frame.js';
+
+class CompileError extends Error {
+ code: string;
+ start: { line: number; column: number };
+ end: { line: number; column: number };
+ pos: number;
+ filename: string;
+ frame: string;
+
+ toString() {
+ return `${this.message} (${this.start.line}:${this.start.column})\n${this.frame}`;
+ }
+}
+
+export default function error(message: string, props: {
+ name: string;
+ code: string;
+ source: string;
+ filename: string;
+ start: number;
+ end?: number;
+}): never {
+ const error = new CompileError(message);
+ error.name = props.name;
+
+ const start = locate(props.source, props.start, { offsetLine: 1 });
+ const end = locate(props.source, props.end || props.start, { offsetLine: 1 });
+
+ error.code = props.code;
+ error.start = start;
+ error.end = end;
+ error.pos = props.start;
+ error.filename = props.filename;
+
+ error.frame = get_code_frame(props.source, start.line - 1, start.column);
+
+ throw error;
+}