InputCommon/ExpressionParser: Require delimited tokens actually have their terminating delimiter.

This commit is contained in:
Jordan Woyak 2024-11-07 21:06:39 -06:00
parent 2c83a256ae
commit 379625a975
2 changed files with 12 additions and 8 deletions

View File

@ -92,12 +92,16 @@ 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; }); std::string value;
if (it != expr.end()) value += FetchCharsWhile([&](char c) { return c != delimeter && c != '\n'; });
++it;
return result; if (it == expr.end() || *it != delimeter)
return Token(TOK_INVALID);
++it;
return Token(type, value);
} }
std::string Lexer::FetchWordChars() std::string Lexer::FetchWordChars()
@ -110,7 +114,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 +124,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();