syster-base 0.1.11-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
#![allow(clippy::unwrap_used)]

use super::super::*;

/// Test finding a symbol in the current scope (first in chain)
#[test]
fn test_find_in_current_scope() {
    let mut table = SymbolTable::new();

    // Insert symbol in root scope (scope 0)
    let symbol = Symbol::Package {
        documentation: None,
        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_mut should find the symbol using find_in_scope_chain
    let found = table.lookup_mut("RootSymbol");
    assert!(found.is_some());
    assert_eq!(found.unwrap().name(), "RootSymbol");
}

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

    // Insert symbol in root scope
    let parent_symbol = Symbol::Package {
        documentation: None,
        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
    table.enter_scope();

    // lookup_mut from child scope should find symbol in parent
    let found = table.lookup_mut("ParentSymbol");
    assert!(found.is_some());
    assert_eq!(found.unwrap().name(), "ParentSymbol");
}

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

    // Insert symbol in root scope (scope 0)
    let root_symbol = Symbol::Package {
        documentation: None,
        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)
    table.enter_scope();

    // lookup_mut from grandchild should find symbol in grandparent
    let found = table.lookup_mut("GrandparentSymbol");
    assert!(found.is_some());
    assert_eq!(found.unwrap().name(), "GrandparentSymbol");
}

/// Test that symbol not found in chain returns None
#[test]
fn test_symbol_not_found_in_chain() {
    let mut table = SymbolTable::new();

    // Insert a different symbol
    let symbol = Symbol::Package {
        documentation: None,
        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
    let found = table.lookup_mut("NonExistentSymbol");
    assert!(found.is_none());
}

/// Test that symbol in current scope takes precedence over parent scope
#[test]
fn test_symbol_precedence_current_over_parent() {
    let mut table = SymbolTable::new();

    // Insert symbol in root scope
    let parent_symbol = Symbol::Package {
        documentation: None,
        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
    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,
        documentation: None,
    };

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

    // lookup_mut should find the child scope symbol (first in chain)
    let found = table.lookup_mut("Symbol");
    assert!(found.is_some());
    let symbol = found.unwrap();
    assert_eq!(symbol.qualified_name(), "Parent::Child::Symbol");
    // Verify it's the Classifier type, not Package
    assert!(matches!(symbol, Symbol::Classifier { .. }));
}

/// Test mutable access to found symbol
#[test]
fn test_mutable_access_to_found_symbol() {
    let mut table = SymbolTable::new();

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

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

    // Get mutable reference and verify it exists
    let found = table.lookup_mut("MutableSymbol");
    assert!(found.is_some());
    assert_eq!(found.unwrap().name(), "MutableSymbol");
}

/// Test finding symbols in deeply nested scopes
#[test]
fn test_deeply_nested_scopes() {
    let mut table = SymbolTable::new();

    // Insert symbol at root (level 0)
    let root_symbol = Symbol::Package {
        documentation: None,
        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)
    for i in 1..=4 {
        table.enter_scope();
        let symbol = Symbol::Package {
            documentation: None,
            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
    assert!(table.lookup_mut("Level0").is_some());
    assert!(table.lookup_mut("Level1").is_some());
    assert!(table.lookup_mut("Level2").is_some());
    assert!(table.lookup_mut("Level3").is_some());
    assert!(table.lookup_mut("Level4").is_some());

    // Exit to level 2
    table.exit_scope(); // level 3
    table.exit_scope(); // level 2

    // From level 2, we should not find Level3 or Level4
    assert!(table.lookup_mut("Level0").is_some());
    assert!(table.lookup_mut("Level1").is_some());
    assert!(table.lookup_mut("Level2").is_some());
    assert!(table.lookup_mut("Level3").is_none());
    assert!(table.lookup_mut("Level4").is_none());
}

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

    // Package at root
    table
        .insert(
            "RootPkg".to_string(),
            Symbol::Package {
                documentation: None,
                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,
                documentation: None,
            },
        )
        .unwrap();

    // Enter scope for feature
    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()),
                documentation: None,
            },
        )
        .unwrap();

    // From deepest scope, should find all three
    assert!(table.lookup_mut("RootPkg").is_some());
    assert!(table.lookup_mut("MyClass").is_some());
    assert!(table.lookup_mut("MyFeature").is_some());

    // Verify they are the correct types
    let pkg = table.lookup_mut("RootPkg").unwrap();
    assert!(matches!(
        pkg,
        Symbol::Package {
            documentation: None,
            ..
        }
    ));

    let class = table.lookup_mut("MyClass").unwrap();
    assert!(matches!(class, Symbol::Classifier { .. }));

    let feature = table.lookup_mut("MyFeature").unwrap();
    assert!(matches!(feature, Symbol::Feature { .. }));
}

