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.cpp64
1 files changed, 55 insertions, 9 deletions
diff --git a/modules/gdscript/gdscript_parser.cpp b/modules/gdscript/gdscript_parser.cpp
index 81652c3d37..2fa5084d84 100644
--- a/modules/gdscript/gdscript_parser.cpp
+++ b/modules/gdscript/gdscript_parser.cpp
@@ -81,8 +81,11 @@ bool GDScriptParser::_enter_indent_block(BlockNode *p_block) {
}
tokenizer->advance();
- if (tokenizer->get_token() != GDScriptTokenizer::TK_NEWLINE) {
+ 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);
@@ -92,10 +95,11 @@ bool GDScriptParser::_enter_indent_block(BlockNode *p_block) {
}
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();
@@ -637,9 +641,21 @@ GDScriptParser::Node *GDScriptParser::_parse_expression(Node *p_parent, bool p_s
expr = op;
} else {
-
- _set_error("Static constant '" + identifier.operator String() + "' not present in built-in type " + Variant::get_type_name(bi_type) + ".");
- return NULL;
+ // 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<ConstantNode>();
+ 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 {
@@ -4851,6 +4867,20 @@ void GDScriptParser::_parse_class(ClassNode *p_class) {
StringName const_id = tokenizer->get_token_literal();
+ if (current_class->constant_expressions.has(const_id)) {
+ _set_error("A constant named '" + String(const_id) + "' already exists in this class (at line: " +
+ itos(current_class->constant_expressions[const_id].expression->line) + ").");
+ return;
+ }
+
+ for (int i = 0; i < current_class->variables.size(); i++) {
+ if (current_class->variables[i].identifier == const_id) {
+ _set_error("A variable named '" + String(const_id) + "' already exists in this class (at line: " +
+ itos(current_class->variables[i].line) + ").");
+ return;
+ }
+ }
+
tokenizer->advance();
if (tokenizer->get_token() == GDScriptTokenizer::TK_OP_ASSIGN) {
@@ -5049,7 +5079,7 @@ void GDScriptParser::_determine_inheritance(ClassNode *p_class) {
if (found) continue;
if (p->constant_expressions.has(base)) {
- if (!p->constant_expressions[base].expression->type == Node::TYPE_CONSTANT) {
+ if (p->constant_expressions[base].expression->type != Node::TYPE_CONSTANT) {
_set_error("Could not resolve constant '" + base + "'.", p_class->line);
return;
}
@@ -5189,6 +5219,8 @@ String GDScriptParser::DataType::to_string() const {
}
return class_type->name.operator String();
} break;
+ case UNRESOLVED: {
+ } break;
}
return "Unresolved";
@@ -5761,7 +5793,10 @@ bool GDScriptParser::_is_type_compatible(const DataType &p_container, const Data
expr_native = base->base_type.native_type;
expr_script = base->base_type.script_type;
}
- }
+ } break;
+ case DataType::BUILTIN: // Already handled above
+ case DataType::UNRESOLVED: // Not allowed, see above
+ break;
}
switch (p_container.kind) {
@@ -5804,7 +5839,10 @@ bool GDScriptParser::_is_type_compatible(const DataType &p_container, const Data
expr_class = expr_class->base_type.class_type;
}
return false;
- }
+ } break;
+ case DataType::BUILTIN: // Already handled above
+ case DataType::UNRESOLVED: // Not allowed, see above
+ break;
}
return false;
@@ -6198,6 +6236,7 @@ GDScriptParser::DataType GDScriptParser::_reduce_node_type(Node *p_node) {
case Variant::COLOR: {
error = index_type.builtin_type != Variant::INT && index_type.builtin_type != Variant::STRING;
} break;
+ default: {}
}
}
if (error) {
@@ -6315,6 +6354,7 @@ GDScriptParser::DataType GDScriptParser::_reduce_node_type(Node *p_node) {
}
}
} break;
+ default: {}
}
p_node->set_datatype(_resolve_type(node_type, p_node->line));
@@ -7204,6 +7244,12 @@ void GDScriptParser::_check_class_level_types(ClassNode *p_class) {
expr.is_constant = true;
c.type = expr;
c.expression->set_datatype(expr);
+
+ DataType tmp;
+ if (_get_member_type(p_class->base_type, E->key(), tmp)) {
+ _set_error("Member '" + String(E->key()) + "' already exists in parent class.", c.expression->line);
+ return;
+ }
}
// Function declarations
@@ -7789,7 +7835,7 @@ void GDScriptParser::_check_block_types(BlockNode *p_block) {
// Figure out function name for warning
String func_name = _find_function_name(op);
if (func_name.empty()) {
- func_name == "<undetected name>";
+ func_name = "<undetected name>";
}
_add_warning(GDScriptWarning::RETURN_VALUE_DISCARDED, op->line, func_name);
}