Skip to main content

oak_zig/kind/
mod.rs

1use oak_core::{ElementType, TokenType, UniversalElementRole, UniversalTokenRole};
2
3#[derive(Copy, Clone, Debug, PartialEq, Eq, Hash, serde::Serialize, serde::Deserialize)]
4pub enum ZigSyntaxKind {
5    Root,
6    // 基础 kind
7    Whitespace,
8    Newline,
9    Comment,
10    DocComment,
11    Error,
12    Eof,
13
14    // 字面量
15    Identifier,
16    StringLiteral,
17    CharLiteral,
18    IntegerLiteral,
19    FloatLiteral,
20    BooleanLiteral,
21
22    // Zig 关键字 - 基本结构
23    Const,
24    Var,
25    Fn,
26    Struct,
27    Union,
28    Enum,
29    Opaque,
30    Type,
31    Comptime,
32    Inline,
33    NoInline,
34    Pub,
35    Export,
36    Extern,
37    Packed,
38    Align,
39    CallConv,
40    LinkSection,
41
42    // Zig 关键字 - 控制流
43    If,
44    Else,
45    Switch,
46    While,
47    For,
48    Break,
49    Continue,
50    Return,
51    Defer,
52    ErrDefer,
53    Unreachable,
54    NoReturn,
55
56    // Zig 关键字 - 错误处理
57    ErrorKeyword,
58
59    // Zig 关键字 - 测试和异步
60    Test,
61    Async,
62    Await,
63    Suspend,
64    Resume,
65    Cancel,
66
67    // Zig 关键字 - 内存管理
68    Undefined,
69    Null,
70    Volatile,
71    AllowZero,
72    NoAlias,
73
74    // Zig 关键字 - 其他
75    And,
76    Or,
77    AnyFrame,
78    AnyType,
79    ThreadLocal,
80
81    // 基本类型
82    Bool,
83    I8,
84    I16,
85    I32,
86    I64,
87    I128,
88    Isize,
89    U8,
90    U16,
91    U32,
92    U64,
93    U128,
94    Usize,
95    F16,
96    F32,
97    F64,
98    F80,
99    F128,
100    CShort,
101    CUshort,
102    CInt,
103    CUint,
104    CLong,
105    CUlong,
106    CLongLong,
107    CUlongLong,
108    CLongDouble,
109    CVoid,
110    Void,
111    ComptimeInt,
112    ComptimeFloat,
113
114    // 操作符
115    Plus,         // +
116    Minus,        // -
117    Star,         // *
118    Slash,        // /
119    Percent,      // %
120    StarStar,     // **
121    PlusPercent,  // +%
122    MinusPercent, // -%
123    StarPercent,  // *%
124    PlusPlus,     // ++
125    MinusMinus,   // --
126
127    // 位操作符
128    Ampersand,      // &
129    Pipe,           // |
130    Caret,          // ^
131    Tilde,          // ~
132    LessLess,       // <<
133    GreaterGreater, // >>
134
135    // 比较操作符
136    Equal,        // ==
137    NotEqual,     // !=
138    Less,         // <
139    Greater,      // >
140    LessEqual,    // <=
141    GreaterEqual, // >=
142
143    // 逻辑操作符
144    AndAnd, // and
145    OrOr,   // or
146
147    // 赋值操作符
148    Assign,               // =
149    PlusAssign,           // +=
150    MinusAssign,          // -=
151    StarAssign,           // *=
152    SlashAssign,          // /=
153    PercentAssign,        // %=
154    AmpersandAssign,      // &=
155    PipeAssign,           // |=
156    CaretAssign,          // ^=
157    LessLessAssign,       // <<=
158    GreaterGreaterAssign, // >>=
159
160    // 标点符号
161    LeftParen,    // (
162    RightParen,   // )
163    LeftBrace,    // {
164    RightBrace,   // }
165    LeftBracket,  // [
166    RightBracket, // ]
167    Semicolon,    // ;
168    Comma,        // ,
169    Dot,          // .
170    DotDot,       // ..
171    DotDotDot,    // ...
172    DotQuestion,  // .?
173    DotStar,      // .*
174    Colon,        // :
175    Question,     // ?
176    Exclamation,  // !
177    Arrow,        // ->
178    FatArrow,     // =>
179
180    // 特殊操作符
181    OrElse,       // orelse
182    CatchKeyword, // catch
183    TryKeyword,   // try
184    AwaitKeyword, // await
185
186    // 内置函数前缀
187    At, // @
188    BuiltinIdentifier,
189
190    // 字符串插值
191    StringStart,
192    StringEnd,
193    StringContent,
194    InterpolationStart,
195    InterpolationEnd,
196
197    // 多行字符串
198    MultilineStringStart,
199    MultilineStringEnd,
200    MultilineStringContent,
201
202    // 编译时指令
203    CompileDirective,
204
205    // 其他
206    Text,
207
208    // 非终结符
209    VarDeclaration,
210    FnDeclaration,
211    StructDeclaration,
212    EnumDeclaration,
213    UnionDeclaration,
214    Block,
215    IfStatement,
216    WhileStatement,
217    ForStatement,
218    ReturnStatement,
219}
220
221impl TokenType for ZigSyntaxKind {
222    type Role = UniversalTokenRole;
223    const END_OF_STREAM: Self = Self::Eof;
224
225    fn role(&self) -> Self::Role {
226        match self {
227            Self::Const
228            | Self::Var
229            | Self::Fn
230            | Self::Struct
231            | Self::Union
232            | Self::Enum
233            | Self::Opaque
234            | Self::Type
235            | Self::Comptime
236            | Self::Inline
237            | Self::NoInline
238            | Self::Pub
239            | Self::Export
240            | Self::Extern
241            | Self::Packed
242            | Self::Align
243            | Self::CallConv
244            | Self::LinkSection
245            | Self::If
246            | Self::Else
247            | Self::Switch
248            | Self::While
249            | Self::For
250            | Self::Break
251            | Self::Continue
252            | Self::Return
253            | Self::Defer
254            | Self::ErrDefer
255            | Self::Unreachable
256            | Self::NoReturn
257            | Self::ErrorKeyword
258            | Self::Test
259            | Self::Async
260            | Self::Await
261            | Self::Suspend
262            | Self::Resume
263            | Self::Cancel
264            | Self::Undefined
265            | Self::Null
266            | Self::Volatile
267            | Self::AllowZero
268            | Self::NoAlias
269            | Self::And
270            | Self::Or
271            | Self::AnyFrame
272            | Self::AnyType
273            | Self::ThreadLocal
274            | Self::OrElse
275            | Self::CatchKeyword
276            | Self::TryKeyword
277            | Self::AwaitKeyword => UniversalTokenRole::Keyword,
278
279            Self::Bool
280            | Self::I8
281            | Self::I16
282            | Self::I32
283            | Self::I64
284            | Self::I128
285            | Self::Isize
286            | Self::U8
287            | Self::U16
288            | Self::U32
289            | Self::U64
290            | Self::U128
291            | Self::Usize
292            | Self::F16
293            | Self::F32
294            | Self::F64
295            | Self::F80
296            | Self::F128
297            | Self::CShort
298            | Self::CUshort
299            | Self::CInt
300            | Self::CUint
301            | Self::CLong
302            | Self::CUlong
303            | Self::CLongLong
304            | Self::CUlongLong
305            | Self::CLongDouble
306            | Self::CVoid
307            | Self::Void
308            | Self::ComptimeInt
309            | Self::ComptimeFloat => UniversalTokenRole::Keyword,
310
311            Self::Identifier => UniversalTokenRole::Name,
312
313            Self::StringLiteral | Self::CharLiteral | Self::IntegerLiteral | Self::FloatLiteral | Self::BooleanLiteral => UniversalTokenRole::Literal,
314
315            Self::Plus
316            | Self::Minus
317            | Self::Star
318            | Self::Slash
319            | Self::Percent
320            | Self::StarStar
321            | Self::PlusPercent
322            | Self::MinusPercent
323            | Self::StarPercent
324            | Self::PlusPlus
325            | Self::Ampersand
326            | Self::Pipe
327            | Self::Caret
328            | Self::Tilde
329            | Self::LessLess
330            | Self::GreaterGreater
331            | Self::Equal
332            | Self::NotEqual
333            | Self::Less
334            | Self::Greater
335            | Self::LessEqual
336            | Self::GreaterEqual
337            | Self::AndAnd
338            | Self::OrOr
339            | Self::Assign
340            | Self::PlusAssign
341            | Self::MinusAssign
342            | Self::StarAssign
343            | Self::SlashAssign
344            | Self::PercentAssign
345            | Self::AmpersandAssign
346            | Self::PipeAssign
347            | Self::CaretAssign
348            | Self::LessLessAssign
349            | Self::GreaterGreaterAssign
350            | Self::At => UniversalTokenRole::Operator,
351
352            Self::LeftParen
353            | Self::RightParen
354            | Self::LeftBrace
355            | Self::RightBrace
356            | Self::LeftBracket
357            | Self::RightBracket
358            | Self::Semicolon
359            | Self::Comma
360            | Self::Dot
361            | Self::DotDot
362            | Self::DotDotDot
363            | Self::Colon
364            | Self::Question
365            | Self::Exclamation
366            | Self::Arrow
367            | Self::FatArrow => UniversalTokenRole::Punctuation,
368
369            Self::Whitespace | Self::Newline => UniversalTokenRole::Whitespace,
370            Self::Comment | Self::DocComment => UniversalTokenRole::Comment,
371            Self::Error => UniversalTokenRole::Error,
372            _ => UniversalTokenRole::None,
373        }
374    }
375
376    fn is_ignored(&self) -> bool {
377        matches!(self, Self::Whitespace | Self::Newline | Self::Comment | Self::DocComment)
378    }
379
380    fn is_comment(&self) -> bool {
381        matches!(self, Self::Comment | Self::DocComment)
382    }
383
384    fn is_whitespace(&self) -> bool {
385        matches!(self, Self::Whitespace | Self::Newline)
386    }
387}
388
389impl ElementType for ZigSyntaxKind {
390    type Role = UniversalElementRole;
391
392    fn role(&self) -> Self::Role {
393        match self {
394            Self::Root => UniversalElementRole::Root,
395            Self::FnDeclaration | Self::StructDeclaration | Self::EnumDeclaration | Self::UnionDeclaration => UniversalElementRole::Definition,
396            Self::VarDeclaration | Self::IfStatement | Self::WhileStatement | Self::ForStatement | Self::ReturnStatement => UniversalElementRole::Statement,
397            Self::Block => UniversalElementRole::Container,
398            Self::Error => UniversalElementRole::Error,
399            _ => UniversalElementRole::None,
400        }
401    }
402}