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
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
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
//! Expression parser tests adapted from Pest to Rowan
//!
//! This file adapts the original Pest-based expression tests to work with the new Rowan parser.
//! These tests focus on SysML expression parsing including:
//! - Primary expressions (member access, instantiation, invocation)
//! - Lambda/calculation bodies
//! - Constraint expressions
//! - Relational and equality operators
//! - Classification expressions (as, hastype, meta, @@)

#![allow(clippy::unwrap_used)]
#![allow(clippy::panic)]
#![allow(dead_code)]
#![allow(unused_imports)]

use rstest::rstest;
use syster::parser::rule_parser::{self, parse_rule as rowan_parse_rule, Rule};
use syster::parser::{parse_sysml, parse_kerml};

// ============================================================================
// Helper Functions
// ============================================================================

/// Parse SysML source and assert it succeeds
fn assert_parses_sysml(input: &str, desc: &str) {
    let result = parse_sysml(input);
    assert!(
        result.ok(),
        "Failed to parse SysML ({}): {:?}\nInput: {}",
        desc,
        result.errors,
        input
    );
}

/// Parse a rule and assert it succeeds
fn assert_rule_parses(rule: Rule, input: &str, desc: &str) {
    let result = rowan_parse_rule(rule, input);
    assert!(
        result.is_ok(),
        "Failed to parse {} ({:?}):\nInput: {:?}\nErrors: {:?}",
        desc,
        rule,
        input,
        result.errors()
    );
}

/// Wrap expression in attribute context for testing
fn wrap_in_attribute(expr: &str) -> String {
    format!("package T {{ attribute x = {}; }}", expr)
}

/// Wrap expression in calc context for testing  
fn wrap_in_calc(body: &str) -> String {
    format!("package T {{ calc def Test {{ {} }} }}", body)
}

/// Wrap expression in constraint context for testing
fn wrap_in_constraint(expr: &str) -> String {
    format!("package T {{ constraint c {{ {} }} }}", expr)
}

// ============================================================================
// Primary Expression Tests
// ============================================================================

#[test]
fn test_chained_member_access() {
    let input = wrap_in_attribute("fn.samples.domainValue");
    assert_parses_sysml(&input, "chained member access");
}

#[test]
fn test_instantiation_expression_with_args() {
    let input = wrap_in_attribute("new SampledFunction(samples = values)");
    assert_parses_sysml(&input, "instantiation expression");
}

#[test]
fn test_instantiation_expression_positional() {
    let input = wrap_in_attribute("new SamplePair(x, y)");
    assert_parses_sysml(&input, "instantiation with positional args");
}

#[test]
fn test_arrow_invocation_with_block() {
    let input = wrap_in_attribute("list->select { in i; true }");
    assert_parses_sysml(&input, "arrow invocation with block");
}

#[test]
fn test_arrow_invocation_with_block_then_index() {
    let input = wrap_in_attribute("list->select { in i; true }#(1)");
    assert_parses_sysml(&input, "arrow invocation with block followed by indexing");
}

#[test]
fn test_typed_parameter_in_lambda() {
    let input = wrap_in_attribute("list->select { in i : Positive; domainValues#(i) <= value }");
    assert_parses_sysml(&input, "lambda with typed parameter");
}

#[test]
fn test_typed_parameter_in_lambda_then_index() {
    let input = wrap_in_attribute("list->select { in i : Positive; domainValues#(i) <= value }#(1)");
    assert_parses_sysml(&input, "lambda with typed parameter followed by indexing");
}

#[test]
fn test_full_sampled_functions_expression() {
    let input = wrap_in_attribute("(1..size(domainValues))->select { in i : Positive; domainValues#(i) <= value }#(1)");
    assert_parses_sysml(&input, "full SampledFunctions expression");
}

#[test]
fn test_nested_instantiation_in_collect() {
    let input = wrap_in_attribute("domainValues->collect { in x; new SamplePair(x, calculation(x)) }");
    assert_parses_sysml(&input, "collect with nested instantiation");
}

// ============================================================================
// Attribute Usage Tests
// ============================================================================

