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
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
use super::*;
pub fn parse_usage<P: SysMLParser>(p: &mut P) {
// Per pest: usage = { (usage_prefix* ~ metadata_prefix* ~ event_prefix? ~ usage_element) | owned_crossing_feature }
// Per pest: usage_element = { keyword ~ usage_declaration ~ value_part? ~ (body | ";") }
// Per pest: owned_crossing_feature = { "end" ~ (identifier ~ multiplicity?)? ~ keyword ~ usage_declaration }
// Pattern: [prefixes] [#metadata] [event] <keyword> [<name>] [<mult>] [<typing>] [<specializations>] [<default>] <body>
p.start_node(SyntaxKind::USAGE);
// Prefixes - returns true if END_KW was seen
let saw_end = parse_usage_prefix(p);
p.skip_trivia();
// Prefix metadata (after prefix keywords, before usage keyword)
while p.at(SyntaxKind::HASH) {
parse_prefix_metadata(p);
p.skip_trivia();
}
// Event modifier (event occurrence pattern)
if p.at(SyntaxKind::EVENT_KW) {
bump_keyword(p);
}
// Check for owned crossing feature after END_KW: end name [mult] usage_kw name
// If we see a name after END prefix (not a usage keyword), it's an owned_crossing_feature
if saw_end && p.at_name_token() {
// Parse: name [mult] usage_keyword name :> ... { }
p.parse_identification();
p.skip_trivia();
// Multiplicity
if p.at(SyntaxKind::L_BRACKET) {
p.parse_multiplicity();
p.skip_trivia();
}
// Now we expect a usage keyword
if p.at_any(SYSML_USAGE_KEYWORDS) {
parse_usage_keyword(p);
p.skip_trivia();
// Parse the actual feature name
if p.at_name_token() || p.at(SyntaxKind::LT) {
p.parse_identification();
p.skip_trivia();
}
// Continue with multiplicity, typing, specializations as normal
if p.at(SyntaxKind::L_BRACKET) {
p.parse_multiplicity();
p.skip_trivia();
}
if p.at(SyntaxKind::COLON) {
p.parse_typing();
p.skip_trivia();
}
parse_specializations(p);
p.skip_trivia();
// Ordering modifiers
while p.at(SyntaxKind::ORDERED_KW) || p.at(SyntaxKind::NONUNIQUE_KW) {
p.bump();
p.skip_trivia();
}
// Body
p.parse_body();
p.finish_node();
return;
}
}
// Check for owned crossing feature: end [mult] keyword ...
// If we see multiplicity before the usage keyword, parse it
if p.at(SyntaxKind::L_BRACKET) {
p.parse_multiplicity();
p.skip_trivia();
}
// Ordering modifiers that may appear before the usage keyword in end features
// Pattern: end [0..*] nonunique item selectedProduct: Product[1];
while p.at(SyntaxKind::ORDERED_KW) || p.at(SyntaxKind::NONUNIQUE_KW) {
bump_keyword(p);
}
let is_constraint = p.at(SyntaxKind::CONSTRAINT_KW);
let is_action = p.at(SyntaxKind::ACTION_KW);
let is_calc = p.at(SyntaxKind::CALC_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_message = p.at(SyntaxKind::MESSAGE_KW);
let is_usecase = p.at(SyntaxKind::USE_KW); // use case usage
let is_connection_kw = p.at(SyntaxKind::CONNECTION_KW);
let is_interface_kw = p.at(SyntaxKind::INTERFACE_KW);
// Usage keyword
parse_usage_keyword(p);
p.skip_trivia();
// Per pest: constraint_usage_declaration is optional (usage_declaration? ~ value_part?)
// So we can have just "requirement;" or "constraint;" with no name/typing/body content
// Check if we're at body start immediately after keyword
if p.at(SyntaxKind::SEMICOLON) || p.at(SyntaxKind::L_BRACE) {
// Minimal usage: just keyword + 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();
return;
}
// For message usages, handle 'of' keyword before identification
// Pattern: message of payload:Type from source to target;
if is_message {
consume_if(p, SyntaxKind::OF_KW);
}
// Handle shorthand redefines: 'attribute :>> name' (no identifier before :>>)
if p.at(SyntaxKind::COLON_GT_GT)
|| p.at(SyntaxKind::COLON_GT)
|| p.at(SyntaxKind::REDEFINES_KW)
|| p.at(SyntaxKind::SUBSETS_KW)
{
// This is a shorthand feature member after a usage keyword
// Wrap in SPECIALIZATION node so AST can extract the relationship
p.start_node(SyntaxKind::SPECIALIZATION);
bump_keyword(p); // the operator
if p.at_name_token() {
p.parse_qualified_name();
p.skip_trivia();
}
p.finish_node(); // finish first SPECIALIZATION
// Handle multiplicity after first name: :>> name[mult]
if p.at(SyntaxKind::L_BRACKET) {
p.parse_multiplicity();
p.skip_trivia();
}
// Handle comma-separated references: :>> A, B, C
while p.at(SyntaxKind::COMMA) {
bump_keyword(p); // comma
p.start_node(SyntaxKind::SPECIALIZATION);
if p.at_name_token() {
p.parse_qualified_name();
p.skip_trivia();
}
p.finish_node(); // finish additional SPECIALIZATION
// Multiplicity after each name
if p.at(SyntaxKind::L_BRACKET) {
p.parse_multiplicity();
p.skip_trivia();
}
}
// Additional specializations (including ::> references)
parse_specializations(p);
p.skip_trivia();
// Typing after shorthand redefinition
if p.at(SyntaxKind::COLON) {
p.parse_typing();
p.skip_trivia();
}
// Multiplicity
if p.at(SyntaxKind::L_BRACKET) {
p.parse_multiplicity();
p.skip_trivia();
}
// Ordering modifiers
while p.at(SyntaxKind::ORDERED_KW) || p.at(SyntaxKind::NONUNIQUE_KW) {
bump_keyword(p);
}
// Default value
parse_optional_default_value(p);
// Body (check type-specific bodies for shorthand redefines too)
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 {
p.parse_body();
}
p.finish_node();
return;
}
// Identification - but NOT if we're at CONNECT_KW (which is part of connector clause)
// Check if this is a feature chain reference (name.member) vs a simple name
// For patterns like `event sendSpeed.sourceEvent;`, the chain is a reference, not a name
let has_chain = if (p.at_name_token() || p.at(SyntaxKind::LT)) && !p.at(SyntaxKind::CONNECT_KW)
{
// Look ahead to see if there's a dot after the name
let mut lookahead = 0;
if p.at(SyntaxKind::LT) {
// Skip past short name <name>
lookahead += 1; // <
if is_name_kind(p.peek_kind(lookahead)) {
lookahead += 1;
}
if p.peek_kind(lookahead) == SyntaxKind::GT {
lookahead += 1;
}
}
if is_name_kind(p.peek_kind(lookahead)) {
lookahead += 1;
}
// Skip any whitespace/trivia in lookahead (simplified - just check next few)
p.peek_kind(lookahead) == SyntaxKind::DOT
} else {
false
};
// For interface/connection usages, check if this looks like a connector pattern
// Pattern: interface X.y to Z.w - the feature chain is a connector endpoint, not a specialization
let looks_like_connector_endpoint = if (is_connection_kw || is_interface_kw) && has_chain {
// Look ahead to see if there's a 'to' keyword after the chain
let mut depth = 0;
let mut found_to = false;
for i in 0..30 {
match p.peek_kind(i) {
SyntaxKind::TO_KW if depth == 0 => {
found_to = true;
break;
}
SyntaxKind::DOT | SyntaxKind::IDENT => {}
SyntaxKind::L_BRACKET => depth += 1,
SyntaxKind::R_BRACKET => depth -= 1,
SyntaxKind::WHITESPACE => {} // Skip whitespace in lookahead
SyntaxKind::SEMICOLON | SyntaxKind::L_BRACE | SyntaxKind::COLON => break,
_ => break,
}
}
found_to
} else {
false
};
if has_chain && !looks_like_connector_endpoint {
// This is a feature chain reference like `sendSpeed.sourceEvent`
// Parse as a SPECIALIZATION with a QUALIFIED_NAME containing the chain
p.start_node(SyntaxKind::SPECIALIZATION);
p.parse_qualified_name(); // Parses the full chain including dots
p.skip_trivia();
p.finish_node();
} else if (p.at_name_token() || p.at(SyntaxKind::LT))
&& !p.at(SyntaxKind::CONNECT_KW)
&& !looks_like_connector_endpoint
{
p.parse_identification();
}
p.skip_trivia();
// For message usages: handle 'of' payload type after name
// Pattern: message sendSensedSpeed of SensedSpeed from ... to ...
if is_message && p.at(SyntaxKind::OF_KW) {
bump_keyword(p);
if p.at_name_token() {
p.parse_qualified_name();
p.skip_trivia();
}
}
// Handle feature chain continuation (e.g., producerBehavior.publish[1])
// This handles cases where chain wasn't detected by lookahead
while p.at(SyntaxKind::DOT) {
bump_keyword(p); // .
if p.at_name_token() {
bump_keyword(p);
}
// Optional indexing after feature access
if p.at(SyntaxKind::L_BRACKET) {
p.parse_multiplicity();
p.skip_trivia();
}
}
// Multiplicity
if p.at(SyntaxKind::L_BRACKET) {
p.parse_multiplicity();
}
p.skip_trivia();
// Typing
if p.at(SyntaxKind::COLON) {
p.parse_typing();
}
p.skip_trivia();
// Specializations
parse_specializations(p);
p.skip_trivia();
// Multiplicity after specializations (e.g., port myPort :>> basePort [5])
if p.at(SyntaxKind::L_BRACKET) {
p.parse_multiplicity();
p.skip_trivia();
}
// Ordering modifiers
while p.at(SyntaxKind::ORDERED_KW) || p.at(SyntaxKind::NONUNIQUE_KW) {
bump_keyword(p);
}
// More specializations
parse_specializations(p);
p.skip_trivia();
// For connection/interface usage: n-ary endpoint syntax after typing
// Pattern: connection : Type ( end1 ::> a, end2 ::> b );
// Pattern: interface : Type ( end1 ::> a, end2 ::> b );
if (is_connection_kw || is_interface_kw) && p.at(SyntaxKind::L_PAREN) {
p.start_node(SyntaxKind::CONNECTOR_PART);
bump_keyword(p); // (
parse_connector_end(p);
p.skip_trivia();
while p.at(SyntaxKind::COMMA) {
bump_keyword(p);
parse_connector_end(p);
p.skip_trivia();
}
p.expect(SyntaxKind::R_PAREN);
p.finish_node(); // CONNECTOR_PART
p.skip_trivia();
}
// For connection/interface usage: binary endpoint syntax with 'to'
// Pattern: interface source.port to target.port;
if (is_connection_kw || is_interface_kw) && p.at_name_token() && !p.at(SyntaxKind::CONNECT_KW) {
// Check if there's a 'to' keyword ahead
let has_to = {
let mut depth = 0;
let mut found_to = false;
for i in 0..20 {
match p.peek_kind(i) {
SyntaxKind::TO_KW if depth == 0 => {
found_to = true;
break;
}
SyntaxKind::DOT | SyntaxKind::IDENT => {}
SyntaxKind::L_BRACKET => depth += 1,
SyntaxKind::R_BRACKET => depth -= 1,
SyntaxKind::SEMICOLON | SyntaxKind::L_BRACE => break,
_ => break,
}
}
found_to
};
if has_to {
p.start_node(SyntaxKind::CONNECTOR_PART);
// Parse source endpoint (chain like source.port)
parse_connector_end(p);
p.skip_trivia();
// 'to' keyword
if p.at(SyntaxKind::TO_KW) {
bump_keyword(p);
// Parse target endpoint
parse_connector_end(p);
p.skip_trivia();
}
p.finish_node(); // CONNECTOR_PART
}
}
// For allocation usage: optional allocate clause
let is_allocation = p.at(SyntaxKind::ALLOCATE_KW);
if is_allocation {
// Parse allocate keyword part: allocate <source> to <target>
bump_keyword(p); // allocate
// Check for n-ary or binary pattern
if p.at(SyntaxKind::L_PAREN) {
// N-ary: allocate (a, b ::> c, ...)
bump_keyword(p); // (
parse_allocate_end_member(p);
while p.at(SyntaxKind::COMMA) {
bump_keyword(p);
parse_allocate_end_member(p);
}
p.expect(SyntaxKind::R_PAREN);
p.skip_trivia();
} else {
// Binary: allocate source to target
parse_allocate_end_member(p);
if consume_if(p, SyntaxKind::TO_KW) {
parse_allocate_end_member(p);
}
}
}
// For connection usage: optional connect clause
let is_connection = p.at(SyntaxKind::CONNECT_KW);
if is_connection {
// Parse connect keyword part: connect <end> to <end> or connect (<ends>)
p.start_node(SyntaxKind::CONNECTOR_PART);
bump_keyword(p); // connect
// Check for n-ary or binary pattern
if p.at(SyntaxKind::L_PAREN) {
// N-ary: connect (a ::> b, c ::> d, ...)
bump_keyword(p); // (
parse_connector_end(p);
p.skip_trivia();
while p.at(SyntaxKind::COMMA) {
bump_keyword(p);
parse_connector_end(p);
p.skip_trivia();
}
p.expect(SyntaxKind::R_PAREN);
p.skip_trivia();
} else {
// Binary: connect source to target
parse_connector_end(p);
p.skip_trivia();
if consume_if(p, SyntaxKind::TO_KW) {
parse_connector_end(p);
p.skip_trivia();
}
}
p.finish_node(); // CONNECTOR_PART
}
// For message: optional from/to clause
parse_optional_from_to(p);
// Default value: 'default' [expr] or '=' expr or ':=' expr
parse_optional_default_value(p);
// About clause (for metadata usages)
// Pattern: about annotation ("," annotation)*
if p.at(SyntaxKind::ABOUT_KW) {
bump_keyword(p); // about
// Parse first annotation (qualified name or identifier)
parse_optional_qualified_name(p);
// Parse additional annotations
while p.at(SyntaxKind::COMMA) {
bump_keyword(p);
parse_optional_qualified_name(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();
}
/// Parse allocate end member: [name ::>] qualified_name
fn parse_allocate_end_member<P: SysMLParser>(p: &mut P) {
if p.at_name_token() {
// Check if this is "name ::> ref" pattern
let lookahead = 1;
if p.peek_kind(lookahead) == SyntaxKind::COLON_COLON_GT {
// Pattern: name ::> qualified_name
p.bump(); // name
p.skip_trivia();
p.bump(); // ::>
p.skip_trivia();
if p.at_name_token() {
p.parse_qualified_name();
}
} else {
// Just a qualified name
p.parse_qualified_name();
}
p.skip_trivia();
}
}
pub fn parse_definition_keyword<P: SysMLParser>(p: &mut P) {
if p.at(SyntaxKind::USE_KW) {
p.bump();
p.skip_trivia();
if p.at(SyntaxKind::CASE_KW) {
p.bump();
}
return;
}
if p.at_any(SYSML_DEFINITION_KEYWORDS) {
p.bump();
}
}
fn parse_usage_keyword<P: SysMLParser>(p: &mut P) {
if p.at(SyntaxKind::USE_KW) {
bump_keyword(p);
if p.at(SyntaxKind::CASE_KW) {
p.bump();
}
return;
}
if p.at_any(SYSML_USAGE_KEYWORDS) {
// Don't consume a keyword if it's actually being used as a name.
// Check if the next non-trivia token indicates this is a name (followed by : or :> or [ etc.)
// This handles cases like `in frame : Integer` where `frame` is a name, not a usage keyword.
if p.at_name_token() {
let lookahead = skip_trivia_lookahead(p, 1);
let next = p.peek_kind(lookahead);
if matches!(
next,
SyntaxKind::COLON
| SyntaxKind::COLON_GT
| SyntaxKind::COLON_GT_GT
| SyntaxKind::L_BRACKET
| SyntaxKind::SEMICOLON
| SyntaxKind::L_BRACE
| SyntaxKind::REDEFINES_KW
| SyntaxKind::SUBSETS_KW
| SyntaxKind::REFERENCES_KW
) {
// This looks like a name followed by typing/specialization, not a usage keyword
return;
}
}
p.bump();
}
}
fn parse_usage_prefix<P: SysMLParser>(p: &mut P) -> bool {
let mut saw_end = false;
while p.at_any(USAGE_PREFIX_KEYWORDS) {
if p.at(SyntaxKind::END_KW) {
saw_end = true;
}
bump_keyword(p);
}
saw_end
}