From bca4d0e794980aee0775aa449f1814cb8cec74a0 Mon Sep 17 00:00:00 2001 From: Bill Currie Date: Fri, 30 Aug 2024 13:13:13 +0900 Subject: [PATCH] [qfcc] Implement location printing for the parsers It's pretty bare-bones, but it's at least consistent across all the parsers, and it's currently used only by the parser debug support. --- tools/qfcc/include/rua-lang.h | 17 +++++++++++++++++ tools/qfcc/source/glsl-parse.y | 4 ++-- tools/qfcc/source/pre-parse.y | 4 ++-- tools/qfcc/source/qc-lex.l | 7 +++++++ tools/qfcc/source/qc-parse.y | 4 ++-- tools/qfcc/source/qp-parse.y | 4 ++-- 6 files changed, 32 insertions(+), 8 deletions(-) diff --git a/tools/qfcc/include/rua-lang.h b/tools/qfcc/include/rua-lang.h index 278009182..d528d73d8 100644 --- a/tools/qfcc/include/rua-lang.h +++ b/tools/qfcc/include/rua-lang.h @@ -49,6 +49,21 @@ typedef struct rua_loc_s { int file; } rua_loc_t; +#define RUA_LOC_DEFAULT(Current, Rhs, N) \ + do if (N) { \ + (Current).line = YYRHSLOC (Rhs, 1).line; \ + (Current).column = YYRHSLOC (Rhs, 1).column; \ + (Current).last_line = YYRHSLOC (Rhs, N).last_line; \ + (Current).last_column = YYRHSLOC (Rhs, N).last_column; \ + (Current).file = YYRHSLOC (Rhs, N).file; \ + } else { \ + (Current).line = (Current).last_line = \ + YYRHSLOC (Rhs, 0).last_line; \ + (Current).column = (Current).last_column = \ + YYRHSLOC (Rhs, 0).last_column; \ + (Current).file = YYRHSLOC (Rhs, 0).file; \ + } while (0) + typedef struct expr_s expr_t; typedef struct symtab_s symtab_t; typedef struct function_s function_t; @@ -156,6 +171,8 @@ void rua_macro_line (rua_macro_t *macro, void *scanner); void rua_macro_va_opt (rua_macro_t *macro, void *scanner); void rua_macro_va_args (rua_macro_t *macro, void *scanner); +void rua_print_location (FILE *out, const rua_loc_t *loc); + #include "tools/qfcc/source/pre-parse.h" typedef struct rua_parser_s { diff --git a/tools/qfcc/source/glsl-parse.y b/tools/qfcc/source/glsl-parse.y index 7fafd12bd..aba8a8464 100644 --- a/tools/qfcc/source/glsl-parse.y +++ b/tools/qfcc/source/glsl-parse.y @@ -103,8 +103,8 @@ parse_error (void *scanner) #define PARSE_ERROR do { parse_error (scanner); YYERROR; } while (0) -#define first_line line -#define first_column column +#define YYLLOC_DEFAULT(Current, Rhs, N) RUA_LOC_DEFAULT(Current, Rhs, N) +#define YYLOCATION_PRINT rua_print_location int yylex (YYSTYPE *yylval, YYLTYPE *yylloc); diff --git a/tools/qfcc/source/pre-parse.y b/tools/qfcc/source/pre-parse.y index 73a684770..edcc32fe2 100644 --- a/tools/qfcc/source/pre-parse.y +++ b/tools/qfcc/source/pre-parse.y @@ -80,8 +80,8 @@ parse_error (void *scanner) #define PARSE_ERROR do { parse_error (scanner); YYERROR; } while (0) #endif -#define first_line line -#define first_column column +#define YYLLOC_DEFAULT(Current, Rhs, N) RUA_LOC_DEFAULT(Current, Rhs, N) +#define YYLOCATION_PRINT rua_print_location %} %code requires { diff --git a/tools/qfcc/source/qc-lex.l b/tools/qfcc/source/qc-lex.l index 996a63573..9f523c46e 100644 --- a/tools/qfcc/source/qc-lex.l +++ b/tools/qfcc/source/qc-lex.l @@ -2408,3 +2408,10 @@ rua_macro_va_args (rua_macro_t *macro, void *scanner) macro->tail = arg->tail; } } + +void +rua_print_location (FILE *out, const rua_loc_t *loc) +{ + fprintf (out, "%s:[%d.%d-%d.%d]", GETSTR (loc->file), + loc->line, loc->column, loc->last_line, loc->last_column); +} diff --git a/tools/qfcc/source/qc-parse.y b/tools/qfcc/source/qc-parse.y index 217aefe3b..8f19f9bb4 100644 --- a/tools/qfcc/source/qc-parse.y +++ b/tools/qfcc/source/qc-parse.y @@ -103,8 +103,8 @@ parse_error (void *scanner) #define PARSE_ERROR do { parse_error (scanner); YYERROR; } while (0) -#define first_line line -#define first_column column +#define YYLLOC_DEFAULT(Current, Rhs, N) RUA_LOC_DEFAULT(Current, Rhs, N) +#define YYLOCATION_PRINT rua_print_location int yylex (YYSTYPE *yylval, YYLTYPE *yylloc); diff --git a/tools/qfcc/source/qp-parse.y b/tools/qfcc/source/qp-parse.y index b1b07bd7a..eb16b1720 100644 --- a/tools/qfcc/source/qp-parse.y +++ b/tools/qfcc/source/qp-parse.y @@ -84,8 +84,8 @@ parse_error (void *scanner) #define PARSE_ERROR do { parse_error (scanner); YYERROR; } while (0) -#define first_line line -#define first_column column +#define YYLLOC_DEFAULT(Current, Rhs, N) RUA_LOC_DEFAULT(Current, Rhs, N) +#define YYLOCATION_PRINT rua_print_location int yylex (void);