1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-05-31 12:48:10 +00:00

LibCpp: Parser no longer holds the program's source

After we moved to storing the text of each token in the token itself,
we no longer have to store the source of the program in the Parser.

This makes more sense because the parser should deal with tokens, not
with raw source code.
This commit is contained in:
Itamar 2021-03-12 15:56:30 +02:00 committed by Andreas Kling
parent 8a102fe3ec
commit 5b22f6f45a
2 changed files with 6 additions and 12 deletions

View file

@ -38,14 +38,11 @@
namespace Cpp {
Parser::Parser(const StringView& program, const String& filename, Preprocessor::Definitions&& definitions)
: m_program(program)
, m_definitions(move(definitions))
: m_definitions(move(definitions))
, m_filename(filename)
{
initialize_program_tokens();
initialize_program_tokens(program);
#if CPP_DEBUG
dbgln("Program:");
dbgln("{}", m_program);
dbgln("Tokens:");
for (auto& token : m_tokens) {
StringView text;
@ -57,9 +54,9 @@ Parser::Parser(const StringView& program, const String& filename, Preprocessor::
}
#endif
}
void Parser::initialize_program_tokens()
void Parser::initialize_program_tokens(const StringView& program)
{
Lexer lexer(m_program);
Lexer lexer(program);
for (auto& token : lexer.lex()) {
if (token.type() == Token::Type::Whitespace)
continue;
@ -713,8 +710,7 @@ String Parser::text_in_range(Position start, Position end) const
VERIFY(start_token_index.has_value());
VERIFY(end_node_index.has_value());
StringBuilder text;
for(size_t i = start_token_index.value(); i <= end_node_index.value(); ++i)
{
for (size_t i = start_token_index.value(); i <= end_node_index.value(); ++i) {
text.append(m_tokens[i].text());
}
return text.build();