greplm-core 0.3.0

Core indexing and search engine for greplm: a trigram code index for LLM agents.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
//! Structural (AST) search.
//!
//! Two input dialects are accepted:
//!   * A native tree-sitter query S-expression (anything starting with `(`),
//!     with captures (`@name`) and `#eq?` / `#match?` predicates. This is the
//!     full power of the tree-sitter query language.
//!   * A friendlier code pattern with `$NAME` meta-variables, e.g.
//!     `fn $NAME($PARAMS) -> Result<$T>`, which is compiled into a tree-sitter
//!     query: concrete tokens become structure + `#eq?` constraints, and each
//!     meta-variable becomes a wildcard capture. A variadic `$$$` (optionally
//!     `$$$NAME`) matches any sequence of sibling nodes, so
//!     `function $NAME($$$) { $$$ }` matches a function with any parameters and
//!     any body. Variadic meta-variables only relax structure; they do not bind
//!     a capture.
//!
//! Matching runs the compiled query over candidate documents. Literal tokens in
//! the pattern double as trigram anchors so the index prunes candidates before
//! parsing.

use std::cell::RefCell;
use std::collections::HashMap;

use tree_sitter::{Node, Parser, Query, QueryCursor, StreamingIterator};

use crate::error::{Error, Result};
use crate::lang::Language;

thread_local! {
    static STRUCT_PARSERS: RefCell<HashMap<Language, Parser>> = RefCell::new(HashMap::new());
}

/// Sentinel prefix used when substituting `$NAME` meta-variables so the pattern
/// still parses as code before we walk it.
const SENTINEL: &str = "GREPLMMV";

/// The capture automatically attached to the root of a compiled pattern, used
/// to locate the matched node.
const ROOT_CAPTURE: &str = "greplm.match";

/// Sentinel that a variadic `$$$` meta-variable is collapsed to. It parses as
/// an identifier in the common contexts (parameter lists, argument lists,
/// statement blocks, arrays) and is dropped during emission, leaving the
/// surrounding structure unconstrained — tree-sitter queries already allow
/// extra, unmatched sibling nodes, which is exactly variadic semantics.
const VARIADIC: &str = "GREPLMVARIADIC";

/// A compiled structural pattern ready to run against documents.
pub struct Compiled {
    query: Query,
    /// Literal tokens that must appear in any matching document (from `#eq?`
    /// constraints); used as a trigram prefilter. Empty disables prefiltering.
    pub anchors: Vec<String>,
    root_capture: Option<u32>,
}

/// A capture bound by a structural match.
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
pub struct StructCapture {
    pub name: String,
    pub text: String,
    pub line: u32,
}

/// A single structural match within one document.
#[derive(Debug, Clone)]
pub struct StructMatch {
    pub line_start: u32,
    pub line_end: u32,
    pub kind: String,
    pub captures: Vec<StructCapture>,
}

/// Compile a pattern (S-expression or meta-variable form) for `lang`.
pub fn compile(lang: Language, pattern: &str) -> Result<Compiled> {
    let grammar = lang
        .grammar()
        .ok_or_else(|| Error::other(format!("language {} is not parseable", lang.id())))?;

    let trimmed = pattern.trim();
    let (query_src, anchors) = if trimmed.starts_with('(') {
        // Raw tree-sitter query: trust the user, no prefilter anchors.
        (trimmed.to_string(), Vec::new())
    } else {
        compile_metavars(lang, trimmed)?
    };

    let query = Query::new(&grammar, &query_src).map_err(|e| {
        Error::other(format!(
            "invalid structural query: {e}\n--- compiled query ---\n{query_src}"
        ))
    })?;
    let root_capture = query
        .capture_names()
        .iter()
        .position(|n| *n == ROOT_CAPTURE)
        .map(|i| i as u32);
    Ok(Compiled {
        query,
        anchors,
        root_capture,
    })
}

