Skip to main content

oak_tex/ast/
mod.rs

1#![doc = include_str!("readme.md")]
2use core::range::Range;
3
4/// Root node of the TeX AST.
5#[derive(Debug, Clone)]
6#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
7pub struct TexRoot {
8    /// The span of the root node.
9    #[cfg_attr(feature = "serde", serde(with = "oak_core::serde_range"))]
10    pub span: Range<usize>,
11    /// The items contained in the root node.
12    pub items: Vec<TexItem>,
13}
14
15/// A top-level item in a TeX document.
16#[derive(Debug, Clone)]
17#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
18pub enum TexItem {
19    /// A TeX command.
20    Command(TexCommand),
21    /// A TeX environment.
22    Environment(TexEnvironment),
23    /// A TeX group.
24    Group(TexGroup),
25    /// A TeX math environment.
26    Math(TexMath),
27    /// A superscript.
28    Superscript(TexSuperscript),
29    /// A subscript.
30    Subscript(TexSubscript),
31    /// Plain text.
32    Text {
33        /// The span of the text.
34        #[cfg_attr(feature = "serde", serde(with = "oak_core::serde_range"))]
35        span: Range<usize>,
36        /// The content of the text.
37        content: String,
38    },
39    /// A comment.
40    Comment {
41        /// The span of the comment.
42        #[cfg_attr(feature = "serde", serde(with = "oak_core::serde_range"))]
43        span: Range<usize>,
44        /// The content of the comment.
45        content: String,
46    },
47}
48
49/// A TeX environment (e.g., \begin{matrix} ... \end{matrix}).
50#[derive(Debug, Clone)]
51#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
52pub struct TexEnvironment {
53    /// The span of the environment.
54    #[cfg_attr(feature = "serde", serde(with = "oak_core::serde_range"))]
55    pub span: Range<usize>,
56    /// The name of the environment.
57    pub name: String,
58    /// The arguments to the environment.
59    pub arguments: Vec<TexArgument>,
60    /// The content of the environment.
61    pub content: TexRoot,
62}
63
64/// A TeX superscript (^).
65#[derive(Debug, Clone)]
66#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
67pub struct TexSuperscript {
68    /// The span of the superscript.
69    #[cfg_attr(feature = "serde", serde(with = "oak_core::serde_range"))]
70    pub span: Range<usize>,
71    /// The target of the superscript.
72    pub target: Option<Box<TexItem>>,
73    /// The content of the superscript.
74    pub content: Box<TexRoot>,
75}
76
77/// A TeX subscript (_).
78#[derive(Debug, Clone)]
79#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
80pub struct TexSubscript {
81    /// The span of the subscript.
82    #[cfg_attr(feature = "serde", serde(with = "oak_core::serde_range"))]
83    pub span: Range<usize>,
84    /// The target of the subscript.
85    pub target: Option<Box<TexItem>>,
86    /// The content of the subscript.
87    pub content: Box<TexRoot>,
88}
89
90/// A TeX math environment ($...$ or $$...$$).
91#[derive(Debug, Clone)]
92#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
93pub struct TexMath {
94    /// The span of the math environment.
95    #[cfg_attr(feature = "serde", serde(with = "oak_core::serde_range"))]
96    pub span: Range<usize>,
97    /// The content of the math environment.
98    pub content: TexRoot,
99    /// Whether this is a display math environment.
100    pub is_display: bool,
101}
102
103/// A TeX command (e.g., \section, \textbf).
104#[derive(Debug, Clone)]
105#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
106pub struct TexCommand {
107    /// The span of the command.
108    #[cfg_attr(feature = "serde", serde(with = "oak_core::serde_range"))]
109    pub span: Range<usize>,
110    /// The name of the command.
111    pub name: String,
112    /// The arguments to the command.
113    pub arguments: Vec<TexArgument>,
114}
115
116/// A TeX argument.
117#[derive(Debug, Clone)]
118#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
119pub enum TexArgument {
120    /// An optional argument ([...]).
121    Optional(TexRoot),
122    /// A mandatory argument ({...}).
123    Required(TexRoot),
124}
125
126/// A TeX group (e.g., { ... }).
127#[derive(Debug, Clone)]
128#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
129pub struct TexGroup {
130    /// The span of the group.
131    #[cfg_attr(feature = "serde", serde(with = "oak_core::serde_range"))]
132    pub span: Range<usize>,
133    /// The content of the group.
134    pub content: TexRoot,
135}
136
137impl TexRoot {
138    /// Creates a new TeX root node.
139    pub fn new(span: Range<usize>) -> Self {
140        Self { span, items: Vec::new() }
141    }
142}