zpl_toolchain_core 0.1.7

Core parser, emitter, and validator for ZPL II label code
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
//! Comprehensive tests for the ZPL parser.
//!
//! Covers: basic parsing, command recognition (heuristic & trie), argument
//! parsing, field data mode, span tracking, trivia preservation, diagnostics,
//! edge cases, and sample file parsing.
//!
//! Validator-specific tests live in `validator.rs`.

mod common;

use common::{
    extract_codes, extract_diag_codes, extract_label_codes, find_args, is_severity_error,
    is_severity_info, is_severity_warn,
};
use zpl_toolchain_core::grammar::ast::{Node, Presence};
use zpl_toolchain_core::grammar::diag::Span;
use zpl_toolchain_core::grammar::parser::{parse_str, parse_with_tables};
use zpl_toolchain_diagnostics::{Severity, codes};

fn tables_with_spacing_command(
    code: &str,
    no_space_after_opcode: bool,
) -> zpl_toolchain_spec_tables::ParserTables {
    zpl_toolchain_spec_tables::ParserTables::new(
        "1.0.0".into(),
        "0.3.0".into(),
        vec![zpl_toolchain_spec_tables::CommandEntry {
            codes: vec![code.to_string()],
            arity: 1,
            raw_payload: false,
            field_data: false,
            opens_field: false,
            closes_field: false,
            hex_escape_modifier: false,
            field_number: false,
            serialization: false,
            requires_field: false,
            signature: Some(zpl_toolchain_spec_tables::Signature {
                params: vec!["n".to_string()],
                joiner: ",".to_string(),
                no_space_after_opcode,
                allow_empty_trailing: true,
                split_rule: None,
            }),
            args: Some(vec![zpl_toolchain_spec_tables::ArgUnion::Single(Box::new(
                zpl_toolchain_spec_tables::Arg {
                    name: Some("num".to_string()),
                    key: Some("n".to_string()),
                    r#type: "int".to_string(),
                    unit: None,
                    range: None,
                    min_length: None,
                    max_length: None,
                    optional: false,
                    presence: None,
                    default: None,
                    default_by_dpi: None,
                    default_from: None,
                    profile_constraint: None,
                    range_when: None,
                    rounding_policy: None,
                    rounding_policy_when: None,
                    resource: None,
                    r#enum: None,
                },
            ))]),
            constraints: None,
            effects: None,
            plane: None,
            scope: None,
            placement: None,
            name: None,
            category: None,
            since: None,
            deprecated: None,
            deprecated_since: None,
            stability: None,
            composites: None,
            defaults: None,
            units: None,
            printer_gates: None,
            signature_overrides: None,
            field_data_rules: None,
            examples: None,
        }],
        None,
    )
}

// ─── 1. Basic Parsing ────────────────────────────────────────────────────────

#[test]
fn empty_input_no_labels() {
    let result = parse_str("");
    assert_eq!(
        result.ast.labels.len(),
        0,
        "empty input should produce no labels"
    );
    assert!(
        result
            .diagnostics
            .iter()
            .any(|d| d.id == codes::PARSER_NO_LABELS),
        "should emit no-labels diagnostic"
    );
}

#[test]
fn single_label_xa_xz() {
    let result = parse_str("^XA^XZ");
    assert_eq!(result.ast.labels.len(), 1, "should produce 1 label");
    let codes = extract_codes(&result);
    assert_eq!(codes, vec!["^XA", "^XZ"]);
}

#[test]
fn multiple_labels() {
    let result = parse_str("^XA^XZ^XA^XZ");
    assert_eq!(result.ast.labels.len(), 2, "should produce 2 labels");
    assert_eq!(extract_label_codes(&result, 0), vec!["^XA", "^XZ"]);
    assert_eq!(extract_label_codes(&result, 1), vec!["^XA", "^XZ"]);
}

#[test]
fn nested_xa_flushes_label() {
    // A second ^XA while inside a label should flush the current label
    let result = parse_str("^XA^FO10,10^XA^XZ");
    assert_eq!(
        result.ast.labels.len(),
        2,
        "nested ^XA should flush to 2 labels"
    );
    // First label: ^XA and ^FO (flushed when second ^XA is seen)
    let first_codes = extract_label_codes(&result, 0);
    assert!(
        first_codes.contains(&"^XA".to_string()),
        "first label should have ^XA"
    );
    assert!(
        first_codes.contains(&"^FO".to_string()),
        "first label should have ^FO"
    );
    // Second label: ^XA and ^XZ
    let second_codes = extract_label_codes(&result, 1);
    assert!(
        second_codes.contains(&"^XA".to_string()),
        "second label should have ^XA"
    );
    assert!(
        second_codes.contains(&"^XZ".to_string()),
        "second label should have ^XZ"
    );
}

// ─── 2. Command Recognition — Heuristic (no tables) ─────────────────────────

#[test]
fn heuristic_two_char_code() {
    // ^FO: F(alpha) O(alpha) 1(digit) → heuristic picks 2-char "FO"
    let result = parse_str("^XA^FO10,10^XZ");
    let codes = extract_codes(&result);
    assert!(
        codes.contains(&"^FO".to_string()),
        "heuristic should recognize ^FO as 2-char code"
    );
}

#[test]
fn heuristic_single_char_code() {
    // ^A0N: the heuristic sees A(alpha), 0(digit) → 2-char "A0" (not single char!)
    // The single-char recognition of ^A only works with tables/trie.
    let result = parse_str("^XA^A0N,22,26^XZ");
    let codes = extract_codes(&result);
    // Heuristic: A(alpha) + 0(digit) → 2-char code ^A0
    assert!(
        codes.contains(&"^A0".to_string()),
        "heuristic sees ^A0 as 2-char code (no tables to identify ^A): got {:?}",
        codes,
    );
}

#[test]
fn tilde_command() {
    let result = parse_str("^XA~JA^XZ");
    let codes = extract_codes(&result);
    assert!(
        codes.contains(&"~JA".to_string()),
        "tilde command ~JA should be recognized: {:?}",
        codes
    );
}

