Skip to main content

oak_tex/parser/
element_type.rs

1use oak_core::{ElementType, UniversalElementRole};
2
3/// Represents an element type in a TeX document.
4#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
5#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
6#[repr(u8)]
7pub enum TexElementType {
8    /// Root node of the AST.
9    Root,
10    /// A source file containing TeX content.
11    SourceFile,
12    /// The document body.
13    Document,
14
15    /// A TeX command (e.g., \section).
16    Command,
17    /// A TeX environment (e.g., \begin{itemize}...\end{itemize}).
18    Environment,
19    /// The beginning of an environment.
20    BeginEnvironment,
21    /// The end of an environment.
22    EndEnvironment,
23
24    /// Mathematical content.
25    MathMode,
26    /// Inline mathematical content ($...$).
27    InlineMath,
28    /// Displayed mathematical content ($$...$$).
29    DisplayMath,
30    /// A grouped set of elements ({...}).
31    Group,
32    /// A superscript expression (^...).
33    Superscript,
34    /// A subscript expression (_...).
35    Subscript,
36
37    /// A generic command argument.
38    Argument,
39    /// An optional command argument ([...]).
40    OptionalArgument,
41    /// A mandatory command argument ({...}).
42    MandatoryArgument,
43
44    /// Plain text content.
45    Text,
46    /// A paragraph of text.
47    Paragraph,
48    /// A section heading.
49    Section,
50    /// A subsection heading.
51    Subsection,
52    /// A subsubsection heading.
53    Subsubsection,
54
55    /// A list environment.
56    List,
57    /// An item within a list.
58    Item,
59    /// A table environment.
60    Table,
61    /// A row within a table.
62    Row,
63    /// A cell within a table row.
64    Cell,
65
66    /// A label for cross-referencing.
67    Label,
68    /// A reference to a label.
69    Reference,
70    /// A bibliographic citation.
71    Citation,
72
73    /// A figure environment.
74    Figure,
75    /// A caption for a figure or table.
76    Caption,
77
78    /// An error node representing a syntax error.
79    Error,
80
81    /// \documentclass command.
82    DocumentClass,
83    /// \usepackage command.
84    UsePackage,
85    /// \begin command.
86    Begin,
87    /// \end command.
88    End,
89    /// \section command.
90    Section_,
91    /// \subsection command.
92    Subsection_,
93    /// \subsubsection command.
94    Subsubsection_,
95    /// \chapter command.
96    Chapter,
97    /// \part command.
98    Part,
99    /// \title command.
100    Title,
101    /// \author command.
102    Author,
103    /// \date command.
104    Date,
105    /// \maketitle command.
106    MakeTitle,
107    /// \tableofcontents command.
108    TableOfContents,
109    /// \newpage command.
110    NewPage,
111    /// \clearpage command.
112    ClearPage,
113
114    /// \frac command.
115    Frac,
116    /// \sqrt command.
117    Sqrt,
118    /// \sum command.
119    Sum,
120    /// \int command.
121    Int,
122    /// \lim command.
123    Lim,
124    /// \alpha command.
125    Alpha,
126    /// \beta command.
127    Beta,
128    /// \gamma command.
129    Gamma,
130    /// \delta command.
131    Delta,
132    /// \epsilon command.
133    Epsilon,
134    /// \zeta command.
135    Zeta,
136    /// \eta command.
137    Eta,
138    /// \theta command.
139    Theta,
140    /// \iota command.
141    Iota,
142    /// \kappa command.
143    Kappa,
144    /// \lambda command.
145    Lambda,
146    /// \mu command.
147    Mu,
148    /// \nu command.
149    Nu,
150    /// \xi command.
151    Xi,
152    /// \omicron command.
153    Omicron,
154    /// \pi command.
155    Pi,
156    /// \rho command.
157    Rho,
158    /// \sigma command.
159    Sigma,
160    /// \tau command.
161    Tau,
162    /// \upsilon command.
163    Upsilon,
164    /// \phi command.
165    Phi,
166    /// \chi command.
167    Chi,
168    /// \psi command.
169    Psi,
170    /// \omega command.
171    Omega,
172    /// \varepsilon command.
173    VarEpsilon,
174    /// \vartheta command.
175    VarTheta,
176    /// \varkappa command.
177    VarKappa,
178    /// \varpi command.
179    VarPi,
180    /// \varrho command.
181    VarRho,
182    /// \varsigma command.
183    VarSigma,
184    /// \varphi command.
185    VarPhi,
186    /// \Gamma command.
187    UpperGamma,
188    /// \Delta command.
189    UpperDelta,
190    /// \Theta command.
191    UpperTheta,
192    /// \Lambda command.
193    UpperLambda,
194    /// \Xi command.
195    UpperXi,
196    /// \Pi command.
197    UpperPi,
198    /// \Sigma command.
199    UpperSigma,
200    /// \Upsilon command.
201    UpperUpsilon,
202    /// \Phi command.
203    UpperPhi,
204    /// \Psi command.
205    UpperPsi,
206    /// \Omega command.
207    UpperOmega,
208
209    /// \textbf command.
210    TextBf,
211    /// \textit command.
212    TextIt,
213    /// \textsc command.
214    TextSc,
215    /// \texttt command.
216    TextTt,
217    /// \emph command.
218    Emph,
219    /// \underline command.
220    Underline,
221
222    /// An identifier.
223    Identifier,
224    /// A string literal.
225    StringLiteral,
226    /// A numeric literal.
227    Number,
228
229    /// Backslash character (\).
230    Backslash,
231    /// Left brace character ({).
232    LeftBrace,
233    /// Right brace character (}).
234    RightBrace,
235    /// Left bracket character ([).
236    LeftBracket,
237    /// Right bracket character (]).
238    RightBracket,
239    /// Left parenthesis character (().
240    LeftParen,
241    /// Right parenthesis character ()).
242    RightParen,
243    /// Dollar sign character ($).
244    Dollar,
245    /// Double dollar sign character ($$).
246    DoubleDollar,
247    /// Ampersand character (&).
248    Ampersand,
249    /// Percent character (%).
250    Percent,
251    /// Hash character (#).
252    Hash,
253    /// Caret character (^).
254    Caret,
255    /// Underscore character (_).
256    Underscore,
257    /// Tilde character (~).
258    Tilde,
259
260    /// Equals character (=).
261    Equal,
262    /// Double equals character (==).
263    Equals,
264    /// Plus character (+).
265    Plus,
266    /// Minus character (-).
267    Minus,
268    /// Asterisk character (*).
269    Star,
270    /// Slash character (/).
271    Slash,
272    /// Pipe character (|).
273    Pipe,
274    /// Less than character (<).
275    Less,
276    /// Less than or equal to character (<=).
277    LessThan,
278    /// Greater than character (>).
279    Greater,
280    /// Greater than or equal to character (>=).
281    GreaterThan,
282    /// Exclamation mark character (!).
283    Exclamation,
284    /// Question mark character (?).
285    Question,
286    /// At sign character (@).
287    At,
288    /// Colon character (:).
289    Colon,
290    /// Semicolon character (;).
291    Semicolon,
292    /// Comma character (,).
293    Comma,
294    /// Dot character (.).
295    Dot,
296
297    /// A comment starting with %.
298    Comment,
299    /// Whitespace characters.
300    Whitespace,
301    /// A newline character.
302    Newline,
303
304    /// 'begin' keyword.
305    BeginKeyword,
306    /// 'end' keyword.
307    EndKeyword,
308    /// 'documentclass' keyword.
309    DocumentclassKeyword,
310    /// 'usepackage' keyword.
311    UsepackageKeyword,
312    /// 'section' keyword.
313    SectionKeyword,
314    /// 'subsection' keyword.
315    SubsectionKeyword,
316    /// 'subsubsection' keyword.
317    SubsubsectionKeyword,
318    /// 'chapter' keyword.
319    ChapterKeyword,
320    /// 'part' keyword.
321    PartKeyword,
322    /// 'title' keyword.
323    TitleKeyword,
324    /// 'author' keyword.
325    AuthorKeyword,
326    /// 'date' keyword.
327    DateKeyword,
328    /// 'maketitle' keyword.
329    MaketitleKeyword,
330    /// 'tableofcontents' keyword.
331    TableofcontentsKeyword,
332    /// 'item' keyword.
333    ItemKeyword,
334    /// 'label' keyword.
335    LabelKeyword,
336    /// 'ref' keyword.
337    RefKeyword,
338    /// 'cite' keyword.
339    CiteKeyword,
340    /// 'includegraphics' keyword.
341    IncludegraphicsKeyword,
342    /// \textbf keyword.
343    TextbfKeyword,
344    /// \textit keyword.
345    TextitKeyword,
346    /// \emph keyword.
347    EmphKeyword,
348
349    /// End of file marker.
350    Eof,
351}
352
353impl ElementType for TexElementType {
354    type Role = UniversalElementRole;
355
356    fn role(&self) -> Self::Role {
357        match self {
358            _ => UniversalElementRole::None,
359        }
360    }
361}
362
363impl oak_core::language::TokenType for TexElementType {
364    type Role = oak_core::UniversalTokenRole;
365    const END_OF_STREAM: Self = Self::Eof;
366
367    fn is_ignored(&self) -> bool {
368        matches!(self, Self::Whitespace | Self::Newline | Self::Comment)
369    }
370
371    fn role(&self) -> Self::Role {
372        match self {
373            Self::Whitespace | Self::Newline => oak_core::UniversalTokenRole::Whitespace,
374            Self::Comment => oak_core::UniversalTokenRole::Comment,
375            Self::Eof => oak_core::UniversalTokenRole::Eof,
376            Self::Error => oak_core::UniversalTokenRole::Error,
377            _ => oak_core::UniversalTokenRole::None,
378        }
379    }
380}
381
382impl From<crate::lexer::token_type::TexTokenType> for TexElementType {
383    fn from(token: crate::lexer::token_type::TexTokenType) -> Self {
384        use crate::lexer::token_type::TexTokenType as T;
385        match token {
386            T::Root => Self::Root,
387            T::SourceFile => Self::SourceFile,
388            T::Document => Self::Document,
389            T::Command => Self::Command,
390            T::Environment => Self::Environment,
391            T::BeginEnvironment => Self::BeginEnvironment,
392            T::EndEnvironment => Self::EndEnvironment,
393            T::MathMode => Self::MathMode,
394            T::InlineMath => Self::InlineMath,
395            T::DisplayMath => Self::DisplayMath,
396            T::Group => Self::Group,
397            T::Superscript => Self::Superscript,
398            T::Subscript => Self::Subscript,
399            T::Argument => Self::Argument,
400            T::OptionalArgument => Self::OptionalArgument,
401            T::MandatoryArgument => Self::MandatoryArgument,
402            T::Text => Self::Text,
403            T::Paragraph => Self::Paragraph,
404            T::Section => Self::Section,
405            T::Subsection => Self::Subsection,
406            T::Subsubsection => Self::Subsubsection,
407            T::List => Self::List,
408            T::Item => Self::Item,
409            T::Table => Self::Table,
410            T::Row => Self::Row,
411            T::Cell => Self::Cell,
412            T::Label => Self::Label,
413            T::Reference => Self::Reference,
414            T::Citation => Self::Citation,
415            T::Figure => Self::Figure,
416            T::Caption => Self::Caption,
417            T::Error => Self::Error,
418            T::DocumentClass => Self::DocumentClass,
419            T::UsePackage => Self::UsePackage,
420            T::Begin => Self::Begin,
421            T::End => Self::End,
422            T::Section_ => Self::Section_,
423            T::Subsection_ => Self::Subsection_,
424            T::Subsubsection_ => Self::Subsubsection_,
425            T::Chapter => Self::Chapter,
426            T::Part => Self::Part,
427            T::Title => Self::Title,
428            T::Author => Self::Author,
429            T::Date => Self::Date,
430            T::MakeTitle => Self::MakeTitle,
431            T::TableOfContents => Self::TableOfContents,
432            T::NewPage => Self::NewPage,
433            T::ClearPage => Self::ClearPage,
434            T::Frac => Self::Frac,
435            T::Sqrt => Self::Sqrt,
436            T::Sum => Self::Sum,
437            T::Int => Self::Int,
438            T::Lim => Self::Lim,
439            T::Alpha => Self::Alpha,
440            T::Beta => Self::Beta,
441            T::Gamma => Self::Gamma,
442            T::Delta => Self::Delta,
443            T::Epsilon => Self::Epsilon,
444            T::Zeta => Self::Zeta,
445            T::Eta => Self::Eta,
446            T::Theta => Self::Theta,
447            T::Iota => Self::Iota,
448            T::Kappa => Self::Kappa,
449            T::Lambda => Self::Lambda,
450            T::Mu => Self::Mu,
451            T::Nu => Self::Nu,
452            T::Xi => Self::Xi,
453            T::Omicron => Self::Omicron,
454            T::Pi => Self::Pi,
455            T::Rho => Self::Rho,
456            T::Sigma => Self::Sigma,
457            T::Tau => Self::Tau,
458            T::Upsilon => Self::Upsilon,
459            T::Phi => Self::Phi,
460            T::Chi => Self::Chi,
461            T::Psi => Self::Psi,
462            T::Omega => Self::Omega,
463            T::VarEpsilon => Self::VarEpsilon,
464            T::VarTheta => Self::VarTheta,
465            T::VarKappa => Self::VarKappa,
466            T::VarPi => Self::VarPi,
467            T::VarRho => Self::VarRho,
468            T::VarSigma => Self::VarSigma,
469            T::VarPhi => Self::VarPhi,
470            T::UpperGamma => Self::UpperGamma,
471            T::UpperDelta => Self::UpperDelta,
472            T::UpperTheta => Self::UpperTheta,
473            T::UpperLambda => Self::UpperLambda,
474            T::UpperXi => Self::UpperXi,
475            T::UpperPi => Self::UpperPi,
476            T::UpperSigma => Self::UpperSigma,
477            T::UpperUpsilon => Self::UpperUpsilon,
478            T::UpperPhi => Self::UpperPhi,
479            T::UpperPsi => Self::UpperPsi,
480            T::UpperOmega => Self::UpperOmega,
481            T::TextBf => Self::TextBf,
482            T::TextIt => Self::TextIt,
483            T::TextSc => Self::TextSc,
484            T::TextTt => Self::TextTt,
485            T::Emph => Self::Emph,
486            T::Underline => Self::Underline,
487            T::Identifier => Self::Identifier,
488            T::StringLiteral => Self::StringLiteral,
489            T::Number => Self::Number,
490            T::Backslash => Self::Backslash,
491            T::LeftBrace => Self::LeftBrace,
492            T::RightBrace => Self::RightBrace,
493            T::LeftBracket => Self::LeftBracket,
494            T::RightBracket => Self::RightBracket,
495            T::LeftParen => Self::LeftParen,
496            T::RightParen => Self::RightParen,
497            T::Dollar => Self::Dollar,
498            T::DoubleDollar => Self::DoubleDollar,
499            T::Ampersand => Self::Ampersand,
500            T::Percent => Self::Percent,
501            T::Hash => Self::Hash,
502            T::Caret => Self::Caret,
503            T::Underscore => Self::Underscore,
504            T::Tilde => Self::Tilde,
505            T::Equal => Self::Equal,
506            T::Equals => Self::Equals,
507            T::Plus => Self::Plus,
508            T::Minus => Self::Minus,
509            T::Star => Self::Star,
510            T::Slash => Self::Slash,
511            T::Pipe => Self::Pipe,
512            T::Less => Self::Less,
513            T::LessThan => Self::LessThan,
514            T::Greater => Self::Greater,
515            T::GreaterThan => Self::GreaterThan,
516            T::Exclamation => Self::Exclamation,
517            T::Question => Self::Question,
518            T::At => Self::At,
519            T::Colon => Self::Colon,
520            T::Semicolon => Self::Semicolon,
521            T::Comma => Self::Comma,
522            T::Dot => Self::Dot,
523            T::Comment => Self::Comment,
524            T::Whitespace => Self::Whitespace,
525            T::Newline => Self::Newline,
526            T::BeginKeyword => Self::BeginKeyword,
527            T::EndKeyword => Self::EndKeyword,
528            T::DocumentclassKeyword => Self::DocumentclassKeyword,
529            T::UsepackageKeyword => Self::UsepackageKeyword,
530            T::SectionKeyword => Self::SectionKeyword,
531            T::SubsectionKeyword => Self::SubsectionKeyword,
532            T::SubsubsectionKeyword => Self::SubsubsectionKeyword,
533            T::ChapterKeyword => Self::ChapterKeyword,
534            T::PartKeyword => Self::PartKeyword,
535            T::TitleKeyword => Self::TitleKeyword,
536            T::AuthorKeyword => Self::AuthorKeyword,
537            T::DateKeyword => Self::DateKeyword,
538            T::MaketitleKeyword => Self::MaketitleKeyword,
539            T::TableofcontentsKeyword => Self::TableofcontentsKeyword,
540            T::ItemKeyword => Self::ItemKeyword,
541            T::LabelKeyword => Self::LabelKeyword,
542            T::RefKeyword => Self::RefKeyword,
543            T::CiteKeyword => Self::CiteKeyword,
544            T::IncludegraphicsKeyword => Self::IncludegraphicsKeyword,
545            T::TextbfKeyword => Self::TextbfKeyword,
546            T::TextitKeyword => Self::TextitKeyword,
547            T::EmphKeyword => Self::EmphKeyword,
548            T::Eof => Self::Eof,
549        }
550    }
551}