#[test]
fn test_attribute_with_complex_initializer() {
    assert_rule_parses(
        Rule::AttributeUsage,
        "attribute index : Positive[0..1] = (1..size(domainValues))->select { in i : Positive; domainValues#(i) <= value }#(1);",
        "attribute with complex initializer"
    );
}

#[test]
fn test_attribute_without_type() {
    assert_rule_parses(
        Rule::AttributeUsage,
        "attribute index = (1..size(domainValues))->select { in i : Positive; domainValues#(i) <= value }#(1);",
        "attribute without type"
    );
}

#[test]
fn test_attribute_in_package_context() {
    let input = "package Test { attribute index = list->select { in i : Positive; vals#(i) <= v }#(1); }";
    assert_parses_sysml(input, "attribute in package");
}

#[test]
fn test_attribute_with_simple_type() {
    assert_rule_parses(
        Rule::AttributeUsage,
        "attribute index : Positive[0..1];",
        "attribute with simple type"
    );
}

#[test]
fn test_feature_value_with_lambda() {
    // Feature value with complex lambda expression - test in attribute context
    let input = "package T { attribute x = (1..size(domainValues))->select { in i : Positive; domainValues#(i) <= value }#(1); }";
    assert_parses_sysml(input, "feature_value with lambda");
}

#[test]
fn test_attribute_with_default_value_and_body() {
    assert_rule_parses(
        Rule::AttributeUsage,
        "attribute x default foo { }",
        "attribute with default value and body"
    );
}

#[test]
fn test_attribute_with_redefinition_default_and_body() {
    assert_rule_parses(
        Rule::AttributeUsage,
        "attribute :>> x default foo { }",
        "attribute with redefinition, default, and body"
    );
}

#[test]
fn test_attribute_with_multiplicity_and_body() {
    assert_rule_parses(
        Rule::AttributeUsage,
        "attribute x[1] { }",
        "attribute with multiplicity and body"
    );
}

#[test]
fn test_attribute_with_multiplicity_and_default() {
    assert_rule_parses(
        Rule::AttributeUsage,
        "attribute x[1] default foo;",
        "attribute with multiplicity and default"
    );
}

#[test]
fn test_attribute_with_multiplicity_default_no_redef_with_body() {
    assert_rule_parses(
        Rule::AttributeUsage,
        "attribute x[1] default foo { }",
        "attribute with multiplicity, default, and body (no redef)"
    );
}

#[test]
fn test_attribute_with_redef_multiplicity_and_body() {
    assert_rule_parses(
        Rule::AttributeUsage,
        "attribute :>> x[1] { }",
        "attribute with redef, multiplicity, and body"
    );
}

#[test]
fn test_attribute_with_redef_multiplicity_and_default() {
    assert_rule_parses(
        Rule::AttributeUsage,
        "attribute :>> x[1] default foo;",
        "attribute with redef, multiplicity, and default"
    );
}

#[test]
fn test_attribute_with_long_name_multiplicity_default_body() {
    assert_rule_parses(
        Rule::AttributeUsage,
        "attribute transformation[1] default nullTransformation { }",
        "attribute with long name, multiplicity, default, body"
    );
}

#[test]
fn test_attribute_y_multiplicity_default_body() {
    assert_rule_parses(
        Rule::AttributeUsage,
        "attribute y[1] default z { }",
        "attribute y[1] default z with body"
    );
}

#[test]
fn test_attribute_x_multiplicity_default_null_x_body() {
    assert_rule_parses(
        Rule::AttributeUsage,
        "attribute x[1] default nullX { }",
        "attribute x[1] default nullX with body"
    );
}

#[test]
fn test_attribute_x_multiplicity_default_abc_body() {
    assert_rule_parses(
        Rule::AttributeUsage,
        "attribute x[1] default abcdef { }",
        "attribute x[1] default abcdef with body"
    );
}

#[test]
fn test_attribute_with_short_name() {
    assert_rule_parses(
        Rule::AttributeUsage,
        "attribute <x> myAttr;",
        "attribute with short name"
    );
}

