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_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

View File

@@ -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<MmappedContent>(&lexerState->content);
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);
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<char>();
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<MmappedContent>(&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

View File

@@ -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 <CaptureBody> capture_rept
%type <CaptureBody> capture_macro
%type <Capture> capture_rept
%type <Capture> capture_macro
%type <SectionModifier> sect_mod
%type <std::shared_ptr<MacroArgs>> macro_args
@@ -926,7 +926,7 @@ def_macro:
capture_macro:
%empty {
$$ = lexer_CaptureMacroBody();
$$ = lexer_CaptureMacro();
}
;