markdoc 0.1.0

Rust implementation of the Markdoc document language (CommonMark plus a {% tag %} extension)
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
//! The parser for the content of a Markdoc tag: everything between `{%` and `%}`.
//!
//! This is a line-by-line port of upstream Markdoc 0.5.11's `src/grammar/tag.js`, the parser
//! PEG.js 0.11.0-master generated from `tag.pegjs`, and it returns what that parser returns
//! when Markdoc calls it as `parse(content, {Variable, Function})` with its `Ast.Variable`
//! and `Ast.Function` classes: the same result objects, and the same `SyntaxError`s with the
//! same message, expectations, found character and location. `tag.rs` holds the port;
//! this module holds the types the JavaScript builds, and `value.rs` the values.
//!
//! JavaScript strings are UTF-16, so every position here (`offset`, `column`) counts UTF-16
//! code units, as `peg$currPos` does.
//!
//! Both [`Parsed`] and [`SyntaxError`] render as JSON exactly as `JSON.stringify` renders the
//! JavaScript objects (`to_json_string`), and convert to the `serde_json::Value` that
//! `JSON.parse` of that text gives (`to_json`; see [`Value::to_json`] for what a
//! `serde_json::Value` cannot hold).

mod json;
mod tag;
mod value;

#[cfg(test)]
mod tests;

use std::fmt;

pub use crate::markdown_it::js::RangeError;
pub use tag::{MAX_NESTING, cost};
pub use value::{Function, Object, Prototype, Value, Variable};

use json::{utf16_to_json, write_str, write_utf16};

/// Parses the content of a tag, as `parse(content, {Variable, Function})` does upstream.
///
/// The error is what upstream's parser throws: its [`SyntaxError`], or, when values nest
/// more than [`MAX_NESTING`] levels deep, the `RangeError` V8 throws once its stack runs out
/// (see [`MAX_NESTING`] for where the two limits differ).
pub fn parse(content: &str) -> Result<Parsed, ParseError> {
    let input: Vec<u16> = content.encode_utf16().collect();
    tag::peg_parse(&input)
}

/// [`parse`] for content that is already UTF-16 (a slice of a JavaScript-model source).
/// String values are Rust strings, so a lone surrogate inside a string literal becomes
/// U+FFFD in them; positions, `found` and `message` keep every code unit.
pub fn parse_utf16(input: &[u16]) -> Result<Parsed, ParseError> {
    tag::peg_parse(input)
}

/// One whole tag, `{% ... %}`, read as Markdoc's tokenizer reads it: the body between the
/// delimiters, with its line breaks as markdown-it hands them over (`\r\n` and `\r` are
/// `\n`, NUL is U+FFFD), trimmed as JavaScript's `String.prototype.trim` trims, through
/// [`parse_utf16`]. `None` when `tag` does not start with `{%` and end with `%}`.
///
/// ```
/// use markdoc::grammar::{ParsedType, parse_tag};
///
/// let parsed = parse_tag("{%\r\n  callout type=\"note\" /%}").unwrap().unwrap();
/// assert_eq!(parsed.kind, ParsedType::Tag);
/// assert_eq!(parsed.meta.tag.as_deref(), Some("callout"));
/// assert!(parse_tag("{% callout x= %}").unwrap().is_err());
/// assert!(parse_tag("callout").is_none());
/// ```
pub fn parse_tag(tag: &str) -> Option<Result<Parsed, ParseError>> {
    let body = tag.strip_prefix("{%")?.strip_suffix("%}")?;
    let body: Vec<u16> = body
        .replace("\r\n", "\n")
        .replace('\r', "\n")
        .replace('\0', "\u{FFFD}")
        .encode_utf16()
        .collect();
    Some(parse_utf16(crate::markdown_it::js::trim(&body)))
}

/// What upstream's parser throws.
///
/// Markdoc's tokenizer catches only the [`SyntaxError`] (it becomes an `error` token, or a
/// fence's `fence-tag-error`) and rethrows anything else, so a [`ParseError::Range`] escapes
/// `Tokenizer.tokenize`.
#[derive(Clone, Debug, PartialEq)]
pub enum ParseError {
    /// `peg$SyntaxError`: the content is not a tag.
    Syntax(SyntaxError),
    /// `RangeError: Maximum call stack size exceeded`: values nested more than
    /// [`MAX_NESTING`] levels deep.
    Range(RangeError),
}

impl ParseError {
    /// The [`SyntaxError`], if this is one.
    pub fn as_syntax(&self) -> Option<&SyntaxError> {
        match self {
            ParseError::Syntax(error) => Some(error),
            ParseError::Range(_) => None,
        }
    }
}

impl From<SyntaxError> for ParseError {
    fn from(error: SyntaxError) -> ParseError {
        ParseError::Syntax(error)
    }
}

impl fmt::Display for ParseError {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            ParseError::Syntax(error) => fmt::Display::fmt(error, f),
            ParseError::Range(error) => fmt::Display::fmt(error, f),
        }
    }
}

impl std::error::Error for ParseError {}

/// The result of a successful parse: `{type, nesting?, meta}`.
#[derive(Clone, Debug, PartialEq)]
pub struct Parsed {
    /// JavaScript's `type`.
    pub kind: ParsedType,
    /// 1 for `tag_open`, 0 for `tag`, -1 for `tag_close`; absent for `annotation` and
    /// `variable` (Markdoc's tokenizer then uses 0).
    pub nesting: Option<i8>,
    pub meta: Meta,
}

/// `type` of a [`Parsed`].
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum ParsedType {
    /// `{% name ... %}`
    TagOpen,
    /// `{% /name %}`
    TagClose,
    /// `{% name ... /%}`
    Tag,
    /// `{% #id .class key=value %}`: attributes without a tag name.
    Annotation,
    /// `{% $variable %}` or `{% function() %}`
    Variable,
}

impl ParsedType {
    /// Every type, in the order of the enum.
    ///
    /// ```
    /// use markdoc::grammar::ParsedType;
    ///
    /// for kind in ParsedType::ALL {
    ///     assert_eq!(kind.as_str().parse::<ParsedType>(), Ok(kind));
    /// }
    /// assert!("tag-open".parse::<ParsedType>().is_err());
    /// ```
    pub const ALL: [ParsedType; 5] = [
        ParsedType::TagOpen,
        ParsedType::TagClose,
        ParsedType::Tag,
        ParsedType::Annotation,
        ParsedType::Variable,
    ];

    /// The name upstream's grammar gives the type (`tag_open`, ...).
    pub fn as_str(self) -> &'static str {
        match self {
            ParsedType::TagOpen => "tag_open",
            ParsedType::TagClose => "tag_close",
            ParsedType::Tag => "tag",
            ParsedType::Annotation => "annotation",
            ParsedType::Variable => "variable",
        }
    }
}

impl std::str::FromStr for ParsedType {
    type Err = String;

    /// The type [`ParsedType::as_str`] names.
    fn from_str(name: &str) -> std::result::Result<ParsedType, String> {
        ParsedType::ALL
            .into_iter()
            .find(|kind| kind.as_str() == name)
            .ok_or_else(|| format!("not a tag type: {name:?}"))
    }
}

/// `meta` of a [`Parsed`]. Which keys are present depends on the type, as in JavaScript:
/// `{tag, attributes}` (tag_open, tag), `{tag}` (tag_close), `{attributes}` (annotation),
/// `{variable}` (variable).
#[derive(Clone, Debug, PartialEq)]
pub struct Meta {
    pub tag: Option<String>,
    /// `None`: no `attributes` key. `Some(None)`: `attributes: null` (a tag with neither a
    /// primary value nor attributes). `Some(Some(list))`: the attributes.
    pub attributes: Option<Option<Vec<Attribute>>>,
    /// A [`Value::Variable`], a [`Value::Function`], or for `@` paths a [`Value::Array`].
    pub variable: Option<Value>,
}

/// One attribute: `{type: "attribute", name, value}`, or `{type: "class", name, value: true}`
/// for `.name`.
#[derive(Clone, Debug, PartialEq)]
pub struct Attribute {
    pub kind: AttributeType,
    pub name: String,
    pub value: Value,
}

/// `type` of an [`Attribute`].
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum AttributeType {
    /// `"attribute"`: `name=value`, `#id` (name `id`) and the primary value (name `primary`).
    Attribute,
    /// `"class"`: `.name`
    Class,
}

impl AttributeType {
    pub fn as_str(self) -> &'static str {
        match self {
            AttributeType::Attribute => "attribute",
            AttributeType::Class => "class",
        }
    }
}

/// A JavaScript string: UTF-16 code units, which may include a surrogate that is not half of
/// a pair (a `SyntaxError`'s `found` is one code unit, so it can be half of an astral
/// character, and its `message` quotes it).
#[derive(Clone, Default, PartialEq, Eq, Hash)]
pub struct JsString(pub Vec<u16>);

impl JsString {
    pub fn as_units(&self) -> &[u16] {
        &self.0
    }

    /// The string, with U+FFFD for a lone surrogate.
    pub fn to_string_lossy(&self) -> String {
        String::from_utf16_lossy(&self.0)
    }
}

impl From<&str> for JsString {
    fn from(s: &str) -> JsString {
        JsString(s.encode_utf16().collect())
    }
}

impl PartialEq<str> for JsString {
    fn eq(&self, other: &str) -> bool {
        self.0.iter().copied().eq(other.encode_utf16())
    }
}

impl PartialEq<&str> for JsString {
    fn eq(&self, other: &&str) -> bool {
        *self == **other
    }
}

impl fmt::Display for JsString {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.write_str(&self.to_string_lossy())
    }
}

impl fmt::Debug for JsString {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        let mut s = String::new();
        write_utf16(&mut s, &self.0);
        f.write_str(&s)
    }
}

/// `peg$SyntaxError`: what the parser throws.
#[derive(Clone, Debug, PartialEq)]
pub struct SyntaxError {
    /// `peg$SyntaxError.buildMessage(expected, found)`.
    pub message: JsString,
    /// The expectations recorded at the furthest position reached, in the order they were
    /// recorded, repeats included.
    pub expected: Vec<Expectation>,
    /// The code unit at that position, or `None` (`null`) at the end of the input.
    pub found: Option<JsString>,
    pub location: Location,
}

/// A PEG.js expectation object. This grammar records only `literal`, `other` and `end`;
/// the other kinds are here because `buildMessage` describes them.
#[derive(Clone, Debug, PartialEq)]
pub enum Expectation {
    /// `{type: "literal", text, ignoreCase}`
    Literal {
        text: &'static str,
        ignore_case: bool,
    },
    /// `{type: "class", parts, inverted, ignoreCase}`
    Class {
        parts: &'static [ClassPart],
        inverted: bool,
        ignore_case: bool,
    },
    /// `{type: "any"}`
    Any,
    /// `{type: "end"}`
    End,
    /// `{type: "other", description}`: a named rule.
    Other { description: &'static str },
    /// `{type: "not", expected}`
    Not(Box<Expectation>),
}

/// One part of a class expectation: a character, or a range `[from, to]`.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum ClassPart {
    Char(&'static str),
    Range(&'static str, &'static str),
}

/// `{start, end}` of a [`SyntaxError`] (no `filename`: Markdoc passes none).
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub struct Location {
    pub start: Position,
    pub end: Position,
}

/// `{offset, line, column}`: offset and column in UTF-16 code units, line and column from 1;
/// only `\n` starts a line.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub struct Position {
    pub offset: usize,
    pub line: usize,
    pub column: usize,
}

impl fmt::Display for SyntaxError {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        fmt::Display::fmt(&self.message, f)
    }
}

impl std::error::Error for SyntaxError {}

// --- JSON: the objects as JSON.stringify renders them ------------------------------------

fn obj(entries: Vec<(&str, serde_json::Value)>) -> serde_json::Value {
    let mut m = serde_json::Map::new();
    for (k, v) in entries {
        m.insert(k.to_string(), v);
    }
    serde_json::Value::Object(m)
}

impl Parsed {
    /// `JSON.parse(JSON.stringify(result))`: `{type, nesting?, meta}`.
    pub fn to_json(&self) -> serde_json::Value {
        let mut entries = vec![("type", self.kind.as_str().into())];
        if let Some(n) = self.nesting {
            entries.push(("nesting", i64::from(n).into()));
        }
        entries.push(("meta", self.meta.to_json()));
        obj(entries)
    }

    /// `JSON.stringify(result)`, byte for byte.
    pub fn to_json_string(&self) -> String {
        let mut out = String::new();
        self.write_json(&mut out);
        out
    }

    pub fn write_json(&self, out: &mut String) {
        out.push_str("{\"type\":");
        write_str(out, self.kind.as_str());
        if let Some(n) = self.nesting {
            out.push_str(",\"nesting\":");
            out.push_str(&n.to_string());
        }
        out.push_str(",\"meta\":");
        self.meta.write_json(out);
        out.push('}');
    }
}

impl Meta {
    pub fn to_json(&self) -> serde_json::Value {
        let mut entries = Vec::new();
        if let Some(tag) = &self.tag {
            entries.push(("tag", tag.clone().into()));
        }
        if let Some(attributes) = &self.attributes {
            entries.push((
                "attributes",
                match attributes {
                    None => serde_json::Value::Null,
                    Some(list) => list.iter().map(Attribute::to_json).collect(),
                },
            ));
        }
        if let Some(variable) = &self.variable {
            entries.push(("variable", variable.to_json()));
        }
        obj(entries)
    }

    pub fn write_json(&self, out: &mut String) {
        out.push('{');
        let mut first = true;
        let mut key = |out: &mut String, k: &str| {
            if !first {
                out.push(',');
            }
            first = false;
            write_str(out, k);
            out.push(':');
        };
        if let Some(tag) = &self.tag {
            key(out, "tag");
            write_str(out, tag);
        }
        if let Some(attributes) = &self.attributes {
            key(out, "attributes");
            match attributes {
                None => out.push_str("null"),
                Some(list) => {
                    out.push('[');
                    for (i, a) in list.iter().enumerate() {
                        if i > 0 {
                            out.push(',');
                        }
                        a.write_json(out);
                    }
                    out.push(']');
                }
            }
        }
        if let Some(variable) = &self.variable {
            key(out, "variable");
            variable.write_json(out);
        }
        out.push('}');
    }
}

impl Attribute {
    pub fn to_json(&self) -> serde_json::Value {
        obj(vec![
            ("type", self.kind.as_str().into()),
            ("name", self.name.clone().into()),
            ("value", self.value.to_json()),
        ])
    }

    pub fn write_json(&self, out: &mut String) {
        out.push_str("{\"type\":");
        write_str(out, self.kind.as_str());
        out.push_str(",\"name\":");
        write_str(out, &self.name);
        out.push_str(",\"value\":");
        self.value.write_json(out);
        out.push('}');
    }
}

impl Expectation {
    pub fn to_json(&self) -> serde_json::Value {
        match self {
            Expectation::Literal { text, ignore_case } => obj(vec![
                ("type", "literal".into()),
                ("text", (*text).into()),
                ("ignoreCase", (*ignore_case).into()),
            ]),
            Expectation::Class {
                parts,
                inverted,
                ignore_case,
            } => obj(vec![
                ("type", "class".into()),
                (
                    "parts",
                    parts
                        .iter()
                        .map(|p| match p {
                            ClassPart::Char(c) => serde_json::Value::from(*c),
                            ClassPart::Range(a, b) => serde_json::Value::from(vec![*a, *b]),
                        })
                        .collect(),
                ),
                ("inverted", (*inverted).into()),
                ("ignoreCase", (*ignore_case).into()),
            ]),
            Expectation::Any => obj(vec![("type", "any".into())]),
            Expectation::End => obj(vec![("type", "end".into())]),
            Expectation::Other { description } => obj(vec![
                ("type", "other".into()),
                ("description", (*description).into()),
            ]),
            Expectation::Not(e) => obj(vec![("type", "not".into()), ("expected", e.to_json())]),
        }
    }

