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";
}
}
}