2019-04-11 15:23:14 -07:00
|
|
|
use crate::common::*;
|
2017-11-16 23:30:08 -08:00
|
|
|
|
2019-11-07 10:55:15 -08:00
|
|
|
/// An expression. Note that the Just language grammar has both an
|
|
|
|
/// `expression` production of additions (`a + b`) and values, and a
|
|
|
|
/// `value` production of all other value types (for example strings,
|
|
|
|
/// function calls, and parenthetical groups).
|
|
|
|
///
|
|
|
|
/// The parser parses both values and expressions into `Expression`s.
|
2017-11-16 23:30:08 -08:00
|
|
|
#[derive(PartialEq, Debug)]
|
2019-11-07 10:55:15 -08:00
|
|
|
pub(crate) enum Expression<'src> {
|
|
|
|
/// `contents`
|
2018-12-08 14:29:41 -08:00
|
|
|
Backtick {
|
2019-11-07 10:55:15 -08:00
|
|
|
contents: &'src str,
|
|
|
|
token: Token<'src>,
|
2018-12-08 14:29:41 -08:00
|
|
|
},
|
2019-11-07 10:55:15 -08:00
|
|
|
/// `name(arguments)`
|
2018-12-08 14:29:41 -08:00
|
|
|
Call {
|
2019-11-07 10:55:15 -08:00
|
|
|
function: Name<'src>,
|
|
|
|
arguments: Vec<Expression<'src>>,
|
2018-12-08 14:29:41 -08:00
|
|
|
},
|
2019-11-07 10:55:15 -08:00
|
|
|
/// `lhs + rhs`
|
2018-12-08 14:29:41 -08:00
|
|
|
Concatination {
|
2019-11-07 10:55:15 -08:00
|
|
|
lhs: Box<Expression<'src>>,
|
|
|
|
rhs: Box<Expression<'src>>,
|
2018-12-08 14:29:41 -08:00
|
|
|
},
|
2019-11-07 10:55:15 -08:00
|
|
|
/// `(contents)`
|
|
|
|
Group { contents: Box<Expression<'src>> },
|
|
|
|
/// `"string_literal"` or `'string_literal'`
|
|
|
|
StringLiteral {
|
|
|
|
string_literal: StringLiteral<'src>,
|
2019-04-11 23:58:08 -07:00
|
|
|
},
|
2019-11-07 10:55:15 -08:00
|
|
|
/// `variable`
|
|
|
|
Variable { name: Name<'src> },
|
2017-11-16 23:30:08 -08:00
|
|
|
}
|
|
|
|
|
2019-11-07 10:55:15 -08:00
|
|
|
impl<'src> Expression<'src> {
|
|
|
|
pub(crate) fn variables<'expression>(&'expression self) -> Variables<'expression, 'src> {
|
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
|
|
|
|
2019-11-07 10:55:15 -08:00
|
|
|
pub(crate) fn functions<'expression>(&'expression self) -> Functions<'expression, 'src> {
|
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
|
|
|
}
|
|
|
|
|
2019-11-07 10:55:15 -08:00
|
|
|
impl<'src> Display for Expression<'src> {
|
2019-04-11 15:23:14 -07:00
|
|
|
fn fmt(&self, f: &mut Formatter) -> Result<(), fmt::Error> {
|
2019-11-07 10:55:15 -08:00
|
|
|
match self {
|
|
|
|
Expression::Backtick { contents, .. } => write!(f, "`{}`", contents)?,
|
|
|
|
Expression::Concatination { lhs, rhs } => write!(f, "{} + {}", lhs, rhs)?,
|
|
|
|
Expression::StringLiteral { string_literal } => write!(f, "{}", string_literal)?,
|
|
|
|
Expression::Variable { name } => write!(f, "{}", name.lexeme())?,
|
2018-12-08 14:29:41 -08:00
|
|
|
Expression::Call {
|
2019-11-07 10:55:15 -08:00
|
|
|
function,
|
|
|
|
arguments,
|
2018-12-08 14:29:41 -08:00
|
|
|
} => {
|
2019-11-07 10:55:15 -08:00
|
|
|
write!(f, "{}(", function.lexeme())?;
|
2017-12-02 14:59:07 -08:00
|
|
|
for (i, argument) in arguments.iter().enumerate() {
|
|
|
|
if i > 0 {
|
|
|
|
write!(f, ", {}", argument)?;
|
|
|
|
} else {
|
|
|
|
write!(f, "{}", argument)?;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
write!(f, ")")?;
|
|
|
|
}
|
2019-11-07 10:55:15 -08:00
|
|
|
Expression::Group { contents } => write!(f, "({})", contents)?,
|
2017-11-16 23:30:08 -08:00
|
|
|
}
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
}
|