chore: move tokenizer.hh to ppc::comp::tree
This commit is contained in:
parent
75af4cd77f
commit
3c08cd13db
@ -41,7 +41,7 @@ namespace ppc::comp::tree::ast {
|
||||
|
||||
public:
|
||||
msg_stack_t &messages;
|
||||
std::vector<tok::token_t> &tokens;
|
||||
std::vector<token_t> &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<tok::token_t> tokens):
|
||||
ast_ctx_t(msg_stack_t &messages, std::vector<token_t> tokens):
|
||||
messages(messages),
|
||||
tokens(tokens),
|
||||
parser(*this),
|
||||
|
@ -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) {
|
||||
|
@ -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<std::string> 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.";
|
||||
|
Loading…
Reference in New Issue
Block a user