Skip to main content

oak_actionscript/lexer/
token_type.rs

1use oak_core::{Token, TokenType, UniversalTokenRole};
2use serde::{Deserialize, Serialize};
3
4/// ActionScript 语言的标记
5pub type ActionScriptToken = Token<ActionScriptTokenType>;
6
7/// Represents the different types of tokens in the ActionScript language.
8#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, Serialize, Deserialize)]
9pub enum ActionScriptTokenType {
10    /// Whitespace characters (spaces, tabs)
11    Whitespace,
12    /// Newline characters
13    Newline,
14    /// Comments (both single-line and multi-line)
15    Comment,
16
17    /// Identifiers (variable names, function names, etc.)
18    Identifier,
19    /// String literals (e.g., "hello")
20    StringLiteral,
21    /// Character literals (e.g., 'a')
22    CharLiteral,
23    /// Number literals (integer and floating-point)
24    NumberLiteral,
25    /// Boolean literals (true, false)
26    BooleanLiteral,
27    /// Null literal
28    NullLiteral,
29
30    // Keywords
31    As,
32    Break,
33    Case,
34    Catch,
35    Class,
36    Const,
37    Continue,
38    Default,
39    Delete,
40    Do,
41    Else,
42    Extends,
43    False,
44    Finally,
45    For,
46    Function,
47    If,
48    Implements,
49    Import,
50    In,
51    Instanceof,
52    Interface,
53    Internal,
54    Is,
55    Native,
56    New,
57    Null,
58    Package,
59    Private,
60    Protected,
61    Public,
62    Return,
63    Static,
64    Super,
65    Switch,
66    This,
67    Throw,
68    True,
69    Try,
70    Typeof,
71    Use,
72    Var,
73    Void,
74    While,
75    With,
76
77    // AS3 specific keywords
78    Each,
79    Get,
80    Set,
81    Namespace,
82    Include,
83    Dynamic,
84    Final,
85    Override,
86
87    // Built-in types
88    Array,
89    Boolean,
90    Date,
91    FunctionType,
92    Number,
93    ObjectType,
94    RegExp,
95    StringType,
96    Uint,
97    Vector,
98    VoidType,
99    Xml,
100    XmlList,
101
102    // Operators
103    Plus,
104    Minus,
105    Star,
106    Slash,
107    Percent,
108    Equal,
109    EqualEqual,
110    EqualEqualEqual,
111    NotEqual,
112    NotEqualEqual,
113    LessThan,
114    LessEqual,
115    GreaterThan,
116    GreaterEqual,
117    LogicalAnd,
118    LogicalOr,
119    LogicalNot,
120    BitwiseAnd,
121    BitwiseOr,
122    BitwiseXor,
123    BitwiseNot,
124    LeftShift,
125    RightShift,
126    UnsignedRightShift,
127    Increment,
128    Decrement,
129    PlusAssign,
130    MinusAssign,
131    StarAssign,
132    SlashAssign,
133    PercentAssign,
134    LeftShiftAssign,
135    RightShiftAssign,
136    UnsignedRightShiftAssign,
137    BitwiseAndAssign,
138    BitwiseOrAssign,
139    BitwiseXorAssign,
140    Question,
141    Colon,
142    Dot,
143    Arrow,
144
145    // Punctuation
146    LeftParen,
147    RightParen,
148    LeftBrace,
149    RightBrace,
150    LeftBracket,
151    RightBracket,
152    Semicolon,
153    Comma,
154
155    // Symbols
156    At,
157    Hash,
158    Dollar,
159    Ampersand,
160    Backslash,
161    Quote,
162    DoubleQuote,
163    Backtick,
164
165    /// End of file marker
166    Eof,
167}
168
169impl TokenType for ActionScriptTokenType {
170    type Role = UniversalTokenRole;
171    const END_OF_STREAM: Self = ActionScriptTokenType::Eof;
172
173    fn is_ignored(&self) -> bool {
174        matches!(self, Self::Whitespace | Self::Newline | Self::Comment)
175    }
176
177    fn is_comment(&self) -> bool {
178        matches!(self, Self::Comment)
179    }
180
181    fn is_whitespace(&self) -> bool {
182        matches!(self, Self::Whitespace | Self::Newline)
183    }
184
185    fn role(&self) -> Self::Role {
186        match self {
187            Self::Whitespace | Self::Newline => UniversalTokenRole::Whitespace,
188            Self::Comment => UniversalTokenRole::Comment,
189            Self::Identifier => UniversalTokenRole::Name,
190            Self::StringLiteral | Self::CharLiteral | Self::NumberLiteral | Self::BooleanLiteral | Self::NullLiteral => UniversalTokenRole::Literal,
191            Self::As
192            | Self::Break
193            | Self::Case
194            | Self::Catch
195            | Self::Class
196            | Self::Const
197            | Self::Continue
198            | Self::Default
199            | Self::Delete
200            | Self::Do
201            | Self::Else
202            | Self::Extends
203            | Self::False
204            | Self::Finally
205            | Self::For
206            | Self::Function
207            | Self::If
208            | Self::Implements
209            | Self::Import
210            | Self::In
211            | Self::Instanceof
212            | Self::Interface
213            | Self::Internal
214            | Self::Is
215            | Self::Native
216            | Self::New
217            | Self::Null
218            | Self::Package
219            | Self::Private
220            | Self::Protected
221            | Self::Public
222            | Self::Return
223            | Self::Static
224            | Self::Super
225            | Self::Switch
226            | Self::This
227            | Self::Throw
228            | Self::True
229            | Self::Try
230            | Self::Typeof
231            | Self::Use
232            | Self::Var
233            | Self::Void
234            | Self::While
235            | Self::With
236            | Self::Each
237            | Self::Get
238            | Self::Set
239            | Self::Namespace
240            | Self::Include
241            | Self::Dynamic
242            | Self::Final
243            | Self::Override => UniversalTokenRole::Keyword,
244            Self::Array | Self::Boolean | Self::Date | Self::FunctionType | Self::Number | Self::ObjectType | Self::RegExp | Self::StringType | Self::Uint | Self::Vector | Self::VoidType | Self::Xml | Self::XmlList => UniversalTokenRole::Keyword,
245            Self::Plus
246            | Self::Minus
247            | Self::Star
248            | Self::Slash
249            | Self::Percent
250            | Self::Equal
251            | Self::EqualEqual
252            | Self::EqualEqualEqual
253            | Self::NotEqual
254            | Self::NotEqualEqual
255            | Self::LessThan
256            | Self::LessEqual
257            | Self::GreaterThan
258            | Self::GreaterEqual
259            | Self::LogicalAnd
260            | Self::LogicalOr
261            | Self::LogicalNot
262            | Self::BitwiseAnd
263            | Self::BitwiseOr
264            | Self::BitwiseXor
265            | Self::BitwiseNot
266            | Self::LeftShift
267            | Self::RightShift
268            | Self::UnsignedRightShift
269            | Self::Increment
270            | Self::Decrement
271            | Self::PlusAssign
272            | Self::MinusAssign
273            | Self::StarAssign
274            | Self::SlashAssign
275            | Self::PercentAssign
276            | Self::LeftShiftAssign
277            | Self::RightShiftAssign
278            | Self::UnsignedRightShiftAssign
279            | Self::BitwiseAndAssign
280            | Self::BitwiseOrAssign
281            | Self::BitwiseXorAssign
282            | Self::Question
283            | Self::Colon
284            | Self::Dot
285            | Self::Arrow => UniversalTokenRole::Operator,
286            Self::LeftParen | Self::RightParen | Self::LeftBrace | Self::RightBrace | Self::LeftBracket | Self::RightBracket | Self::Semicolon | Self::Comma => UniversalTokenRole::Punctuation,
287            _ => UniversalTokenRole::None,
288        }
289    }
290}
291
292impl ActionScriptTokenType {
293    /// Returns true if the token is a literal.
294    pub fn is_literal(&self) -> bool {
295        matches!(self, Self::StringLiteral | Self::CharLiteral | Self::NumberLiteral | Self::BooleanLiteral | Self::NullLiteral)
296    }
297}