From 0a352f29a94f4f7f169ca22e8b979019219def15 Mon Sep 17 00:00:00 2001 From: TopchetoEU <36534413+TopchetoEU@users.noreply.github.com> Date: Sun, 6 Nov 2022 10:42:48 +0200 Subject: [PATCH] fix: some issues with parsing --- src/compiler/treeifier/ast/parsers/exp.cc | 54 +++++++++------------- src/compiler/treeifier/ast/parsers/nmsp.cc | 1 + src/compiler/treeifier/ast/parsers/stat.cc | 6 +-- 3 files changed, 27 insertions(+), 34 deletions(-) diff --git a/src/compiler/treeifier/ast/parsers/exp.cc b/src/compiler/treeifier/ast/parsers/exp.cc index b6ba5e5..4b77c3f 100644 --- a/src/compiler/treeifier/ast/parsers/exp.cc +++ b/src/compiler/treeifier/ast/parsers/exp.cc @@ -154,7 +154,7 @@ bool pop_call(size_t n, location_t loc, std::vector> &op_st op_stack.pop_back(); call["location"] = conv::loc_to_map(loc); - for (size_t i = 0; i <= n; i++) { + for (size_t i = 0; i < n; i++) { args.push_back(res.back()); res.pop_back(); } @@ -181,15 +181,7 @@ bool pop_until(const op_data_t &data, tree_helper_t &h, std::vectorprecedence == precedence_t::PAREN) { + is_paren = true; + break; + } + else if (i->precedence == precedence_t::CALL_START) { + is_call = true; + break; + } + } + + if (is_call) pop_call(call_args_n.back(), h.loc(), op_stack, res); + else if (is_paren) pop_paren(op_stack, res); + else break; + + if (!h.try_advance()) break; + } + else if (last_val) { if (op == operator_t::PAREN_OPEN) { h.advance("Expected an argument."); call_args_n.push_back(0); @@ -255,26 +267,6 @@ bool ast::parse_exp(ast_ctx_t &ctx, size_t &res_i, map_t &out) { call_args_n.back()++; last_val = false; } - else if (op == operator_t::PAREN_CLOSE) { - bool is_call = false, is_paren = false; - - for (auto i = op_stack.rbegin(); i != op_stack.rend(); i++) { - if (i->precedence == precedence_t::PAREN) { - is_paren = true; - break; - } - else if (i->precedence == precedence_t::CALL_START) { - is_call = true; - break; - } - } - - if (is_call) pop_call(call_args_n.back(), h.loc(), op_stack, res); - else if (is_paren) pop_paren(op_stack, res); - else break; - - if (!h.try_advance()) break; - } else if (op == operator_t::COLON) { h.advance("Expected a type."); pop_until({ .precedence = precedence_t::PREFIX, .assoc = true }, h, op_stack, res); diff --git a/src/compiler/treeifier/ast/parsers/nmsp.cc b/src/compiler/treeifier/ast/parsers/nmsp.cc index 13431ad..d2cabf5 100644 --- a/src/compiler/treeifier/ast/parsers/nmsp.cc +++ b/src/compiler/treeifier/ast/parsers/nmsp.cc @@ -12,6 +12,7 @@ bool ast::parse_nmsp(ast_ctx_t &ctx, size_t &res_i, map_t &out) { while (true) { if (h.ended()) break; if (!h.curr().is_operator(operator_t::DOUBLE_COLON)) break; + h.advance("Expected an identifier."); h.force_push_parse(parse_identifier, "Expected an identifier.", arr); } diff --git a/src/compiler/treeifier/ast/parsers/stat.cc b/src/compiler/treeifier/ast/parsers/stat.cc index 4548797..ff31c03 100644 --- a/src/compiler/treeifier/ast/parsers/stat.cc +++ b/src/compiler/treeifier/ast/parsers/stat.cc @@ -3,7 +3,7 @@ bool ast::parse_if(ast_ctx_t &ctx, size_t &res_i, map_t &out) { tree_helper_t h(ctx, res_i); - h.throw_ended(); + h.throw_ended("Expected open parens after if keyword."); if (!h.curr("Expected open parens after if keyword.").is_operator(operator_t::PAREN_OPEN)) { throw message_t::error("Expected open parens after if keyword.", h.loc(1)); } @@ -29,7 +29,7 @@ bool ast::parse_if(ast_ctx_t &ctx, size_t &res_i, map_t &out) { bool ast::parse_while(ast_ctx_t &ctx, size_t &res_i, map_t &out) { tree_helper_t h(ctx, res_i); - h.throw_ended(); + h.throw_ended("Expected open parens after while keyword."); if (!h.curr("Expected open parens after while keyword.").is_operator(operator_t::PAREN_OPEN)) { throw message_t::error("Expected open parens after while keyword.", h.loc(1)); } @@ -50,7 +50,7 @@ bool ast::parse_while(ast_ctx_t &ctx, size_t &res_i, map_t &out) { bool ast::parse_return(ast_ctx_t &ctx, size_t &res_i, map_t &out) { tree_helper_t h(ctx, res_i); - h.advance("Expected an expression."); + h.throw_ended("Expected an expression."); h.force_parse(parse_exp, "Expected an expression.", out["condition"].map({})); if (!h.curr("Expected a semicolon.").is_operator(operator_t::SEMICOLON)) {