Skip to main content

marco_core/parser/inlines/
marco_superscript_parser.rs

1//! Superscript parser (extended syntax).
2
3use super::shared::{opt_span_range, GrammarSpan};
4use crate::grammar::inlines as grammar;
5use crate::parser::ast::{Node, NodeKind};
6use nom::IResult;
7
8/// Parse `^text^` and convert to AST node.
9pub fn parse_superscript(input: GrammarSpan) -> IResult<GrammarSpan, Node> {
10    let start = input;
11    let (rest, content) = grammar::superscript(input)?;
12
13    let span = opt_span_range(start, rest);
14
15    let children = match crate::parser::inlines::parse_inlines_from_span(content) {
16        Ok(children) => children,
17        Err(e) => {
18            log::warn!("Failed to parse superscript children: {}", e);
19            vec![]
20        }
21    };
22
23    Ok((
24        rest,
25        Node {
26            kind: NodeKind::Superscript,
27            span,
28            children,
29        },
30    ))
31}
32
33#[cfg(test)]
34mod tests {
35    use super::*;
36
37    #[test]
38    fn smoke_test_parse_superscript() {
39        let input = GrammarSpan::new("^hi^");
40        let (rest, node) = parse_superscript(input).unwrap();
41        assert_eq!(*rest.fragment(), "");
42        assert!(matches!(node.kind, NodeKind::Superscript));
43    }
44}