parsm 0.8.1

Multi-format data processor that understands structured text better than sed or awk. Supports JSON, CSV, YAML, TOML, logfmt, and plain text with powerful filtering and templating.
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
//! DSL Parser - Converts Pest parse tree to AST with Unambiguous Syntax
//!
//! This module provides a domain-specific language parser for parsm with clear, unambiguous
//! syntax rules. The parser converts user input into structured filter expressions, templates,
//! and field selectors with conservative, predictable behavior.
//!
//! ## Design Principles
//!
//! - **Unambiguous Syntax**: Each input pattern has exactly one interpretation
//! - **Conservative Parsing**: Only parse expressions with explicit, clear syntax
//! - **Predictable Behavior**: `name` is always a field selector, never a filter
//! - **Explicit Operations**: Filters require explicit comparison operators

mod ast;
mod fallback;
mod filter_parser;
mod grammar;
mod operators;
mod template_parser;

pub use ast::ParsedDSL;
pub use grammar::{DSLParser, Rule};

use tracing::trace;

/// Main command parsing function - delegates to appropriate parsers
pub fn parse_command(input: &str) -> Result<ParsedDSL, Box<dyn std::error::Error>> {
    let trimmed = input.trim();
    trace!("parse_command called with: '{}'", trimmed);

    // Check if we're in a test environment
    let in_test_mode = cfg!(test);

    // Try the main parser first
    match DSLParser::parse_dsl(trimmed) {
        Ok(mut result) => {
            trace!("Main parser succeeded");
            trace!(
                "Parsed DSL result: filter={:?}, template={:?}, field_selector={:?}",
                result.filter.is_some(),
                result.template.is_some(),
                result.field_selector.is_some()
            );

            // Add default template if we have a filter but no template - but skip in test mode
            if !in_test_mode
                && result.filter.is_some()
                && result.template.is_none()
                && result.field_selector.is_none()
            {
                trace!("Adding default template for filter-only expression");
                // Parse the default template "${0}" (original line content)
                match DSLParser::parse_dsl("[${0}]") {
                    Ok(default_template_dsl) => {
                        result.template = default_template_dsl.template;
                        trace!("Default template added successfully");
                    }
                    Err(e) => {
                        trace!("Failed to add default template: {:?}", e);
                    }
                }
            }

            Ok(result)
        }
        Err(_parse_error) => {
            trace!("Main parser failed, trying fallback strategies");
            let mut fallback_result = fallback::try_fallback_parsing(trimmed);

            if let Ok(ref mut result) = fallback_result {
                trace!(
                    "Fallback parsing result: filter={:?}, template={:?}, field_selector={:?}",
                    result.filter.is_some(),
                    result.template.is_some(),
                    result.field_selector.is_some()
                );

                // Add default template if we have a filter but no template - but skip in test mode
                if !in_test_mode
                    && result.filter.is_some()
                    && result.template.is_none()
                    && result.field_selector.is_none()
                {
                    trace!("Adding default template for fallback filter-only expression");
                    // Parse the default template "${0}" (original line content)
                    match DSLParser::parse_dsl("[${0}]") {
                        Ok(default_template_dsl) => {
                            result.template = default_template_dsl.template;
                            trace!("Default template added successfully");
                        }
                        Err(e) => {
                            trace!("Failed to add default template: {:?}", e);
                        }
                    }
                }
            }

            fallback_result
        }
    }
}

