oxirs-star 0.2.4

RDF-star and SPARQL-star grammar support for quoted triples
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
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
//! RDF-star parsing implementations for various formats.
//!
//! This module provides parsers for RDF-star formats including:
//! - Turtle-star (*.ttls)
//! - N-Triples-star (*.nts)
//! - TriG-star (*.trigs)
//! - N-Quads-star (*.nqs)

// Internal parser submodules
#[path = "parser/context.rs"]
mod context;
#[path = "parser/jsonld.rs"]
mod jsonld;
#[path = "parser/simd_scanner.rs"]
mod simd_scanner;
#[path = "parser/tokenizer.rs"]
mod tokenizer;

use std::io::{BufRead, BufReader, Read};
use std::str::FromStr;

use anyhow::{Context, Result};
use tracing::{debug, span, Level};

use crate::model::{StarGraph, StarQuad, StarTerm, StarTriple};
use crate::{StarConfig, StarError, StarResult};

// Import parser context types
use context::{ErrorSeverity, ParseContext, TrigParserState};
use simd_scanner::SimdScanner;

// Re-export public error types
pub use context::{ErrorSeverity as PublicErrorSeverity, ParseError as PublicParseError};

/// RDF-star format types
#[derive(Debug, Clone, Copy, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
pub enum StarFormat {
    /// Turtle-star format
    TurtleStar,
    /// N-Triples-star format
    NTriplesStar,
    /// TriG-star format (named graphs)
    TrigStar,
    /// N-Quads-star format
    NQuadsStar,
    /// JSON-LD-star format
    JsonLdStar,
}

// TrigParserState has been moved to parser/context.rs

impl FromStr for StarFormat {
    type Err = StarError;

    fn from_str(s: &str) -> Result<Self, Self::Err> {
        match s.to_lowercase().as_str() {
            "turtle-star" | "ttls" => Ok(StarFormat::TurtleStar),
            "ntriples-star" | "nts" => Ok(StarFormat::NTriplesStar),
            "trig-star" | "trigs" => Ok(StarFormat::TrigStar),
            "nquads-star" | "nqs" => Ok(StarFormat::NQuadsStar),
            "json-ld-star" | "jsonld-star" | "jlds" => Ok(StarFormat::JsonLdStar),
            _ => Err(StarError::parse_error(format!("Unknown format: {s}"))),
        }
    }
}

// ParseContext, ParseError, and ErrorSeverity have been moved to parser/context.rs

/// RDF-star parser with support for multiple formats
pub struct StarParser {
    config: StarConfig,
    simd_scanner: SimdScanner,
}

impl StarParser {
    /// Create a new parser with default configuration
    pub fn new() -> Self {
        Self {
            config: StarConfig::default(),
            simd_scanner: SimdScanner::new(),
        }
    }

    /// Create a new parser with custom configuration
    pub fn with_config(config: StarConfig) -> Self {
        Self {
            config,
            simd_scanner: SimdScanner::new(),
        }
    }

    /// Set strict mode for parsing
    pub fn set_strict_mode(&mut self, strict: bool) {
        self.config.strict_mode = strict;
    }

    /// Set error recovery mode
    pub fn set_error_recovery(&mut self, _enabled: bool) {
        // This is a no-op for now since error recovery is handled per-parse
        // but we keep the method for API compatibility
    }

    /// Get parsing errors (returns empty for stateless parser)
    pub fn get_errors(&self) -> Vec<StarError> {
        // The current parser implementation doesn't store errors
        // since they're returned through the Result type
        Vec::new()
    }

    /// Get parsing context configured for this parser
    fn create_parse_context(&self) -> ParseContext {
        ParseContext::with_config(self.config.strict_mode, true) // Enable error recovery by default
    }

    /// Parse RDF-star data from a reader
    pub fn parse<R: Read>(&self, reader: R, format: StarFormat) -> StarResult<StarGraph> {
        let span = span!(Level::INFO, "parse_rdf_star", format = ?format);
        let _enter = span.enter();

        match format {
            StarFormat::TurtleStar => self.parse_turtle_star(reader),
            StarFormat::NTriplesStar => self.parse_ntriples_star(reader),
            StarFormat::TrigStar => self.parse_trig_star(reader),
            StarFormat::NQuadsStar => self.parse_nquads_star(reader),
            StarFormat::JsonLdStar => self.parse_jsonld_star(reader),
        }
    }

    /// Parse RDF-star from string
    pub fn parse_str(&self, data: &str, format: StarFormat) -> StarResult<StarGraph> {
        self.parse(data.as_bytes(), format)
    }

