From 2461ed1860090d08aa64df028e0579a9d5446c57 Mon Sep 17 00:00:00 2001 From: topchetoeu <36534413+TopchetoEU@users.noreply.github.com> Date: Thu, 26 Jan 2023 10:06:39 +0200 Subject: [PATCH] chore: a lot of file restructuring --- include/lang/common.hh | 85 ++++++------- include/lang/module.hh | 162 ------------------------- include/treeifier/constr.hh | 79 +++++------- include/treeifier/constr/glob.hh | 9 -- include/treeifier/constr/helper.hh | 21 ++-- include/treeifier/constr/identifier.hh | 8 -- include/treeifier/constr/inspoint.hh | 51 ++++++++ include/treeifier/constr/nmsp.hh | 9 -- include/utils/threading.hh | 14 +-- src/lang/common.cc | 12 +- src/lang/module.cc | 25 +--- src/main.proj | 2 +- src/main/main.cc | 4 +- src/treeifier/constr.cc | 48 ++++++++ src/treeifier/constr/glob.cc | 9 +- src/treeifier/constr/identifier.cc | 16 --- src/treeifier/constr/nmsp.cc | 26 ---- 17 files changed, 199 insertions(+), 381 deletions(-) delete mode 100644 include/lang/module.hh delete mode 100644 include/treeifier/constr/glob.hh delete mode 100644 include/treeifier/constr/identifier.hh create mode 100644 include/treeifier/constr/inspoint.hh delete mode 100644 include/treeifier/constr/nmsp.hh create mode 100644 src/treeifier/constr.cc delete mode 100644 src/treeifier/constr/identifier.cc delete mode 100644 src/treeifier/constr/nmsp.cc diff --git a/include/lang/common.hh b/include/lang/common.hh index ba966b9..0f3ec89 100644 --- a/include/lang/common.hh +++ b/include/lang/common.hh @@ -4,29 +4,29 @@ #include namespace ppc::lang { - struct namespace_name_t: public std::vector { + struct nmsp_t: public std::vector { using base = std::vector; - int compare(const namespace_name_t &other) const; + int compare(const nmsp_t &other) const; - bool operator==(const namespace_name_t &other) const { return compare(other) == 0; } - bool operator!=(const namespace_name_t &other) const { return compare(other) != 0; } - bool operator<(const namespace_name_t &other) const { return compare(other) < 0; } - bool operator<=(const namespace_name_t &other) const { return compare(other) <= 0; } - bool operator>(const namespace_name_t &other) const { return compare(other) > 0; } - bool operator>=(const namespace_name_t &other) const { return compare(other) >= 0; } + bool operator==(const nmsp_t &other) const { return compare(other) == 0; } + bool operator!=(const nmsp_t &other) const { return compare(other) != 0; } + bool operator<(const nmsp_t &other) const { return compare(other) < 0; } + bool operator<=(const nmsp_t &other) const { return compare(other) <= 0; } + bool operator>(const nmsp_t &other) const { return compare(other) > 0; } + bool operator>=(const nmsp_t &other) const { return compare(other) >= 0; } operator std::string() const { return to_string(); } std::string to_string() const; - namespace_name_t() { } - namespace_name_t(std::initializer_list segments): base(segments.begin(), segments.end()) { } + nmsp_t() { } + nmsp_t(std::initializer_list segments): base(segments.begin(), segments.end()) { } }; } template <> -struct std::hash { - std::size_t operator()(const ppc::lang::namespace_name_t& k) const { +struct std::hash { + std::size_t operator()(const ppc::lang::nmsp_t& k) const { size_t res = 0; for (auto &el : k) { @@ -41,55 +41,46 @@ struct std::hash { #include "utils/location.hh" namespace ppc::lang { - template - struct located_t: T { + template + struct located_t: ParserT { location_t location; - located_t(location_t loc, const T &val): T(val), location(loc) { } - located_t(const T &val): T(val), location(location_t::NONE) { } + located_t(location_t loc, const ParserT &val): ParserT(val), location(loc) { } + located_t(const ParserT &val): ParserT(val), location(location_t::NONE) { } located_t() { } }; - template - struct slocated_t { - T value; - location_t location; - bool operator ==(const slocated_t &other) { - return value == other.value && location == other.location; - } - bool operator !=(const slocated_t &other) { - return !(*this == other); - } - - slocated_t(location_t loc, const T &val): value(val), location(loc) { } - slocated_t(const T &val): value(val), location(location_t::NONE) { } - slocated_t() { } - }; - - struct loc_namespace_name_t: public std::vector> { + struct loc_nmsp_t: public std::vector> { using base = std::vector>; - int compare(const loc_namespace_name_t &other) const; + int compare(const loc_nmsp_t &other) const; - bool operator==(const loc_namespace_name_t &other) const { return compare(other) == 0; } - bool operator!=(const loc_namespace_name_t &other) const { return compare(other) != 0; } - bool operator<(const loc_namespace_name_t &other) const { return compare(other) < 0; } - bool operator<=(const loc_namespace_name_t &other) const { return compare(other) <= 0; } - bool operator>(const loc_namespace_name_t &other) const { return compare(other) > 0; } - bool operator>=(const loc_namespace_name_t &other) const { return compare(other) >= 0; } + bool operator==(const loc_nmsp_t &other) const { return compare(other) == 0; } + bool operator!=(const loc_nmsp_t &other) const { return compare(other) != 0; } + bool operator<(const loc_nmsp_t &other) const { return compare(other) < 0; } + bool operator<=(const loc_nmsp_t &other) const { return compare(other) <= 0; } + bool operator>(const loc_nmsp_t &other) const { return compare(other) > 0; } + bool operator>=(const loc_nmsp_t &other) const { return compare(other) >= 0; } - namespace_name_t strip_location() const; + nmsp_t strip_location() const; + operator nmsp_t() { return strip_location(); } operator std::string() const { return to_string(); } std::string to_string() const; - loc_namespace_name_t() { } - loc_namespace_name_t(std::initializer_list> segments): base(segments.begin(), segments.end()) { } + loc_nmsp_t() { } + loc_nmsp_t(std::initializer_list> segments): base(segments.begin(), segments.end()) { } }; - bool is_identifier_valid(messages::msg_stack_t &msg_stack, ppc::location_t location, const std::string &name); - inline bool is_identifier_valid(const std::string &name) { - messages::msg_stack_t ms; - return is_identifier_valid(ms, { }, name); + template + bool resolve_name(const SetT &defs, const nmsp_t &src, const nmsp_t &target) { + if (src == target) return true; + + for (auto &it : defs) { + nmsp_t val = (nmsp_t)it; + val.insert(val.end(), src.begin(), src.end()); + + if (val == target) return true; + } } } \ No newline at end of file diff --git a/include/lang/module.hh b/include/lang/module.hh deleted file mode 100644 index 8205604..0000000 --- a/include/lang/module.hh +++ /dev/null @@ -1,162 +0,0 @@ -#include -#include -#include "lang/common.hh" - -namespace ppc::lang { - struct type_t { - namespace_name_t name; - size_t ptr_n; - }; - - struct statement_t { - private: - enum kind_t { - CALL, - STACK, - RETURN, - } kind; - union val_t { - namespace_name_t *call; - int64_t stack; - } val; - - ~statement_t(); - - statement_t(kind_t kind, val_t val) { - this->kind = kind; - this->val = val; - } - statement_t(kind_t kind) { - this->kind = kind; - } - - public: - bool is_call() const { return kind == CALL; } - bool is_stack() const { return kind == STACK; } - bool is_return() const { return kind == RETURN; } - - auto &call() const { - if (!is_call()) throw (std::string)"Statement is not a call."; - return val.call; - } - auto stack() const { - if (!is_call()) throw (std::string)"Statement is not a stack."; - return val.stack; - } - - static statement_t call(const namespace_name_t &func); - static statement_t stack(int64_t stack); - static statement_t ret(); - }; - - struct field_t { - type_t type; - }; - struct struct_t { - std::unordered_map fields; - }; - struct function_t { - std::unordered_map args; - type_t type; - - std::string get_signature(); - }; - - struct definition_t { - private: - enum { - FUNCTION, - STRUCT, - FIELD, - } kind; - union { - field_t *field; - struct_t *str; - function_t *func; - } val; - - public: - ~definition_t(); - definition_t(field_t val); - definition_t(struct_t val); - definition_t(function_t val); - definition_t(const definition_t &other); - - bool is_func() const { return kind == FUNCTION; } - bool is_struct() const { return kind == STRUCT; } - bool is_field() const { return kind == FIELD; } - - function_t &get_func(); - struct_t &get_struct(); - field_t &get_field(); - - const function_t &get_func() const { return ((definition_t&)*this).get_func(); } - const struct_t &get_struct() const { return ((definition_t&)*this).get_struct(); } - const field_t &get_field() const { return ((definition_t&)*this).get_field(); } - }; - - struct module_t { - private: - using fields_t = std::unordered_map; - using structs_t = std::unordered_map; - using funcs_t = std::unordered_map; - struct resolve_res_t { - namespace_name_t name; - definition_t def; - }; - public: - fields_t fields; - structs_t structs; - funcs_t funcs; - - const definition_t &def(namespace_name_t name); - void add_def(namespace_name_t name, const definition_t &def) { - if (def.is_field()) fields.emplace(name, def.get_field()); - if (def.is_func()) funcs.emplace(name, def.get_func()); - if (def.is_struct()) structs.emplace(name, def.get_struct()); - } - bool exists(namespace_name_t name) { - return - fields.find(name) != fields.end() || - structs.find(name) != structs.end() || - funcs.find(name) != funcs.end(); - } - }; - - template - struct resolve_res_t { - namespace_name_t name; - T value; - }; - - - bool resolve_name( - const std::vector &names, const std::set &imports, - const namespace_name_t &name, namespace_name_t &res - ); - - template - bool resolve_name_map( - const MapT &defs, const std::set &imports, - const namespace_name_t &name, namespace_name_t &res - ) { - std::vector names; - for (auto &it : defs) { - const namespace_name_t &val = it.first; - names.push_back(val); - } - return resolve_name(names, imports, name, res); - } - - template - bool resolve( - const MapT &defs, const std::set &imports, - const namespace_name_t &name, typename MapT::iterator &res - ) { - if (resolve_name_map(defs, imports, name, res.name)) { - res.value = defs.find(res.name)->second; - return true; - } - return false; - } -} \ No newline at end of file diff --git a/include/treeifier/constr.hh b/include/treeifier/constr.hh index cd8d39a..2278ccb 100644 --- a/include/treeifier/constr.hh +++ b/include/treeifier/constr.hh @@ -20,20 +20,27 @@ namespace ppc::tree::constr { struct ast_ctx_t; struct glob_t { - loc_namespace_name_t nmsp; - std::vector imports; + loc_nmsp_t nmsp; + std::vector imports; + + #ifdef PROFILE_debug + void print() const { + std::cout << "Namespace: " << nmsp.to_string() << "\n"; + std::cout << "Imports:\n"; + for (auto &imp : imports) { + std::cout << " - " << imp.to_string() << "\n"; + } + } + #endif + }; + struct exp_t: public named_t { }; - template + template class parser_t { public: - virtual bool operator()(ast_ctx_t &ctx, size_t &res_i, T &out) const = 0; - virtual bool simplify(ast_ctx_t &ctx, GlobT &glob, T &val) const = 0; -#ifdef PROFILE_debug - virtual void print(const T &val) { - std::cout << "(unknown)"; - } -#endif + virtual bool operator()(ast_ctx_t &ctx, size_t &res_i, ParserT &out) const = 0; + virtual bool simplify(ast_ctx_t &ctx, GlobT &glob, ParserT &val) const = 0; }; @@ -41,50 +48,24 @@ namespace ppc::tree::constr { public: msg_stack_t &messages; std::vector &tokens; - loc_namespace_name_t nmsp; + loc_nmsp_t nmsp; ast_ctx_t &operator=(const ast_ctx_t &other) = delete; - - template - bool parse(const parser_t &parser, size_t &i, T &out) { - return parser(*this, i, out); - } - - template - static T parse(const parser_t &glob, msg_stack_t &messages, std::vector &tokens) { - ast_ctx_t ctx(messages, tokens); - T res; - size_t i = 0; - - if (!ctx.parse(glob, i, res)) throw message_t::error("Failed to compile."); - return res; - } - - ast_ctx_t(msg_stack_t &messages, std::vector &tokens); + ast_ctx_t(msg_stack_t &messages, std::vector &tokens): + messages(messages), tokens(tokens) { } }; - template - class inspoint_t { - private: - std::map named_parsers; - std::set unnamed_parsers; - std::map parsers; + bool parse_identifier(ast_ctx_t &ctx, size_t &res_i, located_t &out); + bool parse_nmsp(ast_ctx_t &ctx, size_t &res_i, loc_nmsp_t &out); + bool parse_nmsp_id(ast_ctx_t &ctx, size_t &res_i, nmsp_t nmsp); + + class exp_parser_t { public: - inspoint_t &replace(const std::string &name, const T &parser) { - auto it = parsers.find(name); - - if (parsers.find(name) == parsers.end()) { - throw "The parser '" + name + "' isn't in the group."; - } - - it->second = parser; - - return *this; - } - inspoint_t &add(const std::string &name, const T &parser); - inspoint_t &add(const std::string &name, const lang::namespace_name_t &identifier, const T &parser); - - bool operator()(ast_ctx_t &ctx, size_t &i, data::map_t &out) const; + bool operator()(ast_ctx_t &ctx, glob_t &out) const; + }; + class glob_parser_t { + public: + bool operator()(ast_ctx_t &ctx, glob_t &out) const; }; // parser_func_t parse_glob, parse_nmsp, parse_identifier, parse_type, parse_exp, parse_stat_exp; diff --git a/include/treeifier/constr/glob.hh b/include/treeifier/constr/glob.hh deleted file mode 100644 index 9303979..0000000 --- a/include/treeifier/constr/glob.hh +++ /dev/null @@ -1,9 +0,0 @@ -#include "treeifier/constr.hh" - -namespace ppc::tree::constr { - class glob_parser_t: public parser_t { - public: - bool operator()(ast_ctx_t &ctx, size_t &res_i, glob_t &out) const override; - bool simplify(ast_ctx_t &ctx, glob_t &glob, glob_t &val) const override { return false; } - }; -} \ No newline at end of file diff --git a/include/treeifier/constr/helper.hh b/include/treeifier/constr/helper.hh index c91f3a9..bfb7186 100644 --- a/include/treeifier/constr/helper.hh +++ b/include/treeifier/constr/helper.hh @@ -7,7 +7,7 @@ using namespace ppc::tree; using namespace ppc::tree::constr; namespace ppc::tree::constr { - struct parse_helper_t { + struct helper_t { private: ast_ctx_t &ctx; size_t &res_i; @@ -104,28 +104,25 @@ namespace ppc::tree::constr { throw_ended(reason); } - template - bool parse(const parser_t &parser, T &out) { - return ctx.parse(parser, i, out); + template + bool parse(const ParserT &parser, ArgsT &...args) { + return parser(ctx, i, args...); } - template - void force_parse(const parser_t &parser, std::string message, T &out) { + template + void force_parse(const ParserT &parser, std::string message, ArgsT &...args) { throw_ended(message); - bool success; try { - success = parse(parser, out); + if (!parser(ctx, i, args...)) err(message); } catch (const message_t &msg) { ctx.messages.push(msg); - success = false; + err(message); } - - if (!success) err(message); } - parse_helper_t(ast_ctx_t &ctx, size_t &i): ctx(ctx), res_i(i) { + helper_t(ast_ctx_t &ctx, size_t &i): ctx(ctx), res_i(i) { this->i = i; } }; diff --git a/include/treeifier/constr/identifier.hh b/include/treeifier/constr/identifier.hh deleted file mode 100644 index b16e615..0000000 --- a/include/treeifier/constr/identifier.hh +++ /dev/null @@ -1,8 +0,0 @@ -#include "treeifier/constr.hh" - -namespace ppc::tree::constr { - struct identifier_parser_t: public parser_t> { - bool operator()(ast_ctx_t &ctx, size_t &res_i, located_t &out) const override; - bool simplify(ast_ctx_t &ctx, glob_t &glob, located_t &val) const override { return false; } - }; -} \ No newline at end of file diff --git a/include/treeifier/constr/inspoint.hh b/include/treeifier/constr/inspoint.hh new file mode 100644 index 0000000..96bb632 --- /dev/null +++ b/include/treeifier/constr/inspoint.hh @@ -0,0 +1,51 @@ +#include "treeifier/constr.hh" +#include "treeifier/constr/helper.hh" +#include "treeifier/constr/nmsp.hh" + +namespace ppc::tree::constr { + struct named_t { + virtual std::string name() = 0; + virtual ~named_t() = default; + + #ifdef PROFILE_debug + virtual void print() { std::cout << "(" << name() << ")"; } + #endif + }; + + template + class inspoint_t { + private: + std::map> parsers; + public: + inspoint_t &add(const ParserT &parser, bool replace = false) { + const std::string &name = parser.name(); + auto it = parsers.find(name); + if (it != parsers.end()) { + if (!replace) throw "The parser '" + name + "' is already in the group."; + it->second = std::make_unique(parser); + } + else { + parsers.emplace(name, std::make_unique(parser)); + } + + return *this; + } + + bool operator()(ast_ctx_t &ctx, size_t &i, std::unique_ptr &out) const override { + helper_t h(ctx, i); + + if (h.ended()) return false; + + for (std::pair> &pair : parsers) { + ResT res; + + if (pair.second(ctx, i, res)) { + out = std::make_unique(res); + return true; + } + } + + return false; + } + }; +} \ No newline at end of file diff --git a/include/treeifier/constr/nmsp.hh b/include/treeifier/constr/nmsp.hh deleted file mode 100644 index 0c10a85..0000000 --- a/include/treeifier/constr/nmsp.hh +++ /dev/null @@ -1,9 +0,0 @@ -#include "treeifier/constr/identifier.hh" -#include "treeifier/constr.hh" - -namespace ppc::tree::constr { - struct nmsp_parser_t: public parser_t { - bool operator()(ast_ctx_t &ctx, size_t &res_i, loc_namespace_name_t &out) const override; - bool simplify(ast_ctx_t &ctx, glob_t &glob, loc_namespace_name_t &val) const override { return false; } - }; -} \ No newline at end of file diff --git a/include/utils/threading.hh b/include/utils/threading.hh index 51a7a5a..8d50a7a 100644 --- a/include/utils/threading.hh +++ b/include/utils/threading.hh @@ -7,8 +7,8 @@ #endif namespace ppc::threading { - template - using thread_func_t = int (THREAD *)(T *data); + template + using thread_func_t = int (THREAD *)(ParserT *data); using empty_thread_func_t = int (THREAD *)(); struct thread_t { @@ -20,13 +20,13 @@ namespace ppc::threading { int join() const; thread_t(void *handle) { this->handle = handle; } - template - inline static thread_t start(thread_func_t func, const T &args) { - T _args = args; + template + inline static thread_t start(thread_func_t func, const ParserT &args) { + ParserT _args = args; return start_impl((void*)func, &_args); } - template - inline static thread_t start(thread_func_t func, T &args) { + template + inline static thread_t start(thread_func_t func, ParserT &args) { return start_impl((void*)func, &args); } inline static thread_t start(empty_thread_func_t func) { diff --git a/src/lang/common.cc b/src/lang/common.cc index 7c5a004..d4cbf5c 100644 --- a/src/lang/common.cc +++ b/src/lang/common.cc @@ -2,7 +2,7 @@ #include namespace ppc::lang { - std::string loc_namespace_name_t::to_string() const { + std::string loc_nmsp_t::to_string() const { std::stringstream res; for (size_t i = 0; i < size(); i++) { @@ -12,7 +12,7 @@ namespace ppc::lang { return res.str(); } - std::string namespace_name_t::to_string() const { + std::string nmsp_t::to_string() const { std::stringstream res; for (size_t i = 0; i < size(); i++) { @@ -23,7 +23,7 @@ namespace ppc::lang { return res.str(); } - int namespace_name_t::compare(const namespace_name_t &b) const { + int nmsp_t::compare(const nmsp_t &b) const { const auto &a = *this; for (size_t i = 0; i < a.size() && i < b.size(); i++) { auto cmp = a[i].compare(b[i]); @@ -34,7 +34,7 @@ namespace ppc::lang { else if (a.size() == b.size()) return 0; else return -1; } - int loc_namespace_name_t::compare(const loc_namespace_name_t &b) const { + int loc_nmsp_t::compare(const loc_nmsp_t &b) const { const auto &a = *this; for (size_t i = 0; i < a.size() && i < b.size(); i++) { auto cmp = a[i].compare(b[i]); @@ -46,8 +46,8 @@ namespace ppc::lang { else return -1; } - namespace_name_t loc_namespace_name_t::strip_location() const { - namespace_name_t res; + nmsp_t loc_nmsp_t::strip_location() const { + nmsp_t res; for (const auto &el : *this) { res.push_back(el); diff --git a/src/lang/module.cc b/src/lang/module.cc index 1b91993..123f3c8 100644 --- a/src/lang/module.cc +++ b/src/lang/module.cc @@ -59,7 +59,7 @@ field_t &definition_t::get_field() { } -statement_t statement_t::call(const namespace_name_t &func) { +statement_t statement_t::call(const nmsp_t &func) { return { CALL, { .call = new auto(func) } }; } statement_t statement_t::stack(int64_t stack) { @@ -68,26 +68,3 @@ statement_t statement_t::stack(int64_t stack) { statement_t statement_t::ret() { return { RETURN }; } - -bool ppc::lang::resolve_name( - const std::vector &names, const std::set &imports, - const namespace_name_t &name, namespace_name_t &res -) { - for (auto &curr : names) { - if (curr == name) { - res = curr; - return true; - } - } - for (auto &import : imports) { - auto new_name = name; - new_name.insert(new_name.begin(), import.begin(), import.end()); - for (auto &curr : names) { - if (curr == new_name) { - res = curr; - return true; - } - } - } - return false; -} \ No newline at end of file diff --git a/src/main.proj b/src/main.proj index ba8a6bc..3b14ac7 100644 --- a/src/main.proj +++ b/src/main.proj @@ -1,2 +1,2 @@ main -utils, treeifier \ No newline at end of file +utils, treeifier, lang \ No newline at end of file diff --git a/src/main/main.cc b/src/main/main.cc index ce28519..a920688 100644 --- a/src/main/main.cc +++ b/src/main/main.cc @@ -164,7 +164,9 @@ int main(int argc, const char *argv[]) { if (!f.is_open()) throw message_t::error("The file doesn't exist.", { file }); auto tokens = token_t::parse_many(msg_stack, lex::token_t::parse_file(msg_stack, file, f)); auto ast = ast_ctx_t::parse(constr::glob_parser_t(), msg_stack, tokens); - // std::cout << data::json::stringify(ast) << std::endl; + #ifdef PROFILE_debug + ast.print(); + #endif } catch (const messages::message_t &msg) { msg_stack.push(msg); diff --git a/src/treeifier/constr.cc b/src/treeifier/constr.cc new file mode 100644 index 0000000..bb5af9e --- /dev/null +++ b/src/treeifier/constr.cc @@ -0,0 +1,48 @@ +#include "treeifier/constr.hh" +#include "treeifier/constr/helper.hh" +#include "lang/common.hh" + +using namespace ppc::tree::constr; + +bool ppc::tree::constr::parse_identifier(ast_ctx_t &ctx, size_t &res_i, located_t &out) { + helper_t h(ctx, res_i); + + if (h.ended()) return false; + + if (h.curr().is_identifier()) { + out = located_t(h.loc(), h.curr().identifier()); + return h.submit(); + } + else return false; +} + +bool ppc::tree::constr::parse_nmsp(ast_ctx_t &ctx, size_t &res_i, loc_nmsp_t &out) { + helper_t h(ctx, res_i); + + if (h.ended()) return false; + + out.clear(); + located_t val; + + if (!h.parse(parse_identifier, val)) return false; + else out.push_back(val); + + while (true) { + if (h.ended()) break; + if (!h.curr().is_operator(operator_t::DOUBLE_COLON)) break; + h.advance("Expected an identifier."); + h.force_parse(parse_identifier, "Expected an identifier.", val); + out.push_back(val); + } + + return h.submit(false); +} + +bool ppc::tree::constr::parse_nmsp_id(ast_ctx_t &ctx, size_t &res_i, glob_t glob, nmsp_t nmsp) { + helper_t h(ctx, res_i); + nmsp_t src; + + if (!h.parse(parse_nmsp, src)) return false; + if (resolve_name(glob.imports, src, nmsp)) return h.submit(false); + else return false; +} diff --git a/src/treeifier/constr/glob.cc b/src/treeifier/constr/glob.cc index cc23fa6..5dfead8 100644 --- a/src/treeifier/constr/glob.cc +++ b/src/treeifier/constr/glob.cc @@ -4,15 +4,16 @@ using namespace ppc::tree::constr; -bool ppc::tree::constr::glob_parser_t::operator()(ast_ctx_t &ctx, size_t &res_i, glob_t &out) const { - parse_helper_t h(ctx, res_i); +bool ppc::tree::constr::glob_parser_t::operator()(ast_ctx_t &ctx, glob_t &out) const { + size_t res_i = 0; + helper_t h(ctx, res_i); out = {}; if (h.ended()) return h.submit(false); if (h.curr().is_identifier("namespace")) { h.advance("Expected a namespace"); - h.force_parse(nmsp_parser_t(), "Expected a namespace.", out.nmsp); + h.force_parse(parse_nmsp, "Expected a namespace.", out.nmsp); if (!h.curr().is_operator(operator_t::SEMICOLON)) { ctx.messages.push(message_t::error("Expected a semicolon.", h.loc(1))); @@ -22,7 +23,7 @@ bool ppc::tree::constr::glob_parser_t::operator()(ast_ctx_t &ctx, size_t &res_i, } while (h.curr().is_identifier("import")) { - loc_namespace_name_t res; + loc_nmsp_t res; h.advance("Expected a namespace"); h.force_parse(nmsp_parser_t(), "Expected a namespace.", res); diff --git a/src/treeifier/constr/identifier.cc b/src/treeifier/constr/identifier.cc deleted file mode 100644 index 695ec42..0000000 --- a/src/treeifier/constr/identifier.cc +++ /dev/null @@ -1,16 +0,0 @@ -#include "treeifier/constr/helper.hh" -#include "treeifier/constr/identifier.hh" - -using namespace ppc::tree::constr; - -bool identifier_parser_t::operator()(ast_ctx_t& ctx, size_t& res_i, located_t& out) const { - parse_helper_t h(ctx, res_i); - - if (h.ended()) return false; - - if (h.curr().is_identifier()) { - out = located_t(h.loc(), h.curr().identifier()); - return h.submit(); - } - else return false; -} diff --git a/src/treeifier/constr/nmsp.cc b/src/treeifier/constr/nmsp.cc deleted file mode 100644 index 942600a..0000000 --- a/src/treeifier/constr/nmsp.cc +++ /dev/null @@ -1,26 +0,0 @@ -#include "treeifier/constr/helper.hh" -#include "treeifier/constr/nmsp.hh" - -using namespace ppc::tree::constr; - -bool nmsp_parser_t::operator()(ast_ctx_t &ctx, size_t &res_i, loc_namespace_name_t &out) const { - parse_helper_t h(ctx, res_i); - - if (h.ended()) return false; - - out.clear(); - located_t val; - - if (!h.parse(identifier_parser_t(), val)) return false; - else out.push_back(val); - - while (true) { - if (h.ended()) break; - if (!h.curr().is_operator(operator_t::DOUBLE_COLON)) break; - h.advance("Expected an identifier."); - h.force_parse(identifier_parser_t(), "Expected an identifier.", val); - out.push_back(val); - } - - return h.submit(false); -}