Skip to main content

oak_actionscript/lexer/
token_type.rs

1use oak_core::{Token, TokenType, UniversalTokenRole};
2
3/// A token in the ActionScript language.
4pub type ActionScriptToken = Token<ActionScriptTokenType>;
5
6/// Token types for the ActionScript language.
7#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
8#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
9#[repr(u8)]
10pub enum ActionScriptTokenType {
11    /// Whitespace characters.
12    Whitespace,
13    /// Newline characters.
14    Newline,
15    /// Comments.
16    Comment,
17    /// An identifier.
18    Identifier,
19    /// A string literal.
20    StringLiteral,
21    /// A character literal.
22    CharLiteral,
23    /// A number literal.
24    NumberLiteral,
25    /// A boolean literal.
26    BooleanLiteral,
27    /// A null literal.
28    NullLiteral,
29
30    // Keywords
31    /// `as` keyword.
32    As,
33    /// `break` keyword.
34    Break,
35    /// `case` keyword.
36    Case,
37    /// `catch` keyword.
38    Catch,
39    /// `class` keyword.
40    Class,
41    /// `const` keyword.
42    Const,
43    /// `continue` keyword.
44    Continue,
45    /// `default` keyword.
46    Default,
47    /// `delete` keyword.
48    Delete,
49    /// `do` keyword.
50    Do,
51    /// `else` keyword.
52    Else,
53    /// `extends` keyword.
54    Extends,
55    /// `false` keyword.
56    False,
57    /// `finally` keyword.
58    Finally,
59    /// `for` keyword.
60    For,
61    /// `function` keyword.
62    Function,
63    /// `if` keyword.
64    If,
65    /// `implements` keyword.
66    Implements,
67    /// `import` keyword.
68    Import,
69    /// `in` keyword.
70    In,
71    /// `instanceof` keyword.
72    Instanceof,
73    /// `interface` keyword.
74    Interface,
75    /// `internal` keyword.
76    Internal,
77    /// `is` keyword.
78    Is,
79    /// `native` keyword.
80    Native,
81    /// `new` keyword.
82    New,
83    /// `null` keyword.
84    Null,
85    /// `package` keyword.
86    Package,
87    /// `private` keyword.
88    Private,
89    /// `protected` keyword.
90    Protected,
91    /// `public` keyword.
92    Public,
93    /// `return` keyword.
94    Return,
95    /// `static` keyword.
96    Static,
97    /// `super` keyword.
98    Super,
99    /// `switch` keyword.
100    Switch,
101    /// `this` keyword.
102    This,
103    /// `throw` keyword.
104    Throw,
105    /// `true` keyword.
106    True,
107    /// `try` keyword.
108    Try,
109    /// `typeof` keyword.
110    Typeof,
111    /// `use` keyword.
112    Use,
113    /// `var` keyword.
114    Var,
115    /// `void` keyword.
116    Void,
117    /// `while` keyword.
118    While,
119    /// `with` keyword.
120    With,
121    /// `each` keyword.
122    Each,
123    /// `get` keyword.
124    Get,
125    /// `set` keyword.
126    Set,
127    /// `namespace` keyword.
128    Namespace,
129    /// `include` keyword.
130    Include,
131    /// `dynamic` keyword.
132    Dynamic,
133    /// `final` keyword.
134    Final,
135    /// `override` keyword.
136    Override,
137
138    // Types
139    /// `Array` type.
140    Array,
141    /// `Boolean` type.
142    Boolean,
143    /// `Date` type.
144    Date,
145    /// `Number` type.
146    Number,
147    /// `Object` type.
148    ObjectType,
149    /// `RegExp` type.
150    RegExp,
151    /// `String` type.
152    StringType,
153    /// `uint` type.
154    Uint,
155    /// `Vector` type.
156    Vector,
157    /// `void` type.
158    VoidType,
159    /// `XML` type.
160    Xml,
161    /// `XMLList` type.
162    XmlList,
163
164    // Operators
165    /// `+` operator.
166    Plus,
167    /// `-` operator.
168    Minus,
169    /// `*` operator.
170    Star,
171    /// `/` operator.
172    Slash,
173    /// `%` operator.
174    Percent,
175    /// `=` operator.
176    Equal,
177    /// `==` operator.
178    EqualEqual,
179    /// `===` operator.
180    EqualEqualEqual,
181    /// `!=` operator.
182    NotEqual,
183    /// `!==` operator.
184    NotEqualEqual,
185    /// `<` operator.
186    LessThan,
187    /// `<=` operator.
188    LessEqual,
189    /// `>` operator.
190    GreaterThan,
191    /// `>=` operator.
192    GreaterEqual,
193    /// `&&` operator.
194    LogicalAnd,
195    /// `||` operator.
196    LogicalOr,
197    /// `!` operator.
198    LogicalNot,
199    /// `&` operator.
200    BitwiseAnd,
201    /// `|` operator.
202    BitwiseOr,
203    /// `^` operator.
204    BitwiseXor,
205    /// `~` operator.
206    BitwiseNot,
207    /// `<<` operator.
208    LeftShift,
209    /// `>>` operator.
210    RightShift,
211    /// `>>>` operator.
212    UnsignedRightShift,
213    /// `++` operator.
214    Increment,
215    /// `--` operator.
216    Decrement,
217    /// `+=` operator.
218    PlusAssign,
219    /// `-=` operator.
220    MinusAssign,
221    /// `*=` operator.
222    StarAssign,
223    /// `/=` operator.
224    SlashAssign,
225    /// `%=` operator.
226    PercentAssign,
227    /// `<<=` operator.
228    LeftShiftAssign,
229    /// `>>=` operator.
230    RightShiftAssign,
231    /// `>>>=` operator.
232    UnsignedRightShiftAssign,
233    /// `&=` operator.
234    BitwiseAndAssign,
235    /// `|=` operator.
236    BitwiseOrAssign,
237    /// `^=` operator.
238    BitwiseXorAssign,
239
240    // Punctuation
241    /// `?` punctuation.
242    Question,
243    /// `:` punctuation.
244    Colon,
245    /// `.` punctuation.
246    Dot,
247    /// `->` operator.
248    Arrow,
249    /// `(` punctuation.
250    LeftParen,
251    /// `)` punctuation.
252    RightParen,
253    /// `{` punctuation.
254    LeftBrace,
255    /// `}` punctuation.
256    RightBrace,
257    /// `[` punctuation.
258    LeftBracket,
259    /// `]` punctuation.
260    RightBracket,
261    /// `;` punctuation.
262    Semicolon,
263    /// `,` punctuation.
264    Comma,
265    /// `@` punctuation.
266    At,
267    /// `#` punctuation.
268    Hash,
269    /// `$` punctuation.
270    Dollar,
271    /// `&` punctuation.
272    Ampersand,
273    /// `\` punctuation.
274    Backslash,
275    /// `'` punctuation.
276    Quote,
277    /// `"` punctuation.
278    DoubleQuote,
279    /// `` ` `` punctuation.
280    Backtick,
281
282    /// End of file token.
283    Eof,
284
285    // Element types (used for elements)
286    /// A program.
287    Program,
288    /// A block.
289    Block,
290    /// A variable.
291    Variable,
292    /// A function call.
293    FunctionCall,
294    /// A method call.
295    MethodCall,
296    /// A property access.
297    PropertyAccess,
298    /// An array access.
299    ArrayAccess,
300    /// A parameter list.
301    ParameterList,
302    /// A use item.
303    UseItem,
304    /// A module item.
305    ModuleItem,
306    /// A struct item.
307    StructItem,
308    /// An enum item.
309    EnumItem,
310    /// A function type.
311    FunctionType,
312    /// Root element.
313    Root,
314    /// A statement.
315    Statement,
316    /// An expression.
317    Expression,
318    /// An assignment.
319    Assignment,
320    /// A conditional expression.
321    ConditionalExpression,
322    /// A binary expression.
323    BinaryExpression,
324    /// A unary expression.
325    UnaryExpression,
326    /// An if statement.
327    IfStatement,
328    /// A for statement.
329    ForStatement,
330    /// A while statement.
331    WhileStatement,
332    /// A do-while statement.
333    DoWhileStatement,
334    /// A switch statement.
335    SwitchStatement,
336    /// A try statement.
337    TryStatement,
338    /// A throw statement.
339    ThrowStatement,
340    /// A return statement.
341    ReturnStatement,
342    /// A break statement.
343    BreakStatement,
344    /// A continue statement.
345    ContinueStatement,
346    /// Error token.
347    Error,
348    /// A literal expression.
349    LiteralExpression,
350    /// An identifier expression.
351    IdentifierExpression,
352    /// A parenthesized expression.
353    ParenthesizedExpression,
354    /// A source file.
355    SourceFile,
356    /// A block expression.
357    BlockExpression,
358    /// A let statement.
359    LetStatement,
360    /// An if expression.
361    IfExpression,
362    /// A while expression.
363    WhileExpression,
364    /// A loop expression.
365    LoopExpression,
366    /// A for expression.
367    ForExpression,
368    /// A call expression.
369    CallExpression,
370    /// An index expression.
371    IndexExpression,
372    /// A field expression.
373    FieldExpression,
374}
375
376impl TokenType for ActionScriptTokenType {
377    type Role = UniversalTokenRole;
378    const END_OF_STREAM: Self = Self::Eof;
379
380    fn is_ignored(&self) -> bool {
381        matches!(self, Self::Whitespace | Self::Newline | Self::Comment)
382    }
383
384    fn role(&self) -> Self::Role {
385        match self {
386            t if t.is_keyword() => UniversalTokenRole::Keyword,
387            t if t.is_operator() => UniversalTokenRole::Operator,
388            t if t.is_punctuation() => UniversalTokenRole::Punctuation,
389            Self::Identifier => UniversalTokenRole::Name,
390            t if t.is_literal() => UniversalTokenRole::Literal,
391            Self::Comment => UniversalTokenRole::Comment,
392            Self::Eof => UniversalTokenRole::Eof,
393            Self::Error => UniversalTokenRole::Error,
394            _ => UniversalTokenRole::None,
395        }
396    }
397}
398
399impl ActionScriptTokenType {
400    /// Returns true if the token type is a literal.
401    pub fn is_literal(&self) -> bool {
402        matches!(self, Self::StringLiteral | Self::CharLiteral | Self::NumberLiteral | Self::BooleanLiteral | Self::NullLiteral | Self::True | Self::False | Self::Null)
403    }
404
405    /// Returns true if the token type is a keyword.
406    pub fn is_keyword(&self) -> bool {
407        matches!(
408            self,
409            Self::As
410                | Self::Break
411                | Self::Case
412                | Self::Catch
413                | Self::Class
414                | Self::Const
415                | Self::Continue
416                | Self::Default
417                | Self::Delete
418                | Self::Do
419                | Self::Else
420                | Self::Extends
421                | Self::Finally
422                | Self::For
423                | Self::Function
424                | Self::If
425                | Self::Implements
426                | Self::Import
427                | Self::In
428                | Self::Instanceof
429                | Self::Interface
430                | Self::Internal
431                | Self::Is
432                | Self::Native
433                | Self::New
434                | Self::Package
435                | Self::Private
436                | Self::Protected
437                | Self::Public
438                | Self::Return
439                | Self::Static
440                | Self::Super
441                | Self::Switch
442                | Self::This
443                | Self::Throw
444                | Self::Try
445                | Self::Typeof
446                | Self::Use
447                | Self::Var
448                | Self::Void
449                | Self::While
450                | Self::With
451                | Self::Each
452                | Self::Get
453                | Self::Set
454                | Self::Namespace
455                | Self::Include
456                | Self::Dynamic
457                | Self::Final
458                | Self::Override
459        )
460    }
461
462    /// Returns true if the token type is an operator.
463    pub fn is_operator(&self) -> bool {
464        matches!(
465            self,
466            Self::Plus
467                | Self::Minus
468                | Self::Star
469                | Self::Slash
470                | Self::Percent
471                | Self::Equal
472                | Self::EqualEqual
473                | Self::EqualEqualEqual
474                | Self::NotEqual
475                | Self::NotEqualEqual
476                | Self::LessThan
477                | Self::LessEqual
478                | Self::GreaterThan
479                | Self::GreaterEqual
480                | Self::LogicalAnd
481                | Self::LogicalOr
482                | Self::LogicalNot
483                | Self::BitwiseAnd
484                | Self::BitwiseOr
485                | Self::BitwiseXor
486                | Self::BitwiseNot
487                | Self::LeftShift
488                | Self::RightShift
489                | Self::UnsignedRightShift
490                | Self::PlusAssign
491                | Self::MinusAssign
492                | Self::StarAssign
493                | Self::SlashAssign
494                | Self::PercentAssign
495                | Self::LeftShiftAssign
496                | Self::RightShiftAssign
497                | Self::UnsignedRightShiftAssign
498                | Self::BitwiseAndAssign
499                | Self::BitwiseOrAssign
500                | Self::BitwiseXorAssign
501                | Self::Question
502        )
503    }
504
505    /// Returns true if the token type is a punctuation.
506    pub fn is_punctuation(&self) -> bool {
507        matches!(self, Self::LeftParen | Self::RightParen | Self::LeftBracket | Self::RightBracket | Self::LeftBrace | Self::RightBrace | Self::Dot | Self::Comma | Self::Colon | Self::Semicolon)
508    }
509}