2020-09-09 13:00:06 -07:00
|
|
|
#pragma once
|
|
|
|
#include <string>
|
|
|
|
#include <fstream>
|
2020-09-09 13:57:01 -07:00
|
|
|
#include <sstream>
|
2020-09-09 13:00:06 -07:00
|
|
|
#include "definition.hpp"
|
|
|
|
#include "location.hh"
|
|
|
|
#include "parser.hpp"
|
|
|
|
|
|
|
|
struct parse_driver;
|
|
|
|
|
|
|
|
void scanner_init(parse_driver* d, yyscan_t* scanner);
|
|
|
|
void scanner_destroy(yyscan_t* scanner);
|
|
|
|
|
|
|
|
struct parse_driver {
|
|
|
|
std::string file_name;
|
|
|
|
std::ifstream file_stream;
|
2020-09-09 13:57:01 -07:00
|
|
|
std::ostringstream string_stream;
|
2020-09-09 13:00:06 -07:00
|
|
|
|
|
|
|
yy::location location;
|
|
|
|
size_t file_offset;
|
|
|
|
|
|
|
|
std::vector<size_t> line_offsets;
|
|
|
|
definition_group global_defs;
|
2020-09-09 13:57:01 -07:00
|
|
|
std::string read_file;
|
2020-09-09 13:00:06 -07:00
|
|
|
|
|
|
|
parse_driver(const std::string& file)
|
2020-09-09 13:57:01 -07:00
|
|
|
: file_name(file), file_offset(0) {}
|
2020-09-09 13:00:06 -07:00
|
|
|
|
|
|
|
void run_parse() {
|
|
|
|
file_stream.open(file_name);
|
|
|
|
if(!file_stream.good()) throw 0;
|
|
|
|
yyscan_t scanner;
|
|
|
|
scanner_init(this, &scanner);
|
|
|
|
yy::parser parser(scanner, *this);
|
|
|
|
parser();
|
|
|
|
scanner_destroy(&scanner);
|
2020-09-09 13:57:01 -07:00
|
|
|
read_file = string_stream.str();
|
2020-09-09 13:00:06 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
int get() {
|
2020-09-09 13:57:01 -07:00
|
|
|
int new_char = file_stream.get();
|
|
|
|
if(new_char == EOF) return EOF;
|
|
|
|
file_offset++;
|
|
|
|
if(new_char == '\n') line_offsets.push_back(file_offset);
|
|
|
|
string_stream.put(new_char);
|
|
|
|
return new_char;
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t get_index(int line, int column) {
|
|
|
|
assert(line-1 < line_offsets.size());
|
|
|
|
size_t file_offset = line ? 0 : line_offsets[line-1];
|
|
|
|
file_offset += column - 1;
|
|
|
|
return file_offset;
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t get_line_end(int line) {
|
|
|
|
assert(line < line_offsets.size());
|
|
|
|
return line_offsets[line] - 1;
|
2020-09-09 13:00:06 -07:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
#define YY_DECL yy::parser::symbol_type yylex(yyscan_t yyscanner, parse_driver& drv)
|
|
|
|
|
|
|
|
YY_DECL;
|