2019-04-15 22:40:02 -07:00
|
|
|
use crate::common::*;
|
|
|
|
|
2019-11-07 10:55:15 -08:00
|
|
|
pub(crate) struct Variables<'expression, 'src> {
|
|
|
|
stack: Vec<&'expression Expression<'src>>,
|
2019-04-15 22:40:02 -07:00
|
|
|
}
|
|
|
|
|
2019-11-07 10:55:15 -08:00
|
|
|
impl<'expression, 'src> Variables<'expression, 'src> {
|
|
|
|
pub(crate) fn new(root: &'expression Expression<'src>) -> Variables<'expression, 'src> {
|
2019-04-15 22:40:02 -07:00
|
|
|
Variables { stack: vec![root] }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-07 10:55:15 -08:00
|
|
|
impl<'expression, 'src> Iterator for Variables<'expression, 'src> {
|
|
|
|
type Item = Token<'src>;
|
2019-04-15 22:40:02 -07:00
|
|
|
|
2019-11-07 10:55:15 -08:00
|
|
|
fn next(&mut self) -> Option<Token<'src>> {
|
2019-04-15 22:40:02 -07:00
|
|
|
match self.stack.pop() {
|
|
|
|
None
|
2019-11-07 10:55:15 -08:00
|
|
|
| Some(Expression::StringLiteral { .. })
|
2019-04-15 22:40:02 -07:00
|
|
|
| Some(Expression::Backtick { .. })
|
|
|
|
| Some(Expression::Call { .. }) => None,
|
2020-10-26 18:16:42 -07:00
|
|
|
Some(Expression::Conditional {
|
|
|
|
lhs,
|
|
|
|
rhs,
|
|
|
|
then,
|
|
|
|
otherwise,
|
|
|
|
..
|
|
|
|
}) => {
|
|
|
|
self.stack.push(lhs);
|
|
|
|
self.stack.push(rhs);
|
|
|
|
self.stack.push(then);
|
|
|
|
self.stack.push(otherwise);
|
|
|
|
self.next()
|
|
|
|
},
|
2019-11-07 10:55:15 -08:00
|
|
|
Some(Expression::Variable { name, .. }) => Some(name.token()),
|
2019-04-15 22:40:02 -07:00
|
|
|
Some(Expression::Concatination { lhs, rhs }) => {
|
|
|
|
self.stack.push(lhs);
|
|
|
|
self.stack.push(rhs);
|
|
|
|
self.next()
|
2020-02-10 20:07:06 -08:00
|
|
|
},
|
2019-11-07 10:55:15 -08:00
|
|
|
Some(Expression::Group { contents }) => {
|
|
|
|
self.stack.push(contents);
|
2019-04-15 22:40:02 -07:00
|
|
|
self.next()
|
2020-02-10 20:07:06 -08:00
|
|
|
},
|
2019-04-15 22:40:02 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|