Skip to main content

weavatrix_parse/python/
mod.rs

1//! Structural extraction for Python.
2//!
3//! Python scopes by indentation rather than braces, so the walk tracks the
4//! column a declaration was written at and closes it when a later declaration
5//! appears at the same column or further left. Working from token columns
6//! rather than raw line prefixes keeps this correct inside triple-quoted
7//! strings, where a line that looks like `def x():` is text, not code.
8
9use crate::facts::{
10    Declaration, DeclarationKind, Facts, Import, ImportBinding, Reference, ReferenceKind, Span,
11};
12use crate::syntax::Language;
13use crate::token::{Mode, Token, TokenKind, Tokenizer};
14
15/// Extracts structural facts from one Python source.
16#[must_use]
17pub fn extract(source: &str) -> Facts {
18    let tokens = Tokenizer::new(source, Language::Python)
19        .mode(Mode::Lite)
20        .collect::<Vec<_>>();
21    let mut state = Extractor {
22        source,
23        tokens: &tokens,
24        facts: Facts::default(),
25        scopes: Vec::new(),
26    };
27    state.run();
28    state.facts
29}
30
31/// A `def` or `class` whose indented body the walk is inside.
32struct Scope {
33    name: String,
34    column: u32,
35}
36
37struct Extractor<'source, 'tokens> {
38    source: &'source str,
39    tokens: &'tokens [Token],
40    facts: Facts,
41    scopes: Vec<Scope>,
42}
43
44mod extractor;
45
46#[cfg(test)]
47mod tests;