Skip to content

Commit 2bdd318

Browse files
committed
fix: position shift after import directive
1 parent 98f9042 commit 2bdd318

File tree

1 file changed

+13
-1
lines changed

1 file changed

+13
-1
lines changed

lib/preprocessor/import_processor/TokenImportProcessor.cpp

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -173,7 +173,11 @@ std::vector<TokenPtr> TokenImportProcessor::RemoveExtraTokens(const std::vector<
173173

174174
if (token->GetLexeme() == "#import") {
175175
if (i + 1 < tokens.size() && tokens[i + 1]->GetStringType() == "LITERAL:String") {
176-
i += 2;
176+
if (i + 2 >= tokens.size() || tokens[i + 2]->GetStringType() == "EOF") {
177+
i += 2;
178+
} else {
179+
i += 3;
180+
}
177181
} else {
178182
cleaned.push_back(token);
179183
++i;
@@ -208,6 +212,14 @@ std::expected<std::filesystem::path, PreprocessorError> TokenImportProcessor::Re
208212
std::to_string(token->GetPosition().GetColumn())));
209213
}
210214

215+
if (pos + 2 >= tokens.size() ||
216+
!(tokens[pos + 2]->GetStringType() == "EOF" || tokens[pos + 2]->GetStringType() == "NEWLINE" ||
217+
tokens[pos + 2]->GetLexeme() == ";")) {
218+
return std::unexpected(InvalidImportError("Unexpected token after #import at " +
219+
std::to_string(token->GetPosition().GetLine()) + ":" +
220+
std::to_string(token->GetPosition().GetColumn())));
221+
}
222+
211223
const std::string& import_lexeme = path_token->GetLexeme();
212224

213225
if (import_lexeme.size() < 2 || import_lexeme[0] != '"' || import_lexeme.back() != import_lexeme[0]) {

0 commit comments

Comments
 (0)