    /// Parse Turtle-star format with enhanced error handling and multi-line statement support
    pub fn parse_turtle_star<R: Read>(&self, reader: R) -> StarResult<StarGraph> {
        let span = span!(Level::DEBUG, "parse_turtle_star");
        let _enter = span.enter();

        let mut graph = StarGraph::new();
        let mut context = self.create_parse_context();
        let buf_reader = BufReader::new(reader);

        // Accumulator for multi-line statements
        let mut accumulated_line = String::new();
        let mut error_count = 0;
        let max_errors = self.config.max_parse_errors.unwrap_or(100);

        for (line_num, line_result) in buf_reader.lines().enumerate() {
            let line = line_result.map_err(|e| StarError::parse_error(e.to_string()))?;
            // Use SIMD-accelerated trimming
            let line = self.simd_scanner.trim(&line);

            // Update position tracking
            context.update_position(line_num + 1, 0);

            // Skip empty lines and comments
            if line.is_empty() || line.starts_with('#') {
                continue;
            }

            // Strip inline comments (but respect strings)
            let line = tokenizer::strip_inline_comment(line);
            // Use SIMD-accelerated trimming
            let line = self.simd_scanner.trim(line);

            // Skip if line becomes empty after comment stripping
            if line.is_empty() {
                continue;
            }

            // Check if line looks incomplete after comment stripping (no period, not a directive, not in a special block)
            // This handles cases like "ex:charlie ex:knows  # missing object"
            // But allow lines that are part of multi-line blocks (annotation blocks or quoted triples)
            // Use SIMD-accelerated pattern detection
            let in_annotation_block = self
                .simd_scanner
                .contains_annotation_block(&accumulated_line)
                || self.simd_scanner.contains_annotation_block(line);
            let in_quoted_triple = self.simd_scanner.contains_quoted_triple(&accumulated_line);

            if !line.ends_with('.')
                && !line.ends_with('{')
                && !line.ends_with('}')
                && !in_annotation_block
                && !in_quoted_triple
            {
                // Check if line contains incomplete quoted triple using SIMD
                let has_incomplete_quoted_triple = if self.simd_scanner.contains_quoted_triple(line)
                {
                    // Use SIMD-accelerated counting
                    !self.simd_scanner.is_quoted_balanced(line)
                } else {
                    false
                };

                // This line appears incomplete and might have been truncated by inline comment
                // Skip it in error recovery mode if:
                // 1. It's not a directive (doesn't start with '@'), AND
                // 2. It's incomplete (doesn't end with '.', '{', '}' and isn't part of annotation/quoted block)
                if !line.starts_with('@') && !context.strict_mode {
                    // This is an incomplete statement - skip it in error recovery mode
                    let reason = if has_incomplete_quoted_triple {
                        "incomplete quoted triple"
                    } else {
                        "incomplete triple (possibly truncated by comment)"
                    };
                    context.add_error(
                        format!("Skipping {}: {line}", reason),
                        line.to_string(),
                        ErrorSeverity::Warning,
                    );
                    continue;
                }
            }

            // Handle directives immediately (they're always single-line)
            if line.starts_with("@prefix") || line.starts_with("@base") {
                match self.parse_turtle_line_safe(line, &mut context, &mut graph) {
                    Ok(_) => continue,
                    Err(e) => {
                        error_count += 1;
                        let line_number = line_num + 1;
                        let error_context = format!("line {line_number}: {line}");
                        context.add_error(
                            format!("Parse error: {e}"),
                            error_context,
                            if context.strict_mode {
                                ErrorSeverity::Fatal
                            } else {
                                ErrorSeverity::Error
                            },
                        );

                        if context.strict_mode || error_count >= max_errors {
                            return Err(StarError::parse_error(format!(
                                "Parsing failed at line {} with {} errors",
                                line_num + 1,
                                context.get_errors().len()
                            )));
                        }
                        continue;
                    }
                }
            }

            // Accumulate lines for multi-line statements
            accumulated_line.push_str(line);
            accumulated_line.push(' ');

            // Check if we have a complete statement (ends with '.')
            if tokenizer::is_complete_turtle_statement(&accumulated_line) {
                match self.parse_turtle_line_safe(accumulated_line.trim(), &mut context, &mut graph)
                {
                    Ok(_) => {
                        accumulated_line.clear();
                    }
                    Err(e) => {
                        error_count += 1;
                        let line_number = line_num + 1;
                        let error_context =
                            format!("line {line_number}: {}", accumulated_line.trim());
                        context.add_error(
                            format!("Parse error: {e}"),
                            error_context,
                            if context.strict_mode {
                                ErrorSeverity::Fatal
                            } else {
                                ErrorSeverity::Error
                            },
                        );

                        if context.strict_mode || error_count >= max_errors {
                            return Err(StarError::parse_error(format!(
                                "Parsing failed at line {} with {} errors. First error: {}",
                                line_num + 1,
                                context.get_errors().len(),
                                context
                                    .get_errors()
                                    .first()
                                    .map(|e| &e.message)
                                    .unwrap_or(&"Unknown error".to_string())
                            )));
                        }

                        // Clear accumulated line and try to recover
                        accumulated_line.clear();
                    }
                }
            }
        }

        // Handle any remaining incomplete statement
        if !accumulated_line.trim().is_empty() {
            context.add_error(
                "Incomplete Turtle-star statement at end of input".to_string(),
                accumulated_line.trim().to_string(),
                ErrorSeverity::Error,
            );
            if context.strict_mode {
                return Err(StarError::parse_error(format!(
                    "Incomplete Turtle-star statement at end of input: {}",
                    accumulated_line.trim()
                )));
            }
        }

        // Report any accumulated errors
        if !context.get_errors().is_empty() {
            debug!(
                "Parsing completed with {} warnings/errors",
                context.get_errors().len()
            );
            for error in context.get_errors() {
                debug!(
                    "Line {}, Column {}: {} ({})",
                    error.line, error.column, error.message, error.context
                );
            }
        }

        debug!("Parsed {} triples in Turtle-star format", graph.len());
        Ok(graph)
    }

    /// Parse a single Turtle-star line with enhanced error handling
    fn parse_turtle_line_safe(
        &self,
        line: &str,
        context: &mut ParseContext,
        graph: &mut StarGraph,
    ) -> StarResult<()> {
        // Handle directives
        if line.starts_with("@prefix") {
            return self.parse_prefix_directive_safe(line, context);
        }

        if line.starts_with("@base") {
            return self.parse_base_directive_safe(line, context);
        }

        // Parse triple
        if let Some(stripped) = line.strip_suffix('.') {
            let triple_str = stripped.trim();

            // Check for annotation syntax {| ... |}
            if triple_str.contains("{|") {
                return self.parse_triple_with_annotation(triple_str, context, graph);
            }

            match self.parse_triple_pattern_safe(triple_str, context) {
                Ok(triple) => {
                    graph.insert(triple).map_err(|e| {
                        StarError::parse_error(format!("Failed to insert triple: {e}"))
                    })?;
                }
                Err(e) => {
                    let error_msg = format!("Failed to parse triple pattern '{triple_str}': {e}");
                    context.add_error(error_msg.clone(), line.to_string(), ErrorSeverity::Error);

                    if context.strict_mode {
                        return Err(StarError::parse_error(error_msg));
                    }
                    // In non-strict mode, log error and continue
                }
            }
        } else if !line.trim().is_empty() {
            // Non-empty line that doesn't end with '.' - potential malformed statement
            let error_msg = format!("Malformed statement (missing terminating '.'): {line}");
            context.add_error(error_msg.clone(), line.to_string(), ErrorSeverity::Warning);

            if context.strict_mode {
                return Err(StarError::parse_error(error_msg));
            }
        }

        Ok(())
    }

    /// Parse triple with annotation syntax: subject predicate object {| annotation properties |} .
    ///
    /// Example: :alice :age 30 {| :certainty 0.9; :source :survey |} .
    /// Expands to:
    ///   <<:alice :age 30>> :certainty 0.9 .
    ///   <<:alice :age 30>> :source :survey .
    fn parse_triple_with_annotation(
        &self,
        statement: &str,
        context: &mut ParseContext,
        graph: &mut StarGraph,
    ) -> StarResult<()> {
        // Find annotation block delimiters
        let annotation_start = statement.find("{|").ok_or_else(|| {
            StarError::parse_error("Missing annotation start delimiter {|".to_string())
        })?;

        let annotation_end = statement.find("|}").ok_or_else(|| {
            StarError::parse_error("Missing annotation end delimiter |}".to_string())
        })?;

        if annotation_end < annotation_start {
            return Err(StarError::parse_error(
                "Annotation delimiters in wrong order".to_string(),
            ));
        }

        // Extract base triple and annotation block
        let base_triple_str = statement[..annotation_start].trim();
        let annotation_block = statement[annotation_start + 2..annotation_end].trim();

        // Parse the base triple
        let base_triple = self.parse_triple_pattern_safe(base_triple_str, context)?;

        // If the base triple has a quoted triple as subject, insert it
        // This is a regular assertion about a quoted triple, not just metadata
        if base_triple.subject.is_quoted_triple() {
            graph.insert(base_triple.clone())?;
        }

        // Create quoted triple from base triple for annotations
        let quoted_triple = StarTerm::quoted_triple(base_triple.clone());

        // Parse annotation properties (separated by semicolons)
        let annotation_statements = annotation_block.split(';');

        for ann_stmt in annotation_statements {
            let ann_stmt = ann_stmt.trim();
            if ann_stmt.is_empty() {
                continue;
            }

            // Parse annotation as predicate-object pair
            let ann_tokens = self
                .tokenize_triple(&format!("_:dummy {ann_stmt}"))
                .map_err(|e| StarError::parse_error(e.to_string()))?;
            if ann_tokens.len() != 3 {
                let error_msg = format!(
                    "Invalid annotation property: expected predicate object, found {} terms",
                    ann_tokens.len() - 1
                );
                context.add_error(
                    error_msg.clone(),
                    ann_stmt.to_string(),
                    ErrorSeverity::Error,
                );
                if context.strict_mode {
                    return Err(StarError::parse_error(error_msg));
                }
                continue;
            }

            // Parse annotation predicate and object (skip dummy subject at index 0)
            let ann_predicate = self.parse_term_safe(&ann_tokens[1], context)?;
            let ann_object = self.parse_term_safe(&ann_tokens[2], context)?;

            // Create annotation triple: <<base triple>> annotation_predicate annotation_object
            let annotation_triple =
                StarTriple::new(quoted_triple.clone(), ann_predicate, ann_object);

            // Insert annotation triple
            graph.insert(annotation_triple)?;
        }

        Ok(())
    }

    /// Parse a single Turtle-star line (legacy method)
    #[allow(dead_code)]
    fn parse_turtle_line(
        &self,
        line: &str,
        context: &mut ParseContext,
        graph: &mut StarGraph,
    ) -> Result<()> {
        // Handle directives
        if line.starts_with("@prefix") {
            self.parse_prefix_directive(line, context)?;
            return Ok(());
        }

        if line.starts_with("@base") {
            self.parse_base_directive(line, context)?;
            return Ok(());
        }

        // Parse triple
        if let Some(stripped) = line.strip_suffix('.') {
            let triple_str = stripped.trim();
            let triple = self.parse_triple_pattern(triple_str, context)?;
            graph.insert(triple)?;
        }

        Ok(())
    }

    /// Parse a single TriG-star line
    #[allow(dead_code)]
    fn parse_trig_line(
        &self,
        line: &str,
        context: &mut ParseContext,
        graph: &mut StarGraph,
        current_graph: &mut Option<StarTerm>,
        in_graph_block: &mut bool,
        brace_count: &mut usize,
    ) -> Result<()> {
        // Handle directives (same as Turtle)
        if line.starts_with("@prefix") {
            self.parse_prefix_directive(line, context)?;
            return Ok(());
        }

        if line.starts_with("@base") {
            self.parse_base_directive(line, context)?;
            return Ok(());
        }

        // Handle graph declarations
        if line.contains('{') && !*in_graph_block {
            // Start of named graph block
            let graph_part = line
                .split('{')
                .next()
                .expect("split always returns at least one element")
                .trim();
            if !graph_part.is_empty() {
                let graph_term = self.parse_term(graph_part, context)?;
                *current_graph = Some(graph_term);
            } else {
                // Default graph
                *current_graph = None;
            }
            *in_graph_block = true;
            *brace_count = line.chars().filter(|&c| c == '{').count();
        }

        // Handle closing braces
        if line.contains('}') {
            let close_braces = line.chars().filter(|&c| c == '}').count();
            if close_braces >= *brace_count {
                *in_graph_block = false;
                *current_graph = None;
                *brace_count = 0;
            } else {
                *brace_count -= close_braces;
            }
        }

        // Parse triples within the line (excluding graph declaration part)
        let triple_part = if line.contains('{') {
            line.split('{').nth(1).unwrap_or("")
        } else {
            line
        };

        if triple_part.trim().ends_with('.') && !triple_part.trim().is_empty() {
            let triple_str = triple_part.trim();
            let triple_str = &triple_str[..triple_str.len() - 1].trim();
            if !triple_str.is_empty() {
                let triple = self.parse_triple_pattern(triple_str, context)?;
                graph.insert(triple)?;
            }
        }

        Ok(())
    }

    /// Parse @prefix directive with enhanced error handling
    fn parse_prefix_directive_safe(
        &self,
        line: &str,
        context: &mut ParseContext,
    ) -> StarResult<()> {
        // @prefix prefix: <namespace> .
        let parts: Vec<&str> = line.split_whitespace().collect();
        if parts.len() >= 3 {
            let prefix_name = parts[1].trim_end_matches(':');
            let namespace = parts[2].trim_matches(['<', '>', '.']);

            // Validate prefix format
            if prefix_name.is_empty() {
                let error_msg = "Empty prefix name in @prefix directive".to_string();
                context.add_error(error_msg.clone(), line.to_string(), ErrorSeverity::Error);
                if context.strict_mode {
                    return Err(StarError::parse_error(error_msg));
                }
            } else if namespace.is_empty() {
                let error_msg = "Empty namespace in @prefix directive".to_string();
                context.add_error(error_msg.clone(), line.to_string(), ErrorSeverity::Error);
                if context.strict_mode {
                    return Err(StarError::parse_error(error_msg));
                }
            } else {
                context
                    .prefixes
                    .insert(prefix_name.to_string(), namespace.to_string());
            }
        } else {
            let error_msg = format!("Malformed @prefix directive: {line}");
            context.add_error(error_msg.clone(), line.to_string(), ErrorSeverity::Error);
            if context.strict_mode {
                return Err(StarError::parse_error(error_msg));
            }
        }
        Ok(())
    }

    /// Parse @base directive with enhanced error handling
    fn parse_base_directive_safe(&self, line: &str, context: &mut ParseContext) -> StarResult<()> {
        // @base <iri> .
        let parts: Vec<&str> = line.split_whitespace().collect();
        if parts.len() >= 2 {
            let base_iri = parts[1].trim_matches(['<', '>', '.']);
            if base_iri.is_empty() {
                let error_msg = "Empty base IRI in @base directive".to_string();
                context.add_error(error_msg.clone(), line.to_string(), ErrorSeverity::Error);
                if context.strict_mode {
                    return Err(StarError::parse_error(error_msg));
                }
            } else {
                context.base_iri = Some(base_iri.to_string());
            }
        } else {
            let error_msg = format!("Malformed @base directive: {line}");
            context.add_error(error_msg.clone(), line.to_string(), ErrorSeverity::Error);
            if context.strict_mode {
                return Err(StarError::parse_error(error_msg));
            }
        }
        Ok(())
    }

    /// Parse triple pattern with enhanced error handling
    fn parse_triple_pattern_safe(
        &self,
        pattern: &str,
        context: &mut ParseContext,
    ) -> StarResult<StarTriple> {
        self.parse_triple_pattern_safe_with_depth(pattern, context, 0)
    }

    /// Parse term with enhanced error handling
    fn parse_term_safe(&self, term_str: &str, context: &mut ParseContext) -> StarResult<StarTerm> {
        self.parse_term_safe_with_depth(term_str, context, 0)
    }

    /// Parse term with depth tracking for quoted triples
    fn parse_term_safe_with_depth(
        &self,
        term_str: &str,
        context: &mut ParseContext,
        depth: usize,
    ) -> StarResult<StarTerm> {
        const MAX_QUOTED_TRIPLE_DEPTH: usize = 100;

        let term_str = term_str.trim();

        // Check for empty input
        if term_str.is_empty() {
            let error_msg = "Empty term in triple".to_string();
            context.add_error(
                error_msg.clone(),
                term_str.to_string(),
                ErrorSeverity::Error,
            );
            return Err(StarError::parse_error(error_msg));
        }

        // Quoted triple: << ... >>
        if term_str.starts_with("<<") && term_str.ends_with(">>") {
            // Check depth limit to prevent stack overflow
            if depth >= MAX_QUOTED_TRIPLE_DEPTH {
                let error_msg = format!(
                    "Maximum quoted triple nesting depth ({MAX_QUOTED_TRIPLE_DEPTH}) exceeded"
                );
                context.add_error(
                    error_msg.clone(),
                    term_str.to_string(),
                    ErrorSeverity::Error,
                );
                return Err(StarError::parse_error(error_msg));
            }

            let inner = &term_str[2..term_str.len() - 2].trim();

            // Check for empty quoted triple
            if inner.is_empty() {
                let error_msg = "Empty quoted triple content".to_string();
                context.add_error(
                    error_msg.clone(),
                    term_str.to_string(),
                    ErrorSeverity::Error,
                );
                if context.strict_mode {
                    return Err(StarError::parse_error(error_msg));
                }
                // In non-strict mode, create a placeholder error term
                return self
                    .parse_term("<urn:error:empty-quoted-triple>", context)
                    .map_err(|e| StarError::parse_error(e.to_string()));
            }

            // Validate proper quoted triple delimiters
            if !self.validate_quoted_triple_delimiters(term_str) {
                let error_msg = "Malformed quoted triple delimiters".to_string();
                context.add_error(
                    error_msg.clone(),
                    term_str.to_string(),
                    ErrorSeverity::Error,
                );
                if context.strict_mode {
                    return Err(StarError::parse_error(error_msg));
                }
            }

            match self.parse_triple_pattern_safe_with_depth(inner, context, depth + 1) {
                Ok(inner_triple) => return Ok(StarTerm::quoted_triple(inner_triple)),
                Err(e) => {
                    let error_msg = format!("Failed to parse quoted triple content: {e}");
                    context.add_error(
                        error_msg.clone(),
                        term_str.to_string(),
                        ErrorSeverity::Error,
                    );
                    if context.strict_mode {
                        return Err(StarError::parse_error(error_msg));
                    }
                    // In non-strict mode, try to parse as regular term
                    // but with better error context
                    return self.parse_term_fallback(term_str, context, &error_msg);
                }
            }
        }

        // Continue with other term types...
        self.parse_term(term_str, context)
            .map_err(|e| StarError::parse_error(e.to_string()))
    }

    /// Validate quoted triple delimiter structure
    fn validate_quoted_triple_delimiters(&self, term_str: &str) -> bool {
        let mut depth = 0;
        let mut chars = term_str.chars().peekable();

        while let Some(ch) = chars.next() {
            match ch {
                '<' if chars.peek() == Some(&'<') => {
                    chars.next(); // consume second '<'
                    depth += 1;
                }
                '>' if chars.peek() == Some(&'>') => {
                    chars.next(); // consume second '>'
                    depth -= 1;
                    if depth < 0 {
                        return false; // More closing than opening
                    }
                }
                _ => {}
            }
        }

        depth == 0 // Should be balanced
    }

    /// Parse triple pattern with depth tracking
    fn parse_triple_pattern_safe_with_depth(
        &self,
        pattern: &str,
        context: &mut ParseContext,
        depth: usize,
    ) -> StarResult<StarTriple> {
        match self.tokenize_triple(pattern) {
            Ok(terms) => {
                if terms.len() != 3 {
                    let error_msg = format!(
                        "Triple must have exactly 3 terms, found {} (depth: {})",
                        terms.len(),
                        depth
                    );
                    context.add_error(error_msg.clone(), pattern.to_string(), ErrorSeverity::Error);
                    return Err(StarError::parse_error(error_msg));
                }

                let subject = self.parse_term_safe_with_depth(&terms[0], context, depth)?;
                let predicate = self.parse_term_safe_with_depth(&terms[1], context, depth)?;
                let object = self.parse_term_safe_with_depth(&terms[2], context, depth)?;

                let triple = StarTriple::new(subject, predicate, object);

                // Validate the constructed triple
                if let Err(validation_error) = triple.validate() {
                    let error_msg = format!("Invalid triple at depth {depth}: {validation_error}");
                    context.add_error(error_msg.clone(), pattern.to_string(), ErrorSeverity::Error);
                    return Err(StarError::parse_error(error_msg));
                }

                Ok(triple)
            }
            Err(e) => {
                let error_msg = format!("Failed to tokenize triple at depth {depth}: {e}");
                context.add_error(error_msg.clone(), pattern.to_string(), ErrorSeverity::Error);
                Err(StarError::parse_error(error_msg))
            }
        }
    }

    /// Fallback parsing with better error context
    fn parse_term_fallback(
        &self,
        term_str: &str,
        context: &mut ParseContext,
        original_error: &str,
    ) -> StarResult<StarTerm> {
        match self.parse_term(term_str, context) {
            Ok(term) => {
                // Log a warning that we fell back to regular parsing
                context.add_error(
                    format!(
                        "Quoted triple parsing failed, parsed as regular term: {original_error}"
                    ),
                    term_str.to_string(),
                    ErrorSeverity::Warning,
                );
                Ok(term)
            }
            Err(fallback_error) => {
                let combined_error = format!("Both quoted triple and regular term parsing failed. Quoted triple error: {original_error}. Regular term error: {fallback_error}");
                context.add_error(
                    combined_error.clone(),
                    term_str.to_string(),
                    ErrorSeverity::Error,
                );
                Err(StarError::parse_error(combined_error))
            }
        }
    }

    /// Parse @prefix directive (legacy)
    fn parse_prefix_directive(&self, line: &str, context: &mut ParseContext) -> Result<()> {
        // @prefix prefix: <namespace> .
        let parts: Vec<&str> = line.split_whitespace().collect();
        if parts.len() >= 3 {
            let prefix_name = parts[1].trim_end_matches(':');
            let namespace = parts[2].trim_matches(['<', '>', '.']);
            context
                .prefixes
                .insert(prefix_name.to_string(), namespace.to_string());
        }
        Ok(())
    }

    /// Parse @base directive (legacy)
    fn parse_base_directive(&self, line: &str, context: &mut ParseContext) -> Result<()> {
        // @base <iri> .
        let parts: Vec<&str> = line.split_whitespace().collect();
        if parts.len() >= 2 {
            let base_iri = parts[1].trim_matches(['<', '>', '.']);
            context.base_iri = Some(base_iri.to_string());
        }
        Ok(())
    }

