GDScript: Allow strings as multiline comments

Bring back the behavior in 3.x that was left out by oversight.
This commit is contained in:
George Marques 2023-02-09 10:16:00 -03:00
parent d02a7bc00d
commit f95967c299
No known key found for this signature in database
GPG Key ID: 046BD46A3201E43D
5 changed files with 75 additions and 21 deletions

View File

@ -483,7 +483,8 @@ void GDScriptParser::parse_program() {
current_class = head; current_class = head;
bool can_have_class_or_extends = true; bool can_have_class_or_extends = true;
while (match(GDScriptTokenizer::Token::ANNOTATION)) { while (!check(GDScriptTokenizer::Token::TK_EOF)) {
if (match(GDScriptTokenizer::Token::ANNOTATION)) {
AnnotationNode *annotation = parse_annotation(AnnotationInfo::SCRIPT | AnnotationInfo::STANDALONE | AnnotationInfo::CLASS_LEVEL); AnnotationNode *annotation = parse_annotation(AnnotationInfo::SCRIPT | AnnotationInfo::STANDALONE | AnnotationInfo::CLASS_LEVEL);
if (annotation != nullptr) { if (annotation != nullptr) {
if (annotation->applies_to(AnnotationInfo::SCRIPT)) { if (annotation->applies_to(AnnotationInfo::SCRIPT)) {
@ -502,6 +503,15 @@ void GDScriptParser::parse_program() {
break; break;
} }
} }
} else if (check(GDScriptTokenizer::Token::LITERAL) && current.literal.get_type() == Variant::STRING) {
// Allow strings in class body as multiline comments.
advance();
if (!match(GDScriptTokenizer::Token::NEWLINE)) {
push_error("Expected newline after comment string.");
}
} else {
break;
}
} }
while (can_have_class_or_extends) { while (can_have_class_or_extends) {
@ -524,6 +534,16 @@ void GDScriptParser::parse_program() {
end_statement("superclass"); end_statement("superclass");
} }
break; break;
case GDScriptTokenizer::Token::LITERAL:
if (current.literal.get_type() == Variant::STRING) {
// Allow strings in class body as multiline comments.
advance();
if (!match(GDScriptTokenizer::Token::NEWLINE)) {
push_error("Expected newline after comment string.");
}
break;
}
[[fallthrough]];
default: default:
// No tokens are allowed between script annotations and class/extends. // No tokens are allowed between script annotations and class/extends.
can_have_class_or_extends = false; can_have_class_or_extends = false;
@ -829,6 +849,16 @@ void GDScriptParser::parse_class_body(bool p_is_multiline) {
case GDScriptTokenizer::Token::DEDENT: case GDScriptTokenizer::Token::DEDENT:
class_end = true; class_end = true;
break; break;
case GDScriptTokenizer::Token::LITERAL:
if (current.literal.get_type() == Variant::STRING) {
// Allow strings in class body as multiline comments.
advance();
if (!match(GDScriptTokenizer::Token::NEWLINE)) {
push_error("Expected newline after comment string.");
}
break;
}
[[fallthrough]];
default: default:
// Display a completion with identifiers. // Display a completion with identifiers.
make_completion_context(COMPLETION_IDENTIFIER, nullptr); make_completion_context(COMPLETION_IDENTIFIER, nullptr);
@ -1675,6 +1705,12 @@ GDScriptParser::Node *GDScriptParser::parse_statement() {
// Standalone lambdas can't be used, so make this an error. // Standalone lambdas can't be used, so make this an error.
push_error("Standalone lambdas cannot be accessed. Consider assigning it to a variable.", expression); push_error("Standalone lambdas cannot be accessed. Consider assigning it to a variable.", expression);
break; break;
case Node::LITERAL:
if (static_cast<GDScriptParser::LiteralNode *>(expression)->value.get_type() == Variant::STRING) {
// Allow strings as multiline comments.
break;
}
[[fallthrough]];
default: default:
push_warning(expression, GDScriptWarning::STANDALONE_EXPRESSION); push_warning(expression, GDScriptWarning::STANDALONE_EXPRESSION);
} }

View File

@ -0,0 +1,21 @@
"""
This is a comment.
"""
@tool
"""
This is also a comment.
"""
extends RefCounted
'''
This is a comment too.
'''
func test():
"""
This too is a comment.
"""
print("ok")

View File

@ -0,0 +1,2 @@
GDTEST_OK
ok

View File

@ -1,6 +1,5 @@
func test(): func test():
# The following statements should all be reported as standalone expressions: # The following statements should all be reported as standalone expressions:
"This is a standalone expression"
1234 1234
0.0 + 0.0 0.0 + 0.0
Color(1, 1, 1) Color(1, 1, 1)

View File

@ -8,14 +8,10 @@ GDTEST_OK
>> STANDALONE_EXPRESSION >> STANDALONE_EXPRESSION
>> Standalone expression (the line has no effect). >> Standalone expression (the line has no effect).
>> WARNING >> WARNING
>> Line: 5 >> Line: 6
>> STANDALONE_EXPRESSION >> STANDALONE_EXPRESSION
>> Standalone expression (the line has no effect). >> Standalone expression (the line has no effect).
>> WARNING >> WARNING
>> Line: 7 >> Line: 7
>> STANDALONE_EXPRESSION >> STANDALONE_EXPRESSION
>> Standalone expression (the line has no effect). >> Standalone expression (the line has no effect).
>> WARNING
>> Line: 8
>> STANDALONE_EXPRESSION
>> Standalone expression (the line has no effect).