From 0c7961ca6b0cfcc9eacf54abda296074117a75b2 Mon Sep 17 00:00:00 2001 From: TopchetoEU <36534413+TopchetoEU@users.noreply.github.com> Date: Tue, 11 Oct 2022 14:39:01 +0300 Subject: [PATCH] feat: add type parsing --- include/compiler/treeifier/ast.hh | 2 + src/compiler/treeifier/ast/parsers/type.cc | 43 ++++++++++++++++++++++ 2 files changed, 45 insertions(+) create mode 100644 src/compiler/treeifier/ast/parsers/type.cc diff --git a/include/compiler/treeifier/ast.hh b/include/compiler/treeifier/ast.hh index ec079b5..47c3443 100644 --- a/include/compiler/treeifier/ast.hh +++ b/include/compiler/treeifier/ast.hh @@ -23,6 +23,7 @@ namespace ppc::comp::tree::ast { extern parser_factory_t glob_parser; extern parser_factory_t identifier_parser; extern parser_factory_t nmsp_parser; + extern parser_factory_t type_parser; extern group_parser_factory_t def_parser; struct ast_ctx_t { @@ -67,6 +68,7 @@ namespace ppc::comp::tree::ast { add_parser(identifier_parser); add_parser(nmsp_parser); add_parser(def_parser); + add_parser(type_parser); return *this; } diff --git a/src/compiler/treeifier/ast/parsers/type.cc b/src/compiler/treeifier/ast/parsers/type.cc new file mode 100644 index 0000000..80bc252 --- /dev/null +++ b/src/compiler/treeifier/ast/parsers/type.cc @@ -0,0 +1,43 @@ +#include "compiler/treeifier/ast/helper.hh" + +class type_parser_t : public parser_t { + bool parse(ast_ctx_t &ctx, size_t &res_i, map_t &out) const { + tree_helper_t h(ctx, res_i); + + if (h.ended()) return false; + + auto &nmsp = (out["namespace"] = map_t()).map(); + nmsp["$_name"] = "$_nmsp"; + auto &nmsp_content = (out["namespace"].map()["content"] = array_t()).array(); + + if (!h.push_parse("$_identifier", nmsp_content)) return false; + + while (true) { + if (h.ended()) break; + if (!h.curr().is_operator(operator_t::DOUBLE_COLON)) break; + h.force_push_parse("$_identifier", "Expected an identifier.", nmsp_content); + } + + out["location"] = conv::loc_to_map(h.res_loc()); + out["name"] = nmsp_content[nmsp_content.size() - 1]; + nmsp_content.pop(); + + if (nmsp_content.size() == 0) { + auto loc = h.res_loc(); + loc.length = 1; + nmsp["location"] = conv::loc_to_map(loc); + } + else { + auto loc_1 = conv::map_to_loc(nmsp_content[0].map()["location"].map()); + auto loc_2 = conv::map_to_loc(nmsp_content[nmsp_content.size() - 1].map()["location"].map()); + auto loc = loc_1.intersect(loc_2); + nmsp["location"] = conv::loc_to_map(loc); + } + + return h.submit(false); + } + + public: type_parser_t(): parser_t("$_type") { } +}; + +parser_factory_t ppc::comp::tree::ast::type_parser = []() { return (parser_t*)new type_parser_t(); };