Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor NodeKind to take advantage of a parse node only having one token kind. #3486

Merged
merged 2 commits into from
Dec 13, 2023

Conversation

jonmeow
Copy link
Contributor

@jonmeow jonmeow commented Dec 8, 2023

This builds on the series of changes to NodeKinds, aiming to simplify the NodeKind implementation a little, also making it clearer that there's a single associated token for each parse node (or, for placeholders/invalid parses, not validated).

Note that prior to the relevant changes, there were nodes with multiple tokens. This change is also locking in the approach of one token per parse node, by refactoring macros to stop supporting multiple.

@jonmeow
Copy link
Contributor Author

jonmeow commented Dec 8, 2023

(also a change with dependencies)

@jonmeow jonmeow force-pushed the single-token branch 3 times, most recently from 977cfce to eed2070 Compare December 11, 2023 19:38

// An invalid parse. Used to balance the parse tree. Always has an error.
CARBON_PARSE_NODE_KIND_CHILD_COUNT(InvalidParse, 0, CARBON_ANY_TOKEN_ON_ERROR)
CARBON_PARSE_NODE_KIND_CHILD_COUNT(InvalidParse, 0, CARBON_ONLY_IF_VALID(Error))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is confusing :)

Can we drop the CARBON_ONLY_IF_VALID here?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.


// An empty declaration, such as `;`.
CARBON_PARSE_NODE_KIND_CHILD_COUNT(EmptyDecl, 0,
CARBON_TOKEN(Semi) CARBON_ANY_TOKEN_ON_ERROR)
CARBON_PARSE_NODE_KIND_CHILD_COUNT(EmptyDecl, 0, CARBON_ONLY_IF_VALID(Semi))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd find this more readable without the "ONLY" in the macro name. To me, this reads as "the parse node will only correspond to a semicolon if it's valid (and otherwise will not be a semicolon)", not "the parse node will correspond to a semicolon, but that's only guaranteed if it's valid (and otherwise nothing is guaranteed)".

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

github-merge-queue bot pushed a commit that referenced this pull request Dec 13, 2023
Note #3486 rewrites the macro behavior, and is already approved: so this
PR is only for the changed enforcement during error. Also, #3493 already
changed several things to allow any token while this PR was awaiting
review, but this still changes enforcement for `For` and `If`.

This was brought up on
[#toolchain](https://discord.com/channels/655572317891461132/655578254970716160/1182066616456970251),
and I think this any-on-error approach gets at least some support. We
could try setting it to the introducer, but it's quite possible we want
it to be something like the token which led to the parse error, rather
than a static token. That leads to a conclusion that, most typically,
we'll expect arbitrary tokens when error conditions may lead to tokens
which aren't the expected token.

A couple related, recent `CARBON_IF_ERROR` crash fixes can be found in
#3404 and #3424. Something like #3404 would've been needed regardless
because `namespace` didn't have `CARBON_IF_ERROR` before, although I
might've missed the underlying issue with declarations because only
`namespace` had a relevant test (that is, if #3404 had added
`CARBON_ANY_TOKEN_ON_ERROR`, I wouldn't have had a crash in #3462).
#3424 would've been avoided with this change because there was a
`CARBON_IF_ERROR`, and it was just too restrictive.
@jonmeow jonmeow added this pull request to the merge queue Dec 13, 2023
Merged via the queue into carbon-language:trunk with commit add31eb Dec 13, 2023
6 checks passed
@jonmeow jonmeow deleted the single-token branch December 13, 2023 21:22
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants