Skip to main content

oak_php/lexer/
token_type.rs

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