#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
#[cfg_attr(feature = "serde", serde(deny_unknown_fields))]
pub enum BinOp {
LogOr,
LogAnd,
Eq,
Ne,
Lt,
Gt,
Le,
Ge,
}
impl BinOp {
#[must_use]
pub const fn as_str(self) -> &'static str {
match self {
BinOp::LogOr => "||",
BinOp::LogAnd => "&&",
BinOp::Eq => "==",
BinOp::Ne => "!=",
BinOp::Lt => "<",
BinOp::Gt => ">",
BinOp::Le => "<=",
BinOp::Ge => ">=",
}
}
}
#[derive(Debug, Clone, PartialEq, Eq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
#[cfg_attr(feature = "serde", serde(deny_unknown_fields))]
#[non_exhaustive]
pub enum ExprAst<T = ()> {
Integer {
value: i64,
data: T,
},
String {
value: String,
data: T,
},
Macro {
text: String,
data: T,
},
Identifier {
name: String,
data: T,
},
Paren {
inner: Box<ExprAst<T>>,
data: T,
},
Not {
inner: Box<ExprAst<T>>,
data: T,
},
Binary {
kind: BinOp,
lhs: Box<ExprAst<T>>,
rhs: Box<ExprAst<T>>,
data: T,
},
}
impl<T> ExprAst<T> {
pub fn data(&self) -> &T {
match self {
ExprAst::Integer { data, .. }
| ExprAst::String { data, .. }
| ExprAst::Macro { data, .. }
| ExprAst::Identifier { data, .. }
| ExprAst::Paren { data, .. }
| ExprAst::Not { data, .. }
| ExprAst::Binary { data, .. } => data,
}
}
#[must_use]
pub fn peel_parens(&self) -> &Self {
let mut current = self;
while let ExprAst::Paren { inner, .. } = current {
current = inner;
}
current
}
}