liblevenshtein 0.9.1

Levenshtein/Universal Automata for approximate string matching using various dictionary backends
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
//! File loader for `.llre` files with import resolution.
//!
//! This module handles loading .llre files from disk and resolving @import
//! directives by loading the referenced .llev files.

use std::collections::HashSet;
use std::path::{Path, PathBuf};

use crate::phonetic::llev::{self, LLevFile};

use super::ast::{ImportDirective, LLreFile, ResolvedImport, SymbolTable};
use super::error::{LLreError, LLreErrorKind, LLreResult};
use super::parser;

/// Configuration for the .llre file loader.
#[derive(Debug, Clone)]
pub struct LoaderConfig {
    /// Search paths for @import directives
    pub search_paths: Vec<PathBuf>,

    /// Maximum import depth (to detect circular imports)
    pub max_import_depth: usize,

    /// Whether to resolve imports (false = parse only)
    pub resolve_imports: bool,
}

impl Default for LoaderConfig {
    fn default() -> Self {
        Self {
            search_paths: vec![PathBuf::from(".")],
            max_import_depth: 10,
            resolve_imports: true,
        }
    }
}

impl LoaderConfig {
    /// Create a new loader config with custom search paths.
    pub fn with_search_paths(search_paths: Vec<PathBuf>) -> Self {
        Self {
            search_paths,
            ..Default::default()
        }
    }

    /// Add a search path.
    pub fn add_search_path(&mut self, path: impl Into<PathBuf>) {
        self.search_paths.push(path.into());
    }
}

/// Loader for .llre files.
pub struct Loader {
    config: LoaderConfig,
    /// Tracks files being loaded to detect circular imports
    loading_stack: HashSet<PathBuf>,
}

impl Loader {
    /// Create a new loader with default configuration.
    pub fn new() -> Self {
        Self {
            config: LoaderConfig::default(),
            loading_stack: HashSet::new(),
        }
    }

    /// Create a loader with custom configuration.
    pub fn with_config(config: LoaderConfig) -> Self {
        Self {
            config,
            loading_stack: HashSet::new(),
        }
    }

    /// Load a .llre file from the given path.
    pub fn load<P: AsRef<Path>>(&mut self, path: P) -> LLreResult<LLreFile> {
        let path = path.as_ref();
        let canonical = self.canonicalize_path(path)?;

        // Check for circular imports
        if self.loading_stack.contains(&canonical) {
            return Err(LLreError::new(LLreErrorKind::CircularImport(canonical)));
        }

        // Read the file
        let content = std::fs::read_to_string(path).map_err(|e| {
            LLreError::with_file(
                match e.kind() {
                    std::io::ErrorKind::NotFound => {
                        LLreErrorKind::FileNotFound(path.display().to_string())
                    }
                    std::io::ErrorKind::PermissionDenied => {
                        LLreErrorKind::PermissionDenied(path.display().to_string())
                    }
                    _ => LLreErrorKind::IoError(e.to_string()),
                },
                path,
            )
        })?;

        // Parse the file
        let mut file = parser::parse_str(&content)?;
        file.source_file = Some(canonical.clone());

        // Resolve imports if configured
        if self.config.resolve_imports && !file.imports.is_empty() {
            self.loading_stack.insert(canonical.clone());
            self.resolve_imports(&mut file, path.parent())?;
            self.loading_stack.remove(&canonical);
        }

        Ok(file)
    }

    /// Load a .llre file from a string (no import resolution).
    pub fn load_str(&self, content: &str) -> LLreResult<LLreFile> {
        parser::parse_str(content)
    }

    /// Resolve @import directives in the file.
    fn resolve_imports(&mut self, file: &mut LLreFile, base_dir: Option<&Path>) -> LLreResult<()> {
        let mut resolved_imports = Vec::new();
        let mut symbol_table = SymbolTable::new();

        for import in &file.imports {
            let resolved = self.resolve_import(import, base_dir)?;

            // Merge symbols from the imported file
            symbol_table.merge(&resolved.1);

            resolved_imports.push(ResolvedImport {
                directive: import.clone(),
                resolved_path: resolved.0,
                symbols: resolved.1.symbol_names(),
                rules: Vec::new(), // Rules from .llev files
            });
        }

        file.resolved_imports = resolved_imports;
        file.symbol_table = symbol_table;

        Ok(())
    }