    /// Parse N-Triples-star format
    pub fn parse_ntriples_star<R: Read>(&self, reader: R) -> StarResult<StarGraph> {
        let span = span!(Level::DEBUG, "parse_ntriples_star");
        let _enter = span.enter();

        let mut graph = StarGraph::new();
        let mut context = ParseContext::new();
        let buf_reader = BufReader::new(reader);

        for (line_num, line_result) in buf_reader.lines().enumerate() {
            let line = line_result.map_err(|e| StarError::parse_error(e.to_string()))?;
            let line = line.trim();

            // Skip empty lines and comments
            if line.is_empty() || line.starts_with('#') {
                continue;
            }

            if let Some(stripped) = line.strip_suffix('.') {
                let triple_str = &stripped.trim();
                let triple = self
                    .parse_triple_pattern(triple_str, &mut context)
                    .with_context(|| format!("Error parsing line {}: {}", line_num + 1, line))
                    .map_err(|e| StarError::parse_error(e.to_string()))?;
                graph.insert(triple)?;
            } else {
                // In N-Triples-star, all non-empty lines must be valid triples ending with '.'
                return Err(StarError::parse_error(format!(
                    "Invalid N-Triples-star line {}: {}",
                    line_num + 1,
                    line
                )));
            }
        }

        debug!("Parsed {} triples in N-Triples-star format", graph.len());
        Ok(graph)
    }

    /// Parse TriG-star format (with named graphs) - Enhanced version
    pub fn parse_trig_star<R: Read>(&self, reader: R) -> StarResult<StarGraph> {
        let span = span!(Level::DEBUG, "parse_trig_star");
        let _enter = span.enter();

        let mut graph = StarGraph::new();
        let mut context = self.create_parse_context();
        let buf_reader = BufReader::new(reader);

        // Enhanced state tracking for TriG parsing
        let mut trig_state = TrigParserState::new();
        let mut accumulated_line = String::new();
        let mut error_count = 0;
        let max_errors = self.config.max_parse_errors.unwrap_or(100);

        for (line_num, line_result) in buf_reader.lines().enumerate() {
            let line = line_result.map_err(|e| StarError::parse_error(e.to_string()))?;
            let line = line.trim();

            // Update position tracking
            context.update_position(line_num + 1, 0);

            // Skip empty lines and comments
            if line.is_empty() || line.starts_with('#') {
                continue;
            }

            // Handle multi-line statements
            accumulated_line.push_str(line);
            accumulated_line.push(' ');

            // Check if we have a complete statement
            if tokenizer::is_complete_trig_statement(&accumulated_line, &mut trig_state) {
                match self.parse_complete_trig_statement(
                    accumulated_line.trim(),
                    &mut context,
                    &mut graph,
                    &mut trig_state,
                ) {
                    Ok(_) => {
                        // Successfully parsed
                        accumulated_line.clear();
                    }
                    Err(e) => {
                        error_count += 1;
                        let error_context =
                            format!("line {}: {}", line_num + 1, accumulated_line.trim());
                        context.add_error(
                            format!("TriG-star parse error: {e}"),
                            error_context,
                            if context.strict_mode {
                                ErrorSeverity::Fatal
                            } else {
                                ErrorSeverity::Error
                            },
                        );

                        if context.strict_mode || error_count >= max_errors {
                            return Err(StarError::parse_error(format!(
                                "TriG-star parsing failed at line {} with {} errors. First error: {}",
                                line_num + 1,
                                context.get_errors().len(),
                                context.get_errors().first().map(|e| &e.message).unwrap_or(&"Unknown error".to_string())
                            )));
                        }

                        // Try to recover by resetting the accumulated line and state
                        accumulated_line.clear();

                        // If we're in a nested structure, try to recover the state
                        if trig_state.brace_depth > 0 {
                            debug!("Attempting to recover from error in nested graph block");
                            // Don't reset state completely, just try to continue
                        } else {
                            // Reset to a clean state for top-level errors
                            trig_state = TrigParserState::new();
                        }
                    }
                }
            }
        }

        // Handle any remaining incomplete statement
        if !accumulated_line.trim().is_empty() {
            context.add_error(
                "Incomplete TriG-star statement at end of input".to_string(),
                accumulated_line.trim().to_string(),
                ErrorSeverity::Error,
            );
            if context.strict_mode {
                return Err(StarError::parse_error(format!(
                    "Incomplete TriG-star statement at end of input: {}",
                    accumulated_line.trim()
                )));
            }
        }

        // Check for unclosed graph blocks
        if trig_state.in_graph_block {
            context.add_error(
                format!(
                    "Unclosed graph block at end of input. Missing {} closing brace(s)",
                    trig_state.brace_depth
                ),
                "End of file".to_string(),
                ErrorSeverity::Error,
            );
            if context.strict_mode {
                return Err(StarError::parse_error(
                    "Unclosed graph block at end of input".to_string(),
                ));
            }
        }

        // Report any accumulated errors
        if !context.get_errors().is_empty() {
            debug!(
                "TriG-star parsing completed with {} warnings/errors",
                context.get_errors().len()
            );
            for error in context.get_errors() {
                debug!(
                    "Line {}, Column {}: {} [{}] ({})",
                    error.line, error.column, error.message, error.severity, error.context
                );
            }
        }

        debug!(
            "Parsed {} quads ({} total triples) in TriG-star format with {} errors",
            graph.quad_len(),
            graph.total_len(),
            context.get_errors().len()
        );
        Ok(graph)
    }

    /// Parse N-Quads-star format with enhanced error handling and streaming support
    pub fn parse_nquads_star<R: Read>(&self, reader: R) -> StarResult<StarGraph> {
        let span = span!(Level::DEBUG, "parse_nquads_star");
        let _enter = span.enter();

        let mut graph = StarGraph::new();
        let mut context = self.create_parse_context();
        let buf_reader = BufReader::new(reader);

        for (line_num, line_result) in buf_reader.lines().enumerate() {
            let line = line_result.map_err(|e| StarError::parse_error(e.to_string()))?;
            let line = line.trim();

            // Update position tracking
            context.update_position(line_num + 1, 0);

            // Skip empty lines and comments
            if line.is_empty() || line.starts_with('#') {
                continue;
            }

            if let Some(stripped) = line.strip_suffix('.') {
                let quad_str = &stripped.trim();
                match self.parse_quad_pattern_safe(quad_str, &mut context) {
                    Ok(quad) => {
                        graph.insert_quad(quad)?;
                    }
                    Err(e) => {
                        let error_context = format!("line {}: {}", line_num + 1, line);
                        context.add_error(
                            format!("Parse error: {e}"),
                            error_context,
                            if context.strict_mode {
                                ErrorSeverity::Fatal
                            } else {
                                ErrorSeverity::Error
                            },
                        );

                        if context.strict_mode || context.has_fatal_errors() {
                            return Err(StarError::parse_error(format!(
                                "N-Quads-star parsing failed at line {} with {} errors. First error: {}",
                                line_num + 1,
                                context.get_errors().len(),
                                context.get_errors().first().map(|e| &e.message).unwrap_or(&"Unknown error".to_string())
                            )));
                        }

                        // Try error recovery
                        if !context.try_recover_from_error(&format!("line {}", line_num + 1)) {
                            debug!(
                                "Error recovery failed for N-Quads-star line {}",
                                line_num + 1
                            );
                        }
                    }
                }
            } else {
                // In N-Quads-star, all non-empty lines must be valid quads ending with '.'
                let error_msg = format!(
                    "Invalid N-Quads-star line {}: missing terminating '.'",
                    line_num + 1
                );
                context.add_error(error_msg.clone(), line.to_string(), ErrorSeverity::Error);

                if context.strict_mode {
                    return Err(StarError::parse_error(error_msg));
                }
            }
        }

        // Report any accumulated errors
        if !context.get_errors().is_empty() {
            debug!(
                "N-Quads-star parsing completed with {} warnings/errors",
                context.get_errors().len()
            );
            for error in context.get_errors() {
                debug!(
                    "Line {}, Column {}: {} ({})",
                    error.line, error.column, error.message, error.context
                );
            }
        }

        debug!(
            "Parsed {} quads ({} total triples) in N-Quads-star format",
            graph.quad_len(),
            graph.total_len()
        );
        Ok(graph)
    }

    /// Parse a quad pattern with enhanced error handling
    fn parse_quad_pattern_safe(
        &self,
        pattern: &str,
        context: &mut ParseContext,
    ) -> StarResult<StarQuad> {
        match self.tokenize_quad(pattern) {
            Ok(terms) => {
                if terms.len() < 3 || terms.len() > 4 {
                    let error_msg = format!("Quad must have 3 or 4 terms, found {}", terms.len());
                    context.add_error(error_msg.clone(), pattern.to_string(), ErrorSeverity::Error);
                    return Err(StarError::parse_error(error_msg));
                }

                let subject = self.parse_term_safe(&terms[0], context)?;
                let predicate = self.parse_term_safe(&terms[1], context)?;
                let object = self.parse_term_safe(&terms[2], context)?;

                // Graph is optional in N-Quads (default graph if omitted)
                let graph = if terms.len() == 4 {
                    Some(self.parse_term_safe(&terms[3], context)?)
                } else {
                    None
                };

                let quad = StarQuad::new(subject, predicate, object, graph);

                // Validate the constructed quad
                if let Err(validation_error) = quad.validate() {
                    let error_msg = format!("Invalid quad: {validation_error}");
                    context.add_error(error_msg.clone(), pattern.to_string(), ErrorSeverity::Error);
                    return Err(StarError::parse_error(error_msg));
                }

                Ok(quad)
            }
            Err(e) => {
                let error_msg = format!("Failed to tokenize quad: {e}");
                context.add_error(error_msg.clone(), pattern.to_string(), ErrorSeverity::Error);
                Err(StarError::parse_error(error_msg))
            }
        }
    }

    /// Parse a quad pattern (subject predicate object graph)
    #[allow(dead_code)]
    fn parse_quad_pattern(&self, pattern: &str, context: &mut ParseContext) -> Result<StarQuad> {
        let terms = self.tokenize_quad(pattern)?;

        if terms.len() < 3 || terms.len() > 4 {
            return Err(anyhow::anyhow!(
                "Quad must have 3 or 4 terms, found {}",
                terms.len()
            ));
        }

        let subject = self.parse_term(&terms[0], context)?;
        let predicate = self.parse_term(&terms[1], context)?;
        let object = self.parse_term(&terms[2], context)?;

        // Graph is optional in N-Quads (default graph if omitted)
        let graph = if terms.len() == 4 {
            Some(self.parse_term(&terms[3], context)?)
        } else {
            None
        };

        Ok(StarQuad {
            subject,
            predicate,
            object,
            graph,
        })
    }

    /// Parse a triple pattern (subject predicate object)
    fn parse_triple_pattern(
        &self,
        pattern: &str,
        context: &mut ParseContext,
    ) -> Result<StarTriple> {
        let terms = self.tokenize_triple(pattern)?;

        if terms.len() != 3 {
            return Err(anyhow::anyhow!(
                "Triple must have exactly 3 terms, found {}",
                terms.len()
            ));
        }

        let subject = self.parse_term(&terms[0], context)?;
        let predicate = self.parse_term(&terms[1], context)?;
        let object = self.parse_term(&terms[2], context)?;

        let triple = StarTriple::new(subject, predicate, object);
        triple
            .validate()
            .map_err(|e| anyhow::anyhow!("Invalid triple: {}", e))?;

        Ok(triple)
    }

    /// Tokenize a triple into its three components, handling quoted triples
    fn tokenize_triple(&self, pattern: &str) -> Result<Vec<String>> {
        tokenizer::tokenize_triple(pattern)
    }

    /// Tokenize a quad pattern (similar to triple but allows 4 terms)
    fn tokenize_quad(&self, pattern: &str) -> Result<Vec<String>> {
        tokenizer::tokenize_quad(pattern)
    }

    /// Parse a single term (IRI, blank node, literal, or quoted triple)
    fn parse_term(&self, term_str: &str, context: &mut ParseContext) -> Result<StarTerm> {
        let term_str = term_str.trim();

        // Quoted triple: << ... >>
        if term_str.starts_with("<<") && term_str.ends_with(">>") {
            let inner = &term_str[2..term_str.len() - 2];
            let inner_triple = self.parse_triple_pattern(inner, context)?;
            return Ok(StarTerm::quoted_triple(inner_triple));
        }

        // IRI: <...> or prefixed name
        if term_str.starts_with('<') && term_str.ends_with('>') {
            let iri = &term_str[1..term_str.len() - 1];
            let resolved = context.resolve_relative(iri);
            return StarTerm::iri(&resolved).map_err(|e| anyhow::anyhow!("Invalid IRI: {}", e));
        }

        // Prefixed name
        if term_str.contains(':') && !term_str.starts_with('_') && !term_str.starts_with('"') {
            let resolved = context.resolve_prefix(term_str)?;
            return StarTerm::iri(&resolved).map_err(|e| anyhow::anyhow!("Invalid IRI: {}", e));
        }

        // Blank node: _:id
        if let Some(id) = term_str.strip_prefix("_:") {
            return StarTerm::blank_node(id)
                .map_err(|e| anyhow::anyhow!("Invalid blank node: {}", e));
        }

        // Literal: "value"@lang or "value"^^<datatype>
        if term_str.starts_with('"') {
            return self.parse_literal(term_str, context);
        }

        // Variable: ?name (for SPARQL-star)
        if let Some(name) = term_str.strip_prefix('?') {
            return StarTerm::variable(name)
                .map_err(|e| anyhow::anyhow!("Invalid variable: {}", e));
        }

        Err(anyhow::anyhow!("Unrecognized term format: {}", term_str))
    }

    /// Parse a literal term with optional language tag or datatype
    fn parse_literal(&self, literal_str: &str, context: &mut ParseContext) -> Result<StarTerm> {
        let mut chars = literal_str.chars().peekable();
        let mut value = String::new();
        let mut escape_next = false;

        // Skip opening quote
        if chars.next() != Some('"') {
            return Err(anyhow::anyhow!("Literal must start with quote"));
        }
        let mut in_string = true;

        // Parse value
        for ch in chars.by_ref() {
            if escape_next {
                value.push(ch);
                escape_next = false;
                continue;
            }

            match ch {
                '\\' => {
                    escape_next = true;
                }
                '"' => {
                    in_string = false;
                    break;
                }
                _ => {
                    value.push(ch);
                }
            }
        }

        if in_string {
            return Err(anyhow::anyhow!("Unterminated string literal"));
        }

        // Check for language tag or datatype
        let remaining: String = chars.collect();

        if let Some(lang) = remaining.strip_prefix('@') {
            Ok(StarTerm::literal_with_language(&value, lang)
                .map_err(|e| anyhow::anyhow!("Invalid literal: {}", e))?)
        } else if let Some(datatype_str) = remaining.strip_prefix("^^") {
            let datatype = if let Some(stripped) = datatype_str
                .strip_prefix('<')
                .and_then(|s| s.strip_suffix('>'))
            {
                stripped.to_string()
            } else {
                context.resolve_prefix(datatype_str)?
            };
            Ok(StarTerm::literal_with_datatype(&value, &datatype)
                .map_err(|e| anyhow::anyhow!("Invalid literal: {}", e))?)
        } else {
            Ok(StarTerm::literal(&value).map_err(|e| anyhow::anyhow!("Invalid literal: {}", e))?)
        }
    }

