-
Notifications
You must be signed in to change notification settings - Fork 1.2k
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
Support type alias statements in simple statement positions #8916
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,7 @@ | ||
use crate::{lexer::LexResult, token::Tok, Mode}; | ||
use itertools::{Itertools, MultiPeek}; | ||
|
||
use crate::{lexer::LexResult, token::Tok, Mode}; | ||
|
||
/// An [`Iterator`] that transforms a token stream to accommodate soft keywords (namely, `match` | ||
/// `case`, and `type`). | ||
/// | ||
|
@@ -21,7 +22,7 @@ where | |
I: Iterator<Item = LexResult>, | ||
{ | ||
underlying: MultiPeek<I>, | ||
start_of_line: bool, | ||
position: Position, | ||
} | ||
|
||
impl<I> SoftKeywordTransformer<I> | ||
|
@@ -31,7 +32,11 @@ where | |
pub fn new(lexer: I, mode: Mode) -> Self { | ||
Self { | ||
underlying: lexer.multipeek(), // spell-checker:ignore multipeek | ||
start_of_line: !matches!(mode, Mode::Expression), | ||
position: if mode == Mode::Expression { | ||
Position::Other | ||
} else { | ||
Position::Statement | ||
}, | ||
} | ||
} | ||
} | ||
|
@@ -49,17 +54,16 @@ where | |
// If the token is a soft keyword e.g. `type`, `match`, or `case`, check if it's | ||
// used as an identifier. We assume every soft keyword use is an identifier unless | ||
// a heuristic is met. | ||
|
||
match tok { | ||
// For `match` and `case`, all of the following conditions must be met: | ||
// 1. The token is at the start of a logical line. | ||
// 2. The logical line contains a top-level colon (that is, a colon that is not nested | ||
// inside a parenthesized expression, list, or dictionary). | ||
// 3. The top-level colon is not the immediate sibling of a `match` or `case` token. | ||
// (This is to avoid treating `match` or `case` as identifiers when annotated with | ||
// type hints.) type hints.) | ||
// type hints.) | ||
Tok::Match | Tok::Case => { | ||
if self.start_of_line { | ||
if matches!(self.position, Position::Statement) { | ||
let mut nesting = 0; | ||
let mut first = true; | ||
let mut seen_colon = false; | ||
|
@@ -93,7 +97,10 @@ where | |
// 2. The type token is immediately followed by a name token. | ||
// 3. The name token is eventually followed by an equality token. | ||
Tok::Type => { | ||
if self.start_of_line { | ||
if matches!( | ||
self.position, | ||
Position::Statement | Position::SimpleStatement | ||
) { | ||
let mut is_type_alias = false; | ||
if let Some(Ok((tok, _))) = self.underlying.peek() { | ||
if matches!( | ||
|
@@ -132,18 +139,56 @@ where | |
} | ||
} | ||
|
||
self.start_of_line = next.as_ref().is_some_and(|lex_result| { | ||
lex_result.as_ref().is_ok_and(|(tok, _)| { | ||
if matches!(tok, Tok::NonLogicalNewline | Tok::Comment { .. }) { | ||
return self.start_of_line; | ||
// Update the position, to track whether we're at the start of a logical line. | ||
if let Some(lex_result) = next.as_ref() { | ||
if let Ok((tok, _)) = lex_result.as_ref() { | ||
match tok { | ||
Tok::NonLogicalNewline | Tok::Comment { .. } => { | ||
// Nothing to do. | ||
} | ||
Tok::StartModule | Tok::Newline | Tok::Indent | Tok::Dedent => { | ||
self.position = Position::Statement; | ||
} | ||
// If we see a semicolon, assume we're at the start of a simple statement, as in: | ||
// ```python | ||
// type X = int; type Y = float | ||
// ``` | ||
Tok::Semi => { | ||
self.position = Position::SimpleStatement; | ||
} | ||
// If we see a colon, and we're not in a nested context, assume we're at the | ||
// start of a simple statement, as in: | ||
// ```python | ||
// class Class: type X = int | ||
// ``` | ||
Tok::Colon if self.position == Position::Other => { | ||
self.position = Position::SimpleStatement; | ||
} | ||
Tok::Lpar | Tok::Lsqb | Tok::Lbrace => { | ||
self.position = if let Position::Nested(depth) = self.position { | ||
Position::Nested(depth.saturating_add(1)) | ||
} else { | ||
Position::Nested(1) | ||
}; | ||
} | ||
Tok::Rpar | Tok::Rsqb | Tok::Rbrace => { | ||
self.position = if let Position::Nested(depth) = self.position { | ||
let depth = depth.saturating_sub(1); | ||
if depth > 0 { | ||
Position::Nested(depth) | ||
} else { | ||
Position::Other | ||
} | ||
} else { | ||
Position::Other | ||
}; | ||
} | ||
_ => { | ||
self.position = Position::Other; | ||
} | ||
} | ||
|
||
matches!( | ||
tok, | ||
Tok::StartModule | Tok::Newline | Tok::Indent | Tok::Dedent | ||
) | ||
}) | ||
}); | ||
} | ||
} | ||
|
||
next | ||
} | ||
|
@@ -161,3 +206,19 @@ fn soft_to_name(tok: &Tok) -> Tok { | |
name: name.to_owned(), | ||
} | ||
} | ||
|
||
#[derive(Debug, Clone, Copy, PartialEq, Eq)] | ||
enum Position { | ||
/// The lexer is at the start of a logical line, i.e., the start of a simple or compound statement. | ||
Statement, | ||
/// The lexer is at the start of a simple statement, e.g., a statement following a semicolon | ||
/// or colon, as in: | ||
/// ```python | ||
/// class Class: type X = int | ||
/// ``` | ||
SimpleStatement, | ||
/// The lexer is within brackets, with the given bracket nesting depth. | ||
Nested(u32), | ||
/// The lexer is some other location. | ||
Other, | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This and |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Unrelated to your change: It's somewhat unfortunate that it's necessary to track whether we're at the start of the line, because we already know this inside of the
Lexer
.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Definitely. Perhaps we could move this into the lexer...