bloglang/13/parse_driver.cpp

54 lines
1.6 KiB
C++

#include "parse_driver.hpp"
#include "scanner.hpp"
#include <sstream>
bool parse_driver::run_parse() {
FILE* stream = fopen(file_name.c_str(), "r");
if(!stream) return false;
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) {
string_stream.write(buf, len);
file_offset += len;
}
void parse_driver::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;
}
size_t parse_driver::get_line_end(int line) {
if(line == line_offsets.size()) return file_contents.size();
return get_index(line+1, 1);
}
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);
if(highlight) stream << "\033[4;31m";
stream.write(content + highlight_start, highlight_end - highlight_start);
if(highlight) stream << "\033[0m";
stream.write(content + highlight_end, print_end - highlight_end);
}