hedl-core 2.0.0

Core parser and data model for HEDL (Hierarchical Entity Data Language)
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
// Dweve HEDL - Hierarchical Entity Data Language
//
// Copyright (c) 2025 Dweve IP B.V. and individual contributors.
//
// SPDX-License-Identifier: Apache-2.0
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License in the LICENSE file at the
// root of this repository or at: http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

//! CSV parsing for HEDL matrix rows.
//!
//! Implements the normative CSV parsing algorithm from SPEC Section 9.2.

use super::error::LexError;
use super::span::SourcePos;

/// A parsed CSV field with its content and quoting status.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct CsvField {
    /// The field content (unquoted).
    pub value: String,
    /// Whether this field was enclosed in quotes.
    pub is_quoted: bool,
}

/// Parse a CSV row into fields.
///
/// Follows the normative algorithm from SPEC Section 9.2:
/// - Delimiter: comma
/// - Quoting: double quotes only
/// - Escaping: "" inside quotes → literal "
/// - Whitespace: unquoted fields trimmed
/// - Trailing comma: error
///
/// # Examples
///
/// ```
/// use hedl_core::lex::parse_csv_row;
///
/// let fields = parse_csv_row("a, b, c").unwrap();
/// assert_eq!(fields.len(), 3);
/// assert_eq!(fields[0].value, "a");
/// assert!(!fields[0].is_quoted);
///
/// let fields = parse_csv_row(r#""quoted, value", other"#).unwrap();
/// assert_eq!(fields[0].value, "quoted, value");
/// assert!(fields[0].is_quoted);
/// ```
pub fn parse_csv_row(s: &str) -> Result<Vec<CsvField>, LexError> {
    if s.is_empty() {
        return Ok(Vec::new());
    }

    // Check for trailing comma
    if s.trim_end().ends_with(',') {
        return Err(LexError::InvalidToken {
            message: "trailing comma not allowed in CSV row".to_string(),
            pos: SourcePos::new(1, 1),
        });
    }

    // Pre-allocate based on comma count
    let estimated_fields = s.matches(',').count() + 1;
    let mut fields = Vec::with_capacity(estimated_fields);
    let mut i = 0;
    let chars: Vec<char> = s.chars().collect();

    while i < chars.len() {
        // Skip leading whitespace
        while i < chars.len() && chars[i].is_whitespace() {
            i += 1;
        }

        if i >= chars.len() {
            break;
        }

        // Parse one field
        let (field, new_i) = parse_field(&chars, i)?;
        fields.push(field);
        i = new_i;

        // Skip trailing whitespace after field
        while i < chars.len() && chars[i].is_whitespace() {
            i += 1;
        }

        if i < chars.len() {
            if chars[i] == ',' {
                i += 1; // Skip comma
            } else {
                return Err(LexError::InvalidToken {
                    message: format!("expected comma or end of line, got '{}'", chars[i]),
                    pos: SourcePos::new(1, i + 1),
                });
            }
        }
    }

    Ok(fields)
}

/// Parse a single field starting at position i.
/// Returns the field and the position after it.
fn parse_field(chars: &[char], start: usize) -> Result<(CsvField, usize), LexError> {
    let mut i = start;

    // Skip leading whitespace
    while i < chars.len() && chars[i].is_whitespace() {
        i += 1;
    }

    if i >= chars.len() {
        // Empty field
        return Ok((
            CsvField {
                value: String::new(),
                is_quoted: false,
            },
            i,
        ));
    }

    if chars[i] == '"' {
        // Quoted field
        parse_quoted_field(chars, i)
    } else {
        // Unquoted field
        parse_unquoted_field(chars, i)
    }
}

/// Parse a quoted field starting with ".
/// Supports escape sequences: `\n` (newline), `\t` (tab), `\\` (backslash), `\"` (quote).
/// Also supports CSV-style `""` for embedded quotes.
fn parse_quoted_field(chars: &[char], start: usize) -> Result<(CsvField, usize), LexError> {
    let mut i = start + 1; // Skip opening quote
    let mut value = String::new();

    while i < chars.len() {
        if chars[i] == '"' {
            // Check for escaped quote (CSV-style "")
            if i + 1 < chars.len() && chars[i + 1] == '"' {
                value.push('"');
                i += 2;
            } else {
                // End of quoted field
                i += 1; // Skip closing quote
                return Ok((
                    CsvField {
                        value,
                        is_quoted: true,
                    },
                    i,
                ));
            }
        } else if chars[i] == '\\' && i + 1 < chars.len() {
            // Handle escape sequences
            let next = chars[i + 1];
            match next {
                'n' => {
                    value.push('\n');
                    i += 2;
                }
                't' => {
                    value.push('\t');
                    i += 2;
                }
                '\\' => {
                    value.push('\\');
                    i += 2;
                }
                '"' => {
                    value.push('"');
                    i += 2;
                }
                _ => {
                    // Only \", \\, \n, \t are valid escapes
                    return Err(LexError::InvalidEscape {
                        sequence: format!("\\{}", next),
                        pos: SourcePos::new(1, i + 1),
                    });
                }
            }
        } else {
            value.push(chars[i]);
            i += 1;
        }
    }

    // Hit end of line without closing quote
    Err(LexError::UnclosedQuote {
        pos: SourcePos::new(1, start + 1),
    })
}

/// Parse an unquoted field.
fn parse_unquoted_field(chars: &[char], start: usize) -> Result<(CsvField, usize), LexError> {
    let mut i = start;
    let mut value = String::new();
    let mut expr_depth = 0;
    let mut in_expr_quotes = false;

    while i < chars.len() {
        let ch = chars[i];

        // Check if we hit a comma (and not in expression)
        if ch == ',' && expr_depth == 0 {
            break;
        }

        value.push(ch);

        // Track expression depth
        if ch == '$' && i + 1 < chars.len() && chars[i + 1] == '(' {
            expr_depth += 1;
            value.push(chars[i + 1]);
            i += 2;
            continue;
        }

        // Handle quotes inside expressions
        if expr_depth > 0 {
            if ch == '"' {
                if in_expr_quotes {
                    // Check for escaped quote
                    if i + 1 < chars.len() && chars[i + 1] == '"' {
                        value.push(chars[i + 1]);
                        i += 2;
                        continue;
                    } else {
                        in_expr_quotes = false;
                    }
                } else {
                    in_expr_quotes = true;
                }
            } else if !in_expr_quotes {
                if ch == '(' {
                    expr_depth += 1;
                } else if ch == ')' {
                    expr_depth -= 1;
                }
            }
        }

        i += 1;
    }

    if expr_depth > 0 {
        return Err(LexError::UnclosedExpression {
            pos: SourcePos::new(1, start + 1),
        });
    }

    let trimmed = value.trim().to_string();

    // Check for quotes in unquoted field (but not if it's an expression)
    if trimmed.contains('"') && !trimmed.starts_with("$(") {
        return Err(LexError::InvalidToken {
            message: format!(
                "quote character '\"' found in unquoted field: '{}'",
                trimmed
            ),
            pos: SourcePos::new(1, start + 1),
        });
    }

    Ok((
        CsvField {
            value: trimmed,
            is_quoted: false,
        },
        i,
    ))
}

