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}
17
18/// Parse source text into a [`ModuleInfo`].
19///
20/// When `need_complexity` is false the per-function complexity visitor is
21/// skipped, saving one full AST walk per file.  The dead-code analysis
22/// pipeline never consumes complexity data, so callers that only need
23/// imports/exports should pass `false`.
24///
25/// Flag detection uses the built-in patterns only; see
26/// [`parse_source_to_module_with_flags`].
27pub fn parse_source_to_module(
28    file_id: FileId,
29    path: &Path,
30    source: &str,
31    content_hash: u64,
32    need_complexity: bool,
33) -> ModuleInfo {
34    parse_source_to_module_with_flags(
35        file_id,
36        path,
37        source,
38        content_hash,
39        need_complexity,
40        &FlagPatterns::default(),
41    )
42}
43
44/// Parse source text into a [`ModuleInfo`], with the user flag patterns
45/// applied on top of the built-in ones.
46///
47/// A leading byte order mark is removed, and the spans count from the end of
48/// it.
49pub fn parse_source_to_module_with_flags(
50    file_id: FileId,
51    path: &Path,
52    source: &str,
53    content_hash: u64,
54    need_complexity: bool,
55    flag_patterns: &FlagPatterns,
56) -> ModuleInfo {
57    parse_source_without_bom(
58        file_id,
59        path,
60        crate::strip_bom(source),
61        content_hash,
62        need_complexity,
63        &ExtractionOptions {
64            flag_patterns,
65            mode: fallow_types::shared_state::ExtractionMode::Standard,
66        },
67    )
68}
69
70/// [`parse_source_to_module_with_flags`] for a source that a caller already
71/// read without its byte order mark.
72///
73/// Only the first U+FEFF of a file is the mark. Thus this function must not
74/// remove a U+FEFF again: a second U+FEFF is source text, and the spans
75/// count it.
76pub fn parse_source_without_bom(
77    file_id: FileId,
78    path: &Path,
79    source: &str,
80    content_hash: u64,
81    need_complexity: bool,
82    options: &ExtractionOptions<'_>,
83) -> ModuleInfo {
84    let bounded = crate::nesting::parse_file_bounded(path, source, || {
85        let module = parse_source_to_module_unbounded(
86            file_id,
87            path,
88            source,
89            content_hash,
90            need_complexity,
91            options,
92        );
93        // The CSS comment mask counts on the thread that parsed. Carry the
94        // count back in case that was a sized thread.
95        (module, crate::css_masked_bytes::take())
96    });
97    match bounded {
98        Some((module, masked_bytes)) => {
99            crate::css_masked_bytes::add(masked_bytes);
100            module
101        }
102        None => crate::nesting::too_deep_module(file_id, source, content_hash),
103    }
104}
105
106/// Parse source with explicit optional semantic facts.
107#[cfg(test)]
108pub fn parse_source_to_module_with_mode(
109    file_id: FileId,
110    path: &Path,
111    source: &str,
112    content_hash: u64,
113    need_complexity: bool,
114    mode: fallow_types::shared_state::ExtractionMode,
115) -> ModuleInfo {
116    parse_source_without_bom(
117        file_id,
118        path,
119        crate::strip_bom(source),
120        content_hash,
121        need_complexity,
122        &ExtractionOptions {
123            flag_patterns: &FlagPatterns::default(),
124            mode,
125        },
126    )
127}