123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501 |
- <?php
- namespace PhpParser;
- use PhpParser\Node\Name;
- abstract class ParserAbstract implements Parser
- {
- const SYMBOL_NONE = -1;
-
-
- protected $tokenToSymbolMapSize;
-
- protected $actionTableSize;
-
- protected $gotoTableSize;
-
- protected $invalidSymbol;
-
- protected $errorSymbol;
-
- protected $defaultAction;
-
- protected $unexpectedTokenRule;
- protected $YY2TBLSTATE;
- protected $YYNLSTATES;
-
- protected $tokenToSymbol;
-
- protected $symbolToName;
-
- protected $productions;
-
- protected $actionBase;
-
- protected $action;
-
- protected $actionCheck;
-
- protected $actionDefault;
-
- protected $gotoBase;
-
- protected $goto;
-
- protected $gotoCheck;
-
- protected $gotoDefault;
-
- protected $ruleToNonTerminal;
-
- protected $ruleToLength;
-
-
- protected $lexer;
-
- protected $semValue;
-
- protected $stackPos;
-
- protected $semStack;
-
- protected $startAttributeStack;
-
- protected $endAttributes;
-
- protected $lookaheadStartAttributes;
-
- protected $throwOnError;
-
- protected $errors;
-
- public function __construct(Lexer $lexer, array $options = array()) {
- $this->lexer = $lexer;
- $this->errors = array();
- $this->throwOnError = isset($options['throwOnError']) ? $options['throwOnError'] : true;
- }
-
- public function getErrors() {
- return $this->errors;
- }
-
- public function parse($code) {
- $this->lexer->startLexing($code);
- $this->errors = array();
-
- $symbol = self::SYMBOL_NONE;
-
-
-
- $startAttributes = '*POISON';
- $endAttributes = '*POISON';
- $this->endAttributes = $endAttributes;
-
-
- $this->startAttributeStack = array();
-
- $state = 0;
- $stateStack = array($state);
-
- $this->semStack = array();
-
- $this->stackPos = 0;
- $errorState = 0;
- for (;;) {
-
- if ($this->actionBase[$state] == 0) {
- $rule = $this->actionDefault[$state];
- } else {
- if ($symbol === self::SYMBOL_NONE) {
-
-
-
-
- $tokenId = $this->lexer->getNextToken($tokenValue, $startAttributes, $endAttributes);
-
- $symbol = $tokenId >= 0 && $tokenId < $this->tokenToSymbolMapSize
- ? $this->tokenToSymbol[$tokenId]
- : $this->invalidSymbol;
- if ($symbol === $this->invalidSymbol) {
- throw new \RangeException(sprintf(
- 'The lexer returned an invalid token (id=%d, value=%s)',
- $tokenId, $tokenValue
- ));
- }
-
-
- $this->startAttributeStack[$this->stackPos+1] = $startAttributes;
- $this->lookaheadStartAttributes = $startAttributes;
-
- }
- $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) {
-
- if ($action > 0) {
-
-
- ++$this->stackPos;
- $stateStack[$this->stackPos] = $state = $action;
- $this->semStack[$this->stackPos] = $tokenValue;
- $this->startAttributeStack[$this->stackPos] = $startAttributes;
- $this->endAttributes = $endAttributes;
- $symbol = self::SYMBOL_NONE;
- if ($errorState) {
- --$errorState;
- }
- if ($action < $this->YYNLSTATES) {
- continue;
- }
-
- $rule = $action - $this->YYNLSTATES;
- } else {
- $rule = -$action;
- }
- } else {
- $rule = $this->actionDefault[$state];
- }
- }
- for (;;) {
- if ($rule === 0) {
-
-
- return $this->semValue;
- } elseif ($rule !== $this->unexpectedTokenRule) {
-
-
- try {
- $this->{'reduceRule' . $rule}();
- } catch (Error $e) {
- if (-1 === $e->getStartLine() && isset($startAttributes['startLine'])) {
- $e->setStartLine($startAttributes['startLine']);
- }
- $this->errors[] = $e;
- if ($this->throwOnError) {
- throw $e;
- } else {
-
- return null;
- }
- }
-
- $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];
- } else {
- $state = $this->gotoDefault[$nonTerminal];
- }
- ++$this->stackPos;
- $stateStack[$this->stackPos] = $state;
- $this->semStack[$this->stackPos] = $this->semValue;
- } else {
-
- switch ($errorState) {
- case 0:
- $msg = $this->getErrorMessage($symbol, $state);
- $error = new Error($msg, $startAttributes + $endAttributes);
- $this->errors[] = $error;
- if ($this->throwOnError) {
- throw $error;
- }
-
- case 1:
- case 2:
- $errorState = 3;
-
- 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) {
- if ($this->stackPos <= 0) {
-
- return null;
- }
- $state = $stateStack[--$this->stackPos];
-
- }
-
- $stateStack[++$this->stackPos] = $state = $action;
- break;
- case 3:
- if ($symbol === 0) {
-
- return null;
- }
-
- $symbol = self::SYMBOL_NONE;
- break 2;
- }
- }
- if ($state < $this->YYNLSTATES) {
- break;
- }
-
- $rule = $state - $this->YYNLSTATES;
- }
- }
- throw new \RuntimeException('Reached end of parser loop');
- }
- protected function getErrorMessage($symbol, $state) {
- $expectedString = '';
- if ($expected = $this->getExpectedTokens($state)) {
- $expectedString = ', expecting ' . implode(' or ', $expected);
- }
- return 'Syntax error, unexpected ' . $this->symbolToName[$symbol] . $expectedString;
- }
- 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
- && $this->action[$idx] != $this->defaultAction
- ) {
- if (count($expected) == 4) {
-
- return array();
- }
- $expected[] = $name;
- }
- }
- }
- return $expected;
- }
-
-
-
-
- protected function handleNamespaces(array $stmts) {
- $style = $this->getNamespacingStyle($stmts);
- if (null === $style) {
-
- return $stmts;
- } elseif ('brace' === $style) {
-
- $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 {
-
- $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) {
-
- $resultStmts[] = $stmt;
- } else {
- $targetStmts[] = $stmt;
- }
- }
- return $resultStmts;
- }
- }
- private function getNamespacingStyle(array $stmts) {
- $style = null;
- $hasNotAllowedStmts = false;
- foreach ($stmts as $i => $stmt) {
- if ($stmt instanceof Node\Stmt\Namespace_) {
- $currentStyle = null === $stmt->stmts ? 'semicolon' : 'brace';
- if (null === $style) {
- $style = $currentStyle;
- if ($hasNotAllowedStmts) {
- throw new Error('Namespace declaration statement has to be the very first statement in the script', $stmt->getLine());
- }
- } elseif ($style !== $currentStyle) {
- throw new Error('Cannot mix bracketed namespace declarations with unbracketed namespace declarations', $stmt->getLine());
- }
- continue;
- }
-
- if ($stmt instanceof Node\Stmt\Declare_
- || $stmt instanceof Node\Stmt\HaltCompiler
- || $stmt instanceof Node\Stmt\Nop) {
- continue;
- }
-
- if ($i == 0 && $stmt instanceof Node\Stmt\InlineHTML && preg_match('/\A#!.*\r?\n\z/', $stmt->value)) {
- continue;
- }
-
- $hasNotAllowedStmts = true;
- }
- return $style;
- }
- 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;
- }
- }
|