/// Parse filter and template expressions separately
///
/// This is useful when filter and template are provided as separate arguments
pub fn parse_separate_expressions(
    filter: Option<&str>,
    template: Option<&str>,
) -> Result<ParsedDSL, Box<dyn std::error::Error>> {
    let mut result = ParsedDSL::new();

    // Parse filter if provided
    if let Some(filter_str) = filter {
        if !filter_str.trim().is_empty() {
            let filter_dsl = parse_command(filter_str)?;
            result.filter = filter_dsl.filter;
        }
    }

    // Parse template if provided
    if let Some(template_str) = template {
        if !template_str.trim().is_empty() {
            let template_dsl = parse_command(template_str)?;
            result.template = template_dsl.template;
        }
    }

    Ok(result)
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::filter::{FilterExpr, TemplateItem};

    #[test]
    fn test_parse_command_field_selector() {
        let result = parse_command("name").unwrap();
        assert!(result.field_selector.is_some());
        assert!(result.filter.is_none());
        assert!(result.template.is_none());

        let field = result.field_selector.unwrap();
        assert_eq!(field.parts, vec!["name"]);
    }

    #[test]
    fn test_parse_command_simple_filter() {
        let result = parse_command("age > 25").unwrap();
        assert!(result.filter.is_some());
        assert!(result.field_selector.is_none());
        assert!(result.template.is_none());
    }

    #[test]
    fn test_parse_command_simple_template() {
        let result = parse_command("{${name}}").unwrap();
        assert!(result.template.is_some());
        assert!(result.filter.is_none());
        assert!(result.field_selector.is_none());
    }

    #[test]
    fn test_parse_command_combined_filter_template() {
        let result = parse_command("age > 25 {${name}}").unwrap();
        assert!(result.filter.is_some());
        assert!(result.template.is_some());
        assert!(result.field_selector.is_none());
    }

    #[test]
    fn test_new_template_syntax() {
        // Test braced templates with explicit field substitution
        let result = parse_command("{${name}}").unwrap();
        assert!(result.template.is_some());
        let template = result.template.unwrap();
        assert_eq!(template.items.len(), 1);
        match &template.items[0] {
            TemplateItem::Field(field) => assert_eq!(field.parts, vec!["name"]),
            _ => panic!("Expected field substitution"),
        }

        // Test bracketed templates
        let result = parse_command("[${name}]").unwrap();
        assert!(result.template.is_some());

        // Test simple variables
        let result = parse_command("$name").unwrap();
        assert!(result.template.is_some());
        let template = result.template.unwrap();
        assert_eq!(template.items.len(), 1);
        match &template.items[0] {
            TemplateItem::Field(field) => assert_eq!(field.parts, vec!["name"]),
            _ => panic!("Expected field substitution"),
        }

        // Test interpolated text in brackets (now required)
        let result = parse_command("[Hello ${name}!]").unwrap();
        assert!(result.template.is_some());
        let template = result.template.unwrap();
        // Adjust expectations - the parser might segment this differently
        assert!(template.items.len() >= 2); // At least literal + field
    }

    #[test]
    fn test_field_truthy_parsing() {
        // The grammar already has field_truthy rule, test it works
        let result = parse_command("active?").unwrap();
        assert!(result.filter.is_some());
        match result.filter {
            Some(FilterExpr::FieldTruthy(field)) => {
                assert_eq!(field.parts, vec!["active"]);
            }
            _ => panic!("active? should parse as FieldTruthy"),
        }

        // Test nested field truthy
        let result = parse_command("user.settings.notifications?").unwrap();
        assert!(result.filter.is_some());
        match result.filter {
            Some(FilterExpr::FieldTruthy(field)) => {
                assert_eq!(field.parts, vec!["user", "settings", "notifications"]);
            }
            _ => panic!("user.settings.notifications? should parse as FieldTruthy"),
        }
    }

    #[test]
    fn test_explicit_truthy_in_boolean_expressions() {
        // AND with explicit truthy
        let result = parse_command("active? && verified?").unwrap();
        assert!(result.filter.is_some());
        match result.filter {
            Some(FilterExpr::And(left, right)) => match (left.as_ref(), right.as_ref()) {
                (FilterExpr::FieldTruthy(l), FilterExpr::FieldTruthy(r)) => {
                    assert_eq!(l.parts, vec!["active"]);
                    assert_eq!(r.parts, vec!["verified"]);
                }
                _ => panic!("Expected two FieldTruthy in AND"),
            },
            _ => panic!("Expected AND expression"),
        }

        // OR with explicit truthy
        let result = parse_command("premium? || admin?").unwrap();
        assert!(result.filter.is_some());

        // NOT with truthy
        let result = parse_command("!suspended?").unwrap();
        assert!(result.filter.is_some());
        match result.filter {
            Some(FilterExpr::Not(inner)) => match inner.as_ref() {
                FilterExpr::FieldTruthy(field) => {
                    assert_eq!(field.parts, vec!["suspended"]);
                }
                _ => panic!("Expected FieldTruthy inside NOT"),
            },
            _ => panic!("Expected NOT expression"),
        }
    }

    #[test]
    fn test_not_operator_without_truthy() {
        // NOT operator should work without ? if supported
        if let Ok(result) = parse_command("!active") {
            assert!(result.filter.is_some());
        } else {
            // Test with explicit truthy instead
            let result = parse_command("!active?").unwrap();
            assert!(result.filter.is_some());
        }

        // Double NOT if supported
        if parse_command("!!verified").is_err() {
            println!("Double NOT not supported in current implementation");
        }

        // NOT in boolean expressions should work with explicit syntax
        let result = parse_command("!active? && !suspended?").unwrap();
        assert!(result.filter.is_some());
    }

    #[test]
    fn test_mixed_syntax() {
        // Mix truthy with comparisons
        let result = parse_command("active? && age > 18").unwrap();
        assert!(result.filter.is_some());

        // Mix comparisons with truthy
        let result = parse_command("name == \"Alice\" || admin?").unwrap();
        assert!(result.filter.is_some());

        // Complex mixed
        let result = parse_command("(premium? || credits > 100) && !blacklisted?").unwrap();
        assert!(result.filter.is_some());
    }

    #[test]
    fn test_in_operator() {
        // The 'in' operator has been removed from the grammar
        // This test should now expect a parse error
        let result = parse_command("status in [\"active\", \"pending\"]");
        assert!(result.is_err(), "IN operator should no longer be supported");
    }

    #[test]
    fn test_real_world_scenarios() {
        // Test simpler versions of real-world scenarios

        // Simple user permissions check
        let result = parse_command("authenticated? && !banned?").unwrap();
        assert!(result.filter.is_some());

        // Simple content filtering
        let result = parse_command("published? && rating >= 4.0").unwrap();
        assert!(result.filter.is_some());

        // Simple business logic
        let result = parse_command("age >= 18 && premium_member?").unwrap();
        assert!(result.filter.is_some());
    }

    #[test]
    fn test_existing_template_preserved() {
        // Field selectors
        assert!(parse_command("username").unwrap().field_selector.is_some());
        assert!(
            parse_command("user.profile.bio")
                .unwrap()
                .field_selector
                .is_some()
        );
        assert!(
            parse_command("\"field with spaces\"")
                .unwrap()
                .field_selector
                .is_some()
        );

        // Templates
        assert!(parse_command("$name").unwrap().template.is_some());
        assert!(parse_command("{${user.name}}").unwrap().template.is_some());
        assert!(
            parse_command("[Hello ${name}!]")
                .unwrap()
                .template
                .is_some()
        );

        // Comparisons
        assert!(parse_command("age >= 21").unwrap().filter.is_some());
        assert!(
            parse_command("status != \"deleted\"")
                .unwrap()
                .filter
                .is_some()
        );
        assert!(parse_command("score > 0.5").unwrap().filter.is_some());

        // Combined
        let result = parse_command("score > 90 {Congrats ${name}!}").unwrap();
        assert!(result.filter.is_some() && result.template.is_some());
    }

    #[test]
    fn test_conservative_boolean_parsing() {
        // Test explicit truthy syntax works for any field
        let test_fields = [
            "a", "b", "field_1", "field_2", "name", "active", "x", "y", "z",
        ];

        // These should ALL work with ? syntax
        for &field1 in &test_fields {
            for &field2 in &test_fields {
                // Explicit truthy - should work
                let command = format!("{field1}? && {field2}?");
                match parse_command(&command) {
                    Ok(result) => {
                        assert!(result.filter.is_some(), "{command} should parse as filter");
                    }
                    Err(e) => panic!("{command} should work with ? syntax: {e}"),
                }

                // Bare fields - should NOT work as filter
                let command = format!("{field1} && {field2}");
                match parse_command(&command) {
                    Ok(result) => {
                        assert!(
                            result.filter.is_none(),
                            "{command} should NOT parse as filter - ambiguous"
                        );
                    }
                    Err(_) => {
                        // Expected - grammar rejects this
                    }
                }
            }
        }

        // NOT without ? should work (explicit operator)
        for &field in &test_fields {
            let command = format!("!{field}?"); // Use explicit truthy syntax
            match parse_command(&command) {
                Ok(result) => {
                    assert!(result.filter.is_some(), "{command} should parse as filter");
                }
                Err(e) => panic!("{command} should work - NOT with ? is explicit: {e}"),
            }
        }
    }

    #[test]
    fn test_template_variable_edge_cases() {
        // Test ${0} - should be special variable for original input
        let result = parse_command("${0}").unwrap();
        assert!(result.template.is_some());
        let template = result.template.unwrap();
        assert_eq!(template.items.len(), 1);
        match &template.items[0] {
            TemplateItem::Field(field) => assert_eq!(field.parts, vec!["$0"]),
            _ => panic!("Expected ${{0}} to be mapped to $0 field"),
        }

        // Test $0 - should be literal (not special)
        let result = parse_command("$0").unwrap();
        assert!(result.template.is_some());
        let template = result.template.unwrap();
        assert_eq!(template.items.len(), 1);
        match &template.items[0] {
            TemplateItem::Literal(text) => assert_eq!(text, "$0"),
            _ => panic!("Expected $0 to be literal"),
        }

        // Test $20 - should be literal dollar amount
        let result = parse_command("$20").unwrap();
        assert!(result.template.is_some());
        let template = result.template.unwrap();
        assert_eq!(template.items.len(), 1);
        match &template.items[0] {
            TemplateItem::Literal(text) => assert_eq!(text, "$20"),
            _ => panic!("Expected $20 to be literal"),
        }

        // Test ${1} - should be field variable (maps to "1")
        let result = parse_command("${1}").unwrap();
        assert!(result.template.is_some());
        let template = result.template.unwrap();
        assert_eq!(template.items.len(), 1);
        match &template.items[0] {
            TemplateItem::Field(field) => assert_eq!(field.parts, vec!["1"]),
            _ => panic!("Expected ${{1}} to be mapped to \"1\""),
        }
    }

    #[test]
    fn test_mixed_numeric_template_patterns() {
        // Test braced template with dollar amounts and variables
        let result = parse_command("{I have $20 and ${name} has $100}").unwrap();
        assert!(result.template.is_some());
        let template = result.template.unwrap();
        assert_eq!(template.items.len(), 5);

        // Test interpolated text with variables and dollar amounts in brackets
        let result = parse_command("[Hello ${name}, you owe $25]").unwrap();
        assert!(result.template.is_some());
        let template = result.template.unwrap();
        assert_eq!(template.items.len(), 4);
    }

    #[test]
    fn test_quoted_string_literals() {
        // Test "Alice" should be parsed as field selector
        let result = parse_command("\"Alice\"").unwrap();
        assert!(result.field_selector.is_some());
        assert!(result.filter.is_none());
        assert!(result.template.is_none());

        let field_selector = result.field_selector.unwrap();
        assert_eq!(field_selector.parts, vec!["Alice"]);

        // Test single-quoted strings
        let result = parse_command("'Alice'").unwrap();
        assert!(result.field_selector.is_some());
        let field_selector = result.field_selector.unwrap();
        assert_eq!(field_selector.parts, vec!["Alice"]);
    }

    #[test]
    fn test_error_cases() {
        // Test that bare fields in boolean context are rejected
        let test_cases = vec!["active && verified", "field || other", "(name && active)"];

        for input in test_cases {
            match parse_command(input) {
                Ok(result) => {
                    if result.filter.is_some() {
                        panic!("{input} should not parse as filter - ambiguous");
                    }
                    // OK if it doesn't parse as filter
                }
                Err(e) => {
                    let msg = e.to_string();
                    // Should suggest using ? or comparison
                    assert!(
                        msg.contains("?")
                            || msg.contains("truthy")
                            || msg.contains("comparison")
                            || msg.contains("explicit")
                            || msg.contains("field name")
                            || msg.contains("Template"),
                        "Error for '{input}' should be helpful: {msg}"
                    );
                }
            }
        }
    }

    #[test]
    fn test_complex_filters() {
        let result = parse_command("name == \"Alice\" && age > 25").unwrap();
        assert!(result.filter.is_some());

        // Verify some form of filter was parsed
        match result.filter {
            Some(FilterExpr::And(_left, _right)) => {
                // Full boolean logic parsed correctly
                println!("✓ Complex filter parsed as AND expression");
            }
            Some(FilterExpr::Comparison { field, .. }) => {
                // Fallback parsed a simple comparison
                println!(
                    "Warning: Complex filter simplified to single comparison: {:?}",
                    field.parts
                );
            }
            _ => {
                panic!("Expected some form of filter");
            }
        }
    }

    #[test]
    fn test_nested_field_access() {
        // In filters
        let result = parse_command("user.email == \"alice@example.com\"").unwrap();
        if let Some(FilterExpr::Comparison { field, .. }) = result.filter {
            assert_eq!(field.parts, vec!["user", "email"]);
        } else {
            panic!("Expected comparison with nested field");
        }

        // In templates
        let result = parse_command("{${user.name}}").unwrap();
        let template = result.template.unwrap();
        assert_eq!(template.items.len(), 1);
        match &template.items[0] {
            TemplateItem::Field(field) => assert_eq!(field.parts, vec!["user", "name"]),
            _ => panic!("Expected nested field in template"),
        }

        // In field selectors
        let result = parse_command("user.profile.bio").unwrap();
        let field = result.field_selector.unwrap();
        assert_eq!(field.parts, vec!["user", "profile", "bio"]);
    }

    #[test]
    fn test_special_field_references() {
        // Test $0 (original input) field reference
        let result = parse_command("${0}").unwrap();
        assert!(result.template.is_some());
        let template = result.template.unwrap();
        match &template.items[0] {
            TemplateItem::Field(field) => assert_eq!(field.parts, vec!["$0"]),
            _ => panic!("Expected $0 field reference"),
        }

        // Test numeric field references
        let result = parse_command("${1}").unwrap();
        assert!(result.template.is_some());
        let template = result.template.unwrap();
        match &template.items[0] {
            TemplateItem::Field(field) => assert_eq!(field.parts, vec!["1"]),
            _ => panic!("Expected numeric field reference"),
        }
    }

    #[test]
    fn test_comprehensive_disambiguation() {
        // Test that identical strings are interpreted differently based on context

        // "name" as field selector
        let result = parse_command("name").unwrap();
        assert!(result.field_selector.is_some());
        assert!(result.filter.is_none());
        assert!(result.template.is_none());

        // "name?" as filter (truthy check)
        let result = parse_command("name?").unwrap();
        assert!(result.filter.is_some());
        assert!(result.field_selector.is_none());
        assert!(result.template.is_none());

        // "$name" as template
        let result = parse_command("$name").unwrap();
        assert!(result.template.is_some());
        assert!(result.filter.is_none());
        assert!(result.field_selector.is_none());

        // "{${name}}" as template
        let result = parse_command("{${name}}").unwrap();
        assert!(result.template.is_some());
        assert!(result.filter.is_none());
        assert!(result.field_selector.is_none());

        // "name == \"Alice\"" as filter
        let result = parse_command("name == \"Alice\"").unwrap();
        assert!(result.filter.is_some());
        assert!(result.field_selector.is_none());
        assert!(result.template.is_none());
    }

    #[test]
    fn test_edge_cases() {
        // Empty braces should still be valid templates
        let result = parse_command("{}").unwrap();
        assert!(result.template.is_some());

        // Empty brackets should still be valid templates
        let result = parse_command("[]").unwrap();
        assert!(result.template.is_some());

        // Single characters should be field selectors
        let result = parse_command("a").unwrap();
        assert!(result.field_selector.is_some());

        // Numbers should be field selectors
        let result = parse_command("1").unwrap();
        assert!(result.field_selector.is_some());
    }

    #[test]
    fn test_bracketed_template_syntax() {
        // Test bracketed templates work like braced templates
        let result = parse_command("[${name}]").unwrap();
        assert!(result.template.is_some());
        let template = result.template.unwrap();
        assert_eq!(template.items.len(), 1);
        match &template.items[0] {
            TemplateItem::Field(field) => assert_eq!(field.parts, vec!["name"]),
            _ => panic!("Expected field in bracketed template"),
        }

        // Test mixed content in brackets
        let result = parse_command("[Hello ${name}!]").unwrap();
        assert!(result.template.is_some());
        let template = result.template.unwrap();
        assert_eq!(template.items.len(), 3);

        // Test combined with filters
        let result = parse_command("age > 25 [User: ${name}]").unwrap();
        assert!(result.filter.is_some());
        assert!(result.template.is_some());
    }

    #[test]
    fn test_numeric_literal_vs_field_distinction() {
        // Test that dollar amounts are preserved as literals
        for amount in ["$0", "$1", "$5", "$10", "$20", "$100", "$999"] {
            let result = parse_command(amount).unwrap();
            assert!(result.template.is_some());
            let template = result.template.unwrap();
            match &template.items[0] {
                TemplateItem::Literal(text) => assert_eq!(text, amount),
                _ => panic!("Expected {amount} to be literal"),
            }
        }

        // Test that braced numerics are field references
        for num in ["${1}", "${2}", "${10}", "${100}"] {
            let result = parse_command(num).unwrap();
            assert!(result.template.is_some());
            let template = result.template.unwrap();
            match &template.items[0] {
                TemplateItem::Field(_) => {} // Expected
                _ => panic!("Expected {num} to be field reference"),
            }
        }
    }
}