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
use super::*;

// =============================================================================
// Usages — features, steps, expressions, parameters, end features
// =============================================================================

/// Parse feature prefix modifiers (var, composite, const, etc.)
/// Per pest: feature_prefix_modifiers = { (abstract_token | composite_token | portion_token | member_token | const_modifier | derived | end_marker | variable_marker)* }
pub fn parse_feature_prefix_modifiers<P: KerMLParser>(p: &mut P) {
    while p.at_any(&[
        SyntaxKind::VAR_KW,
        SyntaxKind::COMPOSITE_KW,
        SyntaxKind::PORTION_KW,
        SyntaxKind::MEMBER_KW,
        SyntaxKind::ABSTRACT_KW,
        SyntaxKind::DERIVED_KW,
        SyntaxKind::CONST_KW,
        SyntaxKind::END_KW,
        SyntaxKind::VARIATION_KW,
        SyntaxKind::READONLY_KW,
        SyntaxKind::REF_KW,
    ]) {
        p.bump();
        p.skip_trivia();
    }

    while p.at(SyntaxKind::HASH) {
        parse_prefix_metadata(p);
        p.skip_trivia();
    }
}

/// Parse optional feature keyword (feature, step, expr, inv)
/// Per pest: invariant = { prefix_metadata? ~ inv_token ~ not_token? ~ identification? ~ ... }
fn parse_optional_feature_keyword<P: KerMLParser>(p: &mut P) -> bool {
    if p.at(SyntaxKind::INV_KW) {
        p.bump();
        p.skip_trivia();
        // Per pest: inv_token ~ not_token? - handle optional 'not' after 'inv'
        if p.at(SyntaxKind::NOT_KW) {
            p.bump();
            p.skip_trivia();
        }
        true
    } else if p.at_any(&[
        SyntaxKind::FEATURE_KW,
        SyntaxKind::STEP_KW,
        SyntaxKind::EXPR_KW,
    ]) {
        p.bump();
        true
    } else if p.at(SyntaxKind::IDENT) {
        if let Some(text) = p.current_token_text() {
            if text == "feature" || text == "step" || text == "expr" {
                p.bump();
                true
            } else {
                false
            }
        } else {
            false
        }
    } else {
        false
    }
}

/// Parse usage identification or specialization shortcuts
fn parse_usage_name_or_shorthand<P: KerMLParser>(p: &mut P) {
    if p.at(SyntaxKind::REDEFINES_KW)
        || p.at(SyntaxKind::COLON_GT_GT)
        || p.at(SyntaxKind::SUBSETS_KW)
        || p.at(SyntaxKind::COLON_GT)
    {
        // Wrap in SPECIALIZATION node so AST can extract the relationship
        p.start_node(SyntaxKind::SPECIALIZATION);
        bump_and_skip(p);
        parse_optional_qualified_name(p);
        p.finish_node();
        while p.at(SyntaxKind::COMMA) {
            bump_and_skip(p);
            p.start_node(SyntaxKind::SPECIALIZATION);
            parse_qualified_name_and_skip(p);
            p.finish_node();
        }
    } else if p.at_name_token() || p.at(SyntaxKind::LT) {
        // Check for "type name" pattern: two identifiers in a row
        // e.g., "bool signalCondition { }" = feature signalCondition : bool
        if p.at(SyntaxKind::IDENT) {
            let peek1 = p.peek_kind(1);
            if peek1 == SyntaxKind::IDENT || peek1 == SyntaxKind::LT {
                // First identifier is the type, create typing node
                p.start_node(SyntaxKind::TYPING);
                p.bump(); // type name
                p.skip_trivia();
                p.finish_node();
                // Second identifier is the feature name
                p.parse_identification();
                return;
            }
        }
        p.parse_identification();
    }
}

