Skip to content

Commit a74cf3e

Browse files
committed
change how compiler error works
1 parent 8d6168d commit a74cf3e

File tree

2 files changed

+25
-13
lines changed

2 files changed

+25
-13
lines changed

src/diagnostic.ts

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,33 @@
1-
import { DiagnosticSeverity, DocumentDiagnosticReportKind, FullDocumentDiagnosticReport } from './diagnosticTypes.js';
1+
import { Diagnostic, DiagnosticSeverity, DocumentDiagnosticReportKind, FullDocumentDiagnosticReport } from './diagnosticTypes.js';
22
import { sysparser, syxparser } from './ast.js';
33
import { tokenizeSys, tokenizeSyx } from './lexer.js';
4-
import { CompilerError } from './types.js';
4+
import { isCompilerError } from './types.js';
55
import { readFileSync } from 'fs';
66

77

88
/**
99
* Creates a diagnostic report from the file path given.
1010
* @param {string} filePath Path of the file to create a report.
11+
* @param {string} fileContent Content of the file if it is already fetched.
1112
* @returns A diagnostic report language servers can use.
1213
*/
13-
export function createSyntaxScriptDiagnosticReport(filePath: string): FullDocumentDiagnosticReport {
14+
export function createSyntaxScriptDiagnosticReport(filePath: string,fileContent?:string): FullDocumentDiagnosticReport {
1415
const isSyx: boolean = filePath.endsWith('.syx');
1516

17+
const items:Diagnostic[] = [{message:'Base',range:{end:{character:0,line:1},start:{character:0,line:0}},severity:DiagnosticSeverity.Warning,source:'syntax-script'}];
18+
1619
try {
1720

18-
const content = readFileSync(filePath).toString();
21+
const content = fileContent??readFileSync(filePath).toString();
1922
const tokens = (isSyx ? tokenizeSyx : tokenizeSys)(content);
2023
(isSyx ? syxparser : sysparser).parseTokens(tokens);
2124

22-
return { kind: DocumentDiagnosticReportKind.Full, items: [] };
2325
} catch (error) {
24-
if (error instanceof CompilerError) {
25-
const compilerError = error as CompilerError;
26-
return {
27-
kind: DocumentDiagnosticReportKind.Full, items: [
28-
{ message: compilerError.message, range: compilerError.range, severity: DiagnosticSeverity.Error, source: 'syntax-script' }
29-
]
30-
};
26+
if (isCompilerError(error)) {
27+
items.push({ message: error.message, range: error.range, severity: DiagnosticSeverity.Error, source: 'syntax-script' });
3128
}
29+
} finally {
30+
return {items,kind:DocumentDiagnosticReportKind.Full};
3231
}
3332

3433
}

src/types.ts

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -520,7 +520,7 @@ export interface SyxConfigCompile {
520520
/**
521521
* An error that occured while tokenizing, parsing or compiling a file.
522522
* @author efekos
523-
* @version 1.0.1
523+
* @version 1.0.2
524524
* @since 0.0.1-alpha
525525
*/
526526
export class CompilerError extends Error {
@@ -537,5 +537,18 @@ export class CompilerError extends Error {
537537
this.range = range;
538538
this.message = message;
539539
this.file = file;
540+
this.name = 'CompilerError';
540541
}
542+
}
543+
544+
/**
545+
* Checks whether an error is a {@link CompilerError}.
546+
* @param {Error} error Any error.
547+
* @author efekos
548+
* @version 1.0.0
549+
* @since 0.0.1-alpha
550+
* @returns Whether it is a {@link CompilerError} or not.
551+
*/
552+
export function isCompilerError(error:Error):error is CompilerError{
553+
return error.name === 'CompilerError';
541554
}

0 commit comments

Comments
 (0)