shithub: rgbds

Download patch

ref: f7b7a97407c473c246546905e8ed4123503b579b
parent: ece6853e0f5d206d5b2159aa300ce2a81030d130
author: ISSOtm <[email protected]>
date: Tue Aug 18 09:16:56 EDT 2020

Prevent expanding macro args in comments

Also use a cleaner way, instead of hardcoding to capture

--- a/src/asm/lexer.c
+++ b/src/asm/lexer.c
@@ -281,6 +281,7 @@
 	char *captureBuf; /* Buffer to send the captured text to if non-NULL */
 	size_t captureCapacity; /* Size of the buffer above */
 
+	bool disableMacroArgs;
 	size_t macroArgScanDistance; /* Max distance already scanned for macro args */
 	bool expandStrings;
 	struct Expansion *expansions;
@@ -299,6 +300,7 @@
 	state->capturing = false;
 	state->captureBuf = NULL;
 
+	state->disableMacroArgs = false;
 	state->macroArgScanDistance = 0;
 	state->expandStrings = true;
 	state->expansions = NULL;
@@ -723,8 +725,8 @@
 
 	if (distance >= lexerState->macroArgScanDistance) {
 		lexerState->macroArgScanDistance = distance + 1; /* Do not consider again */
-		/* If not capturing and character is a backslash, check for a macro arg */
-		if (!lexerState->capturing && c == '\\') {
+		/* If enabled and character is a backslash, check for a macro arg */
+		if (!lexerState->disableMacroArgs && c == '\\') {
 			distance++;
 			lexerState->macroArgScanDistance++;
 			c = peekInternal(distance);
@@ -873,6 +875,7 @@
 static void discardComment(void)
 {
 	dbgPrint("Discarding comment\n");
+	lexerState->disableMacroArgs = true;
 	for (;;) {
 		int c = peek(0);
 
@@ -880,6 +883,7 @@
 			break;
 		shiftChars(1);
 	}
+	lexerState->disableMacroArgs = false;
 }
 
 /* Function to read a line continuation */
@@ -1748,10 +1752,8 @@
 	int token;
 	bool atLineStart = lexerState->atLineStart;
 
-	/* Prevent expanding macro args in this state by enabling capture to nothing */
-	lexerState->capturing = true;
-	lexerState->captureSize = 0;
-	lexerState->captureBuf = NULL;
+	/* Prevent expanding macro args in this state */
+	lexerState->disableMacroArgs = true;
 
 	for (;;) {
 		if (atLineStart) {
@@ -1811,7 +1813,7 @@
 	}
 finish:
 
-	lexerState->capturing = false;
+	lexerState->disableMacroArgs = false;
 	lexerState->atLineStart = false;
 
 	return token;
@@ -1881,6 +1883,7 @@
 
 	lexerState->capturing = true;
 	lexerState->captureSize = 0;
+	lexerState->disableMacroArgs = true;
 
 	if (lexerState->isMmapped) {
 		return &lexerState->ptr[lexerState->offset];
@@ -1955,6 +1958,7 @@
 	*capture = captureStart;
 	*size = lexerState->captureSize - strlen("ENDR");
 	lexerState->captureBuf = NULL;
+	lexerState->disableMacroArgs = false;
 }
 
 void lexer_CaptureMacroBody(char **capture, size_t *size)
@@ -2043,4 +2047,5 @@
 	*capture = captureStart;
 	*size = lexerState->captureSize - strlen("ENDM");
 	lexerState->captureBuf = NULL;
+	lexerState->disableMacroArgs = false;
 }