refactor: update error handling and diagnostics in IntelliWACC extension

This commit is contained in:
Guy C
2025-03-10 18:23:40 +00:00
parent 54d6e7143b
commit bad6e47e46
2 changed files with 38 additions and 8 deletions

View File

@@ -4,7 +4,6 @@ import parsley.Parsley
import parsley.generic.ErrorBridge
import parsley.ap._
import parsley.position._
import parsley.syntax.zipped._
import cats.data.NonEmptyList
object ast {

View File

@@ -1,22 +1,26 @@
// Developed using the VSC language extension tutorial
// https://code.visualstudio.com/api/language-extensions/overview
const vscode = require('vscode');
const { execSync } = require('child_process');
const { parse } = require('path');
function activate(context) {
console.log('IntelliWACC is now active!');
// Create a DiagnosticCollection for error highlighting
let diagnosticCollection = vscode.languages.createDiagnosticCollection('wacc');
context.subscriptions.push(diagnosticCollection);
// Listen for document saves and run error checking
vscode.workspace.onDidSaveTextDocument((document) => {
if (document.languageId !== 'wacc') return;
let diagnostics = [];
let errors = runCompilerAndGetErrors(document.getText());
let errors = generateErrors(document.getText());
errors.forEach(error => {
let range = new vscode.Range(error.line, error.column, error.line, error.column + 1);
let diagnostic = new vscode.Diagnostic(range, error.message, vscode.DiagnosticSeverity.Error);
console.log(error);
let range = new vscode.Range(error.line - 1 , error.column - 1, error.line - 1, error.column + error.size);
let diagnostic = new vscode.Diagnostic(range, error.errorMessage, vscode.DiagnosticSeverity.Error);
diagnostics.push(diagnostic);
});
@@ -28,8 +32,35 @@ function deactivate() {
console.log('IntelliWACC is deactivating...');
}
function runCompilerAndGetErrors(code) {
// COMPILER INTEGRATION
function generateErrors(code) {
try {
const fs = require('fs');
const tmpFilePath = '/tmp/temp_wacc_file.wacc';
fs.writeFileSync(tmpFilePath, code);
let output;
try {
// currently only works if the wacc compiler exe is in the parent directory
output = execSync(`../wacc_exe ${tmpFilePath}`, { encoding: 'utf8', stdio: 'pipe' });
} catch (err) {
output = err.stdout;
}
let errors = [];
errorRegex = /\(line ([\d]+), column ([\d]+)\):\n([^>]+)([^\^]+)([\^]+)\n([^\n]+)([^\(]*)/g
while((match = errorRegex.exec(output)) !== null) {
console.log(match[5]);
errors.push({
line: parseInt(match[1], 10),
column: parseInt(match[2], 10),
errorMessage: match[3].trim(),
size: match[5].length - 1
});
}
return errors;
} catch (err) {
console.error('Error running compiler:', err);
return [];
}
}
module.exports = {