Merge pull request #13279 from jordan-woyak/delimited-token

InputCommon/ExpressionParser: Require delimited tokens actually have their terminating delimiter.
This commit is contained in:
Tilka 2025-01-20 23:14:22 +00:00 committed by GitHub
commit 510a688a2a
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 11 additions and 8 deletions

View File

@ -92,12 +92,15 @@ Lexer::Lexer(std::string expr_) : expr(std::move(expr_))
it = expr.begin(); it = expr.begin();
} }
std::string Lexer::FetchDelimString(char delim) Token Lexer::GetDelimitedToken(TokenType type, char delimeter)
{ {
const std::string result = FetchCharsWhile([delim](char c) { return c != delim; }); const std::string value = FetchCharsWhile([&](char c) { return c != delimeter && c != '\n'; });
if (it != expr.end())
++it; if (it == expr.end() || *it != delimeter)
return result; return Token(TOK_INVALID);
++it;
return Token(type, value);
} }
std::string Lexer::FetchWordChars() std::string Lexer::FetchWordChars()
@ -110,7 +113,7 @@ std::string Lexer::FetchWordChars()
Token Lexer::GetDelimitedLiteral() Token Lexer::GetDelimitedLiteral()
{ {
return Token(TOK_LITERAL, FetchDelimString('\'')); return GetDelimitedToken(TOK_LITERAL, '\'');
} }
Token Lexer::GetVariable() Token Lexer::GetVariable()
@ -120,7 +123,7 @@ Token Lexer::GetVariable()
Token Lexer::GetFullyQualifiedControl() Token Lexer::GetFullyQualifiedControl()
{ {
return Token(TOK_CONTROL, FetchDelimString('`')); return GetDelimitedToken(TOK_CONTROL, '`');
} }
Token Lexer::GetBareword(char first_char) Token Lexer::GetBareword(char first_char)

View File

@ -91,8 +91,8 @@ private:
return value; return value;
} }
std::string FetchDelimString(char delim);
std::string FetchWordChars(); std::string FetchWordChars();
Token GetDelimitedToken(TokenType type, char delimeter);
Token GetDelimitedLiteral(); Token GetDelimitedLiteral();
Token GetVariable(); Token GetVariable();
Token GetFullyQualifiedControl(); Token GetFullyQualifiedControl();