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
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
use super::{
    import_byte_range, ImportBlock, ImportForm, ImportGroup, ImportKind, ImportRequest,
    ImportStatement, ImportSyntax, PhpImportClause,
};
use tree_sitter::{Node, Tree};

pub fn classify_group_php(_module_path: &str) -> ImportGroup {
    ImportGroup::External
}

pub fn parse_php_imports(source: &str, tree: &Tree) -> ImportBlock {
    let root = tree.root_node();
    let mut imports = Vec::new();
    collect_php_imports(source, root, &mut imports);
    let byte_range = import_byte_range(&imports);
    ImportBlock {
        imports,
        byte_range,
    }
}

fn collect_php_imports(source: &str, root: Node, imports: &mut Vec<ImportStatement>) {
    collect_php_imports_in_scope(source, root, imports);
}

test_support_visibility! {
/// Collects the `use` declarations of `scope` and of the namespace blocks in
/// it, in source order, and returns how many nodes it looked at. Only the
/// children of scopes and of namespaces are looked at, so the walk skips
/// function bodies and expressions.
///
/// The walk keeps its pending nodes on the heap rather than recursing:
/// namespace blocks can nest (error recovery accepts them) arbitrarily
/// deep, and a stack overflow aborts the process. Children are pushed in
/// reverse, so a nested namespace's imports still come before those of the
/// declarations after it.
fn collect_php_imports_in_scope(
    source: &str,
    scope: Node,
    imports: &mut Vec<ImportStatement>,
) -> usize {
    /// Where a pending node sits: directly in a scope, or directly in a
    /// namespace definition.
    #[derive(Clone, Copy)]
    enum Within {
        Scope,
        Namespace,
    }
    fn push_children<'tree>(
        pending: &mut Vec<(Node<'tree>, Within)>,
        node: Node<'tree>,
        within: Within,
    ) {
        let start = pending.len();
        let mut cursor = node.walk();
        pending.extend(node.children(&mut cursor).map(|child| (child, within)));
        pending[start..].reverse();
    }

    let mut visited = 0;
    let mut pending = Vec::new();
    push_children(&mut pending, scope, Within::Scope);
    while let Some((child, within)) = pending.pop() {
        visited += 1;
        match (within, child.kind()) {
            (Within::Scope, "namespace_use_declaration") => {
                if let Some(imp) = parse_php_namespace_use_declaration(source, &child) {
                    imports.push(imp);
                }
            }
            (Within::Scope, "namespace_definition") => {
                push_children(&mut pending, child, Within::Namespace);
            }
            (Within::Namespace, "compound_statement") => {
                push_children(&mut pending, child, Within::Scope);
            }
            _ => {}
        }
    }
    visited
}
}

test_support_visibility! {
fn parse_php_namespace_use_declaration(source: &str, node: &Node) -> Option<ImportStatement> {
    let clauses = direct_children(node, "namespace_use_clause");
    if !clauses.is_empty() {
        return parse_php_clause_declaration(source, node, &clauses);
    }

    if find_direct_child(node, "namespace_use_group").is_some() {
        return parse_php_grouped_namespace_use_declaration(source, node);
    }

    parse_php_payload_namespace_use_declaration(source, node)
}
}

