syster-base 0.1.10-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
#![allow(clippy::unwrap_used)]
use crate::semantic::resolver::Resolver;

use super::super::*;

/// Test finding a symbol in the specified scope itself
#[test]
fn test_lookup_from_scope_in_current_scope() {
    let mut table = SymbolTable::new();

    // Insert symbol in root scope (scope 0)
    let symbol = Symbol::Package {
        scope_id: 0,
        source_file: None,
        span: None,
        name: "RootSymbol".to_string(),
        qualified_name: "RootSymbol".to_string(),
    };

    table.insert("RootSymbol".to_string(), symbol).unwrap();

    // Lookup from scope 0 should find it
    let resolver = Resolver::new(&table);
    let found = resolver.resolve_in_scope("RootSymbol", 0);
    assert!(found.is_some());
    assert_eq!(found.unwrap().name(), "RootSymbol");
}

/// Test finding a symbol in parent scope when not in specified scope
#[test]
fn test_lookup_from_scope_in_parent_scope() {
    let mut table = SymbolTable::new();

    // Insert symbol in root scope (scope 0)
    let parent_symbol = Symbol::Package {
        scope_id: 0,
        source_file: None,
        span: None,
        name: "ParentSymbol".to_string(),
        qualified_name: "ParentSymbol".to_string(),
    };

    table
        .insert("ParentSymbol".to_string(), parent_symbol)
        .unwrap();

    // Enter a child scope (scope 1)
    let child_scope = table.enter_scope();

    // Lookup from child scope should find symbol in parent
    let resolver = Resolver::new(&table);
    let found = resolver.resolve_in_scope("ParentSymbol", child_scope);
    assert!(found.is_some());
    assert_eq!(found.unwrap().name(), "ParentSymbol");
}

/// Test finding a symbol in grandparent scope (multi-level traversal)
#[test]
fn test_lookup_from_scope_in_grandparent_scope() {
    let mut table = SymbolTable::new();

    // Insert symbol in root scope (scope 0)
    let root_symbol = Symbol::Package {
        scope_id: 0,
        source_file: None,
        span: None,
        name: "GrandparentSymbol".to_string(),
        qualified_name: "GrandparentSymbol".to_string(),
    };

    table
        .insert("GrandparentSymbol".to_string(), root_symbol)
        .unwrap();

    // Enter child scope (scope 1)
    table.enter_scope();

    // Enter grandchild scope (scope 2)
    let grandchild_scope = table.enter_scope();

    // Lookup from grandchild should find symbol in grandparent
    let resolver = Resolver::new(&table);
    let found = resolver.resolve_in_scope("GrandparentSymbol", grandchild_scope);
    assert!(found.is_some());
    assert_eq!(found.unwrap().name(), "GrandparentSymbol");
}

/// Test that returns None when symbol doesn't exist in scope chain
#[test]
fn test_lookup_from_scope_not_found() {
    let mut table = SymbolTable::new();

    // Insert a different symbol
    let symbol = Symbol::Package {
        scope_id: 0,
        source_file: None,
        span: None,
        name: "ExistingSymbol".to_string(),
        qualified_name: "ExistingSymbol".to_string(),
    };

    table.insert("ExistingSymbol".to_string(), symbol).unwrap();

    // Try to find a non-existent symbol from root scope
    let resolver = Resolver::new(&table);
    let found = resolver.resolve_in_scope("NonExistentSymbol", 0);
    assert!(found.is_none());
}

/// Test symbol shadowing - nearest scope takes precedence
#[test]
fn test_lookup_from_scope_symbol_shadowing() {
    let mut table = SymbolTable::new();

    // Insert symbol in root scope
    let parent_symbol = Symbol::Package {
        scope_id: 0,
        source_file: None,
        span: None,
        name: "Symbol".to_string(),
        qualified_name: "Parent::Symbol".to_string(),
    };

    table.insert("Symbol".to_string(), parent_symbol).unwrap();

    // Enter child scope
    let child_scope = table.enter_scope();

    // Insert symbol with same name in child scope (shadowing)
    let child_symbol = Symbol::Classifier {
        scope_id: 1,
        source_file: None,
        span: None,
        name: "Symbol".to_string(),
        qualified_name: "Parent::Child::Symbol".to_string(),
        kind: "Class".to_string(),
        is_abstract: false,
    };

    table.insert("Symbol".to_string(), child_symbol).unwrap();

    // Lookup from child scope should find the child scope symbol (shadowing)
    let resolver = Resolver::new(&table);
    let found = resolver.resolve_in_scope("Symbol", child_scope);
    assert!(found.is_some());
    let symbol = found.unwrap();
    assert_eq!(symbol.qualified_name(), "Parent::Child::Symbol");
    assert!(matches!(symbol, Symbol::Classifier { .. }));
}

/// Test lookup from root scope (scope 0)
#[test]
fn test_lookup_from_scope_from_root() {
    let mut table = SymbolTable::new();

    // Insert symbol in root scope
    let symbol = Symbol::Package {
        scope_id: 0,
        source_file: None,
        span: None,
        name: "RootSymbol".to_string(),
        qualified_name: "RootSymbol".to_string(),
    };

    table.insert("RootSymbol".to_string(), symbol).unwrap();

    // Enter child scope
    table.enter_scope();

    // Lookup from root scope should only find root symbol, not check children
    let resolver = Resolver::new(&table);
    let found = resolver.resolve_in_scope("RootSymbol", 0);
    assert!(found.is_some());
}

/// Test lookup from nested scope doesn't find symbols in sibling scopes
#[test]
fn test_lookup_from_scope_no_sibling_access() {
    let mut table = SymbolTable::new();

    // Enter first child scope
    table.enter_scope();
    let sibling1_symbol = Symbol::Package {
        scope_id: 1,
        source_file: None,
        span: None,
        name: "Sibling1".to_string(),
        qualified_name: "Sibling1".to_string(),
    };
    table
        .insert("Sibling1".to_string(), sibling1_symbol)
        .unwrap();

    // Exit back to root
    table.exit_scope();

    // Enter second child scope
    let sibling2_scope = table.enter_scope();

    // Lookup from sibling2 scope should not find sibling1's symbol
    // Using resolve_from_scope_direct to test scope-chain isolation (no global lookup)
    let resolver = Resolver::new(&table);
    let found = resolver.resolve_from_scope_direct("Sibling1", sibling2_scope);
    assert!(found.is_none());
}

/// Test lookup with different symbol types in scope chain
#[test]
fn test_lookup_from_scope_different_symbol_types() {
    let mut table = SymbolTable::new();

    // Package at root
    table
        .insert(
            "RootPkg".to_string(),
            Symbol::Package {
                scope_id: 0,
                source_file: None,
                span: None,
                name: "RootPkg".to_string(),
                qualified_name: "RootPkg".to_string(),
            },
        )
        .unwrap();

    // Enter scope for classifier
    table.enter_scope();
    table
        .insert(
            "MyClass".to_string(),
            Symbol::Classifier {
                scope_id: 1,
                source_file: None,
                span: None,
                name: "MyClass".to_string(),
                qualified_name: "RootPkg::MyClass".to_string(),
                kind: "Class".to_string(),
                is_abstract: false,
            },
        )
        .unwrap();

    // Enter scope for feature
    let feature_scope = table.enter_scope();
    table
        .insert(
            "MyFeature".to_string(),
            Symbol::Feature {
                scope_id: 2,
                source_file: None,
                span: None,
                name: "MyFeature".to_string(),
                qualified_name: "RootPkg::MyClass::MyFeature".to_string(),
                feature_type: Some("String".to_string()),
            },
        )
        .unwrap();

    // From feature scope, should find all three up the chain
    let resolver = Resolver::new(&table);
    let pkg = resolver.resolve_in_scope("RootPkg", feature_scope);
    assert!(pkg.is_some());
    assert!(matches!(pkg.unwrap(), Symbol::Package { .. }));

    let resolver = Resolver::new(&table);
    let class = resolver.resolve_in_scope("MyClass", feature_scope);
    assert!(class.is_some());
    assert!(matches!(class.unwrap(), Symbol::Classifier { .. }));

    let resolver = Resolver::new(&table);
    let feature = resolver.resolve_in_scope("MyFeature", feature_scope);
    assert!(feature.is_some());
    assert!(matches!(feature.unwrap(), Symbol::Feature { .. }));
}

