syster-base 0.4.0-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
541
542
543
544
545
546
547
548
549
550
551
552
553
554
use super::*;

// =============================================================================
// SysML-specific parsing functions (called from trait implementations)
// =============================================================================

/// ConstraintBody = ';' | '{' Expression '}'
/// Per pest: constraint_body = { ";" | ("{" ~ constraint_body_part ~ "}") }
/// Per pest: constraint_body_part = { definition_body_item* ~ (visible_annotating_member* ~ owned_expression)? }
/// Pattern: semicolon | { [members]* [expression] }
pub fn parse_constraint_body<P: SysMLParser>(p: &mut P) {
    p.start_node(SyntaxKind::CONSTRAINT_BODY);

    if p.at(SyntaxKind::SEMICOLON) {
        p.bump();
    } else if p.at(SyntaxKind::L_BRACE) {
        p.bump();
        p.skip_trivia();

        // Per pest grammar: constraint_body_part = definition_body_item* ~ (visible_annotating_member* ~ owned_expression)?
        // This means we can have doc comments, imports, parameters, etc. before the expression
        while !p.at(SyntaxKind::R_BRACE) && !p.at(SyntaxKind::ERROR) {
            // Check for annotations (doc, comment, etc.)
            if p.at(SyntaxKind::COMMENT_KW)
                || p.at(SyntaxKind::DOC_KW)
                || p.at(SyntaxKind::LOCALE_KW)
            {
                parse_annotation(p);
                p.skip_trivia();
            }
            // Check for textual representation
            else if p.at(SyntaxKind::REP_KW) {
                parse_textual_representation(p);
                p.skip_trivia();
            }
            // Check for parameters (in, out, inout, return)
            else if p.at(SyntaxKind::IN_KW)
                || p.at(SyntaxKind::OUT_KW)
                || p.at(SyntaxKind::INOUT_KW)
                || p.at(SyntaxKind::RETURN_KW)
            {
                // Parse as usage which handles parameters
                parse_usage(p);
                p.skip_trivia();
            }
            // Check for if expression (not if action)
            // IF_KW can start either expression or action, but in constraint bodies it's an expression
            else if p.at(SyntaxKind::IF_KW) {
                parse_expression(p);
                p.skip_trivia();
                break;
            }
            // Check for usage members (attribute, part, etc.) that can appear in constraint bodies
            else if p.at_any(SYSML_USAGE_KEYWORDS) {
                // Constraint bodies can contain attribute/part/etc. member declarations
                parse_usage(p);
                p.skip_trivia();
            }
            // Check for shorthand redefines/subsets operators
            else if p.at(SyntaxKind::COLON_GT_GT)
                || p.at(SyntaxKind::COLON_GT)
                || p.at(SyntaxKind::REDEFINES_KW)
                || p.at(SyntaxKind::SUBSETS_KW)
            {
                // Shorthand member like :>> name = value;
                parse_redefines_feature_member(p);
                p.skip_trivia();
            }
            // Check for shorthand feature declaration: name : Type;
            // This is common in constraint bodies for local features
            else if p.at_name_token() {
                // Lookahead to check if this is a feature declaration or expression start
                let lookahead = skip_trivia_lookahead(p, 1);
                if p.peek_kind(lookahead) == SyntaxKind::COLON {
                    // It's a shorthand feature: name : Type;
                    bump_keyword(p); // name
                    bump_keyword(p); // :
                    parse_qualified_name_and_skip(p); // Type
                    consume_if(p, SyntaxKind::SEMICOLON);
                    // Continue to check for more members
                } else {
                    // Not a feature declaration, must be the constraint expression
                    parse_expression(p);
                    p.skip_trivia();
                    break;
                }
            }
            // Otherwise, parse the expression (the actual constraint)
            else if p.can_start_expression() {
                parse_expression(p);
                p.skip_trivia();
                break; // Expression is the last item
            }
            // If we can't parse anything, break to avoid infinite loop
            else {
                break;
            }
        }

        p.expect(SyntaxKind::R_BRACE);
    } else {
        error_missing_body_terminator(p, "constraint");
    }

    p.finish_node();
}

