syster-base 0.1.11-alpha

Core library for SysML v2 and KerML parsing, AST, and semantic analysis
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
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
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
//! SysML AST parsing.
//!
//! This module provides single-pass parsing for efficient AST construction.
//! All information (name, span, relationships, flags, body members) is extracted
//! in one traversal instead of multiple passes.

use super::enums::{DefinitionMember, Element, UsageKind, UsageMember};
use super::types::{
    Alias, Comment, CrossRel, Definition, Import, MetaRel, NamespaceDeclaration, Package,
    RedefinitionRel, ReferenceRel, Relationships, SatisfyRel, SpecializationRel, SubsettingRel,
    SysMLFile, Usage,
};
use super::utils::{
    extract_name_from_identification, find_in, is_body_rule, is_definition_rule, is_usage_rule,
    to_def_kind, to_usage_kind,
};
use crate::core::Span;
use crate::parser::sysml::Rule;
use pest::iterators::{Pair, Pairs};

/// Parse error type for AST construction
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct ParseError {
    pub message: String,
}

impl ParseError {
    pub fn no_match() -> Self {
        Self {
            message: "No matching rule".to_string(),
        }
    }

    pub fn invalid_rule(rule: &str) -> Self {
        Self {
            message: format!("Invalid rule: {rule}"),
        }
    }
}

// ============================================================================
// Parse Context - accumulates all extracted data in single pass
// ============================================================================

/// Context for accumulating parsed data during single-pass traversal
#[derive(Debug, Default)]
struct ParseContext {
    // Identity
    name: Option<String>,
    name_span: Option<Span>,
    short_name: Option<String>,
    short_name_span: Option<Span>,

    // Flags
    is_abstract: bool,
    is_variation: bool,
    is_derived: bool,
    is_const: bool,

    // Relationships
    relationships: Relationships,

    // Body members (for definitions)
    def_members: Vec<DefinitionMember>,

    // Body members (for usages)
    usage_members: Vec<UsageMember>,
}

impl ParseContext {
    fn new() -> Self {
        Self::default()
    }
}

// ============================================================================
// Span conversion
// ============================================================================

#[inline]
fn to_span(pest_span: pest::Span) -> Span {
    let (sl, sc) = pest_span.start_pos().line_col();
    let (el, ec) = pest_span.end_pos().line_col();
    Span::from_coords(sl - 1, sc - 1, el - 1, ec - 1)
}

// ============================================================================
// Reference extraction helpers
// ============================================================================

fn strip_quotes(s: &str) -> String {
    if s.starts_with('\'') && s.ends_with('\'') && s.len() >= 2 {
        s[1..s.len() - 1].to_string()
    } else {
        s.to_string()
    }
}

/// Strip quotes from each part of a qualified name like "'Foo'::'Bar'" -> "Foo::Bar"
/// Also handles single identifiers like "'packet header'" -> "packet header"
fn strip_qualified_name_quotes(s: &str) -> String {
    // Split on :: and strip quotes from each part
    s.split("::")
        .map(|part| strip_quotes(part.trim()))
        .collect::<Vec<_>>()
        .join("::")
}

/// Extract a single reference with span from a pair
pub(super) fn ref_with_span_from(pair: &Pair<Rule>) -> Option<(String, Span)> {
    for inner in pair.clone().into_inner() {
        match inner.as_rule() {
            Rule::qualified_name | Rule::feature_reference | Rule::owned_feature_chain => {
                // Build from parts, stripping quotes where needed
                let parts: Vec<String> = inner
                    .clone()
                    .into_inner()
                    .filter(|p| p.as_rule() == Rule::identifier || p.as_rule() == Rule::quoted_name)
                    .map(|p| {
                        if p.as_rule() == Rule::quoted_name {
                            strip_quotes(p.as_str())
                        } else {
                            p.as_str().to_string()
                        }
                    })
                    .collect();
                if !parts.is_empty() {
                    return Some((parts.join("::"), to_span(inner.as_span())));
                }
                return Some((inner.as_str().trim().to_string(), to_span(inner.as_span())));
            }
            Rule::identifier => {
                return Some((inner.as_str().trim().to_string(), to_span(inner.as_span())));
            }
            Rule::quoted_name => {
                return Some((strip_quotes(inner.as_str()), to_span(inner.as_span())));
            }
            _ => {
                if let Some(result) = ref_with_span_from(&inner) {
                    return Some(result);
                }
            }
        }
    }
    None
}

/// Extract all references with spans from a pair
pub(super) fn all_refs_with_spans_from(pair: &Pair<Rule>) -> Vec<(String, Option<Span>)> {
    let mut refs = Vec::new();
    collect_refs_recursive(pair, &mut refs);
    refs
}

fn collect_refs_recursive(pair: &Pair<Rule>, refs: &mut Vec<(String, Option<Span>)>) {
    match pair.as_rule() {
        Rule::owned_feature_chain => {
            // For feature chains like `pwrCmd.pwrLevel`, emit each part as a separate reference.
            // This enables semantic highlighting for each identifier in the chain.
            let raw = pair.as_str().trim();
            let base_span = pair.as_span();
            let (base_line, base_col) = base_span.start_pos().line_col();

            let mut offset = 0;
            for part in raw.split('.') {
                let part = part.trim();
                if part.is_empty() {
                    continue;
                }

                // Calculate the span for this part
                let part_start = offset;
                let part_end = part_start + part.len();
                let part_span = Span::from_coords(
                    base_line - 1,
                    base_col - 1 + part_start,
                    base_line - 1,
                    base_col - 1 + part_end,
                );

                // Strip quotes if present
                let name = strip_quotes(part);
                refs.push((name, Some(part_span)));

                // Move offset past this part and the dot separator
                offset = part_end + 1; // +1 for the '.'
            }
        }
        Rule::qualified_name => {
            // For qualified names like `SysML::Usage`, emit as a single reference
            // Build the qualified name from parts, stripping quotes where needed
            let parts: Vec<String> = pair
                .clone()
                .into_inner()
                .filter(|p| p.as_rule() == Rule::identifier || p.as_rule() == Rule::quoted_name)
                .map(|p| {
                    if p.as_rule() == Rule::quoted_name {
                        strip_quotes(p.as_str())
                    } else {
                        p.as_str().to_string()
                    }
                })
                .collect();
            if !parts.is_empty() {
                refs.push((parts.join("::"), Some(to_span(pair.as_span()))));
            } else {
                // Fallback for atomic rules: use the raw string but strip quotes if needed
                // Handle qualified names with quoted parts like "'Foo'::'Bar'" or "'Foo'"
                let raw = pair.as_str().trim();
                let name = strip_qualified_name_quotes(raw);
                refs.push((name, Some(to_span(pair.as_span()))));
            }
        }
        Rule::identifier => {
            refs.push((
                pair.as_str().trim().to_string(),
                Some(to_span(pair.as_span())),
            ));
        }
        Rule::quoted_name => {
            refs.push((strip_quotes(pair.as_str()), Some(to_span(pair.as_span()))));
        }
        _ => {
            for inner in pair.clone().into_inner() {
                collect_refs_recursive(&inner, refs);
            }
        }
    }
}

/// Extract type references from expressions (e.g., "= effects meta SysML::Usage" or "= causes as SysML::Usage")
/// Walks the expression tree looking for `meta_operator ~ type_result_member` or `as_operator ~ type_result_member` patterns
fn extract_meta_types_from_expression(pair: &Pair<Rule>) -> Vec<MetaRel> {
    let mut metas = Vec::new();
    collect_meta_types_recursive(pair, &mut metas, false);
    metas
}

fn collect_meta_types_recursive(
    pair: &Pair<Rule>,
    metas: &mut Vec<MetaRel>,
    saw_type_operator: bool,
) {
    let rule = pair.as_rule();

    match rule {
        Rule::meta_operator | Rule::as_operator => {
            // Next sibling should be the type reference
            // We handle this by setting a flag and looking for the type in children
        }
        Rule::type_result_member | Rule::type_reference_member | Rule::type_reference => {
            if saw_type_operator {
                // This is the type after a meta or as operator
                if let Some((target, span)) = ref_with_span_from(pair) {
                    metas.push(MetaRel {
                        target,
                        span: Some(span),
                    });
                }
            }
        }
        Rule::classification_expression => {
            // Look for meta_operator or as_operator followed by type
            let children: Vec<_> = pair.clone().into_inner().collect();
            for (i, child) in children.iter().enumerate() {
                if child.as_rule() == Rule::meta_operator || child.as_rule() == Rule::as_operator {
                    // Next child should be the type
                    if let Some(type_child) = children.get(i + 1)
                        && let Some((target, span)) = ref_with_span_from(type_child)
                    {
                        metas.push(MetaRel {
                            target,
                            span: Some(span),
                        });
                    }
                } else {
                    collect_meta_types_recursive(child, metas, false);
                }
            }
            return; // Don't recurse again
        }
        _ => {}
    }

    // Recurse into children
    for inner in pair.clone().into_inner() {
        collect_meta_types_recursive(&inner, metas, saw_type_operator);
    }
}

// ============================================================================
// Single-pass visitor
// ============================================================================

