rumoca 0.7.28

Modelica compiler written in RUST
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
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
//! High-level API for compiling Modelica models to DAE representations.
//!
//! This module provides a clean, ergonomic interface for using rumoca as a library.
//! The main entry point is the [`Compiler`] struct, which uses a builder pattern
//! for configuration.
//!
//! # Examples
//!
//! Basic usage:
//!
//! ```no_run
//! use rumoca::Compiler;
//!
//! let result = Compiler::new()
//!     .model("MyModel")
//!     .compile_file("model.mo")?;
//! # Ok::<(), anyhow::Error>(())
//! ```
//!
//! With verbose output and template rendering:
//!
//! ```no_run
//! use rumoca::Compiler;
//!
//! let output = Compiler::new()
//!     .model("MyModel")
//!     .verbose(true)
//!     .compile_file("model.mo")?
//!     .render_template("template.j2")?;
//! # Ok::<(), anyhow::Error>(())
//! ```
//!
//! Compiling from a string:
//!
//! ```no_run
//! use rumoca::Compiler;
//!
//! let modelica_code = r#"
//!     model Integrator
//!         Real x(start=0);
//!     equation
//!         der(x) = 1;
//!     end Integrator;
//! "#;
//!
//! let result = Compiler::new()
//!     .model("Integrator")
//!     .compile_str(modelica_code, "Integrator.mo")?;
//! # Ok::<(), anyhow::Error>(())
//! ```

pub mod cache;
mod error_handling;
mod function_collector;
pub mod pipeline;
mod result;

pub use error_handling::extract_parse_error;
pub use result::CompilationResult;

use crate::ir::ast::StoredDefinition;
use crate::modelica_grammar::ModelicaGrammar;
use crate::modelica_parser::parse;
use anyhow::{Context, Result};
use error_handling::create_syntax_error;
use indexmap::IndexSet;
#[cfg(not(target_arch = "wasm32"))]
use rayon::prelude::*;
use std::fs;
use std::path::{Path, PathBuf};
use std::sync::Arc;

// Use web_time on WASM for Instant::now() polyfill
#[cfg(not(target_arch = "wasm32"))]
use std::time::Instant;
#[cfg(target_arch = "wasm32")]
use web_time::Instant;

// ============================================================================
// Standalone parsing functions (used by both compiler and LSP)
// ============================================================================

/// Parse Modelica source code and return the AST.
///
/// This is a low-level parsing function that returns `None` on parse errors.
/// For error details, use [`parse_source`] instead.
///
/// # Arguments
/// * `source` - The Modelica source code
/// * `file_name` - The file name (used for error messages and location tracking)
///
/// # Returns
/// `Some(StoredDefinition)` if parsing succeeded, `None` otherwise.
pub fn parse_source_simple(source: &str, file_name: &str) -> Option<StoredDefinition> {
    let mut grammar = ModelicaGrammar::new();
    if parse(source, file_name, &mut grammar).is_ok() {
        grammar.modelica
    } else {
        None
    }
}

/// Parse Modelica source code and return the AST with detailed errors.
///
/// # Arguments
/// * `source` - The Modelica source code
/// * `file_name` - The file name (used for error messages and location tracking)
///
/// # Returns
/// `Ok(StoredDefinition)` if parsing succeeded, `Err` with detailed error otherwise.
pub fn parse_source(source: &str, file_name: &str) -> Result<StoredDefinition> {
    let mut grammar = ModelicaGrammar::new();
    if let Err(e) = parse(source, file_name, &mut grammar) {
        let diagnostic = create_syntax_error(&e, source);
        let report = miette::Report::new(diagnostic);
        return Err(anyhow::anyhow!("{:?}", report));
    }

    grammar
        .modelica
        .ok_or_else(|| anyhow::anyhow!("Parser succeeded but produced no AST for {}", file_name))
}

/// Parse a Modelica file from disk, using disk cache if available.
///
/// This function checks the AST cache (`~/.cache/rumoca/ast/`) first.
/// If the file hasn't changed since the last parse, the cached AST is returned.
/// Otherwise, the file is parsed and the result is cached for future use.
///
/// # Arguments
/// * `path` - Path to the Modelica file
///
/// # Returns
/// `Some(StoredDefinition)` if parsing succeeded, `None` otherwise.
pub fn parse_file_cached(path: &Path) -> Option<StoredDefinition> {
    // Compute file hash for cache lookup
    let file_hash = cache::compute_file_hash(path).ok()?;

    // Try cache first
    if let Some(ast) = cache::load_cached_ast(path, &file_hash) {
        return Some(ast);
    }

    // Cache miss - read and parse the file
    let text = fs::read_to_string(path).ok()?;
    let path_str = path.to_string_lossy().to_string();

    let ast = parse_source_simple(&text, &path_str)?;

    // Store in cache for next time
    let _ = cache::store_cached_ast(path, &file_hash, &ast);

    Some(ast)
}

