From 14a1ef30ee311856feefc80a7225192204f106d2 Mon Sep 17 00:00:00 2001 From: Max Wash Date: Thu, 8 Jan 2026 19:07:19 +0000 Subject: [PATCH] mie: parse: add mie_file_span to record character span locations within files --- mie/include/mie/parse/file-span.h | 23 +++++++++++++++++++++++ mie/include/mie/parse/token.h | 7 ++----- mie/parse/file-span.c | 7 +++++++ mie/parse/lex.c | 8 ++++---- mie/parse/parse.c | 5 ++++- 5 files changed, 40 insertions(+), 10 deletions(-) create mode 100644 mie/include/mie/parse/file-span.h create mode 100644 mie/parse/file-span.c diff --git a/mie/include/mie/parse/file-span.h b/mie/include/mie/parse/file-span.h new file mode 100644 index 0000000..62d8108 --- /dev/null +++ b/mie/include/mie/parse/file-span.h @@ -0,0 +1,23 @@ +#ifndef MIE_PARSE_FILE_SPAN_H_ +#define MIE_PARSE_FILE_SPAN_H_ + +#include + +struct mie_token; + +/* a single row/column pair, representing a location within a file. + * both values are 1-based (i.e. the first character in a file is at c_row=1, + * c_col=1*/ +struct mie_file_cell { + unsigned int c_row, c_col; +}; + +/* represents a span of characters within a file. + * used for diagnostic reporting */ +struct mie_file_span { + struct mie_file_cell s_start, s_end; +}; + +MIE_API void mie_file_span_init(struct mie_file_span *span, struct mie_token *tok); + +#endif diff --git a/mie/include/mie/parse/token.h b/mie/include/mie/parse/token.h index fdb70bb..9d82b41 100644 --- a/mie/include/mie/parse/token.h +++ b/mie/include/mie/parse/token.h @@ -3,6 +3,7 @@ #include #include +#include #define MIE_TOKEN_TYPE(tok) ((tok) ? (tok)->tok_type : MIE_TOK_NONE) #define MIE_TOKEN_IS(tok, type) ((tok) && ((tok)->tok_type & (type)) != 0) @@ -72,12 +73,8 @@ enum mie_token_symbol { MIE_SYM_OTHER, }; -struct mie_token_location { - unsigned int c_row, c_col; -}; - struct mie_token { - struct mie_token_location tok_start, tok_end; + struct mie_file_span tok_location; enum mie_token_type tok_type; enum mie_token_value_type tok_value_type; b_queue_entry tok_entry; diff --git a/mie/parse/file-span.c b/mie/parse/file-span.c new file mode 100644 index 0000000..7291d01 --- /dev/null +++ b/mie/parse/file-span.c @@ -0,0 +1,7 @@ +#include +#include + +void mie_file_span_init(struct mie_file_span *span, struct mie_token *tok) +{ + *span = tok->tok_location; +} diff --git a/mie/parse/lex.c b/mie/parse/lex.c index 8395e36..858d686 100644 --- a/mie/parse/lex.c +++ b/mie/parse/lex.c @@ -324,10 +324,10 @@ static void set_token_end(struct mie_lex *lex) static enum mie_status push_token(struct mie_lex *lex, struct mie_token *tok) { - tok->tok_start.c_row = lex->lex_token_start_row; - tok->tok_start.c_col = lex->lex_token_start_col; - tok->tok_end.c_row = lex->lex_token_end_row; - tok->tok_end.c_col = lex->lex_token_end_col; + tok->tok_location.s_start.c_row = lex->lex_token_start_row; + tok->tok_location.s_start.c_col = lex->lex_token_start_col; + tok->tok_location.s_end.c_row = lex->lex_token_end_row; + tok->tok_location.s_end.c_col = lex->lex_token_end_col; b_queue_push_back(&lex->lex_queue, &tok->tok_entry); return MIE_SUCCESS; diff --git a/mie/parse/parse.c b/mie/parse/parse.c index d7f16aa..5e58df2 100644 --- a/mie/parse/parse.c +++ b/mie/parse/parse.c @@ -409,11 +409,14 @@ static bool parse_generic_op( struct mie_parser *ctx, struct mie_name_map *names, struct mie_op *dest) { b_string *str = b_string_create(); - if (!mie_parser_parse_opname(ctx, str)) { + struct mie_file_span loc; + if (!mie_parser_parse_opname(ctx, str, &loc)) { + b_string_unref(str); return false; } dest->op_name = b_string_steal(str); + b_string_unref(str); if (!mie_parser_parse_symbol(ctx, MIE_SYM_LEFT_PAREN)) { return false;