/// Visit a pair and extract all relevant information into the context.
/// This is the core single-pass algorithm.
fn visit_pair(pair: &Pair<Rule>, ctx: &mut ParseContext, depth: usize, in_body: bool) {
    let rule = pair.as_rule();

    // Don't descend into nested definitions/usages when extracting relationships
    // But we DO need to collect them as body members
    if depth > 0 && !in_body && (is_definition_rule(rule) || is_usage_rule(rule)) {
        return;
    }

    match rule {
        // ====================================================================
        // Identity extraction
        // ====================================================================
        Rule::identification => {
            for inner in pair.clone().into_inner() {
                visit_pair(&inner, ctx, depth + 1, in_body);
            }
        }

        Rule::regular_name => {
            if ctx.name.is_none() {
                for inner in pair.clone().into_inner() {
                    match inner.as_rule() {
                        Rule::identifier => {
                            ctx.name = Some(inner.as_str().to_string());
                            ctx.name_span = Some(to_span(inner.as_span()));
                        }
                        Rule::quoted_name => {
                            ctx.name = Some(strip_quotes(inner.as_str()));
                            ctx.name_span = Some(to_span(inner.as_span()));
                        }
                        _ => {}
                    }
                }
            }
        }

        Rule::short_name => {
            for inner in pair.clone().into_inner() {
                match inner.as_rule() {
                    Rule::identifier => {
                        ctx.short_name = Some(inner.as_str().to_string());
                        ctx.short_name_span = Some(to_span(inner.as_span()));
                    }
                    Rule::quoted_name => {
                        ctx.short_name = Some(strip_quotes(inner.as_str()));
                        ctx.short_name_span = Some(to_span(inner.as_span()));
                    }
                    _ => {}
                }
            }
        }

        // Fallback: direct identifier at top level (for simple declarations)
        Rule::identifier if ctx.name.is_none() && depth <= 2 => {
            ctx.name = Some(pair.as_str().to_string());
            ctx.name_span = Some(to_span(pair.as_span()));
        }

        // ====================================================================
        // Flag extraction
        // ====================================================================
        Rule::abstract_token => ctx.is_abstract = true,
        Rule::variation_token => ctx.is_variation = true,
        Rule::derived_token => ctx.is_derived = true,
        Rule::constant_token => ctx.is_const = true,

        // Also check in prefix rules
        Rule::basic_definition_prefix | Rule::definition_prefix | Rule::ref_prefix => {
            for inner in pair.clone().into_inner() {
                visit_pair(&inner, ctx, depth + 1, in_body);
            }
        }

        // ====================================================================
        // Relationship extraction
        // ====================================================================
        Rule::subclassification_part => {
            for p in pair.clone().into_inner() {
                if p.as_rule() == Rule::owned_subclassification {
                    for (target, span) in all_refs_with_spans_from(&p) {
                        ctx.relationships
                            .specializes
                            .push(SpecializationRel { target, span });
                    }
                }
            }
        }

        Rule::redefinition_part => {
            for p in pair.clone().into_inner() {
                if p.as_rule() == Rule::owned_subclassification {
                    for (target, span) in all_refs_with_spans_from(&p) {
                        ctx.relationships
                            .redefines
                            .push(RedefinitionRel { target, span });
                    }
                }
            }
        }

        Rule::feature_specialization => {
            for spec in pair.clone().into_inner() {
                match spec.as_rule() {
                    Rule::typings => {
                        if let Some((name, span)) = ref_with_span_from(&spec) {
                            ctx.relationships.typed_by = Some(name);
                            ctx.relationships.typed_by_span = Some(span);
                        }
                    }
                    Rule::subsettings => {
                        for (target, span) in all_refs_with_spans_from(&spec) {
                            ctx.relationships
                                .subsets
                                .push(SubsettingRel { target, span });
                        }
                    }
                    Rule::redefinitions => {
                        for (target, span) in all_refs_with_spans_from(&spec) {
                            ctx.relationships
                                .redefines
                                .push(RedefinitionRel { target, span });
                        }
                    }
                    Rule::references => {
                        for (target, span) in all_refs_with_spans_from(&spec) {
                            ctx.relationships
                                .references
                                .push(ReferenceRel { target, span });
                        }
                    }
                    Rule::crosses => {
                        for (target, span) in all_refs_with_spans_from(&spec) {
                            ctx.relationships.crosses.push(CrossRel { target, span });
                        }
                    }
                    _ => {}
                }
            }
        }

        // Also handle feature_specialization_part which wraps feature_specialization
        Rule::feature_specialization_part => {
            for inner in pair.clone().into_inner() {
                visit_pair(&inner, ctx, depth + 1, in_body);
            }
        }

        // Handle owned_feature_typing for parameter_binding
        Rule::owned_feature_typing => {
            if let Some((name, span)) = ref_with_span_from(pair) {
                ctx.relationships.typed_by = Some(name);
                ctx.relationships.typed_by_span = Some(span);
            }
        }

        // Handle owned_reference_subsetting (used in short-form usages like "satisfy SafetyReq;")
        // This captures the reference as a subsetting relationship
        Rule::owned_reference_subsetting => {
            for (target, span) in all_refs_with_spans_from(pair) {
                ctx.relationships
                    .subsets
                    .push(SubsettingRel { target, span });
            }
        }

        // Domain-specific relationships
        Rule::satisfaction_subject_member => {
            for (target, span) in all_refs_with_spans_from(pair) {
                ctx.relationships
                    .satisfies
                    .push(SatisfyRel { target, span });
            }
        }

        // ====================================================================
        // Value expressions - extract meta type references
        // ====================================================================
        Rule::value_part | Rule::feature_value => {
            // Extract meta type references from the expression
            let meta_refs = extract_meta_types_from_expression(pair);
            ctx.relationships.meta.extend(meta_refs);
        }

        // ====================================================================
        // Body extraction
        // ====================================================================
        _ if is_body_rule(rule) => {
            // Enter body context and collect members
            for inner in pair.clone().into_inner() {
                visit_body_member(&inner, ctx);
            }
        }

        // ====================================================================
        // Default: recurse into children
        // ====================================================================
        _ => {
            for inner in pair.clone().into_inner() {
                visit_pair(&inner, ctx, depth + 1, in_body);
            }
        }
    }
}

/// Visit a body member and add it to the appropriate collection
fn visit_body_member(pair: &Pair<Rule>, ctx: &mut ParseContext) {
    let rule = pair.as_rule();

    match rule {
        // Comments
        Rule::documentation | Rule::block_comment => {
            let comment = Comment {
                content: pair.as_str().to_string(),
                span: Some(to_span(pair.as_span())),
            };
            ctx.def_members
                .push(DefinitionMember::Comment(Box::new(comment.clone())));
            ctx.usage_members.push(UsageMember::Comment(comment));
        }

        // Parameter binding (for in/out/inout parameters)
        Rule::parameter_binding => {
            let usage = parse_usage_with_kind(pair.clone(), UsageKind::Reference);
            ctx.def_members
                .push(DefinitionMember::Usage(Box::new(usage.clone())));
            ctx.usage_members.push(UsageMember::Usage(Box::new(usage)));
        }

        // Nested usages
        _ if is_usage_rule(rule) => {
            let usage = parse_usage_with_kind(
                pair.clone(),
                to_usage_kind(rule).unwrap_or(UsageKind::Reference),
            );
            ctx.def_members
                .push(DefinitionMember::Usage(Box::new(usage.clone())));
            ctx.usage_members.push(UsageMember::Usage(Box::new(usage)));
        }

        // Recurse into containers
        _ => {
            for inner in pair.clone().into_inner() {
                visit_body_member(&inner, ctx);
            }
        }
    }
}

// ============================================================================
// Public API
// ============================================================================

/// Parse a definition from a pest pair using single-pass extraction
pub fn parse_definition(pair: Pair<Rule>) -> Result<Definition, ParseError> {
    let kind = to_def_kind(pair.as_rule()).map_err(|_| ParseError::invalid_rule("definition"))?;

    let mut ctx = ParseContext::new();
    visit_pair(&pair, &mut ctx, 0, false);

    Ok(Definition {
        kind,
        name: ctx.name,
        short_name: ctx.short_name,
        short_name_span: ctx.short_name_span,
        relationships: ctx.relationships,
        body: ctx.def_members,
        span: ctx.name_span,
        is_abstract: ctx.is_abstract,
        is_variation: ctx.is_variation,
    })
}

/// Parse a usage from a pest pair using single-pass extraction
fn parse_usage_with_kind(pair: Pair<Rule>, kind: UsageKind) -> Usage {
    let mut ctx = ParseContext::new();
    visit_pair(&pair, &mut ctx, 0, false);

    Usage {
        kind,
        name: ctx.name,
        short_name: ctx.short_name,
        short_name_span: ctx.short_name_span,
        relationships: ctx.relationships,
        body: ctx.usage_members,
        span: ctx.name_span,
        is_derived: ctx.is_derived,
        is_const: ctx.is_const,
    }
}

/// Parse a usage, inferring kind from the rule
pub fn parse_usage(pair: Pair<Rule>) -> Usage {
    let kind = to_usage_kind(pair.as_rule()).unwrap_or(UsageKind::Reference);
    parse_usage_with_kind(pair, kind)
}

// ============================================================================
// Parse functions for other AST types
// ============================================================================

