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
// 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 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.

//! Parser integration tests for list literals.
//!
//! Tests the integration of list literal parsing `(...)` into the full HEDL parser.

use hedl_core::{parse, Item, Value};

// ==================== Key-Value Pair Tests ====================

#[test]
fn test_parse_document_with_list_in_key_value_pair() {
    let input = br#"%VERSION: 1.1
---
roles: (admin, editor, viewer)
"#;
    let doc = parse(input).unwrap();

    // Verify roles is a List
    let roles = doc.root.get("roles").expect("roles key missing");
    match roles {
        Item::Scalar(Value::List(items)) => {
            assert_eq!(items.len(), 3);
            assert!(matches!(&items[0], Value::String(s) if s.as_ref() == "admin"));
            assert!(matches!(&items[1], Value::String(s) if s.as_ref() == "editor"));
            assert!(matches!(&items[2], Value::String(s) if s.as_ref() == "viewer"));
        }
        _ => panic!("Expected List, got {:?}", roles),
    }
}

#[test]
fn test_parse_document_with_empty_list() {
    let input = br#"%VERSION: 1.1
---
tags: ()
"#;
    let doc = parse(input).unwrap();

    // Verify tags is an empty List
    let tags = doc.root.get("tags").expect("tags key missing");
    match tags {
        Item::Scalar(Value::List(items)) => {
            assert!(items.is_empty(), "Expected empty list");
        }
        _ => panic!("Expected List, got {:?}", tags),
    }
}

#[test]
fn test_parse_document_with_bool_list() {
    let input = br#"%VERSION: 1.1
---
flags: (true, false, true)
"#;
    let doc = parse(input).unwrap();

    // Verify flags is a List of bools
    let flags = doc.root.get("flags").expect("flags key missing");
    match flags {
        Item::Scalar(Value::List(items)) => {
            assert_eq!(items.len(), 3);
            assert!(matches!(&items[0], Value::Bool(true)));
            assert!(matches!(&items[1], Value::Bool(false)));
            assert!(matches!(&items[2], Value::Bool(true)));
        }
        _ => panic!("Expected List, got {:?}", flags),
    }
}

#[test]
fn test_parse_document_with_int_list() {
    let input = br#"%VERSION: 1.1
---
numbers: (1, 2, 3, 42)
"#;
    let doc = parse(input).unwrap();

    // Verify numbers is a List of ints
    let numbers = doc.root.get("numbers").expect("numbers key missing");
    match numbers {
        Item::Scalar(Value::List(items)) => {
            assert_eq!(items.len(), 4);
            assert!(matches!(&items[0], Value::Int(1)));
            assert!(matches!(&items[1], Value::Int(2)));
            assert!(matches!(&items[2], Value::Int(3)));
            assert!(matches!(&items[3], Value::Int(42)));
        }
        _ => panic!("Expected List, got {:?}", numbers),
    }
}

#[test]
fn test_parse_document_with_mixed_type_list() {
    let input = br#"%VERSION: 1.1
---
mixed: (1, "two", true, ~)
"#;
    let doc = parse(input).unwrap();

    // Verify mixed contains int, string, bool, null
    let mixed = doc.root.get("mixed").expect("mixed key missing");
    match mixed {
        Item::Scalar(Value::List(items)) => {
            assert_eq!(items.len(), 4);
            assert!(matches!(&items[0], Value::Int(1)));
            assert!(matches!(&items[1], Value::String(s) if s.as_ref() == "two"));
            assert!(matches!(&items[2], Value::Bool(true)));
            assert!(matches!(&items[3], Value::Null));
        }
        _ => panic!("Expected List, got {:?}", mixed),
    }
}

#[test]
fn test_parse_document_with_nested_object_containing_list() {
    let input = br#"%VERSION: 1.1
---
user:
 name: Alice
 roles: (admin, editor)
"#;
    let doc = parse(input).unwrap();

    // Verify nested structure
    let user = doc.root.get("user").expect("user key missing");
    match user {
        Item::Object(obj) => {
            let name = obj.get("name").expect("name key missing");
            assert!(matches!(name, Item::Scalar(Value::String(s)) if s.as_ref() == "Alice"));

            let roles = obj.get("roles").expect("roles key missing");
            match roles {
                Item::Scalar(Value::List(items)) => {
                    assert_eq!(items.len(), 2);
                    assert!(matches!(&items[0], Value::String(s) if s.as_ref() == "admin"));
                    assert!(matches!(&items[1], Value::String(s) if s.as_ref() == "editor"));
                }
                _ => panic!("Expected List for roles"),
            }
        }
        _ => panic!("Expected Object for user"),
    }
}

#[test]
fn test_parse_document_with_list_and_tensor_distinguished() {
    let input = br#"%VERSION: 1.1
---
roles: (admin, editor)
weights: [0.5, 0.3, 0.2]
"#;
    let doc = parse(input).unwrap();

    // Verify roles is List
    let roles = doc.root.get("roles").expect("roles key missing");
    assert!(matches!(roles, Item::Scalar(Value::List(_))));

    // Verify weights is Tensor
    let weights = doc.root.get("weights").expect("weights key missing");
    assert!(matches!(weights, Item::Scalar(Value::Tensor(_))));
}

// ==================== Matrix Tests ====================

#[test]
fn test_parse_matrix_row_with_list_cell() {
    let input = br#"%VERSION: 1.1
%STRUCT: User: [id, name, roles]
---
users:@User
 |u1, Alice, (admin, editor)
 |u2, Bob, (viewer)
"#;
    let doc = parse(input).unwrap();

    // Verify matrix rows have List values
    let users = doc.root.get("users").expect("users key missing");
    match users {
        Item::List(matrix) => {
            assert_eq!(matrix.rows.len(), 2);

            // Check Alice's roles
            let alice = &matrix.rows[0];
            assert_eq!(alice.fields.len(), 3);
            match &alice.fields[2] {
                Value::List(roles) => {
                    assert_eq!(roles.len(), 2);
                    assert!(matches!(&roles[0], Value::String(s) if s.as_ref() == "admin"));
                    assert!(matches!(&roles[1], Value::String(s) if s.as_ref() == "editor"));
                }
                _ => panic!("Expected List for Alice's roles"),
            }

            // Check Bob's roles
            let bob = &matrix.rows[1];
            assert_eq!(bob.fields.len(), 3);
            match &bob.fields[2] {
                Value::List(roles) => {
                    assert_eq!(roles.len(), 1);
                    assert!(matches!(&roles[0], Value::String(s) if s.as_ref() == "viewer"));
                }
                _ => panic!("Expected List for Bob's roles"),
            }
        }
        _ => panic!("Expected List for users"),
    }
}

#[test]
fn test_parse_list_with_references() {
    let input = br#"%VERSION: 1.1
---
assignees: (@user1, @user2, @user3)
"#;
    let doc = parse(input).unwrap();

    // Verify list contains references
    let assignees = doc.root.get("assignees").expect("assignees key missing");
    match assignees {
        Item::Scalar(Value::List(items)) => {
            assert_eq!(items.len(), 3);
            for item in items.iter() {
                assert!(matches!(item, Value::Reference(_)));
            }
        }
        _ => panic!("Expected List, got {:?}", assignees),
    }
}

#[test]
fn test_parse_list_with_enum_directive_rejected() {
    // %ENUM was removed in v2.0
    let input = br#"%VERSION: 1.1
%ENUM: roles: {a:"admin", e:"editor", v:"viewer"}
---
user_roles: (a, e)
"#;
    let result = parse(input);
    assert!(result.is_err());
    assert!(result.unwrap_err().message.contains("removed"));
}

// ==================== Backward Compatibility Tests ====================

