Skip to main content

oak_swift/parser/
element_type.rs

1use oak_core::{ElementType, UniversalElementRole};
2
3/// Element types for the Swift parser.
4#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
5#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
6#[repr(u8)]
7pub enum SwiftElementType {
8    /// Whitespace characters.
9    Whitespace,
10    /// Line breaks.
11    Newline,
12    /// Comments.
13    Comment,
14    /// Identifiers.
15    Identifier,
16    /// Error element.
17    Error,
18    /// End of stream.
19    Eof,
20    /// Numeric literals.
21    NumberLiteral,
22    /// String literals.
23    StringLiteral,
24    /// Character literals.
25    CharLiteral,
26    /// Boolean literals.
27    BooleanLiteral,
28
29    /// The `class` keyword.
30    Class,
31    /// The `struct` keyword.
32    Struct,
33    /// The `enum` keyword.
34    Enum,
35    /// The `protocol` keyword.
36    Protocol,
37    /// The `extension` keyword.
38    Extension,
39    /// The `func` keyword.
40    Func,
41    /// The `var` keyword.
42    Var,
43    /// The `let` keyword.
44    Let,
45    /// The `init` keyword.
46    Init,
47    /// The `deinit` keyword.
48    Deinit,
49    /// The `subscript` keyword.
50    Subscript,
51    /// The `typealias` keyword.
52    Typealias,
53    /// The `import` keyword.
54    Import,
55    /// The `if` keyword.
56    If,
57    /// The `else` keyword.
58    Else,
59    /// The `switch` keyword.
60    Switch,
61    /// The `case` keyword.
62    Case,
63    /// The `default` keyword.
64    Default,
65    /// The `for` keyword.
66    For,
67    /// The `while` keyword.
68    While,
69    /// The `repeat` keyword.
70    Repeat,
71    /// The `do` keyword.
72    Do,
73    /// The `break` keyword.
74    Break,
75    /// The `continue` keyword.
76    Continue,
77    /// The `fallthrough` keyword.
78    Fallthrough,
79    /// The `return` keyword.
80    Return,
81    /// The `throw` keyword.
82    Throw,
83    /// The `try` keyword.
84    Try,
85    /// The `catch` keyword.
86    Catch,
87    /// The `finally` keyword.
88    Finally,
89    /// The `guard` keyword.
90    Guard,
91    /// The `defer` keyword.
92    Defer,
93    /// The `public` keyword.
94    Public,
95    /// The `private` keyword.
96    Private,
97    /// The `internal` keyword.
98    Internal,
99    /// The `fileprivate` keyword.
100    Fileprivate,
101    /// The `open` keyword.
102    Open,
103    /// The `static` keyword.
104    Static,
105    /// The `final` keyword.
106    Final,
107    /// The `override` keyword.
108    Override,
109    /// The `mutating` keyword.
110    Mutating,
111    /// The `nonmutating` keyword.
112    Nonmutating,
113    /// The `lazy` keyword.
114    Lazy,
115    /// The `weak` keyword.
116    Weak,
117    /// The `unowned` keyword.
118    Unowned,
119    /// The `optional` keyword.
120    Optional,
121    /// The `required` keyword.
122    Required,
123    /// The `convenience` keyword.
124    Convenience,
125    /// The `dynamic` keyword.
126    Dynamic,
127    /// The `infix` keyword.
128    Infix,
129    /// The `prefix` keyword.
130    Prefix,
131    /// The `postfix` keyword.
132    Postfix,
133    /// The `Any` keyword.
134    Any,
135    /// The `AnyObject` keyword.
136    AnyObject,
137    /// The `self` keyword.
138    Self_,
139    /// The `Self` keyword.
140    Type,
141    /// The `Protocol` keyword.
142    Protocol_,
143    /// The `true` keyword.
144    True,
145    /// The `false` keyword.
146    False,
147    /// The `nil` keyword.
148    Nil,
149    /// The `as` keyword.
150    As,
151    /// The `is` keyword.
152    Is,
153    /// The `in` keyword.
154    In,
155    /// The `where` keyword.
156    Where,
157    /// The `associatedtype` keyword.
158    Associatedtype,
159    /// The `operator` keyword.
160    Operator,
161    /// The `precedencegroup` keyword.
162    Precedencegroup,
163    /// The `indirect` keyword.
164    Indirect,
165    /// The `rethrows` keyword.
166    Rethrows,
167    /// The `throws` keyword.
168    Throws,
169    /// The `inout` keyword.
170    Inout,
171
172    /// Plus operator (`+`).
173    Plus,
174    /// Minus operator (`-`).
175    Minus,
176    /// Multiplication operator (`*`).
177    Star,
178    /// Division operator (`/`).
179    Slash,
180    /// Modulo operator (`%`).
181    Percent,
182    /// Equality operator (`==`).
183    Equal,
184    /// Inequality operator (`!=`).
185    NotEqual,
186    /// Less than operator (`<`).
187    Less,
188    /// Greater than operator (`>`).
189    Greater,
190    /// Less than or equal to operator (`<=`).
191    LessEqual,
192    /// Greater than or equal to operator (`>=`).
193    GreaterEqual,
194    /// Logical AND operator (`&&`).
195    LogicalAnd,
196    /// Logical OR operator (`||`).
197    LogicalOr,
198    /// Logical NOT operator (`!`).
199    LogicalNot,
200    /// Bitwise AND operator (`&`).
201    BitAnd,
202    /// Bitwise OR operator (`|`).
203    BitOr,
204    /// Bitwise XOR operator (`^`).
205    BitXor,
206    /// Bitwise NOT operator (`~`).
207    BitNot,
208    /// Left shift operator (`<<`).
209    LeftShift,
210    /// Right shift operator (`>>`).
211    RightShift,
212    /// Assignment operator (`=`).
213    Assign,
214    /// Plus assignment operator (`+=`).
215    PlusAssign,
216    /// Minus assignment operator (`-=`).
217    MinusAssign,
218    /// Multiplication assignment operator (`*=`).
219    StarAssign,
220    /// Division assignment operator (`/=`).
221    SlashAssign,
222    /// Modulo assignment operator (`%=`).
223    PercentAssign,
224    /// Bitwise AND assignment operator (`&=`).
225    AndAssign,
226    /// Bitwise OR assignment operator (`|=`).
227    OrAssign,
228    /// Bitwise XOR assignment operator (`^=`).
229    XorAssign,
230    /// Left shift assignment operator (`<<=`).
231    LeftShiftAssign,
232    /// Right shift assignment operator (`>>=`).
233    RightShiftAssign,
234
235    /// Question mark (`?`).
236    Question,
237    /// Nil-coalescing operator (`??`).
238    QuestionQuestion,
239    /// Dot operator (`.`).
240    Dot,
241    /// Arrow operator (`->`).
242    Arrow,
243    /// Half-open range operator (`..<`).
244    Range,
245    /// Closed range operator (`...`).
246    ClosedRange,
247    /// Left parenthesis (`(`).
248    LeftParen,
249    /// Right parenthesis (`)`).
250    RightParen,
251    /// Left bracket (`[`).
252    LeftBracket,
253    /// Right bracket (`]`).
254    RightBracket,
255    /// Left brace (`{`).
256    LeftBrace,
257    /// Right brace (`}`).
258    RightBrace,
259    /// Comma (`,`).
260    Comma,
261    /// Semicolon (`;`).
262    Semicolon,
263    /// Colon (`:`).
264    Colon,
265    /// At sign (`@`).
266    At,
267    /// Hash sign (`#`).
268    Hash,
269    /// Dollar sign (`$`).
270    Dollar,
271    /// Underscore (`_`).
272    Underscore,
273    /// Backslash (`\`).
274    Backslash,
275
276    /// Source file node.
277    SourceFile,
278    /// Function declaration node.
279    FunctionDeclaration,
280    /// Parameter node.
281    Parameter,
282    /// Parameter list node.
283    ParameterList,
284    /// Variable declaration node.
285    VariableDeclaration,
286    /// Class declaration node.
287    ClassDeclaration,
288    /// Struct declaration node.
289    StructDeclaration,
290    /// Enum declaration node.
291    EnumDeclaration,
292    /// Protocol declaration node.
293    ProtocolDeclaration,
294    /// If statement node.
295    IfStatement,
296    /// While statement node.
297    WhileStatement,
298    /// For statement node.
299    ForStatement,
300    /// Return statement node.
301    ReturnStatement,
302    /// Break statement node.
303    BreakStatement,
304    /// Continue statement node.
305    ContinueStatement,
306    /// Expression statement node.
307    ExpressionStatement,
308    /// Code block node.
309    Block,
310    /// Binary expression node.
311    BinaryExpression,
312    /// Unary expression node.
313    UnaryExpression,
314    /// Function call expression node.
315    CallExpression,
316    /// Member access expression node.
317    MemberExpression,
318    /// Identifier expression node.
319    IdentifierExpression,
320    /// Literal expression node.
321    LiteralExpression,
322}
323
324impl SwiftElementType {
325    /// Returns true if this element type is trivia (whitespace, newline, or comment).
326    pub fn is_trivia(&self) -> bool {
327        matches!(self, Self::Whitespace | Self::Newline | Self::Comment)
328    }
329}
330
331impl ElementType for SwiftElementType {
332    type Role = UniversalElementRole;
333
334    fn role(&self) -> Self::Role {
335        match self {
336            Self::SourceFile => UniversalElementRole::Root,
337            Self::Error => UniversalElementRole::Error,
338            _ => UniversalElementRole::None,
339        }
340    }
341}
342
343impl From<crate::lexer::token_type::SwiftTokenType> for SwiftElementType {
344    fn from(token: crate::lexer::token_type::SwiftTokenType) -> Self {
345        use crate::lexer::token_type::SwiftTokenType as T;
346        match token {
347            T::Whitespace => Self::Whitespace,
348            T::Newline => Self::Newline,
349            T::Comment => Self::Comment,
350            T::Identifier => Self::Identifier,
351            T::Error => Self::Error,
352            T::Eof => Self::Eof,
353            T::NumberLiteral => Self::NumberLiteral,
354            T::StringLiteral => Self::StringLiteral,
355            T::CharLiteral => Self::CharLiteral,
356            T::BooleanLiteral => Self::BooleanLiteral,
357            T::Class => Self::Class,
358            T::Struct => Self::Struct,
359            T::Enum => Self::Enum,
360            T::Protocol => Self::Protocol,
361            T::Extension => Self::Extension,
362            T::Func => Self::Func,
363            T::Var => Self::Var,
364            T::Let => Self::Let,
365            T::Init => Self::Init,
366            T::Deinit => Self::Deinit,
367            T::Subscript => Self::Subscript,
368            T::Typealias => Self::Typealias,
369            T::Import => Self::Import,
370            T::If => Self::If,
371            T::Else => Self::Else,
372            T::Switch => Self::Switch,
373            T::Case => Self::Case,
374            T::Default => Self::Default,
375            T::For => Self::For,
376            T::While => Self::While,
377            T::Repeat => Self::Repeat,
378            T::Do => Self::Do,
379            T::Break => Self::Break,
380            T::Continue => Self::Continue,
381            T::Fallthrough => Self::Fallthrough,
382            T::Return => Self::Return,
383            T::Throw => Self::Throw,
384            T::Try => Self::Try,
385            T::Catch => Self::Catch,
386            T::Finally => Self::Finally,
387            T::Guard => Self::Guard,
388            T::Defer => Self::Defer,
389            T::Public => Self::Public,
390            T::Private => Self::Private,
391            T::Internal => Self::Internal,
392            T::Fileprivate => Self::Fileprivate,
393            T::Open => Self::Open,
394            T::Static => Self::Static,
395            T::Final => Self::Final,
396            T::Override => Self::Override,
397            T::Mutating => Self::Mutating,
398            T::Nonmutating => Self::Nonmutating,
399            T::Lazy => Self::Lazy,
400            T::Weak => Self::Weak,
401            T::Unowned => Self::Unowned,
402            T::Optional => Self::Optional,
403            T::Required => Self::Required,
404            T::Convenience => Self::Convenience,
405            T::Dynamic => Self::Dynamic,
406            T::Infix => Self::Infix,
407            T::Prefix => Self::Prefix,
408            T::Postfix => Self::Postfix,
409            T::Any => Self::Any,
410            T::AnyObject => Self::AnyObject,
411            T::Self_ => Self::Self_,
412            T::Type => Self::Type,
413            T::Protocol_ => Self::Protocol_,
414            T::True => Self::True,
415            T::False => Self::False,
416            T::Nil => Self::Nil,
417            T::As => Self::As,
418            T::Is => Self::Is,
419            T::In => Self::In,
420            T::Where => Self::Where,
421            T::Associatedtype => Self::Associatedtype,
422            T::Operator => Self::Operator,
423            T::Precedencegroup => Self::Precedencegroup,
424            T::Indirect => Self::Indirect,
425            T::Rethrows => Self::Rethrows,
426            T::Throws => Self::Throws,
427            T::Inout => Self::Inout,
428            T::Plus => Self::Plus,
429            T::Minus => Self::Minus,
430            T::Star => Self::Star,
431            T::Slash => Self::Slash,
432            T::Percent => Self::Percent,
433            T::Equal => Self::Equal,
434            T::NotEqual => Self::NotEqual,
435            T::Less => Self::Less,
436            T::Greater => Self::Greater,
437            T::LessEqual => Self::LessEqual,
438            T::GreaterEqual => Self::GreaterEqual,
439            T::LogicalAnd => Self::LogicalAnd,
440            T::LogicalOr => Self::LogicalOr,
441            T::LogicalNot => Self::LogicalNot,
442            T::BitAnd => Self::BitAnd,
443            T::BitOr => Self::BitOr,
444            T::BitXor => Self::BitXor,
445            T::BitNot => Self::BitNot,
446            T::LeftShift => Self::LeftShift,
447            T::RightShift => Self::RightShift,
448            T::Assign => Self::Assign,
449            T::PlusAssign => Self::PlusAssign,
450            T::MinusAssign => Self::MinusAssign,
451            T::StarAssign => Self::StarAssign,
452            T::SlashAssign => Self::SlashAssign,
453            T::PercentAssign => Self::PercentAssign,
454            T::AndAssign => Self::AndAssign,
455            T::OrAssign => Self::OrAssign,
456            T::XorAssign => Self::XorAssign,
457            T::LeftShiftAssign => Self::LeftShiftAssign,
458            T::RightShiftAssign => Self::RightShiftAssign,
459            T::Question => Self::Question,
460            T::QuestionQuestion => Self::QuestionQuestion,
461            T::Dot => Self::Dot,
462            T::Arrow => Self::Arrow,
463            T::Range => Self::Range,
464            T::ClosedRange => Self::ClosedRange,
465            T::LeftParen => Self::LeftParen,
466            T::RightParen => Self::RightParen,
467            T::LeftBracket => Self::LeftBracket,
468            T::RightBracket => Self::RightBracket,
469            T::LeftBrace => Self::LeftBrace,
470            T::RightBrace => Self::RightBrace,
471            T::Comma => Self::Comma,
472            T::Semicolon => Self::Semicolon,
473            T::Colon => Self::Colon,
474            T::At => Self::At,
475            T::Hash => Self::Hash,
476            T::Dollar => Self::Dollar,
477            T::Underscore => Self::Underscore,
478            T::Backslash => Self::Backslash,
479            T::SourceFile => Self::SourceFile,
480            T::FunctionDeclaration => Self::FunctionDeclaration,
481            T::Parameter => Self::Parameter,
482            T::ParameterList => Self::ParameterList,
483            T::VariableDeclaration => Self::VariableDeclaration,
484            T::ClassDeclaration => Self::ClassDeclaration,
485            T::StructDeclaration => Self::StructDeclaration,
486            T::EnumDeclaration => Self::EnumDeclaration,
487            T::ProtocolDeclaration => Self::ProtocolDeclaration,
488            T::IfStatement => Self::IfStatement,
489            T::WhileStatement => Self::WhileStatement,
490            T::ForStatement => Self::ForStatement,
491            T::ReturnStatement => Self::ReturnStatement,
492            T::BreakStatement => Self::BreakStatement,
493            T::ContinueStatement => Self::ContinueStatement,
494            T::ExpressionStatement => Self::ExpressionStatement,
495            T::Block => Self::Block,
496            T::BinaryExpression => Self::BinaryExpression,
497            T::UnaryExpression => Self::UnaryExpression,
498            T::CallExpression => Self::CallExpression,
499            T::MemberExpression => Self::MemberExpression,
500            T::IdentifierExpression => Self::IdentifierExpression,
501            T::LiteralExpression => Self::LiteralExpression,
502        }
503    }
504}