From 32db0a0f181f8f7939ea3f350f6f9c6474017c0b Mon Sep 17 00:00:00 2001 From: Rangi42 Date: Tue, 26 Mar 2024 12:15:09 -0400 Subject: [PATCH] Rename `CaptureBody` to `Capture`, and refactor its methods --- include/asm/lexer.hpp | 20 +++++++++----------- src/asm/lexer.cpp | 35 ++++++++++++++++++----------------- src/asm/parser.y | 6 +++--- 3 files changed, 30 insertions(+), 31 deletions(-) diff --git a/include/asm/lexer.hpp b/include/asm/lexer.hpp index 5720ea4e..10fde61d 100644 --- a/include/asm/lexer.hpp +++ b/include/asm/lexer.hpp @@ -129,20 +129,18 @@ bool lexer_ReachedELSEBlock(); void lexer_RunIFBlock(); void lexer_ReachELSEBlock(); -struct CaptureBody { - uint32_t lineNo; - char const *body; - size_t size; - - void startCapture(); - void endCapture(); -}; - void lexer_CheckRecursionDepth(); uint32_t lexer_GetLineNo(); uint32_t lexer_GetColNo(); void lexer_DumpStringExpansions(); -CaptureBody lexer_CaptureRept(); -CaptureBody lexer_CaptureMacroBody(); + +struct Capture { + uint32_t lineNo; + char const *body; + size_t size; +}; + +Capture lexer_CaptureRept(); +Capture lexer_CaptureMacro(); #endif // RGBDS_ASM_LEXER_H diff --git a/src/asm/lexer.cpp b/src/asm/lexer.cpp index c9b7fd41..cdcd2912 100644 --- a/src/asm/lexer.cpp +++ b/src/asm/lexer.cpp @@ -481,6 +481,7 @@ BufferedContent::~BufferedContent() { } MmappedContent::~MmappedContent() { + // FIXME: This never unmaps a referenced file! if (!isReferenced) munmap(ptr, size); } @@ -2188,7 +2189,7 @@ yy::parser::symbol_type yylex() { } } -void CaptureBody::startCapture() { +static Capture startCapture() { // Due to parser internals, it reads the EOL after the expression before calling this. // Thus, we don't need to keep one in the buffer afterwards. // The following assertion checks that. @@ -2200,28 +2201,30 @@ void CaptureBody::startCapture() { lexerState->disableMacroArgs = true; lexerState->disableInterpolation = true; - lineNo = lexer_GetLineNo(); + Capture capture = {.lineNo = lexer_GetLineNo(), .body = nullptr, .size = 0}; if (auto *mmap = std::get_if(&lexerState->content); mmap && lexerState->expansions.empty()) { - body = &mmap->ptr[mmap->offset]; + capture.body = &mmap->ptr[mmap->offset]; } else if (auto *view = std::get_if(&lexerState->content); view && lexerState->expansions.empty()) { - body = &view->ptr[view->offset]; + capture.body = &view->ptr[view->offset]; } else { - body = nullptr; // Indicates to retrieve the capture buffer when done capturing + // `capture.body == nullptr`; indicates to retrieve the capture buffer when done capturing assert(lexerState->captureBuf == nullptr); + // FIXME: This leaks the captured text! lexerState->captureBuf = new (std::nothrow) std::vector(); if (!lexerState->captureBuf) fatalerror("Failed to allocate capture buffer: %s\n", strerror(errno)); } + return capture; } -void CaptureBody::endCapture() { +static void endCapture(Capture &capture) { // This being `nullptr` means we're capturing from the capture buffer, which is reallocated // during the whole capture process, and so MUST be retrieved at the end - if (!body) - body = lexerState->captureBuf->data(); - size = lexerState->captureSize; + if (!capture.body) + capture.body = lexerState->captureBuf->data(); + capture.size = lexerState->captureSize; // ENDR/ENDM or EOF puts us past the start of the line lexerState->atLineStart = false; @@ -2232,9 +2235,8 @@ void CaptureBody::endCapture() { lexerState->disableInterpolation = false; } -CaptureBody lexer_CaptureRept() { - CaptureBody capture; - capture.startCapture(); +Capture lexer_CaptureRept() { + Capture capture = startCapture(); size_t depth = 0; int c = EOF; @@ -2282,7 +2284,7 @@ CaptureBody lexer_CaptureRept() { } finish: - capture.endCapture(); + endCapture(capture); if (c == EOF) capture.body = nullptr; // Indicates that it reached EOF before an ENDR terminated it @@ -2290,9 +2292,8 @@ finish: return capture; } -CaptureBody lexer_CaptureMacroBody() { - CaptureBody capture; - capture.startCapture(); +Capture lexer_CaptureMacro() { + Capture capture = startCapture(); // If the file is `mmap`ed, we need not to unmap it to keep access to the macro if (auto *mmap = std::get_if(&lexerState->content); mmap) @@ -2333,7 +2334,7 @@ CaptureBody lexer_CaptureMacroBody() { } finish: - capture.endCapture(); + endCapture(capture); if (c == EOF) capture.body = nullptr; // Indicates that it reached EOF before an ENDM terminated it diff --git a/src/asm/parser.y b/src/asm/parser.y index 1ee16346..a07b9236 100644 --- a/src/asm/parser.y +++ b/src/asm/parser.y @@ -245,8 +245,8 @@ %token SECT_WRAM0 "WRAM0" SECT_WRAMX "WRAMX" SECT_HRAM "HRAM" %token SECT_VRAM "VRAM" SECT_SRAM "SRAM" SECT_OAM "OAM" -%type capture_rept -%type capture_macro +%type capture_rept +%type capture_macro %type sect_mod %type > macro_args @@ -926,7 +926,7 @@ def_macro: capture_macro: %empty { - $$ = lexer_CaptureMacroBody(); + $$ = lexer_CaptureMacro(); } ;