oak_elixir/kind/
mod.rs

1use oak_core::{SyntaxKind, Token};
2use serde::{Deserialize, Serialize};
3
4pub type ElixirToken = Token<ElixirSyntaxKind>;
5
6/// Represents all possible syntax kinds in the Elixir programming language.
7#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
8pub enum ElixirSyntaxKind {
9    /// Whitespace characters (spaces, tabs)
10    Whitespace,
11    /// Newline character
12    Newline,
13    /// Comment
14    Comment,
15    /// Identifier (variable names, function names, etc.)
16    Identifier,
17    /// Atom literal
18    Atom,
19    /// Variable name
20    Variable,
21    /// Number literal
22    Number,
23    /// Float literal
24    Float,
25    /// String literal
26    String,
27    /// Character literal
28    Character,
29    /// Sigil literal
30    Sigil,
31
32    /// after keyword
33    After,
34    /// and keyword
35    And,
36    /// case keyword
37    Case,
38    /// catch keyword
39    Catch,
40    /// cond keyword
41    Cond,
42    /// def keyword
43    Def,
44    /// defp keyword (private function)
45    Defp,
46    /// defmodule keyword
47    Defmodule,
48    /// defstruct keyword
49    Defstruct,
50    /// defprotocol keyword
51    Defprotocol,
52    /// defimpl keyword
53    Defimpl,
54    /// defmacro keyword
55    Defmacro,
56    /// defmacrop keyword (private macro)
57    Defmacrop,
58    /// do keyword
59    Do,
60    /// else keyword
61    Else,
62    /// elsif keyword
63    Elsif,
64    /// end keyword
65    End,
66    /// false keyword
67    False,
68    /// fn keyword
69    Fn,
70    /// if keyword
71    If,
72    /// in keyword
73    In,
74    /// not keyword
75    Not,
76    /// or keyword
77    Or,
78    /// receive keyword
79    Receive,
80    /// rescue keyword
81    Rescue,
82    /// true keyword
83    True,
84    /// try keyword
85    Try,
86    /// unless keyword
87    Unless,
88    /// when keyword
89    When,
90    /// with keyword
91    With,
92
93    /// plus operator (+)
94    Plus,
95    /// minus operator (-)
96    Minus,
97    /// multiplication operator (*)
98    Star,
99    /// division operator (/)
100    Slash,
101    /// assignment operator (=)
102    Equal,
103    /// equality operator (==)
104    EqualEqual,
105    /// inequality operator (!=)
106    NotEqual,
107    /// strict equality operator (===)
108    EqualEqualEqual,
109    /// strict inequality operator (!==)
110    NotEqualEqual,
111    /// less than operator (<)
112    Less,
113    /// greater than operator (>)
114    Greater,
115    /// less than or equal operator (<=)
116    LessEqual,
117    /// greater than or equal operator (>=)
118    GreaterEqual,
119    /// concatenation operator (++)
120    PlusPlus,
121    /// subtraction operator (--)
122    MinusMinus,
123    /// exponentiation operator (**)
124    StarStar,
125    /// exclamation mark (!)
126    Exclamation,
127    /// question mark (?)
128    Question,
129    /// ampersand (&)
130    Ampersand,
131    /// at symbol (@)
132    At,
133    /// caret (^)
134    Caret,
135    /// tilde (~)
136    Tilde,
137    /// left shift operator (<<)
138    LeftShift,
139    /// right shift operator (>>)
140    RightShift,
141    /// match operator (=~)
142    MatchOp,
143    /// pipe right operator (|>)
144    PipeRight,
145
146    // 分隔符
147    LeftParen,    // (
148    RightParen,   // )
149    LeftBrace,    // {
150    RightBrace,   // }
151    LeftBracket,  // [
152    RightBracket, // ]
153    Comma,        // ,
154    Semicolon,    // ;
155    Dot,          // .
156    Colon,        // :
157    Arrow,        // ->
158    Pipe,         // |
159    PipePipe,     // ||
160    Hash,         // #
161
162    // 特殊
163    Error,
164    Eof,
165
166    // 语法节点类型 (非终结符)
167    SourceFile,
168    Module,
169    Function,
170    ParameterList,
171    Parameter,
172    BlockExpression,
173    LetStatement,
174    ExpressionStatement,
175    IdentifierExpression,
176    LiteralExpression,
177    BooleanLiteral,
178    ParenthesizedExpression,
179    BinaryExpression,
180    UnaryExpression,
181    CallExpression,
182    FieldExpression,
183    IndexExpression,
184    IfExpression,
185    MatchExpression,
186    LoopExpression,
187    WhileExpression,
188    ForExpression,
189    BreakExpression,
190    ContinueExpression,
191    ReturnExpression,
192    StructExpression,
193    TupleExpression,
194    ArrayExpression,
195    RangeExpression,
196    ClosureExpression,
197    AsyncBlockExpression,
198    UnsafeBlockExpression,
199    TryExpression,
200    AwaitExpression,
201    MacroCall,
202    Path,
203    PathSegment,
204    GenericArgs,
205    TypePath,
206    TupleType,
207    ArrayType,
208    SliceType,
209    ReferenceType,
210    PointerType,
211    FunctionType,
212    TraitObjectType,
213    ImplTraitType,
214    InferredType,
215    NeverType,
216    Pattern,
217    IdentifierPattern,
218    WildcardPattern,
219    TuplePattern,
220    StructPattern,
221    TupleStructPattern,
222    SlicePattern,
223    ReferencePattern,
224    LiteralPattern,
225    RangePattern,
226    OrPattern,
227    RestPattern,
228    StructDeclaration,
229    EnumDeclaration,
230    UnionDeclaration,
231    TraitDeclaration,
232    ImplDeclaration,
233    ModuleDeclaration,
234    UseDeclaration,
235    ConstDeclaration,
236    StaticDeclaration,
237    TypeAliasDeclaration,
238    ExternBlock,
239    ExternFunction,
240    Attribute,
241    Visibility,
242    GenericParams,
243    GenericParam,
244    TypeParam,
245    ConstParam,
246    LifetimeParam,
247    WhereClause,
248    WherePredicate,
249    ReturnType,
250    FieldList,
251    Field,
252    Variant,
253    VariantList,
254    AssociatedItem,
255    TraitItem,
256    ImplItem,
257}
258
259impl SyntaxKind for ElixirSyntaxKind {
260    fn is_trivia(&self) -> bool {
261        matches!(self, Self::Whitespace | Self::Newline | Self::Comment)
262    }
263
264    fn is_comment(&self) -> bool {
265        matches!(self, Self::Comment)
266    }
267
268    fn is_whitespace(&self) -> bool {
269        matches!(self, Self::Whitespace | Self::Newline)
270    }
271
272    fn is_token_type(&self) -> bool {
273        use ElixirSyntaxKind::*;
274        !matches!(
275            self,
276            Error | Eof |
277            // 语法节点类型 (非终结符)
278            SourceFile | Module | Function | ParameterList | Parameter | BlockExpression |
279            LetStatement | ExpressionStatement | IdentifierExpression | LiteralExpression |
280            BooleanLiteral | ParenthesizedExpression | BinaryExpression | UnaryExpression |
281            CallExpression | FieldExpression | IndexExpression | IfExpression |
282            MatchExpression | LoopExpression | WhileExpression | ForExpression |
283            BreakExpression | ContinueExpression | ReturnExpression | StructExpression |
284            TupleExpression | ArrayExpression | RangeExpression | ClosureExpression |
285            AsyncBlockExpression | UnsafeBlockExpression | TryExpression | AwaitExpression |
286            MacroCall | Path | PathSegment | GenericArgs | TypePath | TupleType |
287            ArrayType | SliceType | ReferenceType | PointerType | FunctionType |
288            TraitObjectType | ImplTraitType | InferredType | NeverType | Pattern |
289            IdentifierPattern | WildcardPattern | TuplePattern | StructPattern |
290            TupleStructPattern | SlicePattern | ReferencePattern | LiteralPattern |
291            RangePattern | OrPattern | RestPattern | StructDeclaration | EnumDeclaration | UnionDeclaration |
292             TraitDeclaration | ImplDeclaration | ModuleDeclaration | UseDeclaration | ConstDeclaration |
293             StaticDeclaration | TypeAliasDeclaration | ExternBlock | ExternFunction |
294            Attribute | Visibility | GenericParams | GenericParam | TypeParam |
295            ConstParam | LifetimeParam | WhereClause | WherePredicate |
296            ReturnType | FieldList | Field | Variant | VariantList |
297            AssociatedItem | TraitItem | ImplItem
298        )
299    }
300
301    fn is_element_type(&self) -> bool {
302        use ElixirSyntaxKind::*;
303        matches!(
304            self,
305            Error | Eof |
306            // 语法节点类型 (非终结符)
307            SourceFile | Module | Function | ParameterList | Parameter | BlockExpression |
308            LetStatement | ExpressionStatement | IdentifierExpression | LiteralExpression |
309            BooleanLiteral | ParenthesizedExpression | BinaryExpression | UnaryExpression |
310            CallExpression | FieldExpression | IndexExpression | IfExpression |
311            MatchExpression | LoopExpression | WhileExpression | ForExpression |
312            BreakExpression | ContinueExpression | ReturnExpression | StructExpression |
313            TupleExpression | ArrayExpression | RangeExpression | ClosureExpression |
314            AsyncBlockExpression | UnsafeBlockExpression | TryExpression | AwaitExpression |
315            MacroCall | Path | PathSegment | GenericArgs | TypePath | TupleType |
316            ArrayType | SliceType | ReferenceType | PointerType | FunctionType |
317            TraitObjectType | ImplTraitType | InferredType | NeverType | Pattern |
318            IdentifierPattern | WildcardPattern | TuplePattern | StructPattern |
319            TupleStructPattern | SlicePattern | ReferencePattern | LiteralPattern |
320            RangePattern | OrPattern | RestPattern | StructDeclaration | EnumDeclaration | UnionDeclaration |
321             TraitDeclaration | ImplDeclaration | ModuleDeclaration | UseDeclaration | ConstDeclaration |
322             StaticDeclaration | TypeAliasDeclaration | ExternBlock | ExternFunction |
323            Attribute | Visibility | GenericParams | GenericParam | TypeParam |
324            ConstParam | LifetimeParam | WhereClause | WherePredicate |
325            ReturnType | FieldList | Field | Variant | VariantList |
326            AssociatedItem | TraitItem | ImplItem
327        )
328    }
329}
330
331impl ElixirSyntaxKind {
332    pub fn is_keyword(self) -> bool {
333        matches!(
334            self,
335            Self::After
336                | Self::And
337                | Self::Case
338                | Self::Catch
339                | Self::Cond
340                | Self::Def
341                | Self::Defp
342                | Self::Defmodule
343                | Self::Defstruct
344                | Self::Defprotocol
345                | Self::Defimpl
346                | Self::Defmacro
347                | Self::Defmacrop
348                | Self::Do
349                | Self::Else
350                | Self::Elsif
351                | Self::End
352                | Self::False
353                | Self::Fn
354                | Self::If
355                | Self::In
356                | Self::Not
357                | Self::Or
358                | Self::Receive
359                | Self::Rescue
360                | Self::True
361                | Self::Try
362                | Self::Unless
363                | Self::When
364                | Self::With
365        )
366    }
367}