Skip to main content

oak_erlang/parser/
element_type.rs

1use oak_core::{ElementType, UniversalElementRole};
2
3/// Element types for the Erlang language.
4#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
5#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
6#[repr(u8)]
7pub enum ErlangElementType {
8    /// Whitespace characters.
9    Whitespace,
10    /// Newline characters.
11    Newline,
12    /// Comments.
13    Comment,
14
15    /// Identifiers.
16    Identifier,
17    /// Atoms.
18    Atom,
19    /// Variables.
20    Variable,
21    /// Numbers.
22    Number,
23    /// Strings.
24    String,
25    /// Characters.
26    Character,
27
28    /// `after` keyword.
29    After,
30    /// `and` keyword.
31    And,
32    /// `andalso` keyword.
33    Andalso,
34    /// `band` keyword.
35    Band,
36    /// `begin` keyword.
37    Begin,
38    /// `bnot` keyword.
39    Bnot,
40    /// `bor` keyword.
41    Bor,
42    /// `bsl` keyword.
43    Bsl,
44    /// `bsr` keyword.
45    Bsr,
46    /// `bxor` keyword.
47    Bxor,
48    /// `case` keyword.
49    Case,
50    /// `catch` keyword.
51    Catch,
52    /// `cond` keyword.
53    Cond,
54    /// `div` keyword.
55    Div,
56    /// `end` keyword.
57    End,
58    /// `fun` keyword.
59    Fun,
60    /// `if` keyword.
61    If,
62    /// `let` keyword.
63    Let,
64    /// `not` keyword.
65    Not,
66    /// `of` keyword.
67    Of,
68    /// `or` keyword.
69    Or,
70    /// `orelse` keyword.
71    Orelse,
72    /// `query` keyword.
73    Query,
74    /// `receive` keyword.
75    Receive,
76    /// `rem` keyword.
77    Rem,
78    /// `try` keyword.
79    Try,
80    /// `when` keyword.
81    When,
82    /// `xor` keyword.
83    Xor,
84
85    /// Plus operator (`+`).
86    Plus,
87    /// Minus operator (`-`).
88    Minus,
89    /// Star operator (`*`).
90    Star,
91    /// Slash operator (`/`).
92    Slash,
93    /// Equal operator (`=`).
94    Equal,
95    /// EqualEqual operator (`==`).
96    EqualEqual,
97    /// SlashEqual operator (`/=`).
98    SlashEqual,
99    /// EqualColonEqual operator (`=:=`).
100    EqualColonEqual,
101    /// EqualSlashEqual operator (`=/=`).
102    EqualSlashEqual,
103    /// Less operator (`<`).
104    Less,
105    /// Greater operator (`>`).
106    Greater,
107    /// LessEqual operator (`=<`).
108    LessEqual,
109    /// GreaterEqual operator (`>=`).
110    GreaterEqual,
111    /// PlusPlus operator (`++`).
112    PlusPlus,
113    /// MinusMinus operator (`--`).
114    MinusMinus,
115    /// Exclamation operator (`!`).
116    Exclamation,
117    /// Question operator (`?`).
118    Question,
119
120    /// Left parenthesis (`(`).
121    LeftParen,
122    /// Right parenthesis (`)`).
123    RightParen,
124    /// Left brace (`{`).
125    LeftBrace,
126    /// Right brace (`}`).
127    RightBrace,
128    /// Left bracket (`[`).
129    LeftBracket,
130    /// Right bracket (`]`).
131    RightBracket,
132    /// Comma (`,`).
133    Comma,
134    /// Semicolon (`;`).
135    Semicolon,
136    /// Dot (`.`).
137    Dot,
138    /// Colon (`:`).
139    Colon,
140    /// Arrow (`->`).
141    Arrow,
142    /// Pipe (`|`).
143    Pipe,
144    /// PipePipe (`||`).
145    PipePipe,
146    /// Hash (`#`).
147    Hash,
148
149    /// Root node of the Erlang AST.
150    Root,
151    /// An item in the module.
152    Item,
153    /// Module declaration.
154    Module,
155    /// Export attribute.
156    Export,
157    /// Generic attribute.
158    Attribute,
159    /// Function declaration.
160    Function,
161    /// A clause of a function.
162    FunctionClause,
163    /// A pattern matching expression.
164    Pattern,
165    /// A record pattern.
166    RecordPattern,
167    /// A statement.
168    Statement,
169    /// An expression.
170    Expr,
171    /// A binary expression.
172    BinaryExpr,
173    /// A function call.
174    CallExpr,
175    /// A fun expression.
176    FunExpr,
177    /// A case expression.
178    CaseExpr,
179    /// A clause in a case expression.
180    CaseClause,
181    /// An if expression.
182    IfExpr,
183    /// A clause in an if expression.
184    IfClause,
185    /// A try expression.
186    TryExpr,
187    /// A catch clause.
188    CatchClause,
189    /// A receive expression.
190    ReceiveExpr,
191    /// A clause in a receive expression.
192    ReceiveClause,
193    /// A record expression.
194    RecordExpr,
195
196    /// Error node.
197    Error,
198    /// End of file.
199    Eof,
200}
201
202impl ErlangElementType {
203    /// Returns `true` if the element type is a keyword.
204    pub fn is_keyword(&self) -> bool {
205        matches!(
206            self,
207            Self::After
208                | Self::And
209                | Self::Andalso
210                | Self::Band
211                | Self::Begin
212                | Self::Bnot
213                | Self::Bor
214                | Self::Bsl
215                | Self::Bsr
216                | Self::Bxor
217                | Self::Case
218                | Self::Catch
219                | Self::Cond
220                | Self::Div
221                | Self::End
222                | Self::Fun
223                | Self::If
224                | Self::Let
225                | Self::Not
226                | Self::Of
227                | Self::Or
228                | Self::Orelse
229                | Self::Query
230                | Self::Receive
231                | Self::Rem
232                | Self::Try
233                | Self::When
234                | Self::Xor
235        )
236    }
237
238    /// Returns `true` if the element type is an operator.
239    pub fn is_operator(&self) -> bool {
240        matches!(
241            self,
242            Self::Plus
243                | Self::Minus
244                | Self::Star
245                | Self::Slash
246                | Self::Equal
247                | Self::EqualEqual
248                | Self::SlashEqual
249                | Self::EqualColonEqual
250                | Self::EqualSlashEqual
251                | Self::Less
252                | Self::Greater
253                | Self::LessEqual
254                | Self::GreaterEqual
255                | Self::PlusPlus
256                | Self::MinusMinus
257                | Self::Exclamation
258                | Self::Question
259        )
260    }
261
262    /// Returns `true` if the element type is a punctuation.
263    pub fn is_punctuation(&self) -> bool {
264        matches!(self, Self::LeftParen | Self::RightParen | Self::LeftBrace | Self::RightBrace | Self::LeftBracket | Self::RightBracket | Self::Comma | Self::Semicolon | Self::Dot | Self::Colon | Self::Arrow | Self::Pipe | Self::PipePipe | Self::Hash)
265    }
266}
267
268impl ElementType for ErlangElementType {
269    type Role = UniversalElementRole;
270
271    fn role(&self) -> Self::Role {
272        match self {
273            Self::Root => UniversalElementRole::Root,
274            _ => UniversalElementRole::None,
275        }
276    }
277}
278
279impl From<crate::lexer::token_type::ErlangTokenType> for ErlangElementType {
280    fn from(token: crate::lexer::token_type::ErlangTokenType) -> Self {
281        use crate::lexer::token_type::ErlangTokenType;
282        match token {
283            ErlangTokenType::Whitespace => Self::Whitespace,
284            ErlangTokenType::Newline => Self::Newline,
285            ErlangTokenType::Comment => Self::Comment,
286            ErlangTokenType::Identifier => Self::Identifier,
287            ErlangTokenType::Atom => Self::Atom,
288            ErlangTokenType::Variable => Self::Variable,
289            ErlangTokenType::Number => Self::Number,
290            ErlangTokenType::String => Self::String,
291            ErlangTokenType::Character => Self::Character,
292            ErlangTokenType::After => Self::After,
293            ErlangTokenType::And => Self::And,
294            ErlangTokenType::Andalso => Self::Andalso,
295            ErlangTokenType::Band => Self::Band,
296            ErlangTokenType::Begin => Self::Begin,
297            ErlangTokenType::Bnot => Self::Bnot,
298            ErlangTokenType::Bor => Self::Bor,
299            ErlangTokenType::Bsl => Self::Bsl,
300            ErlangTokenType::Bsr => Self::Bsr,
301            ErlangTokenType::Bxor => Self::Bxor,
302            ErlangTokenType::Case => Self::Case,
303            ErlangTokenType::Catch => Self::Catch,
304            ErlangTokenType::Cond => Self::Cond,
305            ErlangTokenType::Div => Self::Div,
306            ErlangTokenType::End => Self::End,
307            ErlangTokenType::Fun => Self::Fun,
308            ErlangTokenType::If => Self::If,
309            ErlangTokenType::Let => Self::Let,
310            ErlangTokenType::Not => Self::Not,
311            ErlangTokenType::Of => Self::Of,
312            ErlangTokenType::Or => Self::Or,
313            ErlangTokenType::Orelse => Self::Orelse,
314            ErlangTokenType::Query => Self::Query,
315            ErlangTokenType::Receive => Self::Receive,
316            ErlangTokenType::Rem => Self::Rem,
317            ErlangTokenType::Try => Self::Try,
318            ErlangTokenType::When => Self::When,
319            ErlangTokenType::Xor => Self::Xor,
320            ErlangTokenType::Plus => Self::Plus,
321            ErlangTokenType::Minus => Self::Minus,
322            ErlangTokenType::Star => Self::Star,
323            ErlangTokenType::Slash => Self::Slash,
324            ErlangTokenType::Equal => Self::Equal,
325            ErlangTokenType::EqualEqual => Self::EqualEqual,
326            ErlangTokenType::SlashEqual => Self::SlashEqual,
327            ErlangTokenType::EqualColonEqual => Self::EqualColonEqual,
328            ErlangTokenType::EqualSlashEqual => Self::EqualSlashEqual,
329            ErlangTokenType::Less => Self::Less,
330            ErlangTokenType::Greater => Self::Greater,
331            ErlangTokenType::LessEqual => Self::LessEqual,
332            ErlangTokenType::GreaterEqual => Self::GreaterEqual,
333            ErlangTokenType::PlusPlus => Self::PlusPlus,
334            ErlangTokenType::MinusMinus => Self::MinusMinus,
335            ErlangTokenType::Exclamation => Self::Exclamation,
336            ErlangTokenType::Question => Self::Question,
337            ErlangTokenType::LeftParen => Self::LeftParen,
338            ErlangTokenType::RightParen => Self::RightParen,
339            ErlangTokenType::LeftBrace => Self::LeftBrace,
340            ErlangTokenType::RightBrace => Self::RightBrace,
341            ErlangTokenType::LeftBracket => Self::LeftBracket,
342            ErlangTokenType::RightBracket => Self::RightBracket,
343            ErlangTokenType::Comma => Self::Comma,
344            ErlangTokenType::Semicolon => Self::Semicolon,
345            ErlangTokenType::Dot => Self::Dot,
346            ErlangTokenType::Colon => Self::Colon,
347            ErlangTokenType::Arrow => Self::Arrow,
348            ErlangTokenType::Pipe => Self::Pipe,
349            ErlangTokenType::PipePipe => Self::PipePipe,
350            ErlangTokenType::Hash => Self::Hash,
351            ErlangTokenType::Root => Self::Root,
352            ErlangTokenType::Item => Self::Item,
353            ErlangTokenType::Module => Self::Module,
354            ErlangTokenType::Export => Self::Export,
355            ErlangTokenType::Attribute => Self::Attribute,
356            ErlangTokenType::Function => Self::Function,
357            ErlangTokenType::FunctionClause => Self::FunctionClause,
358            ErlangTokenType::Pattern => Self::Pattern,
359            ErlangTokenType::RecordPattern => Self::RecordPattern,
360            ErlangTokenType::Statement => Self::Statement,
361            ErlangTokenType::Expr => Self::Expr,
362            ErlangTokenType::BinaryExpr => Self::BinaryExpr,
363            ErlangTokenType::CallExpr => Self::CallExpr,
364            ErlangTokenType::FunExpr => Self::FunExpr,
365            ErlangTokenType::CaseExpr => Self::CaseExpr,
366            ErlangTokenType::CaseClause => Self::CaseClause,
367            ErlangTokenType::IfExpr => Self::IfExpr,
368            ErlangTokenType::IfClause => Self::IfClause,
369            ErlangTokenType::TryExpr => Self::TryExpr,
370            ErlangTokenType::CatchClause => Self::CatchClause,
371            ErlangTokenType::ReceiveExpr => Self::ReceiveExpr,
372            ErlangTokenType::ReceiveClause => Self::ReceiveClause,
373            ErlangTokenType::RecordExpr => Self::RecordExpr,
374            ErlangTokenType::Error => Self::Error,
375            ErlangTokenType::Eof => Self::Eof,
376        }
377    }
378}