Skip to content

Commit 5ec4540

Browse files
committed
add diagnostic namespace
1 parent a4b3266 commit 5ec4540

File tree

2 files changed

+41
-1
lines changed

2 files changed

+41
-1
lines changed

src/diagnostic.ts

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
import { DiagnosticSeverity, DocumentDiagnosticReportKind, FullDocumentDiagnosticReport } from './diagnosticTypes.js';
2+
import { sysparser, syxparser } from './ast.js';
3+
import { tokenizeSys, tokenizeSyx } from './lexer.js';
4+
import { CompilerError } from './types.js';
5+
import { readFileSync } from 'fs';
6+
7+
8+
export namespace diagnostic {
9+
10+
/**
11+
* Creates a diagnostic report from the file path given.
12+
* @param {string} filePath Path of the file to create a report.
13+
* @returns A diagnostic report language servers can use.
14+
*/
15+
export function createReport(filePath: string): FullDocumentDiagnosticReport {
16+
const isSyx: boolean = filePath.endsWith('.syx');
17+
18+
try {
19+
20+
const content = readFileSync(filePath).toString();
21+
const tokens = (isSyx ? tokenizeSyx : tokenizeSys)(content);
22+
(isSyx ? syxparser : sysparser).parseTokens(tokens);
23+
24+
return { kind: DocumentDiagnosticReportKind.Full, items: [] };
25+
} catch (error) {
26+
if (error instanceof CompilerError) {
27+
const compilerError = error as CompilerError;
28+
return {
29+
kind: DocumentDiagnosticReportKind.Full, items: [
30+
{ message: compilerError.message, range: compilerError.range, severity: DiagnosticSeverity.Error, source: 'syntax-script' }
31+
]
32+
};
33+
}
34+
}
35+
36+
}
37+
38+
}

src/index.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
export * from './ast.js';
22
export * from './compiler.js';
33
export * from './lexer.js';
4-
export * from './types.js';
4+
export * from './types.js';
5+
export * from './diagnostic.js';
6+
export * from './diagnosticTypes.js';

0 commit comments

Comments
 (0)