From d5536467f634e9ec1479033d70c359447f464196 Mon Sep 17 00:00:00 2001 From: Danila Fedorin Date: Wed, 9 Sep 2020 14:20:10 -0700 Subject: [PATCH] Touch up source index code. --- code/compiler/13/parse_driver.hpp | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/code/compiler/13/parse_driver.hpp b/code/compiler/13/parse_driver.hpp index 580e250..3d8592b 100644 --- a/code/compiler/13/parse_driver.hpp +++ b/code/compiler/13/parse_driver.hpp @@ -29,6 +29,7 @@ struct parse_driver { void run_parse() { file_stream.open(file_name); if(!file_stream.good()) throw 0; + line_offsets.push_back(0); yyscan_t scanner; scanner_init(this, &scanner); yy::parser parser(scanner, *this); @@ -47,15 +48,16 @@ struct parse_driver { } size_t get_index(int line, int column) { - assert(line-1 < line_offsets.size()); - size_t file_offset = line ? 0 : line_offsets[line-1]; + assert(line > 0); + assert(line <= line_offsets.size()); + size_t file_offset = 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; + if(line > line_offsets.size()) return read_file.size(); + return get_index(line+1, 1); } };