/// Parse a package from pest pairs
pub fn parse_package(pairs: &mut Pairs<Rule>) -> Result<Package, ParseError> {
    let mut name = None;
    let mut elements = Vec::new();
    let mut span = None;

    for pair in pairs {
        match pair.as_rule() {
            Rule::package_declaration => {
                if let Some(p) = find_in(&pair, Rule::identification) {
                    let (extracted_name, extracted_span) = extract_name_from_identification(p);
                    name = extracted_name;
                    span = extracted_span;
                }
            }
            Rule::package_body => {
                elements = pair
                    .into_inner()
                    .filter(|p| p.as_rule() == Rule::package_body_items)
                    .flat_map(|p| p.into_inner())
                    .filter(|p| p.as_rule() == Rule::package_body_element)
                    .filter_map(|p| parse_element(&mut p.into_inner()).ok())
                    .collect();
            }
            _ => {}
        }
    }

    Ok(Package {
        name,
        elements,
        span,
    })
}

/// Parse a comment from pest pairs
pub fn parse_comment(pairs: &mut Pairs<Rule>) -> Result<Comment, ParseError> {
    let pair = pairs.next().ok_or(ParseError::no_match())?;
    if pair.as_rule() != Rule::comment_annotation {
        return Err(ParseError::no_match());
    }
    Ok(Comment {
        content: pair.as_str().to_string(),
        span: Some(to_span(pair.as_span())),
    })
}

/// Parse an import from pest pairs
pub fn parse_import(pairs: &mut Pairs<Rule>) -> Result<Import, ParseError> {
    let mut is_recursive = false;
    let mut is_public = false;
    let mut path = String::new();
    let mut path_span = None;
    let mut span = None;

    fn process_pair(
        pair: Pair<Rule>,
        path: &mut String,
        path_span: &mut Option<Span>,
        span: &mut Option<Span>,
        is_public: &mut bool,
        is_recursive: &mut bool,
    ) {
        match pair.as_rule() {
            Rule::import_prefix => {
                for child in pair.into_inner() {
                    if child.as_rule() == Rule::visibility {
                        *is_public = child.as_str().trim() == "public";
                    }
                }
            }
            Rule::imported_membership | Rule::imported_namespace => {
                *path = pair.as_str().to_string();
                *span = Some(to_span(pair.as_span()));
                *path_span = Some(to_span(pair.as_span()));
                *is_recursive = pair
                    .clone()
                    .into_inner()
                    .any(|p| p.as_rule() == Rule::recursive_marker);
            }
            Rule::membership_import | Rule::namespace_import => {
                // These contain import_prefix and imported_membership/imported_namespace
                for child in pair.into_inner() {
                    process_pair(child, path, path_span, span, is_public, is_recursive);
                }
            }
            _ => {}
        }
    }

    for pair in pairs {
        process_pair(
            pair,
            &mut path,
            &mut path_span,
            &mut span,
            &mut is_public,
            &mut is_recursive,
        );
    }

    Ok(Import {
        path,
        path_span,
        is_recursive,
        is_public,
        span,
    })
}

/// Parse an alias from pest pairs
pub fn parse_alias(pairs: &mut Pairs<Rule>) -> Result<Alias, ParseError> {
    let mut name = None;
    let mut target = String::new();
    let mut target_span = None;
    let mut span = None;

    for pair in pairs {
        match pair.as_rule() {
            Rule::identification => {
                let (extracted_name, extracted_span) = extract_name_from_identification(pair);
                name = extracted_name;
                span = extracted_span;
            }
            Rule::element_reference => {
                target = pair.as_str().to_string();
                target_span = Some(to_span(pair.as_span()));
            }
            _ => {}
        }
    }

    Ok(Alias {
        name,
        target,
        target_span,
        span,
    })
}

/// Parse an element from pest pairs
pub fn parse_element(pairs: &mut Pairs<Rule>) -> Result<Element, ParseError> {
    let mut pair = pairs.next().ok_or(ParseError::no_match())?;

    // Check for visibility prefix (public/private/protected)
    if pair.as_rule() == Rule::visibility {
        pair = pairs.next().ok_or(ParseError::no_match())?;
    }

    Ok(match pair.as_rule() {
        Rule::package | Rule::library_package | Rule::package_declaration => {
            Element::Package(parse_package(&mut pair.into_inner())?)
        }
        Rule::definition_member_element
        | Rule::usage_member
        | Rule::definition_element
        | Rule::usage_element
        | Rule::occurrence_usage_element
        | Rule::structure_usage_element
        | Rule::behavior_usage_element
        | Rule::non_occurrence_usage_element => parse_element(&mut pair.into_inner())?,
        r if is_definition_rule(r) => Element::Definition(parse_definition(pair)?),
        r if is_usage_rule(r) => Element::Usage(parse_usage(pair)),
        Rule::comment_annotation => Element::Comment(parse_comment(&mut pair.into_inner())?),
        Rule::import => Element::Import(parse_import(&mut pair.into_inner())?),
        Rule::alias_member_element => Element::Alias(parse_alias(&mut pair.into_inner())?),
        _ => return Err(ParseError::no_match()),
    })
}

/// Parse a SysML file from pest pairs (main entry point)
pub fn parse_file(pairs: &mut Pairs<Rule>) -> Result<SysMLFile, ParseError> {
    let model = pairs.next().ok_or(ParseError::no_match())?;
    if model.as_rule() != Rule::file {
        return Err(ParseError::no_match());
    }

    let mut elements = Vec::new();
    let mut namespace = None;
    let mut namespaces = Vec::new();

    // Grammar structure: model = { SOI ~ root_namespace ~ EOI }
    // root_namespace = { package_body_element* }
    // package_body_element = { package | library_package | import | ... }
    // So we need to find root_namespace, then iterate its package_body_element children
    for pair in model.into_inner() {
        if pair.as_rule() == Rule::root_namespace {
            for body_element in pair.into_inner() {
                // body_element is package_body_element, which contains the actual element
                // We need to iterate its inner to get the actual rule (package, import, etc.)
                if let Ok(element) = parse_element(&mut body_element.into_inner()) {
                    // Track all package declarations (Issue #10)
                    if let Element::Package(ref pkg) = element
                        && pkg.elements.is_empty()
                        && let Some(ref name) = pkg.name
                    {
                        let ns = NamespaceDeclaration {
                            name: name.clone(),
                            span: pkg.span,
                        };

                        // Keep first namespace for backward compatibility
                        if namespace.is_none() {
                            namespace = Some(ns.clone());
                        }

                        // Collect all namespaces
                        namespaces.push(ns);
                    }
                    elements.push(element);
                }
            }
        }
    }

    Ok(SysMLFile {
        namespace,
        namespaces,
        elements,
    })
}

// ============================================================================
// Tests
// ============================================================================

#[cfg(test)]
mod tests {
    use super::*;
    use crate::parser::sysml::SysMLParser;
    use crate::syntax::sysml::ast::DefinitionKind;
    use pest::Parser;

    #[test]
    fn test_parse_metadata_def_with_short_name() {
        let source = "metadata def <original> OriginalRequirementMetadata :> SemanticMetadata;";
        let pair = SysMLParser::parse(Rule::metadata_definition, source)
            .unwrap()
            .next()
            .unwrap();

        let def = parse_definition(pair).unwrap();

        assert_eq!(def.kind, DefinitionKind::Metadata);
        // The main name should be OriginalRequirementMetadata, NOT original
        assert_eq!(
            def.name,
            Some("OriginalRequirementMetadata".to_string()),
            "Expected regular name 'OriginalRequirementMetadata', got {:?}",
            def.name
        );
        // The short name should be original
        assert_eq!(
            def.short_name,
            Some("original".to_string()),
            "Expected short name 'original', got {:?}",
            def.short_name
        );
        // Specialization should be captured
        assert_eq!(
            def.relationships.specializes.len(),
            1,
            "Expected 1 specialization"
        );
        assert_eq!(
            def.relationships.specializes[0].target, "SemanticMetadata",
            "Expected specialization target 'SemanticMetadata'"
        );
    }

    #[test]
    fn test_parse_quoted_name_redefines() {
        let source = r#"attribute 'packet primary header' redefines 'packet header';"#;
        let pair = SysMLParser::parse(Rule::attribute_usage, source)
            .unwrap()
            .next()
            .unwrap();

        let usage = parse_usage(pair);

        assert_eq!(
            usage.name,
            Some("packet primary header".to_string()),
            "Name should not have quotes"
        );
        assert_eq!(
            usage.relationships.redefines.len(),
            1,
            "Expected 1 redefinition"
        );
        assert_eq!(
            usage.relationships.redefines[0].target, "packet header",
            "Redefines target should not have quotes"
        );
    }

    #[test]
    fn test_parse_part_def() {
        let source = "part def Vehicle;";
        let pair = SysMLParser::parse(Rule::part_definition, source)
            .unwrap()
            .next()
            .unwrap();

        let def = parse_definition(pair).unwrap();

        assert_eq!(def.kind, DefinitionKind::Part);
        assert_eq!(def.name, Some("Vehicle".to_string()));
        assert!(def.span.is_some());
    }

    #[test]
    fn test_parse_part_def_with_specialization() {
        let source = "part def Car :> Vehicle;";
        let pair = SysMLParser::parse(Rule::part_definition, source)
            .unwrap()
            .next()
            .unwrap();

        let def = parse_definition(pair).unwrap();

        assert_eq!(def.name, Some("Car".to_string()));
        assert_eq!(def.relationships.specializes.len(), 1);
        assert_eq!(def.relationships.specializes[0].target, "Vehicle");
        assert!(def.relationships.specializes[0].span.is_some());
    }