/// Parse usage details (multiplicity, typing, specializations, relationships)
fn parse_usage_details<P: KerMLParser>(p: &mut P) {
    p.skip_trivia();
    parse_optional_multiplicity(p);
    parse_optional_typing(p);
    parse_optional_multiplicity(p);
    // Per pest: ordering_modifiers can appear before or after specializations
    parse_ordering_modifiers(p);
    parse_specializations(p);
    p.skip_trivia();
    // Per SysML v2, multiplicity can also appear after specializations
    // e.g., `feature x redefines y [*] nonunique;`
    parse_optional_multiplicity(p);
    // Parse ordering modifiers again (can appear after specializations too)
    parse_ordering_modifiers(p);
    parse_feature_relationships(p);
    p.skip_trivia();
}

/// Parse ordering modifiers (ordered, nonunique)
fn parse_ordering_modifiers<P: KerMLParser>(p: &mut P) {
    while p.at(SyntaxKind::ORDERED_KW) || p.at(SyntaxKind::NONUNIQUE_KW) {
        bump_and_skip(p);
    }
}

/// Parse optional default value (= or := or default)
/// Per pest: feature_value = { ("=" | ":=" | default_token) ~ owning_membership }
fn parse_optional_default_value<P: KerMLParser>(p: &mut P) {
    if p.at(SyntaxKind::EQ) || p.at(SyntaxKind::COLON_EQ) || p.at(SyntaxKind::DEFAULT_KW) {
        bump_and_skip(p);
        super::kerml_expressions::parse_expression(p);
        p.skip_trivia();
    }
}

