oak_rust/kind/
mod.rs

1use oak_core::{SyntaxKind, Token};
2use serde::{Deserialize, Serialize};
3
4/// Type alias for Token with RustSyntaxKind
5pub type RustToken = Token<RustSyntaxKind>;
6
7/// Represents all possible kind kinds in the Rust language.
8///
9/// This enum includes both terminal tokens (keywords, identifiers, literals, operators, etc.)
10/// and non-terminal kind nodes (expressions, statements, declarations, etc.).
11#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
12pub enum RustSyntaxKind {
13    /// The `as` keyword for type casting
14    As,
15    /// The `async` keyword for async functions or blocks
16    Async,
17    /// The `await` keyword for awaiting futures
18    Await,
19    /// The `break` keyword for breaking out of loops
20    Break,
21    /// The `const` keyword for constant definitions
22    Const,
23    /// The `continue` keyword for continuing to the next loop iteration
24    Continue,
25    /// The `crate` keyword for referring to the crate root
26    Crate,
27    /// The `dyn` keyword for dynamic trait objects
28    Dyn,
29    /// The `else` keyword for else branches in conditionals
30    Else,
31    /// The `enum` keyword for enum definitions
32    Enum,
33    /// The `extern` keyword for external function/block declarations
34    Extern,
35    /// The `false` boolean literal
36    False,
37    /// The `fn` keyword for function definitions
38    Fn,
39    /// The `for` keyword for loops
40    For,
41    /// The `if` keyword for conditional expressions
42    If,
43    /// The `impl` keyword for implementation blocks
44    Impl,
45    /// The `in` keyword for loops and patterns
46    In,
47    /// The `let` keyword for variable bindings
48    Let,
49    /// The `loop` keyword for infinite loops
50    Loop,
51    /// The `match` keyword for pattern matching
52    Match,
53    /// The `mod` keyword for module definitions
54    Mod,
55    /// The `move` keyword for closures
56    Move,
57    /// The `mut` keyword for mutable bindings/references
58    Mut,
59    /// The `pub` keyword for visibility
60    Pub,
61    /// The `ref` keyword for pattern bindings
62    Ref,
63    /// The `return` keyword for returning from functions
64    Return,
65    /// The `self` keyword for the current value
66    SelfValue,
67    /// The `Self` keyword for the implementing type
68    SelfType,
69    /// The `static` keyword for static variables
70    Static,
71    /// The `struct` keyword for struct definitions
72    Struct,
73    /// The `super` keyword for parent modules
74    Super,
75    /// The `trait` keyword for trait definitions
76    Trait,
77    /// The `true` boolean literal
78    True,
79    /// The `type` keyword for type aliases
80    Type,
81    /// The `unsafe` keyword for unsafe blocks/functions
82    Unsafe,
83    /// The `use` keyword for importing items
84    Use,
85    /// The `where` keyword for where clauses
86    Where,
87    /// The `while` keyword for while loops
88    While,
89    /// An identifier (variable name, function name, etc.)
90    Identifier,
91    /// An integer literal (e.g., `42`, `0xFF`, `0o755`, `0b1010`)
92    IntegerLiteral,
93    /// A floating-point literal (e.g., `3.14`, `2.0e10`)
94    FloatLiteral,
95    /// A string literal (e.g., `"hello"`)
96    StringLiteral,
97    /// A character literal (e.g., `'a'`)
98    CharLiteral,
99    /// A lifetime annotation (e.g., `'a`)
100    Lifetime,
101    /// Addition operator: `+`
102    Plus,
103    /// Subtraction operator: `-`
104    Minus,
105    /// Multiplication operator: `*`
106    Star,
107    /// Division operator: `/`
108    Slash,
109    /// Modulo operator: `%`
110    Percent,
111    /// Bitwise XOR operator: `^`
112    Caret,
113    /// Logical NOT/bitwise NOT operator: `!`
114    Not,
115    /// Bitwise AND operator: `&`
116    And,
117    /// Bitwise OR operator: `|`
118    Or,
119    /// Logical AND operator: `&&`
120    AndAnd,
121    /// Logical OR operator: `||`
122    OrOr,
123    /// Left shift operator: `<<`
124    Shl,
125    /// Right shift operator: `>>`
126    Shr,
127    /// Addition assignment operator: `+=`
128    PlusEq,
129    /// Subtraction assignment operator: `-=`
130    MinusEq,
131    /// Multiplication assignment operator: `*=`
132    StarEq,
133    /// Division assignment operator: `/=`
134    SlashEq,
135    /// Modulo assignment operator: `%=`
136    PercentEq,
137    /// Bitwise XOR assignment operator: `^=`
138    CaretEq,
139    /// Bitwise AND assignment operator: `&=`
140    AndEq,
141    /// Bitwise OR assignment operator: `|=`
142    OrEq,
143    /// Left shift assignment operator: `<<=`
144    ShlEq,
145    /// Right shift assignment operator: `>>=`
146    ShrEq,
147    /// Assignment operator: `=`
148    Eq,
149    /// Equality comparison operator: `==`
150    EqEq,
151    /// Inequality comparison operator: `!=`
152    Ne,
153    /// Greater-than operator: `>`
154    Gt,
155    /// Less-than operator: `<`
156    Lt,
157    /// Greater-than-or-equal operator: `>=`
158    Ge,
159    /// Less-than-or-equal operator: `<=`
160    Le,
161    /// At symbol: `@`
162    At,
163    /// Underscore: `_`
164    Underscore,
165    /// Dot: `.`
166    Dot,
167    /// Range operator: `..`
168    DotDot,
169    /// Inclusive range operator: `...`
170    DotDotDot,
171    /// Range-to operator: `..=`
172    DotDotEq,
173    /// Comma: `,`
174    Comma,
175    /// Semicolon: `;`
176    Semicolon,
177    /// Colon: `:`
178    Colon,
179    /// Path separator: `::`
180    PathSep,
181    /// Right arrow: `->`
182    RArrow,
183    /// Fat arrow: `=>`
184    FatArrow,
185    /// Pound sign: `#`
186    Pound,
187    /// Dollar sign: `$`
188    Dollar,
189    /// Question mark: `?`
190    Question,
191    /// Left parenthesis: `(`
192    LeftParen,
193    /// Right parenthesis: `)`
194    RightParen,
195    /// Left brace: `{`
196    LeftBrace,
197    /// Right brace: `}`
198    RightBrace,
199    /// Left bracket: `[`
200    LeftBracket,
201    /// Right bracket: `]`
202    RightBracket,
203    /// Whitespace characters
204    Whitespace,
205    /// Comments (both line and block comments)
206    Comment,
207    /// The root of a source file
208    SourceFile,
209    /// A function definition
210    Function,
211    /// A list of parameters in a function signature
212    ParameterList,
213    /// A single parameter in a function signature
214    Parameter,
215    /// A block expression `{ ... }`
216    BlockExpression,
217    /// A let statement `let x = 5;`
218    LetStatement,
219    /// An expression statement
220    ExpressionStatement,
221    /// An identifier expression
222    IdentifierExpression,
223    /// A literal expression
224    LiteralExpression,
225    /// A boolean literal expression
226    BooleanLiteral,
227    /// A parenthesized expression `(expr)`
228    ParenthesizedExpression,
229    /// A binary expression `a + b`
230    BinaryExpression,
231    /// A unary expression `!x` or `-x`
232    UnaryExpression,
233    /// A function call expression `func(arg)`
234    CallExpression,
235    /// A field access expression `obj.field`
236    FieldExpression,
237    /// An index expression `arr[index]`
238    IndexExpression,
239    /// An if expression `if cond { ... } else { ... }`
240    IfExpression,
241    /// A match expression `match value { ... }`
242    MatchExpression,
243    /// A loop expression `loop { ... }`
244    LoopExpression,
245    /// A while expression `while cond { ... }`
246    WhileExpression,
247    /// A for expression `for pat in iter { ... }`
248    ForExpression,
249    /// A break expression `break value?`
250    BreakExpression,
251    /// A continue expression `continue`
252    ContinueExpression,
253    /// A return expression `return value?`
254    ReturnExpression,
255    /// A struct expression `Struct { field: value }`
256    StructExpression,
257    /// A tuple expression `(a, b, c)`
258    TupleExpression,
259    /// An array expression `[a, b, c]`
260    ArrayExpression,
261    /// A range expression `start..end`
262    RangeExpression,
263    /// A closure expression `|args| body`
264    ClosureExpression,
265    /// An async block expression `async { ... }`
266    AsyncBlockExpression,
267    /// An unsafe block expression `unsafe { ... }`
268    UnsafeBlockExpression,
269    /// A try expression `expr?`
270    TryExpression,
271    /// An await expression `expr.await`
272    AwaitExpression,
273    /// A macro call `macro!(args)`
274    MacroCall,
275    /// A path `module::item`
276    Path,
277    /// A segment in a path
278    PathSegment,
279    /// Generic arguments `<T, U>`
280    GenericArgs,
281    /// A type path
282    TypePath,
283    /// A tuple type `(T, U)`
284    TupleType,
285    /// An array type `[T; N]`
286    ArrayType,
287    /// A slice type `[T]`
288    SliceType,
289    /// A reference type `&T` or `&mut T`
290    ReferenceType,
291    /// A raw pointer type `*const T` or `*mut T`
292    PointerType,
293    /// A function type `fn(T) -> U`
294    FunctionType,
295    /// A trait object type `dyn Trait`
296    TraitObjectType,
297    /// An impl trait type `impl Trait`
298    ImplTraitType,
299    /// An inferred type `_`
300    InferredType,
301    /// The never type `!`
302    NeverType,
303    /// A pattern in match arms or function parameters
304    Pattern,
305    /// An identifier pattern
306    IdentifierPattern,
307    /// A wildcard pattern `_`
308    WildcardPattern,
309    /// A tuple pattern `(a, b)`
310    TuplePattern,
311    /// A struct pattern `Struct { field: pattern }`
312    StructPattern,
313    /// A tuple struct pattern `Tuple(a, b)`
314    TupleStructPattern,
315    /// A slice pattern `[a, b, ..]`
316    SlicePattern,
317    /// A reference pattern `&pattern`
318    ReferencePattern,
319    /// A literal pattern `42` or `"hello"`
320    LiteralPattern,
321    /// A range pattern `start..end`
322    RangePattern,
323    /// An or pattern `pat1 | pat2`
324    OrPattern,
325    /// A rest pattern `..`
326    RestPattern,
327    /// A struct declaration
328    StructDeclaration,
329    /// An enum declaration
330    EnumDeclaration,
331    /// A union declaration
332    UnionDeclaration,
333    /// A trait declaration
334    TraitDeclaration,
335    /// An impl declaration
336    ImplDeclaration,
337    /// A module declaration
338    ModuleDeclaration,
339    /// A use declaration
340    UseDeclaration,
341    /// A const declaration
342    ConstDeclaration,
343    /// A static declaration
344    StaticDeclaration,
345    /// A type alias declaration
346    TypeAliasDeclaration,
347    /// An extern block
348    ExternBlock,
349    /// An extern function declaration
350    ExternFunction,
351    /// An attribute `#[attr]` or `#![attr]`
352    Attribute,
353    /// A visibility specifier `pub`, `pub(crate)`, etc.
354    Visibility,
355    /// Generic parameters `<T: Trait>`
356    GenericParams,
357    /// A single generic parameter
358    GenericParam,
359    /// A type parameter
360    TypeParam,
361    /// A const parameter
362    ConstParam,
363    /// A lifetime parameter
364    LifetimeParam,
365    /// A where clause
366    WhereClause,
367    /// A single where predicate
368    WherePredicate,
369    /// A return type in a function signature
370    ReturnType,
371    /// A list of fields in a struct or enum variant
372    FieldList,
373    /// A single field in a struct or enum variant
374    Field,
375    /// An enum variant
376    Variant,
377    /// A list of enum variants
378    VariantList,
379    /// An associated item in a trait or impl
380    AssociatedItem,
381    /// An item in a trait
382    TraitItem,
383    /// An item in an impl
384    ImplItem,
385    /// End of file marker
386    Eof,
387    /// Represents a kind error
388    Error,
389}
390
391impl SyntaxKind for RustSyntaxKind {
392    fn is_trivia(&self) -> bool {
393        matches!(self, Self::Whitespace | Self::Comment)
394    }
395
396    fn is_comment(&self) -> bool {
397        matches!(self, Self::Comment)
398    }
399
400    fn is_whitespace(&self) -> bool {
401        matches!(self, Self::Whitespace)
402    }
403
404    fn is_token_type(&self) -> bool {
405        use RustSyntaxKind::*;
406        matches!(self, Error | Eof)
407    }
408
409    fn is_element_type(&self) -> bool {
410        use RustSyntaxKind::*;
411        matches!(
412            self,
413            Error | Eof |
414            // 语法节点类型 (非终结符)
415            SourceFile | Function | ParameterList | Parameter | BlockExpression |
416            LetStatement | ExpressionStatement | IdentifierExpression | LiteralExpression |
417            BooleanLiteral | ParenthesizedExpression | BinaryExpression | UnaryExpression |
418            CallExpression | FieldExpression | IndexExpression | IfExpression |
419            MatchExpression | LoopExpression | WhileExpression | ForExpression |
420            BreakExpression | ContinueExpression | ReturnExpression | StructExpression |
421            TupleExpression | ArrayExpression | RangeExpression | ClosureExpression |
422            AsyncBlockExpression | UnsafeBlockExpression | TryExpression | AwaitExpression |
423            MacroCall | Path | PathSegment | GenericArgs | TypePath | TupleType |
424            ArrayType | SliceType | ReferenceType | PointerType | FunctionType |
425            TraitObjectType | ImplTraitType | InferredType | NeverType | Pattern |
426            IdentifierPattern | WildcardPattern | TuplePattern | StructPattern |
427            TupleStructPattern | SlicePattern | ReferencePattern | LiteralPattern |
428            RangePattern | OrPattern | RestPattern | StructDeclaration | EnumDeclaration | UnionDeclaration |
429             TraitDeclaration | ImplDeclaration | ModuleDeclaration | UseDeclaration | ConstDeclaration |
430             StaticDeclaration | TypeAliasDeclaration | ExternBlock | ExternFunction |
431            Attribute | Visibility | GenericParams | GenericParam | TypeParam |
432            ConstParam | LifetimeParam | WhereClause | WherePredicate |
433            ReturnType | FieldList | Field | Variant | VariantList |
434            AssociatedItem | TraitItem | ImplItem
435        )
436    }
437}