panache-parser 0.3.0

Lossless CST parser and syntax wrappers for Pandoc markdown, Quarto, and RMarkdown
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
//! Citation parsing for Pandoc's citations extension.
//!
//! Syntax:
//! - Bracketed: `[@doe99]`, `[@doe99; @smith2000]`
//! - With locator: `[see @doe99, pp. 33-35]`
//! - Suppress author: `[-@doe99]`
//! - Author-in-text: `@doe99` (bare, without brackets)

use crate::syntax::SyntaxKind;
use rowan::GreenNodeBuilder;

/// Try to parse a bracketed citation starting at the current position.
/// Returns Some((length, content)) if successful, None otherwise.
///
/// Bracketed citations have the syntax: [@key], [@key1; @key2], [see @key, pp. 1-10]
pub(crate) fn try_parse_bracketed_citation(text: &str) -> Option<(usize, &str)> {
    let bytes = text.as_bytes();

    // Must start with [
    if bytes.is_empty() || bytes[0] != b'[' {
        return None;
    }

    // Look ahead to see if this contains a citation marker (@)
    // We need to distinguish from regular links
    let mut has_citation = false;
    let mut pos = 1;
    let mut bracket_depth = 0;

    while pos < bytes.len() {
        match bytes[pos] {
            b'\\' => {
                // Skip escaped character
                pos += 2;
                continue;
            }
            b'[' => {
                bracket_depth += 1;
                pos += 1;
            }
            b']' => {
                if bracket_depth == 0 {
                    // Closing bracket of main citation - stop looking
                    break;
                }
                bracket_depth -= 1;
                pos += 1;
            }
            b'@' => {
                // Found a citation marker - this is likely a citation
                has_citation = true;
                break;
            }
            b'(' if bracket_depth == 0 => {
                // Opening paren at top level suggests this might be a link [text](url)
                // Not a citation
                break;
            }
            _ => {
                pos += 1;
            }
        }
    }

    if !has_citation {
        return None;
    }

    // Now find the closing bracket
    pos = 1;
    bracket_depth = 1;

    while pos < bytes.len() {
        match bytes[pos] {
            b'\\' => {
                // Skip escaped character
                pos += 2;
                continue;
            }
            b'[' => {
                bracket_depth += 1;
                pos += 1;
            }
            b']' => {
                bracket_depth -= 1;
                if bracket_depth == 0 {
                    // Found the closing bracket
                    let content = &text[1..pos];
                    return Some((pos + 1, content));
                }
                pos += 1;
            }
            _ => {
                pos += 1;
            }
        }
    }

    // No closing bracket found
    None
}

/// Try to parse a bare citation (author-in-text) starting at the current position.
/// Returns Some((length, key, has_suppress)) if successful, None otherwise.
///
/// Bare citations have the syntax: @key or -@key
pub(crate) fn try_parse_bare_citation(text: &str) -> Option<(usize, &str, bool)> {
    let bytes = text.as_bytes();

    if bytes.is_empty() {
        return None;
    }

    let mut pos = 0;
    let has_suppress = bytes[pos] == b'-';

    if has_suppress {
        pos += 1;
        if pos >= bytes.len() {
            return None;
        }
    }

    // Must have @ next
    if bytes[pos] != b'@' {
        return None;
    }
    pos += 1;

    if pos >= bytes.len() {
        return None;
    }

    // Parse the citation key
    let key_start = pos;
    let key_len = parse_citation_key(&text[pos..])?;

    if key_len == 0 {
        return None;
    }

    let total_len = pos + key_len;
    let key = &text[key_start..total_len];

    Some((total_len, key, has_suppress))
}

/// Try to parse a Quarto cross-reference key (e.g., @fig-plot, @eq-energy).
pub fn is_quarto_crossref_key(key: &str) -> bool {
    let lower = key.to_ascii_lowercase();
    let mut parts = lower.splitn(2, '-');
    let prefix = parts.next().unwrap_or("");
    let rest = parts.next().unwrap_or("");
    if rest.is_empty() {
        return false;
    }
    matches!(
        prefix,
        "fig"
            | "tbl"
            | "lst"
            | "tip"
            | "nte"
            | "wrn"
            | "imp"
            | "cau"
            | "thm"
            | "lem"
            | "cor"
            | "prp"
            | "cnj"
            | "def"
            | "exm"
            | "exr"
            | "sol"
            | "rem"
            | "alg"
            | "eq"
            | "sec"
    )
}

