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:
|
public:
|
||||||
msg_stack_t &messages;
|
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, parser_t &parser);
|
||||||
void add_parser(std::string name, group_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 parser_proxy_t parser;
|
||||||
const group_proxy_t group;
|
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),
|
messages(messages),
|
||||||
tokens(tokens),
|
tokens(tokens),
|
||||||
parser(*this),
|
parser(*this),
|
||||||
|
@ -6,6 +6,10 @@ namespace ppc::comp::tree::ast {
|
|||||||
ast_ctx_t &ctx;
|
ast_ctx_t &ctx;
|
||||||
size_t &res_i;
|
size_t &res_i;
|
||||||
size_t i;
|
size_t i;
|
||||||
|
|
||||||
|
void throw_ended() {
|
||||||
|
if (ended()) throw messages::message_t(message_t::ERROR, "Unexpected end.", loc());
|
||||||
|
}
|
||||||
public:
|
public:
|
||||||
void submit() {
|
void submit() {
|
||||||
res_i = i;
|
res_i = i;
|
||||||
@ -15,7 +19,10 @@ namespace ppc::comp::tree::ast {
|
|||||||
return i == ctx.tokens.size();
|
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 next_loc(size_t n = 1) {
|
||||||
location_t res = loc();
|
location_t res = loc();
|
||||||
@ -39,6 +46,11 @@ namespace ppc::comp::tree::ast {
|
|||||||
else return curr().location;
|
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) {
|
bool try_parse(const parser_t &parser, data::map_t &out, messages::msg_stack_t &messages) {
|
||||||
try {
|
try {
|
||||||
return parser(ctx, i, out);
|
return parser(ctx, i, out);
|
||||||
@ -62,10 +74,10 @@ namespace ppc::comp::tree::ast {
|
|||||||
i++;
|
i++;
|
||||||
return !ended();
|
return !ended();
|
||||||
}
|
}
|
||||||
bool advance() {
|
void advance() {
|
||||||
if (ended()) throw messages::message_t(message_t::ERROR, "Unexpected end.", loc());
|
throw_ended();
|
||||||
i++;
|
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) {
|
tree_helper_t(ast_ctx_t &ctx, size_t &i): ctx(ctx), res_i(i) {
|
||||||
|
@ -2,6 +2,7 @@
|
|||||||
#include "compiler/treeifier/tokenizer.hh"
|
#include "compiler/treeifier/tokenizer.hh"
|
||||||
|
|
||||||
using namespace ppc::comp::tree;
|
using namespace ppc::comp::tree;
|
||||||
|
using namespace ppc::comp;
|
||||||
using namespace std::string_literals;
|
using namespace std::string_literals;
|
||||||
|
|
||||||
|
|
||||||
@ -17,15 +18,14 @@ std::vector<std::string> operators = {
|
|||||||
"[", "]", "{", "}", "(", ")"
|
"[", "]", "{", "}", "(", ")"
|
||||||
};
|
};
|
||||||
|
|
||||||
|
const std::string &tree::operator_stringify(operator_t op) {
|
||||||
const std::string &tok::operator_stringify(tok::operator_t kw) {
|
if (op < 0 || op >= operators.size()) throw "Invalid operator ID given."s;
|
||||||
if (kw < 0 || kw >= operators.size()) throw "Invalid operator ID given."s;
|
return operators[op];
|
||||||
return operators[kw];
|
|
||||||
}
|
}
|
||||||
tok::operator_t tok::operator_find(const std::string &raw) {
|
operator_t tree::operator_find(const std::string &raw) {
|
||||||
std::size_t i = 0;
|
std::size_t i = 0;
|
||||||
for (const auto &op : operators) {
|
for (const auto &op : operators) {
|
||||||
if (op == raw) return (tok::operator_t)i;
|
if (op == raw) return (operator_t)i;
|
||||||
i++;
|
i++;
|
||||||
}
|
}
|
||||||
throw "Invalid operator '"s + raw + "' given.";
|
throw "Invalid operator '"s + raw + "' given.";
|
||||||
|
Loading…
Reference in New Issue
Block a user