MCPcopy Create free account
hub / github.com/PRQL/prql / needs_parenthesis

Function needs_parenthesis

prqlc/prqlc/src/codegen/ast.rs:51–87  ·  view source on GitHub ↗
(this: &pr::Expr, opt: &WriteOpt)

Source from the content-addressed store, hash-verified

49}
50
51fn needs_parenthesis(this: &pr::Expr, opt: &WriteOpt) -> bool {
52 if opt.unbound_expr && can_bind_left(&this.kind) {
53 return true;
54 }
55
56 let binding_strength = binding_strength(&this.kind);
57 if opt.context_strength > binding_strength {
58 // parent has higher binding strength, which means it would "steal" operand of this expr
59 // => parenthesis are needed
60 return true;
61 }
62
63 if opt.context_strength < binding_strength {
64 // parent has lower binding strength, so it won't "steal" operand of this expr
65 // => no parenthesis needed
66 return false;
67 }
68
69 // parent has equal binding strength, which means that now associativity of this expr counts
70 // for example:
71 // this=(a + b), parent=(a + b) + c
72 // asoc of + is left
73 // this is the left operand of parent
74 // => assoc_matches=true => we don't need parenthesis
75
76 // this=(a + b), parent=c + (a + b)
77 // asoc of + is left
78 // this is the right operand of parent
79 // => assoc_matches=false => we need parenthesis
80 let assoc_matches = match opt.binary_position {
81 super::Position::Left => associativity(&this.kind) == super::Position::Left,
82 super::Position::Right => associativity(&this.kind) == super::Position::Right,
83 super::Position::Unspecified => false,
84 };
85
86 !assoc_matches
87}
88
89impl WriteSource for pr::ExprKind {
90 fn write(&self, mut opt: WriteOpt) -> Option<String> {

Callers 1

writeMethod · 0.85

Calls 3

can_bind_leftFunction · 0.85
binding_strengthFunction · 0.85
associativityFunction · 0.85

Tested by

no test coverage detected