shithub: rgbds

Download patch

ref: 9782f7d942c74c5b0c53b56abea462237e1e0b2f
parent: 1b5648bb064d4f002db56cd691baa667d2b537b2
author: Rangi <35663410+Rangi42@users.noreply.github.com>
date: Sun Jul 4 12:08:59 EDT 2021

Factor out `endCapture` to go with `startCapture` (#904)

This also refactors `startCapture` to modify the
capture body as an argument.

--- a/src/asm/lexer.c
+++ b/src/asm/lexer.c
@@ -2399,7 +2399,7 @@
 	return token;
 }
 
-static char *startCapture(void)
+static void startCapture(struct CaptureBody *capture)
 {
 	assert(!lexerState->capturing);
 	lexerState->capturing = true;
@@ -2407,20 +2407,36 @@
 	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 @@
 	}
 
 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 @@
 
 bool lexer_CaptureMacroBody(struct CaptureBody *capture)
 {
-	capture->lineNo = lexer_GetLineNo();
-	capture->body = startCapture();
+	startCapture(capture);
 
-	int c = EOF;
-
 	/* 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 @@
 	}
 
 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;