Skip to content

Commit 06874c2

Browse files
authored
fix: imported patterns used for inheritance were not highlighted. (WerWolv#185)
The problem was that imported files didn't have token sequences to obtain the UDT variables. The fix was to create maps from the file name to the token sequence and then process each imported file to obtain all the variables needed. Function variables are skipped since they can be part of the code. There are also some minor code style corrections and a fix in the text editor where the last line of a selection was not being deleted.
1 parent 44cbfa2 commit 06874c2

3 files changed

Lines changed: 18 additions & 8 deletions

File tree

lib/include/pl/core/preprocessor.hpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,10 @@ namespace pl::core {
113113
}
114114

115115
void appendToNamespaces(std::vector<Token> tokens);
116+
void saveTokens(api::Source *source, const std::vector<Token> &tokens);
117+
const std::map<std::string, std::vector<Token>> &getParsedImports() const {
118+
return m_parsedImports;
119+
}
116120

117121
private:
118122
Preprocessor(const Preprocessor &);
@@ -137,6 +141,7 @@ namespace pl::core {
137141
void registerStatementHandler(const Token::Keyword &name, auto memberFunction);
138142
void reportError(const std::string &message, const std::string &description);
139143

144+
std::map<std::string, std::vector<Token>> m_parsedImports;
140145
std::unordered_map<std::string, api::PragmaHandler> m_pragmaHandlers;
141146
std::unordered_map<Token::Directive, api::DirectiveHandler> m_directiveHandlers;
142147
std::unordered_map<Token::Keyword, api::StatementHandler> m_statementHandlers;

lib/source/pl/core/parser_manager.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ namespace pl::core {
5555

5656
auto result = parser.parse(tokens.value());
5757
oldPreprocessor->appendToNamespaces(tokens.value());
58+
oldPreprocessor->saveTokens(source, tokens.value());
5859

5960
if (result.hasErrs())
6061
return Result::err(result.errs);

lib/source/pl/core/preprocessor.cpp

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -320,6 +320,7 @@ namespace pl::core {
320320
std::ranges::copy(preprocessor.m_pragmas.begin(), preprocessor.m_pragmas.end(), std::inserter(this->m_pragmas, this->m_pragmas.begin()));
321321
std::ranges::copy(preprocessor.m_keys.begin(), preprocessor.m_keys.end(), std::inserter(this->m_keys, this->m_keys.begin()));
322322
std::ranges::copy(preprocessor.m_namespaces.begin(), preprocessor.m_namespaces.end(), std::inserter(this->m_namespaces, this->m_namespaces.begin()));
323+
std::ranges::copy(preprocessor.m_parsedImports.begin(), preprocessor.m_parsedImports.end(),std::inserter(this->m_parsedImports, this->m_parsedImports.begin()));
323324

324325
if (shouldInclude) {
325326
auto content = result.unwrap();
@@ -540,19 +541,16 @@ namespace pl::core {
540541
for (auto token = tokens.begin(); token != tokens.end(); token++ ) {
541542
u32 idx = 1;
542543
if (auto *keyword = std::get_if<Token::Keyword>(&token->value); keyword != nullptr && *keyword == Token::Keyword::Namespace) {
543-
if (auto *valueType = std::get_if<Token::ValueType>(&token[1].value);
544-
valueType != nullptr && *valueType == Token::ValueType::Auto)
544+
if (auto *valueType = std::get_if<Token::ValueType>(&token[1].value); valueType != nullptr && *valueType == Token::ValueType::Auto)
545545
idx += 1;
546546
auto *identifier = std::get_if<Token::Identifier>(&token[idx].value);
547547
while (identifier != nullptr) {
548-
if (auto *separator = std::get_if<Token::Separator>(&token[idx].value);
549-
separator != nullptr && *separator == Token::Separator::EndOfProgram)
548+
if (auto *separator = std::get_if<Token::Separator>(&token[idx].value); separator != nullptr && *separator == Token::Separator::EndOfProgram)
550549
break;
551550
if (std::ranges::find(m_namespaces, identifier->get()) == m_namespaces.end())
552551
m_namespaces.push_back(identifier->get());
553552
idx += 1;
554-
if (auto *operatorToken = std::get_if<Token::Operator>(&token[idx].value);
555-
operatorToken == nullptr || *operatorToken != Token::Operator::ScopeResolution)
553+
if (auto *operatorToken = std::get_if<Token::Operator>(&token[idx].value); operatorToken == nullptr || *operatorToken != Token::Operator::ScopeResolution)
556554
break;
557555
idx += 1;
558556
identifier = std::get_if<Token::Identifier>(&token[idx].value);
@@ -608,6 +606,7 @@ namespace pl::core {
608606
process();
609607

610608
appendToNamespaces(m_output);
609+
saveTokens(source, m_result);
611610

612611
// Handle pragmas
613612
for (const auto &[type, datas] : this->m_pragmas) {
@@ -616,8 +615,7 @@ namespace pl::core {
616615

617616
if (this->m_pragmaHandlers.contains(type)) {
618617
if (!this->m_pragmaHandlers[type](*m_runtime, value))
619-
errorAt(Location { m_source, line, 1, value.length() },
620-
"Value '{}' cannot be used with the '{}' pragma directive.", value, type);
618+
errorAt(Location { m_source, line, 1, value.length() }, "Value '{}' cannot be used with the '{}' pragma directive.", value, type);
621619
}
622620
}
623621
}
@@ -626,6 +624,12 @@ namespace pl::core {
626624
return { m_output, collectErrors() };
627625
}
628626

627+
void Preprocessor::saveTokens(api::Source *source, const std::vector<Token> &tokens) {
628+
if (!source->mainSource && !m_parsedImports.contains(source->source)) {
629+
m_parsedImports[source->source] = tokens;
630+
}
631+
}
632+
629633
bool Preprocessor::eof() {
630634
return m_token == m_result.end();
631635
}

0 commit comments

Comments
 (0)