yamp 0.1.0

Yet Another Minimal Parser - A safe, predictable YAML parser that treats all values as strings
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
use crate::lexer::{Lexer, Token, TokenKind};
use std::borrow::Cow;
use std::collections::BTreeMap;

#[derive(Debug, Clone, Copy)]
enum ChompMode {
    Strip, // - remove trailing newlines
    Clip,  // default - single newline
    Keep,  // + keep all trailing newlines
}

#[derive(Debug, Clone, PartialEq)]
pub enum YamlValue<'a> {
    String(Cow<'a, str>),
    Array(Vec<YamlNode<'a>>),
    Object(BTreeMap<Cow<'a, str>, YamlNode<'a>>),
}

#[derive(Debug, Clone, PartialEq)]
pub struct YamlNode<'a> {
    pub value: YamlValue<'a>,
    pub leading_comment: Option<Cow<'a, str>>,
    pub inline_comment: Option<Cow<'a, str>>,
}

impl<'a> YamlNode<'a> {
    pub(crate) fn new(value: YamlValue<'a>) -> Self {
        YamlNode {
            value,
            leading_comment: None,
            inline_comment: None,
        }
    }

    pub(crate) fn with_comments(
        value: YamlValue<'a>,
        leading: Option<Cow<'a, str>>,
        inline: Option<Cow<'a, str>>,
    ) -> Self {
        YamlNode {
            value,
            leading_comment: leading,
            inline_comment: inline,
        }
    }

    // Public constructor for external use
    pub fn from_value(value: YamlValue<'a>) -> Self {
        YamlNode {
            value,
            leading_comment: None,
            inline_comment: None,
        }
    }

    // Helper methods for ergonomic value access

    /// Returns the string value if this node contains a string
    pub fn as_str(&self) -> Option<&str> {
        match &self.value {
            YamlValue::String(s) => Some(s.as_ref()),
            _ => None,
        }
    }

    /// Returns the object map if this node contains an object
    pub fn as_object(&self) -> Option<&BTreeMap<Cow<'a, str>, YamlNode<'a>>> {
        match &self.value {
            YamlValue::Object(map) => Some(map),
            _ => None,
        }
    }

    /// Returns the array items if this node contains an array
    pub fn as_array(&self) -> Option<&[YamlNode<'a>]> {
        match &self.value {
            YamlValue::Array(items) => Some(items),
            _ => None,
        }
    }

    /// Gets a child node by key if this node is an object
    pub fn get(&self, key: &str) -> Option<&YamlNode<'a>> {
        match &self.value {
            YamlValue::Object(map) => {
                // Try to find the key in the map
                for (k, v) in map.iter() {
                    if k.as_ref() == key {
                        return Some(v);
                    }
                }
                None
            }
            _ => None,
        }
    }

    /// Returns true if this node is a string
    pub fn is_string(&self) -> bool {
        matches!(&self.value, YamlValue::String(_))
    }

    /// Returns true if this node is an object
    pub fn is_object(&self) -> bool {
        matches!(&self.value, YamlValue::Object(_))
    }

    /// Returns true if this node is an array
    pub fn is_array(&self) -> bool {
        matches!(&self.value, YamlValue::Array(_))
    }
}

pub(crate) struct Parser<'g> {
    tokens: Vec<Token<'g>>,
    current: usize,
}

impl<'g> Parser<'g> {
    pub(crate) fn new(source: &'g str) -> Self {
        let mut lexer = Lexer::new(source);
        let tokens = lexer.tokenize();
        Parser { tokens, current: 0 }
    }

