Prevent expanding macro args in comments

Also use a cleaner way, instead of hardcoding to capture
This commit is contained in:
ISSOtm
2020-08-18 13:16:56 +02:00
parent ece6853e0f
commit f7b7a97407

View File

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