ruff/crates/ruff_python_parser
Dhruv Manilawala 7eba967e16
Refactor binary expression parsing (#11073)
## Summary

This PR refactors the binary expression parsing in a way to make it
readable and easy to understand. It draws inspiration from the suggested
edits in the linked messages in #10752.

### Changes

* Ability to get the precedence of an operator
	* From a boolean operator (`BinOp`) to `OperatorPrecedence`
	* From a binary operator (`Operator`) to `OperatorPrecedence`
	* No comparison operator because all of them have the same precedence
* Implement methods on `TokenKind` to convert it to an appropriate
operator enum
	* Add `as_boolean_operator` which returns an `Option<BoolOp>`
	* Add `as_binary_operator` which returns an `Option<Operator>`
* No `as_comparison_operator` because it requires lookahead and I'm not
sure if `token.as_comparison_operator(peek)` is a good way to implement
it
* Introduce `BinaryLikeOperator`
	* Constructed from two tokens using the methods from the second point
* Add `precedence` method using the conversion methods mentioned in the
first point
* Make most of the functions in `TokenKind` private to the module
* Use `self` instead of `&self` for `TokenKind` 

fixes: #11072

## Test Plan

Refer #11088
2024-04-23 04:42:40 +00:00
..
resources Add ExpressionContext for expression parsing (#11055) 2024-04-23 04:19:05 +00:00
src Refactor binary expression parsing (#11073) 2024-04-23 04:42:40 +00:00
tests Add ExpressionContext for expression parsing (#11055) 2024-04-23 04:19:05 +00:00
Cargo.toml Add ExpressionContext for expression parsing (#11055) 2024-04-23 04:19:05 +00:00
CONTRIBUTING.md Replace LALRPOP parser with hand-written parser (#10036) 2024-04-18 17:57:39 +05:30