Skip to main content

oak_ada/lexer/
token_type.rs

1use oak_core::{TokenType, UniversalTokenRole};
2#[cfg(feature = "serde")]
3use serde::{Deserialize, Serialize};
4
5/// Ada 词法分析器的词法单元类型。
6#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
7#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
8pub enum AdaTokenType {
9    /// 空白字符。
10    Whitespace,
11    /// 换行符。
12    Newline,
13    /// 注释。
14    Comment,
15
16    /// 字符串字面量。
17    StringLiteral,
18    /// 字符字面量。
19    CharacterLiteral,
20    /// 数字字面量。
21    NumberLiteral,
22    /// 标识符。
23    Identifier,
24
25    /// `abort` 关键字。
26    Abort,
27    /// `abs` 关键字。
28    Abs,
29    /// `abstract` 关键字。
30    Abstract,
31    /// `accept` 关键字。
32    Accept,
33    /// `access` 关键字。
34    Access,
35    /// `aliased` 关键字。
36    Aliased,
37    /// `all` 关键字。
38    All,
39    /// `and` 关键字。
40    And,
41    /// `array` 关键字。
42    Array,
43    /// `at` 关键字。
44    At,
45    /// `begin` 关键字。
46    Begin,
47    /// `body` 关键字。
48    Body,
49    /// `case` 关键字。
50    Case,
51    /// `constant` 关键字。
52    Constant,
53    /// `declare` 关键字。
54    Declare,
55    /// `delay` 关键字。
56    Delay,
57    /// `delta` 关键字。
58    Delta,
59    /// `digits` 关键字。
60    Digits,
61    /// `do` 关键字。
62    Do,
63    /// `else` 关键字。
64    Else,
65    /// `elsif` 关键字。
66    Elsif,
67    /// `end` 关键字。
68    End,
69    /// `entry` 关键字。
70    Entry,
71    /// `exception` 关键字。
72    Exception,
73    /// `exit` 关键字。
74    Exit,
75    /// `for` 关键字。
76    For,
77    /// `function` 关键字。
78    Function,
79    /// `generic` 关键字。
80    Generic,
81    /// `goto` 关键字。
82    Goto,
83    /// `if` 关键字。
84    If,
85    /// `in` 关键字。
86    In,
87    /// `interface` 关键字。
88    Interface,
89    /// `is` 关键字。
90    Is,
91    /// `limited` 关键字。
92    Limited,
93    /// `loop` 关键字。
94    Loop,
95    /// `mod` 关键字。
96    Mod,
97    /// `new` 关键字。
98    New,
99    /// `not` 关键字。
100    Not,
101    /// `null` 关键字。
102    Null,
103    /// `of` 关键字。
104    Of,
105    /// `or` 关键字。
106    Or,
107    /// `others` 关键字。
108    Others,
109    /// `out` 关键字。
110    Out,
111    /// `overriding` 关键字。
112    Overriding,
113    /// `package` 关键字。
114    Package,
115    /// `pragma` 关键字。
116    Pragma,
117    /// `private` 关键字。
118    Private,
119    /// `procedure` 关键字。
120    Procedure,
121    /// `protected` 关键字。
122    Protected,
123    /// `raise` 关键字。
124    Raise,
125    /// `range` 关键字。
126    Range,
127    /// `record` 关键字。
128    Record,
129    /// `rem` 关键字。
130    Rem,
131    /// `renames` 关键字。
132    Renames,
133    /// `requeue` 关键字。
134    Requeue,
135    /// `return` 关键字。
136    Return,
137    /// `reverse` 关键字。
138    Reverse,
139    /// `select` 关键字。
140    Select,
141    /// `separate` 关键字。
142    Separate,
143    /// `some` 关键字。
144    Some,
145    /// `subtype` 关键字。
146    Subtype,
147    /// `synchronized` 关键字。
148    Synchronized,
149    /// `tagged` 关键字。
150    Tagged,
151    /// `task` 关键字。
152    Task,
153    /// `terminate` 关键字。
154    Terminate,
155    /// `then` 关键字。
156    Then,
157    /// `type` 关键字。
158    Type,
159    /// `until` 关键字。
160    Until,
161    /// `use` 关键字。
162    Use,
163    /// `when` 关键字。
164    When,
165    /// `while` 关键字。
166    While,
167    /// `with` 关键字。
168    With,
169    /// `xor` 关键字。
170    Xor,
171
172    /// 加号 (`+`)。
173    Plus,
174    /// 减号 (`-`)。
175    Minus,
176    /// 乘号 (`*`)。
177    Star,
178    /// 除号 (`/`)。
179    Slash,
180    /// 连接符 (`&`)。
181    Ampersand,
182    /// 等于 (`=`)。
183    Eq,
184    /// 不等于 (`/=`)。
185    Ne,
186    /// 小于 (`<`)。
187    Lt,
188    /// 小于等于 (`<=`)。
189    Le,
190    /// 大于 (`>`)。
191    Gt,
192    /// 大于等于 (`>=`)。
193    Ge,
194    /// 赋值 (`:=`)。
195    Assign,
196    /// 赋值别名 (`:=`)。
197    ColonEq,
198    /// 箭头 (`=>`)。
199    Arrow,
200    /// 点 (`.`)。
201    Dot,
202    /// 范围 (`..`)。
203    DotDot,
204    /// 逗号 (`,`)。
205    Comma,
206    /// 冒号 (`:`)。
207    Colon,
208    /// 分号 (`;`)。
209    Semicolon,
210    /// 竖线 (`|`)。
211    Bar,
212    /// 竖线别名 (`|`)。
213    Pipe,
214    /// 撇号 (`'`)。
215    Apostrophe,
216    /// 撇号别名 (`'`)。
217    Tick,
218    /// 左括号 (`(`)。
219    LeftParen,
220    /// 右括号 (`)`)。
221    RightParen,
222    /// 框号 (`<>`)。
223    Box,
224    /// 幂运算 (`**`)。
225    DoubleStar,
226    /// 幂运算别名 (`**`)。
227    StarStar,
228    /// 左标签分隔符 (`<<`)。
229    LtLt,
230    /// 右标签分隔符 (`>>`)。
231    GtGt,
232    /// 左方括号 (`[`)。
233    LeftBracket,
234    /// 右方括号 (`]`)。
235    RightBracket,
236    /// 左花括号 (`{`)。
237    LeftBrace,
238    /// 右花括号 (`}`)。
239    RightBrace,
240
241    /// 流结束。
242    Eof,
243    /// 错误标记。
244    Error,
245}
246
247impl AdaTokenType {
248    /// 是否为关键字
249    pub fn is_keyword(&self) -> bool {
250        matches!(
251            self,
252            Self::Abort
253                | Self::Abs
254                | Self::Abstract
255                | Self::Accept
256                | Self::Access
257                | Self::Aliased
258                | Self::All
259                | Self::And
260                | Self::Array
261                | Self::At
262                | Self::Begin
263                | Self::Body
264                | Self::Case
265                | Self::Constant
266                | Self::Declare
267                | Self::Delay
268                | Self::Delta
269                | Self::Digits
270                | Self::Do
271                | Self::Else
272                | Self::Elsif
273                | Self::End
274                | Self::Entry
275                | Self::Exception
276                | Self::Exit
277                | Self::For
278                | Self::Function
279                | Self::Generic
280                | Self::Goto
281                | Self::If
282                | Self::In
283                | Self::Interface
284                | Self::Is
285                | Self::Limited
286                | Self::Loop
287                | Self::Mod
288                | Self::New
289                | Self::Not
290                | Self::Null
291                | Self::Of
292                | Self::Or
293                | Self::Others
294                | Self::Out
295                | Self::Overriding
296                | Self::Package
297                | Self::Pragma
298                | Self::Private
299                | Self::Procedure
300                | Self::Protected
301                | Self::Raise
302                | Self::Range
303                | Self::Record
304                | Self::Rem
305                | Self::Renames
306                | Self::Requeue
307                | Self::Return
308                | Self::Reverse
309                | Self::Select
310                | Self::Separate
311                | Self::Some
312                | Self::Subtype
313                | Self::Synchronized
314                | Self::Tagged
315                | Self::Task
316                | Self::Terminate
317                | Self::Then
318                | Self::Type
319                | Self::Until
320                | Self::Use
321                | Self::When
322                | Self::While
323                | Self::With
324                | Self::Xor
325        )
326    }
327
328    /// 是否为标识符
329    pub fn is_identifier(&self) -> bool {
330        matches!(self, Self::Identifier)
331    }
332
333    /// 是否为字面量
334    pub fn is_literal(&self) -> bool {
335        matches!(self, Self::StringLiteral | Self::CharacterLiteral | Self::NumberLiteral)
336    }
337}
338
339impl TokenType for AdaTokenType {
340    type Role = UniversalTokenRole;
341    const END_OF_STREAM: Self = Self::Eof;
342
343    fn is_comment(&self) -> bool {
344        matches!(self, Self::Comment)
345    }
346
347    fn is_whitespace(&self) -> bool {
348        matches!(self, Self::Whitespace | Self::Newline)
349    }
350
351    fn is_error(&self) -> bool {
352        matches!(self, Self::Error)
353    }
354
355    fn role(&self) -> Self::Role {
356        use UniversalTokenRole::*;
357        match self {
358            _ if self.is_keyword() => Keyword,
359            Self::Identifier => Name,
360            _ if self.is_literal() => Literal,
361            Self::Comment => Comment,
362            Self::Whitespace | Self::Newline => Whitespace,
363            Self::Error => Error,
364            Self::Eof => Eof,
365            Self::Plus
366            | Self::Minus
367            | Self::Star
368            | Self::Slash
369            | Self::Ampersand
370            | Self::Eq
371            | Self::Ne
372            | Self::Lt
373            | Self::Le
374            | Self::Gt
375            | Self::Ge
376            | Self::Assign
377            | Self::ColonEq
378            | Self::Arrow
379            | Self::DoubleStar
380            | Self::StarStar
381            | Self::LtLt
382            | Self::GtGt => Operator,
383            Self::Dot
384            | Self::DotDot
385            | Self::Comma
386            | Self::Colon
387            | Self::Semicolon
388            | Self::Bar
389            | Self::Pipe
390            | Self::Apostrophe
391            | Self::Tick
392            | Self::LeftParen
393            | Self::RightParen
394            | Self::Box
395            | Self::LeftBracket
396            | Self::RightBracket
397            | Self::LeftBrace
398            | Self::RightBrace => Punctuation,
399            _ => None,
400        }
401    }
402}