Mochaccino Sprint 2
Share
Explore

icon picker
Error Handler

error_handler.dart

ErrorHandler

Now, when designing a full-fledged language, errors can be reported to the user in many different ways — red underlines in the editor, as “problems” in the debugging tab, or through the console. We want to have a central place to collect and report issues, which is why we need the error handler.

issues

The issues property contains a list of all problems pertaining to the user’s code. Whenever the tokeniser, parser, or interpreter come across issues, they add them to this list. Furthermore, a non-empty issue list while abort the compilation process before the AST is interpreted.

reportAll

This function will report all the issues in the issues list, through the IssueReporter. The IssueReporter specifies how the issues are reported. Do note that this method does not clear the issues list — you'll need to compile the program again.

Issue

An Issue represents an actual error or warning in the program.

consoleString

The consoleString getter returns a String representation of the error, for the purpose of being printed to the console.

IssueTitle

There are different types of errors — syntax errors, module errors, null errors, etc. But each type of error has subtypes too. A syntax error can be due to an unexpected token or an unterminated literal, or a missing semicolon. We could use an enum for the various subtypes, but then could you do the following?

unterminatedPair

unexpectedChar

IssueType

IssueReporter

Final File

part of cortado;

enum IssueType {
SyntaxError,
PackageError,
StackError,
}

enum IssueReporter {
console,
}

enum TextColor {
red,
yellow,
green,
pink,
cyan,
}

class IssueTitle {
static String unterminatedPair(String pair) => "Unterminated '$pair' pair";
static String unexpectedChar(String char) => "Unexpected character '$char'";
}

class Issue {
final IssueType issueType;
final String title;
final String filePath;
final int lineNo;
final String offendingLine;
final int start;
final String description;
Issue(
this.issueType,
this.title, {
required this.lineNo,
required this.offendingLine,
required this.start,
required this.description,
this.filePath = 'MAIN',
});
String get consoleString {
return """
${issueType.toString().replaceAll("IssueType.", "")}: $title
in [$filePath]:

$lineNo| $offendingLine
${' ' * (4 + (lineNo.toString().length) + 2 + start).toInt()}^
$description
""";
}
}

class ErrorHandler {
static final List<Issue> issues = [];
static void reportAll([IssueReporter issueReporter = IssueReporter.console]) {
if (issueReporter == IssueReporter.console) {
issues.forEach((Issue issue) => print(issue.consoleString));
}
}
}

extension ConsoleUtils on String {
String withColor(TextColor color) {
switch (color) {
case TextColor.red:
return "\u001b[31m$this\u001b[0m";
case TextColor.yellow:
return "\u001b[33m$this\u001b[0m";
case TextColor.green:
return "\u001b[32$this\u001b[0m";
case TextColor.pink:
return "\u001b[35m$this\u001b[0m";
case TextColor.cyan:
return "\u001b[36m$this\u001b[0m";
}
}
}

Share
 
Want to print your doc?
This is not the way.
Try clicking the ⋯ next to your doc name or using a keyboard shortcut (
CtrlP
) instead.