maml 0.1.0

A parser and serializer for MAML (Minimal Abstract Markup 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
use std::collections::HashSet;

use crate::error::Error;
use crate::value::Value;

struct Parser<'a> {
    source: &'a str,
    bytes: &'a [u8],
    pos: usize,
    ch: u8,
    line_number: usize,
    done: bool,
}

/// Parses a MAML string into a [`Value`].
///
/// # Errors
///
/// Returns an [`Error`] if the input is not valid MAML. The error includes
/// the line number and a snippet pointing to the problem.
///
/// # Examples
///
/// ```
/// use maml::{parse, Value};
///
/// let value = parse(r#"{name: "maml", version: 1}"#).unwrap();
/// assert_eq!(value["name"], Value::String("maml".into()));
/// assert_eq!(value["version"], Value::Int(1));
/// ```
pub fn parse(source: &str) -> Result<Value, Error> {
    let mut p = Parser::new(source);
    let value = p.parse_value()?;
    p.skip_whitespace();
    if !p.done {
        return Err(p.error_snippet(None));
    }
    match value {
        Some(v) => Ok(v),
        None => Err(p.error_snippet(None)),
    }
}

impl<'a> Parser<'a> {
    fn new(source: &'a str) -> Self {
        let bytes = source.as_bytes();
        let mut p = Parser {
            source,
            bytes,
            pos: 0,
            ch: 0,
            line_number: 1,
            done: false,
        };
        p.next();
        p
    }

    fn next(&mut self) {
        if self.pos < self.bytes.len() {
            self.ch = self.bytes[self.pos];
            self.pos += 1;
            if self.ch == b'\n' {
                self.line_number += 1;
            }
        } else {
            self.ch = 0;
            self.done = true;
        }
    }

    fn lookahead(&self, n: usize) -> &[u8] {
        let end = (self.pos + n).min(self.bytes.len());
        &self.bytes[self.pos..end]
    }

    fn parse_value(&mut self) -> Result<Option<Value>, Error> {
        self.skip_whitespace();
        if let Some(v) = self.parse_raw_string()? {
            return Ok(Some(v));
        }
        if let Some(s) = self.parse_string()? {
            return Ok(Some(Value::String(s)));
        }
        if let Some(v) = self.parse_number()? {
            return Ok(Some(v));
        }
        if let Some(v) = self.parse_object()? {
            return Ok(Some(v));
        }
        if let Some(v) = self.parse_array()? {
            return Ok(Some(v));
        }
        if let Some(v) = self.parse_keyword(b"true", Value::Bool(true))? {
            return Ok(Some(v));
        }
        if let Some(v) = self.parse_keyword(b"false", Value::Bool(false))? {
            return Ok(Some(v));
        }
        if let Some(v) = self.parse_keyword(b"null", Value::Null)? {
            return Ok(Some(v));
        }
        Ok(None)
    }

    fn parse_string(&mut self) -> Result<Option<String>, Error> {
        if self.ch != b'"' {
            return Ok(None);
        }
        self.parse_string_body().map(Some)
    }

    fn parse_string_body(&mut self) -> Result<String, Error> {
        let mut s = String::new();
        let mut escaped = false;
        loop {
            self.next();
            if escaped {
                if self.ch == b'u' {
                    self.next();
                    if self.ch != b'{' {
                        return Err(self.error_snippet(Some(format!(
                            "Invalid escape sequence {} (expected \"{{\")",
                            format_char(self.ch)
                        ))));
                    }
                    let mut hex = String::new();
                    loop {
                        self.next();
                        if self.ch == b'}' {
                            break;
                        }
                        if !is_hex_digit(self.ch) {
                            if hex.is_empty() {
                                return Err(
                                    self.error_snippet(Some("Invalid escape sequence".into()))
                                );
                            }
                            return Err(self.error_snippet(Some(format!(
                                "Invalid escape sequence {}",
                                format_char(self.ch)
                            ))));
                        }
                        hex.push(self.ch as char);
                        if hex.len() > 6 {
                            return Err(self.error_snippet(Some(
                                "Invalid escape sequence (too many hex digits)".into(),
                            )));
                        }
                    }
                    if hex.is_empty() {
                        return Err(self.error_snippet(Some("Invalid escape sequence".into())));
                    }
                    let code_point = u32::from_str_radix(&hex, 16).unwrap();
                    if code_point > 0x10FFFF || (0xD800..=0xDFFF).contains(&code_point) {
                        return Err(self
                            .error_snippet(Some("Invalid escape sequence (out of range)".into())));
                    }
                    // Code point is a valid Unicode scalar value; safe to unwrap.
                    s.push(char::from_u32(code_point).unwrap());
                } else {
                    match escape_char(self.ch) {
                        Some(c) => s.push(c),
                        None => {
                            return Err(self.error_snippet(Some(format!(
                                "Invalid escape sequence {}",
                                format_char(self.ch)
                            ))));
                        }
                    }
                }
                escaped = false;
            } else if self.ch == b'\\' {
                escaped = true;
            } else if self.ch == b'"' {
                break;
            } else if self.ch == b'\n'
                || self.done
                || (self.ch < 0x20 && self.ch != b'\t')
                || self.ch == 0x7F
            {
                return Err(self.error_snippet(None));
            } else {
                // Multi-byte UTF-8: copy full character
                if self.ch < 0x80 {
                    s.push(self.ch as char);
                } else {
                    // Multi-byte UTF-8: source is valid &str, so chars().next() is guaranteed
                    let start = self.pos - 1;
                    let c = self.source[start..].chars().next().unwrap();
                    s.push(c);
                    for _ in 1..c.len_utf8() {
                        self.next();
                    }
                }
            }
        }
        self.next();
        Ok(s)
    }

    fn parse_raw_string(&mut self) -> Result<Option<Value>, Error> {
        if self.ch != b'"' || self.lookahead(2) != b"\"\"" {
            return Ok(None);
        }
        self.next(); // skip second "
        self.next(); // skip third "
        self.next(); // move to content

        let mut has_leading_newline = false;
        // Strip leading CRLF or LF (not bare CR)
        if self.ch == b'\r' && self.lookahead(1) == b"\n" {
            self.next(); // skip \r, now at \n
        }
        if self.ch == b'\n' {
            has_leading_newline = true;
            self.next();
        }

        let mut s = String::new();
        while !self.done {
            if self.ch == b'"' && self.lookahead(2) == b"\"\"" {
                self.next();
                self.next();
                self.next();
                if s.is_empty() && !has_leading_newline {
                    return Err(self.error_snippet(Some("Raw strings cannot be empty".into())));
                }
                return Ok(Some(Value::String(s)));
            }
            // Copy byte — for multi-byte UTF-8, copy full character
            if self.ch < 0x80 {
                s.push(self.ch as char);
            } else {
                // source is valid &str, so chars().next() is guaranteed
                let start = self.pos - 1;
                let c = self.source[start..].chars().next().unwrap();
                s.push(c);
                for _ in 1..c.len_utf8() {
                    self.next();
                }
            }
            self.next();
        }
        Err(self.error_snippet(None))
    }

    fn parse_number(&mut self) -> Result<Option<Value>, Error> {
        if !is_digit(self.ch) && self.ch != b'-' {
            return Ok(None);
        }
        let mut num_str = String::new();
        let mut is_float = false;

        if self.ch == b'-' {
            num_str.push('-');
            self.next();
            if !is_digit(self.ch) {
                return Err(self.error_snippet(None));
            }
        }

        if self.ch == b'0' {
            num_str.push('0');
            self.next();
        } else {
            while is_digit(self.ch) {
                num_str.push(self.ch as char);
                self.next();
            }
        }

        if self.ch == b'.' {
            is_float = true;
            num_str.push('.');
            self.next();
            if !is_digit(self.ch) {
                return Err(self.error_snippet(None));
            }
            while is_digit(self.ch) {
                num_str.push(self.ch as char);
                self.next();
            }
        }

        if self.ch == b'e' || self.ch == b'E' {
            is_float = true;
            num_str.push(self.ch as char);
            self.next();
            if self.ch == b'+' || self.ch == b'-' {
                num_str.push(self.ch as char);
                self.next();
            }
            if !is_digit(self.ch) {
                return Err(self.error_snippet(None));
            }
            while is_digit(self.ch) {
                num_str.push(self.ch as char);
                self.next();
            }
        }

        if is_float {
            // Format is pre-validated, parse cannot fail
            let n: f64 = num_str.parse().unwrap();
            Ok(Some(Value::Float(n)))
        } else if num_str == "-0" {
            Ok(Some(Value::Float(-0.0)))
        } else {
            let n: i64 = num_str.parse().map_err(|_| {
                self.error_snippet(Some(format!("Integer out of range: {num_str}")))
            })?;
            Ok(Some(Value::Int(n)))
        }
    }

    fn parse_object(&mut self) -> Result<Option<Value>, Error> {
        if self.ch != b'{' {
            return Ok(None);
        }
        self.next();
        self.skip_whitespace();

        let mut pairs: Vec<(String, Value)> = Vec::new();
        let mut seen_keys: HashSet<String> = HashSet::new();

        if self.ch == b'}' {
            self.next();
            return Ok(Some(Value::Object(pairs)));
        }

        loop {
            let key_pos = self.pos;
            let key = if self.ch == b'"' {
                self.parse_string_body()?
            } else {
                self.parse_key()?
            };

            if !seen_keys.insert(key.clone()) {
                self.pos = key_pos;
                return Err(self.error_snippet(Some(format!("Duplicate key {:?}", key))));
            }

            self.skip_whitespace();
            if self.ch != b':' {
                return Err(self.error_snippet(None));
            }
            self.next();

            let value = self.parse_value()?;
            match value {
                Some(v) => pairs.push((key, v)),
                None => return Err(self.error_snippet(None)),
            }

            let newline_after_value = self.skip_whitespace();
            if self.ch == b'}' {
                self.next();
                return Ok(Some(Value::Object(pairs)));
            } else if self.ch == b',' {
                self.next();
                self.skip_whitespace();
                if self.ch == b'}' {
                    self.next();
                    return Ok(Some(Value::Object(pairs)));
                }
            } else if newline_after_value {
                continue;
            } else {
                return Err(self.error_snippet(Some(
                    "Expected comma or newline between key-value pairs".into(),
                )));
            }
        }
    }

    fn parse_key(&mut self) -> Result<String, Error> {
        let mut ident = String::new();
        while is_key_char(self.ch) {
            ident.push(self.ch as char);
            self.next();
        }
        if ident.is_empty() {
            return Err(self.error_snippet(None));
        }
        Ok(ident)
    }

    fn parse_array(&mut self) -> Result<Option<Value>, Error> {
        if self.ch != b'[' {
            return Ok(None);
        }
        self.next();
        self.skip_whitespace();

        let mut arr: Vec<Value> = Vec::new();

        if self.ch == b']' {
            self.next();
            return Ok(Some(Value::Array(arr)));
        }

        loop {
            let value = self.parse_value()?;
            match value {
                Some(v) => arr.push(v),
                None => return Err(self.error_snippet(None)),
            }

            let newline_after_value = self.skip_whitespace();
            if self.ch == b']' {
                self.next();
                return Ok(Some(Value::Array(arr)));
            } else if self.ch == b',' {
                self.next();
                self.skip_whitespace();
                if self.ch == b']' {
                    self.next();
                    return Ok(Some(Value::Array(arr)));
                }
            } else if newline_after_value {
                continue;
            } else {
                return Err(
                    self.error_snippet(Some("Expected comma or newline between values".into()))
                );
            }
        }
    }

    fn parse_keyword(&mut self, name: &[u8], value: Value) -> Result<Option<Value>, Error> {
        if self.ch != name[0] {
            return Ok(None);
        }
        for &expected in &name[1..] {
            self.next();
            if self.ch != expected {
                return Err(self.error_snippet(None));
            }
        }
        self.next();
        if is_whitespace(self.ch)
            || self.ch == b','
            || self.ch == b'}'
            || self.ch == b']'
            || self.done
        {
            Ok(Some(value))
        } else {
            Err(self.error_snippet(None))
        }
    }