// ─── 3. Command Recognition — With Tables (trie) ────────────────────────────

#[test]
fn trie_longest_match() {
    let tables = &*common::TABLES;
    let result = parse_with_tables("^XA^BY3,2,50^BCN,142,N,N,N^XZ", Some(tables));
    let codes = extract_codes(&result);
    assert!(
        codes.contains(&"^BY".to_string()),
        "trie should recognize ^BY: {:?}",
        codes
    );
    assert!(
        codes.contains(&"^BC".to_string()),
        "trie should recognize ^BC separately: {:?}",
        codes
    );
}

#[test]
fn unknown_command_warning() {
    let tables = &*common::TABLES;
    // ^QQ is unlikely to be a valid ZPL command
    let result = parse_with_tables("^XA^QQ99^XZ", Some(tables));
    let has_warning = result
        .diagnostics
        .iter()
        .any(|d| d.id == codes::PARSER_UNKNOWN_COMMAND && is_severity_warn(&d.severity));
    assert!(
        has_warning,
        "unknown command should produce a warning: {:?}",
        extract_diag_codes(&result)
    );
}

// ─── 4. Argument Parsing ────────────────────────────────────────────────────

#[test]
fn comma_separated_args() {
    let result = parse_str("^XA^FO100,200^XZ");
    let args = find_args(&result, "^FO");
    assert_eq!(args.len(), 2, "^FO should have 2 args");
    assert_eq!(args[0].value.as_deref(), Some("100"));
    assert_eq!(args[1].value.as_deref(), Some("200"));
    assert!(matches!(args[0].presence, Presence::Value));
    assert!(matches!(args[1].presence, Presence::Value));
}

#[test]
fn empty_trailing_args() {
    // ^BC,,,,, → 6 comma-separated empty segments
    let result = parse_str("^XA^BC,,,,,^XZ");
    // With heuristic, code is "^BC" (B,C both alpha, next is comma → 2-char)
    let args = find_args(&result, "^BC");
    assert_eq!(
        args.len(),
        6,
        "^BC,,,,, should produce exactly 6 empty args, got {}",
        args.len()
    );
    for (i, arg) in args.iter().enumerate() {
        assert!(
            matches!(arg.presence, Presence::Empty),
            "arg {} should be Empty, got {:?}",
            i,
            arg.presence,
        );
    }
}

#[test]
fn mixed_present_empty_args() {
    let tables = &*common::TABLES;
    let result = parse_with_tables("^XA^BCN,142,,N,^XZ", Some(tables));
    let args = find_args(&result, "^BC");
    // ^BC has arity 6 with allowEmptyTrailing=true, so the parser exposes all
    // 6 arg slots including the trailing empty one.
    assert_eq!(
        args.len(),
        6,
        "^BC should have exactly 6 args, got {}: {:?}",
        args.len(),
        args
    );
    // First arg should be Value("N")
    assert!(
        matches!(args[0].presence, Presence::Value),
        "arg 0 should be Value"
    );
    assert_eq!(args[0].value.as_deref(), Some("N"));
    // Second should be Value("142")
    assert!(
        matches!(args[1].presence, Presence::Value),
        "arg 1 should be Value"
    );
    assert_eq!(args[1].value.as_deref(), Some("142"));
    // Third should be Empty
    assert!(
        matches!(args[2].presence, Presence::Empty),
        "arg 2 should be Empty"
    );
    // Fourth should be Value("N")
    assert!(
        matches!(args[3].presence, Presence::Value),
        "arg 3 should be Value"
    );
    assert_eq!(args[3].value.as_deref(), Some("N"));
    // Fifth should be Empty
    assert!(
        matches!(args[4].presence, Presence::Empty),
        "arg 4 should be Empty"
    );
    // Sixth should be Empty (trailing empty arg with allowEmptyTrailing)
    assert!(
        matches!(args[5].presence, Presence::Empty),
        "arg 5 should be Empty"
    );
}

#[test]
fn signature_no_space_after_opcode_rejects_space() {
    let tables = tables_with_spacing_command("^ZZN", true);
    let result = parse_with_tables("^XA^ZZN 5^XZ", Some(&tables));
    assert!(
        result
            .diagnostics
            .iter()
            .any(|d| d.id == codes::PARSER_INVALID_COMMAND
                && d.message.contains("should not include a space")),
        "expected parser spacing diagnostic when noSpaceAfterOpcode=true: {:?}",
        result.diagnostics
    );
}

#[test]
fn signature_space_after_opcode_required() {
    let tables = tables_with_spacing_command("^ZZS", false);
    let result = parse_with_tables("^XA^ZZS5^XZ", Some(&tables));
    assert!(
        result
            .diagnostics
            .iter()
            .any(|d| d.id == codes::PARSER_INVALID_COMMAND && d.message.contains("expects a space")),
        "expected parser spacing diagnostic when noSpaceAfterOpcode=false: {:?}",
        result.diagnostics
    );
}

#[test]
fn signature_space_after_opcode_allows_space() {
    let tables = tables_with_spacing_command("^ZZS", false);
    let result = parse_with_tables("^XA^ZZS 5^XZ", Some(&tables));
    assert!(
        !result
            .diagnostics
            .iter()
            .any(|d| d.id == codes::PARSER_INVALID_COMMAND && d.message.contains("expects a space")),
        "space should be accepted when noSpaceAfterOpcode=false: {:?}",
        result.diagnostics
    );
}

#[test]
fn diag_parser_1302_non_ascii_arg() {
    let tables = &*common::TABLES;
    let result = parse_with_tables("^XA^CCé^XZ", Some(tables));
    assert!(
        result
            .diagnostics
            .iter()
            .any(|d| d.id == codes::PARSER_NON_ASCII_ARG),
        "non-ASCII arg for ^CC should emit ZPL.PARSER.1302: {:?}",
        result.diagnostics
    );
}