#[test]
fn test_attribute_with_short_name_and_quoted_full_name() {
    assert_rule_parses(
        Rule::AttributeUsage,
        "attribute <isq> 'International System of Quantities';",
        "attribute with short name and quoted full name"
    );
}

#[test]
fn test_attribute_with_short_name_typed_and_body() {
    assert_rule_parses(
        Rule::AttributeUsage,
        "attribute <isq> 'International System of Quantities': SystemOfQuantities { }",
        "attribute with short name, type, and body"
    );
}

#[test]
fn test_attribute_with_empty_tuple_value() {
    assert_rule_parses(
        Rule::AttributeUsage,
        "attribute :>> dimensions = ();",
        "attribute with empty tuple value"
    );
}

#[test]
fn test_attribute_with_scientific_notation_value() {
    let input = "package T { attribute yocto { :>> conversionFactor = 1E-24; } }";
    assert_parses_sysml(input, "attribute with scientific notation value");
}

#[test]
fn test_attribute_with_multiplicity_default_and_body() {
    assert_rule_parses(
        Rule::AttributeUsage,
        "attribute :>> transformation[1] default nullTransformation { attribute :>> source; }",
        "attribute with multiplicity, default, and body"
    );
}

#[test]
fn test_attribute_with_qualified_type_and_as_cast() {
    let input = "package T { ref :>> baseType = causes as SysML::Usage; }";
    assert_parses_sysml(input, "attribute with qualified type in as expression");
}

#[test]
fn test_attribute_with_meta_expression() {
    let input = "package T { ref :>> baseType = multicausations meta SysML::Usage; }";
    assert_parses_sysml(input, "attribute with meta expression");
}

// ============================================================================
// Calculation Body Tests
// ============================================================================

#[test]
fn test_calculation_body_minimal() {
    let input = wrap_in_calc("vals#(i)");
    assert_parses_sysml(&input, "minimal calculation body");
}

#[test]
fn test_calculation_body_with_parameter_binding() {
    let input = wrap_in_calc("in i; vals#(i)");
    assert_parses_sysml(&input, "calculation body with parameter binding");
}

#[test]
fn test_calculation_body_with_typed_parameter() {
    let input = wrap_in_calc("in i : Positive; vals#(i)");
    assert_parses_sysml(&input, "calculation body with typed parameter");
}

#[test]
fn test_calculation_body_with_parameter_declaration() {
    let input = wrap_in_calc("in fn : SampledFunction;");
    assert_parses_sysml(&input, "calculation body with parameter declaration");
}

#[test]
fn test_calculation_body_with_param_and_return() {
    let input = wrap_in_calc("in fn : SampledFunction; return : Anything[0..*] = fn.samples.domainValue;");
    assert_parses_sysml(&input, "calc body with param and return");
}

#[test]
fn test_calculation_body_braces() {
    let input = wrap_in_calc("in fn : SampledFunction; return : Anything[0..*] = fn.samples.domainValue;");
    assert_parses_sysml(&input, "calculation_body with braces");
}

#[test]
fn test_calculation_body_with_simple_expression() {
    let input = wrap_in_calc("a == b");
    assert_parses_sysml(&input, "calculation body with simple expression");
}

#[test]
fn test_calculation_body_item_return() {
    let input = wrap_in_calc("return : Anything[0..*] = fn.samples.domainValue;");
    assert_parses_sysml(&input, "calculation_body_item with return");
}

#[test]
fn test_calculation_body_part_with_param_and_return() {
    let input = wrap_in_calc("in fn : SampledFunction; return : Anything[0..*] = fn.samples.domainValue;");
    assert_parses_sysml(&input, "calculation_body_part");
}

#[test]
fn test_calculation_body_item_without_semicolon() {
    let input = wrap_in_calc("in calc calculation { in x; }");
    assert_parses_sysml(&input, "calculation_body_item without trailing semicolon");
}

#[test]
fn test_calculation_body_item_attribute_with_semicolon() {
    let input = wrap_in_calc("in attribute domainValues [0..*];");
    assert_parses_sysml(&input, "attribute usage in calculation_body_item");
}

