Factor out endCapture to go with startCapture (#904)

This also refactors `startCapture` to modify the
capture body as an argument.
This commit is contained in:
Rangi
2021-07-04 16:08:59 -04:00
committed by GitHub
parent 1b5648bb06
commit 9782f7d942

View File

@@ -2399,7 +2399,7 @@ int yylex(void)
return token; return token;
} }
static char *startCapture(void) static void startCapture(struct CaptureBody *capture)
{ {
assert(!lexerState->capturing); assert(!lexerState->capturing);
lexerState->capturing = true; lexerState->capturing = true;
@@ -2407,20 +2407,36 @@ static char *startCapture(void)
lexerState->disableMacroArgs = true; lexerState->disableMacroArgs = true;
lexerState->disableInterpolation = true; lexerState->disableInterpolation = true;
capture->lineNo = lexer_GetLineNo();
if (lexerState->isMmapped && !lexerState->expansions) { if (lexerState->isMmapped && !lexerState->expansions) {
return &lexerState->ptr[lexerState->offset]; capture->body = &lexerState->ptr[lexerState->offset];
} else { } else {
lexerState->captureCapacity = 128; /* The initial size will be twice that */ lexerState->captureCapacity = 128; /* The initial size will be twice that */
assert(lexerState->captureBuf == NULL); assert(lexerState->captureBuf == NULL);
reallocCaptureBuf(); 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) bool lexer_CaptureRept(struct CaptureBody *capture)
{ {
capture->lineNo = lexer_GetLineNo(); startCapture(capture);
capture->body = startCapture();
size_t depth = 0; size_t depth = 0;
int c = EOF; int c = EOF;
@@ -2473,16 +2489,7 @@ bool lexer_CaptureRept(struct CaptureBody *capture)
} }
finish: finish:
// This being NULL means we're capturing from the capture buf, which is `realloc`'d during endCapture(capture);
// 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;
/* Returns true if an ENDR terminated the block, false if it reached EOF first */ /* Returns true if an ENDR terminated the block, false if it reached EOF first */
return c != EOF; return c != EOF;
@@ -2490,15 +2497,14 @@ finish:
bool lexer_CaptureMacroBody(struct CaptureBody *capture) bool lexer_CaptureMacroBody(struct CaptureBody *capture)
{ {
capture->lineNo = lexer_GetLineNo(); startCapture(capture);
capture->body = startCapture();
int c = EOF;
/* 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 (lexerState->isMmapped) if (lexerState->isMmapped)
lexerState->isReferenced = true; lexerState->isReferenced = true;
int c = EOF;
/* /*
* 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.
@@ -2538,16 +2544,7 @@ bool lexer_CaptureMacroBody(struct CaptureBody *capture)
} }
finish: finish:
// This being NULL means we're capturing from the capture buf, which is `realloc`'d during endCapture(capture);
// 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;
/* Returns true if an ENDM terminated the block, false if it reached EOF first */ /* Returns true if an ENDM terminated the block, false if it reached EOF first */
return c != EOF; return c != EOF;