Skip to main content

vibe/vocab/
shake.rs

1//! Tree-shake referenced terms and keep parent IRIs (P17.2).
2
3use crate::ast::{Expr, ExprKind, Item, Program, Stmt};
4use crate::span::Span;
5
6use super::{VocabChunk, VocabTerm};
7
8/// Keep referenced terms plus `rdfs:subClassOf` parents. Canonical IRIs unchanged.
9pub fn tree_shake(chunk: &VocabChunk, referenced: &[&str]) -> VocabChunk {
10    let mut keep: Vec<String> = Vec::new();
11    for r in referenced {
12        if let Some(iri) = chunk.expand(r).or_else(|| {
13            if r.contains("://") {
14                Some((*r).to_string())
15            } else {
16                None
17            }
18        }) {
19            keep.push(iri);
20        }
21        if let Some(t) = chunk.terms.iter().find(|t| t.label.as_deref() == Some(*r)) {
22            keep.push(t.iri.clone());
23        }
24    }
25    let mut out_iris = Vec::new();
26    while let Some(iri) = keep.pop() {
27        if out_iris.iter().any(|x| x == &iri) {
28            continue;
29        }
30        out_iris.push(iri.clone());
31        if let Some(term) = chunk.terms.iter().find(|t| t.iri == iri) {
32            for p in &term.parents {
33                keep.push(p.clone());
34            }
35        }
36    }
37    let terms: Vec<VocabTerm> = chunk
38        .terms
39        .iter()
40        .filter(|t| out_iris.iter().any(|i| i == &t.iri))
41        .cloned()
42        .collect();
43    VocabChunk {
44        prefixes: chunk.prefixes.clone(),
45        terms,
46        content_hash: chunk.content_hash,
47    }
48}
49
50/// Canonical IRIs used by `prefix:local` names in the program that this chunk owns.
51pub fn project_referenced_iris(program: &Program, chunk: &VocabChunk) -> Vec<String> {
52    let mut iris = Vec::new();
53    for (prefix, local, _) in collect_prefixed(program) {
54        if let Some(iri) = chunk.expand(&format!("{prefix}:{local}")) {
55            if !iris.contains(&iri) {
56                iris.push(iri);
57            }
58        }
59    }
60    iris
61}
62
63/// Prefixed names whose prefix is in a chunk but whose local name is not.
64pub fn unknown_prefixed(program: &Program, chunks: &[VocabChunk]) -> Vec<(String, String, Span)> {
65    let mut out = Vec::new();
66    for (prefix, local, span) in collect_prefixed(program) {
67        let Some(chunk) = chunks.iter().find(|c| c.prefixes.contains_key(&prefix)) else {
68            continue;
69        };
70        if !chunk.has_local(&prefix, &local) {
71            out.push((prefix, local, span));
72        }
73    }
74    out
75}
76
77fn collect_prefixed(program: &Program) -> Vec<(String, String, Span)> {
78    let mut out = Vec::new();
79    for item in &program.items {
80        match item {
81            Item::Function(f) => walk_block(&f.body, &mut out),
82            Item::Hook(h) => walk_block(&h.body, &mut out),
83            Item::Const(c) => walk_expr(&c.value, &mut out),
84            Item::Cell(c) => {
85                walk_expr(&c.expr, &mut out);
86                if let Some(w) = &c.when {
87                    walk_expr(w, &mut out);
88                }
89            }
90            Item::Law(l) => {
91                walk_expr(&l.condition, &mut out);
92                walk_expr(&l.consequence, &mut out);
93            }
94            Item::Bind(b) => {
95                walk_expr(&b.left, &mut out);
96                walk_expr(&b.right, &mut out);
97            }
98            Item::Present(p) => {
99                for prop in &p.properties {
100                    walk_expr(&prop.value, &mut out);
101                }
102            }
103            Item::Statement(s) => walk_stmt(s, &mut out),
104            Item::Enum(_) | Item::Field(_) | Item::Material(_) => {}
105        }
106    }
107    out
108}
109
110fn walk_block(block: &crate::ast::Block, out: &mut Vec<(String, String, Span)>) {
111    for s in &block.stmts {
112        walk_stmt(s, out);
113    }
114}
115
116fn walk_stmt(stmt: &Stmt, out: &mut Vec<(String, String, Span)>) {
117    match stmt {
118        Stmt::Let { value, .. } => {
119            if let Some(v) = value {
120                walk_expr(v, out);
121            }
122        }
123        Stmt::LetPat { value, .. } => walk_expr(value, out),
124        Stmt::Assign { target, value, .. } => {
125            walk_expr(target, out);
126            walk_expr(value, out);
127        }
128        Stmt::If {
129            cond,
130            then_block,
131            else_block,
132            ..
133        } => {
134            walk_expr(cond, out);
135            walk_block(then_block, out);
136            if let Some(e) = else_block {
137                walk_stmt(e, out);
138            }
139        }
140        Stmt::While { cond, body, .. }
141        | Stmt::For {
142            iter: cond, body, ..
143        } => {
144            walk_expr(cond, out);
145            walk_block(body, out);
146        }
147        Stmt::Match {
148            scrutinee, arms, ..
149        } => {
150            walk_expr(scrutinee, out);
151            for arm in arms {
152                match &arm.body {
153                    crate::ast::ArmBody::Block(b) => walk_block(b, out),
154                    crate::ast::ArmBody::Expr(e) => walk_expr(e, out),
155                }
156            }
157        }
158        Stmt::Return { value, .. } | Stmt::Yield { value, .. } => {
159            if let Some(v) = value {
160                walk_expr(v, out);
161            }
162        }
163        Stmt::Transaction { body, .. } => walk_block(body, out),
164        Stmt::Effect { expr, .. } | Stmt::Expr { expr, .. } => walk_expr(expr, out),
165        Stmt::Block(b) => walk_block(b, out),
166    }
167}
168
169fn walk_expr(expr: &Expr, out: &mut Vec<(String, String, Span)>) {
170    match &expr.kind {
171        ExprKind::Prefixed(p, l) => out.push((p.clone(), l.clone(), expr.span)),
172        ExprKind::Call { callee, args } => {
173            walk_expr(callee, out);
174            for a in args {
175                match a {
176                    crate::ast::Arg::Pos(e) => walk_expr(e, out),
177                    crate::ast::Arg::Named(n) => walk_expr(&n.value, out),
178                }
179            }
180        }
181        ExprKind::Member { recv, .. } => walk_expr(recv, out),
182        ExprKind::Binary { left, right, .. } => {
183            walk_expr(left, out);
184            walk_expr(right, out);
185        }
186        ExprKind::Unary { expr: inner, .. } => walk_expr(inner, out),
187        ExprKind::List(xs) => {
188            for x in xs {
189                walk_expr(x, out);
190            }
191        }
192        ExprKind::Record(fs) => {
193            for n in fs {
194                walk_expr(&n.value, out);
195            }
196        }
197        ExprKind::Await(inner) | ExprKind::Try(inner) => walk_expr(inner, out),
198        ExprKind::Triple {
199            subject,
200            predicate,
201            object,
202        }
203        | ExprKind::Reified {
204            subject,
205            predicate,
206            object,
207            ..
208        } => {
209            walk_expr(subject, out);
210            walk_expr(predicate, out);
211            walk_expr(object, out);
212        }
213        ExprKind::Interpolate(xs) => {
214            for x in xs {
215                walk_expr(x, out);
216            }
217        }
218        ExprKind::Pipe { left, right } => {
219            walk_expr(left, out);
220            walk_expr(right, out);
221        }
222        ExprKind::Index { recv, index } => {
223            walk_expr(recv, out);
224            walk_expr(index, out);
225        }
226        ExprKind::Lambda { body, .. } => walk_expr(body, out),
227        ExprKind::Tween { from, to, over, .. } => {
228            walk_expr(from, out);
229            walk_expr(to, out);
230            walk_expr(over, out);
231        }
232        ExprKind::IfElse {
233            cond,
234            then_value,
235            else_value,
236        } => {
237            walk_expr(cond, out);
238            walk_expr(then_value, out);
239            walk_expr(else_value, out);
240        }
241        ExprKind::ModalLogic { args, body, .. } => {
242            for a in args {
243                walk_expr(a, out);
244            }
245            if let Some(b) = body {
246                walk_expr(b, out);
247            }
248        }
249        _ => {}
250    }
251}