GDScript: Allow string keys on Lua-style dictionaries

Which is useful when the key isn't a valid identifier, such as keys with
spaces or numeric keys.
This commit is contained in:
George Marques 2021-09-15 09:56:24 -03:00
parent 32f8f74d83
commit 5d31ce4b16
No known key found for this signature in database
GPG key ID: 046BD46A3201E43D
10 changed files with 48 additions and 4 deletions

View file

@ -2472,8 +2472,13 @@ GDScriptParser::ExpressionNode *GDScriptParser::parse_dictionary(ExpressionNode
switch (dictionary->style) {
case DictionaryNode::LUA_TABLE:
if (key != nullptr && key->type != Node::IDENTIFIER) {
push_error("Expected identifier as LUA-style dictionary key.");
if (key != nullptr && key->type != Node::IDENTIFIER && key->type != Node::LITERAL) {
push_error("Expected identifier or string as LUA-style dictionary key.");
advance();
break;
}
if (key != nullptr && key->type == Node::LITERAL && static_cast<LiteralNode *>(key)->value.get_type() != Variant::STRING) {
push_error("Expected identifier or string as LUA-style dictionary key.");
advance();
break;
}
@ -2487,7 +2492,11 @@ GDScriptParser::ExpressionNode *GDScriptParser::parse_dictionary(ExpressionNode
}
if (key != nullptr) {
key->is_constant = true;
key->reduced_value = static_cast<IdentifierNode *>(key)->name;
if (key->type == Node::IDENTIFIER) {
key->reduced_value = static_cast<IdentifierNode *>(key)->name;
} else if (key->type == Node::LITERAL) {
key->reduced_value = StringName(static_cast<LiteralNode *>(key)->value.operator String());
}
}
break;
case DictionaryNode::PYTHON_DICT: