Copied!
Abstract
Methods
public __construct(PhpParser\Lexer $lexer, ?PhpParser\PhpVersion $phpVersion = NULL)
 

Creates a parser instance.

Options:

  • phpVersion: ?PhpVersion,
  • param \Lexer $lexer A lexer
  • param \PhpVersion $phpVersion PHP version to target, defaults to latest supported. This option is best-effort: Even if specified, parsing will generally assume the latest supported version and only adjust behavior in minor ways, for example by omitting errors in older versions and interpreting type hints as a name or identifier depending on version.
public getTokens() : array
public parse(string $code, ?PhpParser\ErrorHandler $errorHandler = NULL) : ?array
 

Parses PHP code into a node tree.

If a non-throwing error handler is used, the parser will continue parsing after an error occurred and attempt to build a partial AST.

  • param string $code The source code to parse
  • param \ErrorHandler|null $errorHandler Error handler to use for lexer/parser errors, defaults to ErrorHandler\Throwing.
  • return \Node\Stmt[]|null Array of statements (or null non-throwing error handler is used and the parser was unable to recover from an error).
Properties
protected array $action
 
  • var int[] Table of actions. Indexed according to $actionBase comment.
protected array $actionBase
 
  • var int[] Map of states to a displacement into the $action table. The corresponding action for this state/symbol pair is $action[$actionBase[$state] + $symbol]. If $actionBase[$state] is 0, the action is defaulted, i.e. $actionDefault[$state] should be used instead.
protected array $actionCheck
 
  • var int[] Table indexed analogously to $action. If $actionCheck[$actionBase[$state] + $symbol] != $symbol then the action is defaulted, i.e. $actionDefault[$state] should be used instead.
protected array $actionDefault
 
  • var int[] Map of states to their default action
protected int $actionTableSize
 
  • var int Size of $action table
protected ?SplObjectStorage $createdArrays
 
  • var \SplObjectStorage<\Array_,null>|null Array nodes created during parsing, for postprocessing of empty elements.
protected int $defaultAction
 
  • var int Action number signifying default action
protected array $dropTokens
 
  • var array<int,bool> Map of PHP token IDs to drop
protected PhpParser\ErrorHandler $errorHandler
 
  • var \ErrorHandler Error handler
protected int $errorState
 
  • var int Error state, used to avoid error floods
protected int $errorSymbol
 
  • var int Symbol number of error recovery token
protected array $goto
 
  • var int[] Table of states to goto after reduction. Indexed according to $gotoBase comment.
protected array $gotoBase
 
  • var int[] Map of non-terminals to a displacement into the $goto table. The corresponding goto state for this non-terminal/state pair is $goto[$gotoBase[$nonTerminal] + $state] (unless defaulted)
protected array $gotoCheck
 
  • var int[] Table indexed analogously to $goto. If $gotoCheck[$gotoBase[$nonTerminal] + $state] != $nonTerminal then the goto state is defaulted, i.e. $gotoDefault[$nonTerminal] should be used.
protected array $gotoDefault
 
  • var int[] Map of non-terminals to the default state to goto after their reduction
protected int $gotoTableSize
 
  • var int Size of $goto table
protected int $invalidSymbol
 
  • var int Symbol number signifying an invalid token
protected PhpParser\Lexer $lexer
 
  • var \Lexer Lexer that is used when parsing
protected int $numNonLeafStates
 
  • var int Number of non-leaf states
protected array $phpTokenToSymbol
 
  • var int[] Map of PHP token IDs to internal symbols
protected PhpParser\PhpVersion $phpVersion
 
  • var \PhpVersion PHP version to target on a best-effort basis
protected array $productions
 
  • var array<int,string> Names of the production rules (only necessary for debugging)
protected array $reduceCallbacks
 
  • var callable[] Semantic action callbacks
protected array $ruleToLength
 
  • var int[] Map of rules to the length of their right-hand side, which is the number of elements that have to be popped from the stack(s) on reduction.
protected array $ruleToNonTerminal
 
  • var int[] Map of rules to the non-terminal on their left-hand side, i.e. the non-terminal to use for determining the state to goto after reduction.
protected array $semStack
 
  • var array Semantic value stack (contains values of tokens and semantic action results)
protected $semValue = NULL
 
  • var mixed Temporary value containing the result of last semantic action (reduction)
protected array $symbolToName
 
  • var string[] Map of symbols to their names
protected array $tokenEndStack
 
  • var int[] Token end position stack
protected int $tokenPos
 
  • var int Current position in token array
protected array $tokens
 
  • var \Token[] Tokens for the current parse
protected array $tokenStartStack
 
  • var int[] Token start position stack
protected array $tokenToSymbol
 
  • var int[] Map of external symbols (static::T_*) to internal symbols
protected int $tokenToSymbolMapSize
 
  • var int Size of $tokenToSymbol map
protected int $unexpectedTokenRule
 
  • var int Rule number signifying that an unexpected token was encountered
protected int $YY2TBLSTATE
Methods
protected addPropertyNameToHooks(PhpParser\Node $node) : void
 
  • param \Property|\Param $node
protected checkClass(PhpParser\Node\Stmt\Class_ $node, int $namePos) : void
protected checkClassConst(PhpParser\Node\Stmt\ClassConst $node, int $modifierPos) : void
protected checkClassMethod(PhpParser\Node\Stmt\ClassMethod $node, int $modifierPos) : void
protected checkClassModifier(int $a, int $b, int $modifierPos) : void
protected checkEmptyPropertyHookList(array $hooks, int $hookPos) : void
 
  • param \PropertyHook[] $hooks