/// Textual representation: rep <name> language <string> or just language <string>
pub(super) fn parse_textual_representation<P: SysMLParser>(p: &mut P) {
    p.start_node(SyntaxKind::TEXTUAL_REPRESENTATION);

    // Optional 'rep' keyword with name
    if p.at(SyntaxKind::REP_KW) {
        bump_keyword(p); // rep

        // Name (e.g., inOCL)
        if p.at_name_token() {
            bump_keyword(p);
        }
    }

    // 'language' keyword
    if p.at(SyntaxKind::LANGUAGE_KW) {
        bump_keyword(p);

        // Language string (e.g., "ocl", "alf")
        if p.at(SyntaxKind::STRING) {
            bump_keyword(p);
        }
    }

    // The actual code is in a comment block, which is trivia
    // So we don't need to explicitly parse it

    p.finish_node();
}

/// Definition or Usage - determined by presence of 'def' keyword
/// Per pest: package_body_item = { (metadata_usage | visibility_prefix? ~ (package_member | import_alias)) ~ ";"? }
pub fn parse_dependency<P: SysMLParser>(p: &mut P) {
    p.start_node(SyntaxKind::DEPENDENCY);

    expect_and_skip(p, SyntaxKind::DEPENDENCY_KW);

    // Check for identification followed by 'from', or just 'from', or direct source
    if p.at(SyntaxKind::FROM_KW) {
        // No identification, just 'from source'
        bump_keyword(p);
    } else if p.at_name_token() && !p.at(SyntaxKind::TO_KW) {
        // Could be identification (if followed by 'from') or direct source
        // Peek ahead to see if 'from' follows
        let next = p.peek_kind(1);
        if next == SyntaxKind::FROM_KW {
            // It's an identification: dependency myDep from source to target
            p.parse_identification();
            p.skip_trivia();
            expect_and_skip(p, SyntaxKind::FROM_KW);
        }
        // Otherwise it's a direct source: dependency source to target
    }

    // Parse source(s)
    if p.at_name_token() && !p.at(SyntaxKind::TO_KW) {
        parse_qualified_name_and_skip(p);

        // Multiple sources separated by comma
        while p.at(SyntaxKind::COMMA) {
            bump_keyword(p);
            if p.at_name_token() && !p.at(SyntaxKind::TO_KW) {
                parse_qualified_name_and_skip(p);
            }
        }
    }

    // 'to' target(s)
    if p.at(SyntaxKind::TO_KW) {
        bump_keyword(p);
        parse_qualified_name_and_skip(p);

        // Multiple targets separated by comma
        while p.at(SyntaxKind::COMMA) {
            bump_keyword(p);
            if p.at_name_token() {
                parse_qualified_name_and_skip(p);
            }
        }
    }

    p.parse_body();
    p.finish_node();
}

/// Filter = 'filter' Expression ';'
pub fn parse_filter<P: SysMLParser>(p: &mut P) {
    p.start_node(SyntaxKind::ELEMENT_FILTER_MEMBER);

    p.expect(SyntaxKind::FILTER_KW);
    p.skip_trivia();

    // Parse the filter expression (can be metadata reference or general expression)
    // Examples:
    // - filter @Safety;
    // - filter @Safety or @Security;
    // - filter @Safety and Safety::isMandatory;
    parse_expression(p);

    p.skip_trivia();
    p.expect(SyntaxKind::SEMICOLON);
    p.finish_node();
}

