Skip to content

Parse parenthesis in type grammar into syntax tree #16743

@straight-shoota

Description

@straight-shoota

The parser currently skips parenthesis in type grammar expressions. It still registers the effects for grouping expressions, so the semantic result is complete.

# When we parse a type, parentheses information is not stored in ASTs, unlike
# for an Expressions node. So when we are printing a type (Path, ProcNotation, Union, etc.)
# we increment this when we find a '(', and decrement it when we find ')', but
# only if `paren_count > 0`: it might be the case of `def foo(x : A)`, but we don't
# want to print that last ')' when printing the type A.
@paren_count = 0

But it does not fully capture the syntactic expressiveness. This would be useful for the formatter and other tooling that operates on the syntax tree and needs to be aware of parenthesis semantics. This information would be critical for formatter enhancements that rewrite parenthesis.

In the normal language, parenthesis map to Expressions nodes, which makes a clear assignment possible.

We can transfer that to the type grammar and introduce a similar node. I believe a Union node with a single member could be a good representation for that. It's semantically valid, even though it's technically redundant (a union of one element is equivalent to that element).

Since it's only needed for the syntax tree, we could consider cleaning up those extra nodes afterwards. Or perhaps make their creation conditional.
I suppose it wouldn't be much overhead to have them created in every parse run.

As a first effect, we can simplify the convoluted formatter code for dealing with parenthesis immensely. It's so much cleaner.

And it opens the door to make enhancements such as #11966 and #16728 relatively trivial to implement.


Add a 👍 reaction to issues you find important.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions