/*************************************************************************/ /* gdscript_parser.cpp */ /*************************************************************************/ /* This file is part of: */ /* GODOT ENGINE */ /* https://godotengine.org */ /*************************************************************************/ /* Copyright (c) 2007-2019 Juan Linietsky, Ariel Manzur. */ /* Copyright (c) 2014-2019 Godot Engine contributors (cf. AUTHORS.md) */ /* */ /* Permission is hereby granted, free of charge, to any person obtaining */ /* a copy of this software and associated documentation files (the */ /* "Software"), to deal in the Software without restriction, including */ /* without limitation the rights to use, copy, modify, merge, publish, */ /* distribute, sublicense, and/or sell copies of the Software, and to */ /* permit persons to whom the Software is furnished to do so, subject to */ /* the following conditions: */ /* */ /* The above copyright notice and this permission notice shall be */ /* included in all copies or substantial portions of the Software. */ /* */ /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */ /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */ /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/ /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */ /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */ /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */ /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ /*************************************************************************/ #include "gdscript_parser.h" #include "core/core_string_names.h" #include "core/engine.h" #include "core/io/resource_loader.h" #include "core/os/file_access.h" #include "core/print_string.h" #include "core/project_settings.h" #include "core/reference.h" #include "core/script_language.h" #include "gdscript.h" template T *GDScriptParser::alloc_node() { T *t = memnew(T); t->next = list; list = t; if (!head) head = t; t->line = tokenizer->get_token_line(); t->column = tokenizer->get_token_column(); return t; } #ifdef DEBUG_ENABLED static String _find_function_name(const GDScriptParser::OperatorNode *p_call); #endif // DEBUG_ENABLED bool GDScriptParser::_end_statement() { if (tokenizer->get_token() == GDScriptTokenizer::TK_SEMICOLON) { tokenizer->advance(); return true; //handle next } else if (tokenizer->get_token() == GDScriptTokenizer::TK_NEWLINE || tokenizer->get_token() == GDScriptTokenizer::TK_EOF) { return true; //will be handled properly } return false; } bool GDScriptParser::_enter_indent_block(BlockNode *p_block) { if (tokenizer->get_token() != GDScriptTokenizer::TK_COLON) { // report location at the previous token (on the previous line) int error_line = tokenizer->get_token_line(-1); int error_column = tokenizer->get_token_column(-1); _set_error("':' expected at end of line.", error_line, error_column); return false; } tokenizer->advance(); if (tokenizer->get_token() == GDScriptTokenizer::TK_EOF) { return false; } if (tokenizer->get_token() != GDScriptTokenizer::TK_NEWLINE) { // be more python-like int current = tab_level.back()->get(); tab_level.push_back(current); return true; //_set_error("newline expected after ':'."); //return false; } while (true) { if (tokenizer->get_token() != GDScriptTokenizer::TK_NEWLINE) { return false; //wtf } else if (tokenizer->get_token(1) == GDScriptTokenizer::TK_EOF) { return false; } else if (tokenizer->get_token(1) != GDScriptTokenizer::TK_NEWLINE) { int indent = tokenizer->get_token_line_indent(); int current = tab_level.back()->get(); if (indent <= current) { return false; } tab_level.push_back(indent); tokenizer->advance(); return true; } else if (p_block) { NewLineNode *nl = alloc_node(); nl->line = tokenizer->get_token_line(); p_block->statements.push_back(nl); } tokenizer->advance(); // go to next newline } } bool GDScriptParser::_parse_arguments(Node *p_parent, Vector &p_args, bool p_static, bool p_can_codecomplete) { if (tokenizer->get_token() == GDScriptTokenizer::TK_PARENTHESIS_CLOSE) { tokenizer->advance(); } else { parenthesis++; int argidx = 0; while (true) { if (tokenizer->get_token() == GDScriptTokenizer::TK_CURSOR) { _make_completable_call(argidx); completion_node = p_parent; } else if (tokenizer->get_token() == GDScriptTokenizer::TK_CONSTANT && tokenizer->get_token_constant().get_type() == Variant::STRING && tokenizer->get_token(1) == GDScriptTokenizer::TK_CURSOR) { //completing a string argument.. completion_cursor = tokenizer->get_token_constant(); _make_completable_call(argidx); completion_node = p_parent; tokenizer->advance(1); return false; } Node *arg = _parse_expression(p_parent, p_static); if (!arg) { return false; } p_args.push_back(arg); if (tokenizer->get_token() == GDScriptTokenizer::TK_PARENTHESIS_CLOSE) { tokenizer->advance(); break; } else if (tokenizer->get_token() == GDScriptTokenizer::TK_COMMA) { if (tokenizer->get_token(1) == GDScriptTokenizer::TK_PARENTHESIS_CLOSE) { _set_error("Expression expected"); return false; } tokenizer->advance(); argidx++; } else { // something is broken _set_error("Expected ',' or ')'"); return false; } } parenthesis--; } return true; } void GDScriptParser::_make_completable_call(int p_arg) { completion_cursor = StringName(); completion_type = COMPLETION_CALL_ARGUMENTS; completion_class = current_class; completion_function = current_function; completion_line = tokenizer->get_token_line(); completion_argument = p_arg; completion_block = current_block; completion_found = true; tokenizer->advance(); } bool GDScriptParser::_get_completable_identifier(CompletionType p_type, StringName &identifier) { identifier = StringName(); if (tokenizer->is_token_literal()) { identifier = tokenizer->get_token_literal(); tokenizer->advance(); } if (tokenizer->get_token() == GDScriptTokenizer::TK_CURSOR) { completion_cursor = identifier; completion_type = p_type; completion_class = current_class; completion_function = current_function; completion_line = tokenizer->get_token_line(); completion_block = current_block; completion_found = true; completion_ident_is_call = false; tokenizer->advance(); if (tokenizer->is_token_literal()) { identifier = identifier.operator String() + tokenizer->get_token_literal().operator String(); tokenizer->advance(); } if (tokenizer->get_token() == GDScriptTokenizer::TK_PARENTHESIS_OPEN) { completion_ident_is_call = true; } return true; } return false; } GDScriptParser::Node *GDScriptParser::_parse_expression(Node *p_parent, bool p_static, bool p_allow_assign, bool p_parsing_constant) { //Vector expressions; //Vector operators; Vector expression; Node *expr = NULL; int op_line = tokenizer->get_token_line(); // when operators are created at the bottom, the line might have been changed (\n found) while (true) { /*****************/ /* Parse Operand */ /*****************/ if (parenthesis > 0) { //remove empty space (only allowed if inside parenthesis while (tokenizer->get_token() == GDScriptTokenizer::TK_NEWLINE) { tokenizer->advance(); } } if (tokenizer->get_token() == GDScriptTokenizer::TK_PARENTHESIS_OPEN) { //subexpression () tokenizer->advance(); parenthesis++; Node *subexpr = _parse_expression(p_parent, p_static, p_allow_assign, p_parsing_constant); parenthesis--; if (!subexpr) return NULL; if (tokenizer->get_token() != GDScriptTokenizer::TK_PARENTHESIS_CLOSE) { _set_error("Expected ')' in expression"); return NULL; } tokenizer->advance(); expr = subexpr; } else if (tokenizer->get_token() == GDScriptTokenizer::TK_DOLLAR) { tokenizer->advance(); String path; bool need_identifier = true; bool done = false; int line = tokenizer->get_token_line(); while (!done) { switch (tokenizer->get_token()) { case GDScriptTokenizer::TK_CURSOR: { completion_type = COMPLETION_GET_NODE; completion_class = current_class; completion_function = current_function; completion_line = tokenizer->get_token_line(); completion_cursor = path; completion_argument = 0; completion_block = current_block; completion_found = true; tokenizer->advance(); } break; case GDScriptTokenizer::TK_CONSTANT: { if (!need_identifier) { done = true; break; } if (tokenizer->get_token_constant().get_type() != Variant::STRING) { _set_error("Expected string constant or identifier after '$' or '/'."); return NULL; } path += String(tokenizer->get_token_constant()); tokenizer->advance(); need_identifier = false; } break; case GDScriptTokenizer::TK_OP_DIV: { if (need_identifier) { done = true; break; } path += "/"; tokenizer->advance(); need_identifier = true; } break; default: { // Instead of checking for TK_IDENTIFIER, we check with is_token_literal, as this allows us to use match/sync/etc. as a name if (need_identifier && tokenizer->is_token_literal()) { path += String(tokenizer->get_token_literal()); tokenizer->advance(); need_identifier = false; } else { done = true; } break; } } } if (path == "") { _set_error("Path expected after $."); return NULL; } OperatorNode *op = alloc_node(); op->op = OperatorNode::OP_CALL; op->line = line; op->arguments.push_back(alloc_node()); op->arguments[0]->line = line; IdentifierNode *funcname = alloc_node(); funcname->name = "get_node"; funcname->line = line; op->arguments.push_back(funcname); ConstantNode *nodepath = alloc_node(); nodepath->value = NodePath(StringName(path)); nodepath->datatype = _type_from_variant(nodepath->value); nodepath->line = line; op->arguments.push_back(nodepath); expr = op; } else if (tokenizer->get_token() == GDScriptTokenizer::TK_CURSOR) { tokenizer->advance(); continue; //no point in cursor in the middle of expression } else if (tokenizer->get_token() == GDScriptTokenizer::TK_CONSTANT) { //constant defined by tokenizer ConstantNode *constant = alloc_node(); constant->value = tokenizer->get_token_constant(); constant->datatype = _type_from_variant(constant->value); tokenizer->advance(); expr = constant; } else if (tokenizer->get_token() == GDScriptTokenizer::TK_CONST_PI) { //constant defined by tokenizer ConstantNode *constant = alloc_node(); constant->value = Math_PI; constant->datatype = _type_from_variant(constant->value); tokenizer->advance(); expr = constant; } else if (tokenizer->get_token() == GDScriptTokenizer::TK_CONST_TAU) { //constant defined by tokenizer ConstantNode *constant = alloc_node(); constant->value = Math_TAU; constant->datatype = _type_from_variant(constant->value); tokenizer->advance(); expr = constant; } else if (tokenizer->get_token() == GDScriptTokenizer::TK_CONST_INF) { //constant defined by tokenizer ConstantNode *constant = alloc_node(); constant->value = Math_INF; constant->datatype = _type_from_variant(constant->value); tokenizer->advance(); expr = constant; } else if (tokenizer->get_token() == GDScriptTokenizer::TK_CONST_NAN) { //constant defined by tokenizer ConstantNode *constant = alloc_node(); constant->value = Math_NAN; constant->datatype = _type_from_variant(constant->value); tokenizer->advance(); expr = constant; } else if (tokenizer->get_token() == GDScriptTokenizer::TK_PR_PRELOAD) { //constant defined by tokenizer tokenizer->advance(); if (tokenizer->get_token() != GDScriptTokenizer::TK_PARENTHESIS_OPEN) { _set_error("Expected '(' after 'preload'"); return NULL; } tokenizer->advance(); if (tokenizer->get_token() == GDScriptTokenizer::TK_CURSOR) { completion_cursor = StringName(); completion_node = p_parent; completion_type = COMPLETION_RESOURCE_PATH; completion_class = current_class; completion_function = current_function; completion_line = tokenizer->get_token_line(); completion_block = current_block; completion_argument = 0; completion_found = true; tokenizer->advance(); } String path; bool found_constant = false; bool valid = false; ConstantNode *cn; Node *subexpr = _parse_and_reduce_expression(p_parent, p_static); if (subexpr) { if (subexpr->type == Node::TYPE_CONSTANT) { cn = static_cast(subexpr); found_constant = true; } if (subexpr->type == Node::TYPE_IDENTIFIER) { IdentifierNode *in = static_cast(subexpr); // Try to find the constant expression by the identifier if (current_class->constant_expressions.has(in->name)) { Node *cn_exp = current_class->constant_expressions[in->name].expression; if (cn_exp->type == Node::TYPE_CONSTANT) { cn = static_cast(cn_exp); found_constant = true; } } } if (found_constant && cn->value.get_type() == Variant::STRING) { valid = true; path = (String)cn->value; } } if (!valid) { _set_error("expected string constant as 'preload' argument."); return NULL; } if (!path.is_abs_path() && base_path != "") path = base_path.plus_file(path); path = path.replace("///", "//").simplify_path(); if (path == self_path) { _set_error("Can't preload itself (use 'get_script()')."); return NULL; } Ref res; 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 { 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; } } if (tokenizer->get_token() != GDScriptTokenizer::TK_PARENTHESIS_CLOSE) { _set_error("Expected ')' after 'preload' path"); return NULL; } Ref gds = res; if (gds.is_valid() && !gds->is_valid()) { _set_error("Could not fully preload the script, possible cyclic reference or compilation error. Use 'load()' instead if a cyclic reference is intended."); return NULL; } tokenizer->advance(); ConstantNode *constant = alloc_node(); constant->value = res; constant->datatype = _type_from_variant(constant->value); expr = constant; } else if (tokenizer->get_token() == GDScriptTokenizer::TK_PR_YIELD) { if (!current_function) { _set_error("yield() can only be used inside function blocks."); return NULL; } current_function->has_yield = true; tokenizer->advance(); if (tokenizer->get_token() != GDScriptTokenizer::TK_PARENTHESIS_OPEN) { _set_error("Expected '(' after 'yield'"); return NULL; } tokenizer->advance(); OperatorNode *yield = alloc_node(); yield->op = OperatorNode::OP_YIELD; while (tokenizer->get_token() == GDScriptTokenizer::TK_NEWLINE) { tokenizer->advance(); } if (tokenizer->get_token() == GDScriptTokenizer::TK_PARENTHESIS_CLOSE) { expr = yield; tokenizer->advance(); } else { parenthesis++; Node *object = _parse_and_reduce_expression(p_parent, p_static); if (!object) return NULL; yield->arguments.push_back(object); if (tokenizer->get_token() != GDScriptTokenizer::TK_COMMA) { _set_error("Expected ',' after first argument of 'yield'"); return NULL; } tokenizer->advance(); if (tokenizer->get_token() == GDScriptTokenizer::TK_CURSOR) { completion_cursor = StringName(); completion_node = object; completion_type = COMPLETION_YIELD; completion_class = current_class; completion_function = current_function; completion_line = tokenizer->get_token_line(); completion_argument = 0; completion_block = current_block; completion_found = true; tokenizer->advance(); } Node *signal = _parse_and_reduce_expression(p_parent, p_static); if (!signal) return NULL; yield->arguments.push_back(signal); if (tokenizer->get_token() != GDScriptTokenizer::TK_PARENTHESIS_CLOSE) { _set_error("Expected ')' after second argument of 'yield'"); return NULL; } parenthesis--; tokenizer->advance(); expr = yield; } } else if (tokenizer->get_token() == GDScriptTokenizer::TK_SELF) { if (p_static) { _set_error("'self'' not allowed in static function or constant expression"); return NULL; } //constant defined by tokenizer SelfNode *self = alloc_node(); tokenizer->advance(); expr = self; } else if (tokenizer->get_token() == GDScriptTokenizer::TK_BUILT_IN_TYPE && tokenizer->get_token(1) == GDScriptTokenizer::TK_PERIOD) { Variant::Type bi_type = tokenizer->get_token_type(); tokenizer->advance(2); StringName identifier; if (_get_completable_identifier(COMPLETION_BUILT_IN_TYPE_CONSTANT, identifier)) { completion_built_in_constant = bi_type; } if (identifier == StringName()) { _set_error("Built-in type constant or static function expected after '.'"); return NULL; } if (!Variant::has_constant(bi_type, identifier)) { if (tokenizer->get_token() == GDScriptTokenizer::TK_PARENTHESIS_OPEN && Variant::is_method_const(bi_type, identifier) && Variant::get_method_return_type(bi_type, identifier) == bi_type) { tokenizer->advance(); OperatorNode *construct = alloc_node(); construct->op = OperatorNode::OP_CALL; TypeNode *tn = alloc_node(); tn->vtype = bi_type; construct->arguments.push_back(tn); OperatorNode *op = alloc_node(); op->op = OperatorNode::OP_CALL; op->arguments.push_back(construct); IdentifierNode *id = alloc_node(); id->name = identifier; op->arguments.push_back(id); if (!_parse_arguments(op, op->arguments, p_static, true)) return NULL; expr = op; } else { // Object is a special case bool valid = false; if (bi_type == Variant::OBJECT) { int object_constant = ClassDB::get_integer_constant("Object", identifier, &valid); if (valid) { ConstantNode *cn = alloc_node(); cn->value = object_constant; cn->datatype = _type_from_variant(cn->value); expr = cn; } } if (!valid) { _set_error("Static constant '" + identifier.operator String() + "' not present in built-in type " + Variant::get_type_name(bi_type) + "."); return NULL; } } } else { ConstantNode *cn = alloc_node(); cn->value = Variant::get_constant_value(bi_type, identifier); cn->datatype = _type_from_variant(cn->value); expr = cn; } } else if (tokenizer->get_token(1) == GDScriptTokenizer::TK_PARENTHESIS_OPEN && tokenizer->is_token_literal()) { // We check with is_token_literal, as this allows us to use match/sync/etc. as a name //function or constructor OperatorNode *op = alloc_node(); op->op = OperatorNode::OP_CALL; //Do a quick Array and Dictionary Check. Replace if either require no arguments. bool replaced = false; if (tokenizer->get_token() == GDScriptTokenizer::TK_BUILT_IN_TYPE) { Variant::Type ct = tokenizer->get_token_type(); if (!p_parsing_constant) { if (ct == Variant::ARRAY) { if (tokenizer->get_token(2) == GDScriptTokenizer::TK_PARENTHESIS_CLOSE) { ArrayNode *arr = alloc_node(); expr = arr; replaced = true; tokenizer->advance(3); } } if (ct == Variant::DICTIONARY) { if (tokenizer->get_token(2) == GDScriptTokenizer::TK_PARENTHESIS_CLOSE) { DictionaryNode *dict = alloc_node(); expr = dict; replaced = true; tokenizer->advance(3); } } } if (!replaced) { TypeNode *tn = alloc_node(); tn->vtype = tokenizer->get_token_type(); op->arguments.push_back(tn); tokenizer->advance(2); } } else if (tokenizer->get_token() == GDScriptTokenizer::TK_BUILT_IN_FUNC) { BuiltInFunctionNode *bn = alloc_node(); bn->function = tokenizer->get_token_built_in_func(); op->arguments.push_back(bn); tokenizer->advance(2); } else { SelfNode *self = alloc_node(); op->arguments.push_back(self); StringName identifier; if (_get_completable_identifier(COMPLETION_FUNCTION, identifier)) { } IdentifierNode *id = alloc_node(); id->name = identifier; op->arguments.push_back(id); tokenizer->advance(1); } if (tokenizer->get_token() == GDScriptTokenizer::TK_CURSOR) { _make_completable_call(0); completion_node = op; } if (!replaced) { if (!_parse_arguments(op, op->arguments, p_static, true)) return NULL; expr = op; } } else if (tokenizer->is_token_literal(0, true)) { // We check with is_token_literal, as this allows us to use match/sync/etc. as a name //identifier (reference) const ClassNode *cln = current_class; bool bfn = false; StringName identifier; int id_line = tokenizer->get_token_line(); if (_get_completable_identifier(COMPLETION_IDENTIFIER, identifier)) { } BlockNode *b = current_block; while (!bfn && b) { if (b->variables.has(identifier)) { IdentifierNode *id = alloc_node(); id->name = identifier; id->declared_block = b; id->line = id_line; expr = id; bfn = true; #ifdef DEBUG_ENABLED LocalVarNode *lv = b->variables[identifier]; switch (tokenizer->get_token()) { case GDScriptTokenizer::TK_OP_ASSIGN_ADD: case GDScriptTokenizer::TK_OP_ASSIGN_BIT_AND: case GDScriptTokenizer::TK_OP_ASSIGN_BIT_OR: case GDScriptTokenizer::TK_OP_ASSIGN_BIT_XOR: case GDScriptTokenizer::TK_OP_ASSIGN_DIV: case GDScriptTokenizer::TK_OP_ASSIGN_MOD: case GDScriptTokenizer::TK_OP_ASSIGN_MUL: case GDScriptTokenizer::TK_OP_ASSIGN_SHIFT_LEFT: case GDScriptTokenizer::TK_OP_ASSIGN_SHIFT_RIGHT: case GDScriptTokenizer::TK_OP_ASSIGN_SUB: { if (lv->assignments == 0) { if (!lv->datatype.has_type) { _set_error("Using assignment with operation on a variable that was never assigned."); return NULL; } _add_warning(GDScriptWarning::UNASSIGNED_VARIABLE_OP_ASSIGN, -1, identifier.operator String()); } FALLTHROUGH; } case GDScriptTokenizer::TK_OP_ASSIGN: { lv->assignments += 1; lv->usages--; // Assignment is not really usage } break; default: { lv->usages++; } } #endif // DEBUG_ENABLED break; } b = b->parent_block; } if (!bfn && p_parsing_constant) { if (cln->constant_expressions.has(identifier)) { expr = cln->constant_expressions[identifier].expression; bfn = true; } else if (GDScriptLanguage::get_singleton()->get_global_map().has(identifier)) { //check from constants ConstantNode *constant = alloc_node(); constant->value = GDScriptLanguage::get_singleton()->get_global_array()[GDScriptLanguage::get_singleton()->get_global_map()[identifier]]; constant->datatype = _type_from_variant(constant->value); constant->line = id_line; expr = constant; bfn = true; } if (!bfn && GDScriptLanguage::get_singleton()->get_named_globals_map().has(identifier)) { //check from singletons ConstantNode *constant = alloc_node(); constant->value = GDScriptLanguage::get_singleton()->get_named_globals_map()[identifier]; expr = constant; bfn = true; } if (!dependencies_only) { if (!bfn && ScriptServer::is_global_class(identifier)) { Ref