/// Test lookup from deeply nested scopes
#[test]
fn test_lookup_from_scope_deeply_nested() {
    let mut table = SymbolTable::new();

    // Insert symbol at root (level 0)
    let root_symbol = Symbol::Package {
        scope_id: 0,
        source_file: None,
        span: None,
        name: "Level0".to_string(),
        qualified_name: "Level0".to_string(),
    };

    table.insert("Level0".to_string(), root_symbol).unwrap();

    // Create multiple nested scopes (levels 1-4)
    let mut scope_ids = vec![0];
    for i in 1..=4 {
        let scope_id = table.enter_scope();
        scope_ids.push(scope_id);
        let symbol = Symbol::Package {
            scope_id: i,
            source_file: None,
            span: None,
            name: format!("Level{i}"),
            qualified_name: format!("Level0::Level{i}"),
        };
        table.insert(format!("Level{i}"), symbol).unwrap();
    }

    // From the deepest scope (level 4), we should be able to find all symbols
    let deepest_scope = *scope_ids.last().unwrap();
    assert!(
        Resolver::new(&table)
            .resolve_in_scope("Level0", deepest_scope)
            .is_some()
    );
    assert!(
        Resolver::new(&table)
            .resolve_in_scope("Level1", deepest_scope)
            .is_some()
    );
    assert!(
        Resolver::new(&table)
            .resolve_in_scope("Level2", deepest_scope)
            .is_some()
    );
    assert!(
        Resolver::new(&table)
            .resolve_in_scope("Level3", deepest_scope)
            .is_some()
    );
    assert!(
        Resolver::new(&table)
            .resolve_in_scope("Level4", deepest_scope)
            .is_some()
    );

    // From level 2, we should only find Level0, Level1, and Level2
    let level2_scope = scope_ids[2];
    assert!(
        Resolver::new(&table)
            .resolve_in_scope("Level0", level2_scope)
            .is_some()
    );
    assert!(
        Resolver::new(&table)
            .resolve_in_scope("Level1", level2_scope)
            .is_some()
    );
    assert!(
        Resolver::new(&table)
            .resolve_in_scope("Level2", level2_scope)
            .is_some()
    );
    assert!(
        Resolver::new(&table)
            .resolve_in_scope("Level3", level2_scope)
            .is_none()
    );
    assert!(
        Resolver::new(&table)
            .resolve_in_scope("Level4", level2_scope)
            .is_none()
    );
}

/// Test that lookup_from_scope doesn't check child scopes
#[test]
fn test_lookup_from_scope_no_child_access() {
    let mut table = SymbolTable::new();

    // Insert symbol in root scope
    let root_symbol = Symbol::Package {
        scope_id: 0,
        source_file: None,
        span: None,
        name: "Root".to_string(),
        qualified_name: "Root".to_string(),
    };

    table.insert("Root".to_string(), root_symbol).unwrap();

    // Enter child scope and add a symbol there
    table.enter_scope();
    let child_symbol = Symbol::Package {
        scope_id: 1,
        source_file: None,
        span: None,
        name: "Child".to_string(),
        qualified_name: "Child".to_string(),
    };

    table.insert("Child".to_string(), child_symbol).unwrap();

    // Lookup from root scope should NOT find child's symbol
    // Using resolve_from_scope_direct to test scope-chain isolation (no global lookup)
    let resolver = Resolver::new(&table);
    let found = resolver.resolve_from_scope_direct("Child", 0);
    assert!(found.is_none());
}