    /// Resolve a single @import directive.
    fn resolve_import(
        &mut self,
        import: &ImportDirective,
        base_dir: Option<&Path>,
    ) -> LLreResult<(PathBuf, SymbolTable)> {
        // Build search paths (base directory first, then configured paths)
        let mut search_paths = Vec::new();
        if let Some(base) = base_dir {
            search_paths.push(base.to_path_buf());
        }
        search_paths.extend(self.config.search_paths.clone());

        // Find the file
        let resolved_path = self.find_file(&import.path, &search_paths).ok_or_else(|| {
            LLreError::with_position(
                LLreErrorKind::ImportNotFound {
                    path: import.path.clone(),
                    search_paths: search_paths.clone(),
                },
                import.position,
            )
        })?;

        // Check import depth
        if self.loading_stack.len() >= self.config.max_import_depth {
            return Err(LLreError::new(LLreErrorKind::ImportDepthExceeded {
                max: self.config.max_import_depth,
                path: resolved_path,
            }));
        }

        // Load the .llev file
        let llev_config = llev::LoaderConfig {
            include_paths: search_paths.clone(),
            max_include_depth: self.config.max_import_depth,
            allow_missing_includes: false,
        };

        let llev_loader = llev::Loader::with_config(llev_config);
        let llev_file = llev_loader
            .load(&resolved_path)
            .map_err(|e| LLreError::from_llev(&e))?;

        // Extract symbols from the .llev file
        let symbol_table = self.extract_symbols(&llev_file, import.alias.as_deref())?;

        Ok((resolved_path, symbol_table))
    }

    /// Find a file in the search paths.
    fn find_file(&self, path: &str, search_paths: &[PathBuf]) -> Option<PathBuf> {
        let path = Path::new(path);

        // If absolute, check directly
        if path.is_absolute() {
            if path.exists() {
                return Some(path.to_path_buf());
            }
            return None;
        }

        // Search in each path
        for base in search_paths {
            let full_path = base.join(path);
            if full_path.exists() {
                return Some(full_path);
            }
        }

        None
    }

    /// Canonicalize a path for comparison.
    fn canonicalize_path(&self, path: &Path) -> LLreResult<PathBuf> {
        std::fs::canonicalize(path).or_else(|_| {
            // If the file doesn't exist yet, just make it absolute
            if path.is_absolute() {
                Ok(path.to_path_buf())
            } else {
                std::env::current_dir()
                    .map(|cwd| cwd.join(path))
                    .map_err(|e| LLreError::new(LLreErrorKind::IoError(e.to_string())))
            }
        })
    }

    /// Extract symbols from an LLev file into a symbol table.
    fn extract_symbols(&self, file: &LLevFile, alias: Option<&str>) -> LLreResult<SymbolTable> {
        let mut table = SymbolTable::new();
        let source = file.source_file.clone();

        for symbol in &file.symbols {
            let name = if let Some(alias) = alias {
                format!("{}_{}", alias, symbol.name)
            } else {
                symbol.name.clone()
            };

            // Convert the symbol expression to a character class
            // This is a simplified extraction - full implementation would
            // evaluate the expression properly
            match &symbol.value {
                llev::Expression::CharClass { chars, .. } => {
                    table.add_char_class(name, chars.clone(), source.clone());
                }
                llev::Expression::Char(c) => {
                    // Single character as a one-element class
                    table.add_char_class(name, vec![*c], source.clone());
                }
                _ => {
                    // For complex expressions, we'd need to evaluate them
                    // For now, skip or convert to simple form
                }
            }
        }

        Ok(table)
    }
}

impl Default for Loader {
    fn default() -> Self {
        Self::new()
    }
}

/// Load a .llre file from disk.
pub fn load_file<P: AsRef<Path>>(path: P) -> LLreResult<LLreFile> {
    let mut loader = Loader::new();
    loader.load(path)
}

/// Load a .llre file with custom configuration.
pub fn load_file_with_config<P: AsRef<Path>>(
    path: P,
    config: LoaderConfig,
) -> LLreResult<LLreFile> {
    let mut loader = Loader::with_config(config);
    loader.load(path)
}

/// Parse a .llre file from a string (no import resolution).
pub fn parse_str(content: &str) -> LLreResult<LLreFile> {
    parser::parse_str(content)
}