/// Run a compiled pattern over one source buffer.
pub fn run(lang: Language, compiled: &Compiled, source: &[u8]) -> Vec<StructMatch> {
    let grammar = match lang.grammar() {
        Some(g) => g,
        None => return Vec::new(),
    };
    STRUCT_PARSERS.with(|cell| {
        let mut map = cell.borrow_mut();
        let parser = map.entry(lang).or_insert_with(|| {
            let mut p = Parser::new();
            let _ = p.set_language(&grammar);
            p
        });
        let tree = match parser.parse(source, None) {
            Some(t) => t,
            None => return Vec::new(),
        };
        let names = compiled.query.capture_names();
        // QueryCursor's match limit defaults to `u32::MAX` (effectively
        // unlimited), so matches are not silently capped on large files.
        let mut cursor = QueryCursor::new();
        let mut out = Vec::new();
        let mut b1 = Vec::new();
        let mut b2 = Vec::new();
        let mut src = source;
        let mut matches = cursor.matches(&compiled.query, tree.root_node(), source);
        while let Some(m) = matches.next() {
            if !m.satisfies_text_predicates(&compiled.query, &mut b1, &mut b2, &mut src) {
                continue;
            }
            // Locate the primary node: the root capture if present, else the
            // first capture.
            let primary = compiled
                .root_capture
                .and_then(|ri| m.captures.iter().find(|c| c.index == ri))
                .or_else(|| m.captures.first());
            let node = match primary {
                Some(c) => c.node,
                None => continue,
            };
            let mut captures = Vec::new();
            for c in m.captures {
                let name = names.get(c.index as usize).copied().unwrap_or("");
                if name == ROOT_CAPTURE {
                    continue;
                }
                if let Ok(text) = std::str::from_utf8(&source[c.node.byte_range()]) {
                    captures.push(StructCapture {
                        name: name.to_string(),
                        text: text.to_string(),
                        line: c.node.start_position().row as u32 + 1,
                    });
                }
            }
            out.push(StructMatch {
                line_start: node.start_position().row as u32 + 1,
                line_end: node.end_position().row as u32 + 1,
                kind: node.kind().to_string(),
                captures,
            });
        }
        out
    })
}

/// Compile a `$NAME` meta-variable pattern into a tree-sitter query, returning
/// the query text and the literal trigram anchors.
fn compile_metavars(lang: Language, pattern: &str) -> Result<(String, Vec<String>)> {
    let grammar = lang
        .grammar()
        .ok_or_else(|| Error::other(format!("language {} is not parseable", lang.id())))?;

    // Substitute `$NAME` with a parseable sentinel identifier we can recognize.
    let (substituted, _names) = substitute_metavars(pattern);

    let mut parser = Parser::new();
    parser
        .set_language(&grammar)
        .map_err(|e| Error::other(format!("set_language: {e}")))?;
    let tree = parser
        .parse(substituted.as_bytes(), None)
        .ok_or_else(|| Error::other("failed to parse pattern".to_string()))?;
    let src = substituted.as_bytes();

    // Find the meaningful root by unwrapping single-child wrapper nodes.
    let root = unwrap_root(tree.root_node());
    if root.is_error() {
        return Err(Error::other(
            "pattern did not parse cleanly; check the syntax or use a tree-sitter query"
                .to_string(),
        ));
    }

    let mut out = String::new();
    let mut anchors = Vec::new();
    let mut counter = 0usize;
    emit(root, src, &mut out, &mut anchors, &mut counter);
    out.push_str(&format!(" @{ROOT_CAPTURE}"));
    Ok((out, anchors))
}

/// Replace `$Ident` occurrences with `GREPLMMV_Ident` sentinels.
fn substitute_metavars(pattern: &str) -> (String, Vec<String>) {
    let mut out = String::with_capacity(pattern.len());
    let mut names = Vec::new();
    let bytes = pattern.as_bytes();
    let mut i = 0;
    while i < bytes.len() {
        if bytes[i] == b'$' {
            // Variadic `$$$` (optionally `$$$NAME`): collapse to a single
            // sentinel identifier that we later drop from the query.
            if i + 2 < bytes.len() && bytes[i + 1] == b'$' && bytes[i + 2] == b'$' {
                let mut j = i + 3;
                while j < bytes.len() && (bytes[j].is_ascii_alphanumeric() || bytes[j] == b'_') {
                    j += 1;
                }
                out.push_str(VARIADIC);
                i = j;
                continue;
            }
            let mut j = i + 1;
            while j < bytes.len() && (bytes[j].is_ascii_alphanumeric() || bytes[j] == b'_') {
                j += 1;
            }
            if j > i + 1 {
                let name = &pattern[i + 1..j];
                out.push_str(SENTINEL);
                out.push('_');
                out.push_str(name);
                names.push(name.to_string());
                i = j;
                continue;
            }
        }
        out.push(bytes[i] as char);
        i += 1;
    }
    (out, names)
}

/// Unwrap single-named-child wrapper nodes (file/program/statement shells) to
/// reach the meaningful pattern node.
fn unwrap_root(node: Node) -> Node {
    const WRAPPERS: &[&str] = &[
        "source_file",
        "program",
        "translation_unit",
        "module",
        "expression_statement",
        "statement",
        "compound_statement",
        "block",
    ];
    let mut cur = node;
    loop {
        if !WRAPPERS.contains(&cur.kind()) {
            return cur;
        }
        let mut cursor = cur.walk();
        let children: Vec<Node> = cur.named_children(&mut cursor).collect();
        if children.len() == 1 {
            cur = children[0];
        } else {
            return cur;
        }
    }
}

