Rename CaptureBody to Capture, and refactor its methods

This commit is contained in:
Rangi42
2024-03-26 12:15:09 -04:00
committed by Sylvie
parent a167d23d01
commit 32db0a0f18
3 changed files with 30 additions and 31 deletions

View File

@@ -129,20 +129,18 @@ bool lexer_ReachedELSEBlock();
void lexer_RunIFBlock(); void lexer_RunIFBlock();
void lexer_ReachELSEBlock(); void lexer_ReachELSEBlock();
struct CaptureBody {
uint32_t lineNo;
char const *body;
size_t size;
void startCapture();
void endCapture();
};
void lexer_CheckRecursionDepth(); void lexer_CheckRecursionDepth();
uint32_t lexer_GetLineNo(); uint32_t lexer_GetLineNo();
uint32_t lexer_GetColNo(); uint32_t lexer_GetColNo();
void lexer_DumpStringExpansions(); 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 #endif // RGBDS_ASM_LEXER_H

View File

@@ -481,6 +481,7 @@ BufferedContent::~BufferedContent() {
} }
MmappedContent::~MmappedContent() { MmappedContent::~MmappedContent() {
// FIXME: This never unmaps a referenced file!
if (!isReferenced) if (!isReferenced)
munmap(ptr, size); 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. // 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. // Thus, we don't need to keep one in the buffer afterwards.
// The following assertion checks that. // The following assertion checks that.
@@ -2200,28 +2201,30 @@ void CaptureBody::startCapture() {
lexerState->disableMacroArgs = true; lexerState->disableMacroArgs = true;
lexerState->disableInterpolation = true; lexerState->disableInterpolation = true;
lineNo = lexer_GetLineNo(); Capture capture = {.lineNo = lexer_GetLineNo(), .body = nullptr, .size = 0};
if (auto *mmap = std::get_if<MmappedContent>(&lexerState->content); if (auto *mmap = std::get_if<MmappedContent>(&lexerState->content);
mmap && lexerState->expansions.empty()) { mmap && lexerState->expansions.empty()) {
body = &mmap->ptr[mmap->offset]; capture.body = &mmap->ptr[mmap->offset];
} else if (auto *view = std::get_if<ViewedContent>(&lexerState->content); } else if (auto *view = std::get_if<ViewedContent>(&lexerState->content);
view && lexerState->expansions.empty()) { view && lexerState->expansions.empty()) {
body = &view->ptr[view->offset]; capture.body = &view->ptr[view->offset];
} else { } 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); assert(lexerState->captureBuf == nullptr);
// FIXME: This leaks the captured text!
lexerState->captureBuf = new (std::nothrow) std::vector<char>(); lexerState->captureBuf = new (std::nothrow) std::vector<char>();
if (!lexerState->captureBuf) if (!lexerState->captureBuf)
fatalerror("Failed to allocate capture buffer: %s\n", strerror(errno)); 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 // 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 // during the whole capture process, and so MUST be retrieved at the end
if (!body) if (!capture.body)
body = lexerState->captureBuf->data(); capture.body = lexerState->captureBuf->data();
size = lexerState->captureSize; capture.size = lexerState->captureSize;
// ENDR/ENDM or EOF puts us past the start of the line // ENDR/ENDM or EOF puts us past the start of the line
lexerState->atLineStart = false; lexerState->atLineStart = false;
@@ -2232,9 +2235,8 @@ void CaptureBody::endCapture() {
lexerState->disableInterpolation = false; lexerState->disableInterpolation = false;
} }
CaptureBody lexer_CaptureRept() { Capture lexer_CaptureRept() {
CaptureBody capture; Capture capture = startCapture();
capture.startCapture();
size_t depth = 0; size_t depth = 0;
int c = EOF; int c = EOF;
@@ -2282,7 +2284,7 @@ CaptureBody lexer_CaptureRept() {
} }
finish: finish:
capture.endCapture(); endCapture(capture);
if (c == EOF) if (c == EOF)
capture.body = nullptr; // Indicates that it reached EOF before an ENDR terminated it capture.body = nullptr; // Indicates that it reached EOF before an ENDR terminated it
@@ -2290,9 +2292,8 @@ finish:
return capture; return capture;
} }
CaptureBody lexer_CaptureMacroBody() { Capture lexer_CaptureMacro() {
CaptureBody capture; Capture capture = startCapture();
capture.startCapture();
// If the file is `mmap`ed, we need not to unmap it to keep access to the macro // If the file is `mmap`ed, we need not to unmap it to keep access to the macro
if (auto *mmap = std::get_if<MmappedContent>(&lexerState->content); mmap) if (auto *mmap = std::get_if<MmappedContent>(&lexerState->content); mmap)
@@ -2333,7 +2334,7 @@ CaptureBody lexer_CaptureMacroBody() {
} }
finish: finish:
capture.endCapture(); endCapture(capture);
if (c == EOF) if (c == EOF)
capture.body = nullptr; // Indicates that it reached EOF before an ENDM terminated it capture.body = nullptr; // Indicates that it reached EOF before an ENDM terminated it

View File

@@ -245,8 +245,8 @@
%token SECT_WRAM0 "WRAM0" SECT_WRAMX "WRAMX" SECT_HRAM "HRAM" %token SECT_WRAM0 "WRAM0" SECT_WRAMX "WRAMX" SECT_HRAM "HRAM"
%token SECT_VRAM "VRAM" SECT_SRAM "SRAM" SECT_OAM "OAM" %token SECT_VRAM "VRAM" SECT_SRAM "SRAM" SECT_OAM "OAM"
%type <CaptureBody> capture_rept %type <Capture> capture_rept
%type <CaptureBody> capture_macro %type <Capture> capture_macro
%type <SectionModifier> sect_mod %type <SectionModifier> sect_mod
%type <std::shared_ptr<MacroArgs>> macro_args %type <std::shared_ptr<MacroArgs>> macro_args
@@ -926,7 +926,7 @@ def_macro:
capture_macro: capture_macro:
%empty { %empty {
$$ = lexer_CaptureMacroBody(); $$ = lexer_CaptureMacro();
} }
; ;