Skip to main content

oak_php/lexer/
token_type.rs

1use oak_core::{Token, TokenType, UniversalTokenRole};
2
3/// Token type for PHP
4pub type PhpToken = Token<PhpTokenType>;
5
6impl PhpTokenType {
7    /// Checks if this syntax kind represents a token (leaf node).
8    pub fn is_token(&self) -> bool {
9        !self.is_element()
10    }
11}
12
13impl PhpTokenType {
14    /// Checks if this syntax kind represents a composite element (non-leaf node).
15    pub fn is_element(&self) -> bool {
16        matches!(
17            self,
18            Self::Root
19                | Self::ClassDef
20                | Self::FunctionDef
21                | Self::MethodDef
22                | Self::PropertyDef
23                | Self::ConstDef
24                | Self::TraitDef
25                | Self::InterfaceDef
26                | Self::NamespaceDef
27                | Self::UseStatement
28                | Self::IfStatement
29                | Self::WhileStatement
30                | Self::DoWhileStatement
31                | Self::ForStatement
32                | Self::ForeachStatement
33                | Self::SwitchStatement
34                | Self::TryStatement
35                | Self::CatchBlock
36                | Self::FinallyBlock
37                | Self::ExpressionStatement
38                | Self::ReturnStatement
39                | Self::ThrowStatement
40                | Self::BreakStatement
41                | Self::ContinueStatement
42                | Self::EchoStatement
43                | Self::GlobalStatement
44                | Self::StaticStatement
45                | Self::UnsetStatement
46                | Self::CompoundStatement
47                | Self::Literal
48                | Self::ParenthesizedExpression
49                | Self::CallExpression
50                | Self::ArrayAccessExpression
51                | Self::MemberAccessExpression
52                | Self::BinaryExpression
53        )
54    }
55}
56
57impl TokenType for PhpTokenType {
58    type Role = UniversalTokenRole;
59    const END_OF_STREAM: Self = Self::Eof;
60
61    fn is_ignored(&self) -> bool {
62        matches!(self, Self::Whitespace | Self::Comment)
63    }
64
65    fn role(&self) -> Self::Role {
66        match self {
67            Self::Whitespace | Self::Newline => UniversalTokenRole::Whitespace,
68            Self::Comment => UniversalTokenRole::Comment,
69            Self::Identifier | Self::Variable => UniversalTokenRole::Name,
70            Self::StringLiteral | Self::NumberLiteral => UniversalTokenRole::Literal,
71            Self::BooleanLiteral | Self::NullLiteral => UniversalTokenRole::Literal,
72            Self::Eof => UniversalTokenRole::Eof,
73            Self::Error => UniversalTokenRole::Error,
74            _ if self.is_keyword() => UniversalTokenRole::Keyword,
75            _ => UniversalTokenRole::None,
76        }
77    }
78}
79
80impl PhpTokenType {
81    fn is_keyword(&self) -> bool {
82        matches!(
83            self,
84            Self::Abstract
85                | Self::And
86                | Self::Array
87                | Self::As
88                | Self::Break
89                | Self::Callable
90                | Self::Case
91                | Self::Catch
92                | Self::Class
93                | Self::Clone
94                | Self::Const
95                | Self::Continue
96                | Self::Declare
97                | Self::Default
98                | Self::Do
99                | Self::Echo
100                | Self::Else
101                | Self::Elseif
102                | Self::Empty
103                | Self::Enddeclare
104                | Self::Endfor
105                | Self::Endforeach
106                | Self::Endif
107                | Self::Endswitch
108                | Self::Endwhile
109                | Self::Eval
110                | Self::Exit
111                | Self::Extends
112                | Self::Final
113                | Self::Finally
114                | Self::For
115                | Self::Foreach
116                | Self::Function
117                | Self::Global
118                | Self::Goto
119                | Self::If
120                | Self::Implements
121                | Self::Include
122                | Self::IncludeOnce
123                | Self::Instanceof
124                | Self::Insteadof
125                | Self::Interface
126                | Self::Isset
127                | Self::List
128                | Self::Namespace
129                | Self::New
130                | Self::Or
131                | Self::Print
132                | Self::Private
133                | Self::Protected
134                | Self::Public
135                | Self::Require
136                | Self::RequireOnce
137                | Self::Return
138                | Self::Static
139                | Self::Switch
140                | Self::Throw
141                | Self::Trait
142                | Self::Try
143                | Self::Unset
144                | Self::Use
145                | Self::Var
146                | Self::While
147                | Self::Xor
148                | Self::Yield
149                | Self::YieldFrom
150        )
151    }
152}
153
154/// Enum representing all possible token types in PHP
155#[derive(Copy, Clone, Debug, PartialEq, Eq, Hash)]
156#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
157pub enum PhpTokenType {
158    /// Whitespace characters
159    Whitespace,
160    /// Newline characters
161    Newline,
162
163    /// Comment
164    Comment,
165
166    /// String literal
167    StringLiteral,
168    /// Number literal
169    NumberLiteral,
170    /// Boolean literal (true, false)
171    BooleanLiteral,
172    /// Null literal
173    NullLiteral,
174
175    /// Identifier
176    Identifier,
177    /// Variable identifier (starting with $)
178    Variable,
179    /// 'abstract' keyword
180    Abstract,
181    /// 'and' operator
182    And,
183    /// 'array' keyword/type
184    Array,
185    /// 'as' keyword
186    As,
187    /// 'break' keyword
188    Break,
189    /// 'callable' type
190    Callable,
191    /// 'case' keyword
192    Case,
193    /// 'catch' keyword
194    Catch,
195    /// 'class' keyword
196    Class,
197    /// 'clone' keyword
198    Clone,
199    /// 'const' keyword
200    Const,
201    /// 'continue' keyword
202    Continue,
203    /// 'declare' keyword
204    Declare,
205    /// 'default' keyword
206    Default,
207    /// 'do' keyword
208    Do,
209    /// 'echo' keyword
210    Echo,
211    /// 'else' keyword
212    Else,
213    /// 'elseif' keyword
214    Elseif,
215    /// 'empty' keyword
216    Empty,
217    /// 'enddeclare' keyword
218    Enddeclare,
219    /// 'endfor' keyword
220    Endfor,
221    /// 'endforeach' keyword
222    Endforeach,
223    /// 'endif' keyword
224    Endif,
225    /// 'endswitch' keyword
226    Endswitch,
227    /// 'endwhile' keyword
228    Endwhile,
229    /// 'eval' keyword
230    Eval,
231    /// 'exit' keyword
232    Exit,
233    /// 'extends' keyword
234    Extends,
235    /// 'final' keyword
236    Final,
237    /// 'finally' keyword
238    Finally,
239    /// 'for' keyword
240    For,
241    /// 'foreach' keyword
242    Foreach,
243    /// 'function' keyword
244    Function,
245    /// 'global' keyword
246    Global,
247    /// 'goto' keyword
248    Goto,
249    /// 'if' keyword
250    If,
251    /// 'implements' keyword
252    Implements,
253    /// 'include' keyword
254    Include,
255    /// 'include_once' keyword
256    IncludeOnce,
257    /// 'instanceof' operator
258    Instanceof,
259    /// 'insteadof' keyword
260    Insteadof,
261    /// 'interface' keyword
262    Interface,
263    /// 'isset' keyword
264    Isset,
265    /// 'list' keyword
266    List,
267    /// 'namespace' keyword
268    Namespace,
269    /// 'new' keyword
270    New,
271    /// 'or' operator
272    Or,
273    /// 'print' keyword
274    Print,
275    /// 'private' keyword
276    Private,
277    /// 'protected' keyword
278    Protected,
279    /// 'public' keyword
280    Public,
281    /// 'require' keyword
282    Require,
283    /// 'require_once' keyword
284    RequireOnce,
285    /// 'return' keyword
286    Return,
287    /// 'static' keyword
288    Static,
289    /// 'switch' keyword
290    Switch,
291    /// 'throw' keyword
292    Throw,
293    /// 'trait' keyword
294    Trait,
295    /// 'try' keyword
296    Try,
297    /// 'unset' keyword
298    Unset,
299    /// 'use' keyword
300    Use,
301    /// 'var' keyword
302    Var,
303    /// 'while' keyword
304    While,
305    /// 'xor' operator
306    Xor,
307    /// 'yield' keyword
308    Yield,
309    /// 'yield from' keyword
310    YieldFrom,
311
312    /// Plus operator (+)
313    Plus,
314    /// Minus operator (-)
315    Minus,
316    /// Multiply operator (*)
317    Multiply,
318    /// Divide operator (/)
319    Divide,
320    /// Modulo operator (%)
321    Modulo,
322    /// Power operator (**)
323    Power,
324    /// Concatenation operator (.)
325    Concat,
326    /// Equality operator (==)
327    Equal,
328    /// Identity operator (===)
329    Identical,
330    /// Inequality operator (!= or <>)
331    NotEqual,
332    /// Non-identity operator (!==)
333    NotIdentical,
334    /// Less than operator (<)
335    Less,
336    /// Greater than operator (>)
337    Greater,
338    /// Less than or equal operator (<=)
339    LessEqual,
340    /// Greater than or equal operator (>=)
341    GreaterEqual,
342    /// Spaceship operator (<=>)
343    Spaceship,
344    /// Logical AND operator (&&)
345    LogicalAnd,
346    /// Logical OR operator (||)
347    LogicalOr,
348    /// Logical XOR operator
349    LogicalXor,
350    /// Logical NOT operator (!)
351    LogicalNot,
352    /// Bitwise AND operator (&)
353    BitwiseAnd,
354    /// Bitwise OR operator (|)
355    BitwiseOr,
356    /// Bitwise XOR operator (^)
357    BitwiseXor,
358    /// Bitwise NOT operator (~)
359    BitwiseNot,
360    /// Left shift operator (<<)
361    LeftShift,
362    /// Right shift operator (>>)
363    RightShift,
364    /// Assignment operator (=)
365    Assign,
366    /// Plus assignment operator (+=)
367    PlusAssign,
368    /// Minus assignment operator (-=)
369    MinusAssign,
370    /// Multiply assignment operator (*=)
371    MultiplyAssign,
372    /// Divide assignment operator (/=)
373    DivideAssign,
374    /// Modulo assignment operator (%=)
375    ModuloAssign,
376    /// Power assignment operator (**=)
377    PowerAssign,
378    /// Concatenation assignment operator (.=)
379    ConcatAssign,
380    /// Bitwise AND assignment operator (&=)
381    BitwiseAndAssign,
382    /// Bitwise OR assignment operator (|=)
383    BitwiseOrAssign,
384    /// Bitwise XOR assignment operator (^=)
385    BitwiseXorAssign,
386    /// Left shift assignment operator (<<=)
387    LeftShiftAssign,
388    /// Right shift assignment operator (>>=)
389    RightShiftAssign,
390    /// Increment operator (++)
391    Increment,
392    /// Decrement operator (--)
393    Decrement,
394    /// Object member access operator (->)
395    Arrow,
396    /// Array element arrow (=>)
397    DoubleArrow,
398    /// Null coalescing operator (??)
399    NullCoalesce,
400    /// Null coalescing assignment operator (??=)
401    NullCoalesceAssign,
402    /// Ellipsis operator (...)
403    Ellipsis,
404
405    /// Left parenthesis (()
406    LeftParen,
407    /// Right parenthesis ())
408    RightParen,
409    /// Left bracket ([)
410    LeftBracket,
411    /// Right bracket (])
412    RightBracket,
413    /// Left brace ({)
414    LeftBrace,
415    /// Right brace (})
416    RightBrace,
417    /// Semicolon (;)
418    Semicolon,
419    /// Comma (,)
420    Comma,
421    /// Dot operator (.)
422    Dot,
423    /// Question mark (?)
424    Question,
425    /// Colon operator (:)
426    Colon,
427    /// Scope resolution operator (::)
428    DoubleColon,
429    /// Backslash (\)
430    Backslash,
431    /// Error suppression operator (@)
432    At,
433    /// Dollar sign ($)
434    Dollar,
435
436    /// PHP opening tag (<?php)
437    OpenTag,
438    /// PHP closing tag (?>)
439    CloseTag,
440    /// PHP echo tag (<?=)
441    EchoTag,
442
443    /// End of file
444    Eof,
445    /// Error token
446    Error,
447
448    /// Root node of the document
449    Root,
450    /// Class definition
451    ClassDef,
452    /// Function definition
453    FunctionDef,
454    /// Method definition
455    MethodDef,
456    /// Property definition
457    PropertyDef,
458    /// Constant definition
459    ConstDef,
460    /// Trait definition
461    TraitDef,
462    /// Interface definition
463    InterfaceDef,
464    /// Namespace definition
465    NamespaceDef,
466    /// Use statement
467    UseStatement,
468    /// If statement
469    IfStatement,
470    /// While statement
471    WhileStatement,
472    /// Do-while statement
473    DoWhileStatement,
474    /// For statement
475    ForStatement,
476    /// Foreach statement
477    ForeachStatement,
478    /// Switch statement
479    SwitchStatement,
480    /// Try statement
481    TryStatement,
482    /// Catch block
483    CatchBlock,
484    /// Finally block
485    FinallyBlock,
486    /// Expression statement
487    ExpressionStatement,
488    /// Return statement
489    ReturnStatement,
490    /// Throw statement
491    ThrowStatement,
492    /// Break statement
493    BreakStatement,
494    /// Continue statement
495    ContinueStatement,
496    /// Echo statement
497    EchoStatement,
498    /// Global statement
499    GlobalStatement,
500    /// Static statement
501    StaticStatement,
502    /// Unset statement
503    UnsetStatement,
504    /// Compound statement (block)
505    CompoundStatement,
506
507    /// Literal expression
508    Literal,
509    /// Parenthesized expression
510    ParenthesizedExpression,
511    /// Function or method call
512    CallExpression,
513    /// Array access expression
514    ArrayAccessExpression,
515    /// Member access expression
516    MemberAccessExpression,
517    /// Binary expression
518    BinaryExpression,
519}