summaryrefslogtreecommitdiff
path: root/modules/gdscript/gdscript_parser.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'modules/gdscript/gdscript_parser.cpp')
-rw-r--r--modules/gdscript/gdscript_parser.cpp128
1 files changed, 85 insertions, 43 deletions
diff --git a/modules/gdscript/gdscript_parser.cpp b/modules/gdscript/gdscript_parser.cpp
index 5ebf68177d..e75b8a14a3 100644
--- a/modules/gdscript/gdscript_parser.cpp
+++ b/modules/gdscript/gdscript_parser.cpp
@@ -473,29 +473,31 @@ GDScriptParser::Node *GDScriptParser::_parse_expression(Node *p_parent, bool p_s
}
Ref<Resource> res;
- if (!validating) {
+ dependencies.push_back(path);
+ if (!dependencies_only) {
+ if (!validating) {
- //this can be too slow for just validating code
- if (for_completion && ScriptCodeCompletionCache::get_singleton() && FileAccess::exists(path)) {
- res = ScriptCodeCompletionCache::get_singleton()->get_cached_resource(path);
- } else if (!for_completion || FileAccess::exists(path)) {
- res = ResourceLoader::load(path);
- }
- } else {
+ //this can be too slow for just validating code
+ if (for_completion && ScriptCodeCompletionCache::get_singleton() && FileAccess::exists(path)) {
+ res = ScriptCodeCompletionCache::get_singleton()->get_cached_resource(path);
+ } else if (!for_completion || FileAccess::exists(path)) {
+ res = ResourceLoader::load(path);
+ }
+ } else {
- if (!FileAccess::exists(path)) {
+ if (!FileAccess::exists(path)) {
+ _set_error("Can't preload resource at path: " + path);
+ return NULL;
+ } else if (ScriptCodeCompletionCache::get_singleton()) {
+ res = ScriptCodeCompletionCache::get_singleton()->get_cached_resource(path);
+ }
+ }
+ if (!res.is_valid()) {
_set_error("Can't preload resource at path: " + path);
return NULL;
- } else if (ScriptCodeCompletionCache::get_singleton()) {
- res = ScriptCodeCompletionCache::get_singleton()->get_cached_resource(path);
}
}
- if (!res.is_valid()) {
- _set_error("Can't preload resource at path: " + path);
- return NULL;
- }
-
if (tokenizer->get_token() != GDScriptTokenizer::TK_PARENTHESIS_CLOSE) {
_set_error("Expected ')' after 'preload' path");
return NULL;
@@ -775,7 +777,8 @@ GDScriptParser::Node *GDScriptParser::_parse_expression(Node *p_parent, bool p_s
}
_add_warning(GDScriptWarning::UNASSIGNED_VARIABLE_OP_ASSIGN, -1, identifier.operator String());
}
- } // fallthrough
+ FALLTHROUGH;
+ }
case GDScriptTokenizer::TK_OP_ASSIGN: {
lv->assignments += 1;
lv->usages--; // Assignment is not really usage
@@ -812,19 +815,31 @@ GDScriptParser::Node *GDScriptParser::_parse_expression(Node *p_parent, bool p_s
bfn = true;
}
- // Check parents for the constant
- if (!bfn && cln->extends_file != StringName()) {
- Ref<GDScript> parent = ResourceLoader::load(cln->extends_file);
- if (parent.is_valid() && parent->is_valid()) {
- Map<StringName, Variant> parent_constants;
- parent->get_constants(&parent_constants);
- if (parent_constants.has(identifier)) {
+ if (!dependencies_only) {
+ if (!bfn && ScriptServer::is_global_class(identifier)) {
+ Ref<Script> scr = ResourceLoader::load(ScriptServer::get_global_class_path(identifier));
+ if (scr.is_valid() && scr->is_valid()) {
ConstantNode *constant = alloc_node<ConstantNode>();
- constant->value = parent_constants[identifier];
+ constant->value = scr;
expr = constant;
bfn = true;
}
}
+
+ // Check parents for the constant
+ if (!bfn && cln->extends_file != StringName()) {
+ Ref<GDScript> parent = ResourceLoader::load(cln->extends_file);
+ if (parent.is_valid() && parent->is_valid()) {
+ Map<StringName, Variant> parent_constants;
+ parent->get_constants(&parent_constants);
+ if (parent_constants.has(identifier)) {
+ ConstantNode *constant = alloc_node<ConstantNode>();
+ constant->value = parent_constants[identifier];
+ expr = constant;
+ bfn = true;
+ }
+ }
+ }
}
}
@@ -2082,7 +2097,7 @@ GDScriptParser::PatternNode *GDScriptParser::_parse_pattern(bool p_static) {
return NULL;
}
pattern->pt_type = GDScriptParser::PatternNode::PT_BIND;
- pattern->bind = tokenizer->get_token_identifier();
+ pattern->bind = tokenizer->get_token_literal();
// Check if variable name is already used
BlockNode *bl = current_block;
while (bl) {
@@ -2211,6 +2226,8 @@ GDScriptParser::PatternNode *GDScriptParser::_parse_pattern(bool p_static) {
void GDScriptParser::_parse_pattern_block(BlockNode *p_block, Vector<PatternBranchNode *> &p_branches, bool p_static) {
int indent_level = tab_level.back()->get();
+ p_block->has_return = true;
+
while (true) {
while (tokenizer->get_token() == GDScriptTokenizer::TK_NEWLINE && _parse_newline())
@@ -2268,8 +2285,8 @@ void GDScriptParser::_parse_pattern_block(BlockNode *p_block, Vector<PatternBran
current_block = p_block;
- if (catch_all && branch->body->has_return) {
- p_block->has_return = true;
+ if (!branch->body->has_return) {
+ p_block->has_return = false;
}
p_branches.push_back(branch);
@@ -2727,6 +2744,8 @@ void GDScriptParser::_parse_block(BlockNode *p_block, bool p_static) {
} break;
case GDScriptTokenizer::TK_NEWLINE: {
+ int line = tokenizer->get_token_line();
+
if (!_parse_newline()) {
if (!error_set) {
p_block->end_line = tokenizer->get_token_line();
@@ -2736,7 +2755,7 @@ void GDScriptParser::_parse_block(BlockNode *p_block, bool p_static) {
}
NewLineNode *nl2 = alloc_node<NewLineNode>();
- nl2->line = tokenizer->get_token_line();
+ nl2->line = line;
p_block->statements.push_back(nl2);
} break;
@@ -3053,7 +3072,6 @@ void GDScriptParser::_parse_block(BlockNode *p_block, bool p_static) {
}
DataType iter_type;
- iter_type.is_constant = true;
if (container->type == Node::TYPE_OPERATOR) {
@@ -3378,6 +3396,13 @@ void GDScriptParser::_parse_extends(ClassNode *p_class) {
p_class->extends_file = constant;
tokenizer->advance();
+ // Add parent script as a dependency
+ String parent = constant;
+ if (parent.is_rel_path()) {
+ parent = base_path.plus_file(parent).simplify_path();
+ }
+ dependencies.push_back(parent);
+
if (tokenizer->get_token() != GDScriptTokenizer::TK_PERIOD) {
return;
} else
@@ -3612,7 +3637,8 @@ void GDScriptParser::_parse_class(ClassNode *p_class) {
return;
}
- }; //fallthrough to function
+ FALLTHROUGH;
+ }
case GDScriptTokenizer::TK_PR_FUNCTION: {
bool _static = false;
@@ -4062,7 +4088,8 @@ void GDScriptParser::_parse_class(ClassNode *p_class) {
break;
}
- }; //fallthrough to use the same
+ FALLTHROUGH;
+ }
case Variant::REAL: {
if (tokenizer->get_token() == GDScriptTokenizer::TK_IDENTIFIER && tokenizer->get_token_identifier() == "EASE") {
@@ -4487,6 +4514,7 @@ void GDScriptParser::_parse_class(ClassNode *p_class) {
#ifdef DEBUG_ENABLED
_add_warning(GDScriptWarning::DEPRECATED_KEYWORD, tokenizer->get_token_line(), "slave", "puppet");
#endif
+ FALLTHROUGH;
case GDScriptTokenizer::TK_PR_PUPPET: {
//may be fallthrough from export, ignore if so
@@ -4554,7 +4582,7 @@ void GDScriptParser::_parse_class(ClassNode *p_class) {
continue;
} break;
case GDScriptTokenizer::TK_PR_VAR: {
- //variale declaration and (eventual) initialization
+ // variable declaration and (eventual) initialization
ClassNode::Member member;
@@ -5266,7 +5294,8 @@ String GDScriptParser::DataType::to_string() const {
if (!gds_class.empty()) {
return gds_class;
}
- } // fallthrough
+ FALLTHROUGH;
+ }
case SCRIPT: {
if (is_meta_type) {
return script_type->get_class_name().operator String();
@@ -5434,7 +5463,7 @@ GDScriptParser::DataType GDScriptParser::_resolve_type(const DataType &p_source,
String script_path = ScriptServer::get_global_class_path(id);
if (script_path == self_path) {
result.kind = DataType::CLASS;
- result.class_type = current_class;
+ result.class_type = static_cast<ClassNode *>(head);
} else {
Ref<Script> script = ResourceLoader::load(script_path);
Ref<GDScript> gds = script;
@@ -7237,6 +7266,7 @@ GDScriptParser::DataType GDScriptParser::_reduce_identifier_type(const DataType
DataType result;
result.has_type = true;
result.script_type = scr;
+ result.is_constant = true;
result.is_meta_type = true;
Ref<GDScript> gds = scr;
if (gds.is_valid()) {
@@ -7287,6 +7317,7 @@ GDScriptParser::DataType GDScriptParser::_reduce_identifier_type(const DataType
if (singleton.is_valid()) {
DataType result;
result.has_type = true;
+ result.is_constant = true;
result.script_type = singleton;
Ref<GDScript> gds = singleton;
@@ -7842,13 +7873,16 @@ void GDScriptParser::_check_block_types(BlockNode *p_block) {
return;
}
- if (!lh_type.has_type && check_types) {
- if (op->arguments[0]->type == Node::TYPE_OPERATOR) {
- _mark_line_as_unsafe(op->line);
+ if (check_types) {
+ if (!lh_type.has_type) {
+ if (op->arguments[0]->type == Node::TYPE_OPERATOR) {
+ _mark_line_as_unsafe(op->line);
+ }
+ }
+ if (lh_type.is_constant) {
+ _set_error("Cannot assign a new value to a constant.", op->line);
+ return;
}
- } else if (lh_type.is_constant) {
- _set_error("Cannot assign a new value to a constant.", op->line);
- return;
}
DataType rh_type;
@@ -8017,7 +8051,8 @@ void GDScriptParser::_check_block_types(BlockNode *p_block) {
if (cn->value.get_type() == Variant::STRING) {
break;
}
- } // falthrough
+ FALLTHROUGH;
+ }
default: {
_mark_line_as_safe(statement->line);
_reduce_node_type(statement); // Test for safety anyway
@@ -8149,6 +8184,10 @@ Error GDScriptParser::_parse(const String &p_base_path) {
return ERR_PARSE_ERROR;
}
+ if (dependencies_only) {
+ return OK;
+ }
+
_determine_inheritance(main_class);
if (error_set) {
@@ -8227,7 +8266,7 @@ Error GDScriptParser::parse_bytecode(const Vector<uint8_t> &p_bytecode, const St
return ret;
}
-Error GDScriptParser::parse(const String &p_code, const String &p_base_path, bool p_just_validate, const String &p_self_path, bool p_for_completion, Set<int> *r_safe_lines) {
+Error GDScriptParser::parse(const String &p_code, const String &p_base_path, bool p_just_validate, const String &p_self_path, bool p_for_completion, Set<int> *r_safe_lines, bool p_dependencies_only) {
clear();
@@ -8237,6 +8276,7 @@ Error GDScriptParser::parse(const String &p_code, const String &p_base_path, boo
validating = p_just_validate;
for_completion = p_for_completion;
+ dependencies_only = p_dependencies_only;
#ifdef DEBUG_ENABLED
safe_lines = r_safe_lines;
#endif // DEBUG_ENABLED
@@ -8293,6 +8333,8 @@ void GDScriptParser::clear() {
parenthesis = 0;
current_export.type = Variant::NIL;
check_types = true;
+ dependencies_only = false;
+ dependencies.clear();
error = "";
#ifdef DEBUG_ENABLED
safe_lines = NULL;