Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 20 additions & 25 deletions modules/gdscript/gdscript_parser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -763,14 +763,10 @@ void GDScriptParser::parse_program() {
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;
if (!parse_standalone_string()) {
break;
}
}
}

Expand Down Expand Up @@ -806,12 +802,7 @@ void GDScriptParser::parse_program() {
can_have_class_or_extends = false;
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.");
}
if (parse_standalone_string()) {
break;
}
[[fallthrough]];
Expand Down Expand Up @@ -1201,12 +1192,7 @@ void GDScriptParser::parse_class_body(bool p_is_multiline) {
class_end = true;
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.");
}
if (parse_standalone_string()) {
break;
}
[[fallthrough]];
Expand Down Expand Up @@ -2204,12 +2190,6 @@ GDScriptParser::Node *GDScriptParser::parse_statement() {
// 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);
break;
case Node::LITERAL:
// Allow strings as multiline comments.
if (static_cast<GDScriptParser::LiteralNode *>(expression)->value.get_type() != Variant::STRING) {
push_warning(expression, GDScriptWarning::STANDALONE_EXPRESSION);
}
break;
case Node::TERNARY_OPERATOR:
push_warning(expression, GDScriptWarning::STANDALONE_TERNARY);
break;
Expand Down Expand Up @@ -3930,6 +3910,21 @@ GDScriptParser::TypeNode *GDScriptParser::parse_type(bool p_allow_void) {
return type;
}

bool GDScriptParser::parse_standalone_string() {
if (check(GDScriptTokenizer::Token::LITERAL) && current.literal.get_type() == Variant::STRING) {
// For compatibility we allow standalone strings without erroring.
advance();
#ifdef DEBUG_ENABLED
push_warning(previous.start_line, previous.start_column, previous.end_line, previous.end_column, GDScriptWarning::STANDALONE_EXPRESSION);
#endif
if (!match(GDScriptTokenizer::Token::NEWLINE)) {
push_error("Expected newline after comment string.");
}
return true;
}
return false;
}

#ifdef TOOLS_ENABLED
enum DocLineState {
DOC_LINE_NORMAL,
Expand Down
2 changes: 2 additions & 0 deletions modules/gdscript/gdscript_parser.h
Original file line number Diff line number Diff line change
Expand Up @@ -1654,6 +1654,8 @@ class GDScriptParser {
ExpressionNode *parse_yield(ExpressionNode *p_previous_operand, bool p_can_assign);
ExpressionNode *parse_invalid_token(ExpressionNode *p_previous_operand, bool p_can_assign);
TypeNode *parse_type(bool p_allow_void = false);
// TODO: Remove in 5.x.
bool parse_standalone_string();
Comment thread
HolonProduction marked this conversation as resolved.

#ifdef TOOLS_ENABLED
int max_script_doc_line = INT_MAX;
Expand Down

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,11 +1,29 @@
"""
This is not a comment.
"""

@tool

"This also isn't a comment."

extends RefCounted

'''
This one neither.
'''

func test():
# The following statements should all be reported as standalone expressions:
1234
0.0 + 0.0
Color(1, 1, 1)
Color(1, 1, 1) # TODO
Vector3.ZERO
[true, false]
float(125)
float(125) # TODO
"""
Python-like "comment".
"""
'single line string'
# The following statements should not produce `STANDALONE_EXPRESSION`:
var _a = 1
_a = 2 # Assignment is a local (or global) side effect.
Expand All @@ -14,8 +32,7 @@ func test():
absi(4) # A call (in general) can have side effects.
@warning_ignore("return_value_discarded")
preload("../../utils.notest.gd") # A static initializer may have side effects.
"""
Python-like "comment".
"""
@warning_ignore("standalone_ternary")
1 if 2 else 3 # Produces `STANDALONE_TERNARY` instead.

'Still no comment...'
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
GDTEST_OK
~~ WARNING at line 3: (STANDALONE_EXPRESSION) Standalone expression (the line may have no effect).
~~ WARNING at line 4: (STANDALONE_EXPRESSION) Standalone expression (the line may have no effect).
~~ WARNING at line 6: (STANDALONE_EXPRESSION) Standalone expression (the line may have no effect).
~~ WARNING at line 1: (STANDALONE_EXPRESSION) Standalone expression (the line may have no effect).
~~ WARNING at line 7: (STANDALONE_EXPRESSION) Standalone expression (the line may have no effect).
~~ WARNING at line 11: (STANDALONE_EXPRESSION) Standalone expression (the line may have no effect).
~~ WARNING at line 17: (STANDALONE_EXPRESSION) Standalone expression (the line may have no effect).
~~ WARNING at line 18: (STANDALONE_EXPRESSION) Standalone expression (the line may have no effect).
~~ WARNING at line 20: (STANDALONE_EXPRESSION) Standalone expression (the line may have no effect).
~~ WARNING at line 21: (STANDALONE_EXPRESSION) Standalone expression (the line may have no effect).
~~ WARNING at line 23: (STANDALONE_EXPRESSION) Standalone expression (the line may have no effect).
~~ WARNING at line 26: (STANDALONE_EXPRESSION) Standalone expression (the line may have no effect).
~~ WARNING at line 38: (STANDALONE_EXPRESSION) Standalone expression (the line may have no effect).