#[test]
fn test_parse_v10_document_still_works() {
    // Backward compatibility test
    let input = br#"%VERSION: 1.0
%STRUCT: User: [id, name]
---
users:@User
 |u1, Alice
 |u2, Bob
"#;
    let doc = parse(input).unwrap();

    // Verify v1.0 still parses correctly (version is preserved)
    assert_eq!(doc.version, (1, 0));
    let users = doc.root.get("users").expect("users key missing");
    match users {
        Item::List(matrix) => {
            assert_eq!(matrix.rows.len(), 2);
            assert_eq!(matrix.rows[0].fields.len(), 2);
            assert_eq!(matrix.rows[1].fields.len(), 2);
        }
        _ => panic!("Expected List for users"),
    }
}

// ==================== Edge Cases ====================

#[test]
fn test_parse_list_with_whitespace() {
    let input = br#"%VERSION: 1.1
---
items: ( a , b , c )
"#;
    let doc = parse(input).unwrap();

    let items = doc.root.get("items").expect("items key missing");
    match items {
        Item::Scalar(Value::List(list)) => {
            assert_eq!(list.len(), 3);
            assert!(matches!(&list[0], Value::String(s) if s.as_ref() == "a"));
            assert!(matches!(&list[1], Value::String(s) if s.as_ref() == "b"));
            assert!(matches!(&list[2], Value::String(s) if s.as_ref() == "c"));
        }
        _ => panic!("Expected List"),
    }
}

#[test]
fn test_parse_list_with_quoted_strings() {
    let input = br#"%VERSION: 1.1
---
messages: ("hello, world", "foo", "bar")
"#;
    let doc = parse(input).unwrap();

    let messages = doc.root.get("messages").expect("messages key missing");
    match messages {
        Item::Scalar(Value::List(list)) => {
            assert_eq!(list.len(), 3);
            assert!(matches!(&list[0], Value::String(s) if s.as_ref() == "hello, world"));
            assert!(matches!(&list[1], Value::String(s) if s.as_ref() == "foo"));
            assert!(matches!(&list[2], Value::String(s) if s.as_ref() == "bar"));
        }
        _ => panic!("Expected List"),
    }
}

#[test]
fn test_parse_list_single_element() {
    let input = br#"%VERSION: 1.1
---
single: (only)
"#;
    let doc = parse(input).unwrap();

    let single = doc.root.get("single").expect("single key missing");
    match single {
        Item::Scalar(Value::List(list)) => {
            assert_eq!(list.len(), 1);
            assert!(matches!(&list[0], Value::String(s) if s.as_ref() == "only"));
        }
        _ => panic!("Expected List"),
    }
}

#[test]
fn test_parse_list_with_expressions() {
    let input = br#"%VERSION: 1.1
---
calcs: ($(now()), $(x), $(concat(a, b)))
"#;
    let doc = parse(input).unwrap();

    let calcs = doc.root.get("calcs").expect("calcs key missing");
    match calcs {
        Item::Scalar(Value::List(list)) => {
            assert_eq!(list.len(), 3);
            assert!(matches!(&list[0], Value::Expression(_)));
            assert!(matches!(&list[1], Value::Expression(_)));
            assert!(matches!(&list[2], Value::Expression(_)));
        }
        _ => panic!("Expected List"),
    }
}

#[test]
fn test_parse_nested_lists_in_object() {
    let input = br#"%VERSION: 1.1
---
config:
 allowed: (read, write)
 denied: (delete, admin)
"#;
    let doc = parse(input).unwrap();

    let config = doc.root.get("config").expect("config key missing");
    match config {
        Item::Object(obj) => {
            let allowed = obj.get("allowed").expect("allowed key missing");
            assert!(matches!(allowed, Item::Scalar(Value::List(_))));

            let denied = obj.get("denied").expect("denied key missing");
            assert!(matches!(denied, Item::Scalar(Value::List(_))));
        }
        _ => panic!("Expected Object"),
    }
}

// ============================================================================
// NOTE: Nested list literals with parentheses like ((a, b), (c, d)) are NOT
// currently supported. The parser tracks parenthesis depth only for expressions.
// Lists CAN contain tensors which provide nested numeric arrays: ([1,2], [3,4])
// ============================================================================

// ==================== List in Matrix Tests ====================

