babbel_yaml 0.1.0

Fast, modular YAML 1.2 parser and emitter with anchors, aliases, and tags
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
//! YAML Library Utility Modules
//!
//! This module aggregates utility submodules for the YAML library, including performance tools,
//! string interning, escaping, streaming, and anchor helpers. These utilities provide reusable
//! building blocks for efficient and robust YAML processing.
//!
//! # Features
//! - Performance measurement and optimization
//! - String interning
//! - String escaping for multiple formats
//! - Streaming and iterator support
//! - Anchor-related helpers
//!
//! # Usage
//! Import and use the relevant submodules as needed throughout the YAML library.

/// Performance measurement and optimization utilities
pub mod performance;

/// String interning for memory optimization
#[cfg(feature = "alloc")]
pub mod string_interner;

/// Performance optimization utilities (lazy evaluation, capacity hints, zero-copy)
#[cfg(feature = "alloc")]
pub mod optimization;

pub mod escape;
/// Streaming and iterator support for efficient YAML processing
#[cfg(feature = "alloc")]
pub mod streaming;

/// Anchor-related helpers for DRY refactor
pub mod anchors_helpers;

use crate::constants::{CHAR_CARRIAGE_RETURN, CHAR_HASH, CHAR_NEWLINE, CHAR_SPACE, CHAR_TAB};
use crate::io::traits::ISource;
use crate::{Node, Numeric};

/// Returns true if the character is a line terminator ("\n" or "\r").
#[inline]
pub fn is_line_terminator(c: char) -> bool {
    c == CHAR_NEWLINE || c == CHAR_CARRIAGE_RETURN
}

/// Returns true if the character is horizontal whitespace (space or tab).
#[inline]
pub fn is_horizontal_space(c: char) -> bool {
    c == CHAR_SPACE || c == CHAR_TAB
}

/// Returns true if the character starts a comment ("#").
#[inline]
pub fn is_comment_start(c: char) -> bool {
    c == CHAR_HASH
}

/// Collects characters from the source until the stop predicate returns true.
///
/// # Arguments
///
/// * `source` - A mutable reference to a source implementing ISource trait
/// * `stop_pred` - A closure that takes a character and returns true when collection should stop
///
/// # Returns
///
/// A String containing all collected characters before the stop condition
///
/// # Safety
///
/// Has a maximum iteration limit of 100,000 characters to prevent infinite loops
pub fn collect_until<F>(source: &mut dyn ISource, mut stop_pred: F) -> String
where
    F: FnMut(char) -> bool,
{
    let mut out = String::new();
    let mut iterations = 0;
    const MAX_ITERATIONS: usize = 100_000;

    while let Some(c) = source.current() {
        if stop_pred(c) {
            break;
        }
        out.push(c);
        source.next();

        iterations += 1;
        if iterations >= MAX_ITERATIONS {
            // Prevent infinite loop - return what we have so far
            break;
        }
    }
    out
}

/// Skips whitespace characters and comment lines in the source.
///
/// Continuously advances through the source, skipping whitespace and comment lines
/// (lines starting with #) until non-whitespace, non-comment content is encountered.
///
/// # Arguments
///
/// * `source` - A mutable reference to a source implementing ISource trait
///
/// # Safety
///
/// Has a maximum iteration limit of 100,000 characters to prevent infinite loops
pub fn skip_whitespace_and_comments(source: &mut dyn ISource) {
    let mut iterations = 0;
    const MAX_ITERATIONS: usize = 100_000;

    loop {
        while let Some(c) = source.current() {
            if source.is_whitespace(c) || is_line_terminator(c) {
                source.next();
                iterations += 1;
                if iterations >= MAX_ITERATIONS {
                    return; // Prevent infinite loop
                }
            } else {
                break;
            }
        }
        if source.current().map_or(false, is_comment_start) {
            while let Some(c) = source.current() {
                if is_line_terminator(c) {
                    source.next(); // consume the newline after comment
                    break;
                }
                source.next();
                iterations += 1;
                if iterations >= MAX_ITERATIONS {
                    return; // Prevent infinite loop
                }
            }

            continue;
        }
        break;
    }
}

/// Skips whitespace and comments, validating that tabs are not used as indentation
/// after newlines. Per YAML 1.2 spec, tabs cannot be used for indentation.
///
/// # Arguments
///
/// * `source` - A mutable reference to a source implementing ISource trait
///
/// # Returns
///
/// `Ok(())` if successful, `Err(String)` if tabs found as indentation
pub fn skip_whitespace_and_comments_validate_tabs(source: &mut dyn ISource) -> Result<(), String> {
    let mut iterations = 0;
    const MAX_ITERATIONS: usize = 100_000;
    let mut after_newline = false;

    loop {
        while let Some(c) = source.current() {
            if is_line_terminator(c) {
                source.next();
                after_newline = true;
                iterations += 1;
                if iterations >= MAX_ITERATIONS {
                    return Ok(()); // Prevent infinite loop
                }
            } else if c == CHAR_TAB && after_newline {
                // Tab after newline is indentation - forbidden in YAML 1.2
                return Err(format!(
                    "Tabs cannot be used for indentation in YAML (current: '{}', indent: {})",
                    c,
                    source.get_current_indent_level()
                ));
            } else if source.is_whitespace(c) {
                source.next();
                iterations += 1;
                if iterations >= MAX_ITERATIONS {
                    return Ok(()); // Prevent infinite loop
                }
            } else {
                // Non-whitespace found
                after_newline = false;
                break;
            }
        }
        if source.current().map_or(false, is_comment_start) {
            while let Some(c) = source.current() {
                if is_line_terminator(c) {
                    source.next(); // consume the newline after comment
                    after_newline = true;
                    break;
                }
                source.next();
                iterations += 1;
                if iterations >= MAX_ITERATIONS {
                    return Ok(()); // Prevent infinite loop
                }
            }

            continue;
        }
        break;
    }
    Ok(())
}

/// Skips characters in the source until a newline character is encountered.
///
/// Advances through the source character by character until it finds a newline
/// character ('\n'), which is also consumed.
///
/// # Arguments
///
/// * `source` - A mutable reference to a source implementing ISource trait
pub fn skip_until_newline(source: &mut dyn ISource) {
    while let Some(c) = source.current() {
        if c == CHAR_NEWLINE {
            source.next();
            break;
        }
        source.next();
    }
}

/// Reads a line from the source and returns it as a trimmed string, excluding comments.
///
/// Collects characters until a newline is encountered. If a hash (#) character is found,
/// everything from the hash to the end of the line is treated as a comment and excluded.
/// The resulting string is trimmed of leading and trailing whitespace.
///
/// # Arguments
///
/// * `source` - A mutable reference to a source implementing ISource trait
///
/// # Returns
///
/// A trimmed String containing the line content without comments
pub fn read_line_trimmed_into_string(source: &mut dyn ISource) -> String {
    let s = collect_until(source, |c| c == CHAR_NEWLINE);

    if let Some(pos) = s.find(CHAR_HASH) {
        return s[..pos].trim().to_string();
    }
    s.trim().to_string()
}

/// Converts a Node to its inline string representation.
///
/// Transforms various Node types into compact string representations suitable for
/// inline display. Arrays and mappings are formatted in compact JSON-like syntax.
///
/// # Arguments
///
/// * `node` - A reference to the Node to convert
///
/// # Returns
///
/// A String containing the inline representation of the node
#[allow(dead_code)]
pub fn node_to_inline_string(node: &Node) -> String {
    match node {
        Node::Str(s, _, _) => s.clone(),
        Node::Number(Numeric::Integer(i)) => i.to_string(),
        Node::Number(Numeric::Float(f)) => f.to_string(),
        Node::Boolean(b) => b.to_string(),
        Node::Array(items) => {
            let parts: Vec<String> = items.iter().map(node_to_inline_string).collect();
            format!("[{}]", parts.join(", "))
        }
        Node::Mapping(pairs) => {
            let parts: Vec<String> = pairs
                .iter()
                .map(|(k, v)| format!("{}: {}", node_to_inline_string(k), node_to_inline_string(v)))
                .collect();
            format!("{{{}}}", parts.join(", "))
        }
        _ => format!("{node:?}"),
    }
}