/// Parse a Modelica file from disk with detailed errors, using disk cache if available.
///
/// Like [`parse_file_cached`] but returns detailed error information on failure.
///
/// # Arguments
/// * `path` - Path to the Modelica file
///
/// # Returns
/// `Ok(StoredDefinition)` if parsing succeeded, `Err` with detailed error otherwise.
pub fn parse_file_cached_result(path: &Path) -> Result<StoredDefinition> {
    // Compute file hash for cache lookup
    let file_hash = cache::compute_file_hash(path)?;

    // Try cache first
    if let Some(ast) = cache::load_cached_ast(path, &file_hash) {
        return Ok(ast);
    }

    // Cache miss - read and parse the file
    let text = fs::read_to_string(path)
        .with_context(|| format!("Failed to read file: {}", path.display()))?;
    let path_str = path.to_string_lossy().to_string();

    let ast = parse_source(&text, &path_str)?;

    // Store in cache for next time
    let _ = cache::store_cached_ast(path, &file_hash, &ast);

    Ok(ast)
}

/// A high-level compiler for Modelica models.
///
/// This struct provides a builder-pattern interface for configuring and executing
/// the compilation pipeline from Modelica source code to DAE representation.
///
/// # Examples
///
/// ```no_run
/// use rumoca::Compiler;
///
/// let result = Compiler::new()
///     .model("MyModel")
///     .verbose(true)
///     .compile_file("model.mo")?;
/// # Ok::<(), anyhow::Error>(())
/// ```
#[derive(Debug, Clone)]
pub struct Compiler {
    verbose: bool,
    /// Main model/class name to simulate (required)
    model_name: Option<String>,
    /// Additional source files to include in compilation (deduplicated by canonical path)
    additional_files: IndexSet<PathBuf>,
    /// Explicit library paths (overrides MODELICAPATH env var if set)
    modelica_paths: Vec<std::path::PathBuf>,
    /// Number of threads for parallel parsing (None = 50% of cores)
    threads: Option<usize>,
    /// Enable AST caching for faster library loading (default: true)
    use_cache: bool,
}

impl Default for Compiler {
    fn default() -> Self {
        Self {
            verbose: false,
            model_name: None,
            additional_files: IndexSet::new(),
            modelica_paths: Vec::new(),
            threads: None,   // Will use 50% of cores
            use_cache: true, // Enable caching by default
        }
    }
}

impl Compiler {
    /// Creates a new compiler with default settings.
    ///
    /// # Examples
    ///
    /// ```
    /// use rumoca::Compiler;
    ///
    /// let compiler = Compiler::new();
    /// ```
    pub fn new() -> Self {
        Self::default()
    }

    /// Enables or disables verbose output during compilation.
    ///
    /// When enabled, the compiler will print timing information and intermediate
    /// representations to stdout.
    ///
    /// # Examples
    ///
    /// ```
    /// use rumoca::Compiler;
    ///
    /// let compiler = Compiler::new().verbose(true);
    /// ```
    pub fn verbose(mut self, verbose: bool) -> Self {
        self.verbose = verbose;
        self
    }

    /// Sets the main model/class name to simulate (required).
    ///
    /// According to the Modelica specification, the user must specify which
    /// class (of specialized class `model` or `block`) to simulate.
    ///
    /// # Examples
    ///
    /// ```
    /// use rumoca::Compiler;
    ///
    /// let compiler = Compiler::new().model("MyModel");
    /// ```
    pub fn model(mut self, name: &str) -> Self {
        self.model_name = Some(name.to_string());
        self
    }

    /// Sets explicit library search paths (overrides MODELICAPATH environment variable).
    ///
    /// This allows specifying library paths programmatically without relying on
    /// environment variables, which is useful for testing and reproducible builds.
    ///
    /// # Examples
    ///
    /// ```no_run
    /// use rumoca::Compiler;
    ///
    /// let result = Compiler::new()
    ///     .model("Modelica.Blocks.Examples.PID_Controller")
    ///     .modelica_path(&["/path/to/MSL", "/path/to/other/libs"])
    ///     .include_from_modelica_path("Modelica")?
    ///     .compile_file("model.mo")?;
    /// # Ok::<(), anyhow::Error>(())
    /// ```
    pub fn modelica_path(mut self, paths: &[&str]) -> Self {
        self.modelica_paths = paths.iter().map(std::path::PathBuf::from).collect();
        self
    }

    /// Sets the number of threads for parallel file parsing.
    ///
    /// By default, uses 50% of available CPU cores to leave resources for other tasks.
    /// Set to 1 for single-threaded parsing, or a higher number to use more cores.
    ///
    /// # Examples
    ///
    /// ```
    /// use rumoca::Compiler;
    ///
    /// // Use all available cores
    /// let compiler = Compiler::new().threads(num_cpus::get());
    ///
    /// // Use single-threaded parsing
    /// let compiler = Compiler::new().threads(1);
    /// ```
    pub fn threads(mut self, threads: usize) -> Self {
        self.threads = Some(threads.max(1));
        self
    }

    /// Returns the number of threads to use for parallel parsing.
    /// Defaults to (num_cpus - 1) to leave one core free for the system, minimum 1.
    ///
    /// If called from within an existing rayon thread pool (nested parallelism),
    /// returns 1 to avoid thread explosion.
    #[cfg(not(target_arch = "wasm32"))]
    fn get_thread_count(&self) -> usize {
        // If explicitly set, use that value
        if let Some(threads) = self.threads {
            return threads;
        }

        // Detect if we're already inside a rayon pool (nested parallelism)
        // rayon::current_num_threads() returns 1 when not in a pool
        let in_pool = rayon::current_num_threads() > 1;
        if in_pool {
            // We're inside a parallel context - use single thread to avoid explosion
            return 1;
        }

        // Default: use num_cpus - 1, minimum 1
        std::cmp::max(1, num_cpus::get().saturating_sub(1))
    }

    /// Enables or disables AST caching.
    ///
    /// When enabled (default), parsed ASTs are cached to `~/.cache/rumoca/ast/`
    /// to speed up subsequent compilations of the same files.
    ///
    /// # Examples
    ///
    /// ```
    /// use rumoca::Compiler;
    ///
    /// // Disable caching
    /// let compiler = Compiler::new().cache(false);
    /// ```
    pub fn cache(mut self, enable: bool) -> Self {
        self.use_cache = enable;
        self
    }

    /// Adds an additional source file to include in compilation.
    ///
    /// Use this to include library files, package definitions, or other
    /// dependencies that the main model requires. Files are automatically
    /// deduplicated by canonical path.
    ///
    /// # Examples
    ///
    /// ```no_run
    /// use rumoca::Compiler;
    ///
    /// let result = Compiler::new()
    ///     .model("MyModel")
    ///     .include("library/utils.mo")
    ///     .include("library/types.mo")
    ///     .compile_file("model.mo")?;
    /// # Ok::<(), anyhow::Error>(())
    /// ```
    pub fn include(mut self, path: &str) -> Self {
        let path_buf = PathBuf::from(path);
        // Canonicalize to detect duplicates (same file via different paths)
        let canonical = path_buf.canonicalize().unwrap_or(path_buf);
        self.additional_files.insert(canonical);
        self
    }

    /// Adds multiple source files to include in compilation.
    ///
    /// # Examples
    ///
    /// ```no_run
    /// use rumoca::Compiler;
    ///
    /// let result = Compiler::new()
    ///     .model("MyModel")
    ///     .include_all(&["lib1.mo", "lib2.mo"])
    ///     .compile_file("model.mo")?;
    /// # Ok::<(), anyhow::Error>(())
    /// ```
    pub fn include_all(mut self, paths: &[&str]) -> Self {
        for path in paths {
            self = self.include(path);
        }
        self
    }

    /// Includes a Modelica package directory in compilation.
    ///
    /// This method discovers all Modelica files in a package directory structure,
    /// following Modelica Spec 13.4 conventions:
    /// - Directories with `package.mo` are treated as packages
    /// - `package.order` files specify the order of nested entities
    /// - Single `.mo` files define classes
    ///
    /// # Examples
    ///
    /// ```no_run
    /// use rumoca::Compiler;
    ///
    /// let result = Compiler::new()
    ///     .model("MyPackage.MyModel")
    ///     .include_package("path/to/MyPackage")?
    ///     .compile_file("model.mo")?;
    /// # Ok::<(), anyhow::Error>(())
    /// ```
    pub fn include_package(mut self, path: &str) -> Result<Self> {
        use crate::ir::transform::multi_file::discover_modelica_files;

        let package_path = std::path::Path::new(path);
        let files = discover_modelica_files(package_path)?;

        for file in files {
            // Canonicalize to detect duplicates
            let canonical = file.canonicalize().unwrap_or(file);
            self.additional_files.insert(canonical);
        }

        Ok(self)
    }

    /// Includes a package from MODELICAPATH by name.
    ///
    /// This method searches the library paths for a package with the given name
    /// and includes all its files. If explicit paths were set via `.modelica_path()`,
    /// those are used; otherwise, the MODELICAPATH environment variable is used.
    ///
    /// According to Modelica Spec 13.3, MODELICAPATH is an ordered list of library
    /// root directories, separated by `:` on Unix or `;` on Windows.
    ///
    /// # Examples
    ///
    /// ```no_run
    /// use rumoca::Compiler;
    ///
    /// // Using explicit paths (recommended for reproducible builds)
    /// let result = Compiler::new()
    ///     .model("Modelica.Mechanics.Rotational.Examples.First")
    ///     .modelica_path(&["/path/to/MSL"])
    ///     .include_from_modelica_path("Modelica")?
    ///     .compile_file("model.mo")?;
    ///
    /// // Or using MODELICAPATH environment variable
    /// let result = Compiler::new()
    ///     .model("Modelica.Mechanics.Rotational.Examples.First")
    ///     .include_from_modelica_path("Modelica")?
    ///     .compile_file("model.mo")?;
    /// # Ok::<(), anyhow::Error>(())
    /// ```
    pub fn include_from_modelica_path(self, package_name: &str) -> Result<Self> {
        use crate::ir::transform::multi_file::{find_package_in_paths, get_modelica_path};

        // Use explicit paths if set, otherwise fall back to env var
        let search_paths = if self.modelica_paths.is_empty() {
            get_modelica_path()
        } else {
            self.modelica_paths.clone()
        };

        let package_path = find_package_in_paths(package_name, &search_paths).ok_or_else(|| {
            anyhow::anyhow!(
                "Package '{}' not found in library paths: {:?}",
                package_name,
                search_paths
            )
        })?;

        self.include_package(&package_path.to_string_lossy())
    }

    /// Compiles a Modelica package directory directly.
    ///
    /// This method discovers all files in a package directory structure and
    /// compiles them together. The main model to simulate is specified via `.model()`.
    ///
    /// # Examples
    ///
    /// ```no_run
    /// use rumoca::Compiler;
    ///
    /// let result = Compiler::new()
    ///     .model("MyPackage.MyModel")
    ///     .compile_package("path/to/MyPackage")?;
    /// # Ok::<(), anyhow::Error>(())
    /// ```
    #[cfg(not(target_arch = "wasm32"))]
    pub fn compile_package(&self, path: &str) -> Result<CompilationResult> {
        use crate::ir::transform::multi_file::discover_modelica_files;

        let package_path = std::path::Path::new(path);
        let files = discover_modelica_files(package_path)?;

        if files.is_empty() {
            anyhow::bail!("No Modelica files found in package: {}", path);
        }

        let file_strs: Vec<&str> = files
            .iter()
            .map(|p| p.to_str().expect("path contains invalid UTF-8"))
            .collect();
        self.compile_files(&file_strs)
    }

    /// Compiles a Modelica file to a DAE representation.
    ///
    /// This method performs the full compilation pipeline:
    /// 1. Reads the file from disk
    /// 2. Parses the Modelica code into an AST
    /// 3. Flattens the hierarchical class structure
    /// 4. Converts to DAE representation
    ///
    /// # Arguments
    ///
    /// * `path` - Path to the Modelica file to compile
    ///
    /// # Returns
    ///
    /// A [`CompilationResult`] containing the DAE and metadata
    ///
    /// # Errors
    ///
    /// Returns an error if:
    /// - The file cannot be read
    /// - The Modelica code contains syntax errors
    /// - The model contains unsupported features (e.g., unexpanded connection equations)
    ///
    /// # Examples
    ///
    /// ```no_run
    /// use rumoca::Compiler;
    ///
    /// let result = Compiler::new()
    ///     .model("MyModel")
    ///     .compile_file("model.mo")?;
    /// println!("Model has {} states", result.dae.x.len());
    /// # Ok::<(), anyhow::Error>(())
    /// ```
    #[cfg(not(target_arch = "wasm32"))]
    pub fn compile_file(&self, path: &str) -> Result<CompilationResult> {
        use std::sync::atomic::{AtomicUsize, Ordering};

        // Configure thread pool
        let thread_count = self.get_thread_count();
        let pool = rayon::ThreadPoolBuilder::new()
            .num_threads(thread_count)
            .build()
            .with_context(|| "Failed to create thread pool")?;

        let cache_hits = AtomicUsize::new(0);
        let cache_misses = AtomicUsize::new(0);

        if self.verbose {
            println!(
                "Parsing {} files using {} threads (cache: {})...",
                self.additional_files.len() + 1,
                thread_count,
                if self.use_cache {
                    "enabled"
                } else {
                    "disabled"
                }
            );
        }

        // Parse additional files in parallel (with caching)
        // Returns (path, definition, file_hash) for each file
        let additional_paths: Vec<_> = self.additional_files.iter().collect();
        let use_cache = self.use_cache;
        let parsed_additional: Result<Vec<_>> = pool.install(|| {
            additional_paths
                .par_iter()
                .map(|additional_path| {
                    let path_str = additional_path.to_string_lossy().to_string();

                    // Compute hash for this file (used for both AST cache and flat cache key)
                    let file_hash = cache::compute_file_hash(additional_path)
                        .unwrap_or_else(|_| "unknown".to_string());

                    // Try cache first
                    if use_cache
                        && let Some(cached_def) =
                            cache::load_cached_ast(additional_path, &file_hash)
                    {
                        cache_hits.fetch_add(1, Ordering::Relaxed);
                        return Ok((path_str, cached_def, file_hash));
                    }

                    // Cache miss - parse the file
                    cache_misses.fetch_add(1, Ordering::Relaxed);
                    let additional_source = fs::read_to_string(additional_path)
                        .with_context(|| format!("Failed to read file: {}", path_str))?;
                    let def = self.parse_source(&additional_source, &path_str)?;

                    // Store in cache
                    if use_cache {
                        let _ = cache::store_cached_ast(additional_path, &file_hash, &def);
                    }

                    Ok((path_str, def, file_hash))
                })
                .collect()
        });

        let additional_results = parsed_additional?;

        if self.verbose {
            println!(
                "Cache: {} hits, {} misses",
                cache_hits.load(Ordering::Relaxed),
                cache_misses.load(Ordering::Relaxed)
            );
        }

        // Parse main file (not cached - it's the user's code that changes frequently)
        let input =
            fs::read_to_string(path).with_context(|| format!("Failed to read file: {}", path))?;
        let main_hash = format!("{:x}", chksum_md5::hash(&input));

        let main_def = self.parse_source(&input, path)?;

        // Collect all definitions and hashes
        let mut all_definitions: Vec<(String, StoredDefinition)> = additional_results
            .iter()
            .map(|(p, d, _)| (p.clone(), d.clone()))
            .collect();
        all_definitions.push((path.to_string(), main_def));

        let mut all_hashes: Vec<String> =
            additional_results.into_iter().map(|(_, _, h)| h).collect();
        all_hashes.push(main_hash);

        // Compile with all definitions and hashes for flat class caching
        self.compile_definitions_with_hashes(all_definitions, &input, path, Some(all_hashes))
    }

