Skip to main content

oak_perl/kind/
mod.rs

1use crate::PerlLanguage;
2use oak_core::{ElementType, TokenType, UniversalElementRole, UniversalTokenRole};
3use serde::{Deserialize, Serialize};
4
5#[derive(Copy, Clone, Debug, PartialEq, Eq, Hash, Serialize, Deserialize)]
6pub enum PerlSyntaxKind {
7    // Basic tokens
8    Whitespace,
9    Newline,
10
11    // Comments
12    Comment,
13
14    // Literals
15    StringLiteral,
16    NumberLiteral,
17    RegexLiteral,
18
19    // Identifiers and keywords
20    Identifier,
21    Package,
22    Use,
23    Sub,
24    My,
25    Our,
26    Local,
27    If,
28    Elsif,
29    Else,
30    Unless,
31    While,
32    Until,
33    For,
34    Foreach,
35    Do,
36    Last,
37    Next,
38    Redo,
39    Return,
40    Die,
41    Warn,
42    Print,
43    Printf,
44    Chomp,
45    Chop,
46    Length,
47    Substr,
48    Index,
49    Rindex,
50    Split,
51    Join,
52    Push,
53    Pop,
54    Shift,
55    Unshift,
56    Sort,
57    Reverse,
58    Keys,
59    Values,
60    Each,
61    Exists,
62    Delete,
63    Defined,
64    Undef,
65    Ref,
66    Bless,
67    New,
68    Can,
69    Isa,
70    Scalar,
71    Array,
72    Hash,
73    Code,
74    Glob,
75    Open,
76    Close,
77    Read,
78    Write,
79    Seek,
80    Tell,
81
82    Binmode,
83    Chodir,
84    Mkdir,
85    Rmdir,
86    Opendir,
87    Readdir,
88    Closedir,
89    Stat,
90    Lstat,
91    Chmod,
92    Chown,
93    Link,
94    Unlink,
95    Rename,
96    Symlink,
97    Readlink,
98    Eval,
99    Require,
100    Import,
101    No,
102    Strict,
103    Warnings,
104    Vars,
105    Subs,
106    Refs,
107
108    // Operators
109    Plus,
110    Minus,
111    Increment,
112    Decrement,
113    Star,
114    Slash,
115    Percent,
116    Power,
117    Concat,
118    Repeat,
119    Match,
120    NotMatch,
121    Substitute,
122    Transliterate,
123    Equal,
124    NotEqual,
125    LessThan,
126    LessEqual,
127    GreaterThan,
128    GreaterEqual,
129    Spaceship,
130    StringEqual,
131    StringNotEqual,
132    StringLess,
133    StringLessEqual,
134    StringGreater,
135    StringGreaterEqual,
136    StringCompare,
137    And,
138    Or,
139    Not,
140    Xor,
141    LogicalAnd,
142    LogicalOr,
143    LogicalNot,
144    LogicalXor,
145    BitwiseAnd,
146    BitwiseOr,
147    BitwiseXor,
148    BitwiseNot,
149    LeftShift,
150    RightShift,
151    Assign,
152    PlusAssign,
153    MinusAssign,
154    MultiplyAssign,
155    DivideAssign,
156    ModuloAssign,
157    PowerAssign,
158    ConcatAssign,
159    LogicalAndAssign,
160    LogicalOrAssign,
161    BitwiseAndAssign,
162    BitwiseOrAssign,
163    BitwiseXorAssign,
164    LeftShiftAssign,
165    RightShiftAssign,
166
167    // Delimiters
168    LeftParen,
169    RightParen,
170    LeftBrace,
171    RightBrace,
172    LeftBracket,
173    RightBracket,
174    Semicolon,
175    Comma,
176    Arrow,
177    FatArrow,
178    Dot,
179    Range,
180    Ellipsis,
181
182    // Special characters
183    Dollar,
184    At,
185    Percent_,
186    Ampersand,
187    Backslash,
188    Question,
189    Colon,
190    DoubleColon,
191    Quote,
192    DoubleQuote,
193    Backtick,
194
195    // Composite nodes
196    Program,
197    Statement,
198    Expression,
199    Block,
200    SubroutineDeclaration,
201    PackageDeclaration,
202    UseStatement,
203    VariableDeclaration,
204    Assignment,
205    FunctionCall,
206    MethodCall,
207    ArrayAccess,
208    HashAccess,
209    Reference,
210    Dereference,
211    ConditionalExpression,
212    LoopStatement,
213    IfStatement,
214    UnlessStatement,
215    WhileStatement,
216    UntilStatement,
217    ForStatement,
218    ForeachStatement,
219    DoStatement,
220    EvalStatement,
221    RegexMatch,
222    RegexSubstitution,
223    RegexTransliteration,
224
225    // Error and EOF
226    Error,
227    Eof,
228}
229
230impl TokenType for PerlSyntaxKind {
231    const END_OF_STREAM: Self = Self::Eof;
232    type Role = UniversalTokenRole;
233
234    fn role(&self) -> Self::Role {
235        match self {
236            Self::Whitespace | Self::Newline => UniversalTokenRole::Whitespace,
237            Self::Comment => UniversalTokenRole::Comment,
238            Self::Eof => UniversalTokenRole::Eof,
239            _ => UniversalTokenRole::None,
240        }
241    }
242
243    fn is_comment(&self) -> bool {
244        matches!(self, Self::Comment)
245    }
246
247    fn is_whitespace(&self) -> bool {
248        matches!(self, Self::Whitespace | Self::Newline)
249    }
250}
251
252pub type PerlToken = oak_core::Token<PerlSyntaxKind>;
253pub type PerlNode<'a> = oak_core::tree::RedNode<'a, PerlLanguage>;
254
255impl PerlSyntaxKind {
256    pub fn is_token(&self) -> bool {
257        !self.is_element()
258    }
259
260    pub fn is_element(&self) -> bool {
261        matches!(
262            self,
263            Self::Program
264                | Self::Statement
265                | Self::Expression
266                | Self::Block
267                | Self::SubroutineDeclaration
268                | Self::PackageDeclaration
269                | Self::UseStatement
270                | Self::VariableDeclaration
271                | Self::Assignment
272                | Self::FunctionCall
273                | Self::MethodCall
274                | Self::ArrayAccess
275                | Self::HashAccess
276                | Self::Reference
277                | Self::Dereference
278                | Self::ConditionalExpression
279                | Self::LoopStatement
280                | Self::IfStatement
281                | Self::UnlessStatement
282                | Self::WhileStatement
283                | Self::UntilStatement
284                | Self::ForStatement
285                | Self::ForeachStatement
286                | Self::DoStatement
287                | Self::EvalStatement
288                | Self::RegexMatch
289                | Self::RegexSubstitution
290                | Self::RegexTransliteration
291        )
292    }
293}
294
295impl ElementType for PerlSyntaxKind {
296    type Role = UniversalElementRole;
297
298    fn role(&self) -> Self::Role {
299        match self {
300            Self::Error => UniversalElementRole::Error,
301            Self::Program => UniversalElementRole::Root,
302            Self::SubroutineDeclaration | Self::PackageDeclaration | Self::VariableDeclaration => UniversalElementRole::Detail,
303            _ => UniversalElementRole::None,
304        }
305    }
306}