#[test]
fn special_a_font_orientation_split() {
    // With tables, ^A is recognized as single-char code, then special-case splitting
    // converts "0N" → f="0", o="N", plus h and w from the comma-separated rest.
    let tables = &*common::TABLES;
    let result = parse_with_tables("^XA^A0N,22,26^XZ", Some(tables));
    let codes = extract_codes(&result);
    assert!(
        codes.contains(&"^A".to_string()),
        "tables should recognize ^A: {:?}",
        codes
    );
    let args = find_args(&result, "^A");
    assert!(
        args.len() >= 4,
        "^A should have at least 4 args (f, o, h, w), got {}: {:?}",
        args.len(),
        args
    );
    assert_eq!(args[0].value.as_deref(), Some("0"), "font");
    assert_eq!(args[1].value.as_deref(), Some("N"), "orientation");
    assert_eq!(args[2].value.as_deref(), Some("22"), "height");
    assert_eq!(args[3].value.as_deref(), Some("26"), "width");
}

// ─── 5. Field Data Mode ─────────────────────────────────────────────────────

#[test]
fn field_data_preserved() {
    // With tables, ^FD is a field data command. When inline (^FD...^FS on same line),
    // the text after the opcode becomes ^FD's argument, since token collection
    // absorbs it before FieldData mode starts.
    let tables = &*common::TABLES;
    let result = parse_with_tables("^XA^FDHello World^FS^XZ", Some(tables));
    let codes = extract_codes(&result);
    assert!(codes.contains(&"^FD".to_string()), "should have ^FD");
    assert!(codes.contains(&"^FS".to_string()), "should have ^FS");
    // The text "Hello World" is in ^FD's args (inline absorption)
    let fd_args = find_args(&result, "^FD");
    assert_eq!(
        fd_args[0].value.as_deref(),
        Some("Hello World"),
        "^FD first arg should be exactly 'Hello World': {:?}",
        fd_args,
    );
}

#[test]
fn field_data_with_commas() {
    // Commas in field data text: inline ^FD absorbs text as args, and commas
    // act as argument separators (default joiner is ",").
    let tables = &*common::TABLES;
    let result = parse_with_tables("^XA^FDPrice: $1,234.56^FS^XZ", Some(tables));
    let fd_args = find_args(&result, "^FD");
    // Field data should be a single unsplit arg with the full content
    assert_eq!(
        fd_args.len(),
        1,
        "^FD should have exactly 1 arg (unsplit field data): {:?}",
        fd_args
    );
    assert_eq!(
        fd_args[0].value.as_deref(),
        Some("Price: $1,234.56"),
        "^FD arg should preserve the full field data including commas: {:?}",
        fd_args,
    );
}

#[test]
fn field_data_fv_also_works() {
    let tables = &*common::TABLES;
    let result = parse_with_tables("^XA^FVVariable data^FS^XZ", Some(tables));
    let codes = extract_codes(&result);
    assert!(codes.contains(&"^FV".to_string()), "should recognize ^FV");
    assert!(codes.contains(&"^FS".to_string()), "should have ^FS");
    let fv_args = find_args(&result, "^FV");
    assert_eq!(
        fv_args[0].value.as_deref(),
        Some("Variable data"),
        "^FV first arg should be exactly 'Variable data': {:?}",
        fv_args,
    );
}

// ─── 6. Span Tracking ──────────────────────────────────────────────────────

#[test]
fn spans_present_on_all_nodes() {
    let result = parse_str("^XA^FO10,20^XZ");
    for label in &result.ast.labels {
        for node in &label.nodes {
            let span = match node {
                Node::Command { span, .. } => span,
                Node::FieldData { span, .. } => span,
                Node::RawData { span, .. } => span,
                Node::Trivia { span, .. } => span,
                _ => unreachable!("unknown Node variant"),
            };
            assert!(
                span.end >= span.start,
                "span end should be >= start: {:?}",
                span
            );
        }
    }
}

#[test]
fn span_positions_correct() {
    // "^XA^FO10,20^XZ"
    //  0123456789...
    let input = "^XA^FO10,20^XZ";
    let result = parse_str(input);

    // ^XA occupies bytes 0..3
    let xa_span = result.ast.labels[0].nodes.iter().find_map(|n| match n {
        Node::Command { code, span, .. } if code == "^XA" => Some(*span),
        _ => None,
    });
    assert_eq!(xa_span, Some(Span::new(0, 3)), "^XA span");

    // ^FO occupies bytes 3..11 ("^FO10,20")
    let fo_span = result.ast.labels[0].nodes.iter().find_map(|n| match n {
        Node::Command { code, span, .. } if code == "^FO" => Some(*span),
        _ => None,
    });
    assert_eq!(fo_span, Some(Span::new(3, 11)), "^FO span");

    // ^XZ occupies bytes 11..14
    let xz_span = result.ast.labels[0].nodes.iter().find_map(|n| match n {
        Node::Command { code, span, .. } if code == "^XZ" => Some(*span),
        _ => None,
    });
    assert_eq!(xz_span, Some(Span::new(11, 14)), "^XZ span");
}

// ─── 7. Trivia Preservation ────────────────────────────────────────────────

#[test]
fn comments_preserved_as_trivia() {
    let result = parse_str("^XA;this is a comment\n^FO10,20^XZ");
    let trivia_texts: Vec<String> = result
        .ast
        .labels
        .iter()
        .flat_map(|l| l.nodes.iter())
        .filter_map(|n| match n {
            Node::Trivia { text, .. } => Some(text.clone()),
            _ => None,
        })
        .collect();
    assert!(
        trivia_texts.iter().any(|t| t.contains("this is a comment")),
        "comment should be preserved as Trivia: {:?}",
        trivia_texts,
    );
}

// ─── 8. Diagnostics ────────────────────────────────────────────────────────

#[test]
fn missing_xz_terminator() {
    let result = parse_str("^XA^FO10,10");
    let diag_codes = extract_diag_codes(&result);
    assert!(
        diag_codes.contains(&codes::PARSER_MISSING_TERMINATOR.to_string()),
        "should emit missing-^XZ diagnostic: {:?}",
        diag_codes,
    );
    // The error should be Error severity
    let diag = result
        .diagnostics
        .iter()
        .find(|d| d.id == codes::PARSER_MISSING_TERMINATOR)
        .unwrap();
    assert!(
        is_severity_error(&diag.severity),
        "1102 should be Error severity"
    );
}