    /// Compiles multiple Modelica files together.
    ///
    /// This method compiles multiple files, merging their class definitions
    /// before flattening. The main model to simulate is specified via `.model()`.
    ///
    /// # Arguments
    ///
    /// * `paths` - Paths to the Modelica files to compile
    ///
    /// # Returns
    ///
    /// A [`CompilationResult`] containing the DAE and metadata
    ///
    /// # Examples
    ///
    /// ```no_run
    /// use rumoca::Compiler;
    ///
    /// let result = Compiler::new()
    ///     .model("MyPackage.MyModel")
    ///     .compile_files(&["library.mo", "model.mo"])?;
    /// # Ok::<(), anyhow::Error>(())
    /// ```
    #[cfg(not(target_arch = "wasm32"))]
    pub fn compile_files(&self, paths: &[&str]) -> Result<CompilationResult> {
        if paths.is_empty() {
            anyhow::bail!("At least one file must be provided");
        }

        // Configure thread pool
        let thread_count = self.get_thread_count();
        let pool = rayon::ThreadPoolBuilder::new()
            .num_threads(thread_count)
            .build()
            .with_context(|| "Failed to create thread pool")?;

        if self.verbose {
            println!(
                "Parsing {} files using {} threads...",
                paths.len(),
                thread_count
            );
        }

        // Parse all files in parallel
        let parsed_results: Result<Vec<_>> = pool.install(|| {
            paths
                .par_iter()
                .map(|path| {
                    let source = fs::read_to_string(path)
                        .with_context(|| format!("Failed to read file: {}", path))?;
                    let def = self.parse_source(&source, path)?;
                    Ok((path.to_string(), def, source))
                })
                .collect()
        });

        let results = parsed_results?;

        // Separate definitions and sources
        let all_definitions: Vec<_> = results
            .iter()
            .map(|(path, def, _)| (path.clone(), def.clone()))
            .collect();
        let main_source = &results.last().expect("no files were compiled").2;
        let main_path = &results.last().expect("no files were compiled").0;

        self.compile_definitions(all_definitions, main_source, main_path)
    }

    /// Parse a source file and return the StoredDefinition
    fn parse_source(&self, source: &str, file_name: &str) -> Result<StoredDefinition> {
        let mut grammar = ModelicaGrammar::new();
        if let Err(e) = parse(source, file_name, &mut grammar) {
            let diagnostic = create_syntax_error(&e, source);
            let report = miette::Report::new(diagnostic);
            return Err(anyhow::anyhow!("{:?}", report));
        }

        grammar.modelica.ok_or_else(|| {
            anyhow::anyhow!("Parser succeeded but produced no AST for {}", file_name)
        })
    }

    /// Compile from pre-parsed definitions (public for WASM caching)
    pub fn compile_definitions(
        &self,
        definitions: Vec<(String, StoredDefinition)>,
        main_source: &str,
        _main_file_name: &str,
    ) -> Result<CompilationResult> {
        self.compile_definitions_with_hashes(definitions, main_source, _main_file_name, None)
    }