#[test]
fn test_calculation_body_mixed_items() {
    let input = wrap_in_calc("in calc calculation { in x; } in attribute domainValues [0..*]; return sampling = value;");
    assert_parses_sysml(&input, "calculation_body with mixed items");
}

#[test]
fn test_calculation_body_part_simple_expression() {
    let input = wrap_in_calc("a == b");
    assert_parses_sysml(&input, "simple expression as calculation_body_part");
}

#[test]
fn test_action_body_item_identifier() {
    // This should parse as a usage reference
    let input = "package T { action def A { a; } }";
    assert_parses_sysml(input, "action body item identifier");
}

#[test]
fn test_calculation_body_item_identifier() {
    // This should parse as a result expression
    let input = wrap_in_calc("a");
    assert_parses_sysml(&input, "calculation body item identifier");
}

// ============================================================================
// Calculation Definition Tests
// ============================================================================

#[test]
fn test_calculation_def_domain() {
    assert_rule_parses(
        Rule::CalcDef,
        r#"calc def Domain {
            in fn : SampledFunction;
            return : Anything[0..*] = fn.samples.domainValue;
        }"#,
        "Domain calc def"
    );
}

#[test]
fn test_calculation_def_with_expression_body() {
    assert_rule_parses(
        Rule::CalcDef,
        "calc def Test { a == b }",
        "calc def with expression body"
    );
}

// ============================================================================
// Parameter Tests
// ============================================================================

#[test]
fn test_parameter_membership() {
    assert_rule_parses(
        Rule::ParameterMembership,
        "in fn : SampledFunction;",
        "typed parameter membership"
    );
}

#[test]
fn test_return_parameter_membership() {
    assert_rule_parses(
        Rule::ReturnParameterMembership,
        "return : Anything[0..*] = fn.samples.domainValue;",
        "return parameter membership"
    );
}

#[test]
fn test_return_parameter_with_name() {
    assert_rule_parses(
        Rule::ReturnParameterMembership,
        "return sampling = new SampledFunction();",
        "return parameter with name"
    );
}

#[test]
fn test_return_parameter_with_name_and_type() {
    assert_rule_parses(
        Rule::ReturnParameterMembership,
        "return result: StateSpace = value;",
        "return parameter with name and type"
    );
}

#[test]
fn test_return_attribute_member() {
    let input = wrap_in_calc("return attribute result;");
    assert_parses_sysml(&input, "return attribute member");
}

#[test]
fn test_return_attribute_member_with_type() {
    let input = wrap_in_calc("return attribute result : ScalarValue[1];");
    assert_parses_sysml(&input, "return attribute member with type");
}

#[test]
fn test_return_attribute_with_body() {
    let input = r#"package T { calc def Test {
        return attribute result : ScalarValue[1] {
            doc
            /*
             * A comment
             */
        }
    } }"#;
    assert_parses_sysml(input, "return attribute with body");
}

#[test]
fn test_parameter_binding_simple() {
    let input = wrap_in_calc("in i;");
    assert_parses_sysml(&input, "simple parameter binding");
}

#[test]
fn test_parameter_binding_typed() {
    let input = wrap_in_calc("in fn : SampledFunction;");
    assert_parses_sysml(&input, "typed parameter binding");
}

#[test]
fn test_parameter_binding_without_direction() {
    // Parameter without direction needs proper context
    let input = "package T { calc def Test { in p : Point; } }";
    assert_parses_sysml(input, "parameter binding without direction");
}

// ============================================================================
// Expression Body Tests
// ============================================================================

#[test]
fn test_expression_body_with_parameter() {
    let input = wrap_in_calc("in i; vals#(i)");
    assert_parses_sysml(&input, "expression body with parameter");
}

#[test]
fn test_expression_body_with_doc() {
    let input = r#"package T {
        calc def Test {
            doc
            /*
             * Some documentation
             */
            in x; eval(x)
        }
    }"#;
    assert_parses_sysml(input, "expression body with doc");
}

#[test]
fn test_expression_body_with_ref_parameter() {
    let input = r#"package T {
        calc def Test {
            in ref a {
                doc
                /* The alternative */
            }
            a
        }
    }"#;
    assert_parses_sysml(input, "expression body with ref parameter");
}

