Skip to main content

katex_parser/
macro_definition.rs

1use crate::token::Token;
2
3#[derive(Debug, Clone, PartialEq)]
4/// A macro expansion with its argument count and delimiters.
5pub struct MacroExpansion {
6    pub tokens: Vec<Token>,
7    pub num_args: usize,
8    pub delimiters: Option<Vec<Vec<String>>>,
9    pub unexpandable: bool,
10}
11
12impl MacroExpansion {
13    pub fn new(
14        tokens: Vec<Token>,
15        num_args: usize,
16        delimiters: Option<Vec<Vec<String>>>,
17        unexpandable: bool,
18    ) -> Self {
19        MacroExpansion {
20            tokens,
21            num_args,
22            delimiters,
23            unexpandable,
24        }
25    }
26}
27
28#[derive(Debug, Clone, PartialEq)]
29/// A macro body: raw text or a pre-expanded token list.
30pub enum MacroDefinition {
31    Text(String),
32    Expansion(MacroExpansion),
33}
34
35impl MacroDefinition {
36    pub fn text(expansion: impl Into<String>) -> Self {
37        MacroDefinition::Text(expansion.into())
38    }
39
40    pub fn expansion(expansion: MacroExpansion) -> Self {
41        MacroDefinition::Expansion(expansion)
42    }
43
44    pub fn as_text(&self) -> Option<&str> {
45        if let MacroDefinition::Text(text) = self {
46            Some(text)
47        } else {
48            None
49        }
50    }
51}
52
53/// A consumed macro argument: the opening and closing tokens plus the
54/// normalized inner token list.
55pub(crate) struct MacroArgument {
56    pub start: Token,
57    pub end: Token,
58    pub tokens: Vec<Token>,
59}