mirror of
https://github.com/LadybirdBrowser/ladybird.git
synced 2025-06-11 18:20:43 +09:00
LibCpp: Do lexing in the Preprocessor
We now call Preprocessor::process_and_lex() and pass the result to the parser. Doing the lexing in the preprocessor will allow us to maintain the original position information of tokens after substituting definitions.
This commit is contained in:
parent
bf7262681e
commit
4673a517f6
Notes:
sideshowbarker
2024-07-18 07:18:16 +09:00
Author: https://github.com/itamar8910
Commit: 4673a517f6
Pull-request: https://github.com/SerenityOS/serenity/pull/9230
Reviewed-by: https://github.com/alimpfard
9 changed files with 42 additions and 39 deletions
|
@ -8,6 +8,7 @@
|
|||
#include <AK/Assertions.h>
|
||||
#include <AK/GenericLexer.h>
|
||||
#include <AK/StringBuilder.h>
|
||||
#include <LibCpp/Lexer.h>
|
||||
#include <ctype.h>
|
||||
|
||||
namespace Cpp {
|
||||
|
@ -36,8 +37,9 @@ Preprocessor::Preprocessor(const String& filename, const StringView& program)
|
|||
}
|
||||
}
|
||||
|
||||
const String& Preprocessor::process()
|
||||
Vector<Token> Preprocessor::process_and_lex()
|
||||
{
|
||||
Vector<Token> all_tokens;
|
||||
for (; m_line_index < m_lines.size(); ++m_line_index) {
|
||||
auto& line = m_lines[m_line_index];
|
||||
|
||||
|
@ -51,14 +53,14 @@ const String& Preprocessor::process()
|
|||
}
|
||||
|
||||
if (include_in_processed_text) {
|
||||
m_builder.append(line);
|
||||
for (auto& token : process_line(line)) {
|
||||
if (token.type() != Token::Type::Whitespace)
|
||||
all_tokens.append(token);
|
||||
}
|
||||
}
|
||||
|
||||
m_builder.append("\n");
|
||||
}
|
||||
|
||||
m_processed_text = m_builder.to_string();
|
||||
return m_processed_text;
|
||||
return all_tokens;
|
||||
}
|
||||
|
||||
static void consume_whitespace(GenericLexer& lexer)
|
||||
|
@ -224,10 +226,14 @@ void Preprocessor::handle_preprocessor_keyword(const StringView& keyword, Generi
|
|||
}
|
||||
}
|
||||
|
||||
const String& Preprocessor::processed_text()
|
||||
Vector<Token> Preprocessor::process_line(const StringView& line)
|
||||
{
|
||||
VERIFY(!m_processed_text.is_null());
|
||||
return m_processed_text;
|
||||
Lexer line_lexer { line, m_line_index };
|
||||
auto tokens = line_lexer.lex();
|
||||
|
||||
// TODO: Go over tokens of line, do token substitution
|
||||
|
||||
return tokens;
|
||||
}
|
||||
|
||||
};
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue