dmc-parser 0.2.3

Typed AST parser for the dmc MDX compiler
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
use crate::ast::*;
use crate::parser::Parser;
use dmc_diagnostic::Code;
use dmc_lexer::token::{QuoteKind, TokenKind};

impl<'eng, 'tokens> Parser<'eng, 'tokens> {
  /// Lowercase / kebab-case JSX tag names are routed through the
  /// CommonMark raw-HTML path. Keep uppercase / namespaced / member
  /// names on the JSX path for MDX component semantics.
  pub(crate) fn is_plain_html_jsx_tag(&self) -> bool {
    let Some(open) = self.tokens.get(self.pos) else {
      return false;
    };
    if !matches!(open.kind, TokenKind::JsxOpenTagStart | TokenKind::JsxCloseTagStart) {
      return false;
    }
    let Some(name_tok) = self.tokens.get(self.pos + 1) else {
      return false;
    };
    if !matches!(name_tok.kind, TokenKind::JsxTagName) {
      return false;
    }
    name_tok.raw.chars().all(|c| c.is_ascii_lowercase() || c.is_ascii_digit() || c == '-')
  }

  pub(crate) fn is_htmlish_jsx_tag(&self) -> bool {
    let Some(open) = self.tokens.get(self.pos) else {
      return false;
    };
    if !matches!(open.kind, TokenKind::JsxOpenTagStart | TokenKind::JsxCloseTagStart) {
      return false;
    }
    let Some(name_tok) = self.tokens.get(self.pos + 1) else {
      return false;
    };
    if !matches!(name_tok.kind, TokenKind::JsxTagName) {
      return false;
    }
    let mut chars = name_tok.raw.chars();
    matches!(chars.next(), Some(c) if c.is_ascii_alphabetic()) && chars.all(|c| c.is_ascii_alphanumeric() || c == '-')
  }

  /// CommonMark raw HTML does not use JS-style quote escaping inside
  /// attribute strings. Reject those cases so malformed tags stay text.
  pub(crate) fn jsx_raw_html_tag_is_valid(&self) -> bool {
    self.jsx_raw_html_tag_is_valid_with(self.is_plain_html_jsx_tag())
  }

  pub(crate) fn jsx_raw_html_tag_is_valid_htmlish(&self) -> bool {
    self.jsx_raw_html_tag_is_valid_with(self.is_htmlish_jsx_tag())
  }

  fn jsx_raw_html_tag_is_valid_with(&self, allowed_tag: bool) -> bool {
    let Some(kind) = self.peek_kind() else {
      return false;
    };
    if !allowed_tag {
      return false;
    }
    let is_close = matches!(kind, TokenKind::JsxCloseTagStart);
    let mut i = self.pos + 2;
    let mut valid = true;
    let mut quote: Option<QuoteKind> = None;
    while let Some(tok) = self.tokens.get(i) {
      match tok.kind {
        TokenKind::JsxAttrStringOpen(kind) if !is_close => {
          quote = Some(kind);
        },
        TokenKind::JsxAttrString if !is_close => {
          if let Some(kind) = quote {
            let escaped_quote = match kind {
              QuoteKind::Double => "\\\"",
              QuoteKind::Single => "\\'",
            };
            if tok.raw.contains(escaped_quote) {
              valid = false;
            }
          }
        },
        TokenKind::JsxAttrStringClose(_) if !is_close => {
          quote = None;
        },
        TokenKind::JsxAttributeName | TokenKind::JsxAttrEq if !is_close => {},
        TokenKind::JsxOpenTagEnd | TokenKind::JsxSelfClosingEnd if !is_close => {
          return valid && quote.is_none();
        },
        TokenKind::JsxCloseTagEnd if is_close => return valid,
        TokenKind::ExpressionStart | TokenKind::ExpressionEnd | TokenKind::JsxAttributeSpread => {
          valid = false;
        },
        TokenKind::Eof | TokenKind::BlankLine | TokenKind::SoftBreak | TokenKind::HardBreak => return false,
        _ => {
          valid = false;
        },
      }
      i += 1;
    }
    false
  }

  /// Reconstruct one JSX-tokenized lowercase HTML tag as either raw HTML
  /// (valid per the lightweight CM checks above) or literal text (for
  /// malformed tags that must escape on output).
  pub(crate) fn parse_inline_raw_html_tag(&mut self) -> Option<Node> {
    let kind = self.peek_kind()?.clone();
    if !matches!(kind, TokenKind::JsxOpenTagStart | TokenKind::JsxCloseTagStart) || !self.is_plain_html_jsx_tag() {
      return None;
    }

    let span = self.current_span();
    let valid = self.jsx_raw_html_tag_is_valid();
    let start_idx = self.pos;
    let mut end_idx = self.pos;
    let want_close = matches!(kind, TokenKind::JsxCloseTagStart);
    while let Some(tok) = self.tokens.get(end_idx) {
      let done = match tok.kind {
        TokenKind::JsxCloseTagEnd => want_close,
        TokenKind::JsxOpenTagEnd | TokenKind::JsxSelfClosingEnd => !want_close,
        _ => false,
      };
      if done {
        break;
      }
      if matches!(tok.kind, TokenKind::Eof) {
        return None;
      }
      end_idx += 1;
    }

    let value = self.raw_source_for_token_range(start_idx, end_idx + 1);
    self.pos = end_idx + 1;

    Some(if valid {
      Node::Html(Html { value, span })
    } else {
      Node::Text(Text { value: Self::unescape_markdown(&value), span })
    })
  }

  /// Skip the inter-token whitespace the lexer now keeps for inline
  /// spacing. JSX tag-internal whitespace is structural noise; the parser
  /// drops it so attribute / closing-tag tokens line up the way they did
  /// before whitespace tokens were preserved.
  fn skip_jsx_ws(&mut self) {
    while matches!(self.peek_kind(), Some(TokenKind::Whitespace(_))) {
      self.advance();
    }
  }

  /// Cursor at `JsxOpenTagStart`. Consumes through the matching close (or
  /// self-close) and returns a `JsxElement`, `JsxSelfClosing`, or `JsxFragment`.
  pub(crate) fn parse_jsx(&mut self) -> Node {
    let span = self.current_span();
    self.advance();
    self.skip_jsx_ws();
    let name = if let Some(t) = self.peek() {
      if matches!(t.kind, TokenKind::JsxTagName) {
        let n = t.raw.to_string();
        self.advance();
        n
      } else {
        String::new()
      }
    } else {
      String::new()
    };
    self.skip_jsx_ws();

    let attrs = self.parse_jsx_attrs();

    self.skip_jsx_ws();
    match self.peek_kind() {
      Some(TokenKind::JsxSelfClosingEnd) => {
        self.advance();
        return Node::JsxSelfClosing(JsxSelfClosing { name, attrs, span });
      },
      Some(TokenKind::JsxOpenTagEnd) => {
        self.advance();
      },
      _ => {
        self.warn(
          Code::RecoveredUnterminatedJsx,
          format!("unterminated JSX open tag <{name}> -- synthesizing self-close"),
        );
        return Node::JsxSelfClosing(JsxSelfClosing { name, attrs, span });
      },
    }

    let mut children = Vec::new();
    loop {
      match self.peek_kind() {
        Some(TokenKind::JsxCloseTagStart) => {
          self.advance();
          self.skip_jsx_ws();
          if matches!(self.peek_kind(), Some(TokenKind::JsxTagName)) {
            self.advance();
          }
          self.skip_jsx_ws();
          if matches!(self.peek_kind(), Some(TokenKind::JsxCloseTagEnd)) {
            self.advance();
          }
          break;
        },
        Some(TokenKind::Eof) | None => break,
        _ => {
          let before = self.pos;
          if let Some(node) = self.parse_block() {
            children.push(node);
          }
          if self.pos == before {
            self.advance();
          }
        },
      }
    }

    let children = unwrap_jsx_only_paragraphs(children);

    if name.is_empty() {
      Node::JsxFragment(JsxFragment { children, span })
    } else {
      Node::JsxElement(JsxElement { name, attrs, children, span })
    }
  }

  /// Consume `name`, `name="str"`, `name={expr}` attributes. Bare names map
  /// to `JsxAttrValue::Boolean`. Stops at the first non-attribute token.
  /// Skips inter-attribute whitespace.
  fn parse_jsx_attrs(&mut self) -> Vec<JsxAttr> {
    let mut out = Vec::new();
    self.skip_jsx_ws();
    loop {
      // Spread attribute `{...rest}` -- lexer wraps the body in
      // ExpressionStart / JsxAttributeSpread / ExpressionEnd.
      if matches!(self.peek_kind(), Some(TokenKind::ExpressionStart)) {
        let span = self.current_span();
        self.advance();
        let body = if matches!(self.peek_kind(), Some(TokenKind::JsxAttributeSpread)) {
          let s = self.peek().unwrap().raw.to_string();
          self.advance();
          s
        } else {
          String::new()
        };
        if matches!(self.peek_kind(), Some(TokenKind::ExpressionEnd)) {
          self.advance();
        }
        out.push(JsxAttr { name: String::new(), value: JsxAttrValue::Spread(body), span });
        self.skip_jsx_ws();
        continue;
      }
      if !matches!(self.peek_kind(), Some(TokenKind::JsxAttributeName)) {
        break;
      }
      let span = self.current_span();
      let name = self.peek().unwrap().raw.to_string();
      self.advance();
      self.skip_jsx_ws();
      let value = if matches!(self.peek_kind(), Some(TokenKind::JsxAttrEq)) {
        self.advance();
        self.skip_jsx_ws();
        match self.peek_kind() {
          Some(TokenKind::JsxAttrStringOpen(_)) => {
            self.advance();
            let s = if matches!(self.peek_kind(), Some(TokenKind::JsxAttrString)) {
              let s = self.peek().unwrap().raw.to_string();
              self.advance();
              s
            } else {
              String::new()
            };
            if matches!(self.peek_kind(), Some(TokenKind::JsxAttrStringClose(_))) {
              self.advance();
            }
            JsxAttrValue::String(s)
          },
          Some(TokenKind::JsxAttrString) => {
            let s = self.peek().unwrap().raw.to_string();
            self.advance();
            JsxAttrValue::String(s)
          },
          Some(TokenKind::ExpressionStart) => {
            self.advance();
            let mut s = String::new();
            while let Some(t) = self.peek() {
              match &t.kind {
                TokenKind::ExpressionEnd | TokenKind::Eof => break,
                _ => {
                  s.push_str(t.raw);
                  self.advance();
                },
              }
            }
            if matches!(self.peek_kind(), Some(TokenKind::ExpressionEnd)) {
              self.advance();
            }
            JsxAttrValue::Expression(s)
          },
          _ => JsxAttrValue::Boolean,
        }
      } else {
        JsxAttrValue::Boolean
      };
      out.push(JsxAttr { name, value, span });
      self.skip_jsx_ws();
    }
    out
  }

  /// JSX fragment `<>...</>`. Cursor at `JsxFragmentOpen`.
  pub(crate) fn parse_jsx_fragment(&mut self) -> Node {
    let span = self.current_span();
    self.advance();
    let mut children = Vec::new();
    loop {
      match self.peek_kind() {
        Some(TokenKind::JsxFragmentClose) => {
          self.advance();
          break;
        },
        Some(TokenKind::Eof) | None => break,
        _ => {
          let before = self.pos;
          if let Some(node) = self.parse_block() {
            children.push(node);
          }
          if self.pos == before {
            self.advance();
          }
        },
      }
    }
    let children = unwrap_jsx_only_paragraphs(children);
    Node::JsxFragment(JsxFragment { children, span })
  }

  /// Standalone `{expr}`. Cursor at `ExpressionStart`.
  pub(crate) fn parse_jsx_expression(&mut self) -> Node {
    let span = self.current_span();
    self.advance();
    let mut s = String::new();
    while let Some(t) = self.peek() {
      match &t.kind {
        TokenKind::ExpressionEnd | TokenKind::Eof => break,
        _ => {
          s.push_str(t.raw);
          self.advance();
        },
      }
    }
    if matches!(self.peek_kind(), Some(TokenKind::ExpressionEnd)) {
      self.advance();
    }
    Node::JsxExpression(JsxExpression { value: s, span })
  }

  /// Skip a markdown comment `{/* ... */}`. Cursor at `MarkdownCommentStart`.
  pub(crate) fn skip_md_comment(&mut self) {
    self.advance();
    while let Some(t) = self.peek() {
      match &t.kind {
        TokenKind::MdxCommentClose => {
          self.advance();
          break;
        },
        TokenKind::Eof => break,
        _ => {
          self.advance();
        },
      }
    }
  }
}

/// Indented block JSX inside a `<Tag>...</Tag>` body looks like
///
///   <TabsList>
///     <TabsTrigger value="cli">CLI</TabsTrigger>
///   </TabsList>
///
/// to the block parser, which sees the leading two-space indent + the
/// JSX opener as inline content and wraps the whole line in a
/// `Paragraph`. That makes the emitted React tree
/// `<TabsList><p>  <TabsTrigger>...</p></TabsList>`, which is wrong both
/// semantically and visually.
///
/// MDX's rule: a JSX element that is the only non-whitespace content
/// on a line is a block child of its enclosing element, *not* a
/// paragraph child. Implement the rule as a post-pass: for each
/// `Paragraph` child, drop pure-whitespace `Text` nodes; if the
/// remainder is one or more JSX nodes only, splice them in as direct
/// children. Otherwise the paragraph stays.
fn unwrap_jsx_only_paragraphs(children: Vec<Node>) -> Vec<Node> {
  // Single-paragraph children unwrap: a JSX element like `<del>*foo*</del>`
  // (the only block child is one Paragraph) renders as raw HTML around
  // inline content per CM 6.6 -- no nested `<p>`. Keeps multi-paragraph
  // JSX bodies intact.
  if children.len() == 1
    && let Some(Node::Paragraph(p)) = children.first()
  {
    return p.children.clone();
  }
  let mut out = Vec::with_capacity(children.len());
  for child in children {
    if let Node::Paragraph(p) = &child {
      let only_jsx = p
        .children
        .iter()
        .filter(|n| !is_whitespace_text(n))
        .all(|n| matches!(n, Node::JsxElement(_) | Node::JsxSelfClosing(_) | Node::JsxFragment(_)));
      let any_jsx =
        p.children.iter().any(|n| matches!(n, Node::JsxElement(_) | Node::JsxSelfClosing(_) | Node::JsxFragment(_)));
      if only_jsx && any_jsx {
        for n in p
          .children
          .iter()
          .filter(|n| matches!(n, Node::JsxElement(_) | Node::JsxSelfClosing(_) | Node::JsxFragment(_)))
        {
          out.push(n.clone());
        }
        continue;
      }
    }
    out.push(child);
  }
  out
}

fn is_whitespace_text(n: &Node) -> bool {
  match n {
    Node::Text(t) => t.value.chars().all(|c| c.is_whitespace()),
    Node::SoftBreak(_) | Node::HardBreak(_) => true,
    _ => false,
  }
}