    /// Parse a complete TriG statement (enhanced version with proper named graph support)
    fn parse_complete_trig_statement(
        &self,
        statement: &str,
        context: &mut ParseContext,
        graph: &mut StarGraph,
        state: &mut TrigParserState,
    ) -> Result<()> {
        let statement = statement.trim();

        // Handle directives
        if statement.starts_with("@prefix") {
            self.parse_prefix_directive(statement, context)?;
            return Ok(());
        }

        if statement.starts_with("@base") {
            self.parse_base_directive(statement, context)?;
            return Ok(());
        }

        // Handle graph blocks
        if statement.contains('{') {
            return self.parse_graph_block(statement, context, graph, state);
        }

        // Handle graph block closing
        if statement == "}" {
            state.exit_graph_block();
            return Ok(());
        }

        // Handle regular triples
        if let Some(stripped) = statement.strip_suffix('.') {
            let triple_str = &stripped.trim();
            if !triple_str.is_empty() {
                let triple = self.parse_triple_pattern(triple_str, context)?;

                // Create quad with current graph context
                let quad = StarQuad::new(
                    triple.subject,
                    triple.predicate,
                    triple.object,
                    state.current_graph.clone(),
                );

                graph.insert_quad(quad)?;
            }
        }

        Ok(())
    }

    /// Parse a graph block declaration (enhanced version)
    /// Parse graph name with error recovery
    fn parse_graph_name_with_recovery(
        &self,
        graph_name: &str,
        context: &mut ParseContext,
    ) -> StarResult<StarTerm> {
        let graph_name = graph_name.trim();

        // Try different graph name formats with recovery

        // Check for quoted graph names (common mistake)
        if graph_name.starts_with('"') && graph_name.ends_with('"') && graph_name.len() >= 2 {
            let inner = &graph_name[1..graph_name.len() - 1];
            context.add_error(
                format!("Graph names should not be quoted strings. Converting '{inner}' to IRI"),
                graph_name.to_string(),
                ErrorSeverity::Warning,
            );
            // Try to convert to IRI
            if inner.contains(':') || inner.starts_with("http") {
                return StarTerm::iri(inner);
            }
        }

        // Parse normally
        self.parse_term_safe(graph_name, context)
    }

    fn parse_graph_block(
        &self,
        statement: &str,
        context: &mut ParseContext,
        graph: &mut StarGraph,
        state: &mut TrigParserState,
    ) -> Result<()> {
        // Find the opening brace
        if let Some(brace_pos) = statement.find('{') {
            let graph_part = statement[..brace_pos].trim();
            let content_part = &statement[brace_pos + 1..];

            // Parse graph name if present
            let graph_term = if graph_part.is_empty() {
                None // Default graph
            } else {
                // Enhanced graph name parsing with better error handling
                match self.parse_graph_name_with_recovery(graph_part, context) {
                    Ok(term) => {
                        // Validate that the graph name is appropriate (IRI or blank node)
                        match &term {
                            StarTerm::NamedNode(_) | StarTerm::BlankNode(_) => Some(term),
                            _ => {
                                let error_msg = format!(
                                    "Graph name must be an IRI or blank node, found: {term:?}"
                                );
                                context.add_error(
                                    error_msg.clone(),
                                    statement.to_string(),
                                    ErrorSeverity::Error,
                                );
                                if context.strict_mode {
                                    return Err(anyhow::anyhow!(error_msg));
                                }
                                None // Continue with default graph in non-strict mode
                            }
                        }
                    }
                    Err(e) => {
                        return Err(anyhow::anyhow!(
                            "Failed to parse graph name '{}': {}",
                            graph_part,
                            e
                        ));
                    }
                }
            };

            // Check for nested graph declarations
            if state.in_graph_block {
                debug!(
                    "Warning: Nested graph declaration detected. Previous graph: {:?}, New graph: {:?}",
                    state.current_graph, graph_term
                );
                // Store the previous graph context for potential recovery
            }

            state.enter_graph_block(graph_term);

            // Parse any triples in the same line
            let remaining_content = content_part.trim();
            if !remaining_content.is_empty() && !remaining_content.starts_with('}') {
                // Handle triples on the same line as graph declaration
                for triple_candidate in remaining_content.split('.') {
                    let triple_str = triple_candidate.trim();
                    if !triple_str.is_empty() && !triple_str.starts_with('}') {
                        match self.parse_triple_pattern_safe(triple_str, context) {
                            Ok(triple) => {
                                let quad = StarQuad::new(
                                    triple.subject,
                                    triple.predicate,
                                    triple.object,
                                    state.current_graph.clone(),
                                );
                                graph.insert_quad(quad)?;
                            }
                            Err(e) => {
                                // Log error but continue parsing
                                debug!(
                                    "Error parsing triple in graph block: '{}' - Error: {}",
                                    triple_str, e
                                );
                                context.add_error(
                                    format!("Failed to parse triple in graph block: {e}"),
                                    triple_str.to_string(),
                                    ErrorSeverity::Warning,
                                );
                            }
                        }
                    }
                }
            }

            // Handle closing brace on same line
            if remaining_content.contains('}') {
                state.exit_graph_block();
            }
        }

        Ok(())
    }

    /// Get parsing errors (placeholder implementation)
    /// Parse JSON-LD-star format with RDF-star extension
    pub fn parse_jsonld_star<R: Read>(&self, reader: R) -> StarResult<StarGraph> {
        let span = span!(Level::DEBUG, "parse_jsonld_star");
        let _enter = span.enter();

        let mut graph = StarGraph::new();
        let mut context = self.create_parse_context();

        // Read the entire JSON content
        let mut content = String::new();
        let mut buf_reader = BufReader::new(reader);
        buf_reader.read_to_string(&mut content).map_err(|e| {
            StarError::parse_error(format!("Failed to read JSON-LD-star content: {e}"))
        })?;

        // Parse JSON
        let json_value: serde_json::Value = serde_json::from_str(&content)
            .map_err(|e| StarError::parse_error(format!("Invalid JSON: {e}")))?;

        // Process JSON-LD-star document
        self.process_jsonld_star_value(&json_value, &mut graph, &mut context)?;

        debug!(
            "Parsed JSON-LD-star: {} quads, {} errors",
            graph.quad_len(),
            context.parsing_errors.len()
        );

        if !context.parsing_errors.is_empty() && context.strict_mode {
            return Err(StarError::parse_error(format!(
                "JSON-LD-star parsing failed with {} errors",
                context.parsing_errors.len()
            )));
        }

        Ok(graph)
    }

