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
// 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 row parsing state machine for HEDL matrix rows.
//!
//! This module implements the normative CSV parsing algorithm from the HEDL spec,
//! handling quoted fields, escaped quotes, and expression regions.
//!
//! # Features
//!
//! - Quoted fields with `""` or `\"` escape for literal quotes
//! - Expression regions `$(...)` where commas don't delimit
//! - Tensor literals `[1, 2, 3]` where commas don't delimit
//! - Whitespace trimming for unquoted fields
//! - Escape sequences in quoted fields: `\n`, `\t`, `\r`, `\\`, `\"`
//! - Full UTF-8 support

use crate::lex::error::LexError;

/// A parsed CSV field with metadata.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct CsvField {
    /// The field value (unquoted, with escapes processed).
    pub value: String,
    /// Whether the field was enclosed in quotes.
    pub is_quoted: bool,
}

impl CsvField {
    /// Creates a field from a borrowed string slice.
    #[inline]
    fn from_borrowed(value: &str, is_quoted: bool) -> Self {
        Self {
            value: value.to_string(),
            is_quoted,
        }
    }

    /// Creates a field from an owned String.
    #[inline]
    fn from_owned(value: String, is_quoted: bool) -> Self {
        Self { value, is_quoted }
    }

    /// Returns `true` if the field value is empty.
    #[inline]
    pub fn is_empty(&self) -> bool {
        self.value.is_empty()
    }
}

impl AsRef<str> for CsvField {
    fn as_ref(&self) -> &str {
        &self.value
    }
}

impl std::fmt::Display for CsvField {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        if self.is_quoted {
            write!(f, "\"{}\"", self.value)
        } else {
            write!(f, "{}", self.value)
        }
    }
}

/// Parser state machine states.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
enum State {
    StartField,
    InUnquotedField,
    InQuotedField,
    AfterQuote,
    InExpression,
}

/// Optimized field finalization that minimizes allocations.
#[inline]
fn finalize_unquoted_field(mut field: String) -> Result<String, LexError> {
    let original_len = field.len();
    let trimmed = field.trim();

    // Check for quotes only outside of expression regions
    // Expressions like $(concat(")", x)) contain valid quotes
    if contains_quote_outside_expressions(trimmed) {
        return Err(LexError::QuoteInUnquotedField(trimmed.to_string()));
    }

    if trimmed.len() == original_len {
        Ok(field)
    } else if trimmed.is_empty() {
        field.clear();
        Ok(field)
    } else {
        Ok(trimmed.to_string())
    }
}

/// Check if a string contains quotes outside of `$(...)` expression regions.
fn contains_quote_outside_expressions(s: &str) -> bool {
    let mut in_expression = false;
    let mut in_expr_quotes = false;
    let mut expression_depth = 0;
    let chars: Vec<char> = s.chars().collect();
    let mut i = 0;

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

        if in_expression {
            if ch == '"' {
                // Check for escaped quote ("") within expression strings
                if in_expr_quotes && i + 1 < chars.len() && chars[i + 1] == '"' {
                    i += 2;
                    continue;
                }
                in_expr_quotes = !in_expr_quotes;
            } else if !in_expr_quotes {
                if ch == '(' {
                    expression_depth += 1;
                } else if ch == ')' {
                    expression_depth -= 1;
                    if expression_depth == 0 {
                        in_expression = false;
                        in_expr_quotes = false;
                    }
                }
            }
        } else {
            // Not in expression
            if ch == '"' {
                return true; // Found quote outside expression
            }
            if ch == '$' && i + 1 < chars.len() && chars[i + 1] == '(' {
                in_expression = true;
                expression_depth = 1;
                i += 1; // Skip the '('
            }
        }

        i += 1;
    }

    false
}