pub const BOOKDOWN_LABEL_PREFIXES: &[&str] = &[
    "eq", "fig", "tab", "thm", "lem", "cor", "prp", "cnj", "def", "exm", "exr", "sol", "rem",
    "alg", "sec", "hyp",
];

pub fn is_bookdown_label(label: &str) -> bool {
    BOOKDOWN_LABEL_PREFIXES.contains(&label)
}

pub fn has_bookdown_prefix(label: &str) -> bool {
    let mut parts = label.splitn(2, ':');
    let prefix = parts.next().unwrap_or("");
    let rest = parts.next().unwrap_or("");
    if rest.is_empty() {
        return false;
    }
    is_bookdown_label(prefix)
}

pub(crate) fn emit_crossref(builder: &mut GreenNodeBuilder, key: &str, has_suppress: bool) {
    builder.start_node(SyntaxKind::CROSSREF.into());

    if has_suppress {
        builder.token(SyntaxKind::CROSSREF_MARKER.into(), "-@");
    } else {
        builder.token(SyntaxKind::CROSSREF_MARKER.into(), "@");
    }

    if key.starts_with('{') && key.ends_with('}') {
        builder.token(SyntaxKind::CROSSREF_BRACE_OPEN.into(), "{");
        builder.token(SyntaxKind::CROSSREF_KEY.into(), &key[1..key.len() - 1]);
        builder.token(SyntaxKind::CROSSREF_BRACE_CLOSE.into(), "}");
    } else {
        builder.token(SyntaxKind::CROSSREF_KEY.into(), key);
    }

    builder.finish_node();
}

pub(crate) fn emit_bookdown_crossref(builder: &mut GreenNodeBuilder, key: &str) {
    builder.start_node(SyntaxKind::CROSSREF.into());
    builder.token(SyntaxKind::CROSSREF_BOOKDOWN_OPEN.into(), "\\@ref(");
    builder.token(SyntaxKind::CROSSREF_KEY.into(), key);
    builder.token(SyntaxKind::CROSSREF_BOOKDOWN_CLOSE.into(), ")");
    builder.finish_node();
}

/// Parse a citation key following Pandoc's rules.
/// Returns the length of the key, or None if invalid.
///
/// Citation keys:
/// - Must start with letter, digit, or _
/// - Can contain alphanumerics and single internal punctuation: :.#$%&-+?<>~/
/// - Keys in braces @{...} can contain anything
/// - Double internal punctuation terminates key
/// - Trailing punctuation not included
fn parse_citation_key(text: &str) -> Option<usize> {
    if text.is_empty() {
        return None;
    }

    // Check for braced key: @{...}
    if text.starts_with('{') {
        // Find matching closing brace
        let mut escape_next = false;

        for (idx, ch) in text.char_indices().skip(1) {
            if escape_next {
                escape_next = false;
                continue;
            }

            match ch {
                '\\' => escape_next = true,
                '}' => return Some(idx + ch.len_utf8()),
                _ => {}
            }
        }

        // No closing brace found
        return None;
    }

    // Regular key: must start with letter, digit, or _
    let mut iter = text.char_indices();
    let (_, first_char) = iter.next()?;
    if !first_char.is_alphanumeric() && first_char != '_' {
        return None;
    }

    let mut last_alnum_end = first_char.len_utf8();
    let mut last_included_end = last_alnum_end;
    let mut last_punct_start: Option<usize> = None;
    let mut prev_was_punct = false;

    for (idx, ch) in iter {
        if ch.is_alphanumeric() || ch == '_' {
            prev_was_punct = false;
            last_alnum_end = idx + ch.len_utf8();
            last_included_end = last_alnum_end;
            last_punct_start = None;
        } else if is_internal_punctuation(ch) {
            // Check if previous was also punctuation (double punct terminates)
            if prev_was_punct {
                // Double punctuation - terminate before the first punctuation
                return Some(last_punct_start.unwrap_or(last_alnum_end));
            }
            prev_was_punct = true;
            last_punct_start = Some(idx);
            last_included_end = idx + ch.len_utf8();
        } else {
            // Not a valid key character - terminate here
            break;
        }
    }

    if prev_was_punct {
        return Some(last_alnum_end);
    }

    if last_included_end == 0 {
        None
    } else {
        Some(last_included_end)
    }
}