    pub(crate) fn parse(&mut self) -> Result<YamlNode<'g>, String> {
        self.skip_whitespace_and_newlines();
        let result = self.parse_value(0)?;
        Ok(result)
    }

    fn current_token(&self) -> Option<&Token<'g>> {
        self.tokens.get(self.current)
    }

    fn advance(&mut self) -> Option<&Token<'g>> {
        if self.current < self.tokens.len() {
            let token = &self.tokens[self.current];
            self.current += 1;
            Some(token)
        } else {
            None
        }
    }

    fn skip_whitespace(&mut self) {
        while let Some(token) = self.current_token() {
            if token.kind != TokenKind::Whitespace {
                break;
            }
            self.advance();
        }
    }

    fn skip_whitespace_and_newlines(&mut self) {
        while let Some(token) = self.current_token() {
            match token.kind {
                TokenKind::Whitespace
                | TokenKind::NewLine
                | TokenKind::Indent
                | TokenKind::Dedent => {
                    self.advance();
                }
                TokenKind::Identifier
                | TokenKind::Colon
                | TokenKind::String
                | TokenKind::Hyphen
                | TokenKind::Comment
                | TokenKind::Pipe
                | TokenKind::GreaterThan => break,
            }
        }
    }

    fn collect_comment(&mut self) -> Option<Cow<'g, str>> {
        self.skip_whitespace();
        if let Some(token) = self.current_token()
            && token.kind == TokenKind::Comment
        {
            let comment = token.text.trim_start_matches('#').trim();
            self.advance();
            return Some(Cow::Borrowed(comment));
        }
        None
    }

    fn parse_value(&mut self, min_indent: usize) -> Result<YamlNode<'g>, String> {
        self.skip_whitespace();

        // Collect leading comment if it's on a line by itself
        let mut leading_comment: Option<Cow<'g, str>> = None;
        if let Some(token) = self.current_token()
            && token.kind == TokenKind::Comment
        {
            leading_comment = Some(Cow::Borrowed(token.text.trim_start_matches('#').trim()));
            self.advance();
            self.skip_whitespace_and_newlines();
        }

        let token = self
            .current_token()
            .ok_or_else(|| "Unexpected end of input".to_string())?;

        let node = match token.kind {
            TokenKind::Hyphen => {
                let value = self.parse_array(min_indent)?;
                YamlNode::new(value)
            }
            TokenKind::Identifier => {
                let text = token.text;
                self.advance();

                self.skip_whitespace();
                if let Some(next) = self.current_token()
                    && next.kind == TokenKind::Colon
                {
                    self.current -= 1; // Back up
                    return self.parse_object(min_indent);
                }

                // It's a scalar value - always treat as string
                YamlNode::new(YamlValue::String(Cow::Borrowed(text)))
            }
            TokenKind::String => {
                let text = token.text;
                let content = if text.starts_with('"') || text.starts_with('\'') {
                    &text[1..text.len() - 1]
                } else {
                    text
                };
                self.advance();
                YamlNode::new(YamlValue::String(Cow::Borrowed(content)))
            }
            TokenKind::Whitespace
            | TokenKind::NewLine
            | TokenKind::Colon
            | TokenKind::Comment
            | TokenKind::Indent
            | TokenKind::Dedent
            | TokenKind::Pipe
            | TokenKind::GreaterThan => {
                return Err(format!("Unexpected token: {:?}", token.kind));
            }
        };

        let inline_comment = self.collect_comment();

        Ok(YamlNode::with_comments(
            node.value,
            leading_comment,
            inline_comment,
        ))
    }

    fn parse_inline_value(&mut self) -> Result<YamlNode<'g>, String> {
        // Collect tokens until we hit a newline or comment
        let start_token = self
            .current_token()
            .ok_or_else(|| "Expected value".to_string())?;

        // Check for special single-token values first
        match start_token.kind {
            TokenKind::String => {
                let text = start_token.text;
                let content = if text.starts_with('"') || text.starts_with('\'') {
                    &text[1..text.len() - 1]
                } else {
                    text
                };
                self.advance();
                let inline_comment = self.collect_comment();
                return Ok(YamlNode::with_comments(
                    YamlValue::String(Cow::Borrowed(content)),
                    None,
                    inline_comment,
                ));
            }
            TokenKind::Identifier
            | TokenKind::Colon
            | TokenKind::Whitespace
            | TokenKind::NewLine
            | TokenKind::Hyphen
            | TokenKind::Comment
            | TokenKind::Indent
            | TokenKind::Dedent
            | TokenKind::Pipe
            | TokenKind::GreaterThan => {}
        }

        // Otherwise collect all tokens until newline or comment
        let mut value_parts = Vec::with_capacity(4); // Most values are 1-4 tokens
        let mut single_token_text: Option<&'g str> = None;

        while let Some(token) = self.current_token() {
            match token.kind {
                TokenKind::NewLine | TokenKind::Comment => break,
                TokenKind::Whitespace => {
                    value_parts.push(" ");
                    self.advance();
                }
                TokenKind::Identifier
                | TokenKind::Colon
                | TokenKind::String
                | TokenKind::Hyphen
                | TokenKind::Indent
                | TokenKind::Dedent
                | TokenKind::Pipe
                | TokenKind::GreaterThan => {
                    if value_parts.is_empty() && single_token_text.is_none() {
                        single_token_text = Some(token.text);
                    }
                    value_parts.push(token.text);
                    self.advance();
                }
            }
        }

        // Trim trailing whitespace from value_parts
        while value_parts.last() == Some(&" ") {
            value_parts.pop();
        }

        // Everything is a string now
        let value = if let Some(text) = single_token_text.filter(|_| value_parts.len() == 1) {
            YamlValue::String(Cow::Borrowed(text))
        } else {
            // For multi-token values, join them
            let value_str = value_parts.join("");
            YamlValue::String(Cow::Owned(value_str))
        };

        let inline_comment = self.collect_comment();

        Ok(YamlNode::with_comments(value, None, inline_comment))
    }

    fn parse_array(&mut self, min_indent: usize) -> Result<YamlValue<'g>, String> {
        let mut items = Vec::new();

        while let Some(token) = self.current_token() {
            if token.kind == TokenKind::Hyphen {
                self.advance(); // consume hyphen
                self.skip_whitespace();

                let item = self.parse_value(min_indent)?;
                items.push(item);

                self.skip_whitespace();
                if let Some(token) = self.current_token() {
                    if token.kind == TokenKind::NewLine {
                        self.advance();
                        self.skip_whitespace_and_newlines();
                    } else if token.kind != TokenKind::Hyphen {
                        break;
                    }
                }
            } else {
                break;
            }
        }

        Ok(YamlValue::Array(items))
    }

    fn parse_multiline_string(
        &mut self,
        base_indent: usize,
        is_literal: bool,
    ) -> Result<YamlNode<'g>, String> {
        // Skip any remaining whitespace and comments on the same line
        self.skip_whitespace();

        // Handle optional chomping indicator (-, +, or none)
        let mut chomp_mode = ChompMode::Clip; // default
        if let Some(token) = self.current_token() {
            match token.text {
                "-" => {
                    chomp_mode = ChompMode::Strip;
                    self.advance();
                }
                "+" => {
                    chomp_mode = ChompMode::Keep;
                    self.advance();
                }
                _ => {}
            }
        }

        // Skip to next line
        while let Some(token) = self.current_token() {
            if token.kind == TokenKind::NewLine {
                self.advance();
                break;
            }
            // Skip any other tokens (comments, etc.)
            self.advance();
        }

        let mut lines = Vec::new();
        let mut content_indent = None;

        // Collect all lines that are more indented than base_indent
        while let Some(token) = self.current_token() {
            // Check if we've dedented back to or past the base level
            if token.kind == TokenKind::Dedent {
                // Check the next non-whitespace token's column
                let mut peek_index = self.current + 1;
                while peek_index < self.tokens.len() {
                    let peek_token = &self.tokens[peek_index];
                    if peek_token.kind != TokenKind::Whitespace
                        && peek_token.kind != TokenKind::Indent
                        && peek_token.kind != TokenKind::Dedent
                    {
                        if peek_token.column <= base_indent {
                            break;
                        }
                        break;
                    }
                    peek_index += 1;
                }
                if peek_index < self.tokens.len() && self.tokens[peek_index].column <= base_indent {
                    break;
                }
            }

            // Skip whitespace but track indentation
            if token.kind == TokenKind::Whitespace || token.kind == TokenKind::Indent {
                self.advance();
                continue;
            }

            // If it's a newline, add an empty line
            if token.kind == TokenKind::NewLine {
                lines.push("");
                self.advance();
                continue;
            }

            // Check indentation
            if token.column <= base_indent {
                break;
            }

            // Set content indent from first content line
            if content_indent.is_none() {
                content_indent = Some(token.column);
            }

            // Collect the line
            let _line_start = self.current;
            let mut line_text = String::new();

            while let Some(token) = self.current_token() {
                if token.kind == TokenKind::NewLine {
                    break;
                }

                // For literal mode, preserve everything
                // For folded mode, we'll process later
                line_text.push_str(token.text);
                self.advance();
            }

            lines.push(line_text.leak()); // Convert to &'static str for simplicity

            if let Some(token) = self.current_token()
                && token.kind == TokenKind::NewLine
            {
                self.advance();
            }
        }

        // Process the lines based on mode
        let result = if is_literal {
            // Literal mode: preserve line breaks
            let mut result = lines.join("\n");

            // Apply chomping
            match chomp_mode {
                ChompMode::Strip => {
                    // Remove all trailing newlines
                    while result.ends_with('\n') {
                        result.pop();
                    }
                }
                ChompMode::Clip => {
                    // Keep single trailing newline (default)
                    while result.ends_with("\n\n") {
                        result.pop();
                    }
                    if !result.ends_with('\n') && !result.is_empty() {
                        result.push('\n');
                    }
                }
                ChompMode::Keep => {
                    // Keep all trailing newlines
                    result.push('\n');
                }
            }

            result
        } else {
            // Folded mode: fold lines together
            let mut result = String::new();
            let mut prev_empty = false;

            for (i, line) in lines.iter().enumerate() {
                if line.is_empty() {
                    if !prev_empty && i > 0 {
                        result.push('\n');
                    }
                    prev_empty = true;
                } else {
                    if i > 0 && !prev_empty {
                        result.push(' ');
                    }
                    result.push_str(line.trim_start());
                    prev_empty = false;
                }
            }

            // Apply chomping
            match chomp_mode {
                ChompMode::Strip => {
                    while result.ends_with('\n') || result.ends_with(' ') {
                        result.pop();
                    }
                }
                ChompMode::Clip => {
                    while result.ends_with('\n') || result.ends_with(' ') {
                        result.pop();
                    }
                    // Add single trailing newline for Clip mode
                    if !result.is_empty() {
                        result.push('\n');
                    }
                }
                ChompMode::Keep => {
                    // Keep trailing whitespace
                    if !result.is_empty() && !result.ends_with('\n') {
                        result.push('\n');
                    }
                }
            }

            result
        };

        Ok(YamlNode::new(YamlValue::String(Cow::Owned(result))))
    }

    fn parse_object(&mut self, min_indent: usize) -> Result<YamlNode<'g>, String> {
        let mut map = BTreeMap::new();

        while let Some(token) = self.current_token() {
            if token.kind != TokenKind::Identifier {
                break;
            }

            // Check if this key is at the right indentation level
            // If we're in a nested object, keys should be more indented than min_indent
            if min_indent > 0 && token.column <= min_indent {
                break;
            }

            let key_column = token.column;
            let key = Cow::Borrowed(token.text);
            self.advance();

            self.skip_whitespace();

            // Early return if no colon found
            let Some(token) = self.current_token() else {
                return Err("Expected colon after key".to_string());
            };
            if token.kind != TokenKind::Colon {
                return Err(format!("Expected colon after key, got {:?}", token.kind));
            }
            self.advance();

            self.skip_whitespace();

            // Skip whitespace after colon
            self.skip_whitespace();

            // Collect the value - could be multiple tokens on the same line
            let Some(token) = self.current_token() else {
                return Err("Expected value after colon".to_string());
            };

            let value = if token.kind == TokenKind::Pipe || token.kind == TokenKind::GreaterThan {
                // Multiline string indicator
                let is_literal = token.kind == TokenKind::Pipe;
                self.advance(); // consume | or >
                self.parse_multiline_string(key_column, is_literal)?
            } else if token.kind == TokenKind::NewLine || token.kind == TokenKind::Indent {
                // Value is on next line
                self.skip_whitespace_and_newlines();
                // Use key_column as the new min_indent for nested values
                self.parse_value(key_column)?
            } else {
                // Value is on same line - collect until newline
                self.parse_inline_value()?
            };

            map.insert(key, value);

            self.skip_whitespace();
            if let Some(token) = self.current_token()
                && token.kind == TokenKind::NewLine
            {
                self.advance();
                self.skip_whitespace_and_newlines();
            }

            // Check if we've dedented or reached end
            if let Some(token) = self.current_token()
                && token.kind == TokenKind::Dedent
            {
                self.advance();
                break;
            }
        }

        Ok(YamlNode::new(YamlValue::Object(map)))
    }
}

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

    #[test]
    fn test_parse_simple_object() {
        let yaml = "name: John\nage: 30";
        let mut parser = Parser::new(yaml);
        let result = parser.parse().unwrap();

        if let YamlValue::Object(map) = &result.value {
            assert_eq!(map.len(), 2);

            let name_node = map.get(&Cow::Borrowed("name")).unwrap();
            assert_eq!(name_node.value, YamlValue::String(Cow::Borrowed("John")));

            let age_node = map.get(&Cow::Borrowed("age")).unwrap();
            assert_eq!(age_node.value, YamlValue::String(Cow::Borrowed("30")));
        } else {
            panic!("Expected object");
        }
    }

    #[test]
    fn test_parse_array() {
        let yaml = "- apple\n- banana\n- cherry";
        let mut parser = Parser::new(yaml);
        let result = parser.parse().unwrap();

        if let YamlValue::Array(items) = &result.value {
            assert_eq!(items.len(), 3);
            assert_eq!(items[0].value, YamlValue::String(Cow::Borrowed("apple")));
            assert_eq!(items[1].value, YamlValue::String(Cow::Borrowed("banana")));
            assert_eq!(items[2].value, YamlValue::String(Cow::Borrowed("cherry")));
        } else {
            panic!("Expected array");
        }
    }

    #[test]
    fn test_parse_with_comments() {
        let yaml = "name: John # inline comment\nage: 30";
        let mut parser = Parser::new(yaml);
        let result = parser.parse().unwrap();

        if let YamlValue::Object(map) = &result.value {
            let name_node = map.get(&Cow::Borrowed("name")).unwrap();
            assert_eq!(
                name_node.inline_comment,
                Some(Cow::Borrowed("inline comment"))
            );
        } else {
            panic!("Expected object");
        }
    }

    #[test]
    fn test_parse_mixed_types() {
        let yaml = "enabled: true\ncount: 42\nratio: 2.5\nempty: null";
        let mut parser = Parser::new(yaml);
        let result = parser.parse().unwrap();

        if let YamlValue::Object(map) = &result.value {
            assert_eq!(
                map.get(&Cow::Borrowed("enabled")).unwrap().value,
                YamlValue::String(Cow::Borrowed("true"))
            );
            assert_eq!(
                map.get(&Cow::Borrowed("count")).unwrap().value,
                YamlValue::String(Cow::Borrowed("42"))
            );
            assert_eq!(
                map.get(&Cow::Borrowed("ratio")).unwrap().value,
                YamlValue::String(Cow::Borrowed("2.5"))
            );
            assert_eq!(
                map.get(&Cow::Borrowed("empty")).unwrap().value,
                YamlValue::String(Cow::Borrowed("null"))
            );
        } else {
            panic!("Expected object");
        }
    }
}