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

Make Lexer lazy #11244

Merged
merged 2 commits into from May 17, 2024
Merged

Make Lexer lazy #11244

merged 2 commits into from May 17, 2024

Conversation

dhruvmanila
Copy link
Member

@dhruvmanila dhruvmanila commented May 2, 2024

Summary

This PR updates the Lexer to make it lazy in the sense that the tokens are emitted only when it's requested.

Lexer

  • Remove Iterator implementation
  • Remove SoftkeywordTransformer
  • Collect all LexicalError in the lexer and return it on finish call
  • Store the current token and provide methods to query it
  • Implement a new TokenValue struct which stores the owned value for certain tokens 1
  • Update all lex_* methods to return the token instead of Result
  • Update Lexer::new to take a start_offset parameter
    • This will be used to start lexing from a specific offset
  • Add checkpoint - rewind logic for the lexer, f-strings and indentation stack

Token Source

  • Remove Iterator implementation
  • Provide lookahead via checkpoint - rewind logic on the Lexer
  • Store all the lexed tokens

Parser

  • Update the soft keywords to identifier when parsing as an identifier
  • Add bump_value to bump the given token kind and return the corresponding owned value
  • Add bump_any to bump any token except for end of file

Footnotes

  1. At the end, we'll remove the Tok completely

Copy link
Member

@MichaReiser MichaReiser left a comment

Choose a reason for hiding this comment

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

I like this a lot! Nice work.

I think we can improve the lexer performance by change how we handle errors in next_token (see inline comment).

We also need to think about how parse_expression_at should work. I think we need to pass the offset through to the lexer so that it can offset all token ranges and start lexing after offset in the source.

crates/ruff_python_parser/src/parser/expression.rs Outdated Show resolved Hide resolved
crates/ruff_python_parser/src/parser/mod.rs Outdated Show resolved Hide resolved
crates/ruff_python_parser/src/parser/mod.rs Outdated Show resolved Hide resolved
crates/ruff_python_parser/src/parser/mod.rs Outdated Show resolved Hide resolved
Copy link
Member

Choose a reason for hiding this comment

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

It almost makes me sad to say goodby to @charliermarsh's genius implementation for supporting soft keywords with lalrpop.

crates/ruff_python_parser/src/lexer.rs Show resolved Hide resolved
crates/ruff_python_parser/src/lexer.rs Outdated Show resolved Hide resolved
crates/ruff_python_parser/src/lexer.rs Outdated Show resolved Hide resolved
crates/ruff_python_parser/src/lexer.rs Outdated Show resolved Hide resolved
crates/ruff_python_parser/src/lexer.rs Outdated Show resolved Hide resolved
@dhruvmanila dhruvmanila force-pushed the dhruv/lazy-lexer branch 3 times, most recently from 3ca2ba3 to 2f35044 Compare May 17, 2024 05:57
@dhruvmanila dhruvmanila changed the base branch from main to dhruv/parser-phase-2 May 17, 2024 09:40
Comment on lines +119 to +121
if start_offset > TextSize::new(0) {
lexer.cursor.skip_bytes(start_offset.to_usize());
}
Copy link
Member Author

Choose a reason for hiding this comment

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

This is how the lexer will start lexing from the given offset.

@dhruvmanila dhruvmanila added the parser Related to the parser label May 17, 2024
@dhruvmanila dhruvmanila changed the title WIP: Make Lexer lazy Make Lexer lazy May 17, 2024
@dhruvmanila
Copy link
Member Author

I'm going to merge this into another PR to keep going. I'm finding it difficult to refactor with changes separated in different branches.

@dhruvmanila dhruvmanila marked this pull request as ready for review May 17, 2024 11:33
@dhruvmanila dhruvmanila merged commit bfc513e into dhruv/parser-phase-2 May 17, 2024
6 of 18 checks passed
@dhruvmanila dhruvmanila deleted the dhruv/lazy-lexer branch May 17, 2024 11:34
dhruvmanila added a commit that referenced this pull request May 17, 2024
This PR updates the `Lexer` to make it lazy in the sense that the tokens
are emitted only when it's requested.

* Remove `Iterator` implementation
* Remove `SoftkeywordTransformer`
* Collect all `LexicalError` in the lexer and return it on `finish` call
* Store the `current` token and provide methods to query it
* Implement a new `TokenValue` struct which stores the owned value for
certain tokens [^1]
* Update all `lex_*` methods to return the token instead of `Result`
* Update `Lexer::new` to take a `start_offset` parameter
	* This will be used to start lexing from a specific offset
* Add checkpoint - rewind logic for the lexer, f-strings and indentation
stack

* Remove `Iterator` implementation
* Provide lookahead via checkpoint - rewind logic on the `Lexer`
* Store all the lexed tokens

* Update the soft keywords to identifier when parsing as an identifier
* Add `bump_value` to bump the given token kind and return the
corresponding owned value
* Add `bump_any` to bump any token except for end of file

[^1]: At the end, we'll remove the `Tok` completely
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
parser Related to the parser
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants