syster-base 0.3.4-alpha

Core library for SysML v2 and KerML parsing, AST, and semantic analysis
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
//! Tests for SysML v2 Release examples
//!
//! This module tests parsing of official SysML v2 Release examples from:
//! https://github.com/Systems-Modeling/SysML-v2-Release
//!
//! The examples are stored in `tests/sysml-examples/` directory.
//!
//! # Setup
//! To populate the examples directory:
//! ```bash
//! git clone --depth 1 https://github.com/Systems-Modeling/SysML-v2-Release.git /tmp/sysml
//! cp -r /tmp/sysml/sysml/src/examples crates/syster-base/tests/sysml-examples
//! ```

#![allow(clippy::unwrap_used)]

use rayon::prelude::*;
use std::collections::HashMap;
use std::path::{Path, PathBuf};
use std::sync::Mutex;
use syster::project::file_loader;

fn get_examples_dir() -> PathBuf {
    PathBuf::from(env!("CARGO_MANIFEST_DIR")).join("tests/sysml-examples")
}

/// Recursively collect all .sysml files from a directory
fn collect_sysml_files(dir: &Path, files: &mut Vec<PathBuf>) {
    if let Ok(entries) = std::fs::read_dir(dir) {
        for entry in entries.flatten() {
            let path = entry.path();
            if path.is_dir() {
                collect_sysml_files(&path, files);
            } else if path.extension().is_some_and(|ext| ext == "sysml") {
                files.push(path);
            }
        }
    }
}

/// Test all SysML v2 Release examples and report results
///
/// This test is ignored by default because it parses ~95 files and takes ~8 seconds.
/// Run with: `cargo test test_sysml_examples_parsing -- --ignored`
#[test]
fn test_sysml_examples_parsing() {
    let examples_dir = get_examples_dir();

    if !examples_dir.exists() {
        eprintln!("⏭️  Skipping: sysml-examples directory not found at {examples_dir:?}");
        eprintln!("   To run these tests, execute:");
        eprintln!(
            "   git clone --depth 1 https://github.com/Systems-Modeling/SysML-v2-Release.git /tmp/sysml"
        );
        eprintln!("   cp -r /tmp/sysml/sysml/src/examples crates/syster-base/tests/sysml-examples");
        return;
    }

    let mut files = Vec::new();
    collect_sysml_files(&examples_dir, &mut files);
    files.sort();

    if files.is_empty() {
        eprintln!("⚠️  No .sysml files found in {examples_dir:?}");
        return;
    }

    let passed = Mutex::new(Vec::new());
    let failed: Mutex<HashMap<String, Vec<String>>> = Mutex::new(HashMap::new());

    files.par_iter().for_each(|file_path| {
        let relative = file_path
            .strip_prefix(&examples_dir)
            .unwrap_or(file_path)
            .display()
            .to_string();

        let content = match std::fs::read_to_string(file_path) {
            Ok(c) => c,
            Err(e) => {
                failed
                    .lock()
                    .unwrap()
                    .entry(format!("IO Error: {e}"))
                    .or_default()
                    .push(relative);
                return;
            }
        };

        let parse_result = file_loader::parse_with_result(&content, file_path);

        if parse_result.content.is_some() && parse_result.errors.is_empty() {
            passed.lock().unwrap().push(relative);
        } else {
            let error_msg = parse_result
                .errors
                .first()
                .map(|e| {
                    // Extract just the "expected X" part for grouping
                    if let Some(pos) = e.message.find("expected ") {
                        let rest = &e.message[pos..];
                        if let Some(end) = rest.find('\n') {
                            rest[..end].to_string()
                        } else {
                            rest.to_string()
                        }
                    } else {
                        e.message.clone()
                    }
                })
                .unwrap_or_else(|| "Unknown error".to_string());

            failed
                .lock()
                .unwrap()
                .entry(error_msg)
                .or_default()
                .push(relative);
        }
    });

    let passed = passed.into_inner().unwrap();
    let failed = failed.into_inner().unwrap();
    let total = files.len();
    let pass_count = passed.len();
    let fail_count = total - pass_count;
    let pass_rate = (pass_count as f64 / total as f64) * 100.0;

    eprintln!("\n╔════════════════════════════════════════════════════════════════╗");
    eprintln!("║           SysML v2 Examples Parsing Summary                    ║");
    eprintln!("╠════════════════════════════════════════════════════════════════╣");
    eprintln!("║ Total files: {total:>4}");
    eprintln!(
        "║ Passed:      {pass_count:>4} ({pass_rate:>5.1}%)                                    ║"
    );
    eprintln!(
        "║ Failed:      {:>4} ({:>5.1}%)                                    ║",
        fail_count,
        100.0 - pass_rate
    );
    eprintln!("╚════════════════════════════════════════════════════════════════╝");

    if !failed.is_empty() {
        eprintln!("\n📋 Failures by error pattern:");

        // Sort by count descending
        let mut error_counts: Vec<_> = failed.iter().collect();
        error_counts.sort_by(|a, b| b.1.len().cmp(&a.1.len()));

        for (error, files) in error_counts {
            eprintln!("\n{} ({} files)", error, files.len());
            for f in files.iter().take(3) {
                eprintln!("     - {f}");
            }
            if files.len() > 3 {
                eprintln!("     ... and {} more", files.len() - 3);
            }
        }
    }

    if !passed.is_empty() {
        eprintln!("\n✅ Passing files ({}):", passed.len());
        for f in &passed {
            eprintln!("   - {f}");
        }
    }

    eprintln!();

    // The test itself always passes - it's informational
    // Uncomment the assertion below to make it fail if any files don't parse:
    // assert_eq!(fail_count, 0, "Some example files failed to parse");
}