/// Split inline children data by `|` at top level only.
///
/// In inline child blocks, `|` splits rows only at top level.
/// Commas and `|` do NOT split when inside:
/// - "quoted strings"
/// - [tensor literals]
/// - (list literals)
/// - $(expressions)
///
/// # Examples
///
/// ```
/// use hedl_core::lex::csv::split_inline_children;
///
/// // Simple split
/// let parts = split_inline_children("a|b|c", '"').unwrap();
/// assert_eq!(parts, vec!["a", "b", "c"]);
///
/// // Quoted pipe preserved
/// let parts = split_inline_children(r#"a|"b|c"|d"#, '"').unwrap();
/// assert_eq!(parts, vec!["a", "\"b|c\"", "d"]);
///
/// // Tensor pipe preserved
/// let parts = split_inline_children("a|[1|2]|c", '"').unwrap();
/// assert_eq!(parts, vec!["a", "[1|2]", "c"]);
/// ```
pub fn split_inline_children(s: &str, quote_char: char) -> Result<Vec<&str>, LexError> {
    if s.is_empty() {
        return Ok(Vec::new());
    }

    let mut parts = Vec::new();
    let mut start = 0;
    let chars: Vec<char> = s.chars().collect();
    let mut i = 0;

    // Track nesting depth for various constructs
    let mut quote_open = false;
    let mut bracket_depth = 0; // [tensor literals]
    let mut paren_depth = 0; // (list literals) and $(expressions)

    while i < chars.len() {
        let ch = chars[i];

        // Handle quotes
        if ch == quote_char && bracket_depth == 0 && paren_depth == 0 {
            if quote_open {
                // Check for escaped quote (doubled quote)
                if i + 1 < chars.len() && chars[i + 1] == quote_char {
                    i += 2; // Skip both quotes
                    continue;
                }
                quote_open = false;
            } else {
                quote_open = true;
            }
            i += 1;
            continue;
        }

        // If inside quotes, just advance
        if quote_open {
            i += 1;
            continue;
        }

        // Handle backslash escapes (like \| inside strings)
        if ch == '\\' && i + 1 < chars.len() {
            i += 2; // Skip escaped character
            continue;
        }

        // Track bracket depth for tensor literals
        if ch == '[' {
            bracket_depth += 1;
        } else if ch == ']' && bracket_depth > 0 {
            bracket_depth -= 1;
        }

        // Track paren depth for list literals and expressions
        if ch == '(' {
            paren_depth += 1;
        } else if ch == ')' && paren_depth > 0 {
            paren_depth -= 1;
        }

        // Split on pipe only at top level
        if ch == '|' && bracket_depth == 0 && paren_depth == 0 {
            // Calculate byte offset for slice
            let byte_start: usize = chars[..start].iter().map(|c| c.len_utf8()).sum();
            let byte_end: usize = chars[..i].iter().map(|c| c.len_utf8()).sum();
            parts.push(&s[byte_start..byte_end]);
            start = i + 1;
        }

        i += 1;
    }

    // Check for unclosed constructs
    if quote_open {
        return Err(LexError::UnclosedQuote {
            pos: SourcePos::new(1, start + 1),
        });
    }
    if bracket_depth > 0 {
        return Err(LexError::InvalidToken {
            message: "unclosed tensor literal '[' in inline children".to_string(),
            pos: SourcePos::new(1, start + 1),
        });
    }
    if paren_depth > 0 {
        return Err(LexError::InvalidToken {
            message: "unclosed list literal '(' in inline children".to_string(),
            pos: SourcePos::new(1, start + 1),
        });
    }

    // Add the last part
    let byte_start: usize = chars[..start].iter().map(|c| c.len_utf8()).sum();
    parts.push(&s[byte_start..]);

    Ok(parts)
}

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

    #[test]
    fn test_parse_csv_simple() {
        let fields = parse_csv_row("a, b, c").unwrap();
        assert_eq!(fields.len(), 3);
        assert_eq!(fields[0].value, "a");
        assert_eq!(fields[1].value, "b");
        assert_eq!(fields[2].value, "c");
        assert!(!fields[0].is_quoted);
    }

    #[test]
    fn test_parse_csv_no_spaces() {
        let fields = parse_csv_row("a,b,c").unwrap();
        assert_eq!(fields.len(), 3);
        assert_eq!(fields[0].value, "a");
    }

    #[test]
    fn test_parse_csv_quoted_field() {
        let fields = parse_csv_row(r#""quoted, value", other"#).unwrap();
        assert_eq!(fields.len(), 2);
        assert_eq!(fields[0].value, "quoted, value");
        assert!(fields[0].is_quoted);
        assert_eq!(fields[1].value, "other");
        assert!(!fields[1].is_quoted);
    }

    #[test]
    fn test_parse_csv_escaped_quote() {
        // Input: "escaped ""quote""" (with proper closing quote)
        // Expected output: escaped "quote"
        let input = "\"escaped \"\"quote\"\"\"";
        let fields = parse_csv_row(input).unwrap();
        assert_eq!(fields.len(), 1);
        assert_eq!(fields[0].value, "escaped \"quote\"");
        assert!(fields[0].is_quoted);
    }

    #[test]
    fn test_parse_csv_empty_fields() {
        let fields = parse_csv_row("a,,c").unwrap();
        assert_eq!(fields.len(), 3);
        assert_eq!(fields[0].value, "a");
        assert_eq!(fields[1].value, "");
        assert_eq!(fields[2].value, "c");
    }

    #[test]
    fn test_parse_csv_whitespace_preserved_quoted() {
        let fields = parse_csv_row(r#""  spaced  ", other"#).unwrap();
        assert_eq!(fields[0].value, "  spaced  ");
        assert!(fields[0].is_quoted);
    }

    #[test]
    fn test_parse_csv_whitespace_trimmed_unquoted() {
        let fields = parse_csv_row("  spaced  ,  other  ").unwrap();
        assert_eq!(fields[0].value, "spaced");
        assert_eq!(fields[1].value, "other");
    }

    #[test]
    fn test_parse_csv_empty_string() {
        let fields = parse_csv_row("").unwrap();
        assert!(fields.is_empty());
    }

    #[test]
    fn test_parse_csv_trailing_comma_error() {
        let result = parse_csv_row("a, b,");
        assert!(result.is_err());
    }

    #[test]
    fn test_parse_csv_unclosed_quote_error() {
        let result = parse_csv_row(r#""unclosed"#);
        assert!(matches!(result, Err(LexError::UnclosedQuote { .. })));
    }

    #[test]
    fn test_parse_csv_quote_in_unquoted_field_error() {
        let result = parse_csv_row(r#"hello"world"#);
        assert!(result.is_err());
    }

    #[test]
    fn test_parse_csv_with_expression() {
        let fields = parse_csv_row("$(x + 1), other").unwrap();
        assert_eq!(fields.len(), 2);
        assert_eq!(fields[0].value, "$(x + 1)");
        assert!(!fields[0].is_quoted);
    }

    #[test]
    fn test_parse_csv_expression_with_comma() {
        let fields = parse_csv_row("$(a, b), other").unwrap();
        assert_eq!(fields.len(), 2);
        assert_eq!(fields[0].value, "$(a, b)");
    }

    #[test]
    fn test_parse_csv_expression_with_nested_parens() {
        let fields = parse_csv_row("$((a + b)), other").unwrap();
        assert_eq!(fields[0].value, "$((a + b))");
    }

    #[test]
    fn test_parse_csv_expression_with_quotes() {
        let fields = parse_csv_row(r#"$(concat("a", "b")), other"#).unwrap();
        assert_eq!(fields[0].value, r#"$(concat("a", "b"))"#);
    }

    #[test]
    fn test_parse_csv_unclosed_expression_error() {
        let result = parse_csv_row("$(incomplete, other");
        assert!(matches!(result, Err(LexError::UnclosedExpression { .. })));
    }

    #[test]
    fn test_parse_csv_single_field() {
        let fields = parse_csv_row("single").unwrap();
        assert_eq!(fields.len(), 1);
        assert_eq!(fields[0].value, "single");
    }

    #[test]
    fn test_parse_csv_all_quoted() {
        let fields = parse_csv_row(r#""a", "b", "c""#).unwrap();
        assert_eq!(fields.len(), 3);
        assert!(fields.iter().all(|f| f.is_quoted));
    }

    #[test]
    fn test_parse_csv_escape_newline() {
        // \n becomes actual newline
        let fields = parse_csv_row(r#""line1\nline2""#).unwrap();
        assert_eq!(fields.len(), 1);
        assert_eq!(fields[0].value, "line1\nline2");
        assert!(fields[0].value.contains('\n'));
    }

    #[test]
    fn test_parse_csv_escape_tab() {
        // \t becomes actual tab
        let fields = parse_csv_row(r#""col1\tcol2""#).unwrap();
        assert_eq!(fields.len(), 1);
        assert_eq!(fields[0].value, "col1\tcol2");
        assert!(fields[0].value.contains('\t'));
    }

    #[test]
    fn test_parse_csv_escape_backslash() {
        // \\ becomes single backslash
        let fields = parse_csv_row(r#""path\\to\\file""#).unwrap();
        assert_eq!(fields.len(), 1);
        assert_eq!(fields[0].value, r"path\to\file");
    }

    #[test]
    fn test_parse_csv_escape_quote() {
        // \" becomes quote (alternative to "")
        let fields = parse_csv_row(r#""say \"hello\"""#).unwrap();
        assert_eq!(fields.len(), 1);
        assert_eq!(fields[0].value, "say \"hello\"");
    }

    #[test]
    fn test_parse_csv_multiple_escapes() {
        // Multiple escape sequences in one field
        let fields = parse_csv_row(r#""line1\nline2\ttabbed""#).unwrap();
        assert_eq!(fields.len(), 1);
        assert!(fields[0].value.contains('\n'));
        assert!(fields[0].value.contains('\t'));
    }

    #[test]
    fn test_parse_csv_carriage_return_invalid() {
        // \r is NOT a valid escape sequence (only \", \\, \n, \t are allowed)
        let result = parse_csv_row(r#""windows\r\nline""#);
        assert!(result.is_err());
        assert!(matches!(
            result.unwrap_err(),
            LexError::InvalidEscape { .. }
        ));
    }

    // ==================== Additional edge cases ====================

    #[test]
    fn test_parse_csv_unicode_unquoted() {
        let fields = parse_csv_row("日本語, émoji, über").unwrap();
        assert_eq!(fields.len(), 3);
        assert_eq!(fields[0].value, "日本語");
        assert_eq!(fields[1].value, "émoji");
        assert_eq!(fields[2].value, "über");
    }

    #[test]
    fn test_parse_csv_unicode_quoted() {
        let fields = parse_csv_row(r#""日本語", "émoji 🎉", "über""#).unwrap();
        assert_eq!(fields.len(), 3);
        assert_eq!(fields[0].value, "日本語");
        assert!(fields[0].is_quoted);
        assert!(fields[1].value.contains('🎉'));
    }

    #[test]
    fn test_parse_csv_only_whitespace_between_commas() {
        let fields = parse_csv_row("a,   ,c").unwrap();
        assert_eq!(fields.len(), 3);
        assert_eq!(fields[0].value, "a");
        assert_eq!(fields[1].value, "");
        assert_eq!(fields[2].value, "c");
    }

    #[test]
    fn test_parse_csv_numbers() {
        let fields = parse_csv_row("123, 45.67, -89, 0").unwrap();
        assert_eq!(fields.len(), 4);
        assert_eq!(fields[0].value, "123");
        assert_eq!(fields[1].value, "45.67");
        assert_eq!(fields[2].value, "-89");
        assert_eq!(fields[3].value, "0");
    }

    #[test]
    fn test_parse_csv_booleans() {
        let fields = parse_csv_row("true, false, null").unwrap();
        assert_eq!(fields.len(), 3);
        assert_eq!(fields[0].value, "true");
        assert_eq!(fields[1].value, "false");
        assert_eq!(fields[2].value, "null");
    }

    #[test]
    fn test_parse_csv_references() {
        let fields = parse_csv_row("@User:123, @Post:456").unwrap();
        assert_eq!(fields.len(), 2);
        assert_eq!(fields[0].value, "@User:123");
        assert_eq!(fields[1].value, "@Post:456");
    }

    #[test]
    fn test_parse_csv_mixed_types() {
        let fields = parse_csv_row(r#"123, "hello", true, null, @Ref:1"#).unwrap();
        assert_eq!(fields.len(), 5);
        assert_eq!(fields[0].value, "123");
        assert_eq!(fields[1].value, "hello");
        assert!(fields[1].is_quoted);
        assert_eq!(fields[2].value, "true");
        assert_eq!(fields[3].value, "null");
        assert_eq!(fields[4].value, "@Ref:1");
    }

    #[test]
    fn test_parse_csv_many_fields() {
        let input = (0..20)
            .map(|i| i.to_string())
            .collect::<Vec<_>>()
            .join(", ");
        let fields = parse_csv_row(&input).unwrap();
        assert_eq!(fields.len(), 20);
        for (i, field) in fields.iter().enumerate() {
            assert_eq!(field.value, i.to_string());
        }
    }

    #[test]
    fn test_parse_csv_expression_nested_quotes() {
        // Expression with nested quoted strings
        let fields = parse_csv_row(r#"$(concat("a""b", "c")), other"#).unwrap();
        assert_eq!(fields.len(), 2);
        assert!(fields[0].value.starts_with("$("));
    }

    #[test]
    fn test_parse_csv_multiple_expressions() {
        let fields = parse_csv_row("$(a), $(b), $(c)").unwrap();
        assert_eq!(fields.len(), 3);
        assert_eq!(fields[0].value, "$(a)");
        assert_eq!(fields[1].value, "$(b)");
        assert_eq!(fields[2].value, "$(c)");
    }

    #[test]
    fn test_parse_csv_expression_with_nested_calls() {
        let fields = parse_csv_row("$(outer(inner(x))), other").unwrap();
        assert_eq!(fields.len(), 2);
        assert_eq!(fields[0].value, "$(outer(inner(x)))");
    }

    #[test]
    fn test_parse_csv_quoted_empty() {
        let fields = parse_csv_row(r#""""#).unwrap();
        assert_eq!(fields.len(), 1);
        assert_eq!(fields[0].value, "");
        assert!(fields[0].is_quoted);
    }

    #[test]
    fn test_parse_csv_quoted_only_spaces() {
        let fields = parse_csv_row(r#""   ""#).unwrap();
        assert_eq!(fields.len(), 1);
        assert_eq!(fields[0].value, "   ");
        assert!(fields[0].is_quoted);
    }

    #[test]
    fn test_parse_csv_unknown_escape_error() {
        // Unknown escape sequences return an error (only \", \\, \n, \t are allowed)
        let result = parse_csv_row(r#""\x""#);
        assert!(result.is_err());
        assert!(matches!(
            result.unwrap_err(),
            LexError::InvalidEscape { .. }
        ));
    }

    #[test]
    fn test_parse_csv_double_quotes_in_expression() {
        // Expression can contain escaped quotes
        let fields = parse_csv_row(r#"$(say("hello""world")), other"#).unwrap();
        assert_eq!(fields.len(), 2);
        assert!(fields[0].value.contains("hello\"\"world"));
    }

    #[test]
    fn test_parse_csv_special_chars_in_quotes() {
        let fields = parse_csv_row(r#""!@#$%^&*()[]{}|;:'<>?/""#).unwrap();
        assert_eq!(fields.len(), 1);
        assert!(fields[0].is_quoted);
        assert!(fields[0].value.contains('#'));
        assert!(fields[0].value.contains('!'));
    }

    #[test]
    fn test_parse_csv_comma_in_quoted() {
        let fields = parse_csv_row(r#""a,b,c""#).unwrap();
        assert_eq!(fields.len(), 1);
        assert!(fields[0].is_quoted);
        assert!(fields[0].value.contains(','));
        assert_eq!(fields[0].value, "a,b,c");
    }

    #[test]
    fn test_csv_field_equality() {
        let a = CsvField {
            value: "test".to_string(),
            is_quoted: true,
        };
        let b = CsvField {
            value: "test".to_string(),
            is_quoted: true,
        };
        let c = CsvField {
            value: "test".to_string(),
            is_quoted: false,
        };
        assert_eq!(a, b);
        assert_ne!(a, c);
    }

    #[test]
    fn test_csv_field_clone() {
        let original = CsvField {
            value: "test".to_string(),
            is_quoted: true,
        };
        let cloned = original.clone();
        assert_eq!(original, cloned);
    }

    #[test]
    fn test_csv_field_debug() {
        let field = CsvField {
            value: "test".to_string(),
            is_quoted: true,
        };
        let debug = format!("{:?}", field);
        assert!(debug.contains("test"));
        assert!(debug.contains("is_quoted"));
    }

    #[test]
    fn test_parse_csv_trailing_comma_with_spaces() {
        // Trailing comma after spaces should still error
        let result = parse_csv_row("a, b,   ");
        assert!(result.is_err());
    }

    #[test]
    fn test_parse_csv_leading_comma_error() {
        // Leading comma gives empty first field
        let fields = parse_csv_row(",a").unwrap();
        assert_eq!(fields.len(), 2);
        assert_eq!(fields[0].value, "");
        assert_eq!(fields[1].value, "a");
    }

    #[test]
    fn test_parse_csv_multiple_consecutive_commas() {
        let fields = parse_csv_row("a,,,b").unwrap();
        assert_eq!(fields.len(), 4);
        assert_eq!(fields[0].value, "a");
        assert_eq!(fields[1].value, "");
        assert_eq!(fields[2].value, "");
        assert_eq!(fields[3].value, "b");
    }

    #[test]
    fn test_parse_csv_tab_in_unquoted_field() {
        // Tab is whitespace, so it gets trimmed in unquoted field
        let fields = parse_csv_row("a\tb, c").unwrap();
        assert_eq!(fields.len(), 2);
        // "a\tb" gets trimmed at edges but tab in middle stays
        assert!(fields[0].value.contains('\t') || fields[0].value == "a\tb");
    }

    #[test]
    fn test_parse_csv_tab_in_quoted_field() {
        // Tab in quoted field is preserved
        let fields = parse_csv_row("\"a\tb\", c").unwrap();
        assert_eq!(fields.len(), 2);
        assert!(fields[0].value.contains('\t'));
    }

    #[test]
    fn test_parse_csv_very_long_field() {
        let long_value = "x".repeat(10000);
        let input = format!("\"{}\", other", long_value);
        let fields = parse_csv_row(&input).unwrap();
        assert_eq!(fields.len(), 2);
        assert_eq!(fields[0].value.len(), 10000);
    }

    #[test]
    fn test_parse_csv_all_valid_escapes_together() {
        // Only \", \\, \n, \t are valid escapes
        let fields = parse_csv_row(r#""line1\nline2\ttab\\path\"""#).unwrap();
        assert_eq!(fields.len(), 1);
        assert!(fields[0].value.contains('\n'));
        assert!(fields[0].value.contains('\t'));
        assert!(fields[0].value.contains('\\'));
        assert!(fields[0].value.contains('"'));
    }
}