#[test]
fn test_expression_body_with_typed_parameter_no_direction() {
    // Typed parameter without direction keyword needs 'in' prefix in SysML calc bodies
    let input = "package T { calc def Test { in p2 : Point; p1 != p2 } }";
    assert_parses_sysml(input, "expression body with typed parameter");
}

// ============================================================================
// Relational and Equality Operators
// ============================================================================

#[rstest]
#[case("a <= b", "<=")]
#[case("a >= b", ">=")]
#[case("a < b", "<")]
#[case("a > b", ">")]
fn test_relational_operators(#[case] expr: &str, #[case] operator: &str) {
    let input = wrap_in_constraint(expr);
    assert_parses_sysml(&input, &format!("{} operator", operator));
}

#[rstest]
#[case("a == b", "==")]
#[case("a === b", "===")]
#[case("a != b", "!=")]
#[case("a !== b", "!==")]
fn test_equality_operators(#[case] expr: &str, #[case] operator: &str) {
    let input = wrap_in_constraint(expr);
    assert_parses_sysml(&input, &format!("{} operator", operator));
}

#[test]
fn test_equality_expression_with_member_access() {
    let input = wrap_in_constraint("stateSpace.order == order");
    assert_parses_sysml(&input, "equality expression with member access");
}

#[test]
fn test_complex_relational_expression() {
    let input = wrap_in_constraint("domainValues#(i) <= value");
    assert_parses_sysml(&input, "complex relational expression");
}

#[test]
fn test_conditional_expression_simple_equality() {
    let input = wrap_in_constraint("a == b");
    assert_parses_sysml(&input, "simple equality as conditional_expression");
}

#[test]
fn test_owned_expression_simple_equality() {
    let input = wrap_in_attribute("a == b");
    assert_parses_sysml(&input, "simple equality as owned_expression");
}

#[test]
fn test_result_expression_member_simple_equality() {
    let input = wrap_in_calc("a == b");
    assert_parses_sysml(&input, "simple equality as result_expression_member");
}

// ============================================================================
// Constraint Usage Tests  
// ============================================================================

#[test]
fn test_constraint_usage_with_expression() {
    assert_rule_parses(
        Rule::ConstraintUsage,
        "constraint { stateSpace.order == order }",
        "constraint usage with expression"
    );
}

#[test]
fn test_constraint_with_in_parameter() {
    let input = "package T { assert constraint c { in x = y; } }";
    assert_parses_sysml(input, "constraint with in parameter");
}

#[test]
fn test_constraint_with_simple_expression() {
    let input = "package T { assert constraint c { x implies y } }";
    assert_parses_sysml(input, "constraint with simple implies");
}

#[test]
fn test_constraint_with_function_on_right() {
    let input = "package T { assert constraint c { x implies f() } }";
    assert_parses_sysml(input, "constraint with function on right");
}

#[test]
fn test_constraint_with_actual_names() {
    let input = "package T { assert constraint c { x implies all(y) } }";
    assert_parses_sysml(input, "constraint with 'all' function");
}

#[test]
fn test_constraint_with_all_true_function() {
    let input = "package T { assert constraint c { x implies allTrue(y) } }";
    assert_parses_sysml(input, "constraint with 'allTrue' function");
}

#[test]
fn test_constraint_without_doc() {
    let input = r#"package T {
        assert constraint originalImpliesDerived {
            originalRequirement.result implies allTrue(derivedRequirements.result)
        }
    }"#;
    assert_parses_sysml(input, "constraint without doc");
}

#[test]
fn test_constraint_with_doc_and_expression() {
    let input = r#"package T {
        assert constraint originalImpliesDerived {
            doc 
            /* comment */
            originalRequirement.result implies allTrue(derivedRequirements.result)
        }
    }"#;
    assert_parses_sysml(input, "constraint with doc and expression");
}

// ============================================================================
// Classification Expression Tests
// ============================================================================

#[test]
fn test_as_expression_with_qualified_name() {
    let input = wrap_in_attribute("causes as SysML::Usage");
    assert_parses_sysml(&input, "as expression with qualified name");
}

