Scony / godot-gdscript-toolkit

Independent set of GDScript tools - parser, linter, formatter, and more
MIT License
903 stars 64 forks source link

Casting a parenthetical expr trips up the grammar #309

Open this-is-envy opened 3 weeks ago

this-is-envy commented 3 weeks ago

In the following code gdformat fails to parse:

  var segments: int = (int)(abs_normalized / 22.5)

The output is as follows:

./Scripts/Utils.gd:

  var segments: int = (int) (abs_normalized / 22.5)
                            ^

Unexpected token Token('LPAR', '(') at line 13, column 29.
Expected one of:
        * IN
        * "-="
        * "+="
        * AMPERSAND
        * ">>="
        * AS
        * "=="
        * EQUAL
        * _NL
        * "|="
        * "*="
        * LESSTHAN
        * VBAR
        * AND
        * ">="
        * MINUS
        * PLUS
        * "**"
        * SLASH
        * DOT
        * OR
        * "!="
        * SEMICOLON
        * "&="
        * LSQB
        * ">>"
        * STAR
        * "||"
        * NOT
        * PERCENT
        * "<="
        * "<<="
        * _DEDENT
        * IS
        * "/="
        * "&&"
        * "**="
        * "%="
        * MORETHAN
        * "<<"
        * IF
        * "^="
        * CIRCUMFLEX
Previous tokens: [Token('RPAR', ')')]
Scony commented 3 weeks ago

This is not implemented currently - either it's a relatively new syntax or it was overlooked.

this-is-envy commented 3 weeks ago

Thanks for the quick triage -- I actually came to say that I have no idea what's going on 😀.

My background is pretty deep but distinctly not-gdscript so I thought (type)(expr) was valid and my project was working when run. At some point godot cleared a cache though and I started getting weird errors that I traced back to (plausibly) this being invalid grammar / not parsing.

Per https://docs.godotengine.org/en/stable/tutorials/scripting/gdscript/gdscript_basics.html#casting x as y seems to be the only valid cast syntax so I'm going to preemptively close this and be somewhat confused at what was going on yesterday.

Scony commented 2 weeks ago

To me it looks like that syntax works in Godot. Anyway - I'll reopen this issue and do deep analysis of Godot behavior in that case.