#[test]
fn missing_fs_before_xz() {
    // With tables, ^FD activates field data mode. When ^XZ follows without ^FS,
    // the parser detects the interruption.
    let tables = &*common::TABLES;
    let result = parse_with_tables("^XA^FDHello^XZ", Some(tables));
    // The parser emits ZPL.PARSER.1203 (field data interrupted) when ^XZ's
    // leader interrupts field data mode, OR ZPL.PARSER.1202 for missing ^FS.
    let diag_codes = extract_diag_codes(&result);
    let has_fs_diag = diag_codes.iter().any(|c| {
        c == codes::PARSER_MISSING_FIELD_SEPARATOR || c == codes::PARSER_FIELD_DATA_INTERRUPTED
    });
    assert!(
        has_fs_diag,
        "should emit field-data diagnostic (1202 or 1203): {:?}",
        diag_codes,
    );
}

#[test]
fn no_labels_info() {
    let result = parse_str("");
    let diag = result
        .diagnostics
        .iter()
        .find(|d| d.id == codes::PARSER_NO_LABELS);
    assert!(
        diag.is_some(),
        "empty input should emit 0001 info diagnostic"
    );
    assert!(
        is_severity_info(&diag.unwrap().severity),
        "0001 should be Info severity"
    );
}

#[test]
fn diagnostic_has_span() {
    // For location-aware errors, spans should be present
    let result = parse_str("^XA^FO10,10");
    let diag_1102 = result
        .diagnostics
        .iter()
        .find(|d| d.id == codes::PARSER_MISSING_TERMINATOR);
    // Note: the 1102 diagnostic for missing ^XZ may or may not have a span
    // depending on implementation — it's emitted at end-of-input.
    // Let's verify the parser at least emits the diagnostic.
    assert!(diag_1102.is_some());

    // For an invalid leader (^^), the 1001 diagnostic should have a span
    let result2 = parse_str("^^");
    let diag_1001 = result2
        .diagnostics
        .iter()
        .find(|d| d.id == codes::PARSER_INVALID_COMMAND);
    assert!(diag_1001.is_some(), "^^ should emit 1001");
    assert!(
        diag_1001.unwrap().span.is_some(),
        "1001 for invalid leader should have a span"
    );
}

// ─── 9. Edge Cases ─────────────────────────────────────────────────────────

#[test]
fn only_leaders_no_code() {
    let result = parse_str("^^");
    let diag_codes = extract_diag_codes(&result);
    assert!(
        diag_codes
            .iter()
            .any(|c| c == codes::PARSER_INVALID_COMMAND),
        "leader-only input should produce error: {:?}",
        diag_codes,
    );
}

#[test]
fn label_with_all_empty_args() {
    let result = parse_str("^XA^BC,,,,,,^XZ");
    let args = find_args(&result, "^BC");
    assert_eq!(
        args.len(),
        7,
        "^BC,,,,,, should produce exactly 7 empty args, got {}",
        args.len()
    );
    for arg in &args {
        assert!(
            matches!(arg.presence, Presence::Empty),
            "all args should be Empty"
        );
    }
}

#[test]
fn utf8_in_field_data() {
    let tables = &*common::TABLES;
    let result = parse_with_tables("^XA^FDHéllo Wörld^FS^XZ", Some(tables));
    let fd_args = find_args(&result, "^FD");
    assert_eq!(
        fd_args[0].value.as_deref(),
        Some("Héllo Wörld"),
        "^FD first arg should preserve UTF-8 content exactly: {:?}",
        fd_args,
    );
}

#[test]
fn multiple_fields_in_label() {
    let tables = &*common::TABLES;
    let result = parse_with_tables("^XA^FO10,10^FDLine1^FS^FO10,50^FDLine2^FS^XZ", Some(tables));
    assert_eq!(result.ast.labels.len(), 1, "should be 1 label");
    let codes = extract_codes(&result);
    // Should have: ^XA, ^FO, ^FD, ^FS, ^FO, ^FD, ^FS, ^XZ
    let fd_count = codes.iter().filter(|c| c.as_str() == "^FD").count();
    let fs_count = codes.iter().filter(|c| c.as_str() == "^FS").count();
    assert_eq!(fd_count, 2, "should have 2 ^FD commands");
    assert_eq!(fs_count, 2, "should have 2 ^FS commands");

    // Check that both field data texts are captured
    let fd_nodes: Vec<_> = result.ast.labels[0]
        .nodes
        .iter()
        .filter(|n| matches!(n, Node::Command { code, .. } if code == "^FD"))
        .collect();
    assert_eq!(fd_nodes.len(), 2);
}

// ─── 9b. Additional Coverage ─────────────────────────────────────────────────

#[test]
fn field_data_interrupted_emits_1203() {
    let tables = &*common::TABLES;
    // ^FD starts field data, then ^FO interrupts before ^FS
    let result = parse_with_tables("^XA^FDHello^FO10,10^FS^XZ", Some(tables));
    assert!(
        result
            .diagnostics
            .iter()
            .any(|d| d.id == codes::PARSER_FIELD_DATA_INTERRUPTED),
        "non-^FS command interrupting field data should emit ZPL.PARSER.1203: {:?}",
        extract_diag_codes(&result),
    );
}

#[test]
fn field_data_at_eof_emits_1202() {
    let tables = &*common::TABLES;
    let result = parse_with_tables("^XA^FDunterminated", Some(tables));
    assert!(
        result
            .diagnostics
            .iter()
            .any(|d| d.id == codes::PARSER_MISSING_FIELD_SEPARATOR),
        "field data at EOF without ^FS should emit ZPL.PARSER.1202: {:?}",
        extract_diag_codes(&result),
    );
}

