aft-parse 0.67.1

Syntax parsing, imports and call resolution for Agent Foundational Toolkit
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
//! AST-grep language implementations for ast-grep-core.
//!
//! Provides `AstGrepLang` enum that implements `Language` and `LanguageExt`
//! traits from ast-grep-core, mapping to our tree-sitter language grammars.

use std::borrow::Cow;

use ast_grep_core::language::Language;
use ast_grep_core::matcher::PatternError;
use ast_grep_core::tree_sitter::{LanguageExt, StrDoc, TSLanguage};
use ast_grep_core::Pattern;

/// Supported languages for AST pattern matching via ast-grep.
///
/// TOML remains outside this enum, like YAML: its data-oriented grammar does
/// not accept ast-grep's identifier metavariable sentinels as keys.
#[derive(Clone, Debug, PartialEq, Eq)]
pub enum AstGrepLang {
    TypeScript,
    Tsx,
    JavaScript,
    Python,
    Rust,
    Go,
    C,
    Cpp,
    Cuda,
    Metal,
    Zig,
    CSharp,
    Solidity,
    Scss,
    Vue,
    Json,
    Java,
    Ruby,
    Kotlin,
    Swift,
    Php,
    Lua,
    Perl,
    Pascal,
    R,
    Groovy,
    ObjC,
}

#[derive(Clone, Debug)]
enum PhpPatternLang {
    Full,
    Snippet,
}

impl PhpPatternLang {
    fn pattern_has_open_tag(source: &str) -> bool {
        source.contains("<?php") || source.contains("<?=")
    }
}

impl Language for PhpPatternLang {
    fn kind_to_id(&self, kind: &str) -> u16 {
        let ts_lang: TSLanguage = self.get_ts_language();
        ts_lang.id_for_node_kind(kind, true)
    }

    fn field_to_id(&self, field: &str) -> Option<u16> {
        self.get_ts_language()
            .field_id_for_name(field)
            .map(|field| field.get())
    }

    fn build_pattern(
        &self,
        builder: &ast_grep_core::matcher::PatternBuilder,
    ) -> Result<Pattern, PatternError> {
        builder.build(|src| StrDoc::try_new(src, self.clone()))
    }

    fn pre_process_pattern<'q>(&self, query: &'q str) -> Cow<'q, str> {
        pre_process_pattern_with_expando(query, self.expando_char())
    }

    fn expando_char(&self) -> char {
        '\u{00B5}'
    }
}

impl LanguageExt for PhpPatternLang {
    fn get_ts_language(&self) -> TSLanguage {
        match self {
            Self::Full => tree_sitter_php::LANGUAGE_PHP.into(),
            Self::Snippet => tree_sitter_php::LANGUAGE_PHP_ONLY.into(),
        }
    }
}

impl AstGrepLang {
    /// Parse from a string (case-insensitive).
    pub fn from_str(s: &str) -> Option<Self> {
        match s.to_lowercase().as_str() {
            "typescript" | "ts" => Some(Self::TypeScript),
            "tsx" => Some(Self::Tsx),
            "javascript" | "js" => Some(Self::JavaScript),
            "python" | "py" => Some(Self::Python),
            "rust" | "rs" => Some(Self::Rust),
            "go" | "golang" => Some(Self::Go),
            "c" => Some(Self::C),
            "cpp" | "c++" | "cplusplus" => Some(Self::Cpp),
            "cuda" | "cu" => Some(Self::Cuda),
            "metal" => Some(Self::Metal),
            "zig" => Some(Self::Zig),
            "csharp" | "c#" | "cs" => Some(Self::CSharp),
            "solidity" | "sol" => Some(Self::Solidity),
            "scss" => Some(Self::Scss),
            "vue" => Some(Self::Vue),
            "json" | "jsonc" => Some(Self::Json),
            "java" => Some(Self::Java),
            "ruby" | "rb" => Some(Self::Ruby),
            "kotlin" | "kt" | "kts" => Some(Self::Kotlin),
            "swift" => Some(Self::Swift),
            "php" => Some(Self::Php),
            "lua" => Some(Self::Lua),
            "perl" | "pl" | "pm" => Some(Self::Perl),
            "pascal" | "pas" | "pp" | "dpr" | "dpk" | "lpr" => Some(Self::Pascal),
            "r" => Some(Self::R),
            "groovy" => Some(Self::Groovy),
            "objc" | "objective-c" | "objectivec" | "obj-c" => Some(Self::ObjC),
            _ => None,
        }
    }