#[test]
fn test_parse_matrix_with_list_and_tensor_cells() {
    let input = br#"%VERSION: 1.1
%STRUCT: Record: [id, tags, values]
---
data:@Record
 |r1, (tag1, tag2), [1, 2, 3]
 |r2, (tag3), [4, 5]
"#;
    let doc = parse(input).unwrap();

    let data = doc.root.get("data").expect("data key missing");
    match data {
        Item::List(matrix) => {
            assert_eq!(matrix.rows.len(), 2);

            // First row: list and tensor
            let row1 = &matrix.rows[0];
            assert!(matches!(&row1.fields[1], Value::List(tags) if tags.len() == 2));
            assert!(matches!(&row1.fields[2], Value::Tensor(_)));

            // Second row: list and tensor
            let row2 = &matrix.rows[1];
            assert!(matches!(&row2.fields[1], Value::List(tags) if tags.len() == 1));
            assert!(matches!(&row2.fields[2], Value::Tensor(_)));
        }
        _ => panic!("Expected List for data"),
    }
}

#[test]
fn test_parse_matrix_with_list_containing_tensors() {
    // Lists CAN contain tensors (nested numeric arrays)
    let input = br#"%VERSION: 1.1
%STRUCT: Record: [id, matrices]
---
data:@Record
 |r1, ([1, 2], [3, 4])
 |r2, ([5], [6, 7, 8])
"#;
    let doc = parse(input).unwrap();

    let data = doc.root.get("data").expect("data key missing");
    match data {
        Item::List(matrix) => {
            assert_eq!(matrix.rows.len(), 2);

            // First row: list containing two tensors
            match &matrix.rows[0].fields[1] {
                Value::List(items) => {
                    assert_eq!(items.len(), 2);
                    assert!(matches!(&items[0], Value::Tensor(_)));
                    assert!(matches!(&items[1], Value::Tensor(_)));
                }
                _ => panic!("Expected list of tensors in row 1"),
            }

            // Second row: list containing two tensors
            match &matrix.rows[1].fields[1] {
                Value::List(items) => {
                    assert_eq!(items.len(), 2);
                    assert!(matches!(&items[0], Value::Tensor(_)));
                    assert!(matches!(&items[1], Value::Tensor(_)));
                }
                _ => panic!("Expected list of tensors in row 2"),
            }
        }
        _ => panic!("Expected List for data"),
    }
}

// ==================== Empty String in List Tests ====================

#[test]
fn test_parse_list_with_empty_string_in_document() {
    let input = br#"%VERSION: 1.1
---
items: ("", filled, "")
"#;
    let doc = parse(input).unwrap();

    let items = doc.root.get("items").expect("items key missing");
    match items {
        Item::Scalar(Value::List(list)) => {
            assert_eq!(list.len(), 3);
            assert!(matches!(&list[0], Value::String(s) if s.is_empty()));
            assert!(matches!(&list[1], Value::String(s) if s.as_ref() == "filled"));
            assert!(matches!(&list[2], Value::String(s) if s.is_empty()));
        }
        _ => panic!("Expected List"),
    }
}

// ==================== Unicode in Lists Tests ====================

#[test]
fn test_parse_list_with_unicode_in_document() {
    let input = "%VERSION: 1.1\n---\nlanguages: (日本語, 中文, Русский)\n";
    let doc = parse(input.as_bytes()).unwrap();

    let languages = doc.root.get("languages").expect("languages key missing");
    match languages {
        Item::Scalar(Value::List(list)) => {
            assert_eq!(list.len(), 3);
            assert!(matches!(&list[0], Value::String(s) if s.as_ref() == "日本語"));
            assert!(matches!(&list[1], Value::String(s) if s.as_ref() == "中文"));
            assert!(matches!(&list[2], Value::String(s) if s.as_ref() == "Русский"));
        }
        _ => panic!("Expected List"),
    }
}