    /// Compile from pre-parsed definitions with optional source hashes for flat class caching
    fn compile_definitions_with_hashes(
        &self,
        definitions: Vec<(String, StoredDefinition)>,
        main_source: &str,
        _main_file_name: &str,
        _source_hashes: Option<Vec<String>>,
    ) -> Result<CompilationResult> {
        use crate::ir::transform::multi_file::merge_stored_definitions;

        let start = Instant::now();

        // Merge all definitions
        let def = if definitions.len() == 1 {
            definitions
                .into_iter()
                .next()
                .expect("definitions is non-empty")
                .1
        } else {
            if self.verbose {
                println!("Merging {} files...", definitions.len());
            }
            merge_stored_definitions(definitions)?
        };

        let model_hash = format!("{:x}", chksum_md5::hash(main_source));
        let parse_time = start.elapsed();

        if self.verbose {
            println!("Parsing took {} ms", parse_time.as_millis());
            println!("AST:\n{:#?}\n", def);
        }

        // Run the compilation pipeline
        pipeline::compile_from_ast_ref(
            &def,
            self.model_name.as_deref(),
            model_hash,
            parse_time,
            self.verbose,
        )
    }

    /// Compiles Modelica source code from a string to a DAE representation.
    ///
    /// This method performs the full compilation pipeline on the provided source code.
    ///
    /// # Arguments
    ///
    /// * `source` - The Modelica source code to compile
    /// * `file_name` - A name to use for error reporting (can be anything)
    ///
    /// # Returns
    ///
    /// A [`CompilationResult`] containing the DAE and metadata
    ///
    /// # Errors
    ///
    /// Returns an error if:
    /// - The Modelica code contains syntax errors
    /// - The model contains unsupported features
    ///
    /// # Examples
    ///
    /// ```no_run
    /// use rumoca::Compiler;
    ///
    /// let code = "model Test\n  Real x;\nequation\n  der(x) = 1;\nend Test;";
    /// let result = Compiler::new()
    ///     .model("Test")
    ///     .compile_str(code, "test.mo")?;
    /// # Ok::<(), anyhow::Error>(())
    /// ```
    pub fn compile_str(&self, source: &str, file_name: &str) -> Result<CompilationResult> {
        // WASM: No filesystem access, so no additional files - just parse main source
        #[cfg(target_arch = "wasm32")]
        {
            let def = self.parse_source(source, file_name)?;
            let all_definitions = vec![(file_name.to_string(), def)];
            return self.compile_definitions(all_definitions, source, file_name);
        }

        // Native: Full parallel processing with thread pool
        #[cfg(not(target_arch = "wasm32"))]
        {
            use std::sync::atomic::{AtomicUsize, Ordering};

            // Configure thread pool
            let thread_count = self.get_thread_count();
            let pool = rayon::ThreadPoolBuilder::new()
                .num_threads(thread_count)
                .build()
                .with_context(|| "Failed to create thread pool")?;

            let cache_hits = AtomicUsize::new(0);
            let cache_misses = AtomicUsize::new(0);

            if self.verbose && !self.additional_files.is_empty() {
                println!(
                    "Parsing {} additional files using {} threads (cache: {})...",
                    self.additional_files.len(),
                    thread_count,
                    if self.use_cache {
                        "enabled"
                    } else {
                        "disabled"
                    }
                );
            }

            // Parse additional files in parallel (with caching)
            let additional_paths: Vec<_> = self.additional_files.iter().collect();
            let use_cache = self.use_cache;
            let parsed_additional: Result<Vec<_>> = pool.install(|| {
                additional_paths
                    .par_iter()
                    .map(|additional_path| {
                        let path_str = additional_path.to_string_lossy().to_string();

                        // Try cache first
                        if use_cache
                            && let Ok(hash) = cache::compute_file_hash(additional_path)
                            && let Some(cached_def) = cache::load_cached_ast(additional_path, &hash)
                        {
                            cache_hits.fetch_add(1, Ordering::Relaxed);
                            return Ok((path_str, cached_def));
                        }

                        // Cache miss - parse the file
                        cache_misses.fetch_add(1, Ordering::Relaxed);
                        let additional_source = fs::read_to_string(additional_path)
                            .with_context(|| format!("Failed to read file: {}", path_str))?;
                        let def = self.parse_source(&additional_source, &path_str)?;

                        // Store in cache
                        if use_cache && let Ok(hash) = cache::compute_file_hash(additional_path) {
                            let _ = cache::store_cached_ast(additional_path, &hash, &def);
                        }

                        Ok((path_str, def))
                    })
                    .collect()
            });

            let mut all_definitions = parsed_additional?;

            if self.verbose && !self.additional_files.is_empty() {
                println!(
                    "Cache: {} hits, {} misses",
                    cache_hits.load(Ordering::Relaxed),
                    cache_misses.load(Ordering::Relaxed)
                );
            }

            // Parse main source
            let def = self.parse_source(source, file_name)?;
            all_definitions.push((file_name.to_string(), def));

            // Compile with all definitions
            self.compile_definitions(all_definitions, source, file_name)
        }
    }