#[test]
fn empty_field_data() {
    let tables = &*common::TABLES;
    let result = parse_with_tables("^XA^FD^FS^XZ", Some(tables));
    let fd_args = find_args(&result, "^FD");
    // ^FD with no content → empty args
    assert!(
        fd_args.is_empty(),
        "^FD with no content should have no args: {:?}",
        fd_args
    );
    // Verify no error-level diagnostics
    assert!(
        !result
            .diagnostics
            .iter()
            .any(|d| matches!(d.severity, Severity::Error)),
        "^FD^FS should not produce errors: {:?}",
        extract_diag_codes(&result),
    );
}

#[test]
fn known_set_fallback_without_trie() {
    let mut tables = common::TABLES.clone();
    tables.opcode_trie = None;
    let result = parse_with_tables("^XA^FO10,10^XZ", Some(&tables));
    let codes = extract_codes(&result);
    assert!(
        codes.contains(&"^FO".to_string()),
        "known-set should recognize ^FO without trie: {:?}",
        codes
    );
}

#[test]
fn diagnostic_code_1002_distinct_from_1001() {
    let tables = &*common::TABLES;
    // ^QQ99 is unknown → should get ZPL.PARSER.1002 (not 1001)
    let result = parse_with_tables("^XA^QQ99^XZ", Some(tables));
    let has_1002 = result
        .diagnostics
        .iter()
        .any(|d| d.id == codes::PARSER_UNKNOWN_COMMAND);
    let has_1001 = result
        .diagnostics
        .iter()
        .any(|d| d.id == codes::PARSER_INVALID_COMMAND);
    assert!(has_1002, "unknown command should produce ZPL.PARSER.1002");
    assert!(
        !has_1001,
        "unknown command should NOT produce ZPL.PARSER.1001 (that's for invalid syntax)"
    );
}

#[test]
fn stray_content_warning() {
    // Text on its own line between commands should produce a stray content warning.
    // Note: stray content only appears when tokens aren't consumed by a command's
    // argument collection, which stops at newlines/leaders.
    let tables = &*common::TABLES;
    let result = parse_with_tables("^XA\n^FDHello^FS\nstray text here\n^XZ", Some(tables));
    let stray = result
        .diagnostics
        .iter()
        .filter(|d| d.id == codes::PARSER_STRAY_CONTENT)
        .collect::<Vec<_>>();
    assert!(
        !stray.is_empty(),
        "stray content should produce ZPL.PARSER.1301: {:?}",
        extract_diag_codes(&result)
    );
    // Verify it's a warning
    assert!(
        is_severity_warn(&stray[0].severity),
        "stray content should be a warning"
    );
    // Verify span exists
    assert!(
        stray[0].span.is_some(),
        "stray content diagnostic should have a span"
    );
}

#[test]
fn no_stray_warning_for_whitespace_between_commands() {
    // Whitespace and newlines between commands should NOT produce stray warnings.
    let tables = &*common::TABLES;
    let result = parse_with_tables("^XA\n  ^FO50,100\n  ^FDHello^FS\n^XZ", Some(tables));
    let stray = result
        .diagnostics
        .iter()
        .filter(|d| d.id == codes::PARSER_STRAY_CONTENT)
        .collect::<Vec<_>>();
    assert!(
        stray.is_empty(),
        "whitespace between commands should not produce stray warnings: {:?}",
        stray
    );
}

#[test]
fn stray_content_coalesces_adjacent_tokens() {
    // Multiple adjacent stray Value/Comma tokens on their own line should
    // produce a single coalesced warning.
    let tables = &*common::TABLES;
    let result = parse_with_tables("^XA\nhello,world\n^XZ", Some(tables));
    let stray = result
        .diagnostics
        .iter()
        .filter(|d| d.id == codes::PARSER_STRAY_CONTENT)
        .collect::<Vec<_>>();
    assert_eq!(
        stray.len(),
        1,
        "adjacent stray tokens should coalesce into one diagnostic, got {:?}",
        stray
    );
}

#[test]
fn recovery_after_invalid_leader() {
    // A bare ^ at end-of-input should recover and still parse subsequent content.
    let tables = &*common::TABLES;
    let result = parse_with_tables("^XA^^FDHello^FS^XZ", Some(tables));
    let has_invalid = result
        .diagnostics
        .iter()
        .any(|d| d.id == codes::PARSER_INVALID_COMMAND);
    assert!(
        has_invalid,
        "bare ^^ should produce an invalid command error"
    );
    // Despite the error, ^FD should still be parsed
    let has_fd = result.ast.labels.iter().any(|l| {
        l.nodes.iter().any(|n| match n {
            zpl_toolchain_core::grammar::ast::Node::Command { code, .. } => code == "^FD",
            _ => false,
        })
    });
    assert!(has_fd, "parser should recover and parse ^FD after ^^");
}

// ─── 10. Parser Diagnostic Coverage ─────────────────────────────────────────
//
// Parser diagnostics (verified existing coverage):
//   ZPL.PARSER.0001 — empty_input_no_labels, no_labels_info
//   ZPL.PARSER.1001 — only_leaders_no_code, diagnostic_has_span
//   ZPL.PARSER.1002 — unknown_command_warning, diagnostic_code_1002_distinct_from_1001
//   ZPL.PARSER.1102 — missing_xz_terminator
//   ZPL.PARSER.1202 — missing_fs_before_xz, field_data_at_eof_emits_1202
//   ZPL.PARSER.1203 — field_data_interrupted_emits_1203
//
// Validator diagnostic tests are in validator.rs.

// ── Parser diagnostic coverage (explicit regression tests) ──────────────────

#[test]
fn diag_parser_0001_no_labels() {
    // Distinct from the basic test: also verify the diagnostic message content
    let result = parse_str("  \t\n  ");
    assert!(
        result
            .diagnostics
            .iter()
            .any(|d| d.id == codes::PARSER_NO_LABELS),
        "whitespace-only input should emit no-labels diagnostic: {:?}",
        extract_diag_codes(&result),
    );
}

