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(parser): Use peek_token() to streamline peek operations #900

Open
wants to merge 3 commits into
base: main
Choose a base branch
from

Conversation

surajk-m
Copy link

Closes #830

This PR refactors parser to use peek_token() instead of separate peek() and peek_data() calls.

Changes:

  • Replaced instances of peek() / peek_data() call pairs.
  • Removed unwrap() by working directly with the peek_token(). This ensures that the token's kind and data are always in sync and that no intermediate changes can occur between the checks.

@apollo-cla
Copy link

@surajk-m: Thank you for submitting a pull request! Before we can merge it, you'll need to sign the Apollo Contributor License Agreement here: https://contribute.apollographql.com/

@SimonSapin
Copy link
Contributor

Hi, and thanks for submitting this PR!

Issue #830 specifically pertains to the pattern where peek() is followed by peek_data().unwrap(), it’s the unwrap() part and its potential panic that we try to avoid. Using peek() or peek_data() without unwrap() is fine and should not be replaced with more verbose code.

There are a few other locations with the peek_data().unwrap() pattern is used:

  • crates/apollo-parser/src/parser/grammar/fragment.rs
  • crates/apollo-parser/src/parser/grammar/document.rs
  • crates/apollo-parser/src/parser/grammar/value.rs
  • crates/apollo-parser/src/parser/grammar/name.rs

Would you be able to update those locations as well and undo the cases where peek_token() isn't needed?

@surajk-m
Copy link
Author

Hi @SimonSapin,

Thanks for reviewing my PR! I'll revert the changes where peek_token() isn't needed and will update the remaining instances of peek_data().unwrap() pattern used in the mentioned files.

@surajk-m surajk-m closed this Sep 16, 2024
@surajk-m surajk-m force-pushed the harden-peek-token-calls branch from a1fa0cc to 84c8d5f Compare September 16, 2024 19:43
@surajk-m surajk-m reopened this Sep 16, 2024
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.

Harden peek() / peek_data() call pairs in the parser
3 participants