Skip to main content

logicaffeine_language/
visitor.rs

1//! AST visitor pattern for traversing logical expressions.
2//!
3//! This module provides a visitor trait for walking the AST without mutation.
4//! It follows the standard visitor pattern with `walk_*` functions that handle
5//! recursive traversal and `visit_*` methods that can be overridden.
6//!
7//! # Usage
8//!
9//! Implement [`Visitor`] and override the `visit_*` methods you need:
10//!
11//! ```no_run
12//! # use logicaffeine_base::Symbol;
13//! # use logicaffeine_language::ast::Term;
14//! # use logicaffeine_language::visitor::{Visitor, walk_term};
15//! struct VariableCollector {
16//!     vars: Vec<Symbol>,
17//! }
18//!
19//! impl<'a> Visitor<'a> for VariableCollector {
20//!     fn visit_term(&mut self, term: &'a Term<'a>) {
21//!         if let Term::Variable(sym) = term {
22//!             self.vars.push(*sym);
23//!         }
24//!         walk_term(self, term);
25//!     }
26//! }
27//! ```
28
29use crate::ast::{LogicExpr, NounPhrase, Term};
30
31/// Trait for visiting AST nodes.
32pub trait Visitor<'a>: Sized {
33    fn visit_expr(&mut self, expr: &'a LogicExpr<'a>) {
34        walk_expr(self, expr);
35    }
36
37    fn visit_term(&mut self, term: &'a Term<'a>) {
38        walk_term(self, term);
39    }
40
41    fn visit_np(&mut self, np: &'a NounPhrase<'a>) {
42        walk_np(self, np);
43    }
44}
45
46pub fn walk_expr<'a, V: Visitor<'a>>(v: &mut V, expr: &'a LogicExpr<'a>) {
47    match expr {
48        LogicExpr::Predicate { args, .. } => {
49            for arg in *args {
50                v.visit_term(arg);
51            }
52        }
53
54        LogicExpr::Identity { left, right } => {
55            v.visit_term(left);
56            v.visit_term(right);
57        }
58
59        LogicExpr::Metaphor { tenor, vehicle } => {
60            v.visit_term(tenor);
61            v.visit_term(vehicle);
62        }
63
64        LogicExpr::Quantifier { body, .. } => {
65            v.visit_expr(body);
66        }
67
68        LogicExpr::Categorical(data) => {
69            v.visit_np(&data.subject);
70            v.visit_np(&data.predicate);
71        }
72
73        LogicExpr::Relation(data) => {
74            v.visit_np(&data.subject);
75            v.visit_np(&data.object);
76        }
77
78        LogicExpr::Modal { operand, .. } => {
79            v.visit_expr(operand);
80        }
81
82        LogicExpr::Temporal { body, .. } => {
83            v.visit_expr(body);
84        }
85
86        LogicExpr::TemporalBinary { left, right, .. } => {
87            v.visit_expr(left);
88            v.visit_expr(right);
89        }
90
91        LogicExpr::Aspectual { body, .. } => {
92            v.visit_expr(body);
93        }
94
95        LogicExpr::Voice { body, .. } => {
96            v.visit_expr(body);
97        }
98
99        LogicExpr::BinaryOp { left, right, .. } => {
100            v.visit_expr(left);
101            v.visit_expr(right);
102        }
103
104        LogicExpr::UnaryOp { operand, .. } => {
105            v.visit_expr(operand);
106        }
107
108        LogicExpr::Question { body, .. } => {
109            v.visit_expr(body);
110        }
111
112        LogicExpr::YesNoQuestion { body } => {
113            v.visit_expr(body);
114        }
115
116        LogicExpr::Atom(_) => {}
117
118        LogicExpr::Lambda { body, .. } => {
119            v.visit_expr(body);
120        }
121
122        LogicExpr::App { function, argument } => {
123            v.visit_expr(function);
124            v.visit_expr(argument);
125        }
126
127        LogicExpr::Intensional { content, .. } => {
128            v.visit_expr(content);
129        }
130
131        LogicExpr::Event { predicate, .. } => {
132            v.visit_expr(predicate);
133        }
134
135        LogicExpr::NeoEvent(data) => {
136            for (_, term) in data.roles.iter() {
137                v.visit_term(term);
138            }
139        }
140
141        LogicExpr::Exclamative { body, .. } => {
142            v.visit_expr(body);
143        }
144        LogicExpr::Optative { wish } => {
145            v.visit_expr(wish);
146        }
147        LogicExpr::Implicature {
148            assertion,
149            implicature,
150        } => {
151            v.visit_expr(assertion);
152            v.visit_expr(implicature);
153        }
154        LogicExpr::Imperative { action } => {
155            v.visit_expr(action);
156        }
157
158        LogicExpr::SpeechAct { content, .. } => {
159            v.visit_expr(content);
160        }
161
162        LogicExpr::Counterfactual {
163            antecedent,
164            consequent,
165        } => {
166            v.visit_expr(antecedent);
167            v.visit_expr(consequent);
168        }
169
170        LogicExpr::Causal { effect, cause } => {
171            v.visit_expr(cause);
172            v.visit_expr(effect);
173        }
174        LogicExpr::Concessive { main, concession } => {
175            v.visit_expr(main);
176            v.visit_expr(concession);
177        }
178
179        LogicExpr::Comparative {
180            subject, object, ..
181        } => {
182            v.visit_term(subject);
183            v.visit_term(object);
184        }
185
186        LogicExpr::Superlative { subject, .. } => {
187            v.visit_term(subject);
188        }
189
190        LogicExpr::Scopal { body, .. } => {
191            v.visit_expr(body);
192        }
193
194        LogicExpr::Control {
195            subject,
196            object,
197            infinitive,
198            ..
199        } => {
200            v.visit_term(subject);
201            if let Some(obj) = object {
202                v.visit_term(obj);
203            }
204            v.visit_expr(infinitive);
205        }
206
207        LogicExpr::Presupposition {
208            assertion,
209            presupposition,
210        } => {
211            v.visit_expr(assertion);
212            v.visit_expr(presupposition);
213        }
214
215        LogicExpr::Focus { focused, scope, .. } => {
216            v.visit_term(focused);
217            v.visit_expr(scope);
218        }
219
220        LogicExpr::TemporalAnchor { body, .. } => {
221            v.visit_expr(body);
222        }
223
224        LogicExpr::Distributive { predicate } => {
225            v.visit_expr(predicate);
226        }
227
228        LogicExpr::GroupQuantifier {
229            restriction, body, ..
230        } => {
231            v.visit_expr(restriction);
232            v.visit_expr(body);
233        }
234    }
235}
236
237pub fn walk_term<'a, V: Visitor<'a>>(v: &mut V, term: &'a Term<'a>) {
238    match term {
239        Term::Constant(_)
240        | Term::Variable(_)
241        | Term::Sigma(_)
242        | Term::Intension(_)
243        | Term::Kind(_)
244        | Term::Value { .. } => {}
245
246        Term::Function(_, args) => {
247            for arg in *args {
248                v.visit_term(arg);
249            }
250        }
251
252        Term::Group(members) => {
253            for m in *members {
254                v.visit_term(m);
255            }
256        }
257
258        Term::Possessed { possessor, .. } => {
259            v.visit_term(possessor);
260        }
261
262        Term::Proposition(expr) => {
263            v.visit_expr(expr);
264        }
265    }
266}
267
268pub fn walk_np<'a, V: Visitor<'a>>(v: &mut V, np: &'a NounPhrase<'a>) {
269    if let Some(poss) = np.possessor {
270        v.visit_np(poss);
271    }
272    for pp in np.pps.iter() {
273        v.visit_expr(pp);
274    }
275}
276
277#[cfg(test)]
278#[path = "../../logicaffeine_tests/unit/logicaffeine_language/visitor/tests.rs"]
279mod tests;