Skip to main content

oak_fsharp/parser/
element_type.rs

1use oak_core::{ElementType, UniversalElementRole};
2
3/// F# element types
4#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
5#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
6pub enum FSharpElementType {
7    /// Root node
8    Root,
9    /// Expression
10    Expression,
11    /// Whitespace
12    Whitespace,
13    /// Newline
14    Newline,
15
16    /// Identifier
17    Identifier,
18    /// Integer literal
19    IntegerLiteral,
20    /// Float literal
21    FloatLiteral,
22    /// String literal
23    StringLiteral,
24    /// Character literal
25    CharLiteral,
26    /// Boolean literal
27    BooleanLiteral,
28    /// Unit literal
29    UnitLiteral,
30    /// Literal
31    Literal,
32
33    /// Match case
34    MatchCase,
35    /// Wildcard pattern
36    WildcardPattern,
37    /// Identifier pattern
38    IdentifierPattern,
39    /// Tuple pattern
40    TuplePattern,
41    /// List pattern
42    ListPattern,
43    /// Active pattern
44    ActivePattern,
45    /// Pattern
46    Pattern,
47    /// Lambda expression
48    Lambda,
49    /// Parenthesized expression
50    Parenthesized,
51    /// List expression
52    List,
53    /// Record expression
54    Record,
55    /// Function application
56    Application,
57
58    /// The 'let' keyword
59    Let,
60    /// The 'rec' keyword
61    Rec,
62    /// The 'and' keyword
63    And,
64    /// The 'in' keyword
65    In,
66    /// The 'if' keyword
67    If,
68    /// The 'then' keyword
69    Then,
70    /// The 'else' keyword
71    Else,
72    /// The 'elif' keyword
73    Elif,
74    /// The 'match' keyword
75    Match,
76    /// The 'with' keyword
77    With,
78    /// The 'when' keyword
79    When,
80    /// The 'function' keyword
81    Function,
82    /// The 'fun' keyword
83    Fun,
84
85    /// The 'type' keyword
86    Type,
87    /// The 'val' keyword
88    Val,
89    /// The 'mutable' keyword
90    Mutable,
91    /// The 'of' keyword
92    Of,
93    /// The 'as' keyword
94    As,
95
96    /// The 'module' keyword
97    Module,
98    /// The 'namespace' keyword
99    Namespace,
100    /// The 'open' keyword
101    Open,
102
103    /// The 'try' keyword
104    Try,
105    /// The 'finally' keyword
106    Finally,
107    /// The 'exception' keyword
108    Exception,
109    /// The 'raise' keyword
110    Raise,
111    /// The 'failwith' keyword
112    Failwith,
113
114    /// The 'for' keyword
115    For,
116    /// The 'to' keyword
117    To,
118    /// The 'downto' keyword
119    Downto,
120    /// The 'do' keyword
121    Do,
122    /// The 'done' keyword
123    Done,
124    /// The 'while' keyword
125    While,
126    /// The 'yield' keyword
127    Yield,
128    /// The 'return' keyword
129    Return,
130
131    /// The 'class' keyword
132    Class,
133    /// The 'interface' keyword
134    Interface,
135    /// The 'inherit' keyword
136    Inherit,
137    /// The 'abstract' keyword
138    Abstract,
139    /// The 'override' keyword
140    Override,
141    /// The 'default' keyword
142    Default,
143    /// The 'member' keyword
144    Member,
145    /// The 'static' keyword
146    Static,
147    /// The 'new' keyword
148    New,
149
150    /// The 'lazy' keyword
151    Lazy,
152    /// The 'async' keyword
153    Async,
154    /// The 'seq' keyword
155    Seq,
156    /// The 'use' keyword
157    Use,
158    /// The 'begin' keyword
159    Begin,
160    /// The 'end' keyword
161    End,
162    /// The 'struct' keyword
163    Struct,
164    /// The 'sig' keyword
165    Sig,
166
167    /// The 'true' keyword
168    True,
169    /// The 'false' keyword
170    False,
171    /// The 'null' keyword
172    Null,
173    /// The 'or' keyword
174    Or,
175
176    /// The 'public' keyword
177    Public,
178    /// The 'private' keyword
179    Private,
180    /// The 'internal' keyword
181    Internal,
182
183    /// The 'inline' keyword
184    Inline,
185    /// The 'extern' keyword
186    Extern,
187    /// The 'upcast' keyword
188    Upcast,
189    /// The 'downcast' keyword
190    Downcast,
191    /// The 'assert' keyword
192    Assert,
193    /// The 'global' keyword
194    Global,
195    /// The 'base' keyword
196    Base,
197    /// The 'this' keyword
198    This,
199    /// The 'void' keyword
200    Void,
201    /// The 'delegate' keyword
202    Delegate,
203    /// The 'select' keyword
204    Select,
205
206    /// The 'obj' keyword
207    Obj,
208    /// The 'unit' keyword
209    Unit,
210    /// The 'int' keyword
211    Int,
212    /// The 'float' keyword
213    Float,
214    /// The 'string' keyword
215    String,
216    /// The 'bool' keyword
217    Bool,
218    /// The 'char' keyword
219    Char,
220    /// The 'byte' keyword
221    Byte,
222    /// The 'sbyte' keyword
223    SByte,
224    /// The 'int16' keyword
225    Int16,
226    /// The 'uint16' keyword
227    UInt16,
228    /// The 'int32' keyword
229    Int32,
230    /// The 'uint32' keyword
231    UInt32,
232    /// The 'int64' keyword
233    Int64,
234    /// The 'uint64' keyword
235    UInt64,
236    /// The 'nativeint' keyword
237    NativeInt,
238    /// The 'unativeint' keyword
239    UNativeInt,
240    /// The 'decimal' keyword
241    Decimal,
242    /// The 'bigint' keyword
243    BigInt,
244
245    /// The '+' operator
246    Plus,
247    /// The '-' operator
248    Minus,
249    /// The '*' operator
250    Star,
251    /// The '/' operator
252    Slash,
253    /// The '%' operator
254    Percent,
255    /// The '**' operator
256    StarStar,
257
258    /// The '=' operator
259    Equal,
260    /// The '<>' operator
261    NotEqual,
262    /// The '<' operator
263    LessThan,
264    /// The '<=' operator
265    LessEqual,
266    /// The '>' operator
267    GreaterThan,
268    /// The '>=' operator
269    GreaterEqual,
270
271    /// The '&&' operator
272    AndAnd,
273    /// The '||' operator
274    OrOr,
275    /// The 'not' operator
276    Not,
277
278    /// The '&&&' operator
279    BitwiseAnd,
280    /// The '|||' operator
281    BitwiseOr,
282    /// The '^^^' operator
283    BitwiseXor,
284    /// The '~~~' operator
285    BitwiseNot,
286    /// The '<<<' operator
287    LeftShift,
288    /// The '>>>' operator
289    RightShift,
290
291    /// The '->' operator
292    Arrow,
293    /// The '=>' operator
294    DoubleArrow,
295    /// The '|' operator
296    Pipe,
297    /// The '|>' operator
298    PipeRight,
299    /// The '||' operator
300    DoublePipe,
301    /// The '::' operator
302    Cons,
303    /// The '@' operator
304    At,
305    /// The '>>' operator
306    Compose,
307    /// The '<<' operator
308    ComposeBack,
309    /// The '$' operator
310    Dollar,
311
312    /// The '&&' logical operator
313    LogicalAnd,
314    /// The '||' logical operator
315    LogicalOr,
316    /// The '&' operator
317    Ampersand,
318    /// The '^' operator
319    Caret,
320    /// The '~' operator
321    Tilde,
322    /// The '<' operator
323    Less,
324    /// The '>' operator
325    Greater,
326    /// The '|>' operator
327    PipeGreater,
328    /// The '!' operator
329    Exclamation,
330    /// The ':=' operator
331    ColonEqual,
332    /// The '<-' operator
333    LArrow,
334    /// The '++' operator
335    PlusPlus,
336    /// The '--' operator
337    MinusMinus,
338
339    /// The '(' delimiter
340    LeftParen,
341    /// The ')' delimiter
342    RightParen,
343    /// The '[' delimiter
344    LeftBracket,
345    /// The ']' delimiter
346    RightBracket,
347    /// The '[|' delimiter
348    LeftArrayBracket,
349    /// The '|]' delimiter
350    RightArrayBracket,
351    /// The '[<' delimiter
352    LeftBracketBar,
353    /// The '>]' delimiter
354    RightBracketBar,
355    /// The '[ <' delimiter
356    LeftBracketAngle,
357    /// The '> ]' delimiter
358    RightBracketAngle,
359    /// The '{' delimiter
360    LeftBrace,
361    /// The '}' delimiter
362    RightBrace,
363    /// The '<' delimiter
364    LeftAngle,
365    /// The '>' delimiter
366    RightAngle,
367
368    /// The ',' punctuation
369    Comma,
370    /// The ';' punctuation
371    Semicolon,
372    /// The ':' punctuation
373    Colon,
374    /// The '::' punctuation
375    DoubleColon,
376    /// The '.' punctuation
377    Dot,
378    /// The '..' punctuation
379    DotDot,
380    /// The '?' punctuation
381    Question,
382    /// The '_' punctuation
383    Underscore,
384    /// The ''' punctuation
385    Apostrophe,
386    /// The '`' punctuation
387    Backtick,
388    /// The '#' punctuation
389    Hash,
390
391    /// Line comment
392    LineComment,
393    /// Block comment
394    BlockComment,
395
396    /// Error
397    Error,
398    /// End of file
399    Eof,
400}
401
402impl FSharpElementType {
403    /// Checks if it is a keyword
404    pub fn is_keyword(&self) -> bool {
405        matches!(
406            self,
407            Self::Let
408                | Self::Rec
409                | Self::And
410                | Self::In
411                | Self::If
412                | Self::Then
413                | Self::Else
414                | Self::Elif
415                | Self::Match
416                | Self::With
417                | Self::When
418                | Self::Function
419                | Self::Fun
420                | Self::Type
421                | Self::Val
422                | Self::Mutable
423                | Self::Of
424                | Self::As
425                | Self::Module
426                | Self::Namespace
427                | Self::Open
428                | Self::Try
429                | Self::Finally
430                | Self::Exception
431                | Self::Raise
432                | Self::Failwith
433                | Self::For
434                | Self::To
435                | Self::Downto
436                | Self::Do
437                | Self::Done
438                | Self::While
439                | Self::Yield
440                | Self::Return
441                | Self::Class
442                | Self::Interface
443                | Self::Inherit
444                | Self::Abstract
445                | Self::Override
446                | Self::Default
447                | Self::Member
448                | Self::Static
449                | Self::New
450                | Self::Lazy
451                | Self::Async
452                | Self::Seq
453                | Self::Use
454                | Self::Begin
455                | Self::End
456                | Self::Struct
457                | Self::Sig
458                | Self::True
459                | Self::False
460                | Self::Null
461                | Self::Or
462                | Self::Public
463                | Self::Private
464                | Self::Internal
465                | Self::Inline
466                | Self::Extern
467                | Self::Upcast
468                | Self::Downcast
469                | Self::Assert
470                | Self::Delegate
471                | Self::Select
472        )
473    }
474}
475
476impl ElementType for FSharpElementType {
477    type Role = UniversalElementRole;
478
479    fn role(&self) -> Self::Role {
480        match self {
481            Self::Error => UniversalElementRole::Error,
482            _ => UniversalElementRole::None,
483        }
484    }
485}
486
487impl From<crate::lexer::token_type::FSharpTokenType> for FSharpElementType {
488    fn from(token: crate::lexer::token_type::FSharpTokenType) -> Self {
489        unsafe { std::mem::transmute(token) }
490    }
491}