Skip to main content

oak_dart/kind/
mod.rs

1use oak_core::{ElementType, Token, TokenType, UniversalElementRole, UniversalTokenRole};
2use serde::{Deserialize, Serialize};
3
4pub type DartToken = Token<DartSyntaxKind>;
5
6/// Represents all possible syntax kinds in the Dart programming language.
7#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
8pub enum DartSyntaxKind {
9    /// Root node of the syntax tree
10    Root,
11    /// Class declaration
12    ClassDeclaration,
13    /// Function declaration
14    FunctionDeclaration,
15    /// Whitespace characters (spaces, tabs)
16    Whitespace,
17    /// Newline character
18    Newline,
19
20    /// Identifier (variable names, function names, etc.)
21    Identifier,
22    /// Integer literal
23    IntegerLiteral,
24    /// Double (floating-point) literal
25    DoubleLiteral,
26    /// String literal
27    StringLiteral,
28    /// Boolean literal (true/false)
29    BooleanLiteral,
30    /// Null literal
31    NullLiteral,
32
33    /// abstract keyword
34    Abstract,
35    /// as keyword
36    As,
37    /// assert keyword
38    Assert,
39    /// async keyword
40    Async,
41    /// await keyword
42    Await,
43    /// break keyword
44    Break,
45    /// case keyword
46    Case,
47    /// catch keyword
48    Catch,
49    /// class keyword
50    Class,
51    /// const keyword
52    Const,
53    /// continue keyword
54    Continue,
55    /// covariant keyword
56    Covariant,
57    /// default keyword
58    Default,
59    /// deferred keyword
60    Deferred,
61    /// do keyword
62    Do,
63    /// dynamic keyword
64    Dynamic,
65    /// else keyword
66    Else,
67    /// enum keyword
68    Enum,
69    /// export keyword
70    Export,
71    /// extends keyword
72    Extends,
73    /// extension keyword
74    Extension,
75    /// external keyword
76    External,
77    /// factory keyword
78    Factory,
79    /// false keyword
80    False,
81    /// final keyword
82    Final,
83    /// finally keyword
84    Finally,
85    /// for keyword
86    For,
87    /// function keyword
88    Function,
89    /// get keyword
90    Get,
91    /// hide keyword
92    Hide,
93    /// if keyword
94    If,
95    /// implements keyword
96    Implements,
97    /// import keyword
98    Import,
99    /// in keyword
100    In,
101    /// interface keyword
102    Interface,
103    /// is keyword
104    Is,
105    /// late keyword
106    Late,
107    /// library keyword
108    Library,
109    /// mixin keyword
110    Mixin,
111    /// new keyword
112    New,
113    /// null keyword
114    Null,
115    /// on keyword
116    On,
117    /// operator keyword
118    Operator,
119    /// part keyword
120    Part,
121    /// required keyword
122    Required,
123    /// rethrow keyword
124    Rethrow,
125    /// return keyword
126    Return,
127    /// set keyword
128    Set,
129    /// show keyword
130    Show,
131    /// static keyword
132    Static,
133    /// super keyword
134    Super,
135    /// switch keyword
136    Switch,
137    /// sync keyword
138    Sync,
139    /// this keyword
140    This,
141    /// throw keyword
142    Throw,
143    /// true keyword
144    True,
145    /// try keyword
146    Try,
147    /// typedef keyword
148    Typedef,
149    /// var keyword
150    Var,
151    /// void keyword
152    Void,
153    /// while keyword
154    While,
155    /// with keyword
156    With,
157    /// yield keyword
158    Yield,
159
160    /// plus operator (+)
161    Plus,
162    /// minus operator (-)
163    Minus,
164    /// multiplication operator (*)
165    Star,
166    /// division operator (/)
167    Slash,
168    /// modulo operator (%)
169    Percent,
170    /// integer division operator (~/)
171    TildeSlash,
172    /// assignment operator (=)
173    Equal,
174    /// equality operator (==)
175    EqualEqual,
176    /// inequality operator (!=)
177    BangEqual,
178    /// less than operator (<)
179    Less,
180    /// greater than operator (>)
181    Greater,
182    /// less than or equal operator (<=)
183    LessEqual,
184    /// greater than or equal operator (>=)
185    GreaterEqual,
186    /// left shift operator (<<)
187    LeftShift,
188    /// right shift operator (>>)
189    RightShift,
190    /// bitwise AND operator (&)
191    Ampersand,
192    /// bitwise OR operator (|)
193    Pipe,
194    /// bitwise XOR operator (^)
195    Caret,
196    /// bitwise NOT operator (~)
197    Tilde,
198    /// logical NOT operator (!)
199    Bang,
200    /// logical AND operator (&&)
201    AmpersandAmpersand,
202    /// logical OR operator (||)
203    PipePipe,
204    /// ternary operator (?)
205    Question,
206    /// null-aware operator (??)
207    QuestionQuestion,
208    /// increment operator (++)
209    PlusPlus,
210    /// decrement operator (--)
211    MinusMinus,
212    /// plus assignment operator (+=)
213    PlusEqual,
214    /// minus assignment operator (-=)
215    MinusEqual,
216    /// multiplication assignment operator (*=)
217    StarEqual,
218    /// division assignment operator (/=)
219    SlashEqual,
220    /// modulo assignment operator (%=)
221    PercentEqual,
222    /// integer division assignment operator (~/=)
223    TildeSlashEqual,
224    /// left shift assignment operator (<<=)
225    LeftShiftEqual,
226    /// right shift assignment operator (>>=)
227    RightShiftEqual,
228    /// bitwise AND assignment operator (&=)
229    AmpersandEqual,
230    /// bitwise OR assignment operator (|=)
231    PipeEqual,
232    /// bitwise XOR assignment operator (^=)
233    CaretEqual,
234    /// null-aware assignment operator (??=)
235    QuestionQuestionEqual,
236    /// arrow operator (=>)
237    Arrow,
238    /// dot operator (.)
239    Dot,
240    /// cascade operator (..)
241    DotDot,
242    /// spread operator (...)
243    DotDotDot,
244    /// null-aware dot operator (?.)
245    QuestionDot,
246
247    /// left parenthesis (
248    LeftParen,
249    /// right parenthesis )
250    RightParen,
251    /// left bracket [
252    LeftBracket,
253    /// right bracket ]
254    RightBracket,
255    /// left brace {
256    LeftBrace,
257    /// right brace }
258    RightBrace,
259    /// semicolon ;
260    Semicolon,
261    /// comma ,
262    Comma,
263    /// colon :
264    Colon,
265    /// at symbol @
266    At,
267    /// hash symbol #
268    Hash,
269
270    /// line comment (//)
271    LineComment,
272    /// block comment (/* */)
273    BlockComment,
274    /// documentation comment (///)
275    DocComment,
276
277    /// error token
278    Error,
279
280    /// end of file
281    Eof,
282    /// Variable declaration
283    VariableDeclaration,
284}
285
286impl TokenType for DartSyntaxKind {
287    const END_OF_STREAM: Self = Self::Eof;
288    type Role = UniversalTokenRole;
289
290    fn is_ignored(&self) -> bool {
291        matches!(self, Self::Whitespace | Self::Newline | Self::LineComment | Self::BlockComment | Self::DocComment)
292    }
293
294    fn is_comment(&self) -> bool {
295        matches!(self, Self::LineComment | Self::BlockComment | Self::DocComment)
296    }
297
298    fn is_whitespace(&self) -> bool {
299        matches!(self, Self::Whitespace | Self::Newline)
300    }
301
302    fn role(&self) -> Self::Role {
303        match self {
304            Self::Whitespace => UniversalTokenRole::Whitespace,
305            Self::Newline => UniversalTokenRole::Whitespace,
306            Self::Identifier => UniversalTokenRole::Name,
307            Self::IntegerLiteral | Self::DoubleLiteral | Self::StringLiteral | Self::BooleanLiteral | Self::NullLiteral => UniversalTokenRole::Literal,
308            Self::Abstract
309            | Self::As
310            | Self::Assert
311            | Self::Async
312            | Self::Await
313            | Self::Break
314            | Self::Case
315            | Self::Catch
316            | Self::Class
317            | Self::Const
318            | Self::Continue
319            | Self::Covariant
320            | Self::Default
321            | Self::Deferred
322            | Self::Do
323            | Self::Dynamic
324            | Self::Else
325            | Self::Enum
326            | Self::Export
327            | Self::Extends
328            | Self::Extension
329            | Self::External
330            | Self::Factory
331            | Self::False
332            | Self::Final
333            | Self::Finally
334            | Self::For
335            | Self::Function
336            | Self::Get
337            | Self::Hide
338            | Self::If
339            | Self::Implements
340            | Self::Import
341            | Self::In
342            | Self::Interface
343            | Self::Is
344            | Self::Late
345            | Self::Library
346            | Self::Mixin
347            | Self::New
348            | Self::Null
349            | Self::On
350            | Self::Operator
351            | Self::Part
352            | Self::Required
353            | Self::Rethrow
354            | Self::Return
355            | Self::Set
356            | Self::Show
357            | Self::Static
358            | Self::Super
359            | Self::Switch
360            | Self::Sync
361            | Self::This
362            | Self::Throw
363            | Self::True
364            | Self::Try
365            | Self::Typedef
366            | Self::Var
367            | Self::Void
368            | Self::While
369            | Self::With
370            | Self::Yield => UniversalTokenRole::Keyword,
371            Self::Plus
372            | Self::Minus
373            | Self::Star
374            | Self::Slash
375            | Self::Percent
376            | Self::TildeSlash
377            | Self::Equal
378            | Self::EqualEqual
379            | Self::BangEqual
380            | Self::Less
381            | Self::Greater
382            | Self::LessEqual
383            | Self::GreaterEqual
384            | Self::LeftShift
385            | Self::RightShift
386            | Self::Ampersand
387            | Self::Pipe
388            | Self::Caret
389            | Self::Tilde
390            | Self::Bang
391            | Self::AmpersandAmpersand
392            | Self::PipePipe
393            | Self::Question
394            | Self::QuestionQuestion
395            | Self::PlusPlus
396            | Self::MinusMinus
397            | Self::PlusEqual
398            | Self::MinusEqual
399            | Self::StarEqual
400            | Self::SlashEqual
401            | Self::PercentEqual
402            | Self::TildeSlashEqual
403            | Self::LeftShiftEqual
404            | Self::RightShiftEqual
405            | Self::AmpersandEqual
406            | Self::PipeEqual
407            | Self::CaretEqual
408            | Self::QuestionQuestionEqual
409            | Self::Arrow
410            | Self::Dot
411            | Self::DotDot
412            | Self::DotDotDot
413            | Self::QuestionDot => UniversalTokenRole::Operator,
414            Self::LeftParen | Self::RightParen | Self::LeftBracket | Self::RightBracket | Self::LeftBrace | Self::RightBrace | Self::Semicolon | Self::Comma | Self::Colon | Self::At | Self::Hash => UniversalTokenRole::Punctuation,
415            Self::LineComment | Self::BlockComment | Self::DocComment => UniversalTokenRole::Comment,
416            Self::Error => UniversalTokenRole::Error,
417            Self::Eof | Self::Root | Self::ClassDeclaration | Self::FunctionDeclaration | Self::VariableDeclaration => UniversalTokenRole::None,
418        }
419    }
420}
421
422impl ElementType for DartSyntaxKind {
423    type Role = UniversalElementRole;
424
425    fn is_root(&self) -> bool {
426        matches!(self, Self::Root)
427    }
428
429    fn is_error(&self) -> bool {
430        matches!(self, Self::Error)
431    }
432
433    fn role(&self) -> Self::Role {
434        match self {
435            Self::Root => UniversalElementRole::Root,
436            Self::ClassDeclaration => UniversalElementRole::Definition,
437            Self::FunctionDeclaration => UniversalElementRole::Definition,
438            Self::VariableDeclaration => UniversalElementRole::Definition,
439            Self::Error => UniversalElementRole::Error,
440            _ => UniversalElementRole::None,
441        }
442    }
443}