Small lexer refactors, commenting when tail recursion occurs

This commit is contained in:
Rangi42
2025-07-19 23:37:59 -04:00
parent 2935942667
commit 8d1b111692

View File

@@ -815,37 +815,37 @@ static int peek() {
// If character is a backslash, check for a macro arg // If character is a backslash, check for a macro arg
++lexerState->macroArgScanDistance; ++lexerState->macroArgScanDistance;
c = lexerState->peekCharAhead(); c = lexerState->peekCharAhead();
if (isMacroChar(c)) { if (!isMacroChar(c)) {
shiftChar(); return '\\';
shiftChar();
std::shared_ptr<std::string> str = readMacroArg(c);
// If the macro arg is invalid or an empty string, it cannot be expanded,
// so skip it and keep peeking.
if (!str || str->empty()) {
return peek();
}
beginExpansion(str, std::nullopt);
// Assuming macro args can't be recursive (I'll be damned if a way
// is found...), then we mark the entire macro arg as scanned.
lexerState->macroArgScanDistance += str->length();
c = str->front();
} else {
c = '\\';
} }
shiftChar();
shiftChar();
std::shared_ptr<std::string> str = readMacroArg(c);
// If the macro arg is invalid or an empty string, it cannot be expanded,
// so skip it and keep peeking.
if (!str || str->empty()) {
return peek(); // Tail recursion
}
beginExpansion(str, std::nullopt);
// Assuming macro args can't be recursive (I'll be damned if a way
// is found...), then we mark the entire macro arg as scanned.
lexerState->macroArgScanDistance += str->length();
return str->front();
} else if (c == '{' && !lexerState->disableInterpolation) { } else if (c == '{' && !lexerState->disableInterpolation) {
// If character is an open brace, do symbol interpolation // If character is an open brace, do symbol interpolation
shiftChar(); shiftChar();
if (std::shared_ptr<std::string> str = readInterpolation(0); str) { if (std::shared_ptr<std::string> str = readInterpolation(0); str) {
beginExpansion(str, *str); beginExpansion(str, *str);
} }
return peek(); return peek(); // Tail recursion
} else {
return c;
} }
return c;
} }
static void shiftChar() { static void shiftChar() {
@@ -1687,7 +1687,7 @@ static Token yylex_NORMAL() {
return Token(nextToken); return Token(nextToken);
} }
for (;;) { for (;; lexerState->atLineStart = false) {
int c = nextChar(); int c = nextChar();
switch (c) { switch (c) {
@@ -1698,7 +1698,7 @@ static Token yylex_NORMAL() {
[[fallthrough]]; [[fallthrough]];
case ' ': case ' ':
case '\t': case '\t':
break; continue;
// Handle unambiguous single-char tokens // Handle unambiguous single-char tokens
@@ -1775,11 +1775,10 @@ static Token yylex_NORMAL() {
case '*': case '*':
shiftChar(); shiftChar();
discardBlockComment(); discardBlockComment();
break; continue;
default: default:
return Token(T_(OP_DIV)); return Token(T_(OP_DIV));
} }
break;
case '|': // Either |=, binary OR, or logical OR case '|': // Either |=, binary OR, or logical OR
switch (peek()) { switch (peek()) {
@@ -1967,7 +1966,7 @@ static Token yylex_NORMAL() {
// Macro args were handled by `peek`, and character escapes do not exist // Macro args were handled by `peek`, and character escapes do not exist
// outside of string literals, so this must be a line continuation. // outside of string literals, so this must be a line continuation.
discardLineContinuation(); discardLineContinuation();
break; continue;
// Handle raw strings... or fall through if '#' is not followed by '"' // Handle raw strings... or fall through if '#' is not followed by '"'
@@ -1991,7 +1990,7 @@ static Token yylex_NORMAL() {
if (!lexerState->capturing) { if (!lexerState->capturing) {
reportGarbageCharacters(c); reportGarbageCharacters(c);
} }
break; continue;
} }
Token token = readIdentifier(c, raw); Token token = readIdentifier(c, raw);
@@ -2020,7 +2019,7 @@ static Token yylex_NORMAL() {
assume(str); assume(str);
beginExpansion(str, sym->name); beginExpansion(str, sym->name);
continue; // Restart, reading from the new buffer return yylex_NORMAL(); // Tail recursion
} }
} }
@@ -2044,7 +2043,6 @@ static Token yylex_NORMAL() {
return token; return token;
} }
lexerState->atLineStart = false;
} }
} }