/// Unescapes a double-quoted string by processing escape sequences.
///
/// # Arguments
///
/// * `s` - The string content to unescape
///
/// # Returns
///
/// A new String with escape sequences processed
pub fn unescape_double_quoted(s: &str) -> String {
    let mut result = String::with_capacity(s.len());
    let mut chars = s.chars().peekable();

    while let Some(c) = chars.next() {
        if c == '\\' {
            match chars.next() {
                Some('u') => {
                    let mut hex = String::new();
                    for _ in 0..4 {
                        if let Some(h) = chars.peek().copied() {
                            if h.is_ascii_hexdigit() {
                                hex.push(h);
                                chars.next();
                            } else {
                                break;
                            }
                        }
                    }
                    if hex.len() < 4 {
                        // Incomplete unicode escape: emit debug trace and return error marker
                        eprintln!("DEBUG: Incomplete unicode escape: \\u{}", hex);
                        result.push_str("[ERROR:INCOMPLETE_UNICODE_ESCAPE]");
                        break;
                    }
                    if let Ok(code) = u16::from_str_radix(&hex, 16) {
                        if let Some(ch) = char::from_u32(code as u32) {
                            result.push(ch);
                            continue;
                        }
                    }
                    result.push('\\');
                    result.push('u');
                    result.push_str(&hex);
                }
                Some('U') => {
                    let mut hex = String::new();
                    for _ in 0..8 {
                        if let Some(h) = chars.peek().copied() {
                            if h.is_ascii_hexdigit() {
                                hex.push(h);
                                chars.next();
                            } else {
                                break;
                            }
                        }
                    }
                    if hex.len() < 8 {
                        eprintln!("DEBUG: Incomplete unicode escape: \\U{}", hex);
                        result.push_str("[ERROR:INCOMPLETE_UNICODE_ESCAPE]");
                        break;
                    }
                    if let Ok(code) = u32::from_str_radix(&hex, 16) {
                        if let Some(ch) = char::from_u32(code) {
                            result.push(ch);
                            continue;
                        }
                    }
                    result.push('\\');
                    result.push('U');
                    result.push_str(&hex);
                }
                Some('x') => {
                    result.push('\\');
                    result.push('x');
                    for _ in 0..2 {
                        if let Some(h) = chars.peek().copied() {
                            if h.is_ascii_hexdigit() {
                                result.push(h);
                                chars.next();
                            } else {
                                break;
                            }
                        }
                    }
                }
                Some('n') => {
                    result.push('\n');
                }
                Some('r') => {
                    result.push('\r');
                }
                Some('t') => {
                    result.push('\t');
                }
                Some('b') => {
                    result.push('\x08'); // backspace
                }
                Some('"') => result.push('"'),
                Some('\\') => result.push('\\'),
                Some('/') => result.push('/'),
                Some(' ') => result.push(' '),
                Some('0') => result.push('\0'),
                Some('a') => result.push('\x07'),
                Some('v') => result.push('\x0B'),
                Some('f') => result.push('\x0C'),
                Some('e') => result.push('\x1B'),
                Some('N') => result.push('\u{0085}'),
                Some('_') => result.push('\u{00A0}'),
                Some('L') => result.push('\u{2028}'),
                Some('P') => result.push('\u{2029}'),
                Some('\n') => {
                    while let Some(&c) = chars.peek() {
                        if c == crate::constants::CHAR_SPACE || c == crate::constants::CHAR_TAB {
                            chars.next();
                        } else {
                            break;
                        }
                    }
                }
                Some('\r') => {
                    if chars.peek() == Some(&'\n') {
                        chars.next();
                    }
                    while let Some(&c) = chars.peek() {
                        if c == crate::constants::CHAR_SPACE || c == crate::constants::CHAR_TAB {
                            chars.next();
                        } else {
                            break;
                        }
                    }
                }
                Some('\t') => {
                    while let Some(&c) = chars.peek() {
                        if c == crate::constants::CHAR_SPACE || c == crate::constants::CHAR_TAB {
                            chars.next();
                        } else {
                            break;
                        }
                    }
                }
                Some(other) => {
                    eprintln!("DEBUG: Invalid escape sequence: \\{}", other);
                    result.push('\\');
                    result.push(other);
                }
                None => {
                    eprintln!("DEBUG: Trailing backslash in string");
                    result.push('\\');
                }
            }
        } else {
            result.push(c);
        }
    }
    result
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::io::sources::buffer::Buffer;

    #[test]
    fn test_collect_until_and_read_line() {
        let mut buf = Buffer::new(b"hello, world\nnext");
        let s = collect_until(&mut buf, |c| c == ',');
        assert_eq!(s, "hello");

        buf.next();
        let line = read_line_trimmed_into_string(&mut buf);

        assert_eq!(line, "world");
    }
    #[test]
    fn test_skip_whitespace_and_comments() {
        let mut buf = Buffer::new(b"hello, world\n# comment\nnext");
        skip_whitespace_and_comments(&mut buf);
        assert_eq!(buf.current(), Some('h'));
    }
}