Merge pull request #102218 from HolonProduction/dictionary-recovery

GDScript: Do phrase level recovery when parsing faulty dictionaries
This commit is contained in:
Thaddeus Crews 2025-04-14 19:39:50 -05:00
commit bef5d1e4f8
No known key found for this signature in database
GPG key ID: 8C6E5FEB5FC03CCC
30 changed files with 177 additions and 4 deletions

View file

@ -3122,13 +3122,9 @@ GDScriptParser::ExpressionNode *GDScriptParser::parse_dictionary(ExpressionNode
case DictionaryNode::LUA_TABLE:
if (key != nullptr && key->type != Node::IDENTIFIER && key->type != Node::LITERAL) {
push_error(R"(Expected identifier or string as Lua-style dictionary key (e.g "{ key = value }").)");
advance();
break;
}
if (key != nullptr && key->type == Node::LITERAL && static_cast<LiteralNode *>(key)->value.get_type() != Variant::STRING) {
push_error(R"(Expected identifier or string as Lua-style dictionary key (e.g "{ key = value }").)");
advance();
break;
}
if (!match(GDScriptTokenizer::Token::EQUAL)) {
if (match(GDScriptTokenizer::Token::COLON)) {
@ -3168,6 +3164,21 @@ GDScriptParser::ExpressionNode *GDScriptParser::parse_dictionary(ExpressionNode
if (key != nullptr && value != nullptr) {
dictionary->elements.push_back({ key, value });
}
// Do phrase level recovery by inserting an imaginary expression for missing keys or values.
// This ensures the successfully parsed expression is part of the AST and can be analyzed.
if (key != nullptr && value == nullptr) {
LiteralNode *dummy = alloc_recovery_node<LiteralNode>();
dummy->value = Variant();
dictionary->elements.push_back({ key, dummy });
} else if (key == nullptr && value != nullptr) {
LiteralNode *dummy = alloc_recovery_node<LiteralNode>();
dummy->value = Variant();
dictionary->elements.push_back({ dummy, value });
}
} while (match(GDScriptTokenizer::Token::COMMA) && !is_at_end());
}
pop_multiline();