Skip to content

Internal improvements to Parser::next_token/prev_token #86

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 2 commits into from
Jun 2, 2019

Conversation

nickolay
Copy link
Contributor

@nickolay nickolay commented Jun 2, 2019

See the commit messages for the details.

This reduces the number of helper functions used by next_token()/prev_token() while slightly improving performance and reducing the chances of errors when using prev_token() after hitting end-of-file.

nickolay added 2 commits June 2, 2019 22:57
- Avoid cloning whitespace tokens in `peek_nth_token()` by using a
  &Token from `tokens.get()` instead of a cloned `Token` from `token_at()`

- Similarly avoid cloning in `next_token_no_skip`, and clone the
  non-whitespace tokens in `next_token` instead.

- Remove `token_at`, which was only used in `peek_token` and
  `peek_nth_token`

- Fold `prev_token_no_skip()` into `prev_token()` and make `prev_token`
  return nothing, as the return value isn't used anyway.
Before this `next_token()` would only increment the index when returning
`Some(token)`. This means that the caller wishing to rewind must be
careful not to call `prev_token()` on EOF (`None`), while not forgetting
to call it for `Some`. Not doing this resulted in bugs in the
undertested code that does error handling.

After making this mistake several times, I'm changing `next_token()` /
`prev_token()` so that calling `next_token(); prev_token(); next_token()`
returns the same token in the first and the last invocation.
@nickolay nickolay requested a review from benesch June 2, 2019 20:07
@coveralls
Copy link

Pull Request Test Coverage Report for Build 233

  • 31 of 31 (100.0%) changed or added relevant lines in 1 file are covered.
  • 1 unchanged line in 1 file lost coverage.
  • Overall coverage decreased (-0.07%) to 90.094%

Files with Coverage Reduction New Missed Lines %
src/sqlparser.rs 1 89.88%
Totals Coverage Status
Change from base Build 232: -0.07%
Covered Lines: 3347
Relevant Lines: 3715

💛 - Coveralls

Copy link
Contributor

@benesch benesch left a comment

Choose a reason for hiding this comment

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

This looks awesome!

@nickolay nickolay merged commit 2308c1c into apache:master Jun 2, 2019
@nickolay nickolay deleted the pr/token-refactor branch June 2, 2019 23:44
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.

3 participants