Lambda requires extra line before End Of File
Tested versions
- Reproducible in Godot 4.3
System information
Windows 11, Godot 4.3-stable
Issue description
If I leave a lamda at the end of the file like this (line 12):
The parser throws an error:
However, this is fine (with extra line 13):
Ignore the fact that I'm doing stupid signal stuff above and causing infinite recursion, that's not a bug that's lack of sleep 💩
Pretty annoying as I frequently write lambdas like this and they happen to be at the EOF.
Steps to reproduce
Open the MRP, issue.gd will not be parsed correctly.
Minimal reproduction project (MRP)
I can confirm the issue on Linux (Manjaro KDE Wayland), although to be fair I always have a single empty line at EOF (and the editor adds one automatically if I don't anyway).
Also, on my end the error looks different:
This occurs both when the parenthesis is on the same line as the function contents and when it's on its own line as in the screenshot.
As I just noticed that for the first time: It also happens when everything is inline.
If no one else is on this one, I'll take it. Looks like it might be a good spot to get my feet wet without stepping on too many toes.
Still looking into this. Current lead: in GDScriptParser::parse_suite(), lambda_ended gets set to true once and then the function gets hit again. Something tells me this shouldn't happen in this scenario.
I'm tapping out on this one. I'm posting my findings here so that someone else can get a head start if they decide to tackle it.
The summary: I've narrowed down the bug to having something to do with the global variables within GDScriptTokenizer::Token GDScriptTokenizerText::scan() in gdscript_tokenizer.cpp. Whether it has to do with the mutation of pending_indents or not, _is_at_end() on line 1443 returns False when there's no newline at the end of the lambda in the MRP, causing make_token(Token::TK_EOF) to not get called. This is the difference between MRP with newline and MRP with no newline.
If anyone has more detailed questions, feel free to ask me. I'll try to give as much guidance as I can.