#[test]
fn test_hastype_with_qualified_name() {
    let input = wrap_in_attribute("value hastype Domain::ItemType");
    assert_parses_sysml(&input, "hastype with qualified name");
}

#[test]
fn test_meta_expression_with_qualified_name() {
    let input = wrap_in_attribute("multicausations meta SysML::Usage");
    assert_parses_sysml(&input, "meta expression with qualified name");
}

#[test]
fn test_metadata_access_with_qualified_name() {
    let input = wrap_in_attribute("myMetadata @@ SysML::Metadata");
    assert_parses_sysml(&input, "@@ expression with qualified name");
}

#[test]
fn test_type_reference_with_qualified_name() {
    let input = "package T { part x : Domain::Library::Type; }";
    assert_parses_sysml(input, "type_reference with qualified name");
}

#[test]
fn test_type_result_with_qualified_name() {
    let input = "package T { part x : SysML::Usage; }";
    assert_parses_sysml(input, "type_result with qualified name");
}

#[test]
fn test_metadata_reference_with_qualified_name() {
    let input = "package T { @MyPackage::MyMetadata part x; }";
    assert_parses_sysml(input, "metadata_reference with qualified name");
}

// ============================================================================
// Qualified Name Tests
// ============================================================================

#[test]
fn test_qualified_name_with_unicode_theta_simple() {
    assert_rule_parses(
        Rule::QualifiedReferenceChain,
        "isq.'Θ'",
        "qualified name with Unicode theta"
    );
}

#[test]
fn test_qualified_name_with_unicode_theta_in_expression() {
    let input = wrap_in_attribute("isq.'Θ'");
    assert_parses_sysml(&input, "qualified name with Unicode theta as expression");
}

#[test]
fn test_unrestricted_name_with_unicode_theta() {
    assert_rule_parses(
        Rule::UnrestrictedName,
        "'Θ'",
        "quoted name with Unicode theta"
    );
}

#[test]
fn test_qualified_name_with_regular_identifiers_in_attribute_body() {
    let input = "package T { attribute pf { :>> quantity = isq.theta; } }";
    assert_parses_sysml(input, "attribute with regular qualified name in body assignment");
}

#[test]
fn test_qualified_name_with_quoted_name_in_attribute_body() {
    let input = "package T { attribute pf { :>> quantity = isq.'z'; } }";
    assert_parses_sysml(input, "attribute with quoted name in body assignment");
}

#[test]
fn test_qualified_name_with_unicode_theta_as_owned_expression() {
    let input = wrap_in_attribute("isq.'Θ'");
    assert_parses_sysml(&input, "qualified name with Unicode theta as owned_expression");
}

#[test]
fn test_qualified_name_with_unicode_theta_assignment() {
    let input = "package T { attribute pf { :>> quantity = isq.'Θ'; } }";
    assert_parses_sysml(input, "attribute with Unicode theta in body assignment");
}

// ============================================================================
// Number Literal Tests
// ============================================================================

#[rstest]
#[case("1E-24", "scientific notation with negative exponent")]
#[case("1E24", "scientific notation with positive exponent")]
#[case("1e-24", "scientific notation lowercase e")]
#[case("3.14", "decimal number")]
#[case("42", "integer")]
#[case(".5", "decimal starting with dot")]
fn test_number_literals(#[case] num: &str, #[case] desc: &str) {
    let input = wrap_in_attribute(num);
    assert_parses_sysml(&input, desc);
}

// ============================================================================
// Function Call Tests
// ============================================================================

#[test]
fn test_function_call_simple() {
    let input = wrap_in_attribute("foo()");
    assert_parses_sysml(&input, "simple function call");
}

#[test]
fn test_function_call_with_argument() {
    let input = wrap_in_attribute("allTrue(x)");
    assert_parses_sysml(&input, "function call with argument");
}

#[test]
fn test_function_call_with_dotted_argument() {
    let input = wrap_in_attribute("allTrue(derivedRequirements.result)");
    assert_parses_sysml(&input, "function call with dotted argument");
}

#[test]
fn test_implies_expression_with_function_call() {
    let input = wrap_in_constraint("originalRequirement.result implies allTrue(derivedRequirements.result)");
    assert_parses_sysml(&input, "implies expression with function call");
}

#[test]
fn test_invocation_expression_direct() {
    assert_rule_parses(
        Rule::InvocationExpression,
        "allTrue(x)",
        "invocation expression directly"
    );
}

#[test]
fn test_nested_function_call() {
    let input = wrap_in_attribute("allTrue(assumptions())");
    assert_parses_sysml(&input, "nested function call");
}

#[test]
fn test_outer_function_with_inner_call() {
    let input = wrap_in_attribute("allTrue(assumptions())");
    assert_parses_sysml(&input, "outer function with inner invocation");
}

#[test]
fn test_nested_invocation_expression() {
    let input = wrap_in_attribute("allTrue(assumptions())");
    assert_parses_sysml(&input, "nested invocation at classification_expression level");
}

#[test]
fn test_nested_invocation_equality() {
    let input = wrap_in_attribute("allTrue(assumptions())");
    assert_parses_sysml(&input, "nested invocation at equality_expression level");
}

#[test]
fn test_nested_invocation_classification() {
    let input = wrap_in_attribute("assumptions()");
    assert_parses_sysml(&input, "invocation at classification_expression level");
}

#[test]
fn test_nested_invocation_relational() {
    let input = wrap_in_attribute("assumptions()");
    assert_parses_sysml(&input, "invocation at relational_expression level");
}

#[test]
fn test_nested_invocation_base() {
    let input = wrap_in_attribute("assumptions()");
    assert_parses_sysml(&input, "invocation at base_expression level");
}

#[test]
fn test_invocation_after_constraint() {
    let input = wrap_in_attribute("assumptions()");
    assert_parses_sysml(&input, "invocation as owned_expression");
}

#[test]
fn test_argument_value_with_invocation() {
    let input = wrap_in_attribute("foo(assumptions())");
    assert_parses_sysml(&input, "invocation at argument_value level");
}

// ============================================================================
// Invocations with "as" prefix (identifier collision tests)
// ============================================================================

#[rstest]
#[case("allTrue(assumptions())", "assumptions starts with 'as'")]
#[case("allTrue(assertion())", "assertion starts with 'as'")]
#[case("allTrue(asdf())", "asdf starts with 'as'")]
#[case("foo(assumptions(), assertion(), asdf())", "multiple args starting with 'as'")]
fn test_invocations_starting_with_as_keyword(#[case] expr: &str, #[case] desc: &str) {
    let input = wrap_in_attribute(expr);
    assert_parses_sysml(&input, desc);
}

// ============================================================================
// "as" operator with proper word boundary
// ============================================================================

#[rstest]
#[case("x as Int", "simple cast")]
#[case("x as SysML::Usage", "cast to qualified name")]
#[case("foo() as MyType::SubType", "invocation result cast")]
#[case("causes as SysML::Usage", "exact case")]
fn test_as_operator_with_qualified_names(#[case] expr: &str, #[case] desc: &str) {
    let input = wrap_in_attribute(expr);
    assert_parses_sysml(&input, desc);
}

// ============================================================================
// "meta" operator with proper word boundary
// ============================================================================

#[rstest]
#[case("x meta Usage", "simple meta")]
#[case("x meta SysML::Usage", "meta with qualified name")]
#[case("multicausations meta SysML::Usage", "identifier starting with similar pattern")]
fn test_meta_operator_with_qualified_names(#[case] expr: &str, #[case] desc: &str) {
    let input = wrap_in_attribute(expr);
    assert_parses_sysml(&input, desc);
}

// ============================================================================
// Multiplicity Tests
// ============================================================================

#[test]
fn test_multiplicity_with_expression() {
    // Multiplicity with expression needs to be in a feature context
    let input = "package T { part x[nCauses]; }";
    assert_parses_sysml(input, "multiplicity with expression");
}

#[test]
fn test_multiplicity_with_range_expressions() {
    // Multiplicity with expression range needs to be in a feature context
    let input = "package T { part x[0..size(items)]; }";
    assert_parses_sysml(input, "multiplicity with expression range");
}

