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

Most upvoted comments

Any news on this subject ?

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 if statement 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 master now.

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 gdtoolkit version.

If someone here would like to help, I’d appreciate it if someone could install gdtoolkit from master using either:

pip3 install git+https://github.com/Scony/godot-gdscript-toolkit.git
# or
pipx install git+https://github.com/Scony/godot-gdscript-toolkit.git

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 true

Multiline 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.

The support for multiline (multi-statement) lambdas has been merged into master now. 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 gdtoolkit version. If someone here would like to help, I’d appreciate it if someone could install gdtoolkit from master using either:

pip3 install git+https://github.com/Scony/godot-gdscript-toolkit.git
# or
pipx install git+https://github.com/Scony/godot-gdscript-toolkit.git

and write a comment here stating that “in my project it works!”

Finally!! Thank you so much. It’s my hugest problem with this formatter. ❤️

Would you mind elaborating what the few remaining issues are? So that people might help on it.

Problem one: var foo = func(): if true: return 1 won’t parse

Problem 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!

Screenshot 2024-09-15 at 20 20 26

“In my project it works!”

TL;DR the lexer and parser support is done, the formatter support is like 5% done.

Does this mean that the linter works? Would it be possible to update gdlint to support multi-line lambdas without gdformat? I have some pretty long lambdas so it is difficult to work with them if they are in a single line but I like having a precommit hook run gdlint. I am working around it at the moment but by keeping the non-minified version in a comment but it is pretty hacky.

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.

So what is the current way to work around this? As this seems to be a lexer error, we can’t really gdlint: disable, I assume. Is there a way to exempt blocks of code from the whole linting process?

You can workaround it by having lambda returning one bing expression, or you can use ; to separate statements. See: https://github.com/Scony/godot-gdscript-toolkit/blob/master/tests/formatter/input-output-pairs/long-inline-lambdas.out.gd As for exempting blocks of code from the whole linting process - it’s not possible atm.

Sorry, this page is gone now… Do you mind explaining a little more about how to workaround?

Also, I am wondering if it would be able to just add some try/catch block to this error, so that at least the formatter can continue and finish formatting the file regardless of the error, instead of crashing at the point it encounters a lambda function and doesn’t format at all.

As for the workaround, lambda may be written in one line where statements are separated by a semicolon like this:

var f = func(key):print("key:", key);return true

also, if the lambda has some expression inside, it can be extended to multiple lines like:

var f = func(x): return (
    x is int
    and x > 0
    and x < 10
)

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

var l_add_and_print := func(root_path, name, is_file):
		if is_file:
			var path = root_path+"/"+name
			gut.p(path)
			paths.append(path)

Error:

print := func(root_path, name, is_file):
                                        ^

Unexpected token Token('_NL', '\n\t\t') at line 8, column 56.
Expected one of: 
	* DOLLAR
	* VAR
	* PERCENT
	* NAME
	* FUNC
	* NUMBER
	* PLUS
	* AMPERSAND
	* RETURN
	* HEX
	* NOT
	* PASS
	* LPAR
	* BIN
	* LONG_STRING
	* TILDE
	* LSQB
	* AWAIT
	* CIRCUMFLEX
	* REGULAR_STRING
	* LBRACE
	* BANG
	* MINUS

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.