/// Check if a character is valid internal punctuation in citation keys.
fn is_internal_punctuation(ch: char) -> bool {
    matches!(
        ch,
        ':' | '.' | '#' | '$' | '%' | '&' | '-' | '+' | '?' | '<' | '>' | '~' | '/'
    )
}

/// Emit a bracketed citation node to the builder.
pub(crate) fn emit_bracketed_citation(builder: &mut GreenNodeBuilder, content: &str) {
    builder.start_node(SyntaxKind::CITATION.into());

    // Opening bracket
    builder.token(SyntaxKind::LINK_START.into(), "[");

    // Emit prefix + citations + suffix with fine-grained tokens.
    emit_bracketed_citation_content(builder, content);

    // Closing bracket
    builder.token(SyntaxKind::LINK_DEST.into(), "]");

    builder.finish_node();
}

fn emit_bracketed_citation_content(builder: &mut GreenNodeBuilder, content: &str) {
    let mut text_start = 0;
    let mut iter = content.char_indices().peekable();

    while let Some((idx, ch)) = iter.next() {
        if ch == '@' || (ch == '-' && matches!(iter.peek(), Some((_, '@')))) {
            if idx > text_start {
                builder.token(
                    SyntaxKind::CITATION_CONTENT.into(),
                    &content[text_start..idx],
                );
            }

            let mut marker_len = 1;
            let marker_text = if ch == '-' {
                iter.next();
                marker_len = 2;
                "-@"
            } else {
                "@"
            };
            builder.token(SyntaxKind::CITATION_MARKER.into(), marker_text);

            let key_start = idx + marker_len;
            if key_start >= content.len() {
                text_start = key_start;
                continue;
            }

            if let Some(key_len) = parse_citation_key(&content[key_start..]) {
                let key_end = key_start + key_len;
                let key = &content[key_start..key_end];
                if key.starts_with('{') && key.ends_with('}') {
                    builder.token(SyntaxKind::CITATION_BRACE_OPEN.into(), "{");
                    if key.len() > 2 {
                        builder.token(SyntaxKind::CITATION_KEY.into(), &key[1..key.len() - 1]);
                    }
                    builder.token(SyntaxKind::CITATION_BRACE_CLOSE.into(), "}");
                } else {
                    builder.token(SyntaxKind::CITATION_KEY.into(), key);
                }
                while matches!(iter.peek(), Some((next_idx, _)) if *next_idx < key_end) {
                    iter.next();
                }
                text_start = key_end;
                continue;
            }

            text_start = key_start;
            continue;
        }

        if ch == ';' {
            if idx > text_start {
                builder.token(
                    SyntaxKind::CITATION_CONTENT.into(),
                    &content[text_start..idx],
                );
            }
            builder.token(SyntaxKind::CITATION_SEPARATOR.into(), ";");
            text_start = idx + ch.len_utf8();
            continue;
        }
    }

    if text_start < content.len() {
        builder.token(SyntaxKind::CITATION_CONTENT.into(), &content[text_start..]);
    }
}