/// Recursively emit an S-expression for `node`.
fn emit(node: Node, src: &[u8], out: &mut String, anchors: &mut Vec<String>, counter: &mut usize) {
    let text = node_text(node, src);

    // A variadic `$$$` node (and any single-child wrapper around it, such as an
    // expression statement) contributes no constraint: emit nothing so the
    // parent matches regardless of the nodes in this position.
    if let Some(t) = &text {
        if t.trim() == VARIADIC {
            return;
        }
    }

    // A substituted meta-variable becomes a wildcard capture.
    if let Some(t) = &text {
        if let Some(name) = t.strip_prefix(&format!("{SENTINEL}_")) {
            if is_clean_ident(name) {
                out.push_str(&format!("(_) @{name}"));
                return;
            }
        }
    }

    let mut cursor = node.walk();
    let named: Vec<Node> = node.named_children(&mut cursor).collect();

    if node.child_count() == 0 {
        // A true terminal token (identifier, literal, keyword). Constrain by
        // kind and exact text.
        out.push_str(&format!("({})", node.kind()));
        if let Some(t) = text {
            if !t.is_empty() && !t.starts_with(SENTINEL) {
                *counter += 1;
                let cap = format!("greplm_a{counter}");
                out.push_str(&format!(" @{cap}"));
                out.push_str(&format!(" (#eq? @{cap} \"{}\")", escape(&t)));
                if t.len() >= 3 && t.chars().all(|c| !c.is_whitespace()) {
                    anchors.push(t);
                }
            }
        }
        return;
    }

    if named.is_empty() {
        // Has only anonymous children (e.g. empty `()` or `{}`): match any node
        // of this kind without constraining its contents.
        out.push_str(&format!("({})", node.kind()));
        return;
    }

    out.push('(');
    out.push_str(node.kind());
    for child in named {
        out.push(' ');
        emit(child, src, out, anchors, counter);
    }
    out.push(')');
}

fn node_text(node: Node, src: &[u8]) -> Option<String> {
    std::str::from_utf8(src.get(node.start_byte()..node.end_byte())?)
        .ok()
        .map(|s| s.to_string())
}

fn is_clean_ident(s: &str) -> bool {
    !s.is_empty() && s.chars().all(|c| c.is_ascii_alphanumeric() || c == '_')
}

fn escape(s: &str) -> String {
    s.replace('\\', "\\\\").replace('"', "\\\"")
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn raw_query_matches_calls() {
        let src = b"fn main() {\n    foo();\n    bar();\n}\n";
        let c = compile(
            Language::Rust,
            "(call_expression function: (identifier) @fn)",
        )
        .unwrap();
        let m = run(Language::Rust, &c, src);
        let names: Vec<&str> = m
            .iter()
            .flat_map(|mm| mm.captures.iter())
            .map(|c| c.text.as_str())
            .collect();
        assert!(names.contains(&"foo"), "got {names:?}");
        assert!(names.contains(&"bar"), "got {names:?}");
    }

    #[test]
    fn predicate_filters_by_name() {
        let src = b"fn main() {\n    foo();\n    bar();\n}\n";
        let c = compile(
            Language::Rust,
            "((call_expression function: (identifier) @fn) (#eq? @fn \"foo\"))",
        )
        .unwrap();
        let m = run(Language::Rust, &c, src);
        let names: Vec<&str> = m
            .iter()
            .flat_map(|mm| mm.captures.iter())
            .map(|c| c.text.as_str())
            .collect();
        assert_eq!(names, vec!["foo"], "predicate should keep only foo");
    }

    #[test]
    fn metavar_pattern_compiles_and_matches() {
        let src = b"struct A;\nfn alpha() {}\nfn beta() {}\n";
        let c = compile(Language::Rust, "fn $NAME() {}").unwrap();
        let m = run(Language::Rust, &c, src);
        // Both functions should match the shape; capture NAME bound.
        let names: Vec<String> = m
            .iter()
            .flat_map(|mm| mm.captures.iter())
            .filter(|c| c.name == "NAME")
            .map(|c| c.text.clone())
            .collect();
        assert!(names.contains(&"alpha".to_string()), "got {names:?}");
        assert!(names.contains(&"beta".to_string()), "got {names:?}");
    }

    #[test]
    fn variadic_metavars_match_any_params_and_body() {
        let src = b"function noop() {}\nfunction add(a, b) { return a + b; }\n";
        let c = compile(Language::JavaScript, "function $NAME($$$) { $$$ }").unwrap();
        let m = run(Language::JavaScript, &c, src);
        let names: Vec<String> = m
            .iter()
            .flat_map(|mm| mm.captures.iter())
            .filter(|c| c.name == "NAME")
            .map(|c| c.text.clone())
            .collect();
        assert!(names.contains(&"noop".to_string()), "got {names:?}");
        assert!(
            names.contains(&"add".to_string()),
            "variadic params/body should match a function with args, got {names:?}"
        );
    }
}