fix: some issues with parsing

This commit is contained in:
TopchetoEU 2022-11-06 10:42:48 +02:00
parent 9bdc97893b
commit 0a352f29a9
3 changed files with 27 additions and 34 deletions

View File

@ -154,7 +154,7 @@ bool pop_call(size_t n, location_t loc, std::vector<located_t<op_data_t>> &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::vector<located_t<op
}
bool ast::parse_exp_var(ast_ctx_t &ctx, size_t &res_i, map_t &out) {
tree_helper_t h(ctx, res_i);
if (h.curr().is_identifier()) {
out["content"] = h.curr().identifier();
out["location"] = conv::loc_to_map(h.loc());
return h.submit(true);
}
return false;
return ctx.parse(parse_nmsp, res_i, out);
}
bool ast::parse_exp_int_lit(ast_ctx_t &ctx, size_t &res_i, map_t &out) {
tree_helper_t h(ctx, res_i);
@ -240,22 +232,7 @@ bool ast::parse_exp(ast_ctx_t &ctx, size_t &res_i, map_t &out) {
}
if (h.curr().is_operator()) {
auto op = h.curr()._operator();
if (last_val) {
if (op == operator_t::PAREN_OPEN) {
h.advance("Expected an argument.");
call_args_n.push_back(0);
op_stack.push_back({ h.loc(), { precedence_t::CALL_START } });
last_val = false;
}
else if (op == operator_t::COMMA) {
if (call_args_n.size() == 0) break;
h.advance("Expected an argument.");
pop_until({ .precedence = precedence_t::CALL_START, .assoc = true }, h, op_stack, res);
call_args_n.back()++;
last_val = false;
}
else if (op == operator_t::PAREN_CLOSE) {
if (op == operator_t::PAREN_CLOSE && (last_val || (!op_stack.empty() && op_stack.back().precedence == precedence_t::CALL_START))) {
bool is_call = false, is_paren = false;
for (auto i = op_stack.rbegin(); i != op_stack.rend(); i++) {
@ -275,6 +252,21 @@ bool ast::parse_exp(ast_ctx_t &ctx, size_t &res_i, map_t &out) {
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);
op_stack.push_back({ h.loc(), { precedence_t::CALL_START } });
last_val = false;
}
else if (op == operator_t::COMMA) {
if (call_args_n.size() == 0) break;
h.advance("Expected an argument.");
pop_until({ .precedence = precedence_t::CALL_START, .assoc = true }, h, op_stack, res);
call_args_n.back()++;
last_val = false;
}
else if (op == operator_t::COLON) {
h.advance("Expected a type.");
pop_until({ .precedence = precedence_t::PREFIX, .assoc = true }, h, op_stack, res);

View File

@ -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);
}

View File

@ -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)) {