/// Regression test: ensure no previously-passing files start failing
///
/// This list should be kept in sync with the actual passing files.
/// Run test_sysml_examples_parsing to see the current list.
#[test]
fn test_no_regressions() {
    let examples_dir = get_examples_dir();

    if !examples_dir.exists() {
        return; // Skip if examples not present
    }

    // List of files that MUST continue to parse successfully
    // This prevents accidental grammar regressions
    let must_pass = [
        "Simple Tests/ImportTest.sysml",
        "Simple Tests/AliasTest.sysml",
        "Simple Tests/EnumerationTest.sysml",
        "Simple Tests/MultiplicityTest.sysml",
        "Simple Tests/DependencyTest.sysml",
        "Simple Tests/DefaultValueTest.sysml",
        "Simple Tests/ConstraintTest.sysml",
        "Import Tests/AliasImport.sysml",
        "Import Tests/CircularImport.sysml",
        "Import Tests/PrivateImportTest.sysml",
        "Import Tests/QualifiedNameImportTest.sysml",
        "Comment Examples/Comments.sysml",
    ];

    let mut regressions = Vec::new();

    for relative_path in must_pass {
        let file_path = examples_dir.join(relative_path);

        if !file_path.exists() {
            continue; // Skip if file doesn't exist
        }

        let content = match std::fs::read_to_string(&file_path) {
            Ok(c) => c,
            Err(_) => continue,
        };

        let parse_result = file_loader::parse_with_result(&content, &file_path);

        if parse_result.content.is_none() || !parse_result.errors.is_empty() {
            let error = parse_result
                .errors
                .first()
                .map(|e| e.message.clone())
                .unwrap_or_else(|| "Unknown error".to_string());
            regressions.push(format!("{relative_path}: {error}"));
        }
    }

    if !regressions.is_empty() {
        panic!(
            "🚨 REGRESSION: {} previously-passing files now fail:\n  - {}",
            regressions.len(),
            regressions.join("\n  - ")
        );
    }
}

macro_rules! example_test {
    ($name:ident, $path:expr) => {
        #[test]
        fn $name() {
            let examples_dir = get_examples_dir();
            let file_path = examples_dir.join($path);

            if !file_path.exists() {
                eprintln!("Skipping: file not found at {:?}", file_path);
                return;
            }

            let content = std::fs::read_to_string(&file_path)
                .unwrap_or_else(|e| panic!("Failed to read {}: {}", $path, e));

            let parse_result = file_loader::parse_with_result(&content, &file_path);

            assert!(
                parse_result.content.is_some() && parse_result.errors.is_empty(),
                "Failed to parse {}:\n{}",
                $path,
                parse_result
                    .errors
                    .iter()
                    .map(|e| format!("  Line {}: {}", e.position.line, e.message))
                    .collect::<Vec<_>>()
                    .join("\n")
            );
        }
    };
}

// Analysis Examples
example_test!(
    example_analysis_annotation,
    "Analysis Examples/AnalysisAnnotation.sysml"
);
example_test!(
    example_turbojet_stage_analysis,
    "Analysis Examples/Turbojet Stage Analysis.sysml"
);
example_test!(
    example_vehicle_analysis_demo,
    "Analysis Examples/Vehicle Analysis Demo.sysml"
);

// Arrowhead Framework Example
example_test!(
    example_ahf_sequences,
    "Arrowhead Framework Example/AHFSequences.sysml"
);
example_test!(
    example_ahf_norway_topics,
    "Arrowhead Framework Example/AHFNorwayTopics.sysml"
);

// Association Examples
example_test!(
    example_product_selection_unowned_ends,
    "Association Examples/ProductSelection_UnownedEnds.sysml"
);

// Camera Example
example_test!(example_picture_taking, "Camera Example/PictureTaking.sysml");

// Cause and Effect Examples
example_test!(
    example_cause_and_effect,
    "Cause and Effect Examples/CauseAndEffectExample.sysml"
);

// Flashlight Example
example_test!(
    example_flashlight,
    "Flashlight Example/Flashlight Example.sysml"
);

// Geometry Examples
example_test!(
    example_external_shape_ref,
    "Geometry Examples/ExternalShapeRefExample.sysml"
);
example_test!(
    example_vehicle_geometry_coords,
    "Geometry Examples/VehicleGeometryAndCoordinateFrames.sysml"
);

// Interaction Sequencing Examples
example_test!(
    example_server_sequence_outside_realization_3,
    "Interaction Sequencing Examples/ServerSequenceOutsideRealization-3.sysml"
);
example_test!(
    example_server_sequence_realization_3,
    "Interaction Sequencing Examples/ServerSequenceRealization-3.sysml"
);

// Mass Roll-up Example
example_test!(example_mass_rollup, "Mass Roll-up Example/MassRollup.sysml");

// Metadata Examples
example_test!(
    example_issue_metadata,
    "Metadata Examples/IssueMetadataExample.sysml"
);
example_test!(
    example_requirement_metadata,
    "Metadata Examples/RequirementMetadataExample.sysml"
);
example_test!(
    example_risk_metadata,
    "Metadata Examples/RiskMetadataExample.sysml"
);
example_test!(
    example_verification_metadata,
    "Metadata Examples/VerificationMetadataExample.sysml"
);

// Requirements Examples
example_test!(
    example_requirement_derivation,
    "Requirements Examples/RequirementDerivationExample.sysml"
);

// Room Model
example_test!(example_room_model, "Room Model/RoomModel.sysml");

// Simple Tests
example_test!(example_action_test, "Simple Tests/ActionTest.sysml");
example_test!(example_allocation_test, "Simple Tests/AllocationTest.sysml");
example_test!(example_analysis_test, "Simple Tests/AnalysisTest.sysml");
example_test!(example_assignment_test, "Simple Tests/AssignmentTest.sysml");
example_test!(example_comment_test, "Simple Tests/CommentTest.sysml");
example_test!(example_connection_test, "Simple Tests/ConnectionTest.sysml");
example_test!(
    example_control_node_test,
    "Simple Tests/ControlNodeTest.sysml"
);
example_test!(example_decision_test, "Simple Tests/DecisionTest.sysml");
example_test!(
    example_feature_path_test,
    "Simple Tests/FeaturePathTest.sysml"
);
example_test!(example_part_test, "Simple Tests/PartTest.sysml");
example_test!(
    example_requirement_test,
    "Simple Tests/RequirementTest.sysml"
);
example_test!(example_state_test, "Simple Tests/StateTest.sysml");
example_test!(
    example_textual_representation_test,
    "Simple Tests/TextualRepresentationTest.sysml"
);
example_test!(example_use_case_test, "Simple Tests/UseCaseTest.sysml");
example_test!(
    example_variability_test,
    "Simple Tests/VariabilityTest.sysml"
);
example_test!(
    example_verification_test,
    "Simple Tests/VerificationTest.sysml"
);
example_test!(example_view_test, "Simple Tests/ViewTest.sysml");

