From 9782f7d942c74c5b0c53b56abea462237e1e0b2f Mon Sep 17 00:00:00 2001 From: Rangi <35663410+Rangi42@users.noreply.github.com> Date: Sun, 4 Jul 2021 16:08:59 -0400 Subject: [PATCH] Factor out `endCapture` to go with `startCapture` (#904) This also refactors `startCapture` to modify the capture body as an argument. --- src/asm/lexer.c | 55 +++++++++++++++++++++++-------------------------- 1 file changed, 26 insertions(+), 29 deletions(-) diff --git a/src/asm/lexer.c b/src/asm/lexer.c index 2cd35007..344698d2 100644 --- a/src/asm/lexer.c +++ b/src/asm/lexer.c @@ -2399,7 +2399,7 @@ int yylex(void) return token; } -static char *startCapture(void) +static void startCapture(struct CaptureBody *capture) { assert(!lexerState->capturing); lexerState->capturing = true; @@ -2407,20 +2407,36 @@ static char *startCapture(void) lexerState->disableMacroArgs = true; lexerState->disableInterpolation = true; + capture->lineNo = lexer_GetLineNo(); + if (lexerState->isMmapped && !lexerState->expansions) { - return &lexerState->ptr[lexerState->offset]; + capture->body = &lexerState->ptr[lexerState->offset]; } else { lexerState->captureCapacity = 128; /* The initial size will be twice that */ assert(lexerState->captureBuf == NULL); reallocCaptureBuf(); - return NULL; // Indicate to retrieve the capture buffer when done capturing + capture->body = NULL; // Indicate to retrieve the capture buffer when done capturing } } +static void endCapture(struct CaptureBody *capture) +{ + // This being NULL means we're capturing from the capture buf, which is `realloc`'d during + // the whole capture process, and so MUST be retrieved at the end + if (!capture->body) + capture->body = lexerState->captureBuf; + capture->size = lexerState->captureSize; + + lexerState->capturing = false; + lexerState->captureBuf = NULL; + lexerState->disableMacroArgs = false; + lexerState->disableInterpolation = false; + lexerState->atLineStart = false; +} + bool lexer_CaptureRept(struct CaptureBody *capture) { - capture->lineNo = lexer_GetLineNo(); - capture->body = startCapture(); + startCapture(capture); size_t depth = 0; int c = EOF; @@ -2473,16 +2489,7 @@ bool lexer_CaptureRept(struct CaptureBody *capture) } finish: - // This being NULL means we're capturing from the capture buf, which is `realloc`'d during - // the whole capture process, and so MUST be retrieved at the end - if (!capture->body) - capture->body = lexerState->captureBuf; - capture->size = lexerState->captureSize; - lexerState->capturing = false; - lexerState->captureBuf = NULL; - lexerState->disableMacroArgs = false; - lexerState->disableInterpolation = false; - lexerState->atLineStart = false; + endCapture(capture); /* Returns true if an ENDR terminated the block, false if it reached EOF first */ return c != EOF; @@ -2490,15 +2497,14 @@ finish: bool lexer_CaptureMacroBody(struct CaptureBody *capture) { - capture->lineNo = lexer_GetLineNo(); - capture->body = startCapture(); - - int c = EOF; + startCapture(capture); /* If the file is `mmap`ed, we need not to unmap it to keep access to the macro */ if (lexerState->isMmapped) lexerState->isReferenced = true; + int c = EOF; + /* * 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. @@ -2538,16 +2544,7 @@ bool lexer_CaptureMacroBody(struct CaptureBody *capture) } finish: - // This being NULL means we're capturing from the capture buf, which is `realloc`'d during - // the whole capture process, and so MUST be retrieved at the end - if (!capture->body) - capture->body = lexerState->captureBuf; - capture->size = lexerState->captureSize; - lexerState->capturing = false; - lexerState->captureBuf = NULL; - lexerState->disableMacroArgs = false; - lexerState->disableInterpolation = false; - lexerState->atLineStart = false; + endCapture(capture); /* Returns true if an ENDM terminated the block, false if it reached EOF first */ return c != EOF;