Remove syntax editing from parenthesis computation

This commit is contained in:
Lukas Wirth 2025-02-28 11:53:16 +01:00
parent e92dc3fd80
commit 570c6adcd3
6 changed files with 50 additions and 87 deletions

View file

@ -41,6 +41,21 @@ pub enum ExprPrecedence {
Unambiguous,
}
impl ExprPrecedence {
pub fn needs_parentheses_in(self, other: ExprPrecedence) -> bool {
match other {
ExprPrecedence::Unambiguous => false,
// postfix ops have higher precedence than any other operator, so we need to wrap
// any inner expression that is below
ExprPrecedence::Postfix => self < ExprPrecedence::Postfix,
// We need to wrap all binary like things, thats everything below prefix except for
// jumps (as those are prefix operations as well)
ExprPrecedence::Prefix => ExprPrecedence::Jump < self && self < ExprPrecedence::Prefix,
parent => self <= parent,
}
}
}
#[derive(PartialEq, Debug)]
pub enum Fixity {
/// The operator is left-associative
@ -137,7 +152,7 @@ impl Expr {
// - https://github.com/rust-lang/rust/blob/b6852428a8ea9728369b64b9964cad8e258403d3/compiler/rustc_ast/src/util/parser.rs#L296
/// Returns `true` if `self` would need to be wrapped in parentheses given that its parent is `parent`.
pub fn needs_parens_in(&self, parent: SyntaxNode) -> bool {
pub fn needs_parens_in(&self, parent: &SyntaxNode) -> bool {
match_ast! {
match parent {
ast::Expr(e) => self.needs_parens_in_expr(&e),