From ced173d80f5e2113cc6a3315debbed068335162f Mon Sep 17 00:00:00 2001 From: augustin64 Date: Wed, 15 Nov 2023 15:23:33 +0100 Subject: [PATCH] main: add pretty errors --- src/errors.cpp | 1 - src/main.cpp | 14 ++++++++++---- src/parser.cpp | 47 +++++++++++++++++++++++++++-------------------- src/tokenize.cpp | 3 +-- 4 files changed, 38 insertions(+), 27 deletions(-) diff --git a/src/errors.cpp b/src/errors.cpp index a03b80c..5309ac7 100644 --- a/src/errors.cpp +++ b/src/errors.cpp @@ -11,7 +11,6 @@ void pretty_print_error(vector history, CodePosition pos) { if (pos.column == -1 || pos.line == -1) return; - cout << pos.column << ";" << pos.line << endl; string line = history[pos.line]; printf(BOLD "%4d " RESET , pos.line+1); diff --git a/src/main.cpp b/src/main.cpp index b9906fc..ecfef8c 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -1,8 +1,9 @@ -#include +#include #include using namespace std; #include "include/input.h" +#include "include/errors.h" #include "include/colors.h" #include "include/parser.h" #include "include/tokenize.h" @@ -20,10 +21,12 @@ int main(int argc, char* argv[]) { i++; } + vector input; + vector tokens; while (true) { try { - vector input = get_input(); - vector tokens = tokenize(input); + input = get_input(); + tokens = tokenize(input); Node ast = parse(tokens); @@ -32,8 +35,11 @@ int main(int argc, char* argv[]) { EvalResult res = eval(ast); cout << get(res) << endl; + } catch (const SyntaxError& e) { - cout << RED "SyntaxError: " RESET << e.what() << endl; + pretty_print_error(input, e.pos); + cout << BOLD RED "Syntax Error: " RESET << e.what() << endl; + } catch (const ParseException& e) { cout << RED "ParsingError" RESET << endl; } diff --git a/src/parser.cpp b/src/parser.cpp index e85ec2d..06d63ab 100644 --- a/src/parser.cpp +++ b/src/parser.cpp @@ -64,21 +64,25 @@ Node parse(vector tokens) { Node node = ret.node; - while (tokens.size() != 0) { - ParseReturn ret = parse_instruction(tokens); - tokens = ret.tokens; - - if (!holds_alternative(ret.node) || get(ret.node).type != NodeType::Epsilon) { - InnerNode new_node = { - .type=NodeType::Prog, - .children={node, ret.node}, - .pos=null_pos - }; - node = new_node; + try { + while (tokens.size() != 0) { + ParseReturn ret = parse_instruction(tokens); + tokens = ret.tokens; + + if (!holds_alternative(ret.node) || get(ret.node).type != NodeType::Epsilon) { + InnerNode new_node = { + .type=NodeType::Prog, + .children={node, ret.node}, + .pos=get_node_pos(ret.node) + }; + node = new_node; + } } + return node; + } catch (const ParseException& pex) { + CodePosition pos = tokens.back().pos; + throw SyntaxError("Unable to parse", pos); } - - return node; } ParseReturn parse_instruction(vector tokens) { @@ -161,7 +165,7 @@ ParseReturn parse_expr_statement(vector tokens) { InnerNode node = { .type=NodeType::Declaration, .children={type, identifier}, - .pos=type.pos + .pos=identifier.pos }; return { .node=node, @@ -178,7 +182,7 @@ ParseReturn parse_expr_statement(vector tokens) { InnerNode node = { .type=NodeType::AssignedDeclaration, .children={type, identifier, ret.node}, - .pos=type.pos + .pos=get_node_pos(ret.node) }; return { .node=node, @@ -225,7 +229,7 @@ ParseReturn parse_expr(vector tokens) { InnerNode new_node = { .type=type, .children={node, ret.node}, - .pos=get_node_pos(node) + .pos=get_node_pos(ret.node) }; node = new_node; } catch (const ParseException& pex) { @@ -286,7 +290,7 @@ ParseReturn parse_t(vector tokens) { InnerNode new_node = { .type=type, .children={node, ret.node}, - .pos=get_node_pos(node) + .pos=get_node_pos(ret.node) }; node = new_node; } catch (const ParseException& pex) { @@ -419,7 +423,7 @@ ParseReturn parse_f(vector tokens) { InnerNode node = { .type = NodeType::Assignment, .children = { ret.node, ret_expr.node }, - .pos=get_node_pos(ret.node) + .pos=get_node_pos(ret_expr.node) }; return { .node=node, @@ -437,14 +441,17 @@ ParseReturn parse_f(vector tokens) { if (tokens.back().type != TokenType::LParenthese) throw ParseException(); - CodePosition pos = tokens.back().pos; tokens.pop_back(); ParseReturn ret = parse_expr(tokens); tokens=ret.tokens; + _debug_print_tokens(tokens); + + CodePosition pos = tokens.back().pos; + cout << pos.line << ";" << pos.column << endl; if (tokens.size() < 1 || tokens.back().type != TokenType::RParenthese) - throw SyntaxError("Missing ')'", pos); + throw SyntaxError("Missing ')'", tokens.back().pos); tokens.pop_back(); diff --git a/src/tokenize.cpp b/src/tokenize.cpp index 99fd77c..75726ef 100644 --- a/src/tokenize.cpp +++ b/src/tokenize.cpp @@ -70,10 +70,9 @@ vector tokenize(vector input) { for (int i = 0; i < int(input.size()); i++) { string line = input[i]; int j = 0; - - CodePosition pos = { .line = i, .column = j }; while (j < int(line.length())) { + CodePosition pos = { .line = i, .column = j }; string str = line.substr(j, string::npos); smatch m;