    #[test]
    fn test_parse_abstract_part_def() {
        let source = "abstract part def AbstractVehicle;";
        let pair = SysMLParser::parse(Rule::part_definition, source)
            .unwrap()
            .next()
            .unwrap();

        let def = parse_definition(pair).unwrap();

        assert_eq!(def.name, Some("AbstractVehicle".to_string()));
        assert!(def.is_abstract);
    }

    #[test]
    fn test_parse_part_usage_with_typing() {
        let source = "part myCar : Car;";
        let pair = SysMLParser::parse(Rule::part_usage, source)
            .unwrap()
            .next()
            .unwrap();

        let usage = parse_usage(pair);

        assert_eq!(usage.kind, UsageKind::Part);
        assert_eq!(usage.name, Some("myCar".to_string()));
        assert_eq!(usage.relationships.typed_by, Some("Car".to_string()));
        assert!(usage.relationships.typed_by_span.is_some());
    }

    #[test]
    fn test_parse_constraint_def_with_parameters() {
        let source = r#"constraint def MassConstraint {
            in totalMass : MassValue;
        }"#;
        let pair = SysMLParser::parse(Rule::constraint_definition, source)
            .unwrap()
            .next()
            .unwrap();

        let def = parse_definition(pair).unwrap();

        assert_eq!(def.kind, DefinitionKind::Constraint);
        assert_eq!(def.name, Some("MassConstraint".to_string()));
        assert_eq!(def.body.len(), 1);

