Refactor errors and update post draft.

This commit is contained in:
2020-09-11 21:29:49 -07:00
parent 4cdb9360fb
commit 03465c07ce
7 changed files with 68 additions and 39 deletions

View File

@@ -5,8 +5,6 @@
bool parse_driver::run_parse() {
FILE* stream = fopen(file_name.c_str(), "r");
if(!stream) return false;
string_stream = std::ostringstream();
file_offset = 0;
line_offsets.push_back(0);
yyscan_t scanner;
yylex_init(&scanner);
@@ -29,27 +27,27 @@ void parse_driver::mark_line() {
}
size_t parse_driver::get_index(int line, int column) {
assert(line > 0);
assert(line <= line_offsets.size());
size_t file_offset = line_offsets[line-1];
file_offset += column - 1;
return file_offset;
assert(line > 0 && line <= line_offsets.size());
return line_offsets[line-1] + column - 1;
}
size_t parse_driver::get_line_end(int line) {
if(line > line_offsets.size()) return file_contents.size();
if(line == line_offsets.size()) return file_contents.size();
return get_index(line+1, 1);
}
void parse_driver::print_highlighted_location(std::ostream& stream, const yy::location& loc) {
void parse_driver::print_location(
std::ostream& stream,
const yy::location& loc,
bool highlight) {
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);
size_t print_end = get_line_end(loc.end.line);
const char* content = file_contents.c_str();
stream.write(content + print_start, highlight_start - print_start);
stream << "\033[4;31m";
if(highlight) stream << "\033[4;31m";
stream.write(content + highlight_start, highlight_end - highlight_start);
stream << "\033[0m";
if(highlight) stream << "\033[0m";
stream.write(content + highlight_end, print_end - highlight_end);
}