lang/include/sources_manager.hpp

100 lines
2.4 KiB
C++
Raw Normal View History

2023-08-02 17:54:39 +03:00
#pragma once
#include "basic_printers.hpp"
#include "error_handling.hpp"
#include "error_log.hpp"
2023-08-02 17:54:39 +03:00
#include "expression_nodes.hpp"
#include "name_tree.hpp"
#include "statement_builders.hpp"
#include "statement_nodes.hpp"
#include "statement_printers.hpp"
#include "tree_sitter_wrapper.hpp"
#include "type_nodes.hpp"
#include <fstream>
#include <iostream>
#include <sstream>
class SourcesManager {
public:
void add_file(const std::string &filename) {
std::ifstream in;
in.open(filename);
std::stringstream source_stream;
source_stream << in.rdbuf();
in.close();
std::string source = source_stream.str();
parser::ParseTree parse_tree(source);
if (!parse_tree.is_properly_parsed()) {
error_handling::handle_parsing_error(
"There are some parsing errors in file", parse_tree.get_root());
}
auto new_statements = builders::build_source_file(
parse_tree.get_root(), expression_storage_, type_storage_, name_tree_);
statements_.reserve(statements_.size() + new_statements.size());
for (auto &new_statement : new_statements) {
statements_.push_back(std::move(new_statement));
}
new_statements.clear();
}
void print(std::ostream &out) {
printers::Printer printer(out, 2, 80, true);
printers::print_source_file(statements_, printer);
}
//
nodes::ExpressionStorage *get_expression_storage() {
return &expression_storage_;
}
const nodes::ExpressionStorage *get_expression_storage() const {
return &expression_storage_;
}
//
nodes::TypeStorage *get_type_storage() { return &type_storage_; }
const nodes::TypeStorage *get_type_storage() const { return &type_storage_; }
//
names::NameTree *get_name_tree() { return &name_tree_; }
const names::NameTree *get_name_tree() const { return &name_tree_; }
//
error_handling::ErrorLog *get_error_log() { return &error_log_; }
const error_handling::ErrorLog *get_error_log() const { return &error_log_; }
//
2024-01-04 19:29:29 +03:00
size_t statements_size() const { return statements_.size(); }
2023-08-02 17:54:39 +03:00
nodes::Statement *get_statement(size_t id) { return &statements_.at(id); }
const nodes::Statement *get_statement(size_t id) const {
return &statements_.at(id);
}
private:
nodes::ExpressionStorage expression_storage_;
nodes::TypeStorage type_storage_;
names::NameTree name_tree_;
error_handling::ErrorLog error_log_;
2023-08-02 17:54:39 +03:00
std::vector<nodes::Statement> statements_;
};