Skip to main content

oak_python/parser/
element_type.rs

1//! Python element types.
2
3use oak_core::{ElementType, UniversalElementRole};
4
5/// Python element types.
6#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
7#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
8#[repr(u16)]
9pub enum PythonElementType {
10    /// Whitespace
11    Whitespace,
12    /// Comment
13    Comment,
14    /// Identifier
15    Identifier,
16
17    /// Number literal
18    Number,
19    /// String literal
20    String,
21    /// Bytes literal
22    Bytes,
23    /// Formatted string literal
24    FString,
25
26    /// `and`
27    AndKeyword,
28    /// `as`
29    AsKeyword,
30    /// `assert`
31    AssertKeyword,
32    /// `async`
33    AsyncKeyword,
34    /// `await`
35    AwaitKeyword,
36    /// `break`
37    BreakKeyword,
38    /// `class`
39    ClassKeyword,
40    /// `continue`
41    ContinueKeyword,
42    /// `def`
43    DefKeyword,
44    /// `del`
45    DelKeyword,
46    /// `elif`
47    ElifKeyword,
48    /// `else`
49    ElseKeyword,
50    /// `except`
51    ExceptKeyword,
52    /// `False`
53    FalseKeyword,
54    /// `finally`
55    FinallyKeyword,
56    /// `for`
57    ForKeyword,
58    /// `from`
59    FromKeyword,
60    /// `global`
61    GlobalKeyword,
62    /// `if`
63    IfKeyword,
64    /// `import`
65    ImportKeyword,
66    /// `in`
67    InKeyword,
68    /// `is`
69    IsKeyword,
70    /// `lambda`
71    LambdaKeyword,
72    /// `None`
73    NoneKeyword,
74    /// `nonlocal`
75    NonlocalKeyword,
76    /// `not`
77    NotKeyword,
78    /// `or`
79    OrKeyword,
80    /// `pass`
81    PassKeyword,
82    /// `raise`
83    RaiseKeyword,
84    /// `return`
85    ReturnKeyword,
86    /// `True`
87    TrueKeyword,
88    /// `try`
89    TryKeyword,
90    /// `while`
91    WhileKeyword,
92    /// `with`
93    WithKeyword,
94    /// `yield`
95    YieldKeyword,
96
97    /// `+`
98    Plus,
99    /// `-`
100    Minus,
101    /// `*`
102    Star,
103    /// `**`
104    DoubleStar,
105    /// `/`
106    Slash,
107    /// `//`
108    DoubleSlash,
109    /// `%`
110    Percent,
111    /// `@`
112    At,
113    /// `<<`
114    LeftShift,
115    /// `>>`
116    RightShift,
117    /// `&`
118    Ampersand,
119    /// `|`
120    Pipe,
121    /// `^`
122    Caret,
123    /// `~`
124    Tilde,
125    /// `<`
126    Less,
127    /// `>`
128    Greater,
129    /// `<=`
130    LessEqual,
131    /// `>=`
132    GreaterEqual,
133    /// `==`
134    Equal,
135    /// `!=`
136    NotEqual,
137
138    /// `=`
139    Assign,
140    /// `+=`
141    PlusAssign,
142    /// `-=`
143    MinusAssign,
144    /// `*=`
145    StarAssign,
146    /// `**=`
147    DoubleStarAssign,
148    /// `/=`
149    SlashAssign,
150    /// `//=`
151    DoubleSlashAssign,
152    /// `%=`
153    PercentAssign,
154    /// `@=`
155    AtAssign,
156    /// `&=`
157    AmpersandAssign,
158    /// `|=`
159    PipeAssign,
160    /// `^=`
161    CaretAssign,
162    /// `<<=`
163    LeftShiftAssign,
164    /// `>>=`
165    RightShiftAssign,
166
167    /// `(`
168    LeftParen,
169    /// `)`
170    RightParen,
171    /// `[`
172    LeftBracket,
173    /// `]`
174    RightBracket,
175    /// `{`
176    LeftBrace,
177    /// `}`
178    RightBrace,
179    /// `,`
180    Comma,
181    /// `:`
182    Colon,
183    /// `;`
184    Semicolon,
185    /// `.`
186    Dot,
187    /// `->`
188    Arrow,
189    /// `...`
190    Ellipsis,
191
192    /// Newline
193    Newline,
194    /// Indent
195    Indent,
196    /// Dedent
197    Dedent,
198    /// End of stream
199    Eof,
200    /// Error node
201    Error,
202
203    /// Root node
204    Root,
205    /// Module
206    Module,
207    /// Interactive module
208    InteractiveModule,
209    /// Expression module
210    ExpressionModule,
211
212    /// Name expression
213    Name,
214    /// Constant expression
215    Constant,
216    /// Joined string expression
217    JoinedStr,
218    /// Expression
219    Expr,
220    /// Tuple expression
221    Tuple,
222    /// Generator expression
223    GeneratorExp,
224    /// List expression
225    List,
226    /// List comprehension
227    ListComp,
228    /// Dictionary expression
229    Dict,
230    /// Dictionary comprehension
231    DictComp,
232    /// Set comprehension
233    SetComp,
234    /// Set expression
235    Set,
236    /// Unary operation
237    UnaryOp,
238    /// Keyword argument
239    Keyword,
240    /// Starred expression
241    Starred,
242    /// Call expression
243    Call,
244    /// Slice expression
245    Slice,
246    /// Subscript expression
247    Subscript,
248    /// Attribute expression
249    Attribute,
250    /// Binary operation
251    BinOp,
252    /// Boolean operation
253    BoolOp,
254    /// Comparison expression
255    Compare,
256    /// If expression
257    IfExp,
258    /// Lambda expression
259    Lambda,
260    /// Yield expression
261    Yield,
262    /// Yield from expression
263    YieldFrom,
264    /// Named expression (walrus operator)
265    NamedExpr,
266    /// Formatted value expression
267    FormattedValue,
268    /// Await expression
269    Await,
270
271    /// Suite of statements
272    Suite,
273    /// Decorator
274    Decorator,
275    /// Assignment statement
276    AssignStmt,
277    /// With item
278    WithItem,
279    /// Return statement (keyword)
280    Return,
281    /// Return statement
282    ReturnStmt,
283    /// Pass statement (keyword)
284    Pass,
285    /// Pass statement
286    PassStmt,
287    /// Break statement (keyword)
288    Break,
289    /// Break statement
290    BreakStmt,
291    /// Continue statement (keyword)
292    Continue,
293    /// Continue statement
294    ContinueStmt,
295    /// Global statement (keyword)
296    Global,
297    /// Global statement
298    GlobalStmt,
299    /// Nonlocal statement (keyword)
300    Nonlocal,
301    /// Nonlocal statement
302    NonlocalStmt,
303    /// Assert statement (keyword)
304    Assert,
305    /// Assert statement
306    AssertStmt,
307    /// If statement (keyword)
308    If,
309    /// If statement
310    IfStmt,
311    /// While statement (keyword)
312    While,
313    /// While statement
314    WhileStmt,
315    /// For statement (keyword)
316    For,
317    /// For statement
318    ForStmt,
319    /// Async for statement
320    AsyncFor,
321    /// Try statement (keyword)
322    Try,
323    /// Try statement
324    TryStmt,
325    /// Except handler
326    ExceptHandler,
327    /// With statement (keyword)
328    With,
329    /// With statement
330    WithStmt,
331    /// Async with statement
332    AsyncWith,
333    /// Function definition
334    FunctionDef,
335    /// Async function definition
336    AsyncFunctionDef,
337    /// Class definition
338    ClassDef,
339    /// Import statement (keyword)
340    Import,
341    /// Import from statement (keyword)
342    ImportFrom,
343    /// Import statement
344    ImportStmt,
345    /// Import from statement
346    ImportFromStmt,
347    /// Expression statement
348    ExprStmt,
349    /// Delete statement (keyword)
350    Delete,
351    /// Delete statement
352    DeleteStmt,
353    /// Raise statement (keyword)
354    Raise,
355    /// Raise statement
356    RaiseStmt,
357    /// Arguments list
358    Arguments,
359    /// Single argument
360    Arg,
361    /// Import alias
362    Alias,
363    /// Comprehension
364    Comprehension,
365}
366
367impl From<u16> for PythonElementType {
368    fn from(d: u16) -> PythonElementType {
369        unsafe { core::mem::transmute::<u16, PythonElementType>(d) }
370    }
371}
372
373impl PythonElementType {
374    /// Returns true if the element type is a keyword.
375    pub fn is_keyword(&self) -> bool {
376        matches!(
377            self,
378            Self::AndKeyword
379                | Self::AsKeyword
380                | Self::AssertKeyword
381                | Self::AsyncKeyword
382                | Self::AwaitKeyword
383                | Self::BreakKeyword
384                | Self::ClassKeyword
385                | Self::ContinueKeyword
386                | Self::DefKeyword
387                | Self::DelKeyword
388                | Self::ElifKeyword
389                | Self::ElseKeyword
390                | Self::ExceptKeyword
391                | Self::FalseKeyword
392                | Self::FinallyKeyword
393                | Self::ForKeyword
394                | Self::FromKeyword
395                | Self::GlobalKeyword
396                | Self::IfKeyword
397                | Self::ImportKeyword
398                | Self::InKeyword
399                | Self::IsKeyword
400                | Self::LambdaKeyword
401                | Self::NoneKeyword
402                | Self::NonlocalKeyword
403                | Self::NotKeyword
404                | Self::OrKeyword
405                | Self::PassKeyword
406                | Self::RaiseKeyword
407                | Self::ReturnKeyword
408                | Self::TrueKeyword
409                | Self::TryKeyword
410                | Self::WhileKeyword
411                | Self::WithKeyword
412                | Self::YieldKeyword
413        )
414    }
415}
416
417impl PythonElementType {
418    /// Returns true if the element type is a trivia (whitespace or comment).
419    pub fn is_trivia(&self) -> bool {
420        matches!(self, Self::Whitespace | Self::Comment)
421    }
422}
423
424impl ElementType for PythonElementType {
425    type Role = UniversalElementRole;
426
427    fn role(&self) -> Self::Role {
428        match self {
429            _ => UniversalElementRole::None,
430        }
431    }
432}
433
434impl From<crate::lexer::token_type::PythonTokenType> for PythonElementType {
435    fn from(token: crate::lexer::token_type::PythonTokenType) -> Self {
436        unsafe { std::mem::transmute(token) }
437    }
438}