protected checkEnum(PhpParser\Node\Stmt\Enum_ $node, int $namePos) : void
protected checkInterface(PhpParser\Node\Stmt\Interface_ $node, int $namePos) : void
protected checkModifier(int $a, int $b, int $modifierPos) : void
protected checkNamespace(PhpParser\Node\Stmt\Namespace_ $node) : void
protected checkParam(PhpParser\Node\Param $node) : void
protected checkPropertyHook(PhpParser\Node\PropertyHook $hook, ?int $paramListPos) : void
protected checkPropertyHookModifiers(int $a, int $b, int $modifierPos) : void
protected checkPropertyHooksForMultiProperty(PhpParser\Node\Stmt\Property $property, int $hookPos) : void
protected checkTryCatch(PhpParser\Node\Stmt\TryCatch $node) : void
protected checkUseUse(PhpParser\Node\UseItem $node, int $namePos) : void
protected createCommentFromToken(PhpParser\Token $token, int $tokenPos) : PhpParser\Comment
protected createEmptyElemAttributes(int $tokenPos) : array
 
  • return array<string,mixed>
protected createExitExpr(string $name, int $namePos, array $args, array $attrs) : PhpParser\Node\Expr
 
  • param (\Node\Arg|\Node\VariadicPlaceholder)[] $args
  • param array<string,mixed> $attrs
protected createTokenMap() : array
 

Creates the token map.

The token map maps the PHP internal token identifiers to the identifiers used by the Parser. Additionally it maps T_OPEN_TAG_WITH_ECHO to T_ECHO and T_CLOSE_TAG to ';'.

  • return array<int,int> The token map
protected doParse() : ?array
 
  • return \Stmt[]|null
protected emitError(PhpParser\Error $error) : void
protected fixupAlternativeElse( $node) : void
 
  • param \ElseIf_|\Else_ $node
protected fixupArrayDestructuring(PhpParser\Node\Expr\Array_ $node) : PhpParser\Node\Expr\List_
protected getAttributes(int $tokenStartPos, int $tokenEndPos) : array
 

Get attributes for a node with the given start and end token positions.

  • param int $tokenStartPos Token position the node starts at
  • param int $tokenEndPos Token position the node ends at
  • return array<string,mixed> Attributes
protected getAttributesAt(int $stackPos) : array
 

Get combined start and end attributes at a stack location

  • param int $stackPos Stack location
  • return array<string,mixed> Combined start and end attributes
protected getAttributesForToken(int $tokenPos) : array
 

Get attributes for a single token at the given token position.

  • return array<string,mixed> Attributes
protected getCommentBeforeToken(int $tokenPos) : ?PhpParser\Comment
 

Get last comment before the given token position, if any

protected getErrorMessage(int $symbol, int $state) : string
 

Format error message including expected tokens.

  • param int $symbol Unexpected symbol
  • param int $state State at time of error
  • return string Formatted error message
protected getExpectedTokens(int $state) : array
 

Get limited number of expected tokens in given state.

  • param int $state State
  • return string[] Expected tokens. If too many, an empty array is returned.
protected getFloatCastKind(string $cast) : int
protected handleBuiltinTypes(PhpParser\Node\Name $name)
 
  • return \Name|\Identifier
protected handleHaltCompiler() : string
protected handleNamespaces(array $stmts) : array
 

Moves statements of semicolon-style namespaces into $ns->stmts and checks various error conditions.

  • param \Node\Stmt[] $stmts
  • return \Node\Stmt[]
protected abstract initReduceCallbacks() : void
 

Initialize $reduceCallbacks map.

protected inlineHtmlHasLeadingNewline(int $stackPos) : bool
protected maybeCreateNop(int $tokenStartPos, int $tokenEndPos) : ?PhpParser\Node\Stmt\Nop
protected maybeCreateZeroLengthNop(int $tokenPos) : ?PhpParser\Node\Stmt\Nop
 

Create a zero-length nop to capture preceding comments, if any.

protected parseDocString(string $startToken, $contents, string $endToken, array $attributes, array $endTokenAttributes, bool $parseUnicodeEscape) : PhpParser\Node\Expr
 
  • param string|(\Expr|\InterpolatedStringPart)[] $contents
  • param array<string,mixed> $attributes
  • param array<string,mixed> $endTokenAttributes
protected parseLNumber(string $str, array $attributes, bool $allowInvalidOctal = false) : PhpParser\Node\Scalar\Int_
 
  • param array<string,mixed> $attributes
protected parseNumString(string $str, array $attributes)
 

Parse a T_NUM_STRING token into either an integer or string node.

  • param string $str Number string
  • param array<string,mixed> $attributes Attributes
  • return \Int_|\String_ Integer or string node.
protected postprocessList(PhpParser\Node\Expr\List_ $node) : void
protected stripIndentation(string $string, int $indentLen, string $indentChar, bool $newlineAtStart, bool $newlineAtEnd, array $attributes) : string
 
  • param array<string,mixed> $attributes
Constants
private PhpParser\ParserAbstract::SYMBOL_NONE = -1
Methods
private checkClassName(?PhpParser\Node\Identifier $name, int $namePos) : void
private checkImplementedInterfaces(array $interfaces) : void
 
  • param \Name[] $interfaces
private fixupNamespaceAttributes(PhpParser\Node\Stmt\Namespace_ $stmt) : void
private getNamespaceErrorAttributes(PhpParser\Node\Stmt\Namespace_ $node) : array
 
  • return array<string,mixed>
private getNamespacingStyle(array $stmts) : ?string
 

Determine namespacing style (semicolon or brace)

  • param \Node[] $stmts Top-level statements.
  • return null|string One of "semicolon", "brace" or null (no namespaces)
private isSimpleExit(array $args) : bool
 
  • param (\Node\Arg|\Node\VariadicPlaceholder)[] $args
© 2025 Bruce Wells
Search Namespaces \ Classes
Configuration