    pub fn compile_pattern(&self, pattern: &str) -> Result<Pattern, PatternError> {
        if matches!(self, Self::Pascal) {
            if pascal_pattern_is_declaration(pattern) {
                return Pattern::try_new(pattern, self.clone());
            }
            let selector = pascal_snippet_selector(pattern)?;
            let context = format!("procedure dummy; begin {pattern} end;");
            return Pattern::contextual(&context, &selector, self.clone());
        }

        if !matches!(self, Self::Php) {
            return Pattern::try_new(pattern, self.clone());
        }

        if PhpPatternLang::pattern_has_open_tag(pattern) {
            return Pattern::try_new(pattern, PhpPatternLang::Full);
        }

        let selector = php_snippet_selector(pattern)?;
        let context = format!("<?php\n{pattern}");
        Pattern::contextual(&context, &selector, PhpPatternLang::Full)
    }

    /// Candidate file extensions. Ambiguous `.h` files are resolved by `matches_path`.
    pub fn extensions(&self) -> &'static [&'static str] {
        match self {
            Self::TypeScript => &["ts", "mts", "cts"],
            Self::Tsx => &["tsx"],
            Self::JavaScript => &["js", "mjs", "cjs", "jsx"],
            Self::Python => &["py", "pyi"],
            Self::Rust => &["rs"],
            Self::Go => &["go"],
            Self::C => &["c", "h"],
            Self::Cpp => &["cc", "cpp", "cxx", "hpp", "hh", "h"],
            Self::Cuda => &["cu", "cuh"],
            Self::Metal => &["metal"],
            Self::Zig => &["zig"],
            Self::CSharp => &["cs"],
            Self::Solidity => &["sol"],
            Self::Scss => &["scss"],
            Self::Vue => &["vue"],
            Self::Json => &["json", "jsonc"],
            Self::Java => &["java"],
            Self::Ruby => &["rb"],
            Self::Kotlin => &["kt", "kts"],
            Self::Swift => &["swift"],
            Self::Php => &["inc", "php"],
            Self::Lua => &["lua"],
            Self::Perl => &["pl", "pm", "t"],
            Self::Pascal => &["pas", "pp", "dpr", "dpk", "lpr"],
            Self::R => &["R", "r"],
            Self::Groovy => &["groovy", "gvy", "gy", "gsh", "gradle"],
            Self::ObjC => &["m", "mm", "h"],
        }
    }

    /// Check if a file extension matches this language.
    pub fn matches_extension(&self, ext: &str) -> bool {
        self.extensions().contains(&ext)
    }

    /// Check a path's language, resolving ambiguous headers from their contents.
    pub fn matches_path(&self, path: &std::path::Path) -> bool {
        if path.extension().and_then(|ext| ext.to_str()) == Some("h") {
            return matches!(
                (self, crate::parser::detect_language(path)),
                (Self::C, Some(crate::parser::LangId::C))
                    | (Self::Cpp, Some(crate::parser::LangId::Cpp))
                    | (Self::ObjC, Some(crate::parser::LangId::ObjC))
            );
        }
        if matches!(self, Self::Groovy)
            && path.file_name().and_then(|name| name.to_str()) == Some("Jenkinsfile")
        {
            return true;
        }

        let ext = path.extension().and_then(|e| e.to_str()).unwrap_or("");
        self.matches_extension(ext)
    }
}