#[test]
fn diag_parser_1001_leader_then_eof() {
    // Leader "^" at end of input with no command code following
    let result = parse_str("^XA^");
    assert!(
        result
            .diagnostics
            .iter()
            .any(|d| d.id == codes::PARSER_INVALID_COMMAND),
        "leader at EOF should emit 1001: {:?}",
        extract_diag_codes(&result),
    );
}

#[test]
fn diag_parser_1002_unknown_command() {
    let tables = &*common::TABLES;
    // ^QQ is not a real ZPL command; use with a trailing digit to ensure
    // the heuristic produces a 2-char code that isn't in the tables.
    let result = parse_with_tables("^XA^QQ99^XZ", Some(tables));
    assert!(
        result
            .diagnostics
            .iter()
            .any(|d| d.id == codes::PARSER_UNKNOWN_COMMAND),
        "unknown command should emit 1002: {:?}",
        extract_diag_codes(&result),
    );
}

#[test]
fn diag_parser_1102_missing_xz() {
    let result = parse_str("^XA^FO10,10^FDHello^FS");
    assert!(
        result
            .diagnostics
            .iter()
            .any(|d| d.id == codes::PARSER_MISSING_TERMINATOR),
        "missing ^XZ should emit 1102: {:?}",
        extract_diag_codes(&result),
    );
}

#[test]
fn diag_parser_1202_missing_fs_eof() {
    let tables = &*common::TABLES;
    let result = parse_with_tables("^XA^FDdata without separator", Some(tables));
    assert!(
        result
            .diagnostics
            .iter()
            .any(|d| d.id == codes::PARSER_MISSING_FIELD_SEPARATOR),
        "field data at EOF without ^FS should emit 1202: {:?}",
        extract_diag_codes(&result),
    );
}

#[test]
fn diag_parser_1203_field_data_interrupted() {
    let tables = &*common::TABLES;
    // ^FD starts field data, then ^CF interrupts before ^FS
    let result = parse_with_tables("^XA^FDHello^CF0,30^FS^XZ", Some(tables));
    assert!(
        result
            .diagnostics
            .iter()
            .any(|d| d.id == codes::PARSER_FIELD_DATA_INTERRUPTED),
        "command interrupting field data should emit 1203: {:?}",
        extract_diag_codes(&result),
    );
}

// ─── 11. Raw Payload Mode ───────────────────────────────────────────────────

#[test]
fn raw_payload_gf_inline_no_extra_node() {
    let tables = &*common::TABLES;
    // When all data is inline (no continuation after args), no RawData node should be created.
    let input = "^XA^GFA,8,8,1,FFAA5500FFAA5500^FS^XZ";
    let r = parse_with_tables(input, Some(tables));

    let nodes = &r.ast.labels[0].nodes;
    // Data is fully captured in args[4] — no spurious RawData node
    let has_raw = nodes.iter().any(|n| matches!(n, Node::RawData { .. }));
    assert!(
        !has_raw,
        "Fully-inline data should NOT produce a RawData node, got: {:?}",
        nodes
    );

    // Verify data is in the command args
    let gf = nodes
        .iter()
        .find(|n| matches!(n, Node::Command { code, .. } if code == "^GF"))
        .unwrap();
    if let Node::Command { args, .. } = gf {
        assert_eq!(
            args[4].value.as_deref(),
            Some("FFAA5500FFAA5500"),
            "data should be in args[4]"
        );
    }
}

#[test]
fn raw_payload_gf_data_preserved() {
    let tables = &*common::TABLES;
    // After ^GF args are parsed (A,4,4,1), the parser hits the newline which
    // ends the arg collection. Then RawData mode collects the continuation data
    // on the next line until ^XZ.
    let input = "^XA^GFA,4,4,1,AABBCCDD^XZ";
    let r = parse_with_tables(input, Some(tables));
    let nodes = &r.ast.labels[0].nodes;

    // The data might be in the last arg of the ^GF command (inline) or in a
    // RawData node (continuation). Either way, we should have no panics.
    let has_gf = nodes
        .iter()
        .any(|n| matches!(n, Node::Command { code, .. } if code == "^GF"));
    assert!(has_gf, "Expected ^GF command node");
}

#[test]
fn raw_payload_gf_multiline() {
    let tables = &*common::TABLES;
    // Multi-line: header on first line, data on subsequent lines
    let input = "^XA^GFA,8,8,1\nFFAA5500\nFFAA5500\n^FS^XZ";
    let r = parse_with_tables(input, Some(tables));
    let nodes = &r.ast.labels[0].nodes;

    // The data spanning multiple lines should be collected as RawData
    let raw = nodes.iter().find_map(|n| {
        if let Node::RawData { data, .. } = n {
            data.clone()
        } else {
            None
        }
    });
    assert!(
        raw.is_some(),
        "Expected raw data content for multi-line ^GF, got nodes: {:?}",
        nodes
    );
    let data = raw.unwrap();
    assert!(
        data.contains("FFAA5500"),
        "Raw data should contain hex payload, got: {}",
        data
    );
}

#[test]
fn raw_payload_empty_data() {
    let tables = &*common::TABLES;
    // ^GF with args but empty data — should not panic
    let input = "^XA^GFA,0,0,1,^XZ";
    let r = parse_with_tables(input, Some(tables));
    assert!(
        !r.ast.labels.is_empty(),
        "Should produce at least one label"
    );
}

#[test]
fn raw_payload_dg_basic() {
    let tables = &*common::TABLES;
    let input = "^XA~DGR:LOGO.GRF,4,1\nFFAA5500\n^XZ";
    let r = parse_with_tables(input, Some(tables));
    let nodes = &r.ast.labels[0].nodes;
    let has_raw = nodes
        .iter()
        .any(|n| matches!(n, Node::RawData { command, .. } if command == "~DG"));
    assert!(
        has_raw,
        "Expected RawData node for ~DG payload, got nodes: {:?}",
        nodes
    );
}

#[test]
fn raw_payload_span_tracking() {
    let tables = &*common::TABLES;
    let input = "^XA^GFA,4,4,1\nAABBCCDD\n^XZ";
    let r = parse_with_tables(input, Some(tables));
    let nodes = &r.ast.labels[0].nodes;

    // RawData node should have a valid span
    for node in nodes {
        if let Node::RawData { span, .. } = node {
            assert!(span.end >= span.start, "span end >= start: {:?}", span);
        }
    }
}

#[test]
fn raw_payload_at_eof() {
    let tables = &*common::TABLES;
    // ^GF at end of input without ^FS or ^XZ — unterminated raw data
    let input = "^XA^GFA,4,4,1\nAABBCCDD";
    let r = parse_with_tables(input, Some(tables));
    let nodes = &r.ast.labels[0].nodes;

    // Should still produce a RawData node from the cleanup path
    let has_raw = nodes.iter().any(|n| matches!(n, Node::RawData { .. }));
    assert!(
        has_raw,
        "Expected RawData node at EOF, got nodes: {:?}",
        nodes
    );
}

#[test]
fn raw_payload_inline_data_then_fs() {
    let tables = &*common::TABLES;
    // Common case: ^GFA,8,8,1,FFAA5500FFAA5500^FS on same line
    // The args parsing absorbs the data as the 5th arg.
    // Then RawData mode starts but immediately hits ^FS leader,
    // so RawData content is empty.
    let input = "^XA^GFA,8,8,1,FFAA5500FFAA5500^FS^XZ";
    let r = parse_with_tables(input, Some(tables));

    // ^GF command should have its args including data
    let gf_args = find_args(&r, "^GF");
    assert!(!gf_args.is_empty(), "^GF should have args");

    // The data arg (5th) should contain the hex payload
    let data_arg = gf_args.iter().find(|a| a.key.as_deref() == Some("data"));
    assert!(
        data_arg.is_some(),
        "^GF should have a 'data' arg: {:?}",
        gf_args
    );
    assert_eq!(
        data_arg.unwrap().value.as_deref(),
        Some("FFAA5500FFAA5500"),
        "data arg should contain hex payload"
    );
}

#[test]
fn raw_payload_no_false_positives_non_raw_command() {
    let tables = &*common::TABLES;
    // ^FO is NOT a raw_payload command — should NOT produce RawData nodes
    let input = "^XA^FO10,20^XZ";
    let r = parse_with_tables(input, Some(tables));
    let nodes = &r.ast.labels[0].nodes;
    let has_raw = nodes.iter().any(|n| matches!(n, Node::RawData { .. }));
    assert!(
        !has_raw,
        "Non-raw-payload commands should NOT produce RawData nodes"
    );
}

// ─── 12. Sample File Parsing ───────────────────────────────────────────────

#[test]
fn usps_sample_no_errors() {
    let tables = &*common::TABLES;
    let mut root = std::path::PathBuf::from(env!("CARGO_MANIFEST_DIR"));
    root.pop();
    root.pop();
    let sample_path = root.join("samples/usps_surepost_sample.zpl");
    let input = std::fs::read_to_string(&sample_path).expect("missing sample file");
    let result = parse_with_tables(&input, Some(tables));

    // Should produce at least 1 label
    assert!(
        !result.ast.labels.is_empty(),
        "sample should produce at least one label"
    );

    // Should have no Error-level diagnostics
    let errors: Vec<_> = result
        .diagnostics
        .iter()
        .filter(|d| is_severity_error(&d.severity))
        .collect();
    assert!(
        errors.is_empty(),
        "sample file should have no error-level diagnostics, got: {:?}",
        errors,
    );
}

// ─── 13. Prefix / Delimiter State Tracking (^CC, ~CT, ^CD) ──────────────────

#[test]
fn prefix_change_cc() {
    let tables = &*common::TABLES;
    // ^CC+ changes the command prefix from ^ to +.
    // After the change, all commands must use + as the prefix.
    // The newline after ^CC+ provides a natural token boundary so that the
    // retokenized stream correctly identifies + as the new leader.
    let input = "^XA^CC+\n+FO10,10+FDHello+FS+XZ";
    let result = parse_with_tables(input, Some(tables));
    let codes = extract_codes(&result);
    // All codes should use canonical ^ prefix regardless of actual prefix used
    assert!(
        codes.contains(&"^XA".to_string()),
        "should have ^XA: {:?}",
        codes
    );
    assert!(
        codes.contains(&"^CC".to_string()),
        "should have ^CC: {:?}",
        codes
    );
    assert!(
        codes.contains(&"^FO".to_string()),
        "should have ^FO (via +FO): {:?}",
        codes
    );
    assert!(
        codes.contains(&"^FD".to_string()),
        "should have ^FD (via +FD): {:?}",
        codes
    );
    assert!(
        codes.contains(&"^FS".to_string()),
        "should have ^FS (via +FS): {:?}",
        codes
    );
    assert!(
        codes.contains(&"^XZ".to_string()),
        "should have ^XZ (via +XZ): {:?}",
        codes
    );
    // Verify the label is properly formed
    assert_eq!(result.ast.labels.len(), 1, "should produce 1 label");
    // Verify ^FO args parsed correctly
    let fo_args = find_args(&result, "^FO");
    assert!(fo_args.len() >= 2, "^FO should have at least 2 args");
    assert_eq!(fo_args[0].value.as_deref(), Some("10"));
    assert_eq!(fo_args[1].value.as_deref(), Some("10"));
}

#[test]
fn prefix_change_ct() {
    let tables = &*common::TABLES;
    // ~CT# changes the control prefix from ~ to #
    // After that, control commands use # as the prefix: #HS
    let input = "^XA~CT#^FO10,10^FDtest^FS^XZ";
    let result = parse_with_tables(input, Some(tables));
    let codes = extract_codes(&result);
    assert!(
        codes.contains(&"~CT".to_string()),
        "should have ~CT: {:?}",
        codes
    );
    assert!(
        codes.contains(&"^FO".to_string()),
        "should have ^FO: {:?}",
        codes
    );
    // The label should parse correctly
    assert_eq!(result.ast.labels.len(), 1, "should produce 1 label");
}

