Skip to main content

oak_php/parser/
element_type.rs

1use oak_core::{ElementType, Parser, UniversalElementRole};
2#[cfg(feature = "serde")]
3use serde::{Deserialize, Serialize};
4
5#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
6#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
7pub enum PhpElementType {
8    // Whitespace and newlines
9    Whitespace,
10    Newline,
11
12    // Comments
13    Comment,
14
15    // Literals
16    StringLiteral,
17    NumberLiteral,
18    BooleanLiteral,
19    NullLiteral,
20
21    // Identifiers and keywords
22    Identifier,
23    Variable,
24    Abstract,
25    And,
26    Array,
27    As,
28    Break,
29    Callable,
30    Case,
31    Catch,
32    Class,
33    Clone,
34    Const,
35    Continue,
36    Declare,
37    Default,
38    Do,
39    Echo,
40    Else,
41    Elseif,
42    Empty,
43    Enddeclare,
44    Endfor,
45    Endforeach,
46    Endif,
47    Endswitch,
48    Endwhile,
49    Eval,
50    Exit,
51    Extends,
52    Final,
53    Finally,
54    For,
55    Foreach,
56    Function,
57    Global,
58    Goto,
59    If,
60    Implements,
61    Include,
62    IncludeOnce,
63    Instanceof,
64    Insteadof,
65    Interface,
66    Isset,
67    List,
68    Namespace,
69    New,
70    Or,
71    Print,
72    Private,
73    Protected,
74    Public,
75    Require,
76    RequireOnce,
77    Return,
78    Static,
79    Switch,
80    Throw,
81    Trait,
82    Try,
83    Unset,
84    Use,
85    Var,
86    While,
87    Xor,
88    Yield,
89    YieldFrom,
90
91    // Operators
92    Plus,
93    Minus,
94    Multiply,
95    Divide,
96    Modulo,
97    Power,
98    Concat,
99    Equal,
100    Identical,
101    NotEqual,
102    NotIdentical,
103    Less,
104    Greater,
105    LessEqual,
106    GreaterEqual,
107    Spaceship,
108    LogicalAnd,
109    LogicalOr,
110    LogicalXor,
111    LogicalNot,
112    BitwiseAnd,
113    BitwiseOr,
114    BitwiseXor,
115    BitwiseNot,
116    LeftShift,
117    RightShift,
118    Assign,
119    PlusAssign,
120    MinusAssign,
121    MultiplyAssign,
122    DivideAssign,
123    ModuloAssign,
124    PowerAssign,
125    ConcatAssign,
126    BitwiseAndAssign,
127    BitwiseOrAssign,
128    BitwiseXorAssign,
129    LeftShiftAssign,
130    RightShiftAssign,
131    Increment,
132    Decrement,
133    Arrow,
134    DoubleArrow,
135    NullCoalesce,
136    NullCoalesceAssign,
137    Ellipsis,
138
139    // Punctuations
140    LeftParen,
141    RightParen,
142    LeftBracket,
143    RightBracket,
144    LeftBrace,
145    RightBrace,
146    Semicolon,
147    Comma,
148    Dot,
149    Question,
150    Colon,
151    DoubleColon,
152    Backslash,
153    At,
154    Dollar,
155
156    // PHP special tags
157    OpenTag,
158    CloseTag,
159    EchoTag,
160
161    // Special
162    Eof,
163    Error,
164
165    // Element types
166    Root,
167    ClassDef,
168    FunctionDef,
169    MethodDef,
170    PropertyDef,
171    ConstDef,
172    TraitDef,
173    InterfaceDef,
174    NamespaceDef,
175    UseStatement,
176    IfStatement,
177    WhileStatement,
178    DoWhileStatement,
179    ForStatement,
180    ForeachStatement,
181    SwitchStatement,
182    TryStatement,
183    CatchBlock,
184    FinallyBlock,
185    ExpressionStatement,
186    ReturnStatement,
187    ThrowStatement,
188    BreakStatement,
189    ContinueStatement,
190    EchoStatement,
191    GlobalStatement,
192    StaticStatement,
193    UnsetStatement,
194    CompoundStatement,
195
196    // Expressions
197    Literal,
198    ParenthesizedExpression,
199    CallExpression,
200    ArrayAccessExpression,
201    MemberAccessExpression,
202    BinaryExpression,
203}
204
205impl ElementType for PhpElementType {
206    type Role = UniversalElementRole;
207
208    fn role(&self) -> Self::Role {
209        match self {
210            _ => UniversalElementRole::None,
211        }
212    }
213}
214
215impl From<crate::lexer::token_type::PhpTokenType> for PhpElementType {
216    fn from(token: crate::lexer::token_type::PhpTokenType) -> Self {
217        match token {
218            // Whitespace and newlines
219            crate::lexer::token_type::PhpTokenType::Whitespace => PhpElementType::Whitespace,
220            crate::lexer::token_type::PhpTokenType::Newline => PhpElementType::Newline,
221
222            // Comments
223            crate::lexer::token_type::PhpTokenType::Comment => PhpElementType::Comment,
224
225            // Literals
226            crate::lexer::token_type::PhpTokenType::StringLiteral => PhpElementType::StringLiteral,
227            crate::lexer::token_type::PhpTokenType::NumberLiteral => PhpElementType::NumberLiteral,
228            crate::lexer::token_type::PhpTokenType::BooleanLiteral => PhpElementType::BooleanLiteral,
229            crate::lexer::token_type::PhpTokenType::NullLiteral => PhpElementType::NullLiteral,
230
231            // Identifiers and keywords
232            crate::lexer::token_type::PhpTokenType::Identifier => PhpElementType::Identifier,
233            crate::lexer::token_type::PhpTokenType::Variable => PhpElementType::Variable,
234            crate::lexer::token_type::PhpTokenType::Abstract => PhpElementType::Abstract,
235            crate::lexer::token_type::PhpTokenType::And => PhpElementType::And,
236            crate::lexer::token_type::PhpTokenType::Array => PhpElementType::Array,
237            crate::lexer::token_type::PhpTokenType::As => PhpElementType::As,
238            crate::lexer::token_type::PhpTokenType::Break => PhpElementType::Break,
239            crate::lexer::token_type::PhpTokenType::Callable => PhpElementType::Callable,
240            crate::lexer::token_type::PhpTokenType::Case => PhpElementType::Case,
241            crate::lexer::token_type::PhpTokenType::Catch => PhpElementType::Catch,
242            crate::lexer::token_type::PhpTokenType::Class => PhpElementType::Class,
243            crate::lexer::token_type::PhpTokenType::Clone => PhpElementType::Clone,
244            crate::lexer::token_type::PhpTokenType::Const => PhpElementType::Const,
245            crate::lexer::token_type::PhpTokenType::Continue => PhpElementType::Continue,
246            crate::lexer::token_type::PhpTokenType::Declare => PhpElementType::Declare,
247            crate::lexer::token_type::PhpTokenType::Default => PhpElementType::Default,
248            crate::lexer::token_type::PhpTokenType::Do => PhpElementType::Do,
249            crate::lexer::token_type::PhpTokenType::Echo => PhpElementType::Echo,
250            crate::lexer::token_type::PhpTokenType::Else => PhpElementType::Else,
251            crate::lexer::token_type::PhpTokenType::Elseif => PhpElementType::Elseif,
252            crate::lexer::token_type::PhpTokenType::Empty => PhpElementType::Empty,
253            crate::lexer::token_type::PhpTokenType::Enddeclare => PhpElementType::Enddeclare,
254            crate::lexer::token_type::PhpTokenType::Endfor => PhpElementType::Endfor,
255            crate::lexer::token_type::PhpTokenType::Endforeach => PhpElementType::Endforeach,
256            crate::lexer::token_type::PhpTokenType::Endif => PhpElementType::Endif,
257            crate::lexer::token_type::PhpTokenType::Endswitch => PhpElementType::Endswitch,
258            crate::lexer::token_type::PhpTokenType::Endwhile => PhpElementType::Endwhile,
259            crate::lexer::token_type::PhpTokenType::Eval => PhpElementType::Eval,
260            crate::lexer::token_type::PhpTokenType::Exit => PhpElementType::Exit,
261            crate::lexer::token_type::PhpTokenType::Extends => PhpElementType::Extends,
262            crate::lexer::token_type::PhpTokenType::Final => PhpElementType::Final,
263            crate::lexer::token_type::PhpTokenType::Finally => PhpElementType::Finally,
264            crate::lexer::token_type::PhpTokenType::For => PhpElementType::For,
265            crate::lexer::token_type::PhpTokenType::Foreach => PhpElementType::Foreach,
266            crate::lexer::token_type::PhpTokenType::Function => PhpElementType::Function,
267            crate::lexer::token_type::PhpTokenType::Global => PhpElementType::Global,
268            crate::lexer::token_type::PhpTokenType::Goto => PhpElementType::Goto,
269            crate::lexer::token_type::PhpTokenType::If => PhpElementType::If,
270            crate::lexer::token_type::PhpTokenType::Implements => PhpElementType::Implements,
271            crate::lexer::token_type::PhpTokenType::Include => PhpElementType::Include,
272            crate::lexer::token_type::PhpTokenType::IncludeOnce => PhpElementType::IncludeOnce,
273            crate::lexer::token_type::PhpTokenType::Instanceof => PhpElementType::Instanceof,
274            crate::lexer::token_type::PhpTokenType::Insteadof => PhpElementType::Insteadof,
275            crate::lexer::token_type::PhpTokenType::Interface => PhpElementType::Interface,
276            crate::lexer::token_type::PhpTokenType::Isset => PhpElementType::Isset,
277            crate::lexer::token_type::PhpTokenType::List => PhpElementType::List,
278            crate::lexer::token_type::PhpTokenType::Namespace => PhpElementType::Namespace,
279            crate::lexer::token_type::PhpTokenType::New => PhpElementType::New,
280            crate::lexer::token_type::PhpTokenType::Or => PhpElementType::Or,
281            crate::lexer::token_type::PhpTokenType::Print => PhpElementType::Print,
282            crate::lexer::token_type::PhpTokenType::Private => PhpElementType::Private,
283            crate::lexer::token_type::PhpTokenType::Protected => PhpElementType::Protected,
284            crate::lexer::token_type::PhpTokenType::Public => PhpElementType::Public,
285            crate::lexer::token_type::PhpTokenType::Require => PhpElementType::Require,
286            crate::lexer::token_type::PhpTokenType::RequireOnce => PhpElementType::RequireOnce,
287            crate::lexer::token_type::PhpTokenType::Return => PhpElementType::Return,
288            crate::lexer::token_type::PhpTokenType::Static => PhpElementType::Static,
289            crate::lexer::token_type::PhpTokenType::Switch => PhpElementType::Switch,
290            crate::lexer::token_type::PhpTokenType::Throw => PhpElementType::Throw,
291            crate::lexer::token_type::PhpTokenType::Trait => PhpElementType::Trait,
292            crate::lexer::token_type::PhpTokenType::Try => PhpElementType::Try,
293            crate::lexer::token_type::PhpTokenType::Unset => PhpElementType::Unset,
294            crate::lexer::token_type::PhpTokenType::Use => PhpElementType::Use,
295            crate::lexer::token_type::PhpTokenType::Var => PhpElementType::Var,
296            crate::lexer::token_type::PhpTokenType::While => PhpElementType::While,
297            crate::lexer::token_type::PhpTokenType::Xor => PhpElementType::Xor,
298            crate::lexer::token_type::PhpTokenType::Yield => PhpElementType::Yield,
299            crate::lexer::token_type::PhpTokenType::YieldFrom => PhpElementType::YieldFrom,
300
301            // Operators
302            crate::lexer::token_type::PhpTokenType::Plus => PhpElementType::Plus,
303            crate::lexer::token_type::PhpTokenType::Minus => PhpElementType::Minus,
304            crate::lexer::token_type::PhpTokenType::Multiply => PhpElementType::Multiply,
305            crate::lexer::token_type::PhpTokenType::Divide => PhpElementType::Divide,
306            crate::lexer::token_type::PhpTokenType::Modulo => PhpElementType::Modulo,
307            crate::lexer::token_type::PhpTokenType::Power => PhpElementType::Power,
308            crate::lexer::token_type::PhpTokenType::Concat => PhpElementType::Concat,
309            crate::lexer::token_type::PhpTokenType::Equal => PhpElementType::Equal,
310            crate::lexer::token_type::PhpTokenType::Identical => PhpElementType::Identical,
311            crate::lexer::token_type::PhpTokenType::NotEqual => PhpElementType::NotEqual,
312            crate::lexer::token_type::PhpTokenType::NotIdentical => PhpElementType::NotIdentical,
313            crate::lexer::token_type::PhpTokenType::Less => PhpElementType::Less,
314            crate::lexer::token_type::PhpTokenType::Greater => PhpElementType::Greater,
315            crate::lexer::token_type::PhpTokenType::LessEqual => PhpElementType::LessEqual,
316            crate::lexer::token_type::PhpTokenType::GreaterEqual => PhpElementType::GreaterEqual,
317            crate::lexer::token_type::PhpTokenType::Spaceship => PhpElementType::Spaceship,
318            crate::lexer::token_type::PhpTokenType::LogicalAnd => PhpElementType::LogicalAnd,
319            crate::lexer::token_type::PhpTokenType::LogicalOr => PhpElementType::LogicalOr,
320            crate::lexer::token_type::PhpTokenType::LogicalXor => PhpElementType::LogicalXor,
321            crate::lexer::token_type::PhpTokenType::LogicalNot => PhpElementType::LogicalNot,
322            crate::lexer::token_type::PhpTokenType::BitwiseAnd => PhpElementType::BitwiseAnd,
323            crate::lexer::token_type::PhpTokenType::BitwiseOr => PhpElementType::BitwiseOr,
324            crate::lexer::token_type::PhpTokenType::BitwiseXor => PhpElementType::BitwiseXor,
325            crate::lexer::token_type::PhpTokenType::BitwiseNot => PhpElementType::BitwiseNot,
326            crate::lexer::token_type::PhpTokenType::LeftShift => PhpElementType::LeftShift,
327            crate::lexer::token_type::PhpTokenType::RightShift => PhpElementType::RightShift,
328            crate::lexer::token_type::PhpTokenType::Assign => PhpElementType::Assign,
329            crate::lexer::token_type::PhpTokenType::PlusAssign => PhpElementType::PlusAssign,
330            crate::lexer::token_type::PhpTokenType::MinusAssign => PhpElementType::MinusAssign,
331            crate::lexer::token_type::PhpTokenType::MultiplyAssign => PhpElementType::MultiplyAssign,
332            crate::lexer::token_type::PhpTokenType::DivideAssign => PhpElementType::DivideAssign,
333            crate::lexer::token_type::PhpTokenType::ModuloAssign => PhpElementType::ModuloAssign,
334            crate::lexer::token_type::PhpTokenType::PowerAssign => PhpElementType::PowerAssign,
335            crate::lexer::token_type::PhpTokenType::ConcatAssign => PhpElementType::ConcatAssign,
336            crate::lexer::token_type::PhpTokenType::BitwiseAndAssign => PhpElementType::BitwiseAndAssign,
337            crate::lexer::token_type::PhpTokenType::BitwiseOrAssign => PhpElementType::BitwiseOrAssign,
338            crate::lexer::token_type::PhpTokenType::BitwiseXorAssign => PhpElementType::BitwiseXorAssign,
339            crate::lexer::token_type::PhpTokenType::LeftShiftAssign => PhpElementType::LeftShiftAssign,
340            crate::lexer::token_type::PhpTokenType::RightShiftAssign => PhpElementType::RightShiftAssign,
341            crate::lexer::token_type::PhpTokenType::Increment => PhpElementType::Increment,
342            crate::lexer::token_type::PhpTokenType::Decrement => PhpElementType::Decrement,
343            crate::lexer::token_type::PhpTokenType::Arrow => PhpElementType::Arrow,
344            crate::lexer::token_type::PhpTokenType::DoubleArrow => PhpElementType::DoubleArrow,
345            crate::lexer::token_type::PhpTokenType::NullCoalesce => PhpElementType::NullCoalesce,
346            crate::lexer::token_type::PhpTokenType::NullCoalesceAssign => PhpElementType::NullCoalesceAssign,
347            crate::lexer::token_type::PhpTokenType::Ellipsis => PhpElementType::Ellipsis,
348
349            // Punctuations
350            crate::lexer::token_type::PhpTokenType::LeftParen => PhpElementType::LeftParen,
351            crate::lexer::token_type::PhpTokenType::RightParen => PhpElementType::RightParen,
352            crate::lexer::token_type::PhpTokenType::LeftBracket => PhpElementType::LeftBracket,
353            crate::lexer::token_type::PhpTokenType::RightBracket => PhpElementType::RightBracket,
354            crate::lexer::token_type::PhpTokenType::LeftBrace => PhpElementType::LeftBrace,
355            crate::lexer::token_type::PhpTokenType::RightBrace => PhpElementType::RightBrace,
356            crate::lexer::token_type::PhpTokenType::Semicolon => PhpElementType::Semicolon,
357            crate::lexer::token_type::PhpTokenType::Comma => PhpElementType::Comma,
358            crate::lexer::token_type::PhpTokenType::Dot => PhpElementType::Dot,
359            crate::lexer::token_type::PhpTokenType::Question => PhpElementType::Question,
360            crate::lexer::token_type::PhpTokenType::Colon => PhpElementType::Colon,
361            crate::lexer::token_type::PhpTokenType::DoubleColon => PhpElementType::DoubleColon,
362            crate::lexer::token_type::PhpTokenType::Backslash => PhpElementType::Backslash,
363            crate::lexer::token_type::PhpTokenType::At => PhpElementType::At,
364            crate::lexer::token_type::PhpTokenType::Dollar => PhpElementType::Dollar,
365
366            // PHP special tags
367            crate::lexer::token_type::PhpTokenType::OpenTag => PhpElementType::OpenTag,
368            crate::lexer::token_type::PhpTokenType::CloseTag => PhpElementType::CloseTag,
369            crate::lexer::token_type::PhpTokenType::EchoTag => PhpElementType::EchoTag,
370
371            // Special
372            crate::lexer::token_type::PhpTokenType::Eof => PhpElementType::Eof,
373            crate::lexer::token_type::PhpTokenType::Error => PhpElementType::Error,
374
375            // Element types
376            crate::lexer::token_type::PhpTokenType::Root => PhpElementType::Root,
377            crate::lexer::token_type::PhpTokenType::ClassDef => PhpElementType::ClassDef,
378            crate::lexer::token_type::PhpTokenType::FunctionDef => PhpElementType::FunctionDef,
379            crate::lexer::token_type::PhpTokenType::MethodDef => PhpElementType::MethodDef,
380            crate::lexer::token_type::PhpTokenType::PropertyDef => PhpElementType::PropertyDef,
381            crate::lexer::token_type::PhpTokenType::ConstDef => PhpElementType::ConstDef,
382            crate::lexer::token_type::PhpTokenType::TraitDef => PhpElementType::TraitDef,
383            crate::lexer::token_type::PhpTokenType::InterfaceDef => PhpElementType::InterfaceDef,
384            crate::lexer::token_type::PhpTokenType::NamespaceDef => PhpElementType::NamespaceDef,
385            crate::lexer::token_type::PhpTokenType::UseStatement => PhpElementType::UseStatement,
386            crate::lexer::token_type::PhpTokenType::IfStatement => PhpElementType::IfStatement,
387            crate::lexer::token_type::PhpTokenType::WhileStatement => PhpElementType::WhileStatement,
388            crate::lexer::token_type::PhpTokenType::DoWhileStatement => PhpElementType::DoWhileStatement,
389            crate::lexer::token_type::PhpTokenType::ForStatement => PhpElementType::ForStatement,
390            crate::lexer::token_type::PhpTokenType::ForeachStatement => PhpElementType::ForeachStatement,
391            crate::lexer::token_type::PhpTokenType::SwitchStatement => PhpElementType::SwitchStatement,
392            crate::lexer::token_type::PhpTokenType::TryStatement => PhpElementType::TryStatement,
393            crate::lexer::token_type::PhpTokenType::CatchBlock => PhpElementType::CatchBlock,
394            crate::lexer::token_type::PhpTokenType::FinallyBlock => PhpElementType::FinallyBlock,
395            crate::lexer::token_type::PhpTokenType::ExpressionStatement => PhpElementType::ExpressionStatement,
396            crate::lexer::token_type::PhpTokenType::ReturnStatement => PhpElementType::ReturnStatement,
397            crate::lexer::token_type::PhpTokenType::ThrowStatement => PhpElementType::ThrowStatement,
398            crate::lexer::token_type::PhpTokenType::BreakStatement => PhpElementType::BreakStatement,
399            crate::lexer::token_type::PhpTokenType::ContinueStatement => PhpElementType::ContinueStatement,
400            crate::lexer::token_type::PhpTokenType::EchoStatement => PhpElementType::EchoStatement,
401            crate::lexer::token_type::PhpTokenType::GlobalStatement => PhpElementType::GlobalStatement,
402            crate::lexer::token_type::PhpTokenType::StaticStatement => PhpElementType::StaticStatement,
403            crate::lexer::token_type::PhpTokenType::UnsetStatement => PhpElementType::UnsetStatement,
404            crate::lexer::token_type::PhpTokenType::CompoundStatement => PhpElementType::CompoundStatement,
405
406            // Expressions
407            crate::lexer::token_type::PhpTokenType::Literal => PhpElementType::Literal,
408            crate::lexer::token_type::PhpTokenType::ParenthesizedExpression => PhpElementType::ParenthesizedExpression,
409            crate::lexer::token_type::PhpTokenType::CallExpression => PhpElementType::CallExpression,
410            crate::lexer::token_type::PhpTokenType::ArrayAccessExpression => PhpElementType::ArrayAccessExpression,
411            crate::lexer::token_type::PhpTokenType::MemberAccessExpression => PhpElementType::MemberAccessExpression,
412            crate::lexer::token_type::PhpTokenType::BinaryExpression => PhpElementType::BinaryExpression,
413        }
414    }
415}