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    Whitespace,
10    Newline,
11    Comment,
12
13    StringLiteral,
14    CharacterLiteral,
15    NumberLiteral,
16    Identifier,
17
18    Abort,
19    Abs,
20    Abstract,
21    Accept,
22    Access,
23    Aliased,
24    All,
25    And,
26    Array,
27    At,
28    Begin,
29    Body,
30    Case,
31    Constant,
32    Declare,
33    Delay,
34    Delta,
35    Digits,
36    Do,
37    Else,
38    Elsif,
39    End,
40    Entry,
41    Exception,
42    Exit,
43    For,
44    Function,
45    Generic,
46    Goto,
47    If,
48    In,
49    Interface,
50    Is,
51    Limited,
52    Loop,
53    Mod,
54    New,
55    Not,
56    Null,
57    Of,
58    Or,
59    Others,
60    Out,
61    Overriding,
62    Package,
63    Pragma,
64    Private,
65    Procedure,
66    Protected,
67    Raise,
68    Range,
69    Record,
70    Rem,
71    Renames,
72    Requeue,
73    Return,
74    Reverse,
75    Select,
76    Separate,
77    Some,
78    Subtype,
79    Synchronized,
80    Tagged,
81    Task,
82    Terminate,
83    Then,
84    Type,
85    Until,
86    Use,
87    When,
88    While,
89    With,
90    Xor,
91
92    // 符号
93    Plus,         // +
94    Minus,        // -
95    Star,         // *
96    Slash,        // /
97    Ampersand,    // &
98    Eq,           // =
99    Ne,           // /=
100    Lt,           // <
101    Le,           // <=
102    Gt,           // >
103    Ge,           // >=
104    Assign,       // :=
105    ColonEq,      // := (alias)
106    Arrow,        // =>
107    Dot,          // .
108    DotDot,       // ..
109    Comma,        // ,
110    Colon,        // :
111    Semicolon,    // ;
112    Bar,          // |
113    Pipe,         // | (alias)
114    Apostrophe,   // '
115    Tick,         // ' (alias)
116    LeftParen,    // (
117    RightParen,   // )
118    Box,          // <>
119    DoubleStar,   // **
120    StarStar,     // ** (alias)
121    LtLt,         // <<
122    GtGt,         // >>
123    LeftBracket,  // [
124    RightBracket, // ]
125    LeftBrace,    // {
126    RightBrace,   // }
127
128    Eof,
129    Error,
130}
131
132impl AdaTokenType {
133    /// 是否为关键字
134    pub fn is_keyword(&self) -> bool {
135        matches!(
136            self,
137            Self::Abort
138                | Self::Abs
139                | Self::Abstract
140                | Self::Accept
141                | Self::Access
142                | Self::Aliased
143                | Self::All
144                | Self::And
145                | Self::Array
146                | Self::At
147                | Self::Begin
148                | Self::Body
149                | Self::Case
150                | Self::Constant
151                | Self::Declare
152                | Self::Delay
153                | Self::Delta
154                | Self::Digits
155                | Self::Do
156                | Self::Else
157                | Self::Elsif
158                | Self::End
159                | Self::Entry
160                | Self::Exception
161                | Self::Exit
162                | Self::For
163                | Self::Function
164                | Self::Generic
165                | Self::Goto
166                | Self::If
167                | Self::In
168                | Self::Interface
169                | Self::Is
170                | Self::Limited
171                | Self::Loop
172                | Self::Mod
173                | Self::New
174                | Self::Not
175                | Self::Null
176                | Self::Of
177                | Self::Or
178                | Self::Others
179                | Self::Out
180                | Self::Overriding
181                | Self::Package
182                | Self::Pragma
183                | Self::Private
184                | Self::Procedure
185                | Self::Protected
186                | Self::Raise
187                | Self::Range
188                | Self::Record
189                | Self::Rem
190                | Self::Renames
191                | Self::Requeue
192                | Self::Return
193                | Self::Reverse
194                | Self::Select
195                | Self::Separate
196                | Self::Some
197                | Self::Subtype
198                | Self::Synchronized
199                | Self::Tagged
200                | Self::Task
201                | Self::Terminate
202                | Self::Then
203                | Self::Type
204                | Self::Until
205                | Self::Use
206                | Self::When
207                | Self::While
208                | Self::With
209                | Self::Xor
210        )
211    }
212
213    /// 是否为标识符
214    pub fn is_identifier(&self) -> bool {
215        matches!(self, Self::Identifier)
216    }
217
218    /// 是否为字面量
219    pub fn is_literal(&self) -> bool {
220        matches!(self, Self::StringLiteral | Self::CharacterLiteral | Self::NumberLiteral)
221    }
222}
223
224impl TokenType for AdaTokenType {
225    type Role = UniversalTokenRole;
226    const END_OF_STREAM: Self = Self::Eof;
227
228    fn is_comment(&self) -> bool {
229        matches!(self, Self::Comment)
230    }
231
232    fn is_whitespace(&self) -> bool {
233        matches!(self, Self::Whitespace | Self::Newline)
234    }
235
236    fn is_error(&self) -> bool {
237        matches!(self, Self::Error)
238    }
239
240    fn role(&self) -> Self::Role {
241        use UniversalTokenRole::*;
242        match self {
243            _ if self.is_keyword() => Keyword,
244            Self::Identifier => Name,
245            _ if self.is_literal() => Literal,
246            Self::Comment => Comment,
247            Self::Whitespace | Self::Newline => Whitespace,
248            Self::Error => Error,
249            Self::Eof => Eof,
250            Self::Plus
251            | Self::Minus
252            | Self::Star
253            | Self::Slash
254            | Self::Ampersand
255            | Self::Eq
256            | Self::Ne
257            | Self::Lt
258            | Self::Le
259            | Self::Gt
260            | Self::Ge
261            | Self::Assign
262            | Self::ColonEq
263            | Self::Arrow
264            | Self::DoubleStar
265            | Self::StarStar
266            | Self::LtLt
267            | Self::GtGt => Operator,
268            Self::Dot
269            | Self::DotDot
270            | Self::Comma
271            | Self::Colon
272            | Self::Semicolon
273            | Self::Bar
274            | Self::Pipe
275            | Self::Apostrophe
276            | Self::Tick
277            | Self::LeftParen
278            | Self::RightParen
279            | Self::Box
280            | Self::LeftBracket
281            | Self::RightBracket
282            | Self::LeftBrace
283            | Self::RightBrace => Punctuation,
284            _ => None,
285        }
286    }
287}