Skip to main content

oak_ruby/parser/
element_type.rs

1use oak_core::{ElementType, UniversalElementRole};
2#[cfg(feature = "serde")]
3use serde::{Deserialize, Serialize};
4use std::fmt;
5
6#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
7#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
8#[repr(u8)]
9pub enum RubyElementType {
10    Identifier,
11    GlobalVariable,
12    InstanceVariable,
13    ClassVariable,
14    Constant,
15    IntegerLiteral,
16    FloatLiteral,
17    StringLiteral,
18    Literal,
19    Symbol,
20    RegexLiteral,
21
22    If,
23    Unless,
24    Elsif,
25    Else,
26    Case,
27    When,
28    Then,
29    For,
30    While,
31    Until,
32    Break,
33    Next,
34    Redo,
35    Retry,
36    Return,
37    Yield,
38    Def,
39    Class,
40    Module,
41    End,
42    Lambda,
43    Proc,
44    Begin,
45    Rescue,
46    Ensure,
47    Raise,
48    Require,
49    Load,
50    Include,
51    Extend,
52    Prepend,
53    And,
54    Or,
55    Not,
56    In,
57    True,
58    False,
59    Nil,
60    Super,
61    Self_,
62    Alias,
63    Undef,
64    Defined,
65    Do,
66
67    Plus,
68    Minus,
69    Multiply,
70    Divide,
71    Modulo,
72    Power,
73    EqualEqual,
74    NotEqual,
75    Less,
76    Greater,
77    LessEqual,
78    GreaterEqual,
79    EqualEqualEqual,
80    Spaceship,
81    Assign,
82    PlusAssign,
83    MinusAssign,
84    MultiplyAssign,
85    DivideAssign,
86    ModuloAssign,
87    PowerAssign,
88    BitAnd,
89    BitOr,
90    Xor,
91    LogicalNot,
92    Tilde,
93    LeftShift,
94    RightShift,
95    AndAssign,
96    OrAssign,
97    XorAssign,
98    LeftShiftAssign,
99    RightShiftAssign,
100    AndAnd,
101    OrOr,
102    OrOrAssign,
103    AndAndAssign,
104    Question,
105    DotDot,
106    DotDotDot,
107    Match,
108    NotMatch,
109
110    LeftParen,
111    RightParen,
112    LeftBracket,
113    RightBracket,
114    LeftBrace,
115    RightBrace,
116    Comma,
117    Colon,
118    Semicolon,
119    Dot,
120    DoubleColon,
121    At,
122    Dollar,
123
124    Whitespace,
125    Newline,
126    Comment,
127    Eof,
128    Invalid,
129    Root,
130    BinaryExpression,
131    UnaryExpression,
132    LiteralExpression,
133    ParenExpression,
134    ParenthesizedExpression,
135    MethodDefinition,
136    ClassDefinition,
137    ModuleDefinition,
138    IfStatement,
139    WhileStatement,
140    ReturnStatement,
141    IfExpression,
142    CallExpression,
143    MemberAccess,
144    ParameterList,
145    ArgumentList,
146    Error,
147    Equal,
148}
149
150impl RubyElementType {
151    pub fn is_ignored(&self) -> bool {
152        matches!(self, Self::Whitespace | Self::Newline | Self::Comment)
153    }
154
155    pub fn is_keyword(&self) -> bool {
156        matches!(
157            self,
158            Self::If
159                | Self::Unless
160                | Self::Elsif
161                | Self::Else
162                | Self::Case
163                | Self::When
164                | Self::Then
165                | Self::For
166                | Self::While
167                | Self::Until
168                | Self::Break
169                | Self::Next
170                | Self::Redo
171                | Self::Retry
172                | Self::Return
173                | Self::Yield
174                | Self::Def
175                | Self::Class
176                | Self::Module
177                | Self::End
178                | Self::Lambda
179                | Self::Proc
180                | Self::Begin
181                | Self::Rescue
182                | Self::Ensure
183                | Self::Raise
184                | Self::Require
185                | Self::Load
186                | Self::Include
187                | Self::Extend
188                | Self::Prepend
189                | Self::And
190                | Self::Or
191                | Self::Not
192                | Self::In
193                | Self::True
194                | Self::False
195                | Self::Nil
196                | Self::Super
197                | Self::Self_
198                | Self::Alias
199                | Self::Undef
200                | Self::Defined
201                | Self::Do
202        )
203    }
204}
205
206impl fmt::Display for RubyElementType {
207    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
208        let name = match self {
209            Self::Identifier => "Identifier",
210            Self::GlobalVariable => "GlobalVariable",
211            Self::InstanceVariable => "InstanceVariable",
212            Self::ClassVariable => "ClassVariable",
213            Self::Constant => "Constant",
214            Self::IntegerLiteral => "IntegerLiteral",
215            Self::FloatLiteral => "FloatLiteral",
216            Self::StringLiteral => "StringLiteral",
217            Self::Literal => "Literal",
218            Self::Symbol => "Symbol",
219            Self::RegexLiteral => "RegexLiteral",
220
221            Self::If => "If",
222            Self::Unless => "Unless",
223            Self::Elsif => "Elsif",
224            Self::Else => "Else",
225            Self::Case => "Case",
226            Self::When => "When",
227            Self::Then => "Then",
228            Self::For => "For",
229            Self::While => "While",
230            Self::Until => "Until",
231            Self::Break => "Break",
232            Self::Next => "Next",
233            Self::Redo => "Redo",
234            Self::Retry => "Retry",
235            Self::Return => "Return",
236            Self::Yield => "Yield",
237            Self::Def => "Def",
238            Self::Class => "Class",
239            Self::Module => "Module",
240            Self::End => "End",
241            Self::Lambda => "Lambda",
242            Self::Proc => "Proc",
243            Self::Begin => "Begin",
244            Self::Rescue => "Rescue",
245            Self::Ensure => "Ensure",
246            Self::Raise => "Raise",
247            Self::Require => "Require",
248            Self::Load => "Load",
249            Self::Include => "Include",
250            Self::Extend => "Extend",
251            Self::Prepend => "Prepend",
252            Self::And => "And",
253            Self::Or => "Or",
254            Self::Not => "Not",
255            Self::In => "In",
256            Self::True => "True",
257            Self::False => "False",
258            Self::Nil => "Nil",
259            Self::Super => "Super",
260            Self::Self_ => "Self",
261            Self::Alias => "Alias",
262            Self::Undef => "Undef",
263            Self::Defined => "Defined",
264            Self::Do => "Do",
265
266            Self::Plus => "Plus",
267            Self::Minus => "Minus",
268            Self::Multiply => "Multiply",
269            Self::Divide => "Divide",
270            Self::Modulo => "Modulo",
271            Self::Power => "Power",
272            Self::EqualEqual => "EqualEqual",
273            Self::NotEqual => "NotEqual",
274            Self::Less => "Less",
275            Self::Greater => "Greater",
276            Self::LessEqual => "LessEqual",
277            Self::GreaterEqual => "GreaterEqual",
278            Self::EqualEqualEqual => "EqualEqualEqual",
279            Self::Spaceship => "Spaceship",
280            Self::Assign => "Assign",
281            Self::PlusAssign => "PlusAssign",
282            Self::MinusAssign => "MinusAssign",
283            Self::MultiplyAssign => "MultiplyAssign",
284            Self::DivideAssign => "DivideAssign",
285            Self::ModuloAssign => "ModuloAssign",
286            Self::PowerAssign => "PowerAssign",
287            Self::BitAnd => "BitAnd",
288            Self::BitOr => "BitOr",
289            Self::Xor => "Xor",
290            Self::LogicalNot => "LogicalNot",
291            Self::Tilde => "Tilde",
292            Self::LeftShift => "LeftShift",
293            Self::RightShift => "RightShift",
294            Self::AndAssign => "AndAssign",
295            Self::OrAssign => "OrAssign",
296            Self::XorAssign => "XorAssign",
297            Self::LeftShiftAssign => "LeftShiftAssign",
298            Self::RightShiftAssign => "RightShiftAssign",
299            Self::AndAnd => "AndAnd",
300            Self::OrOr => "OrOr",
301            Self::OrOrAssign => "OrOrAssign",
302            Self::AndAndAssign => "AndAndAssign",
303            Self::Question => "Question",
304            Self::DotDot => "DotDot",
305            Self::DotDotDot => "DotDotDot",
306            Self::Match => "Match",
307            Self::NotMatch => "NotMatch",
308
309            Self::LeftParen => "LeftParen",
310            Self::RightParen => "RightParen",
311            Self::LeftBracket => "LeftBracket",
312            Self::RightBracket => "RightBracket",
313            Self::LeftBrace => "LeftBrace",
314            Self::RightBrace => "RightBrace",
315            Self::Comma => "Comma",
316            Self::Colon => "Colon",
317            Self::Semicolon => "Semicolon",
318            Self::Dot => "Dot",
319            Self::DoubleColon => "DoubleColon",
320            Self::At => "At",
321            Self::Dollar => "Dollar",
322
323            Self::Whitespace => "Whitespace",
324            Self::Newline => "Newline",
325            Self::Comment => "Comment",
326            Self::Eof => "Eof",
327            Self::Invalid => "Invalid",
328            Self::Root => "Root",
329            Self::BinaryExpression => "BinaryExpression",
330            Self::UnaryExpression => "UnaryExpression",
331            Self::LiteralExpression => "LiteralExpression",
332            Self::ParenExpression => "ParenExpression",
333            Self::ParenthesizedExpression => "ParenthesizedExpression",
334            Self::MethodDefinition => "MethodDefinition",
335            Self::ClassDefinition => "ClassDefinition",
336            Self::ModuleDefinition => "ModuleDefinition",
337            Self::IfStatement => "IfStatement",
338            Self::WhileStatement => "WhileStatement",
339            Self::ReturnStatement => "ReturnStatement",
340            Self::IfExpression => "IfExpression",
341            Self::CallExpression => "CallExpression",
342            Self::MemberAccess => "MemberAccess",
343            Self::ParameterList => "ParameterList",
344            Self::ArgumentList => "ArgumentList",
345            Self::Error => "Error",
346            Self::Equal => "Equal",
347        };
348        write!(f, "{}", name)
349    }
350}
351
352impl ElementType for RubyElementType {
353    type Role = UniversalElementRole;
354
355    fn role(&self) -> Self::Role {
356        match self {
357            _ => UniversalElementRole::None,
358        }
359    }
360}
361
362impl From<crate::lexer::token_type::RubyTokenType> for RubyElementType {
363    fn from(token: crate::lexer::token_type::RubyTokenType) -> Self {
364        unsafe { std::mem::transmute(token) }
365    }
366}