highlighting improvements

This commit is contained in:
ProgramSnail 2023-07-31 13:52:03 +03:00
parent 195a26f9b7
commit bf7fe1f821
9 changed files with 139 additions and 22 deletions

View file

@ -0,0 +1,3 @@
#include "expression_type_check.hpp"
namespace type_check {} // namespace type_check

View file

@ -1,4 +1,6 @@
#include "name_tree.hpp"
#include "error_handling.hpp"
#include "statement_nodes.hpp"
namespace names {
@ -17,18 +19,18 @@ bool NameTree::insert(const std::string &path, nodes::Statement &&statement) {
return true;
}
bool NameTree::insert(const std::string &path,
const nodes::Statement &statement) {
nodes::CombineResult NameTree::insert_combine(const std::string &path,
nodes::Statement &&statement) {
auto name_ids = slice_path_to_name_ids(path);
size_t node_id = add_node(get_root(), name_ids);
if (nodes_[node_id].get_statement().has_value()) {
return false;
if (!nodes_[node_id].get_statement().has_value()) {
nodes_[node_id].set_statement(std::move(statement));
return nodes::CombineResult::OK;
}
nodes_[node_id].set_statement(statement);
return true;
return nodes_[node_id].get_statement().value()->combine(std::move(statement));
}
std::optional<nodes::Statement *> NameTree::find(const std::string &path) {
@ -64,6 +66,41 @@ NameTree::find(const std::string &path) const {
return std::nullopt;
}
void NameTree::print(printers::Printer &printer) const {
nodes_[get_root()].print(nodes_, printer);
}
// TODO
void NameTree::add_statement_children_to_tree() {
for (auto &node : nodes_) {
if (!node.get_statement().has_value()) {
continue;
}
switch (node.get_statement().value()->get_any()->index()) {
case 0: // Import
// TODO: link imported symbols, if named import, otherwise link imported
// symbols to root
break;
case 1: // FunctionDefinition
// TODO: link to result type
break;
case 2: // TypeDefinition
// TODO: link methods + link children to types + connect to typeclasses
break;
case 3: // TypeclassDefinition
// TODO: link methods + connect typeclasses
break;
case 4: // EmptyLines
break;
default:
error_handling::handle_general_error(
"Unexpected statement type in name tree");
break;
}
}
}
// --- private
size_t NameTree::add_node(size_t current_node,

View file

@ -84,7 +84,7 @@ nodes::Statement build_statement(parser::ParseTree::Node parser_node,
->get();
break;
case tokens::Type::EMPTY_LINES:
statement = build_empty_lines(parser_node);
statement = nodes::Statement(build_empty_lines(parser_node));
break;
default:
error_handling::handle_parsing_error("Unexprected statement node type",
@ -97,7 +97,8 @@ nodes::Statement build_statement(parser::ParseTree::Node parser_node,
if (statement_name.has_value()) {
// TODO: combine statements with same name
if (!name_tree.insert(statement_name.value(), statement.value())) {
auto statement_copy = statement.value();
if (!name_tree.insert(statement_name.value(), std::move(statement_copy))) {
error_handling::handle_parsing_error(
"Two or more statements in file have the same name", parser_node);
}