/// MetadataUsage = '@' QualifiedName ...
/// Per pest: metadata_usage = { "@" ~ qualified_name ~ ("about" ~ qualified_name_list)? ~ (";"|metadata_body) }
/// Pattern: @ <qualified_name> [about <references>] <body|semicolon>
/// Also handles prefix annotations: @Metadata part x; where the metadata annotates the part
pub fn parse_metadata_usage<P: SysMLParser>(p: &mut P) {
    p.start_node(SyntaxKind::METADATA_USAGE);

    p.expect(SyntaxKind::AT);
    p.skip_trivia();
    p.parse_qualified_name();
    p.skip_trivia();

    // Optional 'about' clause
    if p.at(SyntaxKind::ABOUT_KW) {
        p.bump();
        p.skip_trivia();
        p.parse_qualified_name_list();
        p.skip_trivia();
    }

    // Check if this is a prefix annotation (followed by a definition/usage keyword)
    // In that case, the metadata annotates the following element
    if is_definition_or_usage_start(p) {
        // This is a prefix annotation - finish the metadata node and parse the annotated element
        p.finish_node();
        p.parse_definition_or_usage();
        return;
    }

    parse_body_or_semicolon(p);

    p.finish_node();
}

/// Check if the current token could start a definition or usage
fn is_definition_or_usage_start<P: SysMLParser>(p: &P) -> bool {
    p.at_any(&[
        // SysML definition/usage keywords
        SyntaxKind::PART_KW,
        SyntaxKind::ATTRIBUTE_KW,
        SyntaxKind::PORT_KW,
        SyntaxKind::ITEM_KW,
        SyntaxKind::STATE_KW,
        SyntaxKind::OCCURRENCE_KW,
        SyntaxKind::CONSTRAINT_KW,
        SyntaxKind::REQUIREMENT_KW,
        SyntaxKind::CASE_KW,
        SyntaxKind::CALC_KW,
        SyntaxKind::CONNECTION_KW,
        SyntaxKind::INTERFACE_KW,
        SyntaxKind::ALLOCATION_KW,
        SyntaxKind::VIEW_KW,
        SyntaxKind::ACTION_KW,
        SyntaxKind::VIEWPOINT_KW,
        SyntaxKind::RENDERING_KW,
        SyntaxKind::METADATA_KW,
        SyntaxKind::ENUM_KW,
        SyntaxKind::ANALYSIS_KW,
        SyntaxKind::VERIFICATION_KW,
        SyntaxKind::USE_KW,
        SyntaxKind::CONCERN_KW,
        SyntaxKind::FLOW_KW,
        SyntaxKind::PARALLEL_KW,
        SyntaxKind::EVENT_KW,
        SyntaxKind::MESSAGE_KW,
        SyntaxKind::SNAPSHOT_KW,
        SyntaxKind::TIMESLICE_KW,
        // Prefix keywords
        SyntaxKind::ABSTRACT_KW,
        SyntaxKind::VARIATION_KW,
        SyntaxKind::INDIVIDUAL_KW,
        SyntaxKind::DERIVED_KW,
        SyntaxKind::READONLY_KW,
        SyntaxKind::VAR_KW,
        SyntaxKind::REF_KW,
        SyntaxKind::COMPOSITE_KW,
        SyntaxKind::PORTION_KW,
        SyntaxKind::IN_KW,
        SyntaxKind::OUT_KW,
        SyntaxKind::INOUT_KW,
        SyntaxKind::END_KW,
    ])
}

/// BindUsage = 'bind' connector_end '=' connector_end body
/// e.g., bind start = done { ... }
/// Per pest: binding_connector = { "bind" ~ connector_end ~ "=" ~ connector_end ~ (";"|connector_body) }
/// Per pest: connector_end = { multiplicity? ~ owned_feature_chain }
/// Pattern: bind [mult] <source> = [mult] <target> <body|semicolon>
pub fn parse_variant_usage<P: SysMLParser>(p: &mut P) {
    p.start_node(SyntaxKind::USAGE);

    p.expect(SyntaxKind::VARIANT_KW);
    p.skip_trivia();

    // Optional usage keyword (e.g., variant part x, variant action a1, variant use case uc1)
    if p.at(SyntaxKind::USE_KW) {
        p.bump(); // use
        p.skip_trivia();
        if p.at(SyntaxKind::CASE_KW) {
            p.bump(); // case
            p.skip_trivia();
        }
    } else if p.at_any(SYSML_USAGE_KEYWORDS) {
        p.bump();
        p.skip_trivia();
    }

    if p.at_name_token() || p.at(SyntaxKind::LT) {
        p.parse_identification();
        p.skip_trivia();
    }

    // Multiplicity (e.g., variant part withSunroof[1])
    if p.at(SyntaxKind::L_BRACKET) {
        p.parse_multiplicity();
        p.skip_trivia();
    }

    parse_optional_typing(p);

    parse_specializations_with_skip(p);

    if p.at(SyntaxKind::L_BRACE) {
        p.parse_body();
    } else {
        p.expect(SyntaxKind::SEMICOLON);
    }

    p.finish_node();
}

