Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
happytomatoe committed Oct 3, 2024
1 parent faef67d commit dd918cb
Showing 1 changed file with 21 additions and 8 deletions.
29 changes: 21 additions & 8 deletions simulator/src/jack/listener/error.listener.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,10 @@
import { ErrorListener, NoViableAltException, RecognitionException, Recognizer, Token } from "antlr4";
import {
ErrorListener,
NoViableAltException,
RecognitionException,
Recognizer,
Token,
} from "antlr4";
import { JackCompilerError, LexerOrParserError } from "../error.js";
interface LexerNoViableAltException {
startIndex: number;
Expand All @@ -14,21 +20,28 @@ export class CustomErrorListener extends ErrorListener<any> {
msg: string,
e: RecognitionException | undefined,
) => {
if (offendingSymbol != null || e != null && e.offendingToken != null) {
const t = offendingSymbol ?? e!.offendingToken as Token;
if (offendingSymbol != null || (e != null && e.offendingToken != null)) {
const t = offendingSymbol ?? (e!.offendingToken as Token);

Check warning on line 24 in simulator/src/jack/listener/error.listener.ts

View workflow job for this annotation

GitHub Actions / all

Forbidden non-null assertion
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));

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) {
else if (e != null && "startIndex" in e) {
const err = e as LexerNoViableAltException;
this.errors.push(new LexerOrParserError(line, err.startIndex, err.startIndex + 1, msg));
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 dd918cb

Please sign in to comment.