/// Test lookup with alias symbols (note: lookup_from_scope doesn't resolve aliases)
#[test]
fn test_lookup_from_scope_with_alias() {
    let mut table = SymbolTable::new();

    // Add a real symbol
    table
        .insert(
            "RealSymbol".to_string(),
            Symbol::Package {
                scope_id: 0,
                source_file: None,
                span: None,
                name: "RealSymbol".to_string(),
                qualified_name: "RealSymbol".to_string(),
            },
        )
        .unwrap();

    // Add an alias in child scope
    let child_scope = table.enter_scope();
    table
        .insert(
            "AliasSymbol".to_string(),
            Symbol::Alias {
                scope_id: 1,
                source_file: None,
                span: None,
                name: "AliasSymbol".to_string(),
                qualified_name: "AliasSymbol".to_string(),
                target: "RealSymbol".to_string(),
                target_span: None,
            },
        )
        .unwrap();

    // lookup_from_scope should find the alias (doesn't resolve it)
    let resolver = Resolver::new(&table);
    let found = resolver.resolve_in_scope("AliasSymbol", child_scope);
    assert!(found.is_some());
    assert!(matches!(found.unwrap(), Symbol::Alias { .. }));

    // Should also find the real symbol from child scope
    let resolver = Resolver::new(&table);
    let real = resolver.resolve_in_scope("RealSymbol", child_scope);
    assert!(real.is_some());
    assert!(matches!(real.unwrap(), Symbol::Package { .. }));
}

/// Test lookup with Definition and Usage symbols
#[test]
fn test_lookup_from_scope_definition_and_usage() {
    let mut table = SymbolTable::new();

    // Add definition at root
    table
        .insert(
            "MyDef".to_string(),
            Symbol::Definition {
                scope_id: 0,
                source_file: None,
                span: None,
                name: "MyDef".to_string(),
                qualified_name: "MyDef".to_string(),
                kind: "Part".to_string(),
                semantic_role: None,
            },
        )
        .unwrap();

    // Add usage in child scope
    let child_scope = table.enter_scope();
    table
        .insert(
            "MyUsage".to_string(),
            Symbol::Usage {
                scope_id: 1,
                source_file: None,
                span: None,
                usage_type: None,
                semantic_role: None,
                name: "MyUsage".to_string(),
                qualified_name: "MyUsage".to_string(),
                kind: "Part".to_string(),
            },
        )
        .unwrap();

    // From child scope, should find both
    let resolver = Resolver::new(&table);
    let def = resolver.resolve_from_scope_direct("MyDef", child_scope);
    assert!(def.is_some());
    assert!(matches!(def.unwrap(), Symbol::Definition { .. }));

    let usage = resolver.resolve_from_scope_direct("MyUsage", child_scope);
    assert!(usage.is_some());
    assert!(matches!(usage.unwrap(), Symbol::Usage { .. }));

    // From root scope, should only find definition
    // Using resolve_from_scope_direct to test scope-chain isolation (no global lookup)
    let def_from_root = resolver.resolve_from_scope_direct("MyDef", 0);
    assert!(def_from_root.is_some());

    let usage_from_root = resolver.resolve_from_scope_direct("MyUsage", 0);
    assert!(usage_from_root.is_none());
}

/// Test lookup multiple times from same scope (idempotence)
#[test]
fn test_lookup_from_scope_idempotent() {
    let mut table = SymbolTable::new();

    let symbol = Symbol::Package {
        scope_id: 0,
        source_file: None,
        span: None,
        name: "Symbol".to_string(),
        qualified_name: "Symbol".to_string(),
    };

    table.insert("Symbol".to_string(), symbol).unwrap();

    // Multiple lookups should return the same result
    let resolver = Resolver::new(&table);
    let found1 = resolver.resolve_in_scope("Symbol", 0);
    let resolver = Resolver::new(&table);
    let found2 = resolver.resolve_in_scope("Symbol", 0);
    let resolver = Resolver::new(&table);
    let found3 = resolver.resolve_in_scope("Symbol", 0);

    assert!(found1.is_some());
    assert!(found2.is_some());
    assert!(found3.is_some());
    assert_eq!(found1.unwrap().name(), found2.unwrap().name());
    assert_eq!(found2.unwrap().name(), found3.unwrap().name());
}