DevSpec:lex: Fix whitespace around EQUALS, fix backslash at EOF

* We had a prior issue where EQUALS would require that there be no
  whitespace between itself and its operands on either side. We got
  a bad solution from ChatGPT 4o. We got a proper solution now from
  o1.
* Previously, if a string ended with a backslash right before EOF, the
  backslash would be included. Now it will be dropped.
* Merge the two regexes for ignoring whitespace into one.
This commit is contained in:
2025-01-17 11:51:27 -04:00
parent 293c1054d1
commit 99c126a08c
+12 -10
View File
@@ -26,31 +26,33 @@
"|" { return PIPE; }
"(" { return LPAREN; }
")" { return RPAREN; }
[ \t]*"="[ \t]* { return EQUALS; } // Allow optional whitespace around equals
"=" { return EQUALS; } // Match "=" on its own
(\\.|[^=\|\(\) \t\r\n])+ {
std::string token(yytext);
// Unescape logic for backslash, pipe
std::string unescaped;
unescaped.reserve(token.size());
for (size_t i = 0; i < token.size(); ++i)
{
if (token[i] == '\\' && i + 1 < token.size())
{
unescaped.push_back(token[++i]);
}
else
if (token[i] != '\\')
{
unescaped.push_back(token[i]);
continue;
}
/* If a backslash is the final char before EOF, just continue so it gets
* dropped as a side effect.
*/
if (i + 1 >= token.size()) { continue; }
// Else push the char following the backslash.
unescaped.push_back(token[++i]);
}
deviceSpecplval.str = strdup(unescaped.c_str());
return STRING;
}
\r?\n { /* ignore newlines */ }
[ \t]+ { /* ignore whitespace */ }
[ \t\r\n]+ { /* ignore all whitespace, including newlines */ }
. { return yytext[0]; }
%%