#[cfg(test)]
mod tests {
    use super::*;
    use tempfile::TempDir;

    #[test]
    fn test_loader_config_default() {
        let config = LoaderConfig::default();
        assert_eq!(config.search_paths, vec![PathBuf::from(".")]);
        assert_eq!(config.max_import_depth, 10);
        assert!(config.resolve_imports);
    }

    #[test]
    fn test_load_simple_file() {
        let temp_dir = TempDir::new().expect("Failed to create temp dir");
        let file_path = temp_dir.path().join("test.llre");

        let content = r#"
            @name "Test"
            ^hello$
        "#;

        std::fs::write(&file_path, content).expect("Failed to write file");

        let file = load_file(&file_path).expect("Failed to load file");
        assert_eq!(file.metadata.name, Some("Test".to_string()));
        assert!(file.source_file.is_some());
    }

    #[test]
    fn test_load_file_not_found() {
        let result = load_file("/nonexistent/path/test.llre");
        assert!(result.is_err());
        let err = result.unwrap_err();
        assert!(matches!(err.kind, LLreErrorKind::FileNotFound(_)));
    }

    #[test]
    fn test_load_with_import() {
        let temp_dir = TempDir::new().expect("Failed to create temp dir");

        // Create a .llev file with symbols
        let llev_path = temp_dir.path().join("symbols.llev");
        let llev_content = r#"
            @name "Symbols"
            @define VOWEL = [aeiou]
        "#;
        std::fs::write(&llev_path, llev_content).expect("Failed to write llev file");

        // Create a .llre file that imports it
        let llre_path = temp_dir.path().join("test.llre");
        let llre_content = r#"
            @import "symbols.llev"
            ^[a-z]+$
        "#;
        std::fs::write(&llre_path, llre_content).expect("Failed to write llre file");

        // Configure loader with the temp directory in search paths
        let config = LoaderConfig {
            search_paths: vec![temp_dir.path().to_path_buf()],
            ..Default::default()
        };

        let file = load_file_with_config(&llre_path, config).expect("Failed to load file");
        assert_eq!(file.imports.len(), 1);
        assert_eq!(file.resolved_imports.len(), 1);
        // The VOWEL symbol should be in the symbol table
        assert!(file.symbol_table.contains("VOWEL"));
    }

    #[test]
    fn test_load_with_aliased_import() {
        let temp_dir = TempDir::new().expect("Failed to create temp dir");

        // Create a .llev file with symbols
        let llev_path = temp_dir.path().join("english.llev");
        let llev_content = r#"
            @name "English"
            @define VOWEL = [aeiou]
        "#;
        std::fs::write(&llev_path, llev_content).expect("Failed to write llev file");

        // Create a .llre file that imports it with alias
        let llre_path = temp_dir.path().join("test.llre");
        let llre_content = r#"
            @import "english.llev" as en
            ^[a-z]+$
        "#;
        std::fs::write(&llre_path, llre_content).expect("Failed to write llre file");

        let config = LoaderConfig {
            search_paths: vec![temp_dir.path().to_path_buf()],
            ..Default::default()
        };

        let file = load_file_with_config(&llre_path, config).expect("Failed to load file");

        // The symbol should be prefixed with the alias
        assert!(file.symbol_table.contains("en_VOWEL"));
        assert!(!file.symbol_table.contains("VOWEL"));
    }

    #[test]
    fn test_import_not_found() {
        let temp_dir = TempDir::new().expect("Failed to create temp dir");

        // Create a .llre file that imports a nonexistent file
        let llre_path = temp_dir.path().join("test.llre");
        let llre_content = r#"
            @import "nonexistent.llev"
            ^test$
        "#;
        std::fs::write(&llre_path, llre_content).expect("Failed to write llre file");

        let result = load_file(&llre_path);
        assert!(result.is_err());
        let err = result.unwrap_err();
        assert!(matches!(err.kind, LLreErrorKind::ImportNotFound { .. }));
    }

    #[test]
    fn test_parse_str_no_imports() {
        let content = r#"
            @name "Test"
            @import "symbols.llev"  # This won't be resolved
            ^hello$
        "#;

        // parse_str doesn't resolve imports
        let file = parse_str(content).expect("Failed to parse");
        assert_eq!(file.imports.len(), 1);
        // Imports are parsed but not resolved
        assert!(file.resolved_imports.is_empty());
    }
}