        // Check the parameter was extracted
        if let DefinitionMember::Usage(usage) = &def.body[0] {
            assert_eq!(usage.name, Some("totalMass".to_string()));
            assert_eq!(usage.relationships.typed_by, Some("MassValue".to_string()));
        } else {
            panic!("Expected Usage member");
        }
    }

    #[test]
    fn test_parse_satisfy_requirement_usage() {
        let source = "satisfy requirement req1 : Req1 by system;";
        let pair = SysMLParser::parse(Rule::satisfy_requirement_usage, source)
            .unwrap()
            .next()
            .unwrap();

        let usage = parse_usage(pair);

        assert_eq!(usage.kind, UsageKind::SatisfyRequirement);
        assert_eq!(usage.name, Some("req1".to_string()));
        // Typing should be extracted
        assert_eq!(usage.relationships.typed_by, Some("Req1".to_string()));
        // Satisfies should contain "system"
        assert_eq!(usage.relationships.satisfies.len(), 1);
        assert_eq!(usage.relationships.satisfies[0].target, "system");
    }

    #[test]
    fn test_parse_satisfy_short_form() {
        // This is the short form: "satisfy SafetyReq;" without explicit typing or by clause
        let source = "satisfy SafetyReq;";
        let pair = SysMLParser::parse(Rule::satisfy_requirement_usage, source)
            .unwrap()
            .next()
            .unwrap();

        let usage = parse_usage(pair);

        assert_eq!(usage.kind, UsageKind::SatisfyRequirement);
        // The target should be captured in subsets
        assert_eq!(usage.relationships.subsets.len(), 1);
        assert_eq!(usage.relationships.subsets[0].target, "SafetyReq");
    }

    #[test]
    fn test_parse_satisfy_with_requirement_keyword() {
        // Syntax: "satisfy requirement SafetyReq;"
        // SafetyReq is the NAME of the satisfy usage, not a type reference
        let source = "satisfy requirement SafetyReq;";
        let pair = SysMLParser::parse(Rule::satisfy_requirement_usage, source)
            .unwrap()
            .next()
            .unwrap();

        let usage = parse_usage(pair);

        assert_eq!(usage.kind, UsageKind::SatisfyRequirement);
        // SafetyReq should be the name of the satisfy usage
        assert_eq!(
            usage.name,
            Some("SafetyReq".to_string()),
            "Expected SafetyReq to be the name of the satisfy usage"
        );
    }

    #[test]
    fn test_parse_reference_usage_with_meta_expression() {
        // Parse a reference usage that includes a meta expression in its value
        let source = "ref :>> baseType = causations meta SysML::Usage;";
        let pair = SysMLParser::parse(Rule::reference_usage, source)
            .unwrap()
            .next()
            .unwrap();

        let usage = parse_usage_with_kind(pair, UsageKind::Reference);

        // Debug output
        println!("name: {:?}", usage.name);
        println!("references: {:?}", usage.relationships.references);
        println!("meta: {:?}", usage.relationships.meta);

        // The meta relationship should be captured
        assert!(
            !usage.relationships.meta.is_empty(),
            "Expected meta relationship to be extracted from expression, got: {:?}",
            usage.relationships.meta
        );
        assert_eq!(
            usage.relationships.meta[0].target, "SysML::Usage",
            "Expected meta target to be SysML::Usage"
        );
    }

    #[test]
    fn test_parse_connection_def_with_end_usages() {
        // Test that end usages in connection definitions capture type references
        let source = r#"connection def Req1_Derivation {
            end r1 : Req1;
            end r1_1 : Req1_1;
        }"#;
        let pair = SysMLParser::parse(Rule::connection_definition, source)
            .unwrap()
            .next()
            .unwrap();

        let def = parse_definition(pair).unwrap();

        assert_eq!(def.kind, DefinitionKind::Connection);
        assert_eq!(def.name, Some("Req1_Derivation".to_string()));

        // Check that we have 2 end usages in the body
        let usages: Vec<_> = def
            .body
            .iter()
            .filter_map(|m| match m {
                DefinitionMember::Usage(u) => Some(u.as_ref()),
                _ => None,
            })
            .collect();

        assert_eq!(usages.len(), 2, "Expected 2 end usages");

        // Check that type references are captured
        assert_eq!(
            usages[0].relationships.typed_by,
            Some("Req1".to_string()),
            "First end should be typed by Req1"
        );
        assert!(
            usages[0].relationships.typed_by_span.is_some(),
            "First end should have typed_by_span"
        );
        assert_eq!(
            usages[1].relationships.typed_by,
            Some("Req1_1".to_string()),
            "Second end should be typed by Req1_1"
        );
        assert!(
            usages[1].relationships.typed_by_span.is_some(),
            "Second end should have typed_by_span"
        );
    }

    #[test]
    fn test_owned_feature_chain_extracts_separate_references() {
        // Test that owned_feature_chain like `pwrCmd.pwrLevel` extracts each part separately
        let source = "attribute :>> pwrCmd.pwrLevel = 0;";
        let pair = SysMLParser::parse(Rule::attribute_usage, source)
            .unwrap()
            .next()
            .unwrap();

        // Use the all_refs_with_spans_from function to check extracted references
        let refs = all_refs_with_spans_from(&pair);

        // Find the references from the owned_feature_chain
        let pwr_cmd_refs: Vec<_> = refs.iter().filter(|(name, _)| name == "pwrCmd").collect();
        let pwr_level_refs: Vec<_> = refs.iter().filter(|(name, _)| name == "pwrLevel").collect();

        assert!(
            !pwr_cmd_refs.is_empty(),
            "Should have a reference for 'pwrCmd', got: {:?}",
            refs
        );
        assert!(
            !pwr_level_refs.is_empty(),
            "Should have a reference for 'pwrLevel', got: {:?}",
            refs
        );

        // Check that pwrCmd span is correct (starts at position of 'p' in pwrCmd)
        if let Some((_, Some(span))) = pwr_cmd_refs.first() {
            // "attribute :>> pwrCmd.pwrLevel = 0;"
            //               ^ pwrCmd starts here (column 15, 0-indexed = 14)
            assert_eq!(span.start.column, 14, "pwrCmd should start at column 14");
            // pwrCmd is 6 characters long, so end column should be 14+6=20
            assert_eq!(span.end.column, 20, "pwrCmd should end at column 20");
        }

        // Check that pwrLevel span is correct
        if let Some((_, Some(span))) = pwr_level_refs.first() {
            // "attribute :>> pwrCmd.pwrLevel = 0;"
            //                      ^ pwrLevel starts here (column 21, 0-indexed = 20+1=21)
            assert_eq!(span.start.column, 21, "pwrLevel should start at column 21");
            // pwrLevel is 8 characters long, so end column should be 21+8=29
            assert_eq!(span.end.column, 29, "pwrLevel should end at column 29");
        }
    }

    // ========================================================================
    // ParseError tests
    // ========================================================================

    #[test]
    fn test_parse_error_no_match() {
        let error = ParseError::no_match();
        assert_eq!(error.message, "No matching rule");
    }

    #[test]
    fn test_parse_error_invalid_rule() {
        let error = ParseError::invalid_rule("some_rule");
        assert_eq!(error.message, "Invalid rule: some_rule");
    }

    #[test]
    fn test_parse_error_invalid_rule_empty() {
        let error = ParseError::invalid_rule("");
        assert_eq!(error.message, "Invalid rule: ");
    }

    #[test]
    fn test_parse_error_equality() {
        let error1 = ParseError::no_match();
        let error2 = ParseError::no_match();
        assert_eq!(error1, error2);

        let error3 = ParseError::invalid_rule("rule_a");
        let error4 = ParseError::invalid_rule("rule_a");
        assert_eq!(error3, error4);

        // Different errors should not be equal
        assert_ne!(error1, error3);
    }

    #[test]
    fn test_parse_error_clone() {
        let error = ParseError::invalid_rule("test");
        let cloned = error.clone();
        assert_eq!(error, cloned);
        assert_eq!(cloned.message, "Invalid rule: test");
    }
}