/// Emit a bare citation node to the builder.
pub(crate) fn emit_bare_citation(builder: &mut GreenNodeBuilder, key: &str, has_suppress: bool) {
    builder.start_node(SyntaxKind::CITATION.into());

    // Emit marker (@ or -@)
    if has_suppress {
        builder.token(SyntaxKind::CITATION_MARKER.into(), "-@");
    } else {
        builder.token(SyntaxKind::CITATION_MARKER.into(), "@");
    }

    // Check if key is braced
    if key.starts_with('{') && key.ends_with('}') {
        builder.token(SyntaxKind::CITATION_BRACE_OPEN.into(), "{");
        builder.token(SyntaxKind::CITATION_KEY.into(), &key[1..key.len() - 1]);
        builder.token(SyntaxKind::CITATION_BRACE_CLOSE.into(), "}");
    } else {
        builder.token(SyntaxKind::CITATION_KEY.into(), key);
    }

    builder.finish_node();
}

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

    // Citation key parsing tests
    #[test]
    fn test_parse_simple_citation_key() {
        assert_eq!(parse_citation_key("doe99"), Some(5));
        assert_eq!(parse_citation_key("smith2000"), Some(9));
    }

    #[test]
    fn test_parse_citation_key_with_internal_punct() {
        assert_eq!(parse_citation_key("Foo_bar.baz"), Some(11));
        assert_eq!(parse_citation_key("author:2020"), Some(11));
    }

    #[test]
    fn test_parse_citation_key_trailing_punct() {
        // Trailing punctuation should be excluded
        assert_eq!(parse_citation_key("Foo_bar.baz."), Some(11));
        assert_eq!(parse_citation_key("key:value:"), Some(9));
    }

    #[test]
    fn test_parse_citation_key_double_punct() {
        // Double punctuation terminates key
        assert_eq!(parse_citation_key("Foo_bar--baz"), Some(7)); // key is "Foo_bar"
    }

    #[test]
    fn test_parse_citation_key_with_braces() {
        assert_eq!(parse_citation_key("{https://example.com}"), Some(21));
        assert_eq!(parse_citation_key("{Foo_bar.baz.}"), Some(14));
    }

    #[test]
    fn test_parse_citation_key_invalid_start() {
        assert_eq!(parse_citation_key(".invalid"), None);
        assert_eq!(parse_citation_key(":invalid"), None);
    }

    #[test]
    fn test_parse_citation_key_stops_at_space() {
        assert_eq!(parse_citation_key("key rest"), Some(3));
    }

    // Bare citation parsing tests
    #[test]
    fn test_parse_bare_citation_simple() {
        let result = try_parse_bare_citation("@doe99");
        assert_eq!(result, Some((6, "doe99", false)));
    }

    #[test]
    fn test_parse_bare_citation_with_suppress() {
        let result = try_parse_bare_citation("-@smith04");
        assert_eq!(result, Some((9, "smith04", true)));
    }

    #[test]
    fn test_parse_bare_citation_with_trailing_text() {
        let result = try_parse_bare_citation("@doe99 says");
        assert_eq!(result, Some((6, "doe99", false)));
    }

    #[test]
    fn test_parse_bare_citation_braced_key() {
        let result = try_parse_bare_citation("@{https://example.com}");
        assert_eq!(result, Some((22, "{https://example.com}", false)));
    }

    #[test]
    fn test_parse_bare_citation_not_citation() {
        assert_eq!(try_parse_bare_citation("not a citation"), None);
        assert_eq!(try_parse_bare_citation("@"), None);
    }

    // Bracketed citation parsing tests
    #[test]
    fn test_parse_bracketed_citation_simple() {
        let result = try_parse_bracketed_citation("[@doe99]");
        assert_eq!(result, Some((8, "@doe99")));
    }

    #[test]
    fn test_parse_bracketed_citation_multiple() {
        let result = try_parse_bracketed_citation("[@doe99; @smith2000]");
        assert_eq!(result, Some((20, "@doe99; @smith2000")));
    }

    #[test]
    fn test_parse_bracketed_citation_with_prefix() {
        let result = try_parse_bracketed_citation("[see @doe99]");
        assert_eq!(result, Some((12, "see @doe99")));
    }

    #[test]
    fn test_parse_bracketed_citation_with_locator() {
        let result = try_parse_bracketed_citation("[@doe99, pp. 33-35]");
        assert_eq!(result, Some((19, "@doe99, pp. 33-35")));
    }

    #[test]
    fn test_parse_bracketed_citation_complex() {
        let result = try_parse_bracketed_citation("[see @doe99, pp. 33-35 and *passim*]");
        assert_eq!(result, Some((36, "see @doe99, pp. 33-35 and *passim*")));
    }

    #[test]
    fn test_parse_bracketed_citation_with_suppress() {
        let result = try_parse_bracketed_citation("[-@doe99]");
        assert_eq!(result, Some((9, "-@doe99")));
    }

    #[test]
    fn test_parse_bracketed_citation_not_citation() {
        // Regular link should not be parsed as citation
        assert_eq!(try_parse_bracketed_citation("[text](url)"), None);
        assert_eq!(try_parse_bracketed_citation("[just text]"), None);
    }

    #[test]
    fn test_parse_bracketed_citation_nested_brackets() {
        let result = try_parse_bracketed_citation("[see [nested] @doe99]");
        assert_eq!(result, Some((21, "see [nested] @doe99")));
    }

    #[test]
    fn test_parse_bracketed_citation_escaped_bracket() {
        let result = try_parse_bracketed_citation(r"[@doe99 with \] escaped]");
        assert_eq!(result, Some((24, r"@doe99 with \] escaped")));
    }
}