Skip to main content

oak_php/lexer/
token_type.rs

1use oak_core::{Source, Token, TokenType, UniversalElementRole, UniversalTokenRole};
2#[cfg(feature = "serde")]
3use serde::{Deserialize, Serialize};
4
5pub type PhpToken = Token<PhpTokenType>;
6
7impl PhpTokenType {
8    /// Checks if this syntax kind represents a token (leaf node).
9    pub fn is_token(&self) -> bool {
10        !self.is_element()
11    }
12}
13
14impl PhpTokenType {
15    /// Checks if this syntax kind represents a composite element (non-leaf node).
16    pub fn is_element(&self) -> bool {
17        matches!(
18            self,
19            Self::Root
20                | Self::ClassDef
21                | Self::FunctionDef
22                | Self::MethodDef
23                | Self::PropertyDef
24                | Self::ConstDef
25                | Self::TraitDef
26                | Self::InterfaceDef
27                | Self::NamespaceDef
28                | Self::UseStatement
29                | Self::IfStatement
30                | Self::WhileStatement
31                | Self::DoWhileStatement
32                | Self::ForStatement
33                | Self::ForeachStatement
34                | Self::SwitchStatement
35                | Self::TryStatement
36                | Self::CatchBlock
37                | Self::FinallyBlock
38                | Self::ExpressionStatement
39                | Self::ReturnStatement
40                | Self::ThrowStatement
41                | Self::BreakStatement
42                | Self::ContinueStatement
43                | Self::EchoStatement
44                | Self::GlobalStatement
45                | Self::StaticStatement
46                | Self::UnsetStatement
47                | Self::CompoundStatement
48                | Self::Literal
49                | Self::ParenthesizedExpression
50                | Self::CallExpression
51                | Self::ArrayAccessExpression
52                | Self::MemberAccessExpression
53                | Self::BinaryExpression
54        )
55    }
56}
57
58impl TokenType for PhpTokenType {
59    type Role = UniversalTokenRole;
60    const END_OF_STREAM: Self = Self::Error;
61
62    fn is_ignored(&self) -> bool {
63        false
64    }
65
66    fn role(&self) -> Self::Role {
67        match self {
68            _ => UniversalTokenRole::None,
69        }
70    }
71}
72
73#[derive(Copy, Clone, Debug, PartialEq, Eq, Hash)]
74#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
75pub enum PhpTokenType {
76    // Whitespace and newlines
77    Whitespace,
78    Newline,
79
80    // Comments
81    Comment,
82
83    // Literals
84    StringLiteral,
85    NumberLiteral,
86    BooleanLiteral,
87    NullLiteral,
88
89    // Identifiers and keywords
90    Identifier,
91    Variable,
92    Abstract,
93    And,
94    Array,
95    As,
96    Break,
97    Callable,
98    Case,
99    Catch,
100    Class,
101    Clone,
102    Const,
103    Continue,
104    Declare,
105    Default,
106    Do,
107    Echo,
108    Else,
109    Elseif,
110    Empty,
111    Enddeclare,
112    Endfor,
113    Endforeach,
114    Endif,
115    Endswitch,
116    Endwhile,
117    Eval,
118    Exit,
119    Extends,
120    Final,
121    Finally,
122    For,
123    Foreach,
124    Function,
125    Global,
126    Goto,
127    If,
128    Implements,
129    Include,
130    IncludeOnce,
131    Instanceof,
132    Insteadof,
133    Interface,
134    Isset,
135    List,
136    Namespace,
137    New,
138    Or,
139    Print,
140    Private,
141    Protected,
142    Public,
143    Require,
144    RequireOnce,
145    Return,
146    Static,
147    Switch,
148    Throw,
149    Trait,
150    Try,
151    Unset,
152    Use,
153    Var,
154    While,
155    Xor,
156    Yield,
157    YieldFrom,
158
159    // Operators
160    Plus,
161    Minus,
162    Multiply,
163    Divide,
164    Modulo,
165    Power,
166    Concat,
167    Equal,
168    Identical,
169    NotEqual,
170    NotIdentical,
171    Less,
172    Greater,
173    LessEqual,
174    GreaterEqual,
175    Spaceship,
176    LogicalAnd,
177    LogicalOr,
178    LogicalXor,
179    LogicalNot,
180    BitwiseAnd,
181    BitwiseOr,
182    BitwiseXor,
183    BitwiseNot,
184    LeftShift,
185    RightShift,
186    Assign,
187    PlusAssign,
188    MinusAssign,
189    MultiplyAssign,
190    DivideAssign,
191    ModuloAssign,
192    PowerAssign,
193    ConcatAssign,
194    BitwiseAndAssign,
195    BitwiseOrAssign,
196    BitwiseXorAssign,
197    LeftShiftAssign,
198    RightShiftAssign,
199    Increment,
200    Decrement,
201    Arrow,
202    DoubleArrow,
203    NullCoalesce,
204    NullCoalesceAssign,
205    Ellipsis,
206
207    // Punctuations
208    LeftParen,
209    RightParen,
210    LeftBracket,
211    RightBracket,
212    LeftBrace,
213    RightBrace,
214    Semicolon,
215    Comma,
216    Dot,
217    Question,
218    Colon,
219    DoubleColon,
220    Backslash,
221    At,
222    Dollar,
223
224    // PHP special tags
225    OpenTag,
226    CloseTag,
227    EchoTag,
228
229    // Special
230    Eof,
231    Error,
232
233    // Element types
234    Root,
235    ClassDef,
236    FunctionDef,
237    MethodDef,
238    PropertyDef,
239    ConstDef,
240    TraitDef,
241    InterfaceDef,
242    NamespaceDef,
243    UseStatement,
244    IfStatement,
245    WhileStatement,
246    DoWhileStatement,
247    ForStatement,
248    ForeachStatement,
249    SwitchStatement,
250    TryStatement,
251    CatchBlock,
252    FinallyBlock,
253    ExpressionStatement,
254    ReturnStatement,
255    ThrowStatement,
256    BreakStatement,
257    ContinueStatement,
258    EchoStatement,
259    GlobalStatement,
260    StaticStatement,
261    UnsetStatement,
262    CompoundStatement,
263
264    // Expressions
265    Literal,
266    ParenthesizedExpression,
267    CallExpression,
268    ArrayAccessExpression,
269    MemberAccessExpression,
270    BinaryExpression,
271}