The PHP Parser class that build the AST tree from the lexer
Type: Parser
Parameters
lexerast
Properties
lexerLexer current lexer instanceastAST the AST factory instancetoken(Integer | String) current tokenextractDocBoolean should extract documentation as AST nodesuppressErrorsBoolean should ignore parsing errors and continuedebugBoolean should output debug informations
helper : gets a token name
Parameters
token
main entry point : converts a source code to AST
Parameters
codefilename
Raise an error
Parameters
messagemsgExpectexpecttoken
handling errors
Parameters
expect
Creates a new AST node
Parameters
name
expects an end of statement or end of file
Returns boolean
Force the parser to check the current token.
If the current token does not match to expected token, the an error will be raised.
If the suppressError mode is activated, then the error will
be added to the program error stack and this function will return false.
Parameters
Returns boolean
Returns the current token contents
Returns String
consume the next token *
consume comments (if found) *
consume the next token (including doc) *
Check if token is of specified type
Parameters
type
outputs some debug information on current token *
Parse an array
array ::= T_ARRAY '(' array_pair_list ')' |
'[' array_pair_list ']'Reads an array entry item
array_pair_list ::= '&' w_variable |
(
expr (
T_DOUBLE_ARROW (
expr | '&' w_variable
)
)?
) dim_offset ::= expr?reading a class
class ::= class_scope? T_CLASS T_STRING (T_EXTENDS NAMESPACE_NAME)? (T_IMPLEMENTS (NAMESPACE_NAME ',')* NAMESPACE_NAME)? '{' CLASS_BODY '}'Read the class visibility
class_scope ::= (T_FINAL | T_ABSTRACT)?Reads a class body
class_body ::= (member_flags? (T_VAR | T_STRING | T_FUNCTION))*Reads variable list
variable_list ::= (variable_declaration ',')* variable_declarationReads constant list
constant_list ::= T_CONST (constant_declaration ',')* constant_declarationRead member flags
Returns any array 1st index : 0 => public, 1 => protected, 2 => private 2nd index : 0 => instance member, 1 => static member 3rd index : 0 => normal, 1 => abstract member, 2 => final member
reading an interface
interface ::= T_INTERFACE T_STRING (T_EXTENDS (NAMESPACE_NAME ',')* NAMESPACE_NAME)? '{' INTERFACE_BODY '}'Reads an interface body
interface_body ::= (member_flags? (T_CONST | T_FUNCTION))*reading a trait
trait ::= T_TRAIT T_STRING (T_EXTENDS (NAMESPACE_NAME ',')* NAMESPACE_NAME)? '{' FUNCTION* '}'reading a use statement
trait_use_statement ::= namespace_name (',' namespace_name)* ('{' trait_use_alias '}')?Reading trait alias
trait_use_alias ::= namespace_name ( T_DOUBLE_COLON T_STRING )? (T_INSTEADOF namespace_name) | (T_AS member_flags? T_STRING)Reads a variable declaration
variable_declaration ::= T_VARIABLE '=' scalarReads a constant declaration
constant_declaration ::= T_STRING '=' exprReturns Constant 🔗
Comments with // or # or / _ ... _ /
Comments with / *_ ... _ /
Reads an expression
expr ::= @todo new_expr ::= T_NEW (namespace_name function_argument_list) | (T_CLASS ... class declaration)https://github.com/php/php-src/blob/master/Zend/zend_language_parser.y#L850
Reads a class name
class_name_reference ::= namespace_name | variable assignment_list ::= assignment_list_element (',' assignment_list_element?)* assignment_list_element ::= expr | expr T_DOUBLE_ARROW exprchecks if current token is a reference keyword
checks if current token is a variadic keyword
reading a function
function ::= function_declaration code_blockreads a function declaration (without his body)
function_declaration ::= T_FUNCTION '&'? T_STRING '(' parameter_list ')'lexical_var ::= '&'? T_VARIABLEreads a list of parameters
parameter_list ::= (parameter ',')* parameter? parameter ::= type? '&'? T_ELLIPSIS? T_VARIABLE ('=' expr)?Reads a list of arguments
function_argument_list ::= '(' (argument_list (',' argument_list)*)? ')' argument_list ::= T_ELLIPSIS? exprread type hinting
type ::= T_ARRAY | T_CALLABLE | namespace_nameReads an IF statement
if ::= T_IF '(' expr ')' ':' ...reads an if expression : '(' expr ')'
reads an elseif (expr): statements
ignore : if (..) { } /* *./ else { }
Reads a while statement
while ::= T_WHILE (statement | ':' inner_statement_list T_ENDWHILE ';')Returns While
Reads a do / while loop
do ::= T_DO statement T_WHILE '(' expr ')' ';'Returns Do
Read a for incremental loop
for ::= T_FOR '(' for_exprs ';' for_exprs ';' for_exprs ')' for_statement
for_statement ::= statement | ':' inner_statement_list T_ENDFOR ';'
for_exprs ::= expr? (',' expr)*Returns For
Reads a foreach loop
foreach ::= '(' expr T_AS foreach_variable (T_DOUBLE_ARROW foreach_variable)? ')' statementReturns Foreach
Reads a foreach variable statement
foreach_variable = variable |
T_LIST '(' assignment_list ')' |
'[' array_pair_list ']'Returns Expression
start ::= (namespace | top_statement)*Reads a namespace declaration block
namespace ::= T_NAMESPACE namespace_name? '{'
top_statements
'}'
| T_NAMESPACE namespace_name ';' top_statementsReturns Namespace
Reads a namespace name
namespace_name ::= T_NS_SEPARATOR? (T_STRING T_NS_SEPARATOR)* T_STRINGReturns Identifier
Reads a use statement
use_statement ::= T_USE
use_type? use_declarations |
use_type use_statement '{' use_declarations '}' |
use_statement '{' use_declarations(=>typed) '}'
';'Returns UseGroup
Reads a use declaration
use_declaration ::= use_type? namespace_name use_aliasReturns UseItem
Reads a list of use declarations
use_declarations ::= use_declaration (',' use_declaration)*Returns Array<UseItem>
Reads a use statement
use_alias ::= (T_AS T_STRING)?Returns (String | null)
Reads the namespace type declaration
use_type ::= (T_FUNCTION | T_CONST)?Returns (String | null) Possible values : function, const
Unescape special chars
scalar ::= T_MAGIC_CONST
| T_LNUMBER | T_DNUMBER
| T_START_HEREDOC T_ENCAPSED_AND_WHITESPACE? T_END_HEREDOC
| '"' encaps_list '"'
| T_START_HEREDOC encaps_list T_END_HEREDOC
| namespace_name (T_DOUBLE_COLON T_STRING)?Handles the dereferencing
Reads and extracts an encapsed item
encapsed_string_item ::= T_ENCAPSED_AND_WHITESPACE
| T_DOLLAR_OPEN_CURLY_BRACES expr '}'
| T_DOLLAR_OPEN_CURLY_BRACES T_STRING_VARNAME '}'
| T_DOLLAR_OPEN_CURLY_BRACES T_STRING_VARNAME '[' expr ']' '}'
| T_CURLY_OPEN variable '}'
| variable
| variable '[' expr ']'
| variable T_OBJECT_OPERATOR T_STRINGReturns (String | Variable | Expr | Lookup)
Reads an encapsed string
Constant token
reading a list of top statements (helper for top_statement*)
top_statements ::= top_statement*reading a top statement
top_statement ::=
namespace | function | class
| interface | trait
| use_statements | const_list
| statementreads a list of simple inner statements (helper for inner_statement*)
inner_statements ::= inner_statement*Reads a list of constants declaration
const_list ::= T_CONST T_STRING '=' expr (',' T_STRING '=' expr)* ';'Reads a list of constants declaration
declare_list ::= T_STRING '=' expr (',' T_STRING '=' expr)*reads a simple inner statement
inner_statement ::= '{' inner_statements '}' | tokenReads statements
code_block ::= '{' (inner_statements | top_statements) '}'Reads a switch statement
switch ::= T_SWITCH '(' expr ')' switch_case_listReturns Switch
switch_case_list ::= '{' ';'? case_list* '}' | ':' ';'? case_list* T_ENDSWITCH ';' case_list ::= ((T_CASE expr) | T_DEFAULT) (':' | ';') inner_statement* try ::= T_TRY '{' inner_statement* '}'
(
T_CATCH '(' namespace_name variable ')' '{' inner_statement* '}'
)*
(T_FINALLY '{' inner_statement* '}')?Returns Try
Reads a short form of tokens
Parameters
tokenNumber The ending token
Returns Block
Helper : reads a list of tokens / sample : T_STRING ',' T_STRING ...
list ::= separator? ( item separator )* itemReads a list of names separated by a comma
name_list ::= namespace (',' namespace)*Sample code :
<?php class foo extends bar, baz { }Returns Array<Identifier>
Reads a list of variables declarations
variable_declaration ::= T_VARIABLE ('=' expr)?*
variable_declarations ::= variable_declaration (',' variable_declaration)*Sample code :
<?php class foo extends bar, baz { }Returns (Array<Variable> | Array<Assign>) Returns an array composed by a list of variables, or assign values
Reads a variable
variable ::= &? ...complex @todoSome samples of parsed code :
&$var // simple var
$var // simple var
classname::CONST_NAME // dynamic class name with const retrieval
foo() // function call
$var->func()->property // chained calls reference_variable ::= simple_variable ('[' OFFSET ']')* | '{' EXPR '}'
$foo[123]; // foo is an array ==> gets its entry
$foo{1}; // foo is a string ==> get the 2nd char offset
${'foo'}[123]; // get the dynamic var $foo
$foo[123]{1}; // gets the 2nd char from the 123 array entry
simple_variable ::= T_VARIABLE | '$' '{' expr '}' | '$' simple_variable