2014-04-20 00:08:59 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace PhpParser;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* This parser is based on a skeleton written by Moriyoshi Koizumi, which in
|
|
|
|
* turn is based on work by Masato Bito.
|
|
|
|
*/
|
2015-06-13 20:16:09 +02:00
|
|
|
use PhpParser\Node\Name;
|
|
|
|
|
2015-06-20 11:47:25 +02:00
|
|
|
abstract class ParserAbstract implements Parser
|
2014-04-20 00:08:59 +02:00
|
|
|
{
|
2014-04-20 13:16:54 +02:00
|
|
|
const SYMBOL_NONE = -1;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* The following members will be filled with generated parsing data:
|
|
|
|
*/
|
|
|
|
|
|
|
|
/** @var int Size of $tokenToSymbol map */
|
|
|
|
protected $tokenToSymbolMapSize;
|
|
|
|
/** @var int Size of $action table */
|
|
|
|
protected $actionTableSize;
|
|
|
|
/** @var int Size of $goto table */
|
|
|
|
protected $gotoTableSize;
|
|
|
|
|
|
|
|
/** @var int Symbol number signifying an invalid token */
|
|
|
|
protected $invalidSymbol;
|
2015-02-02 23:25:34 +01:00
|
|
|
/** @var int Symbol number of error recovery token */
|
|
|
|
protected $errorSymbol;
|
2014-04-20 13:16:54 +02:00
|
|
|
/** @var int Action number signifying default action */
|
|
|
|
protected $defaultAction;
|
|
|
|
/** @var int Rule number signifying that an unexpected token was encountered */
|
|
|
|
protected $unexpectedTokenRule;
|
|
|
|
|
|
|
|
protected $YY2TBLSTATE;
|
|
|
|
protected $YYNLSTATES;
|
|
|
|
|
|
|
|
/** @var array Map of lexer tokens to internal symbols */
|
|
|
|
protected $tokenToSymbol;
|
|
|
|
/** @var array Map of symbols to their names */
|
|
|
|
protected $symbolToName;
|
|
|
|
/** @var array Names of the production rules (only necessary for debugging) */
|
|
|
|
protected $productions;
|
|
|
|
|
|
|
|
/** @var array 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 $actionBase;
|
|
|
|
/** @var array Table of actions. Indexed according to $actionBase comment. */
|
|
|
|
protected $action;
|
|
|
|
/** @var array 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 $actionCheck;
|
|
|
|
/** @var array Map of states to their default action */
|
|
|
|
protected $actionDefault;
|
|
|
|
|
|
|
|
/** @var array 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 $gotoBase;
|
|
|
|
/** @var array Table of states to goto after reduction. Indexed according to $gotoBase comment. */
|
|
|
|
protected $goto;
|
|
|
|
/** @var array 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 $gotoCheck;
|
|
|
|
/** @var array Map of non-terminals to the default state to goto after their reduction */
|
|
|
|
protected $gotoDefault;
|
|
|
|
|
|
|
|
/** @var array 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 $ruleToNonTerminal;
|
|
|
|
/** @var array 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 $ruleToLength;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* The following members are part of the parser state:
|
|
|
|
*/
|
|
|
|
|
|
|
|
/** @var Lexer Lexer that is used when parsing */
|
2014-04-20 00:08:59 +02:00
|
|
|
protected $lexer;
|
2014-04-20 13:16:54 +02:00
|
|
|
/** @var mixed Temporary value containing the result of last semantic action (reduction) */
|
|
|
|
protected $semValue;
|
|
|
|
/** @var int Position in stacks (state stack, semantic value stack, attribute stack) */
|
|
|
|
protected $stackPos;
|
2015-04-22 15:57:29 +02:00
|
|
|
/** @var array Semantic value stack (contains values of tokens and semantic action results) */
|
|
|
|
protected $semStack;
|
|
|
|
/** @var array[] Start attribute stack */
|
|
|
|
protected $startAttributeStack;
|
|
|
|
/** @var array End attributes of last *shifted* token */
|
|
|
|
protected $endAttributes;
|
2016-04-05 11:53:50 +02:00
|
|
|
/** @var array Start attributes of last *read* token */
|
|
|
|
protected $lookaheadStartAttributes;
|
2014-04-20 00:08:59 +02:00
|
|
|
|
2015-02-02 23:25:34 +01:00
|
|
|
/** @var bool Whether to throw on first error */
|
|
|
|
protected $throwOnError;
|
|
|
|
/** @var Error[] Errors collected during last parse */
|
|
|
|
protected $errors;
|
|
|
|
|
2014-04-20 00:08:59 +02:00
|
|
|
/**
|
|
|
|
* Creates a parser instance.
|
|
|
|
*
|
|
|
|
* @param Lexer $lexer A lexer
|
2015-04-30 17:37:19 +02:00
|
|
|
* @param array $options Options array. The boolean 'throwOnError' option determines whether an exception should be
|
2015-02-02 23:25:34 +01:00
|
|
|
* thrown on first error, or if the parser should try to continue parsing the remaining code
|
|
|
|
* and build a partial AST.
|
2014-04-20 00:08:59 +02:00
|
|
|
*/
|
2015-02-02 23:25:34 +01:00
|
|
|
public function __construct(Lexer $lexer, array $options = array()) {
|
2014-04-20 00:08:59 +02:00
|
|
|
$this->lexer = $lexer;
|
2015-02-02 23:25:34 +01:00
|
|
|
$this->errors = array();
|
|
|
|
$this->throwOnError = isset($options['throwOnError']) ? $options['throwOnError'] : true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get array of errors that occurred during the last parse.
|
|
|
|
*
|
2015-04-30 17:37:19 +02:00
|
|
|
* This method may only return multiple errors if the 'throwOnError' option is disabled.
|
2015-02-02 23:25:34 +01:00
|
|
|
*
|
|
|
|
* @return Error[]
|
|
|
|
*/
|
|
|
|
public function getErrors() {
|
|
|
|
return $this->errors;
|
2014-04-20 00:08:59 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Parses PHP code into a node tree.
|
|
|
|
*
|
|
|
|
* @param string $code The source code to parse
|
|
|
|
*
|
2015-04-30 17:37:19 +02:00
|
|
|
* @return Node[]|null Array of statements (or null if the 'throwOnError' option is disabled and the parser was
|
|
|
|
* unable to recover from an error).
|
2014-04-20 00:08:59 +02:00
|
|
|
*/
|
|
|
|
public function parse($code) {
|
|
|
|
$this->lexer->startLexing($code);
|
2015-02-02 23:25:34 +01:00
|
|
|
$this->errors = array();
|
2014-04-20 00:08:59 +02:00
|
|
|
|
|
|
|
// We start off with no lookahead-token
|
2014-04-20 13:16:54 +02:00
|
|
|
$symbol = self::SYMBOL_NONE;
|
2014-04-20 00:08:59 +02:00
|
|
|
|
|
|
|
// The attributes for a node are taken from the first and last token of the node.
|
|
|
|
// From the first token only the startAttributes are taken and from the last only
|
|
|
|
// the endAttributes. Both are merged using the array union operator (+).
|
2015-04-26 22:32:18 +02:00
|
|
|
$startAttributes = '*POISON';
|
|
|
|
$endAttributes = '*POISON';
|
|
|
|
$this->endAttributes = $endAttributes;
|
2014-04-20 00:08:59 +02:00
|
|
|
|
|
|
|
// In order to figure out the attributes for the starting token, we have to keep
|
|
|
|
// them in a stack
|
2015-04-26 22:32:18 +02:00
|
|
|
$this->startAttributeStack = array();
|
2014-04-20 00:08:59 +02:00
|
|
|
|
|
|
|
// Start off in the initial state and keep a stack of previous states
|
|
|
|
$state = 0;
|
|
|
|
$stateStack = array($state);
|
|
|
|
|
2014-04-20 13:16:54 +02:00
|
|
|
// Semantic value stack (contains values of tokens and semantic action results)
|
|
|
|
$this->semStack = array();
|
2014-04-20 00:08:59 +02:00
|
|
|
|
|
|
|
// Current position in the stack(s)
|
|
|
|
$this->stackPos = 0;
|
|
|
|
|
2015-02-02 23:25:34 +01:00
|
|
|
$errorState = 0;
|
|
|
|
|
2014-04-20 00:08:59 +02:00
|
|
|
for (;;) {
|
2014-04-20 13:16:54 +02:00
|
|
|
//$this->traceNewState($state, $symbol);
|
2014-04-20 00:08:59 +02:00
|
|
|
|
2014-04-20 13:16:54 +02:00
|
|
|
if ($this->actionBase[$state] == 0) {
|
|
|
|
$rule = $this->actionDefault[$state];
|
2014-04-20 00:08:59 +02:00
|
|
|
} else {
|
2014-04-20 13:16:54 +02:00
|
|
|
if ($symbol === self::SYMBOL_NONE) {
|
2014-04-20 00:08:59 +02:00
|
|
|
// Fetch the next token id from the lexer and fetch additional info by-ref.
|
|
|
|
// The end attributes are fetched into a temporary variable and only set once the token is really
|
|
|
|
// shifted (not during read). Otherwise you would sometimes get off-by-one errors, when a rule is
|
|
|
|
// reduced after a token was read but not yet shifted.
|
2015-04-26 22:32:18 +02:00
|
|
|
$tokenId = $this->lexer->getNextToken($tokenValue, $startAttributes, $endAttributes);
|
2014-04-20 00:08:59 +02:00
|
|
|
|
2014-04-20 13:16:54 +02:00
|
|
|
// map the lexer token id to the internally used symbols
|
|
|
|
$symbol = $tokenId >= 0 && $tokenId < $this->tokenToSymbolMapSize
|
|
|
|
? $this->tokenToSymbol[$tokenId]
|
|
|
|
: $this->invalidSymbol;
|
2014-04-20 00:08:59 +02:00
|
|
|
|
2014-04-20 13:16:54 +02:00
|
|
|
if ($symbol === $this->invalidSymbol) {
|
2014-04-20 00:08:59 +02:00
|
|
|
throw new \RangeException(sprintf(
|
|
|
|
'The lexer returned an invalid token (id=%d, value=%s)',
|
2014-04-20 13:16:54 +02:00
|
|
|
$tokenId, $tokenValue
|
2014-04-20 00:08:59 +02:00
|
|
|
));
|
|
|
|
}
|
|
|
|
|
2015-04-23 13:13:45 +02:00
|
|
|
// This is necessary to assign some meaningful attributes to /* empty */ productions. They'll get
|
|
|
|
// the attributes of the next token, even though they don't contain it themselves.
|
|
|
|
$this->startAttributeStack[$this->stackPos+1] = $startAttributes;
|
2016-04-05 11:53:50 +02:00
|
|
|
$this->lookaheadStartAttributes = $startAttributes;
|
2014-04-20 00:08:59 +02:00
|
|
|
|
2014-04-20 13:16:54 +02:00
|
|
|
//$this->traceRead($symbol);
|
2014-04-20 00:08:59 +02:00
|
|
|
}
|
|
|
|
|
2014-04-20 13:16:54 +02:00
|
|
|
$idx = $this->actionBase[$state] + $symbol;
|
|
|
|
if ((($idx >= 0 && $idx < $this->actionTableSize && $this->actionCheck[$idx] == $symbol)
|
|
|
|
|| ($state < $this->YY2TBLSTATE
|
|
|
|
&& ($idx = $this->actionBase[$state + $this->YYNLSTATES] + $symbol) >= 0
|
|
|
|
&& $idx < $this->actionTableSize && $this->actionCheck[$idx] == $symbol))
|
|
|
|
&& ($action = $this->action[$idx]) != $this->defaultAction) {
|
2014-04-20 00:08:59 +02:00
|
|
|
/*
|
2014-04-20 13:16:54 +02:00
|
|
|
* >= YYNLSTATES: shift and reduce
|
2014-04-20 00:08:59 +02:00
|
|
|
* > 0: shift
|
|
|
|
* = 0: accept
|
|
|
|
* < 0: reduce
|
|
|
|
* = -YYUNEXPECTED: error
|
|
|
|
*/
|
2014-04-20 13:16:54 +02:00
|
|
|
if ($action > 0) {
|
2014-04-20 00:08:59 +02:00
|
|
|
/* shift */
|
2014-04-20 13:16:54 +02:00
|
|
|
//$this->traceShift($symbol);
|
2014-04-20 00:08:59 +02:00
|
|
|
|
|
|
|
++$this->stackPos;
|
2015-04-22 15:57:29 +02:00
|
|
|
$stateStack[$this->stackPos] = $state = $action;
|
2014-04-20 13:16:54 +02:00
|
|
|
$this->semStack[$this->stackPos] = $tokenValue;
|
2015-04-22 15:57:29 +02:00
|
|
|
$this->startAttributeStack[$this->stackPos] = $startAttributes;
|
2015-04-26 22:32:18 +02:00
|
|
|
$this->endAttributes = $endAttributes;
|
2014-04-20 13:16:54 +02:00
|
|
|
$symbol = self::SYMBOL_NONE;
|
2014-04-20 00:08:59 +02:00
|
|
|
|
2015-02-02 23:25:34 +01:00
|
|
|
if ($errorState) {
|
|
|
|
--$errorState;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($action < $this->YYNLSTATES) {
|
2014-04-20 00:08:59 +02:00
|
|
|
continue;
|
2015-02-02 23:25:34 +01:00
|
|
|
}
|
2014-04-20 00:08:59 +02:00
|
|
|
|
|
|
|
/* $yyn >= YYNLSTATES means shift-and-reduce */
|
2014-04-20 13:16:54 +02:00
|
|
|
$rule = $action - $this->YYNLSTATES;
|
2014-04-20 00:08:59 +02:00
|
|
|
} else {
|
2014-04-20 13:16:54 +02:00
|
|
|
$rule = -$action;
|
2014-04-20 00:08:59 +02:00
|
|
|
}
|
|
|
|
} else {
|
2014-04-20 13:16:54 +02:00
|
|
|
$rule = $this->actionDefault[$state];
|
2014-04-20 00:08:59 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for (;;) {
|
2014-04-20 13:16:54 +02:00
|
|
|
if ($rule === 0) {
|
2014-04-20 00:08:59 +02:00
|
|
|
/* accept */
|
2014-04-20 13:16:54 +02:00
|
|
|
//$this->traceAccept();
|
|
|
|
return $this->semValue;
|
|
|
|
} elseif ($rule !== $this->unexpectedTokenRule) {
|
2014-04-20 00:08:59 +02:00
|
|
|
/* reduce */
|
2014-04-20 13:16:54 +02:00
|
|
|
//$this->traceReduce($rule);
|
2014-04-20 00:08:59 +02:00
|
|
|
|
|
|
|
try {
|
2015-04-22 15:57:29 +02:00
|
|
|
$this->{'reduceRule' . $rule}();
|
2014-04-20 00:08:59 +02:00
|
|
|
} catch (Error $e) {
|
2015-04-18 12:51:26 +02:00
|
|
|
if (-1 === $e->getStartLine() && isset($startAttributes['startLine'])) {
|
|
|
|
$e->setStartLine($startAttributes['startLine']);
|
2014-04-20 00:08:59 +02:00
|
|
|
}
|
|
|
|
|
2015-04-30 17:37:19 +02:00
|
|
|
$this->errors[] = $e;
|
|
|
|
if ($this->throwOnError) {
|
|
|
|
throw $e;
|
|
|
|
} else {
|
|
|
|
// Currently can't recover from "special" errors
|
|
|
|
return null;
|
|
|
|
}
|
2014-04-20 00:08:59 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Goto - shift nonterminal */
|
2014-04-20 13:16:54 +02:00
|
|
|
$this->stackPos -= $this->ruleToLength[$rule];
|
|
|
|
$nonTerminal = $this->ruleToNonTerminal[$rule];
|
|
|
|
$idx = $this->gotoBase[$nonTerminal] + $stateStack[$this->stackPos];
|
|
|
|
if ($idx >= 0 && $idx < $this->gotoTableSize && $this->gotoCheck[$idx] == $nonTerminal) {
|
|
|
|
$state = $this->goto[$idx];
|
2014-04-20 00:08:59 +02:00
|
|
|
} else {
|
2014-04-20 13:16:54 +02:00
|
|
|
$state = $this->gotoDefault[$nonTerminal];
|
2014-04-20 00:08:59 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
++$this->stackPos;
|
|
|
|
$stateStack[$this->stackPos] = $state;
|
2014-04-20 13:16:54 +02:00
|
|
|
$this->semStack[$this->stackPos] = $this->semValue;
|
2014-04-20 00:08:59 +02:00
|
|
|
} else {
|
|
|
|
/* error */
|
2015-02-02 23:25:34 +01:00
|
|
|
switch ($errorState) {
|
|
|
|
case 0:
|
|
|
|
$msg = $this->getErrorMessage($symbol, $state);
|
2015-04-26 22:32:18 +02:00
|
|
|
$error = new Error($msg, $startAttributes + $endAttributes);
|
2015-02-02 23:25:34 +01:00
|
|
|
$this->errors[] = $error;
|
|
|
|
if ($this->throwOnError) {
|
|
|
|
throw $error;
|
|
|
|
}
|
|
|
|
// Break missing intentionally
|
|
|
|
case 1:
|
|
|
|
case 2:
|
|
|
|
$errorState = 3;
|
|
|
|
|
|
|
|
// Pop until error-expecting state uncovered
|
|
|
|
while (!(
|
|
|
|
(($idx = $this->actionBase[$state] + $this->errorSymbol) >= 0
|
|
|
|
&& $idx < $this->actionTableSize && $this->actionCheck[$idx] == $this->errorSymbol)
|
|
|
|
|| ($state < $this->YY2TBLSTATE
|
|
|
|
&& ($idx = $this->actionBase[$state + $this->YYNLSTATES] + $this->errorSymbol) >= 0
|
|
|
|
&& $idx < $this->actionTableSize && $this->actionCheck[$idx] == $this->errorSymbol)
|
|
|
|
) || ($action = $this->action[$idx]) == $this->defaultAction) { // Not totally sure about this
|
|
|
|
if ($this->stackPos <= 0) {
|
2015-04-30 17:37:19 +02:00
|
|
|
// Could not recover from error
|
|
|
|
return null;
|
2015-02-02 23:25:34 +01:00
|
|
|
}
|
|
|
|
$state = $stateStack[--$this->stackPos];
|
|
|
|
//$this->tracePop($state);
|
|
|
|
}
|
|
|
|
|
|
|
|
//$this->traceShift($this->errorSymbol);
|
|
|
|
$stateStack[++$this->stackPos] = $state = $action;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 3:
|
|
|
|
if ($symbol === 0) {
|
2015-04-30 17:37:19 +02:00
|
|
|
// Reached EOF without recovering from error
|
|
|
|
return null;
|
2015-02-02 23:25:34 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
//$this->traceDiscard($symbol);
|
|
|
|
$symbol = self::SYMBOL_NONE;
|
|
|
|
break 2;
|
2014-04-20 00:08:59 +02:00
|
|
|
}
|
2015-02-02 23:25:34 +01:00
|
|
|
}
|
2014-04-20 00:08:59 +02:00
|
|
|
|
2015-02-02 23:25:34 +01:00
|
|
|
if ($state < $this->YYNLSTATES) {
|
|
|
|
break;
|
2014-04-20 00:08:59 +02:00
|
|
|
}
|
2015-02-02 23:25:34 +01:00
|
|
|
|
|
|
|
/* >= YYNLSTATES means shift-and-reduce */
|
|
|
|
$rule = $state - $this->YYNLSTATES;
|
2014-04-20 13:16:54 +02:00
|
|
|
}
|
|
|
|
}
|
2015-04-30 17:37:19 +02:00
|
|
|
|
|
|
|
throw new \RuntimeException('Reached end of parser loop');
|
2014-04-20 13:16:54 +02:00
|
|
|
}
|
|
|
|
|
2015-02-02 23:25:34 +01:00
|
|
|
protected function getErrorMessage($symbol, $state) {
|
|
|
|
$expectedString = '';
|
|
|
|
if ($expected = $this->getExpectedTokens($state)) {
|
|
|
|
$expectedString = ', expecting ' . implode(' or ', $expected);
|
|
|
|
}
|
|
|
|
|
|
|
|
return 'Syntax error, unexpected ' . $this->symbolToName[$symbol] . $expectedString;
|
|
|
|
}
|
|
|
|
|
2014-04-20 13:16:54 +02:00
|
|
|
protected function getExpectedTokens($state) {
|
|
|
|
$expected = array();
|
|
|
|
|
|
|
|
$base = $this->actionBase[$state];
|
|
|
|
foreach ($this->symbolToName as $symbol => $name) {
|
|
|
|
$idx = $base + $symbol;
|
|
|
|
if ($idx >= 0 && $idx < $this->actionTableSize && $this->actionCheck[$idx] === $symbol
|
|
|
|
|| $state < $this->YY2TBLSTATE
|
|
|
|
&& ($idx = $this->actionBase[$state + $this->YYNLSTATES] + $symbol) >= 0
|
|
|
|
&& $idx < $this->actionTableSize && $this->actionCheck[$idx] === $symbol
|
|
|
|
) {
|
|
|
|
if ($this->action[$idx] != $this->unexpectedTokenRule) {
|
|
|
|
if (count($expected) == 4) {
|
|
|
|
/* Too many expected tokens */
|
|
|
|
return array();
|
|
|
|
}
|
2014-04-20 00:08:59 +02:00
|
|
|
|
2014-04-20 13:16:54 +02:00
|
|
|
$expected[] = $name;
|
|
|
|
}
|
2014-04-20 00:08:59 +02:00
|
|
|
}
|
|
|
|
}
|
2014-04-20 13:16:54 +02:00
|
|
|
|
|
|
|
return $expected;
|
2014-04-20 00:08:59 +02:00
|
|
|
}
|
|
|
|
|
2014-04-21 12:30:55 +02:00
|
|
|
/*
|
|
|
|
* Tracing functions used for debugging the parser.
|
|
|
|
*/
|
|
|
|
|
2015-05-01 20:17:39 +02:00
|
|
|
/*
|
2014-04-20 13:16:54 +02:00
|
|
|
protected function traceNewState($state, $symbol) {
|
2014-04-20 00:08:59 +02:00
|
|
|
echo '% State ' . $state
|
2014-04-20 13:16:54 +02:00
|
|
|
. ', Lookahead ' . ($symbol == self::SYMBOL_NONE ? '--none--' : $this->symbolToName[$symbol]) . "\n";
|
2014-04-20 00:08:59 +02:00
|
|
|
}
|
|
|
|
|
2014-04-20 13:16:54 +02:00
|
|
|
protected function traceRead($symbol) {
|
|
|
|
echo '% Reading ' . $this->symbolToName[$symbol] . "\n";
|
2014-04-20 00:08:59 +02:00
|
|
|
}
|
|
|
|
|
2014-04-20 13:16:54 +02:00
|
|
|
protected function traceShift($symbol) {
|
|
|
|
echo '% Shift ' . $this->symbolToName[$symbol] . "\n";
|
2014-04-20 00:08:59 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
protected function traceAccept() {
|
|
|
|
echo "% Accepted.\n";
|
|
|
|
}
|
|
|
|
|
|
|
|
protected function traceReduce($n) {
|
2014-04-20 13:16:54 +02:00
|
|
|
echo '% Reduce by (' . $n . ') ' . $this->productions[$n] . "\n";
|
2014-04-20 00:08:59 +02:00
|
|
|
}
|
2014-04-21 12:30:55 +02:00
|
|
|
|
2015-02-02 23:25:34 +01:00
|
|
|
protected function tracePop($state) {
|
|
|
|
echo '% Recovering, uncovered state ' . $state . "\n";
|
|
|
|
}
|
|
|
|
|
|
|
|
protected function traceDiscard($symbol) {
|
|
|
|
echo '% Discard ' . $this->symbolToName[$symbol] . "\n";
|
|
|
|
}
|
2015-05-01 20:17:39 +02:00
|
|
|
*/
|
2015-02-02 23:25:34 +01:00
|
|
|
|
2014-04-21 12:30:55 +02:00
|
|
|
/*
|
|
|
|
* Helper functions invoked by semantic actions
|
|
|
|
*/
|
|
|
|
|
2014-04-21 14:49:35 +02:00
|
|
|
/**
|
|
|
|
* Moves statements of semicolon-style namespaces into $ns->stmts and checks various error conditions.
|
|
|
|
*
|
|
|
|
* @param Node[] $stmts
|
|
|
|
* @return Node[]
|
|
|
|
*/
|
2014-04-21 12:30:55 +02:00
|
|
|
protected function handleNamespaces(array $stmts) {
|
2014-04-21 14:49:35 +02:00
|
|
|
$style = $this->getNamespacingStyle($stmts);
|
|
|
|
if (null === $style) {
|
|
|
|
// not namespaced, nothing to do
|
|
|
|
return $stmts;
|
|
|
|
} elseif ('brace' === $style) {
|
|
|
|
// For braced namespaces we only have to check that there are no invalid statements between the namespaces
|
|
|
|
$afterFirstNamespace = false;
|
|
|
|
foreach ($stmts as $stmt) {
|
|
|
|
if ($stmt instanceof Node\Stmt\Namespace_) {
|
|
|
|
$afterFirstNamespace = true;
|
|
|
|
} elseif (!$stmt instanceof Node\Stmt\HaltCompiler && $afterFirstNamespace) {
|
|
|
|
throw new Error('No code may exist outside of namespace {}', $stmt->getLine());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return $stmts;
|
|
|
|
} else {
|
|
|
|
// For semicolon namespaces we have to move the statements after a namespace declaration into ->stmts
|
|
|
|
$resultStmts = array();
|
|
|
|
$targetStmts =& $resultStmts;
|
|
|
|
foreach ($stmts as $stmt) {
|
|
|
|
if ($stmt instanceof Node\Stmt\Namespace_) {
|
|
|
|
$stmt->stmts = array();
|
|
|
|
$targetStmts =& $stmt->stmts;
|
|
|
|
$resultStmts[] = $stmt;
|
|
|
|
} elseif ($stmt instanceof Node\Stmt\HaltCompiler) {
|
|
|
|
// __halt_compiler() is not moved into the namespace
|
|
|
|
$resultStmts[] = $stmt;
|
|
|
|
} else {
|
|
|
|
$targetStmts[] = $stmt;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return $resultStmts;
|
|
|
|
}
|
|
|
|
}
|
2014-04-21 12:30:55 +02:00
|
|
|
|
2014-04-21 14:49:35 +02:00
|
|
|
private function getNamespacingStyle(array $stmts) {
|
|
|
|
$style = null;
|
2014-04-21 12:30:55 +02:00
|
|
|
$hasNotAllowedStmts = false;
|
2016-01-15 22:01:51 +01:00
|
|
|
foreach ($stmts as $i => $stmt) {
|
2014-04-21 12:30:55 +02:00
|
|
|
if ($stmt instanceof Node\Stmt\Namespace_) {
|
2014-04-21 14:49:35 +02:00
|
|
|
$currentStyle = null === $stmt->stmts ? 'semicolon' : 'brace';
|
|
|
|
if (null === $style) {
|
|
|
|
$style = $currentStyle;
|
2014-04-21 12:30:55 +02:00
|
|
|
if ($hasNotAllowedStmts) {
|
|
|
|
throw new Error('Namespace declaration statement has to be the very first statement in the script', $stmt->getLine());
|
|
|
|
}
|
2014-04-21 14:49:35 +02:00
|
|
|
} elseif ($style !== $currentStyle) {
|
2014-04-21 12:30:55 +02:00
|
|
|
throw new Error('Cannot mix bracketed namespace declarations with unbracketed namespace declarations', $stmt->getLine());
|
|
|
|
}
|
2016-01-15 22:01:51 +01:00
|
|
|
continue;
|
2014-04-21 12:30:55 +02:00
|
|
|
}
|
2016-01-15 22:01:51 +01:00
|
|
|
|
2016-04-16 21:39:49 +02:00
|
|
|
/* declare(), __halt_compiler() and nops can be used before a namespace declaration */
|
|
|
|
if ($stmt instanceof Node\Stmt\Declare_
|
|
|
|
|| $stmt instanceof Node\Stmt\HaltCompiler
|
|
|
|
|| $stmt instanceof Node\Stmt\Nop) {
|
2016-01-15 22:01:51 +01:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* There may be a hashbang line at the very start of the file */
|
|
|
|
if ($i == 0 && $stmt instanceof Node\Stmt\InlineHTML && preg_match('/\A#!.*\r?\n\z/', $stmt->value)) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Everything else if forbidden before namespace declarations */
|
|
|
|
$hasNotAllowedStmts = true;
|
2014-04-21 12:30:55 +02:00
|
|
|
}
|
2014-04-21 14:49:35 +02:00
|
|
|
return $style;
|
2014-04-21 12:30:55 +02:00
|
|
|
}
|
2015-06-13 20:16:09 +02:00
|
|
|
|
|
|
|
protected function handleScalarTypes(Name $name) {
|
|
|
|
$scalarTypes = [
|
|
|
|
'bool' => true,
|
|
|
|
'int' => true,
|
|
|
|
'float' => true,
|
|
|
|
'string' => true,
|
|
|
|
];
|
|
|
|
|
|
|
|
if (!$name->isUnqualified()) {
|
|
|
|
return $name;
|
|
|
|
}
|
|
|
|
|
|
|
|
$lowerName = strtolower($name->toString());
|
|
|
|
return isset($scalarTypes[$lowerName]) ? $lowerName : $name;
|
|
|
|
}
|
2014-04-20 00:08:59 +02:00
|
|
|
}
|