ppc-lang/include/compiler/treeifier/ast/helper.hh

87 lines
2.2 KiB
C++
Raw Normal View History

2022-10-04 13:00:18 +00:00
#include "compiler/treeifier/ast.hh"
namespace ppc::comp::tree::ast {
struct tree_helper_t {
private:
ast_ctx_t &ctx;
size_t &res_i;
size_t i;
void throw_ended() {
if (ended()) throw messages::message_t(message_t::ERROR, "Unexpected end.", loc());
}
2022-10-04 13:00:18 +00:00
public:
void submit() {
res_i = i;
}
bool ended() {
return i == ctx.tokens.size();
}
token_t &curr() {
throw_ended();
return ctx.tokens[i];
}
2022-10-04 13:00:18 +00:00
location_t next_loc(size_t n = 1) {
location_t res = loc();
res.start += res.length;
res.code_start += res.length;
res.length = n;
return res;
}
location_t loc() {
if (ended()) {
if (i == 0) return location_t::NONE;
location_t loc = ctx.tokens[i - 1].location;
loc.start += loc.length;
loc.code_start += loc.length;
loc.length = 1;
return loc;
}
else return curr().location;
}
location_t res_loc() {
if (res_i >= ctx.tokens.size()) return loc();
else return ctx.tokens[res_i].location.intersect(loc());
}
2022-10-04 13:00:18 +00:00
bool try_parse(const parser_t &parser, data::map_t &out, messages::msg_stack_t &messages) {
try {
return parser(ctx, i, out);
}
catch (messages::message_t msg) {
messages.push(msg);
return false;
}
}
bool try_parse(const parser_t &parser, data::map_t &out) {
try {
return parser(ctx, i, out);
}
catch (messages::message_t msg) {
return false;
}
}
bool try_advance() {
if (ended()) return false;
i++;
return !ended();
}
void advance() {
throw_ended();
2022-10-04 13:00:18 +00:00
i++;
throw_ended();
2022-10-04 13:00:18 +00:00
}
tree_helper_t(ast_ctx_t &ctx, size_t &i): ctx(ctx), res_i(i) {
this->i = i;
}
};
}