Rename some variables left from the C parser (e.g. yylval)

This commit is contained in:
Rangi42
2024-03-29 19:42:23 -04:00
parent cf3e5e15f8
commit eb708ebee5
2 changed files with 40 additions and 42 deletions

View File

@@ -1225,26 +1225,26 @@ static std::shared_ptr<std::string> readInterpolation(size_t depth) {
return nullptr; return nullptr;
} }
static void appendEscapedSubstring(std::string &yylval, std::string const &str) { static void appendEscapedString(std::string &str, std::string const &escape) {
for (char c : str) { for (char c : escape) {
// Escape characters that need escaping // Escape characters that need escaping
switch (c) { switch (c) {
case '\\': case '\\':
case '"': case '"':
case '{': case '{':
yylval += '\\'; str += '\\';
[[fallthrough]]; [[fallthrough]];
default: default:
yylval += c; str += c;
break; break;
case '\n': case '\n':
yylval += "\\n"; str += "\\n";
break; break;
case '\r': case '\r':
yylval += "\\r"; str += "\\r";
break; break;
case '\t': case '\t':
yylval += "\\t"; str += "\\t";
break; break;
} }
} }
@@ -1267,13 +1267,13 @@ static std::string readString(bool raw) {
} }
} }
for (std::string yylval = ""s;;) { for (std::string str = ""s;;) {
int c = peek(); int c = peek();
// '\r', '\n' or EOF ends a single-line string early // '\r', '\n' or EOF ends a single-line string early
if (c == EOF || (!multiline && (c == '\r' || c == '\n'))) { if (c == EOF || (!multiline && (c == '\r' || c == '\n'))) {
error("Unterminated string\n"); error("Unterminated string\n");
return yylval; return str;
} }
// We'll be staying in the string, so we can safely consume the char // We'll be staying in the string, so we can safely consume the char
@@ -1295,12 +1295,12 @@ static std::string readString(bool raw) {
break; break;
shiftChar(); shiftChar();
if (peek() != '"') { if (peek() != '"') {
yylval += '"'; str += '"';
break; break;
} }
shiftChar(); shiftChar();
} }
return yylval; return str;
case '\\': // Character escape or macro arg case '\\': // Character escape or macro arg
if (raw) if (raw)
@@ -1349,8 +1349,8 @@ static std::string readString(bool raw) {
case '9': case '9':
case '<': case '<':
shiftChar(); shiftChar();
if (auto str = readMacroArg(c); str) { if (auto arg = readMacroArg(c); arg) {
yylval.append(*str); str.append(*arg);
} }
continue; // Do not copy an additional character continue; // Do not copy an additional character
@@ -1373,7 +1373,7 @@ static std::string readString(bool raw) {
// (Not interpolations, since they're handled by the function itself...) // (Not interpolations, since they're handled by the function itself...)
lexerState->disableMacroArgs = false; lexerState->disableMacroArgs = false;
if (auto interpolation = readInterpolation(0); interpolation) { if (auto interpolation = readInterpolation(0); interpolation) {
yylval.append(*interpolation); str.append(*interpolation);
} }
lexerState->disableMacroArgs = true; lexerState->disableMacroArgs = true;
continue; // Do not copy an additional character continue; // Do not copy an additional character
@@ -1381,22 +1381,22 @@ static std::string readString(bool raw) {
// Regular characters will just get copied // Regular characters will just get copied
} }
yylval += c; str += c;
} }
} }
static void appendStringLiteral(std::string &yylval, bool raw) { static void appendStringLiteral(std::string &str, bool raw) {
Defer reenableExpansions = scopedDisableExpansions(); Defer reenableExpansions = scopedDisableExpansions();
// We reach this function after reading a single quote, but we also support triple quotes // We reach this function after reading a single quote, but we also support triple quotes
bool multiline = false; bool multiline = false;
yylval += '"'; str += '"';
if (peek() == '"') { if (peek() == '"') {
yylval += '"'; str += '"';
shiftChar(); shiftChar();
if (peek() == '"') { if (peek() == '"') {
// """ begins a multi-line string // """ begins a multi-line string
yylval += '"'; str += '"';
shiftChar(); shiftChar();
multiline = true; multiline = true;
} else { } else {
@@ -1431,14 +1431,14 @@ static void appendStringLiteral(std::string &yylval, bool raw) {
// Only """ ends a multi-line string // Only """ ends a multi-line string
if (peek() != '"') if (peek() != '"')
break; break;
yylval += '"'; str += '"';
shiftChar(); shiftChar();
if (peek() != '"') if (peek() != '"')
break; break;
yylval += '"'; str += '"';
shiftChar(); shiftChar();
} }
yylval += '"'; str += '"';
return; return;
case '\\': // Character escape or macro arg case '\\': // Character escape or macro arg
@@ -1455,7 +1455,7 @@ static void appendStringLiteral(std::string &yylval, bool raw) {
case 'r': case 'r':
case 't': case 't':
// Return that character unchanged // Return that character unchanged
yylval += '\\'; str += '\\';
shiftChar(); shiftChar();
break; break;
@@ -1481,9 +1481,8 @@ static void appendStringLiteral(std::string &yylval, bool raw) {
case '9': case '9':
case '<': { case '<': {
shiftChar(); shiftChar();
auto str = readMacroArg(c); if (auto arg = readMacroArg(c); arg) {
if (str) { appendEscapedString(str, *arg);
appendEscapedSubstring(yylval, *str);
} }
continue; // Do not copy an additional character continue; // Do not copy an additional character
} }
@@ -1507,7 +1506,7 @@ static void appendStringLiteral(std::string &yylval, bool raw) {
// (Not interpolations, since they're handled by the function itself...) // (Not interpolations, since they're handled by the function itself...)
lexerState->disableMacroArgs = false; lexerState->disableMacroArgs = false;
if (auto interpolation = readInterpolation(0); interpolation) { if (auto interpolation = readInterpolation(0); interpolation) {
appendEscapedSubstring(yylval, *interpolation); appendEscapedString(str, *interpolation);
} }
lexerState->disableMacroArgs = true; lexerState->disableMacroArgs = true;
continue; // Do not copy an additional character continue; // Do not copy an additional character
@@ -1515,7 +1514,7 @@ static void appendStringLiteral(std::string &yylval, bool raw) {
// Regular characters will just get copied // Regular characters will just get copied
} }
yylval += c; str += c;
} }
} }
@@ -1820,7 +1819,7 @@ static Token yylex_NORMAL() {
static Token yylex_RAW() { static Token yylex_RAW() {
// This is essentially a modified `appendStringLiteral` // This is essentially a modified `appendStringLiteral`
std::string yylval; std::string str;
size_t parenDepth = 0; size_t parenDepth = 0;
int c; int c;
@@ -1848,15 +1847,15 @@ static Token yylex_RAW() {
switch (c) { switch (c) {
case '"': // String literals inside macro args case '"': // String literals inside macro args
shiftChar(); shiftChar();
appendStringLiteral(yylval, false); appendStringLiteral(str, false);
break; break;
case '#': // Raw string literals inside macro args case '#': // Raw string literals inside macro args
yylval += c; str += c;
shiftChar(); shiftChar();
if (peek() == '"') { if (peek() == '"') {
shiftChar(); shiftChar();
appendStringLiteral(yylval, true); appendStringLiteral(str, true);
} }
break; break;
@@ -1876,7 +1875,7 @@ static Token yylex_RAW() {
discardBlockComment(); discardBlockComment();
continue; continue;
} }
yylval += c; // Append the slash str += c; // Append the slash
break; break;
case ',': // End of macro arg case ',': // End of macro arg
@@ -1941,7 +1940,7 @@ backslash:
default: // Regular characters will just get copied default: // Regular characters will just get copied
append: append:
yylval += c; str += c;
shiftChar(); shiftChar();
break; break;
} }
@@ -1949,8 +1948,8 @@ append:
finish: finish:
// Trim right whitespace // Trim right whitespace
auto rightPos = std::find_if_not(yylval.rbegin(), yylval.rend(), isWhitespace); auto rightPos = std::find_if_not(str.rbegin(), str.rend(), isWhitespace);
yylval.resize(rightPos.base() - yylval.begin()); str.resize(rightPos.base() - str.begin());
// Returning COMMAs to the parser would mean that two consecutive commas // Returning COMMAs to the parser would mean that two consecutive commas
// (i.e. an empty argument) need to return two different tokens (STRING // (i.e. an empty argument) need to return two different tokens (STRING
@@ -1958,7 +1957,7 @@ finish:
// mode end the current macro argument but are not tokenized themselves. // mode end the current macro argument but are not tokenized themselves.
if (c == ',') { if (c == ',') {
shiftChar(); shiftChar();
return Token(T_(STRING), yylval); return Token(T_(STRING), str);
} }
// The last argument may end in a trailing comma, newline, or EOF. // The last argument may end in a trailing comma, newline, or EOF.
@@ -1967,8 +1966,8 @@ finish:
// an empty raw string before it). This will not be treated as a // an empty raw string before it). This will not be treated as a
// macro argument. To pass an empty last argument, use a second // macro argument. To pass an empty last argument, use a second
// trailing comma. // trailing comma.
if (!yylval.empty()) if (!str.empty())
return Token(T_(STRING), yylval); return Token(T_(STRING), str);
lexer_SetMode(LEXER_NORMAL); lexer_SetMode(LEXER_NORMAL);
if (c == '\r' || c == '\n') { if (c == '\r' || c == '\n') {

View File

@@ -323,9 +323,8 @@ int main(int argc, char *argv[]) {
// Init lexer and file stack, providing file info // Init lexer and file stack, providing file info
fstk_Init(mainFileName, maxDepth); fstk_Init(mainFileName, maxDepth);
// Perform parse // Perform parse (`yy::parser` is auto-generated from `parser.y`)
yy::parser parser; if (yy::parser parser; parser.parse() != 0 && nbErrors == 0)
if (parser.parse() != 0 && nbErrors == 0)
nbErrors = 1; nbErrors = 1;
sect_CheckUnionClosed(); sect_CheckUnionClosed();