    pub fn write_json(&self, out: &mut String) {
        match self {
            Expectation::Literal { text, ignore_case } => {
                out.push_str("{\"type\":\"literal\",\"text\":");
                write_str(out, text);
                out.push_str(if *ignore_case {
                    ",\"ignoreCase\":true}"
                } else {
                    ",\"ignoreCase\":false}"
                });
            }
            Expectation::Class {
                parts,
                inverted,
                ignore_case,
            } => {
                out.push_str("{\"type\":\"class\",\"parts\":[");
                for (i, p) in parts.iter().enumerate() {
                    if i > 0 {
                        out.push(',');
                    }
                    match p {
                        ClassPart::Char(c) => write_str(out, c),
                        ClassPart::Range(a, b) => {
                            out.push('[');
                            write_str(out, a);
                            out.push(',');
                            write_str(out, b);
                            out.push(']');
                        }
                    }
                }
                out.push_str("],\"inverted\":");
                out.push_str(if *inverted { "true" } else { "false" });
                out.push_str(",\"ignoreCase\":");
                out.push_str(if *ignore_case { "true}" } else { "false}" });
            }
            Expectation::Any => out.push_str("{\"type\":\"any\"}"),
            Expectation::End => out.push_str("{\"type\":\"end\"}"),
            Expectation::Other { description } => {
                out.push_str("{\"type\":\"other\",\"description\":");
                write_str(out, description);
                out.push('}');
            }
            Expectation::Not(e) => {
                out.push_str("{\"type\":\"not\",\"expected\":");
                e.write_json(out);
                out.push('}');
            }
        }
    }
}

impl Position {
    pub fn to_json(&self) -> serde_json::Value {
        obj(vec![
            ("offset", self.offset.into()),
            ("line", self.line.into()),
            ("column", self.column.into()),
        ])
    }

    pub fn write_json(&self, out: &mut String) {
        out.push_str(&format!(
            "{{\"offset\":{},\"line\":{},\"column\":{}}}",
            self.offset, self.line, self.column
        ));
    }
}

impl Location {
    pub fn to_json(&self) -> serde_json::Value {
        obj(vec![
            ("start", self.start.to_json()),
            ("end", self.end.to_json()),
        ])
    }

    pub fn write_json(&self, out: &mut String) {
        out.push_str("{\"start\":");
        self.start.write_json(out);
        out.push_str(",\"end\":");
        self.end.write_json(out);
        out.push('}');
    }
}

impl SyntaxError {
    /// `JSON.parse(JSON.stringify(error))`: `{message, expected, found, location, name}`
    /// (the constructor's own enumerable properties; `name` is `"SyntaxError"`). A lone
    /// surrogate in `message` or `found` becomes U+FFFD (see [`JsString`]).
    pub fn to_json(&self) -> serde_json::Value {
        obj(vec![
            ("message", utf16_to_json(&self.message.0)),
            (
                "expected",
                self.expected.iter().map(Expectation::to_json).collect(),
            ),
            (
                "found",
                self.found
                    .as_ref()
                    .map_or(serde_json::Value::Null, |f| utf16_to_json(&f.0)),
            ),
            ("location", self.location.to_json()),
            ("name", "SyntaxError".into()),
        ])
    }

    /// `JSON.stringify(error)`, byte for byte.
    pub fn to_json_string(&self) -> String {
        let mut out = String::new();
        self.write_fields_json(&mut out);
        out.insert(0, '{');
        out.push_str(",\"name\":\"SyntaxError\"}");
        out
    }

    /// `"message":…,"expected":[…],"found":…,"location":{…}`: the four fields PEG.js
    /// documents, in `JSON.stringify` order, without the surrounding braces.
    pub fn write_fields_json(&self, out: &mut String) {
        out.push_str("\"message\":");
        write_utf16(out, &self.message.0);
        out.push_str(",\"expected\":[");
        for (i, e) in self.expected.iter().enumerate() {
            if i > 0 {
                out.push(',');
            }
            e.write_json(out);
        }
        out.push_str("],\"found\":");
        match &self.found {
            None => out.push_str("null"),
            Some(f) => write_utf16(out, &f.0),
        }
        out.push_str(",\"location\":");
        self.location.write_json(out);
    }
}