diff --git a/include/compiler/treeifier/ast.hh b/include/compiler/treeifier/ast.hh index f779f38..f49b59f 100644 --- a/include/compiler/treeifier/ast.hh +++ b/include/compiler/treeifier/ast.hh @@ -41,7 +41,7 @@ namespace ppc::comp::tree::ast { public: msg_stack_t &messages; - std::vector &tokens; + std::vector &tokens; void add_parser(std::string name, parser_t &parser); void add_parser(std::string name, group_parser_t &parser); @@ -49,7 +49,7 @@ namespace ppc::comp::tree::ast { const parser_proxy_t parser; const group_proxy_t group; - ast_ctx_t(msg_stack_t &messages, std::vector tokens): + ast_ctx_t(msg_stack_t &messages, std::vector tokens): messages(messages), tokens(tokens), parser(*this), diff --git a/include/compiler/treeifier/ast/helper.hh b/include/compiler/treeifier/ast/helper.hh index 9d64d7e..3fe8348 100644 --- a/include/compiler/treeifier/ast/helper.hh +++ b/include/compiler/treeifier/ast/helper.hh @@ -6,6 +6,10 @@ namespace ppc::comp::tree::ast { 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()); + } public: void submit() { res_i = i; @@ -15,7 +19,10 @@ namespace ppc::comp::tree::ast { return i == ctx.tokens.size(); } - tok::token_t &curr() { return ctx.tokens[i]; } + token_t &curr() { + throw_ended(); + return ctx.tokens[i]; + } location_t next_loc(size_t n = 1) { location_t res = loc(); @@ -39,6 +46,11 @@ namespace ppc::comp::tree::ast { 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()); + } + bool try_parse(const parser_t &parser, data::map_t &out, messages::msg_stack_t &messages) { try { return parser(ctx, i, out); @@ -62,10 +74,10 @@ namespace ppc::comp::tree::ast { i++; return !ended(); } - bool advance() { - if (ended()) throw messages::message_t(message_t::ERROR, "Unexpected end.", loc()); + void advance() { + throw_ended(); i++; - if (ended()) throw messages::message_t(message_t::ERROR, "Unexpected end.", loc()); + throw_ended(); } tree_helper_t(ast_ctx_t &ctx, size_t &i): ctx(ctx), res_i(i) { diff --git a/src/compiler/treeifier/operators.cc b/src/compiler/treeifier/operators.cc index 4d560c4..06df236 100644 --- a/src/compiler/treeifier/operators.cc +++ b/src/compiler/treeifier/operators.cc @@ -2,6 +2,7 @@ #include "compiler/treeifier/tokenizer.hh" using namespace ppc::comp::tree; +using namespace ppc::comp; using namespace std::string_literals; @@ -17,15 +18,14 @@ std::vector operators = { "[", "]", "{", "}", "(", ")" }; - -const std::string &tok::operator_stringify(tok::operator_t kw) { - if (kw < 0 || kw >= operators.size()) throw "Invalid operator ID given."s; - return operators[kw]; +const std::string &tree::operator_stringify(operator_t op) { + if (op < 0 || op >= operators.size()) throw "Invalid operator ID given."s; + return operators[op]; } -tok::operator_t tok::operator_find(const std::string &raw) { +operator_t tree::operator_find(const std::string &raw) { std::size_t i = 0; for (const auto &op : operators) { - if (op == raw) return (tok::operator_t)i; + if (op == raw) return (operator_t)i; i++; } throw "Invalid operator '"s + raw + "' given.";