    /// Compiles from a pre-parsed StoredDefinition.
    ///
    /// This method is useful when you have already parsed the Modelica code
    /// and want to avoid re-parsing it. This is especially beneficial when
    /// compiling multiple models from the same source files (e.g., testing).
    ///
    /// # Arguments
    ///
    /// * `def` - The pre-parsed StoredDefinition
    /// * `source` - The original source code (for error reporting)
    ///
    /// # Returns
    ///
    /// A [`CompilationResult`] containing the DAE and metadata
    ///
    /// # Examples
    ///
    /// ```no_run
    /// use rumoca::{Compiler, ir::ast::StoredDefinition};
    /// use rumoca::modelica_grammar::ModelicaGrammar;
    /// use rumoca::modelica_parser::parse;
    ///
    /// // Parse once
    /// let source = "model Test\n  Real x;\nequation\n  der(x) = 1;\nend Test;";
    /// let mut grammar = ModelicaGrammar::new();
    /// parse(source, "test.mo", &mut grammar).unwrap();
    /// let def = grammar.modelica.unwrap();
    ///
    /// // Compile multiple times without re-parsing
    /// let result = Compiler::new()
    ///     .model("Test")
    ///     .compile_parsed(def.clone(), source)?;
    /// # Ok::<(), anyhow::Error>(())
    /// ```
    pub fn compile_parsed(&self, def: StoredDefinition, source: &str) -> Result<CompilationResult> {
        let model_hash = format!("{:x}", chksum_md5::hash(source));
        pipeline::compile_from_ast(
            def,
            self.model_name.as_deref(),
            model_hash,
            std::time::Duration::ZERO, // No parse time for pre-parsed
            self.verbose,
        )
    }

    /// Compiles from a reference to a pre-parsed StoredDefinition.
    ///
    /// This is more efficient than `compile_parsed` when compiling many models
    /// from the same AST because it avoids cloning the StoredDefinition during
    /// the compilation process. The def is only cloned once at the end.
    pub fn compile_parsed_ref(
        &self,
        def: &StoredDefinition,
        source: &str,
    ) -> Result<CompilationResult> {
        let model_hash = format!("{:x}", chksum_md5::hash(source));
        pipeline::compile_from_ast_ref(
            def,
            self.model_name.as_deref(),
            model_hash,
            std::time::Duration::ZERO,
            self.verbose,
        )
    }

    /// Performs a lightweight balance check only, without full compilation.
    ///
    /// This is much faster than full compilation when you only need to check
    /// if a model is balanced. It skips the StoredDefinition cloning entirely.
    pub fn check_balance(
        &self,
        def: &StoredDefinition,
    ) -> Result<crate::dae::balance::BalanceResult> {
        pipeline::check_balance_only(def, self.model_name.as_deref())
    }

    /// Compile with pre-parsed library definitions.
    ///
    /// This is the fastest way to compile when you have cached library ASTs.
    /// The library definitions are cloned and merged with the main source.
    ///
    /// # Arguments
    ///
    /// * `source` - The main Modelica source code
    /// * `file_name` - Name for the main source file (for error messages)
    /// * `libraries` - Pre-parsed library definitions (filename, AST pairs)
    ///
    /// # Returns
    ///
    /// A [`CompilationResult`] containing the DAE and metadata
    pub fn compile_str_with_definitions(
        &self,
        source: &str,
        file_name: &str,
        libraries: &[(String, StoredDefinition)],
    ) -> Result<CompilationResult> {
        // Parse the main source
        let main_def = self.parse_source(source, file_name)?;

        // Clone library definitions and add main source
        let mut all_definitions: Vec<(String, StoredDefinition)> = libraries.to_vec();
        all_definitions.push((file_name.to_string(), main_def));

        self.compile_definitions(all_definitions, source, file_name)
    }

    /// Compile with pre-merged library ASTs.
    ///
    /// This is the fastest way to compile when libraries are pre-merged at load time.
    /// Instead of merging 2500+ files on every compile, we only merge a few
    /// pre-merged library ASTs with the user's source.
    ///
    /// # Arguments
    ///
    /// * `source` - The main Modelica source code
    /// * `file_name` - Name for the main source file (for error messages)
    /// * `libraries` - Pre-merged library ASTs (references, not cloned)
    ///
    /// # Returns
    ///
    /// A [`CompilationResult`] containing the DAE and metadata
    pub fn compile_str_with_merged_libraries(
        &self,
        source: &str,
        file_name: &str,
        libraries: &[&StoredDefinition],
    ) -> Result<CompilationResult> {
        // Parse the main source
        let main_def = self.parse_source(source, file_name)?;

        // Collect all definitions: pre-merged libraries + main source
        // We clone libraries here but since they're pre-merged, there are only a few
        let mut all_definitions: Vec<(String, StoredDefinition)> = libraries
            .iter()
            .enumerate()
            .map(|(i, lib)| (format!("<library-{}>", i), (*lib).clone()))
            .collect();
        all_definitions.push((file_name.to_string(), main_def));

        self.compile_definitions(all_definitions, source, file_name)
    }

