From c1ab956dd037cf5e3bdbf3d4940c4e7a9421f9b9 Mon Sep 17 00:00:00 2001 From: Ignacio Etcheverry Date: Fri, 13 Dec 2019 19:50:38 +0100 Subject: Mono/C#: Fix class parser bug with 'where T : struct' The struct decl parsing was outdated. Make both struct decl and class declparsing share the same code. --- modules/mono/editor/script_class_parser.cpp | 53 ++++++++--------------------- 1 file changed, 15 insertions(+), 38 deletions(-) (limited to 'modules/mono/editor') diff --git a/modules/mono/editor/script_class_parser.cpp b/modules/mono/editor/script_class_parser.cpp index dcb0ca5a80..987a2a4aa6 100644 --- a/modules/mono/editor/script_class_parser.cpp +++ b/modules/mono/editor/script_class_parser.cpp @@ -501,7 +501,10 @@ Error ScriptClassParser::parse(const String &p_code) { int type_curly_stack = 0; while (!error && tk != TK_EOF) { - if (tk == TK_IDENTIFIER && String(value) == "class") { + String identifier = value; + if (tk == TK_IDENTIFIER && (identifier == "class" || identifier == "struct")) { + bool is_class = identifier == "class"; + tk = get_token(); if (tk == TK_IDENTIFIER) { @@ -568,48 +571,22 @@ Error ScriptClassParser::parse(const String &p_code) { NameDecl name_decl; name_decl.name = name; - name_decl.type = NameDecl::CLASS_DECL; + name_decl.type = is_class ? NameDecl::CLASS_DECL : NameDecl::STRUCT_DECL; name_stack[at_level] = name_decl; - if (!generic) { // no generics, thanks - classes.push_back(class_decl); - } else if (OS::get_singleton()->is_stdout_verbose()) { - String full_name = class_decl.namespace_; - if (full_name.length()) - full_name += "."; - full_name += class_decl.name; - OS::get_singleton()->print("Ignoring generic class declaration: %s\n", class_decl.name.utf8().get_data()); - } - } - } else if (tk == TK_IDENTIFIER && String(value) == "struct") { - String name; - int at_level = type_curly_stack; - while (true) { - tk = get_token(); - if (tk == TK_IDENTIFIER && name.empty()) { - name = String(value); - } else if (tk == TK_CURLY_BRACKET_OPEN) { - if (name.empty()) { - error_str = "Expected " + get_token_name(TK_IDENTIFIER) + " after keyword 'struct', found " + get_token_name(TK_CURLY_BRACKET_OPEN); - error = true; - return ERR_PARSE_ERROR; + if (is_class) { + if (!generic) { // no generics, thanks + classes.push_back(class_decl); + } else if (OS::get_singleton()->is_stdout_verbose()) { + String full_name = class_decl.namespace_; + if (full_name.length()) + full_name += "."; + full_name += class_decl.name; + OS::get_singleton()->print("Ignoring generic class declaration: %s\n", class_decl.name.utf8().get_data()); } - - curly_stack++; - type_curly_stack++; - break; - } else if (tk == TK_EOF) { - error_str = "Expected " + get_token_name(TK_CURLY_BRACKET_OPEN) + " after struct decl, found " + get_token_name(TK_EOF); - error = true; - return ERR_PARSE_ERROR; } } - - NameDecl name_decl; - name_decl.name = name; - name_decl.type = NameDecl::STRUCT_DECL; - name_stack[at_level] = name_decl; - } else if (tk == TK_IDENTIFIER && String(value) == "namespace") { + } else if (tk == TK_IDENTIFIER && identifier == "namespace") { if (type_curly_stack > 0) { error_str = "Found namespace nested inside type."; error = true; -- cgit v1.2.3 From f2a2293709472f5b4e418b8f38d48eef987cc0de Mon Sep 17 00:00:00 2001 From: Ignacio Etcheverry Date: Fri, 13 Dec 2019 19:55:32 +0100 Subject: Mono/C#: Fix class parser incorrectly handling nested namespaces It would incorrectly error thinking the nested namespace is being declared inside a struct/class. This was because of an incorrect nesting level being used for classes and structs. --- modules/mono/editor/script_class_parser.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'modules/mono/editor') diff --git a/modules/mono/editor/script_class_parser.cpp b/modules/mono/editor/script_class_parser.cpp index 987a2a4aa6..1c3a583601 100644 --- a/modules/mono/editor/script_class_parser.cpp +++ b/modules/mono/editor/script_class_parser.cpp @@ -509,7 +509,7 @@ Error ScriptClassParser::parse(const String &p_code) { if (tk == TK_IDENTIFIER) { String name = value; - int at_level = type_curly_stack; + int at_level = curly_stack; ClassDecl class_decl; @@ -582,7 +582,7 @@ Error ScriptClassParser::parse(const String &p_code) { if (full_name.length()) full_name += "."; full_name += class_decl.name; - OS::get_singleton()->print("Ignoring generic class declaration: %s\n", class_decl.name.utf8().get_data()); + OS::get_singleton()->print("Ignoring generic class declaration: %s\n", full_name.utf8().get_data()); } } } -- cgit v1.2.3