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(); op_stack.pop_back();
call["location"] = conv::loc_to_map(loc); 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()); args.push_back(res.back());
res.pop_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) { bool ast::parse_exp_var(ast_ctx_t &ctx, size_t &res_i, map_t &out) {
tree_helper_t h(ctx, res_i); return ctx.parse(parse_nmsp, res_i, out);
if (h.curr().is_identifier()) {
out["content"] = h.curr().identifier();
out["location"] = conv::loc_to_map(h.loc());
return h.submit(true);
}
return false;
} }
bool ast::parse_exp_int_lit(ast_ctx_t &ctx, size_t &res_i, map_t &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); tree_helper_t h(ctx, res_i);
@ -240,7 +232,27 @@ bool ast::parse_exp(ast_ctx_t &ctx, size_t &res_i, map_t &out) {
} }
if (h.curr().is_operator()) { if (h.curr().is_operator()) {
auto op = h.curr()._operator(); auto op = h.curr()._operator();
if (last_val) { 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++) {
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 (last_val) {
if (op == operator_t::PAREN_OPEN) { if (op == operator_t::PAREN_OPEN) {
h.advance("Expected an argument."); h.advance("Expected an argument.");
call_args_n.push_back(0); 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()++; call_args_n.back()++;
last_val = false; 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) { else if (op == operator_t::COLON) {
h.advance("Expected a type."); h.advance("Expected a type.");
pop_until({ .precedence = precedence_t::PREFIX, .assoc = true }, h, op_stack, res); 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) { while (true) {
if (h.ended()) break; if (h.ended()) break;
if (!h.curr().is_operator(operator_t::DOUBLE_COLON)) 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); 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) { bool ast::parse_if(ast_ctx_t &ctx, size_t &res_i, map_t &out) {
tree_helper_t h(ctx, res_i); 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)) { 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)); 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) { bool ast::parse_while(ast_ctx_t &ctx, size_t &res_i, map_t &out) {
tree_helper_t h(ctx, res_i); 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)) { 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)); 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) { bool ast::parse_return(ast_ctx_t &ctx, size_t &res_i, map_t &out) {
tree_helper_t h(ctx, res_i); 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({})); h.force_parse(parse_exp, "Expected an expression.", out["condition"].map({}));
if (!h.curr("Expected a semicolon.").is_operator(operator_t::SEMICOLON)) { if (!h.curr("Expected a semicolon.").is_operator(operator_t::SEMICOLON)) {