Skip to main content

oak_mojo/parser/
element_type.rs

1use crate::lexer::MojoTokenType;
2use oak_core::UniversalElementRole;
3
4/// Element types for the Mojo language parser.
5///
6/// This enum represents all possible element types in the Mojo language,
7/// including tokens mapped from `MojoTokenType`, statement types, expression types,
8/// and special node types.
9#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
10#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
11pub enum MojoElementType {
12    // Tokens (mapped from MojoTokenType)
13    /// Function keyword `fn`.
14    Fn,
15    /// Struct keyword `struct`.
16    Struct,
17    /// Variable keyword `var`.
18    Var,
19    /// Let keyword `let`.
20    Let,
21    /// If keyword `if`.
22    If,
23    /// Else keyword `else`.
24    Else,
25    /// While keyword `while`.
26    While,
27    /// For keyword `for`.
28    For,
29    /// In keyword `in`.
30    In,
31    /// Return keyword `return`.
32    Return,
33    /// Break keyword `break`.
34    Break,
35    /// Continue keyword `continue`.
36    Continue,
37    /// Import keyword `import`.
38    Import,
39    /// From keyword `from`.
40    From,
41    /// Boolean literal `True`.
42    True,
43    /// Boolean literal `False`.
44    False,
45    /// None literal.
46    None,
47    /// Identifier token.
48    Identifier,
49    /// Integer literal token.
50    Integer,
51    /// Float literal token.
52    Float,
53    /// String literal token.
54    String,
55    /// Plus operator `+`.
56    Plus,
57    /// Minus operator `-`.
58    Minus,
59    /// Star operator `*`.
60    Star,
61    /// Slash operator `/`.
62    Slash,
63    /// Percent operator `%`.
64    Percent,
65    /// Assignment operator `=`.
66    Equal,
67    /// Equality operator `==`.
68    EqualEqual,
69    /// Inequality operator `!=`.
70    NotEqual,
71    /// Less than operator `<`.
72    Less,
73    /// Less than or equal operator `<=`.
74    LessEqual,
75    /// Greater than operator `>`.
76    Greater,
77    /// Greater than or equal operator `>=`.
78    GreaterEqual,
79    /// Logical and operator `and`.
80    And,
81    /// Logical or operator `or`.
82    Or,
83    /// Logical not operator `not`.
84    Not,
85    /// Left parenthesis `(`.
86    LeftParen,
87    /// Right parenthesis `)`.
88    RightParen,
89    /// Left bracket `[`.
90    LeftBracket,
91    /// Right bracket `]`.
92    RightBracket,
93    /// Left brace `{`.
94    LeftBrace,
95    /// Right brace `}`.
96    RightBrace,
97    /// Comma `,`.
98    Comma,
99    /// Dot `.`.
100    Dot,
101    /// Colon `:`.
102    Colon,
103    /// Semicolon `;`.
104    Semicolon,
105    /// Arrow operator `->`.
106    Arrow,
107    /// Whitespace token.
108    Whitespace,
109    /// Newline token.
110    Newline,
111    /// Comment token.
112    Comment,
113    /// Indent token for significant whitespace.
114    Indent,
115    /// Dedent token for significant whitespace.
116    Dedent,
117    /// End of stream marker.
118    EndOfStream,
119
120    // Statements
121    /// Function definition statement.
122    FunctionDef,
123    /// Struct definition statement.
124    StructDef,
125    /// Variable declaration statement.
126    VariableDecl,
127    /// Assignment statement.
128    Assignment,
129    /// If statement.
130    IfStatement,
131    /// While statement.
132    WhileStatement,
133    /// For statement.
134    ForStatement,
135    /// Return statement.
136    ReturnStatement,
137    /// Expression statement.
138    ExpressionStatement,
139
140    // Expressions
141    /// Binary expression.
142    BinaryExpr,
143    /// Unary expression.
144    UnaryExpr,
145    /// Function call expression.
146    CallExpr,
147    /// Literal expression.
148    LiteralExpr,
149    /// Identifier expression.
150    IdentifierExpr,
151    /// Member access expression.
152    MemberExpr,
153    /// List expression.
154    ListExpr,
155
156    // Components
157    /// Parameter list.
158    ParamList,
159    /// Argument list.
160    ArgList,
161    /// Code block.
162    Block,
163
164    // Special
165    /// Root node of the AST.
166    Root,
167    /// Grouping expression.
168    Grouping,
169    /// Error node.
170    Error,
171}
172
173impl MojoElementType {
174    /// Checks if the node is trivia (whitespace, comments, etc.).
175    pub fn is_trivia(&self) -> bool {
176        matches!(self, MojoElementType::Whitespace | MojoElementType::Newline | MojoElementType::Comment)
177    }
178}
179
180impl From<MojoTokenType> for MojoElementType {
181    fn from(token: MojoTokenType) -> Self {
182        match token {
183            MojoTokenType::Fn => MojoElementType::Fn,
184            MojoTokenType::Struct => MojoElementType::Struct,
185            MojoTokenType::Var => MojoElementType::Var,
186            MojoTokenType::Let => MojoElementType::Let,
187            MojoTokenType::If => MojoElementType::If,
188            MojoTokenType::Else => MojoElementType::Else,
189            MojoTokenType::While => MojoElementType::While,
190            MojoTokenType::For => MojoElementType::For,
191            MojoTokenType::In => MojoElementType::In,
192            MojoTokenType::Return => MojoElementType::Return,
193            MojoTokenType::Break => MojoElementType::Break,
194            MojoTokenType::Continue => MojoElementType::Continue,
195            MojoTokenType::Import => MojoElementType::Import,
196            MojoTokenType::From => MojoElementType::From,
197            MojoTokenType::True => MojoElementType::True,
198            MojoTokenType::False => MojoElementType::False,
199            MojoTokenType::None => MojoElementType::None,
200            MojoTokenType::Identifier => MojoElementType::Identifier,
201            MojoTokenType::Integer => MojoElementType::Integer,
202            MojoTokenType::Float => MojoElementType::Float,
203            MojoTokenType::String => MojoElementType::String,
204            MojoTokenType::Plus => MojoElementType::Plus,
205            MojoTokenType::Minus => MojoElementType::Minus,
206            MojoTokenType::Star => MojoElementType::Star,
207            MojoTokenType::Slash => MojoElementType::Slash,
208            MojoTokenType::Percent => MojoElementType::Percent,
209            MojoTokenType::Equal => MojoElementType::Equal,
210            MojoTokenType::EqualEqual => MojoElementType::EqualEqual,
211            MojoTokenType::NotEqual => MojoElementType::NotEqual,
212            MojoTokenType::Less => MojoElementType::Less,
213            MojoTokenType::LessEqual => MojoElementType::LessEqual,
214            MojoTokenType::Greater => MojoElementType::Greater,
215            MojoTokenType::GreaterEqual => MojoElementType::GreaterEqual,
216            MojoTokenType::And => MojoElementType::And,
217            MojoTokenType::Or => MojoElementType::Or,
218            MojoTokenType::Not => MojoElementType::Not,
219            MojoTokenType::LeftParen => MojoElementType::LeftParen,
220            MojoTokenType::RightParen => MojoElementType::RightParen,
221            MojoTokenType::LeftBracket => MojoElementType::LeftBracket,
222            MojoTokenType::RightBracket => MojoElementType::RightBracket,
223            MojoTokenType::LeftBrace => MojoElementType::LeftBrace,
224            MojoTokenType::RightBrace => MojoElementType::RightBrace,
225            MojoTokenType::Comma => MojoElementType::Comma,
226            MojoTokenType::Dot => MojoElementType::Dot,
227            MojoTokenType::Colon => MojoElementType::Colon,
228            MojoTokenType::Semicolon => MojoElementType::Semicolon,
229            MojoTokenType::Arrow => MojoElementType::Arrow,
230            MojoTokenType::Whitespace => MojoElementType::Whitespace,
231            MojoTokenType::Newline => MojoElementType::Newline,
232            MojoTokenType::Comment => MojoElementType::Comment,
233            MojoTokenType::Indent => MojoElementType::Indent,
234            MojoTokenType::Dedent => MojoElementType::Dedent,
235            MojoTokenType::EndOfStream => MojoElementType::EndOfStream,
236            MojoTokenType::Error => MojoElementType::Error,
237        }
238    }
239}
240
241impl oak_core::ElementType for MojoElementType {
242    type Role = UniversalElementRole;
243
244    fn role(&self) -> Self::Role {
245        UniversalElementRole::None
246    }
247}