#[test]
fn delimiter_change_cd() {
    let tables = &*common::TABLES;
    // ^CD| changes the argument delimiter from , to |
    // After that, ^FO uses | as the delimiter: ^FO10|20
    // (Note: `;` can't be used because the lexer treats it as a comment start)
    let input = "^XA^CD|^FO10|20^FS^XZ";
    let result = parse_with_tables(input, Some(tables));
    let codes = extract_codes(&result);
    assert!(
        codes.contains(&"^CD".to_string()),
        "should have ^CD: {:?}",
        codes
    );
    assert!(
        codes.contains(&"^FO".to_string()),
        "should have ^FO: {:?}",
        codes
    );
    // Verify ^FO args parsed correctly with | delimiter
    // ^FO has allowEmptyTrailing with 3 params, so padding gives 3 args
    let fo_args = find_args(&result, "^FO");
    assert!(
        fo_args.len() >= 2,
        "^FO should have at least 2 args with | delimiter"
    );
    assert_eq!(fo_args[0].value.as_deref(), Some("10"), "first arg");
    assert_eq!(fo_args[1].value.as_deref(), Some("20"), "second arg");
}

#[test]
fn prefix_and_delimiter_regression() {
    let tables = &*common::TABLES;
    // Default input with no prefix/delimiter changes should still work correctly
    let input = "^XA^FO10,20^FS^XZ";
    let result = parse_with_tables(input, Some(tables));
    let codes = extract_codes(&result);
    assert_eq!(codes, vec!["^XA", "^FO", "^FS", "^XZ"]);
    // ^FO has allowEmptyTrailing with 3 params (x,y,z), so 2 provided + 1 padded = 3
    let fo_args = find_args(&result, "^FO");
    assert!(fo_args.len() >= 2, "^FO should have at least x,y args");
    assert_eq!(fo_args[0].value.as_deref(), Some("10"));
    assert_eq!(fo_args[1].value.as_deref(), Some("20"));
    assert_eq!(result.ast.labels.len(), 1);
    // No errors
    let errors: Vec<_> = result
        .diagnostics
        .iter()
        .filter(|d| matches!(d.severity, Severity::Error))
        .collect();
    assert!(
        errors.is_empty(),
        "regression: should have no errors: {:?}",
        errors
    );
}

#[test]
fn prefix_change_cc_with_tilde_variant() {
    let tables = &*common::TABLES;
    // ~CC+ changes the caret command prefix from ^ to +
    // After this, + is the leader for format commands, ^ is no longer a leader.
    // Note: we use canonical codes (^FD, ^FS, ^XZ) in the AST even though the
    // source uses +FD, +FS, +XZ — the parser maps custom prefixes to canonical ^.
    let input = "^XA~CC++FO10,10+FDHello+FS+XZ";
    let result = parse_with_tables(input, Some(tables));
    let codes = extract_codes(&result);
    assert!(
        codes.contains(&"~CC".to_string()),
        "should have ~CC: {:?}",
        codes
    );
    // After prefix change, +FO → ^FO, +FD → ^FD, +FS → ^FS, +XZ → ^XZ
    assert!(
        codes.contains(&"^FO".to_string()),
        "should have ^FO (via +FO): {:?}",
        codes
    );
    assert!(
        codes.contains(&"^FD".to_string()),
        "should have ^FD (via +FD): {:?}",
        codes
    );
    assert!(
        codes.contains(&"^XZ".to_string()),
        "should have ^XZ (via +XZ): {:?}",
        codes
    );
}

#[test]
fn tokenize_with_config_basic() {
    use zpl_toolchain_core::grammar::lexer::{TokKind, tokenize_with_config};
    // With + as command prefix and ~ as control prefix
    let toks = tokenize_with_config("+XA+FO10,20+XZ", '+', '~', ',');
    let leaders: Vec<&str> = toks
        .iter()
        .filter(|t| t.kind == TokKind::Leader)
        .map(|t| t.text)
        .collect();
    assert_eq!(leaders, vec!["+", "+", "+"], "should recognize + as leader");
    // Verify values don't contain + (it's treated as a leader, not part of value)
    let values: Vec<&str> = toks
        .iter()
        .filter(|t| t.kind == TokKind::Value)
        .map(|t| t.text)
        .collect();
    assert!(values.contains(&"XA"), "should have XA value token");
    assert!(values.contains(&"FO10"), "should have FO10 value token");
    assert!(values.contains(&"20"), "should have 20 value token");
    assert!(values.contains(&"XZ"), "should have XZ value token");
}

#[test]
fn tokenize_default_unchanged() {
    use zpl_toolchain_core::grammar::lexer::{TokKind, tokenize};
    // Default tokenize should still work with ^ and ~
    let toks = tokenize("^XA~JA^XZ");
    let leaders: Vec<&str> = toks
        .iter()
        .filter(|t| t.kind == TokKind::Leader)
        .map(|t| t.text)
        .collect();
    assert_eq!(
        leaders,
        vec!["^", "~", "^"],
        "default tokenize should recognize ^ and ~"
    );
}

// ─── 14. Parser Context on Diagnostics ───────────────────────────────────────

#[test]
fn context_parser_unknown_command() {
    let tables = &*common::TABLES;
    // ^QQ is not a real ZPL command
    let result = parse_with_tables("^XA^QQ99^XZ", Some(tables));
    let d = common::find_diag(&result.diagnostics, codes::PARSER_UNKNOWN_COMMAND);
    let ctx = d
        .context
        .as_ref()
        .expect("parser diagnostic should have context");
    assert_eq!(ctx.get("command").unwrap(), "^QQ");
}

#[test]
fn context_parser_missing_terminator() {
    let tables = &*common::TABLES;
    let result = parse_with_tables("^XA^FO10,10^FDHello^FS", Some(tables));
    let d = common::find_diag(&result.diagnostics, codes::PARSER_MISSING_TERMINATOR);
    let ctx = d
        .context
        .as_ref()
        .expect("parser diagnostic should have context");
    assert_eq!(ctx.get("expected").unwrap(), "^XZ");
}