Skip to main content

oak_ada/lexer/
token_type.rs

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