fn php_snippet_selector(pattern: &str) -> Result<String, PatternError> {
    Pattern::try_new(pattern, PhpPatternLang::Snippet)?;
    let processed = PhpPatternLang::Snippet.pre_process_pattern(pattern);
    let grep = PhpPatternLang::Snippet.ast_grep(processed.as_ref());
    let mut node = grep.root();
    while node.children().len() == 1 {
        node = node.child(0).expect("single child exists");
    }
    Ok(node.kind().into_owned())
}

test_support_visibility! {
fn pascal_pattern_is_declaration(pattern: &str) -> bool {
    let trimmed = pattern.trim_start().to_lowercase();
    [
        "program",
        "unit",
        "procedure",
        "function",
        "constructor",
        "destructor",
        "type",
        "const",
        "var",
    ]
    .iter()
    .any(|keyword| {
        trimmed.strip_prefix(keyword).is_some_and(|rest| {
            rest.chars()
                .next()
                .is_none_or(|ch| !ch.is_alphanumeric() && ch != '_')
        })
    })
}
}

fn pascal_snippet_selector(pattern: &str) -> Result<String, PatternError> {
    let context = format!("procedure dummy; begin {pattern} end;");
    let grep = AstGrepLang::Pascal.ast_grep(context);
    let root = grep.root();
    let mut node = root;
    while node.children().len() == 1 {
        node = node.child(0).expect("single child exists");
    }
    let mut block_node = None;
    for child in node.children() {
        if child.kind() == "block" {
            block_node = Some(child);
            break;
        }
    }
    let Some(block) = block_node else {
        return Err(PatternError::NoContent(
            "invalid Pascal pattern structure".to_string(),
        ));
    };
    let mut stmt_node = None;
    for child in block.children() {
        if child.kind() != "kBegin" && child.kind() != "kEnd" {
            stmt_node = Some(child);
            break;
        }
    }
    let Some(stmt) = stmt_node else {
        return Err(PatternError::NoContent("empty Pascal pattern".to_string()));
    };
    Ok(stmt.kind().into_owned())
}

fn pre_process_pattern_with_expando<'q>(query: &'q str, expando: char) -> Cow<'q, str> {
    if expando == '$' {
        // TS, JS, Go: $ is valid in identifiers, no preprocessing needed
        return Cow::Borrowed(query);
    }
    // Python, Rust, PHP, and other grammars that reject `$` identifiers:
    // replace $ with µ in meta-variable positions. Logic from ast-grep's
    // official pre_process_pattern.
    let mut ret = Vec::with_capacity(query.len());
    let mut dollar_count = 0;
    for c in query.chars() {
        if c == '$' {
            dollar_count += 1;
            continue;
        }
        let need_replace = matches!(c, 'A'..='Z' | '_') || dollar_count == 3;
        let sigil = if need_replace { expando } else { '$' };
        ret.extend(std::iter::repeat(sigil).take(dollar_count));
        dollar_count = 0;
        ret.push(c);
    }
    // trailing anonymous multiple ($$$)
    let sigil = if dollar_count == 3 { expando } else { '$' };
    ret.extend(std::iter::repeat(sigil).take(dollar_count));
    Cow::Owned(ret.into_iter().collect())
}

impl Language for AstGrepLang {
    fn kind_to_id(&self, kind: &str) -> u16 {
        let ts_lang: TSLanguage = self.get_ts_language();
        ts_lang.id_for_node_kind(kind, /* named */ true)
    }

    fn field_to_id(&self, field: &str) -> Option<u16> {
        self.get_ts_language()
            .field_id_for_name(field)
            .map(|f| f.get())
    }

    fn build_pattern(
        &self,
        builder: &ast_grep_core::matcher::PatternBuilder,
    ) -> Result<Pattern, PatternError> {
        if matches!(self, Self::Php) {
            return builder.build(|src| StrDoc::try_new(src, PhpPatternLang::Snippet));
        }

        builder.build(|src| StrDoc::try_new(src, self.clone()))
    }

