From c94ec85460070b91f9c337dec59b7f9f45e0f825 Mon Sep 17 00:00:00 2001 From: Jordan Woyak Date: Thu, 7 Nov 2024 19:34:55 -0600 Subject: InputCommon: Make input expression multiline-comment tokenizing less hacky. --- .../ControlReference/ExpressionParser.cpp | 34 ++++++++++------------ 1 file changed, 15 insertions(+), 19 deletions(-) (limited to 'Source/Core/InputCommon/ControlReference/ExpressionParser.cpp') diff --git a/Source/Core/InputCommon/ControlReference/ExpressionParser.cpp b/Source/Core/InputCommon/ControlReference/ExpressionParser.cpp index 7fdf677a91..f3cb5206af 100644 --- a/Source/Core/InputCommon/ControlReference/ExpressionParser.cpp +++ b/Source/Core/InputCommon/ControlReference/ExpressionParser.cpp @@ -181,7 +181,19 @@ Token Lexer::NextToken() case '*': return Token(TOK_MUL); case '/': + { + // Handle /* */ style comments. + if (it != expr.end() && *it == '*') + { + ++it; + const auto end_of_comment = expr.find("*/", it - expr.begin()); + if (end_of_comment == std::string::npos) + return Token(TOK_INVALID); + it = expr.begin() + end_of_comment + 2; + return Token(TOK_COMMENT); + } return Token(TOK_DIV); + } case '%': return Token(TOK_MOD); case '=': @@ -214,26 +226,10 @@ ParseStatus Lexer::Tokenize(std::vector& tokens) { while (true) { - const std::size_t string_position = it - expr.begin(); + const std::string::iterator prev_it = it; Token tok = NextToken(); - - tok.string_position = string_position; - tok.string_length = it - expr.begin(); - - // Handle /* */ style comments. - if (tok.type == TOK_DIV && PeekToken().type == TOK_MUL) - { - const auto end_of_comment = expr.find("*/", it - expr.begin()); - - if (end_of_comment == std::string::npos) - return ParseStatus::SyntaxError; - - tok.type = TOK_COMMENT; - tok.string_length = end_of_comment + 4; - - it = expr.begin() + end_of_comment + 2; - } - + tok.string_position = prev_it - expr.begin(); + tok.string_length = it - prev_it; tokens.push_back(tok); if (tok.type == TOK_INVALID) -- cgit v1.2.3 From a618854413b50f6056e4cf614e8b8c447cdd9611 Mon Sep 17 00:00:00 2001 From: Jordan Woyak Date: Fri, 17 Jan 2025 02:07:23 -0600 Subject: ExpressionParser: Remove RemoveInertTokens. --- .../ControlReference/ExpressionParser.cpp | 21 +++++++++++---------- 1 file changed, 11 insertions(+), 10 deletions(-) (limited to 'Source/Core/InputCommon/ControlReference/ExpressionParser.cpp') diff --git a/Source/Core/InputCommon/ControlReference/ExpressionParser.cpp b/Source/Core/InputCommon/ControlReference/ExpressionParser.cpp index f3cb5206af..00e3200989 100644 --- a/Source/Core/InputCommon/ControlReference/ExpressionParser.cpp +++ b/Source/Core/InputCommon/ControlReference/ExpressionParser.cpp @@ -671,6 +671,11 @@ ParseResult ParseResult::MakeErrorResult(Token token, std::string description) return result; } +bool IsInertToken(const Token& tok) +{ + return tok.type == TOK_COMMENT || tok.type == TOK_WHITESPACE; +} + class Parser { public: @@ -700,7 +705,12 @@ private: return tok; } - Token Peek() { return *m_it; } + Token Peek() + { + while (IsInertToken(*m_it)) + ++m_it; + return *m_it; + } bool Expects(TokenType type) { @@ -959,18 +969,9 @@ static ParseResult ParseComplexExpression(const std::string& str) if (tokenize_status != ParseStatus::Successful) return ParseResult::MakeErrorResult(Token(TOK_INVALID), Common::GetStringT("Tokenizing failed.")); - - RemoveInertTokens(&tokens); return ParseTokens(tokens); } -void RemoveInertTokens(std::vector* tokens) -{ - std::erase_if(*tokens, [](const Token& tok) { - return tok.type == TOK_COMMENT || tok.type == TOK_WHITESPACE; - }); -} - static std::unique_ptr ParseBarewordExpression(const std::string& str) { ControlQualifier qualifier; -- cgit v1.2.3