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