Skip to main content

ridx_syntax/
syntax_kind.rs

1/*
2 * rust-intelligence — Ultra-fast, zero-memory code intelligence for Rust
3 * Copyright (c) 2026 Alexander Panasenko
4 *
5 * Contact: alex@prod.codes
6 * Author: https://prod.codes/about/
7 * Project: https://github.com/alex09x/rust-intelligence
8 * SPDX-License-Identifier: MIT OR Apache-2.0
9 */
10
11//! The kinds of syntax tree nodes. Tokens are not nodes: a node covers a range of lexer tokens.
12
13/// Kind of a [`crate::tree::SyntaxTree`] node.
14#[repr(u8)]
15#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
16pub enum SyntaxKind {
17    /// Placeholder of an abandoned or not yet completed marker; never in a built tree.
18    Tombstone,
19    SourceFile,
20    Error,
21    Attr,
22    Visibility,
23    Name,
24    TokenTree,
25    Abi,
26
27    // Items.
28    Fn,
29    ParamList,
30    Param,
31    SelfParam,
32    RetType,
33    WhereClause,
34    WherePred,
35    GenericParamList,
36    LifetimeParam,
37    TypeParam,
38    ConstParam,
39    Impl,
40    Trait,
41    Struct,
42    Enum,
43    Union,
44    TypeAlias,
45    Const,
46    Static,
47    Module,
48    Use,
49    ExternCrate,
50    ExternBlock,
51    MacroRules,
52    MacroCall,
53    ItemList,
54
55    // Types and paths.
56    PathType,
57    RefType,
58    PtrType,
59    TupleType,
60    ArrayType,
61    SliceType,
62    FnPtrType,
63    ImplTraitType,
64    DynTraitType,
65    NeverType,
66    InferType,
67    ParenType,
68    TypeBoundList,
69    TypeBound,
70    ForBinder,
71    Path,
72    PathSegment,
73    GenericArgList,
74    TypeArg,
75    LifetimeArg,
76    ConstArg,
77    AssocTypeArg,
78    Lifetime,
79
80    // Patterns.
81    IdentPat,
82    WildcardPat,
83    RefPat,
84    TuplePat,
85    TupleStructPat,
86    RecordPat,
87    RecordPatField,
88    PathPat,
89    LiteralPat,
90    RangePat,
91    OrPat,
92    SlicePat,
93    RestPat,
94    BoxPat,
95    MacroPat,
96    ParenPat,
97    ConstBlockPat,
98
99    // Expressions.
100    Literal,
101    PathExpr,
102    CallExpr,
103    ArgList,
104    MethodCallExpr,
105    FieldExpr,
106    IndexExpr,
107    TryExpr,
108    AwaitExpr,
109    PrefixExpr,
110    RefExpr,
111    BinExpr,
112    CastExpr,
113    RangeExpr,
114    ParenExpr,
115    TupleExpr,
116    ArrayExpr,
117    BlockExpr,
118    IfExpr,
119    MatchExpr,
120    MatchArmList,
121    MatchArm,
122    MatchGuard,
123    LoopExpr,
124    WhileExpr,
125    ForExpr,
126    ReturnExpr,
127    BreakExpr,
128    ContinueExpr,
129    ClosureExpr,
130    RecordExpr,
131    RecordExprFieldList,
132    RecordExprField,
133    LetExpr,
134    Label,
135    UnderscoreExpr,
136    YieldExpr,
137    BecomeExpr,
138
139    // Statements.
140    LetStmt,
141    LetElse,
142    ExprStmt,
143}
144
145const F_EXPR: u8 = 1 << 0;
146const F_PAT: u8 = 1 << 1;
147const F_TYPE: u8 = 1 << 2;
148const F_ITEM: u8 = 1 << 3;
149const F_BLOCK_LIKE: u8 = 1 << 4;
150const F_CF_CUT: u8 = 1 << 5;
151const F_SEMA_SKIP: u8 = 1 << 6;
152
153const KIND_FLAGS: [u8; 256] = {
154    use SyntaxKind as K;
155    let mut t = [0u8; 256];
156
157    // Expressions: Literal..=BecomeExpr excluding non-expr helper nodes.
158    let mut i = K::Literal as usize;
159    while i <= K::BecomeExpr as usize {
160        t[i] |= F_EXPR;
161        i += 1;
162    }
163    t[K::ArgList as usize] &= !F_EXPR;
164    t[K::MatchArmList as usize] &= !F_EXPR;
165    t[K::MatchArm as usize] &= !F_EXPR;
166    t[K::MatchGuard as usize] &= !F_EXPR;
167    t[K::RecordExprFieldList as usize] &= !F_EXPR;
168    t[K::RecordExprField as usize] &= !F_EXPR;
169    t[K::Label as usize] &= !F_EXPR;
170
171    // Patterns: IdentPat..=ConstBlockPat excluding RecordPatField.
172    let mut p = K::IdentPat as usize;
173    while p <= K::ConstBlockPat as usize {
174        t[p] |= F_PAT;
175        p += 1;
176    }
177    t[K::RecordPatField as usize] &= !F_PAT;
178
179    // Types: PathType..=ParenType.
180    let mut ty = K::PathType as usize;
181    while ty <= K::ParenType as usize {
182        t[ty] |= F_TYPE | F_CF_CUT | F_SEMA_SKIP;
183        ty += 1;
184    }
185
186    // Items: Fn plus Impl..=MacroRules.
187    t[K::Fn as usize] |= F_ITEM | F_CF_CUT | F_SEMA_SKIP;
188    let mut it = K::Impl as usize;
189    while it <= K::MacroRules as usize {
190        t[it] |= F_ITEM | F_CF_CUT | F_SEMA_SKIP;
191        it += 1;
192    }
193
194    // Block-like expressions.
195    t[K::BlockExpr as usize] |= F_BLOCK_LIKE;
196    t[K::IfExpr as usize] |= F_BLOCK_LIKE;
197    t[K::MatchExpr as usize] |= F_BLOCK_LIKE;
198    t[K::LoopExpr as usize] |= F_BLOCK_LIKE;
199    t[K::WhileExpr as usize] |= F_BLOCK_LIKE;
200    t[K::ForExpr as usize] |= F_BLOCK_LIKE;
201
202    // Control-flow & sema subtree cutoffs.
203    t[K::ClosureExpr as usize] |= F_CF_CUT;
204    t[K::TokenTree as usize] |= F_CF_CUT | F_SEMA_SKIP;
205    t[K::Attr as usize] |= F_CF_CUT | F_SEMA_SKIP;
206    t[K::ConstBlockPat as usize] |= F_CF_CUT | F_SEMA_SKIP;
207    t[K::Path as usize] |= F_CF_CUT | F_SEMA_SKIP;
208    t[K::ParamList as usize] |= F_CF_CUT | F_SEMA_SKIP;
209    t[K::GenericParamList as usize] |= F_CF_CUT | F_SEMA_SKIP;
210    t[K::WhereClause as usize] |= F_CF_CUT | F_SEMA_SKIP;
211
212    // Additional value-sema leaf/non-expr skip subtrees.
213    t[K::Name as usize] |= F_SEMA_SKIP;
214    t[K::Lifetime as usize] |= F_SEMA_SKIP;
215    t[K::Label as usize] |= F_SEMA_SKIP;
216    t[K::Visibility as usize] |= F_SEMA_SKIP;
217    t[K::RetType as usize] |= F_SEMA_SKIP;
218    t[K::GenericArgList as usize] |= F_SEMA_SKIP;
219
220    t
221};
222
223impl SyntaxKind {
224    /// Whether a node of this kind is an expression.
225    #[inline(always)]
226    pub const fn is_expr(self) -> bool {
227        (KIND_FLAGS[self as usize] & F_EXPR) != 0
228    }
229
230    /// Whether a node of this kind is a pattern.
231    #[inline(always)]
232    pub const fn is_pat(self) -> bool {
233        (KIND_FLAGS[self as usize] & F_PAT) != 0
234    }
235
236    /// Whether a node of this kind is a type.
237    #[inline(always)]
238    pub const fn is_type(self) -> bool {
239        (KIND_FLAGS[self as usize] & F_TYPE) != 0
240    }
241
242    /// Whether a node of this kind is an item.
243    #[inline(always)]
244    pub const fn is_item(self) -> bool {
245        (KIND_FLAGS[self as usize] & F_ITEM) != 0
246    }
247
248    /// Whether an expression of this kind ends a statement without a `;` (`if`, `match`, loops, blocks).
249    #[inline(always)]
250    pub const fn is_block_like(self) -> bool {
251        (KIND_FLAGS[self as usize] & F_BLOCK_LIKE) != 0
252    }
253
254    #[inline(always)]
255    pub(crate) const fn is_cf_cutoff(self) -> bool {
256        (KIND_FLAGS[self as usize] & F_CF_CUT) != 0
257    }
258
259    #[inline(always)]
260    pub(crate) const fn is_sema_skip(self) -> bool {
261        (KIND_FLAGS[self as usize] & F_SEMA_SKIP) != 0
262    }
263}