Skip to content

Introduce peek(isAt:) #1942

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

Merged
merged 1 commit into from
Jul 25, 2023
Merged

Introduce peek(isAt:) #1942

merged 1 commit into from
Jul 25, 2023

Conversation

ahoppen
Copy link
Member

@ahoppen ahoppen commented Jul 24, 2023

This makes it a lot easier to check if the next token is of a specific kind.

This makes it a lot easier to check if the next token is of a specific kind.
@ahoppen ahoppen requested a review from bnbarham July 24, 2023 16:24
@ahoppen
Copy link
Member Author

ahoppen commented Jul 24, 2023

@swift-ci Please test

extension TokenConsumer {
/// Returns whether the next (peeked) token matches `spec`
@inline(__always)
mutating func peek(isAt spec: TokenSpec) -> Bool {
Copy link
Contributor

Choose a reason for hiding this comment

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

Thoughts on just at instead of isAt? next could work as well. Or maybe nextAt(_:) instead of peek to match at(_:)?

Copy link
Member Author

Choose a reason for hiding this comment

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

I deliberately decided against peek(at:) because we already have peek(at offset: Int) in the lexer, which peeks offset bytes ahead, which has completely different semantics.

We could consider renaming peek to next altogether. I’m not sure how much of a term of art peek is in parsers in general and whether we should stick to it because of that reason though.

Copy link
Contributor

Choose a reason for hiding this comment

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

Could also change peek(at offset: Int) to something else. You don't need to block the PR on this though, easy enough to rename peek(isAt:) if we want to after.

@ahoppen ahoppen merged commit 1a594a6 into swiftlang:main Jul 25, 2023
@ahoppen ahoppen deleted the ahoppen/peek-is-at branch July 25, 2023 16:33
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