Pretty print stack trace
This commit is contained in:
parent
a0c1724b2e
commit
19116dcbd0
@ -1,8 +1,10 @@
|
||||
#include <vector>
|
||||
#include <string>
|
||||
#include <iomanip>
|
||||
#include <iostream>
|
||||
using namespace std;
|
||||
|
||||
#include "include/utils.h"
|
||||
#include "include/errors.h"
|
||||
#include "include/colors.h"
|
||||
#include "include/tokenize.h"
|
||||
@ -60,14 +62,26 @@ void print_error_position(vector<string> history, CodePosition pos) {
|
||||
if (pos.column == -1 || pos.line == -1)
|
||||
return;
|
||||
|
||||
cout << endl;
|
||||
string line = history[pos.line];
|
||||
|
||||
printf(BOLD "%4d " RESET , pos.line+1);
|
||||
cout << line << endl;
|
||||
cout << endl << BOLD
|
||||
<< setw(4) << setfill(' ')
|
||||
<< pos.line+1 << " " RESET
|
||||
<< history[pos.line] << endl;
|
||||
|
||||
for (int i=0; i < pos.column + 5; i++)
|
||||
cout << " ";
|
||||
|
||||
cout << BOLD RED "^--" RESET << endl;
|
||||
}
|
||||
|
||||
void print_error_stack_trace(vector<string> history, const RuntimeError& error) {
|
||||
cout << "\n" BOLD "Traceback" RESET " (most recent call last)" << endl;
|
||||
for (StackTraceEntry e : error.trace) {
|
||||
cout << BOLD << setw(4) << setfill(' ')
|
||||
<< get<1>(e).line+1 << RESET BLUE
|
||||
<< setw(16) << setfill(' ')
|
||||
<< get<0>(e) << RESET << "\t"
|
||||
<< trim(history[get<1>(e).line]) << endl;
|
||||
}
|
||||
|
||||
cout << BOLD RED "Runtime Error: " RESET << error.get_message() << endl;
|
||||
}
|
@ -115,4 +115,9 @@ public:
|
||||
*/
|
||||
void print_error_position(vector<string> history, CodePosition pos);
|
||||
|
||||
/**
|
||||
* Display the stack trace associated with an error
|
||||
*/
|
||||
void print_error_stack_trace(vector<string> history, const RuntimeError& error);
|
||||
|
||||
#endif
|
@ -39,4 +39,9 @@ string _debug_get_type_type_name(TypeType type);
|
||||
*/
|
||||
bool equal_types(Type type1, Type type2);
|
||||
|
||||
/**
|
||||
* Trim a string (remove whitespaces before and after)
|
||||
*/
|
||||
string trim(const string& str);
|
||||
|
||||
#endif
|
@ -119,12 +119,7 @@ int main(int argc, char* argv[]) {
|
||||
cout << BOLD RED "Type Error: " RESET << e.get_message() << endl;
|
||||
|
||||
} catch (const RuntimeError& e) {
|
||||
print_error_position(input, e.pos);
|
||||
cout << BOLD RED "Runtime Error: " RESET << e.get_message() << endl;
|
||||
|
||||
for (StackTraceEntry e : e.trace) {
|
||||
cout << get<0>(e) << " " << (get<1>(e).line + 1) << endl;
|
||||
}
|
||||
print_error_stack_trace(input, e);
|
||||
}
|
||||
cout << endl;
|
||||
}
|
||||
|
@ -111,4 +111,14 @@ bool equal_types(Type type1, Type type2) {
|
||||
return false;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
string trim(const string& str) {
|
||||
size_t first = str.find_first_not_of(" \t\n\r");
|
||||
size_t last = str.find_last_not_of(" \t\n\r");
|
||||
|
||||
if (first == string::npos || last == string::npos)
|
||||
return ""; // or handle empty string
|
||||
|
||||
return str.substr(first, last - first + 1);
|
||||
}
|
Loading…
Reference in New Issue
Block a user