This commit is contained in:
Jan 2025-04-21 18:12:57 +01:00
parent 1cccfe352b
commit 02b2c93e30
No known key found for this signature in database
GPG Key ID: 44B581F78FF5C57C
4 changed files with 43 additions and 43 deletions

View File

@ -18,10 +18,10 @@ template<typename TokenType> class AbstractLexer : public ILexer<TokenType>
protected: protected:
std::deque<TokenType> m_token_cache; std::deque<TokenType> m_token_cache;
std::deque<ParserLine> m_line_cache; std::deque<ParserLine> m_line_cache;
IParserLineStream* const m_stream; IParserLineStream* m_stream;
unsigned m_line_index; size_t m_line_index;
unsigned m_current_line_offset; size_t m_current_line_offset;
explicit AbstractLexer(IParserLineStream* stream) explicit AbstractLexer(IParserLineStream* stream)
: m_stream(stream), : m_stream(stream),

View File

@ -5,7 +5,6 @@
#include "Parsing/Simple/Expression/ISimpleExpression.h" #include "Parsing/Simple/Expression/ISimpleExpression.h"
#include "Parsing/Simple/Expression/SimpleExpressionMatchers.h" #include "Parsing/Simple/Expression/SimpleExpressionMatchers.h"
#include "Parsing/Simple/SimpleExpressionInterpreter.h" #include "Parsing/Simple/SimpleExpressionInterpreter.h"
#include "Utils/ClassUtils.h"
#include "Utils/StringUtils.h" #include "Utils/StringUtils.h"
#include <regex> #include <regex>
@ -40,7 +39,7 @@ DefinesStreamProxy::DefineParameterPosition::DefineParameterPosition(const unsig
} }
DefinesStreamProxy::Define::Define() DefinesStreamProxy::Define::Define()
: m_contains_token_pasting_operators(false){}; : m_contains_token_pasting_operators(false) {};
DefinesStreamProxy::Define::Define(std::string name, std::string value) DefinesStreamProxy::Define::Define(std::string name, std::string value)
: m_name(std::move(name)), : m_name(std::move(name)),
@ -114,7 +113,7 @@ void DefinesStreamProxy::Define::IdentifyParameters(const std::vector<std::strin
{ {
m_contains_token_pasting_operators = true; m_contains_token_pasting_operators = true;
// Skip next char since it's # anyway and we do not want to count it as stringize // Skip next char since it's # anyway, and we do not want to count it as stringize
i++; i++;
} }
} }
@ -484,7 +483,7 @@ bool DefinesStreamProxy::MatchEndifDirective(const ParserLine& line, const size_
return true; return true;
} }
bool DefinesStreamProxy::MatchDirectives(ParserLine& line) bool DefinesStreamProxy::MatchDirectives(const ParserLine& line)
{ {
size_t directiveStartPos; size_t directiveStartPos;
size_t directiveEndPos; size_t directiveEndPos;
@ -522,7 +521,7 @@ bool DefinesStreamProxy::FindMacroForIdentifier(const std::string& input,
} }
void DefinesStreamProxy::ExtractParametersFromMacroUsage( void DefinesStreamProxy::ExtractParametersFromMacroUsage(
const ParserLine& line, unsigned& linePos, MacroParameterState& state, const std::string& input, unsigned& inputPos) const ParserLine& line, const unsigned& linePos, MacroParameterState& state, const std::string& input, unsigned& inputPos)
{ {
if (input[inputPos] != '(') if (input[inputPos] != '(')
return; return;
@ -588,7 +587,7 @@ void DefinesStreamProxy::ExpandDefinedExpressions(ParserLine& line) const
} }
} }
bool DefinesStreamProxy::FindNextMacro(const std::string& input, unsigned& inputPos, unsigned& defineStart, const DefinesStreamProxy::Define*& define) bool DefinesStreamProxy::FindNextMacro(const std::string& input, unsigned& inputPos, unsigned& defineStart, const Define*& define) const
{ {
const auto inputSize = input.size(); const auto inputSize = input.size();
auto wordStart = 0u; auto wordStart = 0u;
@ -655,7 +654,7 @@ bool DefinesStreamProxy::FindNextMacro(const std::string& input, unsigned& input
namespace namespace
{ {
enum class TokenPasteTokenType enum class TokenPasteTokenType : std::uint8_t
{ {
NONE, NONE,
STRING, STRING,
@ -673,13 +672,7 @@ namespace
{ {
} }
~TokenPasteToken() = default; void SetFromInput(const ParserLine& line, const unsigned& linePos, const std::string& input, unsigned& offset)
TokenPasteToken(const TokenPasteToken& other) = default;
TokenPasteToken(TokenPasteToken&& other) = default;
TokenPasteToken& operator=(const TokenPasteToken& other) = default;
TokenPasteToken& operator=(TokenPasteToken&& other) noexcept = default;
void SetFromInput(ParserLine& line, unsigned& linePos, const std::string& input, unsigned& offset)
{ {
m_start = offset; m_start = offset;
@ -698,7 +691,7 @@ namespace
} }
if (offset >= inputSize) if (offset >= inputSize)
throw new ParsingException(TokenPos(*line.m_filename, line.m_line_number, static_cast<int>(linePos + 1)), throw ParsingException(TokenPos(*line.m_filename, line.m_line_number, static_cast<int>(linePos + 1)),
"Token-pasting operator cannot be used on unclosed string"); "Token-pasting operator cannot be used on unclosed string");
offset++; offset++;
@ -743,11 +736,15 @@ namespace
unsigned m_end; unsigned m_end;
}; };
void EmitPastedTokens( void EmitPastedTokens(const ParserLine& line,
ParserLine& line, unsigned& linePos, std::ostream& out, const std::string& input, const TokenPasteToken& token0, const TokenPasteToken& token1) const unsigned& linePos,
std::ostream& out,
const std::string& input,
const TokenPasteToken& token0,
const TokenPasteToken& token1)
{ {
if ((token0.m_type == TokenPasteTokenType::STRING) != (token1.m_type == TokenPasteTokenType::STRING)) if ((token0.m_type == TokenPasteTokenType::STRING) != (token1.m_type == TokenPasteTokenType::STRING))
throw new ParsingException(TokenPos(*line.m_filename, line.m_line_number, static_cast<int>(linePos + 1)), throw ParsingException(TokenPos(*line.m_filename, line.m_line_number, static_cast<int>(linePos + 1)),
"String token can only use token-pasting operator on other string token"); "String token can only use token-pasting operator on other string token");
if (token0.m_type == TokenPasteTokenType::STRING) if (token0.m_type == TokenPasteTokenType::STRING)
{ {
@ -767,7 +764,7 @@ namespace
} // namespace } // namespace
void DefinesStreamProxy::ProcessTokenPastingOperators( void DefinesStreamProxy::ProcessTokenPastingOperators(
ParserLine& line, unsigned& linePos, std::vector<const Define*>& callstack, std::string& input, unsigned& inputPos) const ParserLine& line, const unsigned& linePos, std::vector<const Define*>& callstack, std::string& input, unsigned& inputPos)
{ {
std::ostringstream ss; std::ostringstream ss;
@ -789,7 +786,7 @@ void DefinesStreamProxy::ProcessTokenPastingOperators(
if (c == '#' && IsTokenPastingOperatorForwardLookup(input, i)) if (c == '#' && IsTokenPastingOperatorForwardLookup(input, i))
{ {
if (currentToken.m_type == TokenPasteTokenType::NONE) if (currentToken.m_type == TokenPasteTokenType::NONE)
throw new ParsingException(CreatePos(line, linePos), "Cannot use token-pasting operator without previous token"); throw ParsingException(CreatePos(line, linePos), "Cannot use token-pasting operator without previous token");
if (previousToken.m_end < currentToken.m_start) if (previousToken.m_end < currentToken.m_start)
ss << std::string(input, previousToken.m_end, currentToken.m_start - previousToken.m_end); ss << std::string(input, previousToken.m_end, currentToken.m_start - previousToken.m_end);
@ -816,12 +813,12 @@ void DefinesStreamProxy::ProcessTokenPastingOperators(
ss << std::string(input, previousToken.m_end, inputSize - previousToken.m_end); ss << std::string(input, previousToken.m_end, inputSize - previousToken.m_end);
if (pasteNext) if (pasteNext)
throw new ParsingException(CreatePos(line, linePos), "Cannot use token-pasting operator without following token"); throw ParsingException(CreatePos(line, linePos), "Cannot use token-pasting operator without following token");
input = ss.str(); input = ss.str();
} }
void DefinesStreamProxy::InsertMacroParameters(std::ostringstream& out, const DefinesStreamProxy::Define* macro, std::vector<std::string>& parameterValues) void DefinesStreamProxy::InsertMacroParameters(std::ostringstream& out, const Define* macro, const std::vector<std::string>& parameterValues)
{ {
if (parameterValues.empty() || macro->m_parameter_positions.empty()) if (parameterValues.empty() || macro->m_parameter_positions.empty())
{ {
@ -829,7 +826,7 @@ void DefinesStreamProxy::InsertMacroParameters(std::ostringstream& out, const De
return; return;
} }
auto lastPos = 0u; auto lastPos = 0uz;
for (const auto& parameterPosition : macro->m_parameter_positions) for (const auto& parameterPosition : macro->m_parameter_positions)
{ {
if (lastPos < parameterPosition.m_parameter_position) if (lastPos < parameterPosition.m_parameter_position)
@ -857,7 +854,7 @@ void DefinesStreamProxy::ExpandMacro(ParserLine& line,
std::ostringstream& out, std::ostringstream& out,
std::vector<const Define*>& callstack, std::vector<const Define*>& callstack,
const DefinesStreamProxy::Define* macro, const DefinesStreamProxy::Define* macro,
std::vector<std::string>& parameterValues) const std::vector<std::string>& parameterValues)
{ {
std::ostringstream rawOutput; std::ostringstream rawOutput;
InsertMacroParameters(rawOutput, macro, parameterValues); InsertMacroParameters(rawOutput, macro, parameterValues);
@ -873,7 +870,7 @@ void DefinesStreamProxy::ExpandMacro(ParserLine& line,
} }
void DefinesStreamProxy::ContinueMacroParameters( void DefinesStreamProxy::ContinueMacroParameters(
const ParserLine& line, unsigned& linePos, MacroParameterState& state, const std::string& input, unsigned& inputPos) const ParserLine& line, const unsigned& linePos, MacroParameterState& state, const std::string& input, unsigned& inputPos)
{ {
const auto inputLength = input.size(); const auto inputLength = input.size();
while (state.m_parameter_state != ParameterState::NOT_IN_PARAMETERS && inputPos < inputLength) while (state.m_parameter_state != ParameterState::NOT_IN_PARAMETERS && inputPos < inputLength)

View File

@ -107,26 +107,29 @@ private:
_NODISCARD bool MatchIfdefDirective(const ParserLine& line, size_t directiveStartPosition, size_t directiveEndPosition); _NODISCARD bool MatchIfdefDirective(const ParserLine& line, size_t directiveStartPosition, size_t directiveEndPosition);
_NODISCARD bool MatchElseDirective(const ParserLine& line, size_t directiveStartPosition, size_t directiveEndPosition); _NODISCARD bool MatchElseDirective(const ParserLine& line, size_t directiveStartPosition, size_t directiveEndPosition);
_NODISCARD bool MatchEndifDirective(const ParserLine& line, size_t directiveStartPosition, size_t directiveEndPosition); _NODISCARD bool MatchEndifDirective(const ParserLine& line, size_t directiveStartPosition, size_t directiveEndPosition);
_NODISCARD bool MatchDirectives(ParserLine& line); _NODISCARD bool MatchDirectives(const ParserLine& line);
void ExtractParametersFromMacroUsage(const ParserLine& line, unsigned& linePos, MacroParameterState& state, const std::string& input, unsigned& inputPos); static void ExtractParametersFromMacroUsage(
bool FindMacroForIdentifier(const std::string& input, unsigned wordStart, unsigned wordEnd, const Define*& value) const; const ParserLine& line, const unsigned& linePos, MacroParameterState& state, const std::string& input, unsigned& inputPos);
bool FindMacroForIdentifier(const std::string& input, unsigned identifierStart, unsigned identifierEnd, const Define*& value) const;
static bool MatchDefinedExpression(const ParserLine& line, size_t& pos, std::string& definitionName); static bool MatchDefinedExpression(const ParserLine& line, size_t& pos, std::string& definitionName);
void ExpandDefinedExpressions(ParserLine& line) const; void ExpandDefinedExpressions(ParserLine& line) const;
bool FindNextMacro(const std::string& input, unsigned& inputPos, unsigned& defineStart, const DefinesStreamProxy::Define*& define); bool FindNextMacro(const std::string& input, unsigned& inputPos, unsigned& defineStart, const DefinesStreamProxy::Define*& define) const;
void ProcessTokenPastingOperators(ParserLine& line, unsigned& linePos, std::vector<const Define*>& callstack, std::string& input, unsigned& inputPos); static void ProcessTokenPastingOperators(
void InsertMacroParameters(std::ostringstream& out, const DefinesStreamProxy::Define* macro, std::vector<std::string>& parameterValues); const ParserLine& line, const unsigned& linePos, std::vector<const Define*>& callstack, std::string& input, unsigned& inputPos);
static void InsertMacroParameters(std::ostringstream& out, const DefinesStreamProxy::Define* macro, const std::vector<std::string>& parameterValues);
void ExpandMacro(ParserLine& line, void ExpandMacro(ParserLine& line,
unsigned& linePos, unsigned& linePos,
std::ostringstream& out, std::ostringstream& out,
std::vector<const Define*>& callstack, std::vector<const Define*>& callstack,
const DefinesStreamProxy::Define* macro, const DefinesStreamProxy::Define* macro,
std::vector<std::string>& parameterValues); const std::vector<std::string>& parameterValues);
void ContinueMacroParameters(const ParserLine& line, unsigned& linePos, MacroParameterState& state, const std::string& input, unsigned& inputPos); static void
ContinueMacroParameters(const ParserLine& line, const unsigned& linePos, MacroParameterState& state, const std::string& input, unsigned& inputPos);
void ContinueMultiLineMacro(ParserLine& line); void ContinueMultiLineMacro(ParserLine& line);
void ProcessNestedMacros(ParserLine& line, unsigned& linePos, std::vector<const Define*>& callstack, std::string& input, unsigned& inputPos); void ProcessNestedMacros(ParserLine& line, unsigned& linePos, std::vector<const Define*>& callstack, std::string& input, unsigned& inputPos);

View File

@ -42,7 +42,7 @@ bool ArgumentParser::ParseArguments(std::vector<std::string>& args)
m_path = args[0]; m_path = args[0];
const auto argCount = args.size(); const auto argCount = args.size();
for (unsigned argIndex = 1u; argIndex < argCount; argIndex++) for (auto argIndex = 1uz; argIndex < argCount; argIndex++)
{ {
auto& arg = args[argIndex]; auto& arg = args[argIndex];
@ -85,7 +85,7 @@ bool ArgumentParser::ParseArguments(std::vector<std::string>& args)
return false; return false;
} }
if (m_matched_options.find(matchedOption) != m_matched_options.end()) if (m_matched_options.contains(matchedOption))
{ {
if (!matchedOption->m_multi_use) if (!matchedOption->m_multi_use)
{ {
@ -106,7 +106,7 @@ bool ArgumentParser::ParseArguments(std::vector<std::string>& args)
} }
auto& parameters = m_matched_options[matchedOption]; auto& parameters = m_matched_options[matchedOption];
for (unsigned parameterIndex = 0; parameterIndex < parameterCount; parameterIndex++) for (auto parameterIndex = 0uz; parameterIndex < parameterCount; parameterIndex++)
{ {
std::string& param = args[argIndex + parameterIndex + 1]; std::string& param = args[argIndex + parameterIndex + 1];
@ -116,14 +116,14 @@ bool ArgumentParser::ParseArguments(std::vector<std::string>& args)
return false; return false;
} }
parameters.push_back(param); parameters.emplace_back(param);
} }
argIndex += parameterCount; argIndex += parameterCount;
} }
else else
{ {
m_matched_arguments.push_back(arg); m_matched_arguments.emplace_back(arg);
} }
} }