/// Redefines feature member\n/// Per pest: owned_feature_member = { visibility_prefix? ~ (owned_feature_declaration|owned_redefinition) ~ value_part? ~ (body|\";\") }\n/// Per pest: owned_redefinition = { usage_prefix* ~ (\":>>\" ~ qualified_name_list | \"subsets\" ~ qualified_name_list) }\n/// Pattern: [prefixes] :>>|subsets <name>[,<name>]* [typing] [mult] [specializations] [default] <body|semicolon>
pub fn parse_redefines_feature_member<P: SysMLParser>(p: &mut P) {
    p.start_node(SyntaxKind::USAGE);

    // Handle optional prefix (e.g., ref :>> name)
    while p.at_any(USAGE_PREFIX_KEYWORDS) {
        p.bump();
        p.skip_trivia();
    }

    // Wrap in SPECIALIZATION node so AST can extract the relationship
    p.start_node(SyntaxKind::SPECIALIZATION);
    p.bump(); // redefines/subsets operator
    p.skip_trivia();

    if p.at_name_token() {
        p.parse_qualified_name();
        p.skip_trivia();
    }
    p.finish_node(); // finish first SPECIALIZATION

    // Handle comma-separated qualified names for :>> A, B pattern
    while p.at(SyntaxKind::COMMA) {
        p.bump();
        p.skip_trivia();
        p.start_node(SyntaxKind::SPECIALIZATION);
        p.parse_qualified_name();
        p.skip_trivia();
        p.finish_node();
    }

    parse_optional_typing(p);

    parse_optional_multiplicity(p);

    parse_specializations_with_skip(p);

    // Default value or assignment
    parse_optional_default_value(p);

    if p.at(SyntaxKind::L_BRACE) {
        p.parse_body();
    } else {
        p.expect(SyntaxKind::SEMICOLON);
    }

    p.finish_node();
}

/// Shorthand feature member
/// Parse anonymous usage: `: Type;` or `typed by Type;`
/// This is an anonymous feature/usage that has no name, just a type
pub(super) fn parse_anonymous_usage<P: SysMLParser>(p: &mut P) {
    p.start_node(SyntaxKind::USAGE);

    // Parse typing (: Type or typed by Type)
    p.parse_typing();
    p.skip_trivia();

    // Optional specializations
    parse_specializations_with_skip(p);

    // Optional value assignment
    parse_optional_default_value(p);

    // Body or semicolon
    if p.at(SyntaxKind::L_BRACE) {
        p.parse_body();
    } else {
        p.expect(SyntaxKind::SEMICOLON);
    }

    p.finish_node();
}

