Skip to main content

oak_python/parser/
element_type.rs

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