2019-04-15 22:40:02 -07:00
|
|
|
use crate::common::*;
|
|
|
|
|
2019-09-21 15:35:03 -07:00
|
|
|
pub(crate) struct Variables<'a> {
|
2019-04-15 22:40:02 -07:00
|
|
|
stack: Vec<&'a Expression<'a>>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a> Variables<'a> {
|
2019-09-21 15:35:03 -07:00
|
|
|
pub(crate) fn new(root: &'a Expression<'a>) -> Variables<'a> {
|
2019-04-15 22:40:02 -07:00
|
|
|
Variables { stack: vec![root] }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a> Iterator for Variables<'a> {
|
|
|
|
type Item = &'a Token<'a>;
|
|
|
|
|
|
|
|
fn next(&mut self) -> Option<&'a Token<'a>> {
|
|
|
|
match self.stack.pop() {
|
|
|
|
None
|
|
|
|
| Some(Expression::String { .. })
|
|
|
|
| Some(Expression::Backtick { .. })
|
|
|
|
| Some(Expression::Call { .. }) => None,
|
|
|
|
Some(Expression::Variable { token, .. }) => Some(token),
|
|
|
|
Some(Expression::Concatination { lhs, rhs }) => {
|
|
|
|
self.stack.push(lhs);
|
|
|
|
self.stack.push(rhs);
|
|
|
|
self.next()
|
|
|
|
}
|
|
|
|
Some(Expression::Group { expression }) => {
|
|
|
|
self.stack.push(expression);
|
|
|
|
self.next()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|