oak_python/kind/
mod.rs

1use oak_core::SyntaxKind;
2use serde::Serialize;
3
4/// Python 语法节点类型
5#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Serialize)]
6#[repr(u16)]
7pub enum PythonSyntaxKind {
8    // 基础 kind
9    Whitespace,
10    Comment,
11    Identifier,
12
13    // 字面量
14    Number,
15    String,
16    Bytes,
17    FString,
18
19    // 关键字
20    AndKeyword,
21    AsKeyword,
22    AssertKeyword,
23    AsyncKeyword,
24    AwaitKeyword,
25    BreakKeyword,
26    ClassKeyword,
27    ContinueKeyword,
28    DefKeyword,
29    DelKeyword,
30    ElifKeyword,
31    ElseKeyword,
32    ExceptKeyword,
33    FalseKeyword,
34    FinallyKeyword,
35    ForKeyword,
36    FromKeyword,
37    GlobalKeyword,
38    IfKeyword,
39    ImportKeyword,
40    InKeyword,
41    IsKeyword,
42    LambdaKeyword,
43    NoneKeyword,
44    NonlocalKeyword,
45    NotKeyword,
46    OrKeyword,
47    PassKeyword,
48    RaiseKeyword,
49    ReturnKeyword,
50    TrueKeyword,
51    TryKeyword,
52    WhileKeyword,
53    WithKeyword,
54    YieldKeyword,
55
56    // 运算符
57    Plus,
58    Minus,
59    Star,
60    DoubleStar,
61    Slash,
62    DoubleSlash,
63    Percent,
64    At,
65    LeftShift,
66    RightShift,
67    Ampersand,
68    Pipe,
69    Caret,
70    Tilde,
71    Less,
72    Greater,
73    LessEqual,
74    GreaterEqual,
75    Equal,
76    NotEqual,
77
78    // 赋值运算符
79    Assign,
80    PlusAssign,
81    MinusAssign,
82    StarAssign,
83    DoubleStarAssign,
84    SlashAssign,
85    DoubleSlashAssign,
86    PercentAssign,
87    AtAssign,
88    AmpersandAssign,
89    PipeAssign,
90    CaretAssign,
91    LeftShiftAssign,
92    RightShiftAssign,
93
94    // 分隔符
95    LeftParen,
96    RightParen,
97    LeftBracket,
98    RightBracket,
99    LeftBrace,
100    RightBrace,
101    Comma,
102    Colon,
103    Semicolon,
104    Dot,
105    Arrow,
106    Ellipsis,
107
108    // 特殊
109    Newline,
110    Indent,
111    Dedent,
112    Eof,
113    Error,
114
115    // 语法节点
116    Root,
117    Module,
118    InteractiveModule,
119    ExpressionModule,
120    FunctionDef,
121    AsyncFunctionDef,
122    ClassDef,
123    Return,
124    Delete,
125    AssignStmt,
126    AugAssign,
127    AnnAssign,
128    For,
129    AsyncFor,
130    While,
131    If,
132    With,
133    AsyncWith,
134    Raise,
135    Try,
136    Assert,
137    Import,
138    ImportFrom,
139    Global,
140    Nonlocal,
141    Expr,
142    Pass,
143    Break,
144    Continue,
145
146    // 表达式
147    BoolOp,
148    NamedExpr,
149    BinOp,
150    UnaryOp,
151    Lambda,
152    IfExp,
153    Dict,
154    Set,
155    ListComp,
156    SetComp,
157    DictComp,
158    GeneratorExp,
159    Await,
160    Yield,
161    YieldFrom,
162    Compare,
163    Call,
164    FormattedValue,
165    JoinedStr,
166    Constant,
167    Attribute,
168    Subscript,
169    Starred,
170    Name,
171    List,
172    Tuple,
173    Slice,
174
175    // 模式匹配 (Python 3.10+)
176    Match,
177    MatchValue,
178    MatchSingleton,
179    MatchSequence,
180    MatchMapping,
181    MatchClass,
182    MatchStar,
183    MatchAs,
184    MatchOr,
185
186    // 类型注解
187    TypeIgnore,
188
189    // 参数
190    Arguments,
191    Arg,
192    Keyword,
193
194    // 异常处理
195    ExceptHandler,
196
197    // 别名
198    Alias,
199
200    // With 项
201    WithItem,
202
203    // 推导式
204    Comprehension,
205
206    // 操作符
207    Add,
208    Sub,
209    Mult,
210    MatMult,
211    Div,
212    Mod,
213    Pow,
214    LShift,
215    RShift,
216    BitOr,
217    BitXor,
218    BitAnd,
219    FloorDiv,
220
221    // 一元操作符
222    Invert,
223    Not,
224    UAdd,
225    USub,
226
227    // 比较操作符
228    Eq,
229    NotEq,
230    Lt,
231    LtE,
232    Gt,
233    GtE,
234    Is,
235    IsNot,
236    In,
237    NotIn,
238
239    // 布尔操作符
240    And,
241    Or,
242
243    // 表达式上下文
244    Load,
245    Store,
246    Del,
247}
248
249impl From<u16> for PythonSyntaxKind {
250    fn from(d: u16) -> PythonSyntaxKind {
251        assert!(d <= (PythonSyntaxKind::Del as u16));
252        unsafe { core::mem::transmute::<u16, PythonSyntaxKind>(d) }
253    }
254}
255
256impl From<PythonSyntaxKind> for u16 {
257    fn from(k: PythonSyntaxKind) -> u16 {
258        k as u16
259    }
260}
261
262impl SyntaxKind for PythonSyntaxKind {
263    fn is_trivia(&self) -> bool {
264        matches!(self, Self::Whitespace | Self::Comment | Self::Newline)
265    }
266
267    fn is_comment(&self) -> bool {
268        matches!(self, Self::Comment)
269    }
270
271    fn is_whitespace(&self) -> bool {
272        matches!(self, Self::Whitespace | Self::Newline)
273    }
274
275    fn is_token_type(&self) -> bool {
276        !matches!(
277            self,
278            Self::Root
279                | Self::Module
280                | Self::InteractiveModule
281                | Self::ExpressionModule
282                | Self::FunctionDef
283                | Self::AsyncFunctionDef
284                | Self::ClassDef
285        )
286    }
287
288    fn is_element_type(&self) -> bool {
289        matches!(
290            self,
291            Self::Root
292                | Self::Module
293                | Self::InteractiveModule
294                | Self::ExpressionModule
295                | Self::FunctionDef
296                | Self::AsyncFunctionDef
297                | Self::ClassDef
298        )
299    }
300}