diff --git a/src/main/wacc/frontend/ast.scala b/src/main/wacc/frontend/ast.scala index ac0e585..9b14b13 100644 --- a/src/main/wacc/frontend/ast.scala +++ b/src/main/wacc/frontend/ast.scala @@ -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 { diff --git a/wacc-syntax/extension.js b/wacc-syntax/extension.js index b5894de..c2fd39c 100644 --- a/wacc-syntax/extension.js +++ b/wacc-syntax/extension.js @@ -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 = {