aboutsummaryrefslogtreecommitdiff
path: root/src/parser.cpp
diff options
context:
space:
mode:
authorgingerBill <bill@gingerbill.org>2021-05-19 12:57:30 +0100
committergingerBill <bill@gingerbill.org>2021-05-19 12:57:30 +0100
commit86dbcb1b20e7a5575013f2acbf0f48d194595d27 (patch)
tree171442a4df55dd09f8e65c1c33c8c8ffe879d977 /src/parser.cpp
parent3ac934dd15b59be670627213643e80c6d902fe4d (diff)
Add `-verbose-errors` which shows the error in the line of code
Diffstat (limited to 'src/parser.cpp')
-rw-r--r--src/parser.cpp43
1 files changed, 43 insertions, 0 deletions
diff --git a/src/parser.cpp b/src/parser.cpp
index 2e27b8698..77ce7ea70 100644
--- a/src/parser.cpp
+++ b/src/parser.cpp
@@ -115,6 +115,48 @@ Token token_end_of_line(AstFile *f, Token tok) {
return tok;
}
+gbString get_file_line_as_string(TokenPos const &pos, i32 *offset_) {
+ AstFile *file = get_ast_file_from_id(pos.file_id);
+ if (file == nullptr) {
+ return nullptr;
+ }
+ isize offset = pos.offset;
+
+ u8 *start = file->tokenizer.start;
+ u8 *end = file->tokenizer.end;
+ isize len = end-start;
+ if (len < offset) {
+ return nullptr;
+ }
+
+ u8 *pos_offset = start+offset;
+
+ u8 *line_start = pos_offset;
+ u8 *line_end = pos_offset;
+ while (line_start >= start) {
+ if (*line_start == '\n') {
+ line_start += 1;
+ break;
+ }
+ line_start -= 1;
+ }
+
+ while (line_end < end) {
+ if (*line_end == '\n') {
+ line_end -= 1;
+ break;
+ }
+ line_end += 1;
+ }
+ String the_line = make_string(line_start, line_end-line_start);
+ the_line = string_trim_whitespace(the_line);
+
+ if (offset_) *offset_ = cast(i32)(pos_offset - the_line.text);
+
+ return gb_string_make_length(heap_allocator(), the_line.text, the_line.len);
+}
+
+
isize ast_node_size(AstKind kind) {
return align_formula_isize(gb_size_of(AstCommonStuff) + ast_variant_sizes[kind], gb_align_of(void *));
@@ -4602,6 +4644,7 @@ ParseFileError init_ast_file(AstFile *f, String fullpath, TokenPos *err_pos) {
GB_ASSERT(f != nullptr);
f->fullpath = string_trim_whitespace(fullpath); // Just in case
set_file_path_string(f->id, fullpath);
+ set_ast_file_from_id(f->id, f);
if (!string_ends_with(f->fullpath, str_lit(".odin"))) {
return ParseFile_WrongExtension;
}