    fn skip_whitespace(&mut self) -> bool {
        let mut has_newline = false;
        loop {
            match self.ch {
                b' ' | b'\t' => self.next(),
                b'\n' => {
                    has_newline = true;
                    self.next();
                }
                b'\r' => {
                    // Only consume CR if followed by LF (CRLF newline)
                    if self.pos < self.bytes.len() && self.bytes[self.pos] == b'\n' {
                        self.next(); // consume CR, now at LF
                    } else {
                        break; // bare CR is not valid whitespace
                    }
                }
                _ => break,
            }
        }
        let has_newline_after_comment = self.skip_comment();
        has_newline || has_newline_after_comment
    }

    fn skip_comment(&mut self) -> bool {
        if self.ch == b'#' {
            while !self.done && self.ch != b'\n' {
                self.next();
            }
            return self.skip_whitespace();
        }
        false
    }

    fn error_snippet(&self, message: Option<String>) -> Error {
        let message = if self.done {
            "Unexpected end of input".to_string()
        } else {
            message.unwrap_or_else(|| {
                // !self.done guarantees pos >= 1 and source[pos-1..] is non-empty valid UTF-8
                let byte_pos = self.pos - 1;
                let c = self.source[byte_pos..].chars().next().unwrap();
                let ch_repr = format!("{:?}", c.to_string());
                format!("Unexpected character {ch_repr}")
            })
        };

        let pos = self.pos;
        // Ensure start and end are on char boundaries
        let start = floor_char_boundary(self.source, pos.saturating_sub(40));
        let safe_pos = floor_char_boundary(self.source, pos);
        let before = &self.source[start..safe_pos];
        let lines: Vec<&str> = before.split('\n').collect();

        let mut last_line = lines.last().copied().unwrap_or("");
        let end = ceil_char_boundary(self.source, pos + 40);
        let safe_pos_after = ceil_char_boundary(self.source, pos);
        let after = &self.source[safe_pos_after..end];
        let mut postfix = after.split('\n').next().unwrap_or("");

        let mut line_number = self.line_number;

        if last_line.is_empty() && lines.len() >= 2 {
            // error at "\n"
            last_line = lines[lines.len() - 2];
            postfix = "";
            line_number -= 1;
        }

        let snippet = format!("    {last_line}{postfix}");
        // Use char count for dots to match JS behavior
        let dot_count = last_line.chars().count().saturating_sub(1);
        let dots = ".".repeat(dot_count);
        let pointer = format!("    {dots}^");
        let formatted = format!("{message} on line {line_number}.\n\n{snippet}\n{pointer}");

        Error::parse_error(formatted, line_number)
    }
}

fn is_whitespace(ch: u8) -> bool {
    ch == b' ' || ch == b'\n' || ch == b'\t' || ch == b'\r'
}

fn is_digit(ch: u8) -> bool {
    ch.is_ascii_digit()
}

fn is_hex_digit(ch: u8) -> bool {
    ch.is_ascii_digit() || (b'A'..=b'F').contains(&ch)
}

fn is_key_char(ch: u8) -> bool {
    ch.is_ascii_alphanumeric() || ch == b'_' || ch == b'-'
}

fn escape_char(ch: u8) -> Option<char> {
    match ch {
        b'"' => Some('"'),
        b'\\' => Some('\\'),
        b'n' => Some('\n'),
        b'r' => Some('\r'),
        b't' => Some('\t'),
        _ => None,
    }
}

fn format_char(ch: u8) -> String {
    if ch == 0 {
        return "\"\"".to_string();
    }
    let c = ch as char;
    format!("{:?}", c.to_string())
}

fn floor_char_boundary(s: &str, pos: usize) -> usize {
    let pos = pos.min(s.len());
    let mut p = pos;
    while p > 0 && !s.is_char_boundary(p) {
        p -= 1;
    }
    p
}

fn ceil_char_boundary(s: &str, pos: usize) -> usize {
    let pos = pos.min(s.len());
    let mut p = pos;
    while p < s.len() && !s.is_char_boundary(p) {
        p += 1;
    }
    p
}