Make some refactors for name mangling and encapsulation.

This commit is contained in:
2020-09-15 18:51:28 -07:00
parent 18aac4c8fd
commit ed1f9a1460
18 changed files with 895 additions and 303 deletions

View File

@@ -2,44 +2,37 @@
#include "scanner.hpp"
#include <sstream>
bool parse_driver::run_parse() {
FILE* stream = fopen(file_name.c_str(), "r");
if(!stream) return false;
file_mgr::file_mgr() : file_offset(0) {
line_offsets.push_back(0);
yyscan_t scanner;
yylex_init(&scanner);
yyset_in(stream, scanner);
yy::parser parser(scanner, *this);
parser();
yylex_destroy(scanner);
fclose(stream);
file_contents = string_stream.str();
return true;
}
void parse_driver::write(const char* buf, size_t len) {
void file_mgr::write(const char* buf, size_t len) {
string_stream.write(buf, len);
file_offset += len;
}
void parse_driver::mark_line() {
void file_mgr::mark_line() {
line_offsets.push_back(file_offset);
}
size_t parse_driver::get_index(int line, int column) {
assert(line > 0 && line <= line_offsets.size());
return line_offsets[line-1] + column - 1;
void file_mgr::finalize() {
file_contents = string_stream.str();
}
size_t parse_driver::get_line_end(int line) {
size_t file_mgr::get_index(int line, int column) const {
assert(line > 0 && line <= line_offsets.size());
return line_offsets.at(line-1) + column - 1;
}
size_t file_mgr::get_line_end(int line) const {
if(line == line_offsets.size()) return file_contents.size();
return get_index(line+1, 1);
}
void parse_driver::print_location(
void file_mgr::print_location(
std::ostream& stream,
const yy::location& loc,
bool highlight) {
bool highlight) const {
size_t print_start = get_index(loc.begin.line, 1);
size_t highlight_start = get_index(loc.begin.line, loc.begin.column);
size_t highlight_end = get_index(loc.end.line, loc.end.column);
@@ -51,3 +44,29 @@ void parse_driver::print_location(
if(highlight) stream << "\033[0m";
stream.write(content + highlight_end, print_end - highlight_end);
}
bool parse_driver::run_parse() {
FILE* stream = fopen(file_name.c_str(), "r");
if(!stream) return false;
yyscan_t scanner;
yylex_init(&scanner);
yyset_in(stream, scanner);
yy::parser parser(scanner, *this);
parser();
yylex_destroy(scanner);
fclose(stream);
file_m->finalize();
return true;
}
yy::location& parse_driver::get_current_location() {
return location;
}
file_mgr& parse_driver::get_file_manager() const {
return *file_m;
}
definition_group& parse_driver::get_global_defs() const {
return *global_defs;
}