Skip to main content

sbpf_assembler/preprocessor/
mod.rs

1pub mod expand;
2pub mod include;
3pub mod macro_def;
4pub mod source_map;
5
6use {
7    crate::errors::CompileError,
8    source_map::{FileRegistry, SourceMap, SourceOrigin},
9    std::path::PathBuf,
10};
11
12/// A line of source with its origin tracking
13#[derive(Debug, Clone)]
14pub(crate) struct SourceLine {
15    pub text: String,
16    pub origin: SourceOrigin,
17}
18
19/// Trait for resolving `.include` file paths to their contents.
20/// Abstracted as a trait so tests can use an in-memory mock.
21pub trait FileResolver {
22    /// Resolve the filename used for nested includes and cycle detection.
23    /// The returned name must also be accepted by `resolve`.
24    /// Resolvers with a flat virtual namespace may keep the default behavior.
25    fn resolve_path(&self, path: &str, _relative_to: &str) -> Result<String, std::io::Error> {
26        Ok(path.to_string())
27    }
28
29    /// Resolve an include path relative to the including file's directory.
30    /// Returns the file contents.
31    fn resolve(&self, path: &str, relative_to: &str) -> Result<String, std::io::Error>;
32}
33
34/// File resolver that reads from the real filesystem.
35#[derive(Debug, Clone)]
36pub struct FsFileResolver {
37    /// Additional directories to search for includes
38    pub include_paths: Vec<PathBuf>,
39}
40
41impl FsFileResolver {
42    pub fn new() -> Self {
43        Self {
44            include_paths: Vec::new(),
45        }
46    }
47
48    pub fn with_include_paths(include_paths: Vec<PathBuf>) -> Self {
49        Self { include_paths }
50    }
51}
52
53impl Default for FsFileResolver {
54    fn default() -> Self {
55        Self::new()
56    }
57}
58
59impl FileResolver for FsFileResolver {
60    fn resolve(&self, path: &str, relative_to: &str) -> Result<String, std::io::Error> {
61        std::fs::read_to_string(self.resolve_path(path, relative_to)?)
62    }
63
64    fn resolve_path(&self, path: &str, relative_to: &str) -> Result<String, std::io::Error> {
65        // Canonical names keep aliases such as `../` from bypassing cycle detection.
66        let resolved_name = |candidate: PathBuf| {
67            candidate
68                .canonicalize()?
69                .into_os_string()
70                .into_string()
71                .map_err(|_| {
72                    std::io::Error::new(
73                        std::io::ErrorKind::InvalidData,
74                        "include path is not UTF-8",
75                    )
76                })
77        };
78        // Try relative to the including file's directory first
79        let base_dir = std::path::Path::new(relative_to)
80            .parent()
81            .unwrap_or(std::path::Path::new("."));
82        let candidate = base_dir.join(path);
83        if candidate.exists() {
84            return resolved_name(candidate);
85        }
86
87        // Try each include path
88        for include_dir in &self.include_paths {
89            let candidate = include_dir.join(path);
90            if candidate.exists() {
91                return resolved_name(candidate);
92            }
93        }
94
95        Err(std::io::Error::new(
96            std::io::ErrorKind::NotFound,
97            format!("file not found: {}", path),
98        ))
99    }
100}
101
102/// In-memory file resolver for testing
103#[derive(Debug, Clone, Default)]
104pub struct MockFileResolver {
105    files: std::collections::HashMap<String, String>,
106}
107
108impl MockFileResolver {
109    pub fn new() -> Self {
110        Self::default()
111    }
112
113    pub fn add_file(&mut self, path: &str, content: &str) -> &mut Self {
114        self.files.insert(path.to_string(), content.to_string());
115        self
116    }
117}
118
119impl FileResolver for MockFileResolver {
120    fn resolve_path(&self, path: &str, relative_to: &str) -> Result<String, std::io::Error> {
121        Ok(std::path::Path::new(relative_to)
122            .parent()
123            .unwrap_or(std::path::Path::new(""))
124            .join(path)
125            .to_string_lossy()
126            .into_owned())
127    }
128
129    fn resolve(&self, path: &str, _relative_to: &str) -> Result<String, std::io::Error> {
130        self.files.get(path).cloned().ok_or_else(|| {
131            std::io::Error::new(
132                std::io::ErrorKind::NotFound,
133                format!("file not found: {}", path),
134            )
135        })
136    }
137}
138
139/// Result of preprocessing: expanded source text + a source map for diagnostics
140pub struct PreprocessResult {
141    pub expanded_source: String,
142    pub source_map: SourceMap,
143}
144
145/// A preprocessor error paired with its source origin.
146#[derive(Debug)]
147pub struct PreprocessorError {
148    pub error: CompileError,
149    pub origin: Option<SourceOrigin>,
150}
151
152/// Returned on preprocessing failure: errors + the file registry
153/// (so the caller can still render diagnostics against original files).
154pub struct PreprocessFailure {
155    pub errors: Vec<PreprocessorError>,
156    pub file_registry: FileRegistry,
157}
158
159/// Run the full preprocessor pipeline:
160/// 1. Resolve `.include` directives (flatten files)
161/// 2. Expand `.macro`/`.endm`, `.rept`/`.endr`, `.irp`/`.endr`,
162///    `.if`/`.else`/`.endif`
163///
164/// The resulting `expanded_source` can be fed directly to the pest parser.
165/// The `source_map` allows remapping pest error spans back to original locations.
166pub fn preprocess(
167    source: &str,
168    source_path: &str,
169    resolver: Option<&dyn FileResolver>,
170) -> Result<PreprocessResult, PreprocessFailure> {
171    let mut registry = FileRegistry::new();
172
173    // Pass 1: Include resolution
174    let lines = match include::resolve_includes(source, source_path, resolver, &mut registry) {
175        Ok(lines) => lines,
176        Err(errors) => {
177            return Err(PreprocessFailure {
178                errors: errors
179                    .into_iter()
180                    .map(|e| PreprocessorError {
181                        error: e,
182                        origin: None,
183                    })
184                    .collect(),
185                file_registry: registry,
186            });
187        }
188    };
189
190    // Pass 2: Macro expansion
191    let (expanded_lines, errors) = match expand::expand_macros(lines) {
192        Ok(result) => result,
193        Err(errors) => {
194            return Err(PreprocessFailure {
195                errors: errors
196                    .into_iter()
197                    .map(|e| PreprocessorError {
198                        error: e.error,
199                        origin: e.origin,
200                    })
201                    .collect(),
202                file_registry: registry,
203            });
204        }
205    };
206
207    if !errors.is_empty() {
208        return Err(PreprocessFailure {
209            errors: errors
210                .into_iter()
211                .map(|e| PreprocessorError {
212                    error: e.error,
213                    origin: e.origin,
214                })
215                .collect(),
216            file_registry: registry,
217        });
218    }
219
220    // Build the expanded source string and source map
221    let mut expanded_source = String::new();
222    let mut line_origins = Vec::with_capacity(expanded_lines.len());
223
224    for line in &expanded_lines {
225        expanded_source.push_str(&line.text);
226        expanded_source.push('\n');
227        line_origins.push(line.origin.clone());
228    }
229
230    let source_map = SourceMap::new(registry, line_origins);
231
232    Ok(PreprocessResult {
233        expanded_source,
234        source_map,
235    })
236}