1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-05-28 13:55:06 +00:00

LibCpp: Do macro substitution in the preprocessor instead of the parser

After this change, the parser is completely separated from preprocessor
concepts.
This commit is contained in:
Itamar 2021-08-06 12:23:20 +03:00 committed by Andreas Kling
parent 0c4dc00f01
commit 9da9398bf0
5 changed files with 55 additions and 65 deletions

View file

@ -15,11 +15,10 @@
namespace Cpp {
Parser::Parser(Vector<Token> const& tokens, const String& filename, Preprocessor::Definitions const& definitions)
: m_preprocessor_definitions(move(definitions))
, m_filename(filename)
Parser::Parser(Vector<Token> tokens, String const& filename)
: m_filename(filename)
, m_tokens(move(tokens))
{
initialize_program_tokens(tokens);
if constexpr (CPP_DEBUG) {
dbgln("Tokens:");
for (size_t i = 0; i < m_tokens.size(); ++i) {
@ -28,22 +27,6 @@ Parser::Parser(Vector<Token> const& tokens, const String& filename, Preprocessor
}
}
void Parser::initialize_program_tokens(Vector<Token> const& tokens)
{
for (auto& token : tokens) {
if (token.type() == Token::Type::Whitespace)
continue;
if (token.type() == Token::Type::Identifier) {
if (auto defined_value = m_preprocessor_definitions.find(text_of_token(token)); defined_value != m_preprocessor_definitions.end()) {
add_tokens_for_preprocessor(token, defined_value->value);
m_replaced_preprocessor_tokens.append({ token, defined_value->value });
continue;
}
}
m_tokens.append(move(token));
}
}
NonnullRefPtr<TranslationUnit> Parser::parse()
{
LOG_SCOPE();
@ -1396,19 +1379,6 @@ bool Parser::match_ellipsis()
return false;
return peek().type() == Token::Type::Dot && peek(1).type() == Token::Type::Dot && peek(2).type() == Token::Type::Dot;
}
void Parser::add_tokens_for_preprocessor(Token const& replaced_token, Preprocessor::DefinedValue& definition)
{
if (!definition.value.has_value())
return;
Lexer lexer(definition.value.value());
for (auto token : lexer.lex()) {
if (token.type() == Token::Type::Whitespace)
continue;
token.set_start(replaced_token.start());
token.set_end(replaced_token.end());
m_tokens.append(move(token));
}
}
NonnullRefPtr<NamespaceDeclaration> Parser::parse_namespace_declaration(ASTNode& parent, bool is_nested_namespace)
{