#[test]
fn test_parse_list_with_emoji_in_document() {
    let input = "%VERSION: 1.1\n---\nemojis: (😀, 🎉, 🚀)\n";
    let doc = parse(input.as_bytes()).unwrap();

    let emojis = doc.root.get("emojis").expect("emojis key missing");
    match emojis {
        Item::Scalar(Value::List(list)) => {
            assert_eq!(list.len(), 3);
            assert!(matches!(&list[0], Value::String(s) if s.as_ref() == "😀"));
            assert!(matches!(&list[1], Value::String(s) if s.as_ref() == "🎉"));
            assert!(matches!(&list[2], Value::String(s) if s.as_ref() == "🚀"));
        }
        _ => panic!("Expected List"),
    }
}

// ==================== Whitespace Handling Tests ====================

#[test]
fn test_parse_list_with_extensive_whitespace() {
    let input = br#"%VERSION: 1.1
---
spaced: (  a  ,  b  ,  c  )
"#;
    let doc = parse(input).unwrap();

    let spaced = doc.root.get("spaced").expect("spaced key missing");
    match spaced {
        Item::Scalar(Value::List(list)) => {
            assert_eq!(list.len(), 3);
            assert!(matches!(&list[0], Value::String(s) if s.as_ref() == "a"));
            assert!(matches!(&list[1], Value::String(s) if s.as_ref() == "b"));
            assert!(matches!(&list[2], Value::String(s) if s.as_ref() == "c"));
        }
        _ => panic!("Expected List"),
    }
}

// ==================== Error Cases ====================

#[test]
fn test_parse_unclosed_list_error() {
    let input = br#"%VERSION: 1.1
---
broken: (a, b, c
"#;
    let result = parse(input);
    assert!(result.is_err(), "Expected error for unclosed list");
    let err = result.unwrap_err();
    assert!(err.message.contains("unclosed") || err.message.contains("expected ')'"));
}

#[test]
fn test_parse_trailing_comma_in_list_error() {
    let input = br#"%VERSION: 1.1
---
bad: (a, b,)
"#;
    let result = parse(input);
    assert!(result.is_err(), "Expected error for trailing comma");
    let err = result.unwrap_err();
    assert!(err.message.contains("trailing comma") || err.message.contains("empty element"));
}

#[test]
fn test_parse_consecutive_commas_in_list_error() {
    let input = br#"%VERSION: 1.1
---
bad: (a,,b)
"#;
    let result = parse(input);
    assert!(result.is_err(), "Expected error for consecutive commas");
    let err = result.unwrap_err();
    assert!(err.message.contains("empty element") || err.message.contains("consecutive commas"));
}

// ==================== Multiple Lists in Document ====================

#[test]
fn test_parse_document_with_multiple_lists() {
    let input = br#"%VERSION: 1.1
---
permissions: (read, write, delete)
status_codes: (200, 404, 500)
flags: (true, false, true)
"#;
    let doc = parse(input).unwrap();

    // Verify all three lists parse correctly
    assert!(matches!(
        doc.root.get("permissions").unwrap(),
        Item::Scalar(Value::List(_))
    ));
    assert!(matches!(
        doc.root.get("status_codes").unwrap(),
        Item::Scalar(Value::List(_))
    ));
    assert!(matches!(
        doc.root.get("flags").unwrap(),
        Item::Scalar(Value::List(_))
    ));
}

#[test]
fn test_parse_list_preserves_value_types() {
    let input = br#"%VERSION: 1.1
---
mixed: (42, 3.5, true, false, ~, hello, @ref1)
"#;
    let doc = parse(input).unwrap();

    let mixed = doc.root.get("mixed").expect("mixed key missing");
    match mixed {
        Item::Scalar(Value::List(items)) => {
            assert_eq!(items.len(), 7);
            assert!(matches!(&items[0], Value::Int(42)));
            assert!(matches!(&items[1], Value::Float(f) if (*f - 3.5).abs() < 0.001));
            assert!(matches!(&items[2], Value::Bool(true)));
            assert!(matches!(&items[3], Value::Bool(false)));
            assert!(matches!(&items[4], Value::Null));
            assert!(matches!(&items[5], Value::String(s) if s.as_ref() == "hello"));
            assert!(matches!(&items[6], Value::Reference(_)));
        }
        _ => panic!("Expected List"),
    }
}