-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Consider soft keyword for various parsing logic (#11474)
## Summary This PR updates various checks to consider soft keywords. 1. Update the following methods to also check whether the parser is at a soft keyword token: 1. `at_simple_stmt` 2. `at_stmt` 3. `at_expr` 4. `at_pattern_start` 5. `at_mapping_pattern_start` 2. Update various references of `parser.at(TokenKind::Name)` to use `parser.at_name_or_soft_keyword`. In the future, we should update it to also include other keywords on a case-by-case basis. 3. Re-use `parse_name` and `parse_identifier` for literal pattern parsing. We should always use either of these methods instead of doing the same manually to make sure that soft keyword are correctly tranformed. For (i) and (ii), we could've just extended the `EXPR_SET` with the soft keyword tokens but I think it's better to be explicit about what the method checks and to have separation between the token set corresponding to statement and soft keywords. ## Test Plan Add a few test cases and update the snapshots. These snapshots were generated through the local fork of the parser which compiles.
- Loading branch information
1 parent
c393e70
commit 306f702
Showing
15 changed files
with
774 additions
and
76 deletions.
There are no files selected for viewing
4 changes: 4 additions & 0 deletions
4
crates/ruff_python_parser/resources/inline/ok/except_stmt_as_name_soft_keyword.py
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 |
---|---|---|
@@ -0,0 +1,4 @@ | ||
try: ... | ||
except Exception as match: ... | ||
except Exception as case: ... | ||
except Exception as type: ... |
4 changes: 4 additions & 0 deletions
4
crates/ruff_python_parser/resources/inline/ok/from_import_soft_keyword_module_name.py
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 |
---|---|---|
@@ -0,0 +1,4 @@ | ||
from match import pattern | ||
from type import bar | ||
from case import pattern | ||
from match.type.case import foo |
3 changes: 3 additions & 0 deletions
3
crates/ruff_python_parser/resources/inline/ok/import_as_name_soft_keyword.py
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 |
---|---|---|
@@ -0,0 +1,3 @@ | ||
import foo as match | ||
import bar as case | ||
import baz as type |
4 changes: 4 additions & 0 deletions
4
crates/ruff_python_parser/resources/inline/ok/match_as_pattern_soft_keyword.py
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 |
---|---|---|
@@ -0,0 +1,4 @@ | ||
match foo: | ||
case case: ... | ||
case match: ... | ||
case type: ... |
5 changes: 5 additions & 0 deletions
5
crates/ruff_python_parser/resources/inline/ok/match_attr_pattern_soft_keyword.py
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 |
---|---|---|
@@ -0,0 +1,5 @@ | ||
match foo: | ||
case match.bar: ... | ||
case case.bar: ... | ||
case type.bar: ... | ||
case match.case.type.bar.type.case.match: ... |
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
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
Oops, something went wrong.