oak_rust/parser/element_type.rs
1use crate::lexer::RustTokenType;
2use oak_core::{ElementType, GreenNode, UniversalElementRole};
3use serde::{Deserialize, Serialize};
4use std::sync::Arc;
5
6/// Rust 语法树元素的类型别名
7pub type RustElement<'a> = Arc<GreenNode<'a, RustElementType>>;
8
9/// Rust 语法树中所有可能的元素类型。
10#[derive(Copy, Clone, Debug, PartialEq, Eq, Hash, Serialize, Deserialize)]
11#[allow(missing_docs)]
12pub enum RustElementType {
13 /// The root element of any Rust source file
14 /// ```rust
15 /// // Entire file content
16 /// fn main() {}
17 /// ```
18 SourceFile,
19
20 /// A function definition
21 /// ```rust
22 /// fn add(a: i32, b: i32) -> i32 {
23 /// a + b
24 /// }
25 /// ```
26 Function,
27
28 /// A trait definition
29 /// ```rust
30 /// trait Display {
31 /// fn fmt(&self) -> String;
32 /// }
33 /// ```
34 Trait,
35
36 /// An impl block
37 /// ```rust,ignore
38 /// impl Display for Point {
39 /// fn fmt(&self, f: &mut Formatter) -> Result { /* ... */ }
40 /// }
41 /// ```
42 Impl,
43
44 /// A use statement
45 /// ```rust
46 /// use std::collections::HashMap;
47 /// ```
48 UseItem,
49
50 /// A static variable
51 /// ```rust
52 /// static COUNT: i32 = 0;
53 /// ```
54 Static,
55
56 /// A constant
57 /// ```rust
58 /// const MAX_SIZE: usize = 100;
59 /// ```
60 Const,
61
62 /// A type alias
63 /// ```rust
64 /// type MyResult = Result<i32, String>;
65 /// ```
66 TypeAlias,
67
68 /// A macro invocation or definition
69 /// ```rust,ignore
70 /// println!("Hello, {}!", name);
71 /// vec![1, 2, 3]
72 /// ```
73 Macro,
74
75 /// An extern crate declaration
76 /// ```rust
77 /// extern crate serde;
78 /// ```
79 ExternCrate,
80
81 /// An extern block
82 /// ```rust,ignore
83 /// extern "C" {
84 /// fn call_c_function();
85 /// }
86 /// ```
87 ExternBlock,
88
89 /// A module item
90 /// ```rust,ignore
91 /// mod my_module;
92 /// mod my_module { /* content */ }
93 /// ```
94 ModuleItem,
95
96 /// A struct definition
97 /// ```rust
98 /// struct Point {
99 /// x: f64,
100 /// y: f64,
101 /// }
102 /// ```
103 StructItem,
104
105 /// An enum definition
106 /// ```rust
107 /// enum Color {
108 /// Red,
109 /// Green,
110 /// Blue,
111 /// }
112 /// ```
113 EnumItem,
114
115 /// A return statement
116 /// ```rust,ignore
117 /// return 42;
118 /// ```
119 ReturnStatement,
120
121 /// A parameter list in function definitions
122 /// ```rust,ignore
123 /// fn example(a: i32, b: String) // (a: i32, b: String)
124 /// ```
125 ParameterList,
126
127 /// A single parameter in a function
128 /// ```rust,ignore
129 /// fn example(x: i32) // x: i32
130 /// ```
131 Parameter,
132
133 /// A return type annotation
134 /// ```rust,ignore
135 /// fn example() -> i32 // -> i32
136 /// ```
137 ReturnType,
138
139 /// A let statement
140 /// ```rust,ignore
141 /// let x = 42;
142 /// let mut y: String = "hello".to_string();
143 /// ```
144 LetStatement,
145
146 /// An expression used as a statement
147 /// ```rust,ignore
148 /// x + 1; // Expression statement
149 /// ```
150 ExpressionStatement,
151
152 /// A block expression
153 /// ```rust,ignore
154 /// {
155 /// let x = 1;
156 /// x + 2
157 /// }
158 /// ```
159 BlockExpression,
160
161 /// An if expression
162 /// ```rust,ignore
163 /// if x > 0 { "positive" } else { "non-positive" }
164 /// ```
165 IfExpression,
166
167 /// A match expression
168 /// ```rust,ignore
169 /// match x {
170 /// 1 => "one",
171 /// 2 => "two",
172 /// _ => "other",
173 /// }
174 /// ```
175 MatchExpression,
176
177 /// A loop expression
178 /// ```rust,ignore
179 /// loop {
180 /// println!("infinite loop");
181 /// }
182 /// ```
183 LoopExpression,
184
185 /// A while loop
186 /// ```rust,ignore
187 /// while x < 10 {
188 /// x += 1;
189 /// }
190 /// ```
191 WhileExpression,
192
193 /// A for loop
194 /// ```rust,ignore
195 /// for item in collection {
196 /// println!("{}", item);
197 /// }
198 /// ```
199 ForExpression,
200
201 /// A variable or identifier expression
202 /// ```rust,ignore
203 /// let name = x; // x is an identifier expression
204 /// ```
205 IdentifierExpression,
206
207 /// A literal expression (any literal value)
208 /// ```rust,ignore
209 /// let x = 42; // 42 is a literal expression
210 /// ```
211 LiteralExpression,
212
213 /// A path expression
214 /// ```rust,ignore
215 /// std::collections::HashMap::new()
216 /// ```
217 PathExpression,
218
219 /// An integer literal
220 /// ```rust,ignore
221 /// 42, -10, 0xFF, 0b1010
222 /// ```
223 IntegerLiteral,
224
225 /// A floating-point literal
226 /// ```rust,ignore
227 /// 3.14, -2.5, 1e10
228 /// ```
229 FloatLiteral,
230
231 /// A string literal
232 /// ```rust,ignore
233 /// "hello", "world\n", r#"raw string"#
234 /// ```
235 StringLiteral,
236
237 /// A character literal
238 /// ```rust,ignore
239 /// 'a', '\n', '🦀'
240 /// ```
241 CharLiteral,
242
243 /// A boolean literal
244 /// ```text
245 /// true, false
246 /// ```
247 BooleanLiteral,
248
249 /// A parenthesized expression
250 /// ```rust,ignore
251 /// (x + 1) * 2
252 /// ```
253 ParenthesizedExpression,
254
255 /// A tuple expression
256 /// ```rust,ignore
257 /// (1, "hello", 3.14)
258 /// ```
259 TupleExpression,
260
261 /// An array expression
262 /// ```rust,ignore
263 /// [1, 2, 3, 4, 5]
264 /// ```
265 ArrayExpression,
266
267 /// A struct expression (struct instantiation)
268 /// ```rust,ignore
269 /// Point { x: 1.0, y: 2.0 }
270 /// ```
271 StructExpression,
272
273 /// An assignment expression
274 /// ```rust,ignore
275 /// x = 42
276 /// ```
277 AssignmentExpression,
278
279 /// A function call expression
280 /// ```rust,ignore
281 /// println!("Hello!")
282 /// ```
283 CallExpression,
284
285 /// A method call expression
286 /// ```rust,ignore
287 /// string.trim().to_uppercase()
288 /// ```
289 MethodCallExpression,
290
291 /// A field access expression
292 /// ```rust,ignore
293 /// point.x, user.name
294 /// ```
295 FieldExpression,
296
297 /// An index expression
298 /// ```rust,ignore
299 /// array[0], vector[1..3]
300 /// ```
301 IndexExpression,
302
303 /// A return expression
304 /// ```rust,ignore
305 /// return 42;
306 /// ```
307 ReturnExpression,
308
309 /// A break expression
310 /// ```rust,ignore
311 /// break;
312 /// break 42;
313 /// ```
314 BreakExpression,
315
316 /// A continue expression
317 /// ```rust,ignore
318 /// continue;
319 /// ```
320 ContinueExpression,
321
322 /// A pattern (used in match, let, etc.)
323 /// ```rust,ignore
324 /// Some(x), Ok(value), Point { x, y }
325 /// ```
326 Pattern,
327
328 /// A match arm
329 /// ```rust,ignore
330 /// 1 => "one", // pattern => expression
331 /// ```
332 MatchArm,
333
334 /// A type annotation
335 /// ```rust,ignore
336 /// i32, String, Vec<T>
337 /// ```
338 Type,
339
340 /// A path type
341 /// ```rust,ignore
342 /// std::collections::HashMap<K, V>
343 /// ```
344 PathType,
345
346 /// A tuple type
347 /// ```rust,ignore
348 /// (i32, String)
349 /// ```
350 TupleType,
351
352 /// An array type
353 /// ```rust,ignore
354 /// [i32; 10]
355 /// ```
356 ArrayType,
357
358 /// A slice type
359 /// ```rust,ignore
360 /// [i32], str
361 /// ```
362 SliceType,
363
364 /// A reference type
365 /// ```rust,ignore
366 /// &str, &mut i32
367 /// ```
368 ReferenceType,
369
370 /// A raw pointer type
371 /// ```rust,ignore
372 /// *const i32, *mut String
373 /// ```
374 PointerType,
375
376 /// A function type
377 /// ```rust,ignore
378 /// fn(i32) -> String
379 /// ```
380 FunctionType,
381
382 /// Generic parameters
383 /// ```rust,ignore
384 /// fn example<T, U>(x: T) -> U
385 /// ^^^^^^ generic parameters
386 /// ```
387 GenericParams,
388
389 /// Generic arguments
390 /// ```rust,ignore
391 /// Vec<i32>, Option<String>
392 /// ^^^ generic arguments
393 /// ```
394 GenericArgs,
395
396 /// A where clause
397 /// ```rust,ignore
398 /// where T: Display + Clone, U: Debug
399 /// ```
400 WhereClause,
401
402 /// An attribute
403 /// ```rust,ignore
404 /// #[derive(Debug)]
405 /// #[inline]
406 /// ```
407 Attribute,
408
409 /// A visibility modifier
410 /// ```rust,ignore
411 /// pub, pub(crate), private (no modifier)
412 /// ```
413 Visibility,
414
415 /// The async keyword
416 /// ```rust,ignore
417 /// async fn example() {}
418 /// ```
419 Async,
420
421 /// The unsafe keyword
422 /// ```rust,ignore
423 /// unsafe fn example() {}
424 /// ```
425 Unsafe,
426
427 /// The extern keyword
428 /// ```rust,ignore
429 /// extern "C" fn example();
430 /// ```
431 Extern,
432
433 /// An identifier
434 /// ```rust,ignore
435 /// variable_name, function_name, TypeName
436 /// ```
437 Identifier,
438
439 /// A generic expression (catch-all for expressions)
440 /// ```rust,ignore
441 /// Any expression that doesn't fit specific categories
442 /// ```
443 Expression,
444
445 /// A binary expression
446 /// ```rust,ignore
447 /// x + y, a && b, count > 0
448 /// ```
449 BinaryExpression,
450
451 /// A unary expression
452 /// ```rust,ignore
453 /// !flag, -number, *pointer
454 /// ```
455 UnaryExpression,
456
457 /// An item used as a statement
458 /// ```rust,ignore
459 /// fn nested() {}
460 /// ```
461 ItemStatement,
462
463 /// An argument list in function calls
464 /// ```rust,ignore
465 /// example(1, "hello", true)
466 /// ```
467 ArgumentList,
468
469 /// A public item
470 /// ```rust,ignore
471 /// pub struct PublicStruct;
472 /// ```
473 PubItem,
474
475 /// A block expression
476 /// ```rust,ignore
477 /// {
478 /// statements;
479 /// expr
480 /// }
481 /// ```
482 Block,
483
484 /// Represents a syntax error in the parsed code
485 /// ```rust,ignore
486 /// Invalid or unparseable syntax
487 /// ```
488 Error,
489}
490
491impl ElementType for RustElementType {
492 type Role = UniversalElementRole;
493
494 fn is_root(&self) -> bool {
495 matches!(self, Self::SourceFile)
496 }
497
498 fn is_error(&self) -> bool {
499 matches!(self, Self::Error)
500 }
501
502 fn role(&self) -> UniversalElementRole {
503 use UniversalElementRole::*;
504 match self {
505 Self::SourceFile => Root,
506
507 // Symbol Management: Definitions
508 // These define names in a scope and are primary targets for outlines.
509 Self::Function | Self::StructItem | Self::EnumItem | Self::Trait | Self::Impl | Self::ModuleItem | Self::Static | Self::Const | Self::TypeAlias | Self::ExternCrate | Self::Macro => Definition,
510
511 // Hierarchy & Scoping: Containers
512 // These provide structural grouping and lexical scopes.
513 Self::Block | Self::ExternBlock | Self::ParameterList | Self::ArgumentList | Self::ArrayExpression | Self::TupleExpression | Self::StructExpression | Self::ParenthesizedExpression => Container,
514
515 // Flow Control & Logic: Statements
516 // Discrete instructions or units within a container.
517 Self::LetStatement | Self::ExpressionStatement | Self::ItemStatement | Self::UseItem | Self::ReturnExpression | Self::BreakExpression | Self::ContinueExpression => Statement,
518
519 // Flow Control & Logic: Expressions
520 // Computed results involving operators or complex logic.
521 Self::BinaryExpression
522 | Self::UnaryExpression
523 | Self::IfExpression
524 | Self::MatchExpression
525 | Self::LoopExpression
526 | Self::WhileExpression
527 | Self::ForExpression
528 | Self::AssignmentExpression
529 | Self::FieldExpression
530 | Self::IndexExpression
531 | Self::BlockExpression
532 | Self::Expression => Expression,
533
534 // Flow Control & Logic: Calls
535 // Explicit invocations of functions or macros.
536 Self::CallExpression | Self::MethodCallExpression => Call,
537
538 // Atomic Values
539 // Primitive data payloads (literals).
540 Self::IntegerLiteral | Self::FloatLiteral | Self::StringLiteral | Self::CharLiteral | Self::BooleanLiteral | Self::LiteralExpression => Value,
541
542 // Symbol Management: References
543 Self::IdentifierExpression | Self::PathExpression => Reference,
544
545 // Metadata & Auxiliaries: Typing
546 Self::Type | Self::PathType | Self::TupleType | Self::ArrayType | Self::SliceType | Self::ReferenceType | Self::PointerType | Self::FunctionType | Self::ReturnType | Self::GenericParams | Self::GenericArgs | Self::WhereClause => Typing,
547
548 // Metadata & Auxiliaries: Attributes & Modifiers
549 Self::Attribute => Metadata,
550 Self::Visibility | Self::Async | Self::Unsafe | Self::Extern => Attribute,
551
552 // Special cases or unclassified
553 Self::Parameter | Self::MatchArm | Self::Pattern => None,
554
555 Self::Error => Error,
556 _ => None,
557 }
558 }
559}
560
561impl From<RustTokenType> for RustElementType {
562 fn from(token_type: RustTokenType) -> Self {
563 match token_type {
564 // Error type - represents invalid tokens
565 RustTokenType::Error => Self::Error,
566
567 // Identifiers and literals map directly to their element types
568 RustTokenType::Identifier => Self::Identifier,
569 RustTokenType::IntegerLiteral => Self::IntegerLiteral,
570 RustTokenType::FloatLiteral => Self::FloatLiteral,
571 RustTokenType::StringLiteral => Self::StringLiteral,
572 RustTokenType::CharLiteral => Self::CharLiteral,
573 RustTokenType::BoolLiteral => Self::BooleanLiteral,
574
575 // Boolean keywords are treated as boolean literals
576 RustTokenType::True => Self::BooleanLiteral,
577 RustTokenType::False => Self::BooleanLiteral,
578
579 // Other keywords and symbols are not converted to element types
580 // The parser should handle these tokens directly without conversion
581 _ => {
582 // For all other token types, we don't perform conversion
583 // Let the parser use token types directly for matching
584 Self::Error // Temporary placeholder - parser should use tokens directly
585 }
586 }
587 }
588}