    /// Process a JSON-LD-star value recursively
    fn process_jsonld_star_value(
        &self,
        value: &serde_json::Value,
        graph: &mut StarGraph,
        context: &mut ParseContext,
    ) -> StarResult<()> {
        jsonld::process_jsonld_star_value(value, graph, context)
    }
}

impl Default for StarParser {
    fn default() -> Self {
        Self::new()
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_simple_triple_parsing() {
        let parser = StarParser::new();
        let data = r#"
            <http://example.org/alice> <http://example.org/knows> <http://example.org/bob> .
        "#;

        let graph = parser.parse_str(data, StarFormat::NTriplesStar).unwrap();
        assert_eq!(graph.len(), 1);

        let triples = graph.triples();
        let triple = &triples[0];
        assert!(triple.subject.is_named_node());
        assert!(triple.predicate.is_named_node());
        assert!(triple.object.is_named_node());
    }

    #[test]
    fn test_quoted_triple_parsing() {
        let parser = StarParser::new();
        let data = r#"
            << <http://example.org/alice> <http://example.org/age> "25" >> <http://example.org/certainty> "0.9" .
        "#;

        let graph = parser.parse_str(data, StarFormat::NTriplesStar).unwrap();
        assert_eq!(graph.len(), 1);

        let triples = graph.triples();
        let triple = &triples[0];
        assert!(triple.subject.is_quoted_triple());
        assert!(triple.predicate.is_named_node());
        assert!(triple.object.is_literal());
    }

    #[test]
    fn test_turtle_star_with_prefixes() {
        let parser = StarParser::new();
        let data = r#"
            @prefix ex: <http://example.org/> .
            @prefix foaf: <http://xmlns.com/foaf/0.1/> .

            ex:alice foaf:knows ex:bob .
            << ex:alice foaf:age "25" >> ex:certainty "high" .
        "#;

        let graph = parser.parse_str(data, StarFormat::TurtleStar).unwrap();
        assert_eq!(graph.len(), 2);
    }

    #[test]
    fn test_literal_parsing() {
        let parser = StarParser::new();
        let mut context = ParseContext::new();

        // Simple literal
        let term = parser.parse_term(r#""hello""#, &mut context).unwrap();
        assert!(term.is_literal());

        // Literal with language tag
        let term = parser.parse_term(r#""hello"@en"#, &mut context).unwrap();
        assert!(term.is_literal());
        if let Some(literal) = term.as_literal() {
            assert_eq!(literal.language, Some("en".to_string()));
        }

        // Literal with datatype
        let term = parser
            .parse_term(
                r#""25"^^<http://www.w3.org/2001/XMLSchema#integer>"#,
                &mut context,
            )
            .unwrap();
        assert!(term.is_literal());
    }

    #[test]
    fn test_tokenization() {
        let parser = StarParser::new();

        // Simple triple
        let tokens = parser.tokenize_triple(r#"<s> <p> <o>"#).unwrap();
        assert_eq!(tokens, vec!["<s>", "<p>", "<o>"]);

        // Quoted triple as subject
        let tokens = parser
            .tokenize_triple(r#"<< <s> <p> <o> >> <certainty> "high""#)
            .unwrap();
        assert_eq!(
            tokens,
            vec!["<< <s> <p> <o> >>", "<certainty>", r#""high""#]
        );
    }

    #[test]
    fn test_error_handling() {
        let parser = StarParser::new();

        // Invalid format
        let result = parser.parse_str("invalid data", StarFormat::NTriplesStar);
        assert!(result.is_err());

        // Unclosed quoted triple
        let result = parser.parse_str(
            r#"<< <s> <p> <o> <certainty> "high" ."#,
            StarFormat::NTriplesStar,
        );
        assert!(result.is_err());
    }

    #[test]
    fn test_nquads_star_parsing() {
        let parser = StarParser::new();

        // Simple quad
        let nquads = r#"<http://example.org/s> <http://example.org/p> "test" <http://example.org/g> .
<http://example.org/s2> <http://example.org/p2> <http://example.org/o2> ."#;

        let result = parser.parse_str(nquads, StarFormat::NQuadsStar).unwrap();
        assert_eq!(result.quad_len(), 2);
        assert_eq!(result.total_len(), 2);

        // Quad with quoted triple
        let nquads_with_quoted = r#"<< <http://example.org/alice> <http://example.org/says> "hello" >> <http://example.org/certainty> "0.9" <http://example.org/provenance> ."#;

        let result = parser
            .parse_str(nquads_with_quoted, StarFormat::NQuadsStar)
            .unwrap();
        assert_eq!(result.quad_len(), 1);
        assert!(result.count_quoted_triples() > 0);
    }

    #[test]
    fn test_trig_star_parsing() {
        let parser = StarParser::new();

        // Simple TriG with named graphs
        let trig = r#"
@prefix ex: <http://example.org/> .

{
    ex:alice ex:knows ex:bob .
}

ex:graph1 {
    ex:charlie ex:likes ex:dave .
    << ex:charlie ex:likes ex:dave >> ex:certainty "0.8" .
}
"#;

        let result = parser.parse_str(trig, StarFormat::TrigStar).unwrap();
        assert_eq!(result.quad_len(), 3);
        assert_eq!(result.named_graph_names().len(), 1);

        // Test default graph
        let default_triples = result.triples();
        assert_eq!(default_triples.len(), 1);

        // Test named graph
        let graph_name = result.named_graph_names()[0];
        let named_triples = result.named_graph_triples(graph_name).unwrap();
        assert_eq!(named_triples.len(), 2);
    }

    #[test]
    fn test_trig_star_error_recovery() {
        let config = StarConfig {
            strict_mode: false,
            ..Default::default()
        }; // Enable error recovery
        let parser = StarParser::with_config(config);

        // TriG with errors that should be recoverable
        let trig_with_errors = r#"
@prefix ex: <http://example.org/> .

# Valid triple
ex:alice ex:knows ex:bob .

# Invalid triple (missing object) - should be skipped
ex:charlie ex:likes .

# Valid graph block
ex:graph1 {
    ex:dave ex:age "30" .
}

# Unclosed graph block - should report error but continue
ex:graph2 {
    ex:eve ex:age "25" .
    # Missing closing brace

# Valid triple after error
ex:frank ex:knows ex:grace .
"#;

        let result = parser.parse_str(trig_with_errors, StarFormat::TrigStar);
        // Should parse successfully with errors logged
        assert!(result.is_ok());
        let graph = result.unwrap();
        // Should have parsed the valid triples
        assert!(graph.quad_len() >= 3); // At least the valid triples
    }

    #[test]
    fn test_nquads_star_with_blank_nodes() {
        let parser = StarParser::new();

        let nquads = r#"_:b1 <http://example.org/p> "test" <http://example.org/g> .
<< _:b1 <http://example.org/says> "hello" >> <http://example.org/certainty> "0.9" _:g1 ."#;

        let result = parser.parse_str(nquads, StarFormat::NQuadsStar).unwrap();
        assert_eq!(result.quad_len(), 2);
    }
}