pub fn parse_shorthand_feature_member<P: SysMLParser>(p: &mut P) {
    p.start_node(SyntaxKind::USAGE);

    // Check if there's a keyword prefix (actor, subject, stakeholder, etc.)
    if matches!(
        p.current_kind(),
        SyntaxKind::ACTOR_KW
            | SyntaxKind::SUBJECT_KW
            | SyntaxKind::STAKEHOLDER_KW
            | SyntaxKind::OBJECTIVE_KW
            | SyntaxKind::FILTER_KW
    ) {
        p.bump(); // Consume the keyword
        p.skip_trivia();
    }

    p.parse_identification();
    p.skip_trivia();

    if p.at(SyntaxKind::L_BRACKET) {
        p.parse_multiplicity();
        p.skip_trivia();
    }

    // Only COLON is typing; COLON_GT and COLON_GT_GT are specializations
    parse_optional_typing(p);

    parse_specializations_with_skip(p);

    parse_optional_default_value(p);

    if p.at(SyntaxKind::L_BRACE) {
        p.parse_body();
    } else {
        p.expect(SyntaxKind::SEMICOLON);
    }

    p.finish_node();
}

// =============================================================================
// Definition/Usage Classification & Dispatch
// =============================================================================

/// Per pest: package_member = { (definition | usage | alias_member | ...) }
/// Pattern: Determines whether to parse as definition (has 'def') or usage (no 'def')
pub fn parse_definition_or_usage<P: SysMLParser>(p: &mut P) {
    if has_def_keyword(p) {
        parse_definition(p);
    } else {
        parse_usage(p);
    }
}

/// Scan ahead (skipping trivia) to determine if this declaration has a 'def' keyword
fn has_def_keyword<P: SysMLParser>(p: &P) -> bool {
    for i in 0..20 {
        let kind = p.peek_kind(i);

        if kind == SyntaxKind::DEF_KW {
            return true;
        }

        // Stop scanning at statement-ending tokens
        if kind == SyntaxKind::SEMICOLON
            || kind == SyntaxKind::L_BRACE
            || kind == SyntaxKind::COLON
            || kind == SyntaxKind::COLON_GT
            || kind == SyntaxKind::COLON_GT_GT
            || kind == SyntaxKind::EQ
            || kind == SyntaxKind::ERROR
        {
            return false;
        }
    }
    false
}

fn parse_definition<P: SysMLParser>(p: &mut P) {
    // Per pest: definition = { prefix* ~ definition_declaration ~ definition_body }
    // Pattern: [abstract|variation|individual] <keyword> def <name> <specializations> <body>
    p.start_node(SyntaxKind::DEFINITION);

    // Prefixes (variation point and individual markers)
    while p.at(SyntaxKind::ABSTRACT_KW)
        || p.at(SyntaxKind::VARIATION_KW)
        || p.at(SyntaxKind::INDIVIDUAL_KW)
    {
        bump_keyword(p);
    }

    let is_constraint = p.at(SyntaxKind::CONSTRAINT_KW);
    let is_calc = p.at(SyntaxKind::CALC_KW);
    let is_action = p.at(SyntaxKind::ACTION_KW);
    let is_state = p.at(SyntaxKind::STATE_KW);
    let is_analysis = p.at(SyntaxKind::ANALYSIS_KW);
    let is_verification = p.at(SyntaxKind::VERIFICATION_KW);
    let is_metadata = p.at(SyntaxKind::METADATA_KW);
    let is_usecase = p.at(SyntaxKind::USE_KW); // use case def

    // Definition keyword
    parse_definition_keyword(p);
    p.skip_trivia();

    // 'def' keyword (or 'case def' for analysis/verification)
    consume_if(p, SyntaxKind::CASE_KW);
    expect_and_skip(p, SyntaxKind::DEF_KW);

    // Identification
    if p.at(SyntaxKind::IDENT) || p.at(SyntaxKind::LT) {
        p.parse_identification();
    }
    p.skip_trivia();

    // Specializations
    parse_specializations_with_skip(p);

    // Body
    if is_constraint {
        parse_constraint_body(p);
    } else if is_calc {
        parse_sysml_calc_body(p);
    } else if is_action {
        parse_action_body(p);
    } else if is_state {
        parse_state_body(p);
    } else if is_analysis || is_verification || is_usecase {
        parse_case_body(p);
    } else if is_metadata {
        parse_metadata_body(p);
    } else {
        p.parse_body();
    }

    p.finish_node();
}