Skip to content

Commit

Permalink
Melhorar exibição de erros e de saída
Browse files Browse the repository at this point in the history
  • Loading branch information
dgadelha committed Oct 9, 2023
1 parent 8562ef2 commit ac2db77
Show file tree
Hide file tree
Showing 11 changed files with 67 additions and 35 deletions.
1 change: 1 addition & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
"eslint:check": "eslint 'packages/**/*.{t,j}s'",
"start": "lerna run start --scope=@portugol-webstudio/ide",
"build": "lerna run build",
"build:w": "lerna run --parallel build:w",
"build:parser": "lerna run build --scope=@portugol-webstudio/parser",
"build:runner": "lerna run build --scope=@portugol-webstudio/runner",
"build:runtime": "lerna run build --scope=@portugol-webstudio/runtime",
Expand Down
3 changes: 2 additions & 1 deletion packages/antlr/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,8 @@
"antlr-update:PortugolLexico": "wget https://raw.githubusercontent.com/UNIVALI-LITE/Portugol-Studio/master/core/src/main/antlr/PortugolLexico.g4 -O src/PortugolLexico.g4",
"antlr-update:PortugolLexico:patch": "patch src/PortugolLexico.g4 < src/lexico.patch",
"antlr-update": "npm run antlr-update:Portugol && npm run antlr-update:PortugolLexico && npm run antlr-update:PortugolLexico:patch",
"build": "tsc"
"build": "tsc",
"build:w": "tsc -w"
},
"dependencies": {
"antlr4ts": "^0.5.0-alpha.4"
Expand Down
2 changes: 1 addition & 1 deletion packages/ide/src/app/tab-editor/tab-editor.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -288,7 +288,7 @@ export class TabEditorComponent implements OnInit, OnDestroy {
this._code$ = fromEventPattern(editor.onDidChangeModelContent)
.pipe(debounceTime(500))
.subscribe(() => {
this.setEditorErrors(PortugolErrorChecker.check(this.code ?? ""));
this.setEditorErrors(PortugolErrorChecker.checkCode(this.code ?? ""));
});
}

Expand Down
3 changes: 2 additions & 1 deletion packages/parser/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,8 @@
"lib"
],
"scripts": {
"build": "tsc"
"build": "tsc",
"build:w": "tsc -w"
},
"dependencies": {
"@portugol-webstudio/antlr": "*",
Expand Down
19 changes: 14 additions & 5 deletions packages/parser/src/PortugolErrorChecker.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,10 @@
import { PortugolCodeError, PortugolErrorListener, PortugolLexer, PortugolParser } from "@portugol-webstudio/antlr";
import {
ArquivoContext,
PortugolCodeError,
PortugolErrorListener,
PortugolLexer,
PortugolParser,
} from "@portugol-webstudio/antlr";
import { CharStreams, CommonTokenStream } from "antlr4ts";

import errorCheckers from "./errors/index.js";
Expand All @@ -10,17 +16,20 @@ export class PortugolErrorChecker {
private static portugolNode = new PortugolNode();
private static errorListener = new PortugolErrorListener();

public static check(code: string): PortugolCodeError[] {
public static checkCode(code: string): PortugolCodeError[] {
const inputStream = CharStreams.fromString(code);
const lexer = new PortugolLexer(inputStream);
const tokenStream = new CommonTokenStream(lexer);
const parser = new PortugolParser(tokenStream);

this.errorListener.reset();
const tree = parser.arquivo();

parser.addErrorListener(this.errorListener);

const tree = parser.arquivo();
return this.checkTree(tree);
}

public static checkTree(tree: ArquivoContext): PortugolCodeError[] {
this.errorListener.reset();

try {
const arquivo = this.portugolNode.visit(tree) as Arquivo;
Expand Down
4 changes: 3 additions & 1 deletion packages/runner/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,12 @@
"lib"
],
"scripts": {
"build": "tsc"
"build": "tsc",
"build:w": "tsc -w"
},
"dependencies": {
"@portugol-webstudio/antlr": "*",
"@portugol-webstudio/parser": "*",
"@portugol-webstudio/runtime": "*",
"antlr4ts": "^0.5.0-alpha.4",
"rxjs": "^7.8.1"
Expand Down
45 changes: 35 additions & 10 deletions packages/runner/src/PortugolExecutor.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { PortugolErrorListener, PortugolLexer, PortugolParser } from "@portugol-webstudio/antlr";
import { PortugolErrorChecker } from "@portugol-webstudio/parser";
import { CharStreams, CommonTokenStream } from "antlr4ts";
import { Subscription, Subject } from "rxjs";

Expand Down Expand Up @@ -61,6 +62,20 @@ export class PortugolExecutor {
parser.addErrorListener(this.errorListener);

const tree = parser.arquivo();
const errors = PortugolErrorChecker.checkTree(tree);

if (errors.length > 0) {
this.stdOut += `⛔ O seu código possui ${errors.length} erro${errors.length > 1 ? "s" : ""} de compilação:\n`;
this.stdOut += errors
.map(error => ` - ${error.message} (linha ${error.startLine}, posição ${error.startCol})\n`)
.join("");

this.stdOut +=
"\n⚠️ Durante essa fase experimental, o código ainda será executado mesmo com erros, porém se não corrigi-los, a execução abaixo pode exibir mensagens de erro em inglês ou sem explicação.\n";
this.stdOut += ` Caso acredite que o erro não faça sentido, por favor, abra uma issue em https://github.com/dgadelha/Portugol-Webstudio/issues/new e anexe o código que você está tentando executar.\n`;
this.stdOut += "\n- O seu programa irá iniciar abaixo -\n";
this.stdOut$.next(this.stdOut);
}

// @ts-ignore
this._runner = new this.runner(tree);
Expand All @@ -72,7 +87,7 @@ export class PortugolExecutor {
this.byteCode = this._runner.byteCode;

this._runner.stdOut$.subscribe(data => {
this.stdOut = data;
this.stdOut += data;
this.stdOut$.next(data);
});

Expand All @@ -88,27 +103,37 @@ export class PortugolExecutor {

this._runner.run().subscribe({
next: event => {
if (event.type === "finish") {
this.stdOut += `\nPrograma finalizado. Tempo de execução: ${event.time} ms\n`;
this.stdOut$.next(this.stdOut);
switch (event.type) {
case "finish":
this.stdOut += `\nPrograma finalizado. Tempo de execução: ${event.time} ms\n`;
this.stdOut$.next(this.stdOut);
break;

case "clear":
this.stdOut = "";
this.stdOut$.next(this.stdOut);
break;

case "error":
this.stdOut += `\n⛔ ${event.error.message}\n`;
this.stdOut$.next(this.stdOut);
break;

default:
break;
}

this.events.next(event);
},

error: error => {
console.error(error);

this.stdOut += `⛔ ${error.message}\n`;
this.stdOut$.next(this.stdOut);

this.reset(false);
},
});
} catch (err) {
console.error(err);

this.stdOut += `⛔ O seu código possui um erro de compilação!\n`;
this.stdOut += `\n⛔ O seu código possui um erro de compilação!\n`;
this.stdOut$.next(this.stdOut);

this.reset(false);
Expand Down
2 changes: 0 additions & 2 deletions packages/runner/src/runners/IPortugolRunner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,6 @@ export abstract class IPortugolRunner {
abstract byteCode: string;

abstract stdIn: Subject<string>;

abstract stdOut: string;
abstract stdOut$: Observable<string>;

abstract waitingForInput: boolean;
Expand Down
19 changes: 6 additions & 13 deletions packages/runner/src/runners/PortugolWebWorkersRunner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ export class PortugolWebWorkersRunner extends IPortugolRunner {
stdIn = new Subject<string>();
private _stdIn$?: Subscription;

stdOut = "";
stdOut$ = new Subject<string>();

waitingForInput = false;
Expand Down Expand Up @@ -135,8 +134,7 @@ export class PortugolWebWorkersRunner extends IPortugolRunner {
this.worker.addEventListener("message", (message: MessageEvent) => {
switch (message.data.type) {
case "stdOut":
this.stdOut += message.data.content;
this.stdOut$.next(this.stdOut);
this.stdOut$.next(message.data.content);
break;

case "stdIn":
Expand All @@ -151,14 +149,11 @@ export class PortugolWebWorkersRunner extends IPortugolRunner {

error.stack = message.data.error.stack;

this._run.error(error);
this._run.next({ type: "error", error });
this.destroy();
break;

case "clear":
this.stdOut = "";
this.stdOut$.next(this.stdOut);

this._run.next({ type: "clear" });
break;

Expand All @@ -168,20 +163,18 @@ export class PortugolWebWorkersRunner extends IPortugolRunner {
}
});

this.worker.onerror = error => {
console.error(error);
this.worker.onerror = err => {
const error = err.error ?? new Error(err.message);

this._run.error(error);
this._run.next({ type: "error", error });
this.destroy();
};

this._stdIn$ = this.stdIn.subscribe(content => {
if (this.waitingForInput) {
this.waitingForInput = false;
this.waitingForInput$.next(this.waitingForInput);

this.stdOut += `${content}\n`;
this.stdOut$.next(this.stdOut);

this.worker.postMessage({ type: "stdIn", content });
}
});
Expand Down
3 changes: 2 additions & 1 deletion packages/runtime/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,8 @@
"lib"
],
"scripts": {
"build": "tsc"
"build": "tsc",
"build:w": "tsc -w"
},
"dependencies": {
"@portugol-webstudio/antlr": "*",
Expand Down

0 comments on commit ac2db77

Please sign in to comment.