plotnik_compiler/analyze/
refs.rs

1//! Utilities for working with definition references in expressions.
2
3use indexmap::IndexSet;
4
5use crate::parser::ast::{self, Expr};
6
7/// Iterate over all Ref nodes in an expression tree.
8pub fn ref_nodes(expr: &Expr) -> impl Iterator<Item = ast::Ref> + '_ {
9    expr.as_cst().descendants().filter_map(ast::Ref::cast)
10}
11
12/// Collect all reference names as owned strings.
13pub fn ref_names(expr: &Expr) -> IndexSet<String> {
14    ref_nodes(expr)
15        .filter_map(|r| r.name())
16        .map(|tok| tok.text().to_string())
17        .collect()
18}
19
20/// Check if expression contains a reference to the given name.
21pub fn contains_ref(expr: &Expr, name: &str) -> bool {
22    ref_nodes(expr)
23        .filter_map(|r| r.name())
24        .any(|tok| tok.text() == name)
25}