Skip to content

Commit

Permalink
Fix lexer error handling
Browse files Browse the repository at this point in the history
  • Loading branch information
happytomatoe committed Oct 3, 2024
1 parent afbf33b commit faef67d
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 3 deletions.
7 changes: 7 additions & 0 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,13 @@
],
"outFiles": ["${workspaceFolder}/extension/build/test/**/*.js"],
"preLaunchTask": "${defaultBuildTask}"
},
{
"type": "chrome",
"request": "launch",
"name": "Test web ide",
"url": "http://localhost:3000/web-ide",
"webRoot": "${workspaceFolder}"
}
]
}
23 changes: 20 additions & 3 deletions simulator/src/jack/listener/error.listener.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
import { ErrorListener, RecognitionException, Recognizer, Token } from "antlr4";
import { ErrorListener, NoViableAltException, RecognitionException, Recognizer, Token } from "antlr4";
import { JackCompilerError, LexerOrParserError } from "../error.js";
interface LexerNoViableAltException {
startIndex: number;
}
export class CustomErrorListener extends ErrorListener<any> {
public errors: JackCompilerError[] = [];

Expand All @@ -11,7 +14,21 @@ export class CustomErrorListener extends ErrorListener<any> {
msg: string,
e: RecognitionException | undefined,
) => {
const t = offendingSymbol as Token;
this.errors.push(new LexerOrParserError(line, t.start, t.stop + 1, msg));
if (offendingSymbol != null || e != null && e.offendingToken != null) {
const t = offendingSymbol ?? e!.offendingToken as Token;
this.errors.push(new LexerOrParserError(line, t.start, t.stop + 1, msg));
} else if (e instanceof NoViableAltException) {
this.errors.push(new LexerOrParserError(line, e.startToken.start, e.startToken.stop + 1, msg));

}
//antlr doesn't provide a class for LexerNoViableAltException atm. Once https://github.com/antlr/antlr4/pull/4711 is release we can change it
else if (e != null && 'startIndex' in e) {
const err = e as LexerNoViableAltException;
this.errors.push(new LexerOrParserError(line, err.startIndex, err.startIndex + 1, msg));
} else {
console.error("Don't know how to handle this error");
throw new Error("Don't know how to handle this error");
}
};
}

0 comments on commit faef67d

Please sign in to comment.