// ============================================================================
// Connector End Tests
// ============================================================================

#[test]
fn test_connector_end_with_multiplicity_and_chain() {
    let input = "package T { succession first [nCauses] causes.startShot then effects; }";
    assert_parses_sysml(input, "connector end with multiplicity and feature chain");
}

#[test]
fn test_connector_end_with_multiplicity_and_identifier() {
    let input = "package T { succession first [1] endpoint then target; }";
    assert_parses_sysml(input, "connector end with multiplicity and identifier");
}

#[test]
fn test_connector_end_with_name_references() {
    let input = "package T { connection connect myEnd references source.port to target.port; }";
    assert_parses_sysml(input, "connector end with name and references");
}

#[test]
fn test_succession_with_multiplicity() {
    assert_rule_parses(
        Rule::SuccessionAsUsage,
        r#"succession causalOrdering first [nCauses] causes.startShot then [nEffects] effects {
            doc /* test */
        }"#,
        "succession with multiplicities"
    );
}

// ============================================================================
// Interface and Flow Usage Tests
// ============================================================================

#[test]
fn test_interface_usage_with_nonunique() {
    assert_rule_parses(
        Rule::InterfaceUsage,
        "abstract interface interfaces: Interface[0..*] nonunique :> connections { }",
        "interface usage with nonunique"
    );
}

#[test]
fn test_flow_usage_with_nonunique() {
    let input = "package T { abstract flow flows: Flow[0..*] nonunique :> messages, flowTransfers { } }";
    assert_parses_sysml(input, "flow usage with nonunique");
}

// ============================================================================
// Misc Expression Tests
// ============================================================================

#[test]
fn test_empty_tuple_expression() {
    let input = wrap_in_attribute("()");
    assert_parses_sysml(&input, "empty tuple expression");
}

#[test]
fn test_in_parameter_with_default_block() {
    let input = "package T { action def A { in whileTest default {true} { } } }";
    assert_parses_sysml(input, "in parameter with default block");
}

#[test]
fn test_ref_with_feature_chain_subsetting() {
    let input = "package T { part def P { ref :>> outgoingTransfersFromSelf :> interfacingPorts.incomingTransfersToSelf { } } }";
    assert_parses_sysml(input, "ref with feature chain subsetting");
}

#[test]
fn test_end_ref_with_name_only() {
    let input = "package T { connection def C { end ref source; } }";
    assert_parses_sysml(input, "end ref with name only");
}

// ============================================================================
// Invocation in calc body with constraints
// ============================================================================

#[test]
fn test_invocation_in_calc_body_with_constraints() {
    let input = r#"package T {
        calc def Test {
            constraint assumptions[0..*] :> constraintChecks, subperformances { }
            constraint constraints[0..*] :> constraintChecks, subperformances { }
            return result = allTrue(assumptions()) implies allTrue(constraints()) { }
        }
    }"#;
    assert_parses_sysml(input, "calc body with constraint declarations before return");
}

// ============================================================================
// Identifier Tests
// ============================================================================

#[rstest]
#[case("myVar")]
#[case("calculation1")]
#[case("result_value")]
#[case("InCamelCase")]
fn test_identifier_allows_valid_names(#[case] ident: &str) {
    assert_rule_parses(
        Rule::RegularName,
        ident,
        &format!("valid identifier '{}'", ident)
    );
}

#[test]
fn test_all_true_as_identifier() {
    assert_rule_parses(
        Rule::RegularName,
        "allTrue",
        "'allTrue' as identifier"
    );
}

// ============================================================================
// Return with Expressions
// ============================================================================

#[test]
fn test_return_with_equality_expression() {
    // Return with expression needs to be in calc context
    let input = "package T { calc def Test { return a == b; } }";
    assert_parses_sysml(input, "return with equality expression");
}

// ============================================================================
// Case Body Tests
// ============================================================================

#[test]
fn test_case_body_with_doc() {
    let input = r#"package T {
        case def Test {
            doc
            /*
             * A TradeStudy documentation
             */
        }
    }"#;
    assert_parses_sysml(input, "case body with doc");
}