Merge pull request #110 from tsmt09/iter_read_write_vars

iter_(read/write)_variable_identifiers
This commit is contained in:
ISibboI 2022-07-22 11:05:48 +02:00 committed by GitHub
commit b8a90b065b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 53 additions and 1 deletions

View File

@ -99,6 +99,48 @@ impl Node {
}) })
} }
/// Returns an iterator over all read variable identifiers in this expression.
/// Each occurrence of a variable identifier is returned separately.
///
/// # Examples
///
/// ```rust
/// use evalexpr::*;
///
/// let tree = build_operator_tree("d = a + f(b + c)").unwrap(); // Do proper error handling here
/// let mut iter = tree.iter_read_variable_identifiers();
/// assert_eq!(iter.next(), Some("a"));
/// assert_eq!(iter.next(), Some("b"));
/// assert_eq!(iter.next(), Some("c"));
/// assert_eq!(iter.next(), None);
/// ```
pub fn iter_read_variable_identifiers(&self) -> impl Iterator<Item = &str> {
self.iter().filter_map(|node| match node.operator() {
Operator::VariableIdentifierRead { identifier } => Some(identifier.as_str()),
_ => None,
})
}
/// Returns an iterator over all write variable identifiers in this expression.
/// Each occurrence of a variable identifier is returned separately.
///
/// # Examples
///
/// ```rust
/// use evalexpr::*;
///
/// let tree = build_operator_tree("d = a + f(b + c)").unwrap(); // Do proper error handling here
/// let mut iter = tree.iter_write_variable_identifiers();
/// assert_eq!(iter.next(), Some("d"));
/// assert_eq!(iter.next(), None);
/// ```
pub fn iter_write_variable_identifiers(&self) -> impl Iterator<Item = &str> {
self.iter().filter_map(|node| match node.operator() {
Operator::VariableIdentifierWrite { identifier } => Some(identifier.as_str()),
_ => None,
})
}
/// Returns an iterator over all function identifiers in this expression. /// Returns an iterator over all function identifiers in this expression.
/// Each occurrence of a function identifier is returned separately. /// Each occurrence of a function identifier is returned separately.
/// ///

View File

@ -1616,16 +1616,26 @@ fn test_error_constructors() {
#[test] #[test]
fn test_iterators() { fn test_iterators() {
let tree = build_operator_tree("5 + 3 + fun(4) + var").unwrap(); let tree = build_operator_tree("writevar = 5 + 3 + fun(4) + var").unwrap();
let mut iter = tree.iter_identifiers(); let mut iter = tree.iter_identifiers();
assert_eq!(iter.next(), Some("writevar"));
assert_eq!(iter.next(), Some("fun")); assert_eq!(iter.next(), Some("fun"));
assert_eq!(iter.next(), Some("var")); assert_eq!(iter.next(), Some("var"));
assert_eq!(iter.next(), None); assert_eq!(iter.next(), None);
let mut iter = tree.iter_variable_identifiers(); let mut iter = tree.iter_variable_identifiers();
assert_eq!(iter.next(), Some("writevar"));
assert_eq!(iter.next(), Some("var")); assert_eq!(iter.next(), Some("var"));
assert_eq!(iter.next(), None); assert_eq!(iter.next(), None);
let mut iter = tree.iter_read_variable_identifiers();
assert_eq!(iter.next(), Some("var"));
assert_eq!(iter.next(), None);
let mut iter = tree.iter_write_variable_identifiers();
assert_eq!(iter.next(), Some("writevar"));
assert_eq!(iter.next(), None);
let mut iter = tree.iter_function_identifiers(); let mut iter = tree.iter_function_identifiers();
assert_eq!(iter.next(), Some("fun")); assert_eq!(iter.next(), Some("fun"));
assert_eq!(iter.next(), None); assert_eq!(iter.next(), None);