fn parse_php_clause_declaration(
    source: &str,
    node: &Node,
    clause_nodes: &[Node<'_>],
) -> Option<ImportStatement> {
    let mut clauses: Vec<PhpImportClause> = clause_nodes
        .iter()
        .filter_map(|clause| {
            let (module_path, alias, import_kind) = parse_php_namespace_use_clause(source, clause)?;
            Some(PhpImportClause {
                module_path,
                alias,
                import_kind,
            })
        })
        .collect();
    if clauses.is_empty() {
        return None;
    }

    // In `use function A, B;` and `use const A, B;`, PHP writes the kind once
    // for the whole declaration. The grammar attaches it to one clause, so copy
    // it to siblings to keep every clause semantically complete.
    let declaration_kind = clauses
        .first()
        .and_then(|clause| clause.import_kind.clone());
    if let Some(kind) = declaration_kind {
        for clause in &mut clauses {
            clause.import_kind.get_or_insert_with(|| kind.clone());
        }
    }

    let module_path = clauses.first()?.module_path.clone();
    let group = classify_group_php(&module_path);

    Some(ImportStatement {
        module_path,
        names: Vec::new(),
        default_import: None,
        namespace_import: None,
        kind: ImportKind::Value,
        group,
        byte_range: node.byte_range(),
        raw_text: source[node.byte_range()].to_string(),
        form: ImportForm::Php { clauses },
    })
}

fn parse_php_payload_namespace_use_declaration(
    source: &str,
    node: &Node,
) -> Option<ImportStatement> {
    let raw_text = source[node.byte_range()].to_string();
    let byte_range = node.byte_range();
    let mut payload = php_use_payload(source, node)?;
    let mut import_kind = None;

    for kind in ["function", "const"] {
        if let Some(rest) = payload
            .strip_prefix(kind)
            .and_then(|rest| rest.strip_prefix(' '))
        {
            import_kind = Some(kind.to_string());
            payload = rest.trim().to_string();
            break;
        }
    }

    let (module_path, alias) = if let Some((path, alias)) = payload.split_once(" as ") {
        (path.trim().to_string(), Some(alias.trim().to_string()))
    } else {
        (payload.trim().to_string(), None)
    };
    if module_path.is_empty() {
        return None;
    }

    let group = classify_group_php(&module_path);

    Some(ImportStatement {
        module_path,
        names: Vec::new(),
        default_import: None,
        namespace_import: None,
        kind: ImportKind::Value,
        group,
        byte_range,
        raw_text,
        form: ImportForm::Structured {
            named: Vec::new(),
            namespace: None,
            alias,
            modifiers: vec![],
            import_kind,
        },
    })
}

fn parse_php_grouped_namespace_use_declaration(
    source: &str,
    node: &Node,
) -> Option<ImportStatement> {
    let raw_text = source[node.byte_range()].to_string();
    let byte_range = node.byte_range();
    let (module_path, import_kind) = parse_php_grouped_use_header(source, node)
        .or_else(|| php_use_payload(source, node).map(|payload| (payload, None)))?;
    if module_path.is_empty() {
        return None;
    }

    let names = parse_php_grouped_use_members(&raw_text);
    let group = classify_group_php(&module_path);

    Some(ImportStatement {
        module_path,
        names: names.clone(),
        default_import: None,
        namespace_import: None,
        kind: ImportKind::Value,
        group,
        byte_range,
        raw_text,
        form: ImportForm::Structured {
            named: names,
            namespace: None,
            alias: None,
            modifiers: vec!["group".to_string()],
            import_kind,
        },
    })
}

fn parse_php_grouped_use_members(raw_text: &str) -> Vec<String> {
    let Some(open) = raw_text.find('{') else {
        return Vec::new();
    };
    let Some(close) = raw_text.rfind('}') else {
        return Vec::new();
    };
    if close <= open {
        return Vec::new();
    }

    raw_text[open + 1..close]
        .split(',')
        .map(str::trim)
        .filter(|member| !member.is_empty())
        .map(str::to_string)
        .collect()
}

fn parse_php_grouped_use_header(source: &str, node: &Node) -> Option<(String, Option<String>)> {
    let mut module_path: Option<String> = None;
    let mut import_kind: Option<String> = None;
    let mut leading_absolute = false;

    let mut cursor = node.walk();
    if cursor.goto_first_child() {
        loop {
            let child = cursor.node();
            if child.kind() == "namespace_use_group" {
                break;
            }

            let text = source[child.byte_range()].trim();
            match child.kind() {
                "function" | "const" => import_kind = Some(text.to_string()),
                "\\" if module_path.is_none() => leading_absolute = true,
                "qualified_name" | "namespace_name" | "name" => {
                    if module_path.is_none() {
                        module_path = Some(text.to_string());
                    }
                }
                _ => {}
            }

            if !cursor.goto_next_sibling() {
                break;
            }
        }
    }

    let mut module_path = module_path?;
    if leading_absolute && !module_path.starts_with('\\') {
        module_path.insert(0, '\\');
    }

    Some((module_path, import_kind))
}

fn php_use_payload(source: &str, node: &Node) -> Option<String> {
    let raw = source[node.byte_range()].trim();
    raw.strip_prefix("use")
        .map(str::trim)
        .map(|payload| payload.strip_suffix(';').map(str::trim).unwrap_or(payload))
        .map(str::to_string)
        .filter(|payload| !payload.is_empty())
}

fn parse_php_namespace_use_clause(
    source: &str,
    node: &Node,
) -> Option<(String, Option<String>, Option<String>)> {
    let mut module_path: Option<String> = None;
    let mut alias: Option<String> = None;
    let mut import_kind: Option<String> = None;
    let mut saw_as = false;
    let mut leading_absolute = false;

    let mut cursor = node.walk();
    if cursor.goto_first_child() {
        loop {
            let child = cursor.node();
            let text = source[child.byte_range()].trim();
            match child.kind() {
                "function" | "const" => import_kind = Some(text.to_string()),
                "\\" if module_path.is_none() => leading_absolute = true,
                "qualified_name" => {
                    if module_path.is_none() {
                        module_path = Some(text.to_string());
                    }
                }
                "name" => {
                    if saw_as {
                        alias = Some(text.to_string());
                    } else if module_path.is_none() {
                        module_path = Some(text.to_string());
                    }
                }
                "as" => saw_as = true,
                _ => {}
            }

            if !cursor.goto_next_sibling() {
                break;
            }
        }
    }

    let mut module_path = module_path?;
    if leading_absolute && !module_path.starts_with('\\') {
        module_path.insert(0, '\\');
    }
    if module_path.is_empty() {
        return None;
    }

    Some((module_path, alias, import_kind))
}

pub fn php_import_satisfies_request(imp: &ImportStatement, req: &ImportRequest<'_>) -> bool {
    let ImportForm::Php { clauses } = &imp.form else {
        return false;
    };

    clauses.iter().any(|clause| {
        clause.module_path == req.module_path
            && clause.alias.as_deref() == req.alias
            && clause.import_kind.as_deref() == req.import_kind
    })
}

pub fn php_import_matches_module(imp: &ImportStatement, module: &str) -> bool {
    match &imp.form {
        ImportForm::Php { clauses } => clauses.iter().any(|clause| clause.module_path == module),
        _ => imp.module_path == module,
    }
}

pub fn rewrite_php_import_without_module(
    imp: &ImportStatement,
    module: &str,
) -> Option<Option<String>> {
    let ImportForm::Php { clauses } = &imp.form else {
        return None;
    };
    let remaining: Vec<&PhpImportClause> = clauses
        .iter()
        .filter(|clause| clause.module_path != module)
        .collect();
    if remaining.len() == clauses.len() {
        return None;
    }
    if remaining.is_empty() {
        return Some(None);
    }

    let shared_kind = remaining.first().and_then(|first| {
        first.import_kind.as_deref().filter(|kind| {
            remaining
                .iter()
                .all(|clause| clause.import_kind.as_deref() == Some(*kind))
        })
    });
    let body = remaining
        .iter()
        .map(|clause| {
            let kind = clause
                .import_kind
                .as_deref()
                .filter(|kind| Some(*kind) != shared_kind)
                .map(|kind| format!("{kind} "))
                .unwrap_or_default();
            let alias = clause
                .alias
                .as_deref()
                .map(|alias| format!(" as {alias}"))
                .unwrap_or_default();
            format!("{kind}{}{alias}", clause.module_path)
        })
        .collect::<Vec<_>>()
        .join(", ");
    let kind_prefix = shared_kind
        .map(|kind| format!("{kind} "))
        .unwrap_or_default();
    Some(Some(format!("use {kind_prefix}{body};")))
}

pub fn php_grouped_use_shares_prefix(imp: &ImportStatement, module: &str) -> bool {
    if !php_import_is_grouped(imp) {
        return false;
    }

    let prefix = imp.module_path.trim_matches('\\');
    let module = module.trim_matches('\\');
    module == prefix
        || module
            .strip_prefix(prefix)
            .is_some_and(|rest| rest.starts_with('\\'))
}

pub fn php_grouped_use_matches_module(imp: &ImportStatement, module: &str) -> bool {
    if !php_grouped_use_shares_prefix(imp, module) {
        return false;
    }

    let prefix = imp.module_path.trim_matches('\\');
    let module = module.trim_matches('\\');
    if module == prefix {
        return true;
    }

    let member = module
        .strip_prefix(prefix)
        .and_then(|rest| rest.strip_prefix('\\'))
        .unwrap_or(module);
    imp.names
        .iter()
        .any(|name| super::specifier_matches(name, member))
}

fn php_import_is_grouped(imp: &ImportStatement) -> bool {
    matches!(
        &imp.form,
        ImportForm::Structured { modifiers, .. } if modifiers.iter().any(|modifier| modifier == "group")
    )
}

fn direct_children<'tree>(node: &Node<'tree>, kind: &str) -> Vec<Node<'tree>> {
    let mut children = Vec::new();
    let mut cursor = node.walk();
    if cursor.goto_first_child() {
        loop {
            let child = cursor.node();
            if child.kind() == kind {
                children.push(child);
            }
            if !cursor.goto_next_sibling() {
                break;
            }
        }
    }
    children
}

fn find_direct_child<'tree>(node: &Node<'tree>, kind: &str) -> Option<Node<'tree>> {
    direct_children(node, kind).into_iter().next()
}

pub fn generate_php_import_line(req: &ImportRequest) -> String {
    let mut line = String::from("use ");
    if let Some(kind) = req.import_kind {
        if !kind.is_empty() {
            line.push_str(kind);
            line.push(' ');
        }
    }
    line.push_str(req.module_path);
    if let Some(alias) = req.alias {
        if !alias.is_empty() {
            line.push_str(" as ");
            line.push_str(alias);
        }
    }
    line.push(';');
    line
}

pub struct PhpSyntax;

impl ImportSyntax for PhpSyntax {
    fn parse(&self, source: &str, tree: &Tree) -> ImportBlock {
        parse_php_imports(source, tree)
    }

    fn generate_line(&self, req: &ImportRequest) -> String {
        generate_php_import_line(req)
    }

    fn classify_group(&self, module_path: &str) -> ImportGroup {
        classify_group_php(module_path)
    }
}

pub static PHP_SYNTAX: PhpSyntax = PhpSyntax;