    /// Compile with Arc-wrapped pre-merged library ASTs.
    ///
    /// This is the most efficient way to compile for the LSP use case.
    /// Libraries are stored in Arc to avoid cloning the large library data
    /// structure on every compile. The merge operation clones individual
    /// class definitions, but the Arc itself is not cloned.
    ///
    /// # Arguments
    ///
    /// * `source` - The main Modelica source code
    /// * `file_name` - Name for the main source file (for error messages)
    /// * `libraries` - Arc-wrapped pre-merged library ASTs
    ///
    /// # Returns
    ///
    /// A [`CompilationResult`] containing the DAE and metadata
    pub fn compile_str_with_arc_libraries(
        &self,
        source: &str,
        file_name: &str,
        libraries: &[Arc<StoredDefinition>],
    ) -> Result<CompilationResult> {
        use crate::ir::transform::multi_file::merge_with_arc_libraries;

        // Parse the main source
        let main_def = self.parse_source(source, file_name)?;

        let start = Instant::now();

        // Merge libraries with user source using Arc-aware merge
        let def = merge_with_arc_libraries(libraries, main_def, file_name)?;

        let model_hash = format!("{:x}", chksum_md5::hash(source));
        let parse_time = start.elapsed();

        if self.verbose {
            println!("Parsing and merging took {} ms", parse_time.as_millis());
            println!("AST:\n{:#?}\n", def);
        }

        // Run the compilation pipeline
        pipeline::compile_from_ast_ref(
            &def,
            self.model_name.as_deref(),
            model_hash,
            parse_time,
            self.verbose,
        )
    }

    /// Compile with additional library sources provided as strings.
    ///
    /// This is useful for WASM where filesystem access is not available.
    /// Library sources are parsed and merged with the main source before compilation.
    ///
    /// # Arguments
    ///
    /// * `source` - The main Modelica source code
    /// * `file_name` - Name for the main source file (for error messages)
    /// * `libraries` - Vector of (file_name, source_code) pairs for library files
    ///
    /// # Returns
    ///
    /// A [`CompilationResult`] containing the DAE and metadata
    pub fn compile_str_with_sources(
        &self,
        source: &str,
        file_name: &str,
        libraries: Vec<(&str, &str)>,
    ) -> Result<CompilationResult> {
        // Parse the main source
        let main_def = self.parse_source(source, file_name)?;

        // Parse all library sources
        let mut all_definitions = Vec::with_capacity(libraries.len() + 1);

        for (lib_name, lib_source) in libraries {
            let lib_def = self.parse_source(lib_source, lib_name)?;
            all_definitions.push((lib_name.to_string(), lib_def));
        }

        // Add main source last (so its classes take precedence)
        all_definitions.push((file_name.to_string(), main_def));

        self.compile_definitions(all_definitions, source, file_name)
    }
}

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

    #[test]
    fn test_compiler_default() {
        let compiler = Compiler::new();
        assert!(!compiler.verbose);
    }

    #[test]
    fn test_compiler_verbose() {
        let compiler = Compiler::new().verbose(true);
        assert!(compiler.verbose);
    }

    #[test]
    fn test_compile_simple_model() {
        let source = r#"
model Integrator
    Real x(start=0);
equation
    der(x) = 1;
end Integrator;
"#;

        let result = Compiler::new()
            .model("Integrator")
            .compile_str(source, "test.mo");
        assert!(result.is_ok(), "Failed to compile: {:?}", result.err());

        let result = result.unwrap();
        assert!(!result.dae.x.is_empty(), "Should have state variables");
        assert_eq!(result.dae.x.len(), 1, "Should have exactly one state");
    }

    #[test]
    fn test_compile_requires_model_name() {
        let source = r#"
model Test
    Real x;
equation
    der(x) = 1;
end Test;
"#;

        let result = Compiler::new().compile_str(source, "test.mo");
        assert!(result.is_err(), "Should error when model name not provided");
        let err_msg = result.unwrap_err().to_string();
        assert!(
            err_msg.contains("Model name is required"),
            "Error should mention model name is required: {}",
            err_msg
        );
    }

    #[test]
    fn test_compilation_result_total_time() {
        let source = r#"
model Test
    Real x;
equation
    der(x) = 1;
end Test;
"#;

        let result = Compiler::new()
            .model("Test")
            .compile_str(source, "test.mo")
            .unwrap();
        let total = result.total_time();
        assert!(total > std::time::Duration::from_nanos(0));
        assert_eq!(
            total,
            result.parse_time + result.flatten_time + result.dae_time
        );
    }
}