Skip to main content

oak_actionscript/lexer/
token_type.rs

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