/// KerML usage (feature, step, expr)
/// Per pest: feature = { prefix_metadata? ~ visibility_kind? ~ feature_direction_kind? ~ feature_prefix_modifiers ~ feature_token ~ all_token? ~ identification? ~ feature_specialization_part? ~ ordering_modifiers ~ feature_relationship_part* ~ feature_value? ~ namespace_body }
/// Per pest: step = { prefix_metadata? ~ feature_direction_kind? ~ connector_feature_modifiers ~ step_token ~ identification? ~ feature_specialization_part? ~ feature_value? ~ membership? ~ owning_membership? ~ namespace_body }
/// Per pest: expression = similar to feature with expr_token
pub fn parse_usage_impl<P: KerMLParser>(p: &mut P) {
    p.start_node(SyntaxKind::USAGE);

    parse_feature_prefix_modifiers(p);

    let _consumed_feature_keyword = parse_optional_feature_keyword(p);
    p.skip_trivia();

    // Handle 'all' keyword after feature keyword (universal quantification)
    // Per SysML v2 Spec §7.3.3.4: feature all x means x covers all instances
    if p.at(SyntaxKind::ALL_KW) {
        p.bump();
        p.skip_trivia();
    }

    parse_usage_name_or_shorthand(p);

    parse_usage_details(p);

    parse_optional_default_value(p);

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

/// KerML invariant (inv [not]? name? { expression })
/// Per pest: invariant = { prefix_metadata? ~ inv_token ~ not_token? ~ identification? ~ invariant_body }
pub fn parse_invariant<P: KerMLParser>(p: &mut P) {
    p.start_node(SyntaxKind::USAGE);

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

    // Optional 'not'
    if p.at(SyntaxKind::NOT_KW) {
        p.bump();
        p.skip_trivia();
    }

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

    // Body: { expression }
    if p.at(SyntaxKind::L_BRACE) {
        p.start_node(SyntaxKind::NAMESPACE_BODY);
        p.bump(); // {
        p.skip_trivia();

        // Handle doc comment before expression (common in stdlib)
        if p.at(SyntaxKind::DOC_KW) || p.at(SyntaxKind::COMMENT_KW) {
            parse_annotation(p);
            p.skip_trivia();
        }

        // Parse the invariant expression
        if !p.at(SyntaxKind::R_BRACE) {
            super::kerml_expressions::parse_expression(p);
        }

        p.skip_trivia();
        p.expect(SyntaxKind::R_BRACE);
        p.finish_node();
    } else {
        p.parse_body();
    }

    p.finish_node();
}

/// KerML parameter (in, out, inout, return)
/// Per pest: feature_direction_kind = { inout_token | in_token | out_token }
/// Per pest: parameter_membership = { direction ~ (type_name ~ name | name | ...) ~ ... }
/// Parameters are features with explicit direction keywords
pub fn parse_parameter_impl<P: KerMLParser>(p: &mut P) {
    p.start_node(SyntaxKind::USAGE);

    // Parse parameter direction keyword
    if p.at_any(&[
        SyntaxKind::IN_KW,
        SyntaxKind::OUT_KW,
        SyntaxKind::INOUT_KW,
        SyntaxKind::END_KW,
        SyntaxKind::RETURN_KW,
    ]) {
        p.bump();
    }
    p.skip_trivia();

    parse_feature_prefix_modifiers(p);

    // Optional usage keyword
    if p.at_any(&[
        SyntaxKind::ATTRIBUTE_KW,
        SyntaxKind::PART_KW,
        SyntaxKind::ITEM_KW,
        SyntaxKind::CALC_KW,
        SyntaxKind::ACTION_KW,
        SyntaxKind::STATE_KW,
        SyntaxKind::PORT_KW,
        SyntaxKind::FEATURE_KW,
        SyntaxKind::STEP_KW,
        SyntaxKind::EXPR_KW,
    ]) {
        bump_and_skip(p);
    }

    // Per pest grammar, parameters can have: type_name name | name | ...
    // Check for two identifiers in a row (type + name pattern)
    if p.at(SyntaxKind::IDENT) {
        let peek1 = p.peek_kind(1);
        if peek1 == SyntaxKind::IDENT {
            // First identifier is the type, bump it
            p.bump();
            p.skip_trivia();
            // Second identifier is the name
            if p.at_name_token() {
                p.parse_identification();
            }
        } else {
            // Just a name (or starts with shorthand)
            parse_usage_name_or_shorthand(p);
        }
    } else {
        parse_usage_name_or_shorthand(p);
    }

    parse_usage_details(p);

    parse_optional_default_value(p);

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

/// Parse multiplicity with ordering modifiers
fn parse_multiplicity_with_ordering<P: KerMLParser>(p: &mut P) {
    if p.at(SyntaxKind::L_BRACKET) {
        parse_multiplicity(p);
        p.skip_trivia();

        while p.at(SyntaxKind::ORDERED_KW) || p.at(SyntaxKind::NONUNIQUE_KW) {
            bump_and_skip(p);
        }
    }
}

/// Parse all prefix modifiers (ref, readonly, derived, etc.)
fn parse_prefix_modifiers<P: KerMLParser>(p: &mut P) {
    while p.at_any(&[
        SyntaxKind::REF_KW,
        SyntaxKind::READONLY_KW,
        SyntaxKind::DERIVED_KW,
        SyntaxKind::ABSTRACT_KW,
        SyntaxKind::VARIATION_KW,
        SyntaxKind::VAR_KW,
        SyntaxKind::COMPOSITE_KW,
        SyntaxKind::PORTION_KW,
        SyntaxKind::INDIVIDUAL_KW,
    ]) {
        bump_and_skip(p);
    }
}

/// Parse optional SysML usage keyword (item, part, action, etc.)
fn parse_optional_sysml_usage_keyword<P: KerMLParser>(p: &mut P) {
    if p.at_any(&[
        SyntaxKind::ITEM_KW,
        SyntaxKind::PART_KW,
        SyntaxKind::ACTION_KW,
        SyntaxKind::STATE_KW,
        SyntaxKind::ATTRIBUTE_KW,
        SyntaxKind::PORT_KW,
    ]) {
        bump_and_skip(p);
    }
}

/// Parse end feature prefix (metadata and modifiers)
fn parse_end_feature_prefix<P: KerMLParser>(p: &mut P) {
    while p.at(SyntaxKind::HASH) {
        parse_prefix_metadata(p);
        p.skip_trivia();
    }

    parse_prefix_modifiers(p);
    parse_multiplicity_with_ordering(p);
    parse_optional_sysml_usage_keyword(p);
}

/// Parse feature details (identification, typing, specializations)
fn parse_feature_details<P: KerMLParser>(p: &mut P, parse_id: bool) {
    if parse_id {
        parse_optional_identification(p);
    }

    parse_multiplicity_with_ordering(p);

    parse_optional_typing(p);

    parse_specializations(p);
    p.skip_trivia();

    parse_feature_relationships(p);
    p.skip_trivia();
}

/// Parse end feature when FEATURE_KW is present
fn parse_end_feature_with_keyword<P: KerMLParser>(p: &mut P) {
    bump_and_skip(p);

    // Check for specialization-first pattern or identification
    if p.at(SyntaxKind::REDEFINES_KW)
        || p.at(SyntaxKind::COLON_GT_GT)
        || p.at(SyntaxKind::SUBSETS_KW)
        || p.at(SyntaxKind::COLON_GT)
        || p.at(SyntaxKind::REFERENCES_KW)
        || p.at(SyntaxKind::COLON_COLON_GT)
    {
        parse_feature_details(p, false);
    } else {
        parse_feature_details(p, true);
    }
}

/// Parse typing and relationships without FEATURE keyword
fn parse_typing_and_relationships<P: KerMLParser>(p: &mut P) {
    parse_optional_typing(p);
    parse_specializations(p);
    p.skip_trivia();
    parse_feature_relationships(p);
    p.skip_trivia();
}

/// Parse end feature when starting with name/identification
fn parse_end_feature_with_name<P: KerMLParser>(p: &mut P) {
    parse_identification_and_skip(p);
    parse_multiplicity_with_ordering(p);
    parse_specializations(p);
    p.skip_trivia();
    parse_feature_relationships(p);
    p.skip_trivia();

    if p.at(SyntaxKind::FEATURE_KW) {
        bump_and_skip(p);
        parse_feature_details(p, true);
    } else {
        parse_typing_and_relationships(p);
    }
}

/// Parse minimal end feature (no name, no FEATURE_KW initially)
fn parse_end_feature_minimal<P: KerMLParser>(p: &mut P) {
    parse_multiplicity_with_ordering(p);
    parse_specializations(p);
    p.skip_trivia();

    if p.at(SyntaxKind::FEATURE_KW) {
        bump_and_skip(p);
        parse_feature_details(p, true);
    }
}

/// End feature or parameter
/// Per pest: end_feature = { prefix_metadata? ~ const_token? ~ end_marker ~ (...various patterns...) ~ feature_value? ~ namespace_body }
/// Per pest: EndFeaturePrefix = ( isConstant ?= 'const')? isEnd ?= 'end'
pub fn parse_end_feature_or_parameter<P: KerMLParser>(p: &mut P) {
    p.start_node(SyntaxKind::USAGE);

    consume_if(p, SyntaxKind::CONST_KW);
    expect_and_skip(p, SyntaxKind::END_KW);

    parse_end_feature_prefix(p);

    if p.at(SyntaxKind::FEATURE_KW) {
        parse_end_feature_with_keyword(p);
    } else if p.at_name_token() || p.at(SyntaxKind::LT) {
        // Check for "type name" shorthand pattern: `end bool constrainedGuard;`
        // Two identifiers in a row means first is type, second is name
        if p.at(SyntaxKind::IDENT) {
            let peek1 = p.peek_kind(1);
            if peek1 == SyntaxKind::IDENT || peek1 == SyntaxKind::LT {
                // First identifier is the type, create typing node
                p.start_node(SyntaxKind::TYPING);
                p.bump(); // type name
                p.skip_trivia();
                p.finish_node();
                // Second identifier is the feature name
                p.parse_identification();
                // Continue with end feature details
                parse_multiplicity_with_ordering(p);
                parse_specializations(p);
                p.skip_trivia();
                parse_feature_relationships(p);
                p.skip_trivia();
            } else {
                parse_end_feature_with_name(p);
            }
        } else {
            parse_end_feature_with_name(p);
        }
    } else {
        parse_end_feature_minimal(p);
    }

    parse_optional_default_value(p);

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