2019-04-11 15:23:14 -07:00
|
|
|
use crate::common::*;
|
2017-11-16 23:30:08 -08:00
|
|
|
|
|
|
|
#[derive(PartialEq, Debug)]
|
|
|
|
pub enum Expression<'a> {
|
2018-12-08 14:29:41 -08:00
|
|
|
Backtick {
|
|
|
|
raw: &'a str,
|
|
|
|
token: Token<'a>,
|
|
|
|
},
|
|
|
|
Call {
|
|
|
|
name: &'a str,
|
|
|
|
token: Token<'a>,
|
|
|
|
arguments: Vec<Expression<'a>>,
|
|
|
|
},
|
|
|
|
Concatination {
|
|
|
|
lhs: Box<Expression<'a>>,
|
|
|
|
rhs: Box<Expression<'a>>,
|
|
|
|
},
|
|
|
|
String {
|
2019-04-15 22:40:02 -07:00
|
|
|
cooked_string: StringLiteral<'a>,
|
2018-12-08 14:29:41 -08:00
|
|
|
},
|
|
|
|
Variable {
|
|
|
|
name: &'a str,
|
|
|
|
token: Token<'a>,
|
|
|
|
},
|
2019-04-11 23:58:08 -07:00
|
|
|
Group {
|
|
|
|
expression: Box<Expression<'a>>,
|
|
|
|
},
|
2017-11-16 23:30:08 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a> Expression<'a> {
|
|
|
|
pub fn variables(&'a self) -> Variables<'a> {
|
2019-04-15 22:40:02 -07:00
|
|
|
Variables::new(self)
|
2017-11-16 23:30:08 -08:00
|
|
|
}
|
2017-12-02 05:37:10 -08:00
|
|
|
|
|
|
|
pub fn functions(&'a self) -> Functions<'a> {
|
2019-04-15 22:40:02 -07:00
|
|
|
Functions::new(self)
|
2017-12-02 05:37:10 -08:00
|
|
|
}
|
2017-11-16 23:30:08 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a> Display for Expression<'a> {
|
2019-04-11 15:23:14 -07:00
|
|
|
fn fmt(&self, f: &mut Formatter) -> Result<(), fmt::Error> {
|
2017-11-16 23:30:08 -08:00
|
|
|
match *self {
|
2018-12-08 14:29:41 -08:00
|
|
|
Expression::Backtick { raw, .. } => write!(f, "`{}`", raw)?,
|
|
|
|
Expression::Concatination { ref lhs, ref rhs } => write!(f, "{} + {}", lhs, rhs)?,
|
2019-04-11 23:58:08 -07:00
|
|
|
Expression::String { ref cooked_string } => write!(f, "{}", cooked_string)?,
|
2018-12-08 14:29:41 -08:00
|
|
|
Expression::Variable { name, .. } => write!(f, "{}", name)?,
|
|
|
|
Expression::Call {
|
|
|
|
name,
|
|
|
|
ref arguments,
|
|
|
|
..
|
|
|
|
} => {
|
2017-12-02 14:59:07 -08:00
|
|
|
write!(f, "{}(", name)?;
|
|
|
|
for (i, argument) in arguments.iter().enumerate() {
|
|
|
|
if i > 0 {
|
|
|
|
write!(f, ", {}", argument)?;
|
|
|
|
} else {
|
|
|
|
write!(f, "{}", argument)?;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
write!(f, ")")?;
|
|
|
|
}
|
2019-04-11 23:58:08 -07:00
|
|
|
Expression::Group { ref expression } => write!(f, "({})", expression)?,
|
2017-11-16 23:30:08 -08:00
|
|
|
}
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
}
|