godot-gdscript-toolkit: GDScript 2.0 - multiline lambdas not supported
This is flagged as error on gd-format but completely legit syntax in GDScript 2.0:
func f():
return func():
pass
Error:
return func():
^
Unexpected token Token('_NL', '\n\t\t') at line 11, column 16.
Expected one of:
* NOT
* HEX
* BANG
* BIN
* VAR
* AWAIT
* PERCENT
* NUMBER
* LPAR
* LBRACE
* REGULAR_STRING
* CIRCUMFLEX
* TILDE
* LSQB
* RETURN
* NAME
* LONG_STRING
* DOLLAR
* MINUS
* FUNC
* PLUS
* AMPERSAND
* PASS
About this issue
- Original URL
- State: closed
- Created 4 years ago
- Reactions: 18
- Comments: 31 (13 by maintainers)
Commits related to this issue
- Merge branch 'multiline-lambdas', fixes #191 — committed to kuruk-mm/godot-gdscript-toolkit by Scony 2 years ago
It’s WIP, you can track progress here: https://github.com/Scony/godot-gdscript-toolkit/commits/multiline-lambdas/
TL;DR the lexer and parser support is done, the formatter support is like 5% done.
@Skyway666 that’s a good point - I can try exploring that. It’s definitely not as simple as writing a single
ifstatement or so, but maybe with some more conditions, it will be possible to give such helpful error in, say, 80% of situations. That would help a lot, I agree.The support for multiline (multi-statement) lambdas has been merged into
masternow.There are few not implemented problems, but IMO 99.9% of users should not notice them, so I decided to proceed.
Now, I’d like the change to sink in for few days/weeks before I release new
gdtoolkitversion.If someone here would like to help, I’d appreciate it if someone could install
gdtoolkitfrommasterusing either:and write a comment here stating that “in my project it works!”
@MikeSchulze since it does happen on the parser level, the
# gdlint:disable=is not possible. I’ll try to fix it at some point probably. Anyway, it’s still a low prio as for fairly small lambdas it can be worked around by turning multiline lambda into single-line one:func(key):prints("key:", key);return trueMultiline lambdas are currently not supported. They won’t be anytime soon as it requires writing a custom lexer to avoid ambiguity on the parser level.
Problem one:
var foo = func(): if true: return 1won’t parseProblem two: https://github.com/Scony/godot-gdscript-toolkit/commit/89b8200f403fcd1364bbcc8ae4cf80597b8243b1#diff-deb96115f719c01f21e10dcc674db18d6be7d759a1a3badaf506c9c9f6625a0fR216-R221
Both are most likely a bug in the lexer, so hopefully, I can address those fairly easily.
In my project it works! This is fantastic - thank you!
“In my project it works!”
Yes, linter should work - but making
gdformat(even broken) aligned to that requires effort, therefore I can only suggest you to try https://github.com/Scony/godot-gdscript-toolkit/tree/multiline-lambdas branch yourself.P.S. I’m not sure but there may be some regression in the parser so if you get parser errors then it won’t work for you anyway.
As for the workaround, lambda may be written in one line where statements are separated by a semicolon like this:
also, if the lambda has some expression inside, it can be extended to multiple lines like:
As for the try-catch - it’s not possible without multiline lambdas support.
I can understand not wanting to support Multiline Lambdas if it implies too much work (I’ve never build a linter or formatter) since a simple workaround is creating a separate function for the callback.
However, would it be much harder to work on handling the exception? When parsing multiple files the user doesn’t even know which one is causing the error, and it’s hard to deduce why it is happening. An error message as the following would be helpful:
Error in "sample.gd" line 32: gdscript toolkit doesn't support Multiline Lambda functions, please create a separate function to assign as callback.At least it will prevent users from writing more duplicate issues about the matter 😃
Same problem here on a maybe more tricky problem
Error:
Disable the linter is ok but we can’t disable the formatter for this problem. Also if we have a “if”, can’t make the trick to do all in the same line
Unexpected token Token('COLON', ':') at line 11, column 66.