use derive_more::From;
#[cfg(doc)]
use crate::parser::*;
#[derive(Debug, Clone, PartialEq)]
pub enum Expression {
Unary {
op: UnaryOperator,
arg: Box<Expression>,
},
Binary {
op: BinaryOperator,
arg1: Box<Expression>,
arg2: Box<Expression>,
},
Relation {
op: RelationOperator,
lhs: Box<Expression>,
rhs: Box<Expression>,
},
Literal(Literal),
QualifiableFactor {
factor: QualifiableFactor,
qualifiers: Vec<Qualifier>,
},
EntityConstructor {
name: String,
values: Vec<Expression>,
},
Interval {
op_low: IntervalOperator,
op_high: IntervalOperator,
high: Box<Expression>,
low: Box<Expression>,
item: Box<Expression>,
},
EnumerationReference {
ty: Option<String>,
enum_ref: String,
},
AggregateInitializer {
elements: Vec<Element>,
},
Query {
variable: String,
source: Box<Expression>,
expr: Box<Expression>,
},
}
macro_rules! impl_relation_op_expression {
($f:ident, $op:path) => {
pub fn $f(self, other: Self) -> Self {
Expression::Relation {
op: $op,
lhs: Box::new(self),
rhs: Box::new(other),
}
}
};
}
macro_rules! impl_binary_op_expression {
($f:ident, $op:path) => {
pub fn $f(self, other: Self) -> Self {
Expression::Binary {
op: $op,
arg1: Box::new(self),
arg2: Box::new(other),
}
}
};
}
impl Expression {
pub fn self_() -> Self {
Self::self_qualified(Vec::new())
}
pub fn self_qualified(qualifiers: Vec<Qualifier>) -> Self {
Expression::QualifiableFactor {
factor: QualifiableFactor::BuiltInConstant(BuiltInConstant::Self_),
qualifiers,
}
}
pub fn indeterminate() -> Self {
Expression::QualifiableFactor {
factor: QualifiableFactor::BuiltInConstant(BuiltInConstant::Indeterminate),
qualifiers: Vec::new(),
}
}
pub fn real(value: f64) -> Self {
Expression::Literal(Literal::Real(value))
}
impl_relation_op_expression!(leq, RelationOperator::Leq);
impl_relation_op_expression!(geq, RelationOperator::Geq);
impl_relation_op_expression!(lt, RelationOperator::Lt);
impl_relation_op_expression!(gt, RelationOperator::Gt);
impl_relation_op_expression!(eq, RelationOperator::Equal);
impl_relation_op_expression!(neq, RelationOperator::NotEqual);
impl_relation_op_expression!(in_, RelationOperator::In);
impl_relation_op_expression!(like, RelationOperator::Like);
impl_binary_op_expression!(and, BinaryOperator::And);
impl_binary_op_expression!(or, BinaryOperator::Or);
impl_binary_op_expression!(xor, BinaryOperator::Xor);
}
macro_rules! impl_binary_op_expression {
($f:ident, $ops:path, $op:path) => {
impl $ops for Expression {
type Output = Self;
fn $f(self, other: Expression) -> Self {
Expression::Binary {
op: $op,
arg1: Box::new(self),
arg2: Box::new(other),
}
}
}
};
}
impl_binary_op_expression!(add, std::ops::Add, BinaryOperator::Add);
impl_binary_op_expression!(sub, std::ops::Sub, BinaryOperator::Sub);
impl_binary_op_expression!(mul, std::ops::Mul, BinaryOperator::Mul);
#[derive(Debug, Clone, PartialEq)]
pub enum QualifiableFactor {
Reference(String),
BuiltInConstant(BuiltInConstant),
FunctionCall {
name: FunctionCallName,
args: Vec<Expression>,
},
}
#[derive(Debug, Clone, PartialEq)]
pub enum FunctionCallName {
BuiltInFunction(BuiltInFunction),
Reference(String),
}
#[allow(non_camel_case_types, clippy::upper_case_acronyms)] #[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub enum BuiltInFunction {
ABS,
ACOS,
ASIN,
ATAN,
BLENGTH,
COS,
EXISTS,
EXP,
FORMAT,
HIBOUND,
HIINDEX,
LENGTH,
LOBOUND,
LOINDEX,
LOG,
LOG2,
LOG10,
NVL,
ODD,
ROLESOF,
SIN,
SIZEOF,
SQRT,
TAN,
TYPEOF,
USEDIN,
VALUE,
VALUE_IN,
VALUE_UNIQUE,
}
#[derive(Debug, Clone, PartialEq)]
pub enum Qualifier {
Attribute(String),
Group(String),
Index(Expression),
Range { begin: Expression, end: Expression },
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum BuiltInConstant {
Napier,
Pi,
Self_,
Indeterminate,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum RelationOperator {
Equal,
NotEqual,
Lt,
Gt,
Leq,
Geq,
InstanceEqual,
InstanceNotEqual,
In,
Like,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum UnaryOperator {
Plus,
Minus,
Not,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum BinaryOperator {
Mul,
RealDiv,
IntegerDiv,
Mod,
And,
ComplexEntityInstanceConstruction,
Add,
Sub,
Or,
Xor,
Power,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum IntervalOperator {
LessThan,
LessThanEqual,
}
#[derive(Debug, Clone, PartialEq)]
pub struct Element {
pub expr: Expression,
pub repetition: Option<Expression>,
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum Logical {
False,
True,
Unknown,
}
#[derive(Debug, Clone, PartialEq, From)]
pub enum Literal {
Real(f64),
String(String),
Logial(Logical),
}