Skip to main content

fallow_extract/
module_parse.rs

1//! Public entry points that parse one source into a [`ModuleInfo`] on a stack
2//! that is large enough for it.
3
4use std::path::Path;
5
6use fallow_types::discover::FileId;
7use fallow_types::extract::ModuleInfo;
8
9use crate::parse::parse_source_to_module_unbounded;
10use fallow_types::extract::FlagPatterns;
11
12/// Extraction settings that must agree with the caller's cache identity.
13pub struct ExtractionOptions<'a> {
14    pub flag_patterns: &'a FlagPatterns,
15    pub mode: fallow_types::shared_state::ExtractionMode,
16    /// Collect the call facts (`ModuleInfo::function_contracts`). A run
17    /// whose rules do not read them leaves this off, and each module then
18    /// has `function_contracts: None`.
19    pub call_facts: bool,
20    /// Walk the program for feature flags (`ModuleInfo::flag_uses` and
21    /// `ModuleInfo::flag_registry_facts`). Off gives no flag facts.
22    pub flags: bool,
23    /// Collect the component contracts (`ModuleInfo::component_contracts`).
24    /// Off gives `component_contracts: None`.
25    pub component_contracts: bool,
26}
27
28impl ExtractionOptions<'_> {
29    pub const fn gates(&self) -> ExtractionGates {
30        ExtractionGates {
31            mode: self.mode,
32            call_facts: self.call_facts,
33            component_contracts: self.component_contracts,
34        }
35    }
36}
37
38/// The optional semantic facts that a parse collects, carried from
39/// [`ExtractionOptions`] to the semantic pass of each embedded script.
40#[derive(Clone, Copy)]
41pub struct ExtractionGates {
42    pub mode: fallow_types::shared_state::ExtractionMode,
43    pub call_facts: bool,
44    pub component_contracts: bool,
45}
46
47#[cfg(test)]
48impl ExtractionGates {
49    /// Every fact in the standard mode, for tests that expect a full module.
50    pub const STANDARD: Self = Self {
51        mode: fallow_types::shared_state::ExtractionMode::Standard,
52        call_facts: true,
53        component_contracts: true,
54    };
55}
56
57/// Parse source text into a [`ModuleInfo`].
58///
59/// When `need_complexity` is false the per-function complexity visitor is
60/// skipped, saving one full AST walk per file.  The dead-code analysis
61/// pipeline never consumes complexity data, so callers that only need
62/// imports/exports should pass `false`.
63///
64/// Flag detection uses the built-in patterns only; see
65/// [`parse_source_to_module_with_flags`].
66pub fn parse_source_to_module(
67    file_id: FileId,
68    path: &Path,
69    source: &str,
70    content_hash: u64,
71    need_complexity: bool,
72) -> ModuleInfo {
73    parse_source_to_module_with_flags(
74        file_id,
75        path,
76        source,
77        content_hash,
78        need_complexity,
79        &FlagPatterns::default(),
80    )
81}
82
83/// Parse source text into a [`ModuleInfo`], with the user flag patterns
84/// applied on top of the built-in ones.
85///
86/// A leading byte order mark is removed, and the spans count from the end of
87/// it.
88pub fn parse_source_to_module_with_flags(
89    file_id: FileId,
90    path: &Path,
91    source: &str,
92    content_hash: u64,
93    need_complexity: bool,
94    flag_patterns: &FlagPatterns,
95) -> ModuleInfo {
96    parse_source_without_bom(
97        file_id,
98        path,
99        crate::strip_bom(source),
100        content_hash,
101        need_complexity,
102        &ExtractionOptions {
103            flag_patterns,
104            mode: fallow_types::shared_state::ExtractionMode::Standard,
105            call_facts: true,
106            flags: true,
107            component_contracts: true,
108        },
109    )
110}
111
112/// [`parse_source_to_module_with_flags`] for a source that a caller already
113/// read without its byte order mark.
114///
115/// Only the first U+FEFF of a file is the mark. Thus this function must not
116/// remove a U+FEFF again: a second U+FEFF is source text, and the spans
117/// count it.
118pub fn parse_source_without_bom(
119    file_id: FileId,
120    path: &Path,
121    source: &str,
122    content_hash: u64,
123    need_complexity: bool,
124    options: &ExtractionOptions<'_>,
125) -> ModuleInfo {
126    let mut module = parse_source_bounded(
127        file_id,
128        path,
129        source,
130        content_hash,
131        need_complexity,
132        options,
133    );
134    if !options.call_facts {
135        // The visitor and the HTML host mark call facts outside the gated
136        // collection, so an empty marker must not reach a run without them.
137        module.function_contracts = None;
138    }
139    if !options.component_contracts {
140        // The HTML host collects its template contracts outside the gate.
141        module.component_contracts = None;
142    }
143    module
144}
145
146fn parse_source_bounded(
147    file_id: FileId,
148    path: &Path,
149    source: &str,
150    content_hash: u64,
151    need_complexity: bool,
152    options: &ExtractionOptions<'_>,
153) -> ModuleInfo {
154    let bounded = crate::nesting::parse_file_bounded(path, source, || {
155        let module = parse_source_to_module_unbounded(
156            file_id,
157            path,
158            source,
159            content_hash,
160            need_complexity,
161            options,
162        );
163        // The CSS comment mask counts on the thread that parsed. Carry the
164        // count back in case that was a sized thread.
165        (module, crate::css_masked_bytes::take())
166    });
167    match bounded {
168        Some((module, masked_bytes)) => {
169            crate::css_masked_bytes::add(masked_bytes);
170            module
171        }
172        None => crate::nesting::too_deep_module(file_id, source, content_hash),
173    }
174}
175
176/// Parse source with explicit optional semantic facts.
177#[cfg(test)]
178pub fn parse_source_to_module_with_mode(
179    file_id: FileId,
180    path: &Path,
181    source: &str,
182    content_hash: u64,
183    need_complexity: bool,
184    mode: fallow_types::shared_state::ExtractionMode,
185) -> ModuleInfo {
186    parse_source_without_bom(
187        file_id,
188        path,
189        crate::strip_bom(source),
190        content_hash,
191        need_complexity,
192        &ExtractionOptions {
193            flag_patterns: &FlagPatterns::default(),
194            mode,
195            call_facts: true,
196            flags: true,
197            component_contracts: true,
198        },
199    )
200}