From b2072de7173659118cab71ddea2fc618b2a5bf60 Mon Sep 17 00:00:00 2001 From: Josh Holtrop Date: Wed, 18 Apr 2018 20:10:23 -0400 Subject: [PATCH] Keep track of line number and filename from preprocessor output for error messages --- src/parser/parser.l | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/src/parser/parser.l b/src/parser/parser.l index 73bcaa5..c5ae6a0 100644 --- a/src/parser/parser.l +++ b/src/parser/parser.l @@ -6,10 +6,14 @@ #include "parser.h" #include "parser.tab.h" #include "String.h" +#include #define YY_USER_ACTION yylloc->first_column += yyleng; static String * build_string = NULL; +static size_t current_line = 1u; +static String * current_file = NULL; +void handle_loc(const char * input); %} @@ -164,13 +168,36 @@ L?\" { [a-zA-Z_][a-zA-Z_0-9]* return TOK_IDENTIFIER; ^[ ]*#[ ]+[0-9]+[ ]+\".+\".*$ { + handle_loc(yytext); } \n { yylloc->first_line++; yylloc->first_column = 0; yylloc->last_line++; yylloc->last_column = 0; + current_line++; } [ \t\v] /* ignore whitespace */ %% + +void handle_loc(const char * input) +{ + while ((*input < '0') || (*input > '9')) + { + input++; + } + current_line = (size_t)atol(input) - 1u; + while (*input != '"') + { + input++; + } + input++; + const char * fname_start = input; + while (*input != '"') + { + input++; + } + size_t fname_len = (input - fname_start); + current_file = String_new_size(fname_start, fname_len); +}