ParserAbstract
in package
implements
Parser
Table of Contents
Interfaces
Constants
- SYMBOL_NONE = -1
Properties
- $action : array<string|int, int>
- $actionBase : array<string|int, int>
- $actionCheck : array<string|int, int>
- $actionDefault : array<string|int, int>
- $actionTableSize : int
- $createdArrays : SplObjectStorage<Array_, null>|null
- $defaultAction : int
- $dropTokens : array<int, bool>
- $errorHandler : ErrorHandler
- $errorState : int
- $errorSymbol : int
- $goto : array<string|int, int>
- $gotoBase : array<string|int, int>
- $gotoCheck : array<string|int, int>
- $gotoDefault : array<string|int, int>
- $gotoTableSize : int
- $invalidSymbol : int
- $lexer : Lexer
- $numNonLeafStates : int
- $phpTokenToSymbol : array<string|int, int>
- $phpVersion : PhpVersion
- $productions : array<int, string>
- $reduceCallbacks : array<string|int, callable>
- $ruleToLength : array<string|int, int>
- $ruleToNonTerminal : array<string|int, int>
- $semStack : array<string|int, mixed>
- $semValue : mixed
- $symbolToName : array<string|int, string>
- $tokenEndStack : array<string|int, int>
- $tokenPos : int
- $tokens : array<string|int, Token>
- $tokenStartStack : array<string|int, int>
- $tokenToSymbol : array<string|int, int>
- $tokenToSymbolMapSize : int
- $unexpectedTokenRule : int
- $YY2TBLSTATE : int
Methods
- __construct() : mixed
- Creates a parser instance.
- getTokens() : array<string|int, Token>
- Return tokens for the last parse.
- parse() : array<string|int, Stmt>|null
- Parses PHP code into a node tree.
- checkClass() : void
- checkClassConst() : void
- checkClassMethod() : void
- checkClassModifier() : void
- checkEnum() : void
- checkInterface() : void
- checkModifier() : void
- checkNamespace() : void
- checkParam() : void
- checkPropertyHook() : void
- checkPropertyHookList() : void
- checkPropertyHookModifiers() : void
- checkTryCatch() : void
- checkUseUse() : void
- createCommentFromToken() : Comment
- createEmptyElemAttributes() : array<string, mixed>
- createExitExpr() : Expr
- createTokenMap() : array<int, int>
- Creates the token map.
- doParse() : array<string|int, Stmt>|null
- emitError() : void
- fixupAlternativeElse() : void
- fixupArrayDestructuring() : List_
- getAttributes() : array<string, mixed>
- Get attributes for a node with the given start and end token positions.
- getAttributesAt() : array<string, mixed>
- Get combined start and end attributes at a stack location
- getAttributesForToken() : array<string, mixed>
- Get attributes for a single token at the given token position.
- getCommentBeforeToken() : Comment|null
- Get last comment before the given token position, if any
- getErrorMessage() : string
- Format error message including expected tokens.
- getExpectedTokens() : array<string|int, string>
- Get limited number of expected tokens in given state.
- getFloatCastKind() : int
- handleBuiltinTypes() : Name|Identifier
- handleHaltCompiler() : string
- handleNamespaces() : array<string|int, Stmt>
- Moves statements of semicolon-style namespaces into $ns->stmts and checks various error conditions.
- initReduceCallbacks() : void
- Initialize $reduceCallbacks map.
- inlineHtmlHasLeadingNewline() : bool
- maybeCreateNop() : Nop|null
- maybeCreateZeroLengthNop() : Nop|null
- Create a zero-length nop to capture preceding comments, if any.
- parseDocString() : Expr
- parseLNumber() : Int_
- parseNumString() : Int_|String_
- Parse a T_NUM_STRING token into either an integer or string node.
- postprocessList() : void
- stripIndentation() : string
- checkClassName() : void
- checkImplementedInterfaces() : void
- fixupNamespaceAttributes() : void
- getNamespaceErrorAttributes() : array<string, mixed>
- getNamespacingStyle() : null|string
- Determine namespacing style (semicolon or brace)
- isSimpleExit() : bool
Constants
SYMBOL_NONE
private
mixed
SYMBOL_NONE
= -1
Properties
$action
protected
array<string|int, int>
$action
Table of actions. Indexed according to $actionBase comment.
$actionBase
protected
array<string|int, int>
$actionBase
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.
$actionCheck
protected
array<string|int, int>
$actionCheck
Table indexed analogously to $action. If $actionCheck[$actionBase[$state] + $symbol] != $symbol then the action is defaulted, i.e. $actionDefault[$state] should be used instead.
$actionDefault
protected
array<string|int, int>
$actionDefault
Map of states to their default action
$actionTableSize
protected
int
$actionTableSize
Size of $action table
$createdArrays
protected
SplObjectStorage<Array_, null>|null
$createdArrays
Array nodes created during parsing, for postprocessing of empty elements.
$defaultAction
protected
int
$defaultAction
Action number signifying default action
$dropTokens
protected
array<int, bool>
$dropTokens
Map of PHP token IDs to drop
$errorHandler
protected
ErrorHandler
$errorHandler
Error handler
$errorState
protected
int
$errorState
Error state, used to avoid error floods
$errorSymbol
protected
int
$errorSymbol
Symbol number of error recovery token
$goto
protected
array<string|int, int>
$goto
Table of states to goto after reduction. Indexed according to $gotoBase comment.
$gotoBase
protected
array<string|int, int>
$gotoBase
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)
$gotoCheck
protected
array<string|int, int>
$gotoCheck
Table indexed analogously to $goto. If $gotoCheck[$gotoBase[$nonTerminal] + $state] != $nonTerminal then the goto state is defaulted, i.e. $gotoDefault[$nonTerminal] should be used.
$gotoDefault
protected
array<string|int, int>
$gotoDefault
Map of non-terminals to the default state to goto after their reduction
$gotoTableSize
protected
int
$gotoTableSize
Size of $goto table
$invalidSymbol
protected
int
$invalidSymbol
Symbol number signifying an invalid token
$lexer
protected
Lexer
$lexer
Lexer that is used when parsing
$numNonLeafStates
protected
int
$numNonLeafStates
Number of non-leaf states
$phpTokenToSymbol
protected
array<string|int, int>
$phpTokenToSymbol
Map of PHP token IDs to internal symbols
$phpVersion
protected
PhpVersion
$phpVersion
PHP version to target on a best-effort basis
$productions
protected
array<int, string>
$productions
Names of the production rules (only necessary for debugging)
$reduceCallbacks
protected
array<string|int, callable>
$reduceCallbacks
Semantic action callbacks
$ruleToLength
protected
array<string|int, int>
$ruleToLength
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.
$ruleToNonTerminal
protected
array<string|int, int>
$ruleToNonTerminal
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.
$semStack
protected
array<string|int, mixed>
$semStack
Semantic value stack (contains values of tokens and semantic action results)
$semValue
protected
mixed
$semValue
Temporary value containing the result of last semantic action (reduction)
$symbolToName
protected
array<string|int, string>
$symbolToName
Map of symbols to their names
$tokenEndStack
protected
array<string|int, int>
$tokenEndStack
Token end position stack
$tokenPos
protected
int
$tokenPos
Current position in token array
$tokens
protected
array<string|int, Token>
$tokens
Tokens for the current parse
$tokenStartStack
protected
array<string|int, int>
$tokenStartStack
Token start position stack
$tokenToSymbol
protected
array<string|int, int>
$tokenToSymbol
Map of external symbols (static::T_*) to internal symbols
$tokenToSymbolMapSize
protected
int
$tokenToSymbolMapSize
Size of $tokenToSymbol map
$unexpectedTokenRule
protected
int
$unexpectedTokenRule
Rule number signifying that an unexpected token was encountered
$YY2TBLSTATE
protected
int
$YY2TBLSTATE
Methods
__construct()
Creates a parser instance.
public
__construct(Lexer $lexer[, PhpVersion $phpVersion = null ]) : mixed
Options:
- phpVersion: ?PhpVersion,
Parameters
- $lexer : Lexer
-
A lexer
- $phpVersion : PhpVersion = null
-
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.
getTokens()
Return tokens for the last parse.
public
getTokens() : array<string|int, Token>
Return values
array<string|int, Token>parse()
Parses PHP code into a node tree.
public
parse(string $code[, ErrorHandler|null $errorHandler = null ]) : array<string|int, Stmt>|null
If a non-throwing error handler is used, the parser will continue parsing after an error occurred and attempt to build a partial AST.
Parameters
- $code : string
-
The source code to parse
- $errorHandler : ErrorHandler|null = null
-
Error handler to use for lexer/parser errors, defaults to ErrorHandler\Throwing.
Return values
array<string|int, Stmt>|null —Array of statements (or null non-throwing error handler is used and the parser was unable to recover from an error).
checkClass()
protected
checkClass(Class_ $node, int $namePos) : void
Parameters
- $node : Class_
- $namePos : int
checkClassConst()
protected
checkClassConst(ClassConst $node, int $modifierPos) : void
Parameters
- $node : ClassConst
- $modifierPos : int
checkClassMethod()
protected
checkClassMethod(ClassMethod $node, int $modifierPos) : void
Parameters
- $node : ClassMethod
- $modifierPos : int
checkClassModifier()
protected
checkClassModifier(int $a, int $b, int $modifierPos) : void
Parameters
- $a : int
- $b : int
- $modifierPos : int
checkEnum()
protected
checkEnum(Enum_ $node, int $namePos) : void
Parameters
- $node : Enum_
- $namePos : int
checkInterface()
protected
checkInterface(Interface_ $node, int $namePos) : void
Parameters
- $node : Interface_
- $namePos : int
checkModifier()
protected
checkModifier(int $a, int $b, int $modifierPos) : void
Parameters
- $a : int
- $b : int
- $modifierPos : int
checkNamespace()
protected
checkNamespace(Namespace_ $node) : void
Parameters
- $node : Namespace_
checkParam()
protected
checkParam(Param $node) : void
Parameters
- $node : Param
checkPropertyHook()
protected
checkPropertyHook(PropertyHook $hook, int|null $paramListPos) : void
Parameters
- $hook : PropertyHook
- $paramListPos : int|null
checkPropertyHookList()
protected
checkPropertyHookList(array<string|int, PropertyHook> $hooks, int $hookPos) : void
Parameters
- $hooks : array<string|int, PropertyHook>
- $hookPos : int
checkPropertyHookModifiers()
protected
checkPropertyHookModifiers(int $a, int $b, int $modifierPos) : void
Parameters
- $a : int
- $b : int
- $modifierPos : int
checkTryCatch()
protected
checkTryCatch(TryCatch $node) : void
Parameters
- $node : TryCatch
checkUseUse()
protected
checkUseUse(UseItem $node, int $namePos) : void
Parameters
- $node : UseItem
- $namePos : int
createCommentFromToken()
protected
createCommentFromToken(Token $token, int $tokenPos) : Comment
Parameters
- $token : Token
- $tokenPos : int
Return values
CommentcreateEmptyElemAttributes()
protected
createEmptyElemAttributes(int $tokenPos) : array<string, mixed>
Parameters
- $tokenPos : int
Return values
array<string, mixed>createExitExpr()
protected
createExitExpr(string $name, int $namePos, array<string|int, Arg|VariadicPlaceholder> $args, array<string, mixed> $attrs) : Expr
Parameters
- $name : string
- $namePos : int
- $args : array<string|int, Arg|VariadicPlaceholder>
- $attrs : array<string, mixed>
Return values
ExprcreateTokenMap()
Creates the token map.
protected
createTokenMap() : array<int, int>
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 values
array<int, int> —The token map
doParse()
protected
doParse() : array<string|int, Stmt>|null
Return values
array<string|int, Stmt>|nullemitError()
protected
emitError(Error $error) : void
Parameters
- $error : Error
fixupAlternativeElse()
protected
fixupAlternativeElse(ElseIf_|Else_ $node) : void
Parameters
fixupArrayDestructuring()
protected
fixupArrayDestructuring(Array_ $node) : List_
Parameters
- $node : Array_
Return values
List_getAttributes()
Get attributes for a node with the given start and end token positions.
protected
getAttributes(int $tokenStartPos, int $tokenEndPos) : array<string, mixed>
Parameters
- $tokenStartPos : int
-
Token position the node starts at
- $tokenEndPos : int
-
Token position the node ends at
Return values
array<string, mixed> —Attributes
getAttributesAt()
Get combined start and end attributes at a stack location
protected
getAttributesAt(int $stackPos) : array<string, mixed>
Parameters
- $stackPos : int
-
Stack location
Return values
array<string, mixed> —Combined start and end attributes
getAttributesForToken()
Get attributes for a single token at the given token position.
protected
getAttributesForToken(int $tokenPos) : array<string, mixed>
Parameters
- $tokenPos : int
Return values
array<string, mixed> —Attributes
getCommentBeforeToken()
Get last comment before the given token position, if any
protected
getCommentBeforeToken(int $tokenPos) : Comment|null
Parameters
- $tokenPos : int
Return values
Comment|nullgetErrorMessage()
Format error message including expected tokens.
protected
getErrorMessage(int $symbol, int $state) : string
Parameters
- $symbol : int
-
Unexpected symbol
- $state : int
-
State at time of error
Return values
string —Formatted error message
getExpectedTokens()
Get limited number of expected tokens in given state.
protected
getExpectedTokens(int $state) : array<string|int, string>
Parameters
- $state : int
-
State
Return values
array<string|int, string> —Expected tokens. If too many, an empty array is returned.
getFloatCastKind()
protected
getFloatCastKind(string $cast) : int
Parameters
- $cast : string
Return values
inthandleBuiltinTypes()
protected
handleBuiltinTypes(Name $name) : Name|Identifier
Parameters
- $name : Name
Return values
Name|IdentifierhandleHaltCompiler()
protected
handleHaltCompiler() : string
Return values
stringhandleNamespaces()
Moves statements of semicolon-style namespaces into $ns->stmts and checks various error conditions.
protected
handleNamespaces(array<string|int, Stmt> $stmts) : array<string|int, Stmt>
Parameters
- $stmts : array<string|int, Stmt>
Return values
array<string|int, Stmt>initReduceCallbacks()
Initialize $reduceCallbacks map.
protected
abstract initReduceCallbacks() : void
inlineHtmlHasLeadingNewline()
protected
inlineHtmlHasLeadingNewline(int $stackPos) : bool
Parameters
- $stackPos : int
Return values
boolmaybeCreateNop()
protected
maybeCreateNop(int $tokenStartPos, int $tokenEndPos) : Nop|null
Parameters
- $tokenStartPos : int
- $tokenEndPos : int
Return values
Nop|nullmaybeCreateZeroLengthNop()
Create a zero-length nop to capture preceding comments, if any.
protected
maybeCreateZeroLengthNop(int $tokenPos) : Nop|null
Parameters
- $tokenPos : int
Return values
Nop|nullparseDocString()
protected
parseDocString(string $startToken, string|array<string|int, Expr|InterpolatedStringPart> $contents, string $endToken, array<string, mixed> $attributes, array<string, mixed> $endTokenAttributes, bool $parseUnicodeEscape) : Expr
Parameters
- $startToken : string
- $contents : string|array<string|int, Expr|InterpolatedStringPart>
- $endToken : string
- $attributes : array<string, mixed>
- $endTokenAttributes : array<string, mixed>
- $parseUnicodeEscape : bool
Return values
ExprparseLNumber()
protected
parseLNumber(string $str, array<string, mixed> $attributes[, bool $allowInvalidOctal = false ]) : Int_
Parameters
- $str : string
- $attributes : array<string, mixed>
- $allowInvalidOctal : bool = false
Return values
Int_parseNumString()
Parse a T_NUM_STRING token into either an integer or string node.
protected
parseNumString(string $str, array<string, mixed> $attributes) : Int_|String_
Parameters
- $str : string
-
Number string
- $attributes : array<string, mixed>
-
Attributes
Return values
Int_|String_ —Integer or string node.
postprocessList()
protected
postprocessList(List_ $node) : void
Parameters
- $node : List_
stripIndentation()
protected
stripIndentation(string $string, int $indentLen, string $indentChar, bool $newlineAtStart, bool $newlineAtEnd, array<string, mixed> $attributes) : string
Parameters
- $string : string
- $indentLen : int
- $indentChar : string
- $newlineAtStart : bool
- $newlineAtEnd : bool
- $attributes : array<string, mixed>
Return values
stringcheckClassName()
private
checkClassName(Identifier|null $name, int $namePos) : void
Parameters
- $name : Identifier|null
- $namePos : int
checkImplementedInterfaces()
private
checkImplementedInterfaces(array<string|int, Name> $interfaces) : void
Parameters
- $interfaces : array<string|int, Name>
fixupNamespaceAttributes()
private
fixupNamespaceAttributes(Namespace_ $stmt) : void
Parameters
- $stmt : Namespace_
getNamespaceErrorAttributes()
private
getNamespaceErrorAttributes(Namespace_ $node) : array<string, mixed>
Parameters
- $node : Namespace_
Return values
array<string, mixed>getNamespacingStyle()
Determine namespacing style (semicolon or brace)
private
getNamespacingStyle(array<string|int, Node> $stmts) : null|string
Parameters
- $stmts : array<string|int, Node>
-
Top-level statements.
Return values
null|string —One of "semicolon", "brace" or null (no namespaces)
isSimpleExit()
private
isSimpleExit(array<string|int, Arg|VariadicPlaceholder> $args) : bool
Parameters
- $args : array<string|int, Arg|VariadicPlaceholder>