1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
//! Helpers to deal with precedence and parentheses.
use crate::{
ast::{BinaryOp, SymbolBinaryOp, AST},
operators::{self, Op},
};
/// Precedence >.
fn prec_gt(prec1: &Option<u8>, prec2: &Option<u8>) -> bool {
prec1
.map(|p| p > prec2.unwrap_or_default())
.unwrap_or(false)
}
/// Returns a tuple indicating whether the left and right children need
/// parentheses to be displayed properly.
pub fn need_parens(bin_op: &BinaryOp, lchild: &AST, rchild: &AST) -> (bool, bool) {
match &bin_op {
BinaryOp::Generic(SymbolBinaryOp {
op:
Op {
sym: _,
l_prec,
r_prec,
},
fixity: _,
}) => (
match lchild {
AST::Sym(_) => false, // a + b is fine
AST::Number(_) => false, // 2 + 3 is fine
// for binary operations: if the corresponding
// precedence is higher than the precedence of
// this operator, then parenthese are needed.
// For example, 2 ^ 3 + 1 ^ 2 is fine, but
// (2 + 3) ^ (1 + 2) needs parentheses.
// None is treated as never needing parentheses:
// 2 ^ ±3 is unambiguous, if weird
AST::BinaryExpr(bop, _, _) => match bop {
BinaryOp::Generic(SymbolBinaryOp {
op: l_op,
fixity: _,
}) => prec_gt(&l_op.r_prec, l_prec),
BinaryOp::Power => prec_gt(&operators::POWER.r_prec, l_prec),
BinaryOp::Frac => prec_gt(&operators::DIV.r_prec, l_prec),
BinaryOp::Log => prec_gt(&operators::POWER.r_prec, l_prec),
BinaryOp::Concat => false,
}, // (a + 2)b needs parens
AST::UnaryExpr(_, _) => false, // -ab doesn't need one, but future might
AST::Function(_, _) => false, // sin(x)a is fine
},
match rchild {
AST::Sym(_) => false, // a + b is fine
AST::Number(_) => false, // 2 + 3 is fine
// for binary operations: if the corresponding
// precedence is higher than the precedence of
// this operator, then parenthese are needed.
// For example, 2 ^ 3 + 1 ^ 2 is fine, but
// (2 + 3) ^ (1 + 2) needs parentheses.
// None is treated as never needing parentheses:
// 2 ^ ±3 is unambiguous, if weird
AST::BinaryExpr(bop, _, _) => match bop {
BinaryOp::Generic(SymbolBinaryOp {
op: l_op,
fixity: _,
}) => prec_gt(&l_op.l_prec, r_prec),
BinaryOp::Power => prec_gt(&operators::POWER.l_prec, r_prec),
BinaryOp::Frac => prec_gt(&operators::DIV.l_prec, r_prec),
BinaryOp::Log => prec_gt(&operators::POWER.l_prec, r_prec),
BinaryOp::Concat => false,
}, // (a + 2)b needs parens
AST::UnaryExpr(_, _) => false, // -ab doesn't need one, but future might
AST::Function(_, _) => false, // sin(x)a is fine
},
),
// defer others to the normal symbol versions
// LaTeX or other formats with special syntax can ignore as needed
BinaryOp::Power => need_parens(
&BinaryOp::Generic(SymbolBinaryOp {
op: operators::POWER.to_owned(),
fixity: crate::ast::Fixity::Infix,
}),
lchild,
rchild,
),
BinaryOp::Frac => need_parens(
&BinaryOp::Generic(SymbolBinaryOp {
op: operators::DIV.to_owned(),
fixity: crate::ast::Fixity::Infix,
}),
lchild,
rchild,
),
// treat log the same as exp
BinaryOp::Log => need_parens(
&BinaryOp::Generic(SymbolBinaryOp {
op: operators::POWER.to_owned(),
fixity: crate::ast::Fixity::Infix,
}),
lchild,
rchild,
),
BinaryOp::Concat => (
match lchild {
AST::Sym(_) => false, // ab is fine
AST::Number(_) => false, // 2a is fine
AST::BinaryExpr(_, _, _) => true, // (a + 2)b needs parens
AST::UnaryExpr(_, _) => false, // -ab doesn't need one, but future might
AST::Function(_, _) => false, // sin(x)a is fine
},
match rchild {
AST::Sym(_) => false, // ab is fine
AST::Number(_) => false, // a2 is fine, if weird
AST::BinaryExpr(_, _, _) => true, // b(2 + a) needs parens
AST::UnaryExpr(_, _) => true, // b(-a) needs parens
AST::Function(_, _) => false, // a sin(x) needs no parens
},
),
}
}
#[cfg(test)]
mod tests {
use crate::{
ast::Fixity,
ast::SymbolBinaryOp,
operators,
parsers::{ASTParser, AsciiParser},
};
use super::*;
#[test]
fn test_need_parens() {
let parser = AsciiParser::default();
// (2 - 3) + 1 / 2 is proper
assert_eq!(
need_parens(
&BinaryOp::Generic(SymbolBinaryOp {
op: operators::ADD.to_owned(),
fixity: Fixity::Infix
}),
&parser.parse(&"2 - 3").unwrap(),
&parser.parse(&"1 / 2").unwrap()
),
(true, false)
);
// 2 + 3 * 1 ^ 2 needs parens on the left
assert_eq!(
need_parens(
&BinaryOp::Generic(SymbolBinaryOp {
op: operators::MULT.to_owned(),
fixity: Fixity::Infix
}),
&parser.parse(&"2 + 3").unwrap(),
&parser.parse(&"1 ^ 2").unwrap()
),
(true, false)
);
}
}