// State Space Representation Examples
example_test!(
    example_cart_sample,
    "State Space Representation Examples/CartSample.sysml"
);

// Timeslice and Snapshot Examples
example_test!(
    example_time_varying_attribute,
    "Timeslice and Snapshot Examples/TimeVaryingAttribute.sysml"
);

// Variability Examples
example_test!(
    example_vehicle_variability_model,
    "Variability Examples/VehicleVariabilityModel.sysml"
);

// Vehicle Example
example_test!(
    example_vehicle_individuals,
    "Vehicle Example/VehicleIndividuals.sysml"
);
example_test!(
    example_sysml_spec_annex_a,
    "Vehicle Example/SysML v2 Spec Annex A SimpleVehicleModel.sysml"
);

// =============================================================================
// SEMANTIC ANALYSIS TESTS
// =============================================================================
//
// These tests verify that example files have zero semantic errors (undefined
// references, duplicate definitions, etc.) when analyzed with the standard library.
//
// Note: For comprehensive semantic false-positive testing, see tests_semantic_false_positives.rs
// which tests both stdlib and all examples together.

use syster::ide::AnalysisHost;
use syster::project::StdLibLoader;

fn get_stdlib_dir() -> PathBuf {
    PathBuf::from(env!("CARGO_MANIFEST_DIR")).join("sysml.library")
}

/// Create an AnalysisHost with stdlib loaded
fn create_host_with_stdlib() -> AnalysisHost {
    let mut host = AnalysisHost::new();
    let stdlib_dir = get_stdlib_dir();
    if stdlib_dir.exists() {
        let mut loader = StdLibLoader::with_path(stdlib_dir);
        let _ = loader.ensure_loaded_into_host(&mut host);
    }
    host
}

/// Load all .sysml files from a directory into the host
fn load_example_dir(host: &mut AnalysisHost, dir: &Path) {
    if !dir.exists() {
        return;
    }
    for entry in walkdir::WalkDir::new(dir).into_iter().flatten() {
        let path = entry.path();
        if path
            .extension()
            .is_some_and(|e| e == "sysml" || e == "kerml")
        {
            if let Ok(content) = std::fs::read_to_string(path) {
                host.set_file_content(&path.to_string_lossy(), &content);
            }
        }
    }
    // Rebuild to process all files
    host.rebuild_index();
}

/// Get errors only for files in a specific directory (not stdlib)
fn get_errors_for_dir(host: &AnalysisHost, dir: &Path) -> Vec<(String, syster::hir::Diagnostic)> {
    host.all_errors()
        .into_iter()
        .filter(|(path, _)| Path::new(path).starts_with(dir))
        .collect()
}

macro_rules! semantic_example_test {
    ($name:ident, $dir:expr) => {
        #[test]
        fn $name() {
            let examples_dir = get_examples_dir();
            let target_dir = examples_dir.join($dir);

            if !target_dir.exists() {
                eprintln!("⏭️  Skipping {}: not found", $dir);
                return;
            }

            let mut host = create_host_with_stdlib();
            load_example_dir(&mut host, &target_dir);

            let errors = get_errors_for_dir(&host, &target_dir);
            let file_count = host
                .file_id_map()
                .keys()
                .filter(|p| Path::new(*p).starts_with(&target_dir))
                .count();

            if errors.is_empty() {
                eprintln!("{}: {} files, 0 semantic errors", $dir, file_count);
            } else {
                for (path, diag) in &errors {
                    let rel = Path::new(path)
                        .file_name()
                        .unwrap_or_default()
                        .to_string_lossy();
                    eprintln!(
                        "  {}:{}:{}: {}",
                        rel,
                        diag.start_line + 1,
                        diag.start_col + 1,
                        diag.message
                    );
                }
                panic!("{}: expected 0 errors, found {}", $dir, errors.len());
            }
        }
    };
}

// Individual example semantic tests
semantic_example_test!(
    test_arrowhead_framework_semantic,
    "Arrowhead Framework Example"
);
semantic_example_test!(test_simple_vehicle_semantic, "Simple Vehicle Example");
semantic_example_test!(test_vehicle_example_semantic, "Vehicle Example");
semantic_example_test!(test_analysis_examples_semantic, "Analysis Examples");
semantic_example_test!(test_metadata_examples_semantic, "Metadata Examples");