Skip to content

Return nil from parsing functions that didn’t consume any tokens #656

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

Closed

Conversation

ahoppen
Copy link
Member

@ahoppen ahoppen commented Aug 30, 2022

Depends on #655


Previously, we returned a missing syntax in these cases, which made it harder for recovery to determine whether anything was consumed.

Recovery should be handled as unexpected nodes, which are usually produced by `expect`.
Previously, we returned a missing syntax in these cases, which made it harder for recovery to determine whether anything was consumed.
while currentToken.tokenKind != .eof {
extraneousTokens.append(RawSyntax(consumeAnyToken()))
}
let unexpectedBeforeEof = extraneousTokens.isEmpty ? nil : RawUnexpectedNodesSyntax(elements: extraneousTokens, arena: self.arena)
Copy link
Contributor

Choose a reason for hiding this comment

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

Does this mean that if we encounter an un-parsable decl in the middle of the file that we'll drain the rest of the tokens until the EOF into this token list?

Copy link
Member Author

Choose a reason for hiding this comment

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

What do you mean by an un-parsable decl? If you mean

  • A declaration with syntax errors: No, absolutely not. That declaration will get stuck into the the tree with the syntax errors.
  • A declaration that’s so broken that the parser doesn’t make any progress trying to parse it: Yes. If we encounter such cases, we should improve recovery inside the parsing logic of that declaration so that the parser makes progress and puts the unexpected tokens into the declarations’s syntax node.

The idea is that unexpectedBeforeEof should contain those tokens that were previously errorToken of the last CodeBlockItem in the top level code block.

@ahoppen
Copy link
Member Author

ahoppen commented Aug 31, 2022

swiftlang/swift#60847

@swift-ci Please test

@ahoppen
Copy link
Member Author

ahoppen commented Sep 7, 2022

After some offline discussion, we decided that returning a missing node is the correct design here.

@ahoppen ahoppen closed this Sep 7, 2022
@ahoppen ahoppen deleted the ahoppen/nil-if-didnt-consume-tokens branch January 14, 2023 08:27
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants