Skip to main content

harn_rules/
pattern.rs

1//! The pattern compiler: a code snippet with metavariable holes → a
2//! tree-sitter query.
3//!
4//! This is the atomic-tier `pattern` form. The idea (from ast-grep) is to
5//! let rule authors write a *snippet of real code* with `$VAR` holes
6//! instead of hand-authoring a tree-sitter S-expression query:
7//!
8//! ```text
9//!   $SRC?.$KEY ?? $DEFAULT
10//! ```
11//!
12//! compiles to
13//!
14//! ```text
15//!   ((binary_expression
16//!      left: (member_expression object: (_) @SRC (optional_chain) property: (_) @KEY)
17//!      "??"
18//!      right: (_) @DEFAULT) @__match)
19//! ```
20//!
21//! ## How it works
22//!
23//! 1. Each `$VAR` is replaced with a unique placeholder identifier so the
24//!    snippet parses as ordinary code in the target grammar.
25//! 2. We parse the substituted snippet — bare, then in a per-language
26//!    wrapper context (e.g. a function body) when the fragment is not a
27//!    valid compilation unit — and locate the snippet's own subtree by its
28//!    byte range in the parsed source.
29//! 3. We walk that subtree and mirror it into a query: every named child is
30//!    emitted with its field name, every anonymous token (operators,
31//!    keywords, punctuation) is emitted as a quoted literal so the structure
32//!    is matched precisely, and every placeholder becomes a `(_) @VAR`
33//!    wildcard capture.
34//! 4. Repeated metavariables unify: the second and later occurrences get
35//!    helper captures plus an `(#eq? …)` predicate so `$X … $X` only matches
36//!    when both holes carry identical text.
37//!
38//! Variadic `$$$` holes are not yet supported (tracked for the relational
39//! tier, #2833); they compile to a clear error.
40
41use std::collections::HashMap;
42
43use harn_hostlib::ast::{api, Language};
44use tree_sitter::Node;
45
46/// The capture name bound to the whole matched pattern, used for range
47/// extraction. Chosen to not collide with a user metavar (which are
48/// uppercase by convention and never start with `__`).
49pub const ROOT_CAPTURE: &str = "__match";
50
51/// Placeholder identifier stem substituted for each `$VAR`. Lowercase +
52/// `__` prefix keeps it a valid identifier across grammars and unlikely to
53/// collide with real snippet text.
54const PLACEHOLDER_STEM: &str = "__harn_hole_";
55
56/// A snippet pattern compiled to a tree-sitter query string.
57#[derive(Debug, Clone)]
58pub struct CompiledPattern {
59    /// The generated S-expression query. Always binds the pattern root to
60    /// `@__match` ([`ROOT_CAPTURE`]).
61    pub query: String,
62    /// Metavar names in first-appearance order (without the leading `$`).
63    pub metavars: Vec<String>,
64}
65
66/// Compile a `pattern` snippet for `language` into a tree-sitter query.
67///
68/// A snippet is often a *fragment* (`a + a`, `foo(bar)`) that is not a
69/// valid compilation unit on its own. We therefore try the snippet bare
70/// first (works for expression-statement languages like TS/JS/Python),
71/// then in a small set of per-language wrapper contexts (e.g. a function
72/// body for Rust/Go), and locate the snippet's own subtree by byte range.
73pub fn compile_pattern(snippet: &str, language: Language) -> Result<CompiledPattern, String> {
74    let sub = substitute(snippet)?;
75    let mut last_err: Option<String> = None;
76
77    for (prefix, suffix) in contexts(language) {
78        let wrapped = format!("{prefix}{}{suffix}", sub.text);
79        let tree = api::parse_tree(&wrapped, language).map_err(|err| err.to_string())?;
80        let root = tree.root_node();
81        if root.has_error() {
82            last_err = Some(format!(
83                "snippet did not parse cleanly in `{}`: `{snippet}`",
84                language.name()
85            ));
86            continue;
87        }
88
89        // The snippet occupies `[start, end)` inside the wrapped source; the
90        // deepest node spanning that range is its own subtree (no need to
91        // descend wrappers — and no risk of over-descending a single-child
92        // node like a unary expression).
93        let start = prefix.len();
94        let end = start + sub.text.len();
95        let Some(pattern_root) = root.descendant_for_byte_range(start, end.saturating_sub(1))
96        else {
97            last_err = Some(format!(
98                "could not locate snippet subtree in `{}`",
99                language.name()
100            ));
101            continue;
102        };
103
104        let bytes = wrapped.as_bytes();
105        let mut builder = QueryBuilder::new(bytes, &sub.placeholder_to_metavar);
106        let body = builder.build(pattern_root);
107        let predicates = builder.predicates();
108        let query = if predicates.is_empty() {
109            format!("({body} @{ROOT_CAPTURE})")
110        } else {
111            format!("({body} @{ROOT_CAPTURE} {predicates})")
112        };
113        return Ok(CompiledPattern {
114            query,
115            metavars: sub.metavar_order,
116        });
117    }
118
119    Err(last_err.unwrap_or_else(|| format!("snippet did not parse in `{}`", language.name())))
120}
121
122/// Candidate parse contexts for a snippet, tried in order. The bare context
123/// (`""`, `""`) comes first; item-required languages add a wrapper that
124/// makes an expression/statement fragment parse. Languages whose top level
125/// already accepts expression statements (TS/JS/Python/Ruby/…) only need
126/// the bare context.
127fn contexts(language: Language) -> Vec<(&'static str, &'static str)> {
128    let mut v = vec![("", "")];
129    let wrapper = match language {
130        Language::Rust => Some(("fn __harn_probe() { ", " }")),
131        Language::Go => Some(("package p\nfunc __harn_probe() { ", " }")),
132        Language::Java | Language::CSharp => {
133            Some(("class __HarnProbe { void __harn_probe() { ", " } }"))
134        }
135        Language::C | Language::Cpp => Some(("void __harn_probe() { ", " }")),
136        Language::Kotlin => Some(("fun __harn_probe() { ", " }")),
137        Language::Swift => Some(("func __harn_probe() { ", " }")),
138        Language::Scala => Some(("def __harn_probe() = { ", " }")),
139        _ => None,
140    };
141    v.extend(wrapper);
142    v
143}
144
145// ---------------------------------------------------------------------------
146// Step 1: metavar substitution
147// ---------------------------------------------------------------------------
148
149struct Substituted {
150    /// Snippet with `$VAR` replaced by placeholder identifiers.
151    text: String,
152    /// placeholder identifier → metavar name.
153    placeholder_to_metavar: HashMap<String, String>,
154    /// Metavar names in first-appearance order.
155    metavar_order: Vec<String>,
156}
157
158fn substitute(snippet: &str) -> Result<Substituted, String> {
159    let mut text = String::with_capacity(snippet.len());
160    let mut placeholder_to_metavar = HashMap::new();
161    let mut metavar_to_placeholder: HashMap<String, String> = HashMap::new();
162    let mut metavar_order: Vec<String> = Vec::new();
163
164    let bytes = snippet.as_bytes();
165    let mut i = 0;
166    while i < bytes.len() {
167        if bytes[i] != b'$' {
168            // Copy this UTF-8 scalar verbatim. Indexing the &str at byte
169            // boundaries is safe because we only special-case ASCII `$`.
170            let ch = snippet[i..].chars().next().unwrap();
171            text.push(ch);
172            i += ch.len_utf8();
173            continue;
174        }
175        if snippet[i..].starts_with("$$$") {
176            return Err(
177                "variadic `$$$` metavariables are not yet supported (tracked in #2833)".into(),
178            );
179        }
180        // Parse `$NAME` where NAME is `[A-Za-z_][A-Za-z0-9_]*`.
181        let name_start = i + 1;
182        let mut j = name_start;
183        if j < bytes.len() && is_ident_start(bytes[j]) {
184            j += 1;
185            while j < bytes.len() && is_ident_continue(bytes[j]) {
186                j += 1;
187            }
188        }
189        if j == name_start {
190            // A lone `$` that is not a metavar — keep it literal.
191            text.push('$');
192            i += 1;
193            continue;
194        }
195        let name = &snippet[name_start..j];
196        let placeholder = metavar_to_placeholder
197            .entry(name.to_string())
198            .or_insert_with(|| {
199                let placeholder = format!("{PLACEHOLDER_STEM}{}", metavar_order.len());
200                metavar_order.push(name.to_string());
201                placeholder
202            })
203            .clone();
204        placeholder_to_metavar.insert(placeholder.clone(), name.to_string());
205        text.push_str(&placeholder);
206        i = j;
207    }
208
209    if metavar_order.is_empty() {
210        return Err("pattern has no `$VAR` metavariables".into());
211    }
212
213    Ok(Substituted {
214        text,
215        placeholder_to_metavar,
216        metavar_order,
217    })
218}
219
220fn is_ident_start(b: u8) -> bool {
221    b.is_ascii_alphabetic() || b == b'_'
222}
223
224fn is_ident_continue(b: u8) -> bool {
225    b.is_ascii_alphanumeric() || b == b'_'
226}
227
228// ---------------------------------------------------------------------------
229// Step 2: walk the located subtree into a query
230// ---------------------------------------------------------------------------
231
232struct QueryBuilder<'a> {
233    src: &'a [u8],
234    placeholder_to_metavar: &'a HashMap<String, String>,
235    /// occurrence count per metavar, to mint unification helper captures.
236    occurrences: HashMap<String, usize>,
237    /// `(#eq? @X @X.2)` predicates for repeated metavars.
238    eq_predicates: Vec<String>,
239}
240
241impl<'a> QueryBuilder<'a> {
242    fn new(src: &'a [u8], placeholder_to_metavar: &'a HashMap<String, String>) -> Self {
243        QueryBuilder {
244            src,
245            placeholder_to_metavar,
246            occurrences: HashMap::new(),
247            eq_predicates: Vec::new(),
248        }
249    }
250
251    fn build(&mut self, node: Node<'_>) -> String {
252        // A placeholder leaf is a metavar hole.
253        if node.child_count() == 0 {
254            let text = self.node_text(node);
255            if let Some(metavar) = self.placeholder_to_metavar.get(text) {
256                return format!("(_) @{}", self.capture_for(metavar));
257            }
258            if node.is_named() {
259                return format!("({})", node.kind());
260            }
261            return quote_literal(text);
262        }
263
264        let mut parts: Vec<String> = Vec::new();
265        let mut cursor = node.walk();
266        for (i, child) in node.children(&mut cursor).enumerate() {
267            let sub = self.build(child);
268            // Field names only attach to named children; an anonymous token
269            // in a field slot is matched positionally as a literal, which
270            // tree-sitter accepts where `field: "literal"` may not.
271            match node.field_name_for_child(i as u32) {
272                Some(field) if child.is_named() => parts.push(format!("{field}: {sub}")),
273                _ => parts.push(sub),
274            }
275        }
276        format!("({} {})", node.kind(), parts.join(" "))
277    }
278
279    /// Mint the capture name for this occurrence of `metavar`. The first
280    /// occurrence is `@NAME`; later ones are `@NAME.k` plus an `(#eq? …)`
281    /// predicate tying them to the first (metavar unification).
282    fn capture_for(&mut self, metavar: &str) -> String {
283        let count = self.occurrences.entry(metavar.to_string()).or_insert(0);
284        *count += 1;
285        if *count == 1 {
286            metavar.to_string()
287        } else {
288            let helper = format!("{metavar}.{count}");
289            self.eq_predicates
290                .push(format!("(#eq? @{metavar} @{helper})"));
291            helper
292        }
293    }
294
295    fn predicates(&self) -> String {
296        self.eq_predicates.join(" ")
297    }
298
299    fn node_text(&self, node: Node<'_>) -> &'a str {
300        std::str::from_utf8(&self.src[node.start_byte()..node.end_byte()]).unwrap_or_default()
301    }
302}
303
304/// Quote an anonymous token as a tree-sitter query literal, escaping `"`
305/// and `\`.
306fn quote_literal(text: &str) -> String {
307    let mut out = String::with_capacity(text.len() + 2);
308    out.push('"');
309    for ch in text.chars() {
310        if ch == '"' || ch == '\\' {
311            out.push('\\');
312        }
313        out.push(ch);
314    }
315    out.push('"');
316    out
317}
318
319#[cfg(test)]
320mod tests {
321    use super::*;
322    use streaming_iterator::StreamingIterator;
323    use tree_sitter::{Query, QueryCursor};
324
325    /// Compile `snippet`, run the query against `code`, and return the
326    /// captured text for each requested metavar from the first match.
327    fn run(snippet: &str, language: Language, code: &str) -> Vec<(String, Vec<String>)> {
328        let compiled = compile_pattern(snippet, language).expect("compiles");
329        let ts_language = language.ts_language().expect("grammar");
330        let query = Query::new(&ts_language, &compiled.query)
331            .unwrap_or_else(|e| panic!("query rejected: {e}\nquery: {}", compiled.query));
332        let tree = api::parse_tree(code, language).expect("parse code");
333        let names: Vec<&str> = query.capture_names().to_vec();
334        let mut cursor = QueryCursor::new();
335        let mut matches = cursor.matches(&query, tree.root_node(), code.as_bytes());
336        let mut out = Vec::new();
337        while let Some(m) = matches.next() {
338            let mut per_capture: HashMap<String, Vec<String>> = HashMap::new();
339            for cap in m.captures {
340                let name = names[cap.index as usize].to_string();
341                let text = code[cap.node.start_byte()..cap.node.end_byte()].to_string();
342                per_capture.entry(name).or_default().push(text);
343            }
344            for (name, texts) in per_capture {
345                out.push((name, texts));
346            }
347        }
348        out
349    }
350
351    fn capture<'a>(binds: &'a [(String, Vec<String>)], name: &str) -> &'a [String] {
352        binds
353            .iter()
354            .find(|(n, _)| n == name)
355            .map(|(_, v)| v.as_slice())
356            .unwrap_or(&[])
357    }
358
359    #[test]
360    fn compiles_destructuring_default_in_typescript() {
361        // The #2824 / burin-code#1629 codemod shape.
362        let snippet = "$SRC?.$KEY ?? $DEFAULT";
363        let compiled = compile_pattern(snippet, Language::TypeScript).expect("compiles");
364        assert_eq!(compiled.metavars, vec!["SRC", "KEY", "DEFAULT"]);
365        // It captures the optional-chain object/property and the fallback.
366        let binds = run(
367            snippet,
368            Language::TypeScript,
369            "const a = cfg?.timeout ?? 30;",
370        );
371        assert_eq!(capture(&binds, "SRC"), ["cfg".to_string()]);
372        assert_eq!(capture(&binds, "KEY"), ["timeout".to_string()]);
373        assert_eq!(capture(&binds, "DEFAULT"), ["30".to_string()]);
374    }
375
376    #[test]
377    fn operator_is_constrained_not_just_structure() {
378        // The `??` literal in the query must reject a `||` with the same
379        // structural shape — otherwise the codemod would be unsound.
380        let snippet = "$SRC?.$KEY ?? $DEFAULT";
381        let binds = run(
382            snippet,
383            Language::TypeScript,
384            "const a = cfg?.timeout || 30;",
385        );
386        assert!(
387            capture(&binds, "SRC").is_empty(),
388            "|| must not match the ?? pattern"
389        );
390    }
391
392    #[test]
393    fn round_trips_the_assignment_form() {
394        // The literal acceptance pattern: `$NAME = $SRC?.$KEY ?? $DEFAULT`.
395        let snippet = "$NAME = $SRC?.$KEY ?? $DEFAULT";
396        let compiled = compile_pattern(snippet, Language::TypeScript).expect("compiles");
397        assert_eq!(compiled.metavars, vec!["NAME", "SRC", "KEY", "DEFAULT"]);
398        let binds = run(
399            snippet,
400            Language::TypeScript,
401            "x = src?.userId ?? fallback;",
402        );
403        assert_eq!(capture(&binds, "NAME"), ["x".to_string()]);
404        assert_eq!(capture(&binds, "SRC"), ["src".to_string()]);
405        assert_eq!(capture(&binds, "KEY"), ["userId".to_string()]);
406        assert_eq!(capture(&binds, "DEFAULT"), ["fallback".to_string()]);
407    }
408
409    #[test]
410    fn lifts_metavars_in_rust() {
411        let snippet = "let $NAME = $VALUE;";
412        let binds = run(snippet, Language::Rust, "fn f() { let total = compute(); }");
413        assert_eq!(capture(&binds, "NAME"), ["total".to_string()]);
414        assert_eq!(capture(&binds, "VALUE"), ["compute()".to_string()]);
415    }
416
417    #[test]
418    fn lifts_metavars_in_python() {
419        let snippet = "$FN($ARG)";
420        let binds = run(snippet, Language::Python, "print(value)");
421        assert_eq!(capture(&binds, "FN"), ["print".to_string()]);
422        assert_eq!(capture(&binds, "ARG"), ["value".to_string()]);
423    }
424
425    #[test]
426    fn lifts_metavars_in_go() {
427        let snippet = "$FN($ARG)";
428        let binds = run(snippet, Language::Go, "package main\nfunc m() { log(err) }");
429        assert_eq!(capture(&binds, "FN"), ["log".to_string()]);
430        assert_eq!(capture(&binds, "ARG"), ["err".to_string()]);
431    }
432
433    #[test]
434    fn repeated_metavar_unifies() {
435        // `$X + $X` must match `a + a` but not `a + b`.
436        let snippet = "$X + $X";
437        let same = run(snippet, Language::Rust, "fn f() { let _ = a + a; }");
438        assert_eq!(capture(&same, "X"), ["a".to_string()]);
439        let different = run(snippet, Language::Rust, "fn f() { let _ = a + b; }");
440        assert!(
441            capture(&different, "X").is_empty(),
442            "unification must reject `a + b`"
443        );
444    }
445
446    #[test]
447    fn rejects_unparseable_snippet() {
448        let err = compile_pattern("$A ?? ?? $B", Language::TypeScript).unwrap_err();
449        assert!(err.contains("did not parse"), "got: {err}");
450    }
451
452    #[test]
453    fn rejects_variadic_for_now() {
454        let err = compile_pattern("foo($$$ARGS)", Language::TypeScript).unwrap_err();
455        assert!(err.contains("variadic"), "got: {err}");
456    }
457
458    #[test]
459    fn rejects_pattern_without_metavars() {
460        let err = compile_pattern("foo()", Language::TypeScript).unwrap_err();
461        assert!(err.contains("no `$VAR`"), "got: {err}");
462    }
463}