diff --git a/compiler/ast/ast.ts b/compiler/ast/ast.ts index 4040f6e..2a16763 100644 --- a/compiler/ast/ast.ts +++ b/compiler/ast/ast.ts @@ -1,3 +1,5 @@ +import { Span } from "../diagnostics.ts"; + export type File = { stmts: Stmt[]; }; @@ -5,6 +7,7 @@ export type File = { export type Stmt = { id: number; kind: StmtKind; + span: Span; }; export type StmtKind = diff --git a/compiler/main.ts b/compiler/main.ts index fb99755..5dcf461 100644 --- a/compiler/main.ts +++ b/compiler/main.ts @@ -52,7 +52,7 @@ export class FileTreeCollector implements ast.Visitor<[_P]> { } visitModFileStmt( - _stmt: ast.Stmt, + stmt: ast.Stmt, kind: ast.ModFileStmt, { file }: _P, ): ast.VisitRes { @@ -65,6 +65,7 @@ export class FileTreeCollector implements ast.Visitor<[_P]> { severity: "fatal", msg: `module '${ident}' already declared`, file, + span: stmt.span, }); } return new FileTreeCollector( diff --git a/compiler/test_diagnostics.ts b/compiler/test_diagnostics.ts new file mode 100644 index 0000000..8b0bfd9 --- /dev/null +++ b/compiler/test_diagnostics.ts @@ -0,0 +1,27 @@ +import { Ctx } from "./ctx.ts"; +import { prettyPrintReport } from "./diagnostics.ts"; + +const ctx = new Ctx(); + +const text = ` +make an error here +`; + +const file = ctx.addFile( + "root", + "path/file.ts", + "path/file.ts", + undefined, + text, +); + +prettyPrintReport(ctx, { + file, + msg: "an error", + severity: "error", + origin: "compiler", + span: { + begin: { idx: 6, line: 2, col: 6 }, + end: { idx: 13, line: 2, col: 13 }, + }, +});