    /// Some languages (Python, Rust) don't accept `$` as a valid identifier
    /// character. We replace `$` with the expando char (`µ`) in meta-variable
    /// positions so tree-sitter can parse the pattern as valid code.
    fn pre_process_pattern<'q>(&self, query: &'q str) -> Cow<'q, str> {
        pre_process_pattern_with_expando(query, self.expando_char())
    }

    fn expando_char(&self) -> char {
        match self {
            // $ is not a valid identifier char in Python, Rust, C-family, Zig, or C#.
            // Solidity intentionally allows `$` in identifiers
            // (`identifier: /[a-zA-Z$_][a-zA-Z0-9$_]*/` in tree-sitter-solidity),
            // so keep the meta-var sigil as `$` — replacing it with `µ`
            // breaks pattern parsing (µ is not in the Solidity identifier
            // character set, so `µNAME` is rejected by the grammar and
            // meta-vars never bind).
            Self::Python
            | Self::Rust
            | Self::C
            | Self::Cpp
            | Self::Cuda
            | Self::Metal
            | Self::Zig
            | Self::CSharp
            | Self::Java
            | Self::Ruby
            | Self::Kotlin
            | Self::Swift
            | Self::Php
            | Self::Lua
            | Self::Perl
            | Self::R
            | Self::ObjC => '\u{00B5}', // µ
            Self::Pascal => '_',
            // $ is valid in TS, JS, Go, Solidity, Groovy, and Vue template identifiers.
            // Groovy also uses $ inside GStrings, so keeping $ avoids rejecting
            // literal interpolation patterns while still letting $NAME meta-vars bind.
            _ => '$',
        }
    }
}

impl LanguageExt for AstGrepLang {
    fn get_ts_language(&self) -> TSLanguage {
        match self {
            Self::TypeScript => tree_sitter_typescript::LANGUAGE_TYPESCRIPT.into(),
            Self::Tsx => tree_sitter_typescript::LANGUAGE_TSX.into(),
            Self::JavaScript => tree_sitter_javascript::LANGUAGE.into(),
            Self::Python => tree_sitter_python::LANGUAGE.into(),
            Self::Rust => tree_sitter_rust::LANGUAGE.into(),
            Self::Go => tree_sitter_go::LANGUAGE.into(),
            Self::C => tree_sitter_c::LANGUAGE.into(),
            Self::Cpp => tree_sitter_cpp::LANGUAGE.into(),
            Self::Cuda => tree_sitter_cuda::LANGUAGE.into(),
            Self::Metal => tree_sitter_cpp::LANGUAGE.into(),
            Self::Zig => tree_sitter_zig::LANGUAGE.into(),
            Self::CSharp => tree_sitter_c_sharp::LANGUAGE.into(),
            Self::Solidity => tree_sitter_solidity::LANGUAGE.into(),
            Self::Scss => tree_sitter_scss::language(),
            Self::Vue => tree_sitter_vue::LANGUAGE.into(),
            Self::Json => tree_sitter_json::LANGUAGE.into(),
            Self::Java => tree_sitter_java::LANGUAGE.into(),
            Self::Ruby => tree_sitter_ruby::LANGUAGE.into(),
            Self::Kotlin => tree_sitter_kotlin_sg::LANGUAGE.into(),
            Self::Swift => tree_sitter_swift::LANGUAGE.into(),
            Self::Php => tree_sitter_php::LANGUAGE_PHP.into(),
            Self::Lua => tree_sitter_lua::LANGUAGE.into(),
            Self::Perl => tree_sitter_perl::LANGUAGE.into(),
            Self::Pascal => tree_sitter_pascal::LANGUAGE.into(),
            Self::R => tree_sitter_r::LANGUAGE.into(),
            Self::Groovy => dekobon_tree_sitter_groovy::LANGUAGE.into(),
            Self::ObjC => tree_sitter_objc::LANGUAGE.into(),
        }
    }
}