/// Test that lookup_mut works correctly after entering and exiting scopes
#[test]
fn test_scope_chain_after_enter_exit() {
    let mut table = SymbolTable::new();

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

    // Enter scope 1
    table.enter_scope();
    table
        .insert(
            "Child1".to_string(),
            Symbol::Package {
                documentation: None,
                scope_id: 1,
                source_file: None,
                span: None,
                name: "Child1".to_string(),
                qualified_name: "Root::Child1".to_string(),
            },
        )
        .unwrap();

    // Enter scope 2
    table.enter_scope();
    table
        .insert(
            "Child2".to_string(),
            Symbol::Package {
                documentation: None,
                scope_id: 2,
                source_file: None,
                span: None,
                name: "Child2".to_string(),
                qualified_name: "Root::Child1::Child2".to_string(),
            },
        )
        .unwrap();

    // Verify all accessible from scope 2
    assert!(table.lookup_mut("Root").is_some());
    assert!(table.lookup_mut("Child1").is_some());
    assert!(table.lookup_mut("Child2").is_some());

    // Exit to scope 1
    table.exit_scope();
    assert!(table.lookup_mut("Root").is_some());
    assert!(table.lookup_mut("Child1").is_some());
    assert!(table.lookup_mut("Child2").is_none());

    // Re-enter scope 2
    table.enter_scope();
    // Child2 is not in the new scope 3, it was in scope 2
    assert!(table.lookup_mut("Child2").is_none());

    // Add it to new scope
    table
        .insert(
            "Child2New".to_string(),
            Symbol::Package {
                documentation: None,
                scope_id: 3,
                source_file: None,
                span: None,
                name: "Child2New".to_string(),
                qualified_name: "Root::Child1::Child2New".to_string(),
            },
        )
        .unwrap();

    assert!(table.lookup_mut("Child2New").is_some());
    assert!(table.lookup_mut("Child1").is_some());
    assert!(table.lookup_mut("Root").is_some());
}

/// Test with alias symbols in scope chain
#[test]
fn test_alias_symbols_in_chain() {
    let mut table = SymbolTable::new();

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

    // Add an alias in 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_mut should find the alias (note: it doesn't resolve aliases)
    let found = table.lookup_mut("AliasSymbol");
    assert!(found.is_some());
    assert!(matches!(found.unwrap(), Symbol::Alias { .. }));

    // Should still find the real symbol
    let real = table.lookup_mut("RealSymbol");
    assert!(real.is_some());
    assert!(matches!(
        real.unwrap(),
        Symbol::Package {
            documentation: None,
            ..
        }
    ));
}

/// Test with empty string as symbol name
#[test]
fn test_empty_string_name() {
    let mut table = SymbolTable::new();

    // Insert a symbol with empty name (edge case)
    let symbol = Symbol::Package {
        documentation: None,
        scope_id: 0,
        source_file: None,
        span: None,
        name: "".to_string(),
        qualified_name: "".to_string(),
    };

    // Should be able to insert and find empty string name
    table.insert("".to_string(), symbol).unwrap();

    let found = table.lookup_mut("");
    assert!(found.is_some());
    assert_eq!(found.unwrap().name(), "");
}

/// Test with special characters in symbol name
#[test]
fn test_special_characters_in_name() {
    let mut table = SymbolTable::new();

    // Test with various special characters that might appear in SysML names
    let special_names = vec![
        "name-with-dash",
        "name_with_underscore",
        "name::with::colons",
        "name.with.dots",
        "name123",
    ];

    for name in &special_names {
        let symbol = Symbol::Package {
            documentation: None,
            scope_id: 0,
            source_file: None,
            span: None,
            name: name.to_string(),
            qualified_name: name.to_string(),
        };

        table.insert(name.to_string(), symbol).unwrap();
    }

    // Verify all special names can be found
    for name in special_names {
        let found = table.lookup_mut(name);
        assert!(found.is_some(), "Should find symbol with name: {name}");
        assert_eq!(found.unwrap().name(), name);
    }
}

/// Test finding symbol when multiple scopes exist but symbol only in one
#[test]
fn test_symbol_in_middle_of_chain() {
    let mut table = SymbolTable::new();

    // Scope 0 - no symbols
    table.enter_scope(); // Scope 1

    // Scope 1 - add a symbol here
    let symbol = Symbol::Package {
        documentation: None,
        scope_id: 1,
        source_file: None,
        span: None,
        name: "MiddleSymbol".to_string(),
        qualified_name: "MiddleSymbol".to_string(),
    };
    table.insert("MiddleSymbol".to_string(), symbol).unwrap();

    table.enter_scope(); // Scope 2 - no symbols
    table.enter_scope(); // Scope 3 - no symbols

    // From scope 3, should find symbol in scope 1 (middle of chain)
    let found = table.lookup_mut("MiddleSymbol");
    assert!(found.is_some());
    assert_eq!(found.unwrap().name(), "MiddleSymbol");
}