/// Parses a CSV string into a list of fields.
///
/// This implements the normative algorithm from the HEDL spec.
/// It handles:
/// - Quoted fields with `""` escape for literal quotes
/// - Expression regions `$(...)` where commas don't delimit
/// - Tensor literals `[...]` where commas don't delimit
/// - Whitespace trimming for unquoted fields
/// - Full UTF-8 support for multi-byte characters
/// - Escape sequences: `\n`, `\t`, `\\`, `\"` (only these are valid)
///
/// # Examples
///
/// ```
/// use hedl_core::lex::parse_csv_row;
///
/// // Simple fields
/// let fields = parse_csv_row("a, b, c").unwrap();
/// assert_eq!(fields.len(), 3);
/// assert_eq!(fields[0].value, "a");
///
/// // Quoted field with comma
/// let fields = parse_csv_row(r#""hello, world", other"#).unwrap();
/// assert_eq!(fields[0].value, "hello, world");
/// assert!(fields[0].is_quoted);
///
/// // Expression region
/// let fields = parse_csv_row("id, $(a, b), value").unwrap();
/// assert_eq!(fields[1].value, "$(a, b)");
///
/// // Tensor literal
/// let fields = parse_csv_row("id, [1, 2, 3]").unwrap();
/// assert_eq!(fields[1].value, "[1, 2, 3]");
/// ```
///
/// # Errors
///
/// Returns error for:
/// - Trailing comma
/// - Unclosed quoted string
/// - Unclosed expression
/// - Quote character in unquoted field
pub fn parse_csv_row(csv_string: &str) -> Result<Vec<CsvField>, LexError> {
    if csv_string.is_empty() {
        return Ok(Vec::new());
    }

    // Check for trailing comma
    if csv_string.trim_end().ends_with(',') {
        return Err(LexError::TrailingComma);
    }

    // Pre-allocate based on estimated field count
    let estimated_fields = csv_string.bytes().filter(|&b| b == b',').count() + 1;
    let mut fields = Vec::with_capacity(estimated_fields);

    let estimated_field_capacity = (csv_string.len() / estimated_fields.max(1)).max(16);
    let mut current_field = String::with_capacity(estimated_field_capacity);
    let mut _current_is_quoted = false;
    let mut state = State::StartField;
    let mut expression_depth: usize = 0;
    let mut bracket_depth: usize = 0;
    let mut paren_depth: usize = 0;
    let mut in_expression_quotes = false;

    let mut chars = csv_string.chars().peekable();

    while let Some(ch) = chars.next() {
        match state {
            State::StartField => {
                _current_is_quoted = false;
                if ch.is_ascii_whitespace() {
                    continue;
                } else if ch == ',' {
                    fields.push(CsvField::from_borrowed("", false));
                } else if ch == '"' {
                    _current_is_quoted = true;
                    state = State::InQuotedField;
                } else if ch == '$' && chars.peek() == Some(&'(') {
                    chars.next();
                    current_field.push_str("$(");
                    state = State::InExpression;
                    expression_depth = 1;
                } else if ch == '[' {
                    bracket_depth = 1;
                    current_field.push(ch);
                    state = State::InUnquotedField;
                } else if ch == '(' {
                    paren_depth = 1;
                    current_field.push(ch);
                    state = State::InUnquotedField;
                } else {
                    state = State::InUnquotedField;
                    current_field.push(ch);
                }
            }

            State::InUnquotedField => {
                if ch == '[' {
                    bracket_depth += 1;
                    current_field.push(ch);
                } else if ch == ']' {
                    bracket_depth = bracket_depth.saturating_sub(1);
                    current_field.push(ch);
                } else if ch == '(' {
                    paren_depth += 1;
                    current_field.push(ch);
                } else if ch == ')' {
                    paren_depth = paren_depth.saturating_sub(1);
                    current_field.push(ch);
                } else if ch == ',' && bracket_depth == 0 && paren_depth == 0 {
                    let value = finalize_unquoted_field(std::mem::take(&mut current_field))?;
                    fields.push(CsvField::from_owned(value, false));
                    bracket_depth = 0;
                    paren_depth = 0;
                    state = State::StartField;
                } else {
                    current_field.push(ch);
                }
            }

            State::InQuotedField => {
                if ch == '"' {
                    if chars.peek() == Some(&'"') {
                        chars.next();
                        current_field.push('"');
                    } else {
                        state = State::AfterQuote;
                    }
                } else if ch == '\\' {
                    if let Some(&next_ch) = chars.peek() {
                        match next_ch {
                            'n' => {
                                chars.next();
                                current_field.push('\n');
                            }
                            't' => {
                                chars.next();
                                current_field.push('\t');
                            }
                            '\\' => {
                                chars.next();
                                current_field.push('\\');
                            }
                            '"' => {
                                chars.next();
                                current_field.push('"');
                            }
                            _ => {
                                // Only \", \\, \n, \t are valid escapes
                                return Err(LexError::InvalidEscape {
                                    sequence: format!("\\{}", next_ch),
                                    pos: crate::lex::error::SourcePos::default(),
                                });
                            }
                        }
                    } else {
                        current_field.push(ch);
                    }
                } else {
                    current_field.push(ch);
                }
            }

            State::AfterQuote => {
                if ch.is_ascii_whitespace() {
                    continue;
                } else if ch == ',' {
                    fields.push(CsvField::from_owned(
                        std::mem::take(&mut current_field),
                        true,
                    ));
                    state = State::StartField;
                } else {
                    return Err(LexError::ExpectedCommaAfterQuote(ch));
                }
            }

            State::InExpression => {
                current_field.push(ch);

                if ch == '"' {
                    // Check for escaped quote ("") within expression strings
                    if in_expression_quotes && chars.peek() == Some(&'"') {
                        chars.next();
                        current_field.push('"');
                    } else {
                        in_expression_quotes = !in_expression_quotes;
                    }
                } else if !in_expression_quotes {
                    // Only count parens when not inside a quoted string
                    if ch == '(' {
                        expression_depth += 1;
                    } else if ch == ')' {
                        expression_depth = expression_depth.saturating_sub(1);
                        if expression_depth == 0 {
                            in_expression_quotes = false; // Reset for next expression
                            state = State::InUnquotedField;
                        }
                    }
                }
            }
        }
    }

    // Handle end of string
    match state {
        State::InQuotedField => {
            return Err(LexError::UnclosedQuote {
                pos: crate::lex::error::SourcePos::default(),
            });
        }
        State::InExpression => {
            return Err(LexError::UnclosedExpression {
                pos: crate::lex::error::SourcePos::default(),
            });
        }
        State::AfterQuote => {
            fields.push(CsvField::from_owned(current_field, true));
        }
        State::InUnquotedField | State::StartField => {
            if !current_field.is_empty() || state == State::InUnquotedField {
                let value = finalize_unquoted_field(current_field)?;
                fields.push(CsvField::from_owned(value, false));
            }
        }
    }

    Ok(fields)
}

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

    // ==================== Basic field tests ====================

    #[test]
    fn test_simple_fields() {
        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_single_field() {
        let fields = parse_csv_row("hello").unwrap();
        assert_eq!(fields.len(), 1);
        assert_eq!(fields[0].value, "hello");
    }

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

    // ==================== Quoted field tests ====================

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

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

    #[test]
    fn test_backslash_escapes() {
        let fields = parse_csv_row(r#""line1\nline2""#).unwrap();
        assert_eq!(fields[0].value, "line1\nline2");

        let fields = parse_csv_row(r#""col1\tcol2""#).unwrap();
        assert_eq!(fields[0].value, "col1\tcol2");

        let fields = parse_csv_row(r#""path\\file""#).unwrap();
        assert_eq!(fields[0].value, "path\\file");

        let fields = parse_csv_row(r#""say \"hi\"""#).unwrap();
        assert_eq!(fields[0].value, "say \"hi\"");
    }

    // ==================== Expression tests ====================

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

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

    #[test]
    fn test_expression_with_quoted_paren() {
        // Test that quoted parens in expressions don't break parsing
        // This was a bug: $(")") would prematurely close at the first )
        let fields = parse_csv_row(r#"$(")"), next"#).unwrap();
        assert_eq!(fields.len(), 2);
        assert_eq!(fields[0].value, r#"$(")")"#);
        assert_eq!(fields[1].value, "next");
    }

    #[test]
    fn test_expression_with_quoted_string_containing_parens() {
        // More complex case: $(concat(")", x))
        let fields = parse_csv_row(r#"$(concat(")", x))"#).unwrap();
        assert_eq!(fields[0].value, r#"$(concat(")", x))"#);
    }

    #[test]
    fn test_expression_with_escaped_quote() {
        // Test escaped quotes within expression strings: $("a""b")
        let fields = parse_csv_row(r#"$("a""b")"#).unwrap();
        assert_eq!(fields[0].value, r#"$("a""b")"#);
    }

    #[test]
    fn test_expression_with_multiple_quoted_strings() {
        // Multiple quoted strings in one expression
        let fields = parse_csv_row(r#"$(concat("(", ")"))"#).unwrap();
        assert_eq!(fields[0].value, r#"$(concat("(", ")"))"#);
    }

    // ==================== Tensor literal tests ====================

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

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

    // ==================== Empty field tests ====================

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

    // ==================== Error tests ====================

    #[test]
    fn test_trailing_comma_error() {
        assert!(matches!(
            parse_csv_row("a, b,"),
            Err(LexError::TrailingComma)
        ));
    }

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

    #[test]
    fn test_unclosed_expression_error() {
        assert!(matches!(
            parse_csv_row("$(unclosed"),
            Err(LexError::UnclosedExpression { .. })
        ));
    }

    #[test]
    fn test_quote_in_unquoted_error() {
        assert!(matches!(
            parse_csv_row(r#"hello"world"#),
            Err(LexError::QuoteInUnquotedField(_))
        ));
    }

    // ==================== Unicode tests ====================

    #[test]
    fn test_unicode() {
        let fields = parse_csv_row("hello, wörld, 日本語").unwrap();
        assert_eq!(fields[0].value, "hello");
        assert_eq!(fields[1].value, "wörld");
        assert_eq!(fields[2].value, "日本語");
    }

    // ==================== List literal tests ====================

    #[test]
    fn test_list_literal() {
        let fields = parse_csv_row("id, (admin, editor), value").unwrap();
        assert_eq!(fields.len(), 3);
        assert_eq!(fields[1].value, "(admin, editor)");
    }

    #[test]
    fn test_list_literal_with_references() {
        let fields = parse_csv_row("id, (@user1, @user2), value").unwrap();
        assert_eq!(fields.len(), 3);
        assert_eq!(fields[1].value, "(@user1, @user2)");
    }

    #[test]
    fn test_empty_list() {
        let fields = parse_csv_row("id, (), value").unwrap();
        assert_eq!(fields.len(), 3);
        assert_eq!(fields[1].value, "()");
    }

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

    // ==================== CsvField tests ====================

    #[test]
    fn test_csv_field_is_empty() {
        let field = CsvField::from_borrowed("", false);
        assert!(field.is_empty());

        let field = CsvField::from_borrowed("hello", false);
        assert!(!field.is_empty());
    }

    #[test]
    fn test_csv_field_as_ref() {
        let field = CsvField::from_borrowed("hello", false);
        let s: &str = field.as_ref();
        assert_eq!(s, "hello");
    }

    #[test]
    fn test_csv_field_display() {
        let field = CsvField::from_borrowed("hello", false);
        assert_eq!(format!("{}", field), "hello");

        let field = CsvField::from_borrowed("hello", true);
        assert_eq!(format!("{}", field), "\"hello\"");
    }
}