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:
parent
32f8f74d83
commit
5d31ce4b16
10 changed files with 48 additions and 4 deletions
|
@ -440,7 +440,7 @@ GDScriptCodeGenerator::Address GDScriptCompiler::_parse_expression(CodeGen &code
|
|||
break;
|
||||
case GDScriptParser::DictionaryNode::LUA_TABLE:
|
||||
// Lua-style: key is an identifier interpreted as StringName.
|
||||
StringName key = static_cast<const GDScriptParser::IdentifierNode *>(dn->elements[i].key)->name;
|
||||
StringName key = dn->elements[i].key->reduced_value.operator StringName();
|
||||
element = codegen.add_constant(key);
|
||||
break;
|
||||
}
|
||||
|
|
|
@ -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:
|
||||
|
|
|
@ -0,0 +1,6 @@
|
|||
func test():
|
||||
var lua_dict = {
|
||||
a = 1,
|
||||
b = 2,
|
||||
a = 3, # Duplicate isn't allowed.
|
||||
}
|
|
@ -0,0 +1,2 @@
|
|||
GDTEST_ANALYZER_ERROR
|
||||
Key "a" was already used in this dictionary (at line 3).
|
|
@ -0,0 +1,6 @@
|
|||
func test():
|
||||
var lua_dict_with_string = {
|
||||
a = 1,
|
||||
b = 2,
|
||||
"a" = 3, # Duplicate isn't allowed.
|
||||
}
|
|
@ -0,0 +1,2 @@
|
|||
GDTEST_ANALYZER_ERROR
|
||||
Key "a" was already used in this dictionary (at line 3).
|
|
@ -0,0 +1,6 @@
|
|||
func test():
|
||||
var python_dict = {
|
||||
"a": 1,
|
||||
"b": 2,
|
||||
"a": 3, # Duplicate isn't allowed.
|
||||
}
|
|
@ -0,0 +1,2 @@
|
|||
GDTEST_ANALYZER_ERROR
|
||||
Key "a" was already used in this dictionary (at line 3).
|
|
@ -0,0 +1,9 @@
|
|||
func test():
|
||||
var lua_dict = {
|
||||
a = 1,
|
||||
"b" = 2, # Using strings are allowed too.
|
||||
"with spaces" = 3, # Especially useful when key has spaces...
|
||||
"2" = 4, # ... or invalid identifiers.
|
||||
}
|
||||
|
||||
print(lua_dict)
|
|
@ -0,0 +1,2 @@
|
|||
GDTEST_OK
|
||||
{2:4, a:1, b:2, with spaces:3}
|
Loading…
Reference in a new issue