mirror of
https://github.com/phabelio/PHP-Parser.git
synced 2024-11-30 04:29:15 +01:00
Separate parser code from generated data
This commit is contained in:
parent
3db3ad7d1e
commit
0faa844a75
@ -11,23 +11,12 @@ namespace PhpParser;
|
||||
/* This is an automatically GENERATED file, which should not be manually edited.
|
||||
* Instead edit one of the following:
|
||||
* * the grammar file grammar/zend_language_parser.phpy
|
||||
* * the parser skeleton grammar/kymacc.php.parser
|
||||
* * the skeleton file grammar/kymacc.php.parser
|
||||
* * the preprocessing script grammar/rebuildParser.php
|
||||
*
|
||||
* The skeleton for this parser was written by Moriyoshi Koizumi and is based on
|
||||
* the work by Masato Bito and is in the PUBLIC DOMAIN.
|
||||
*/
|
||||
#if -t
|
||||
class #(-p)_Debug extends #(-p)
|
||||
#endif
|
||||
#ifnot -t
|
||||
class #(-p)
|
||||
#endif
|
||||
class Parser extends ParserAbstract
|
||||
{
|
||||
#ifnot -t
|
||||
const TOKEN_NONE = -1;
|
||||
const TOKEN_INVALID = #(YYBADCH);
|
||||
|
||||
const TOKEN_MAP_SIZE = #(YYMAXLEX);
|
||||
|
||||
const YYLAST = #(YYLAST);
|
||||
@ -37,19 +26,15 @@ class #(-p)
|
||||
const YYUNEXPECTED = #(YYUNEXPECTED);
|
||||
const YYDEFAULT = #(YYDEFAULT);
|
||||
|
||||
// {{{ Tokens
|
||||
#tokenval
|
||||
const %s = %n;
|
||||
#endtokenval
|
||||
// }}}
|
||||
|
||||
/* @var array Map of token ids to their respective names */
|
||||
protected static $terminals = array(
|
||||
#listvar terminals
|
||||
, "???"
|
||||
);
|
||||
|
||||
/* @var array Map which translates lexer tokens to internal tokens */
|
||||
protected static $translate = array(
|
||||
#listvar yytranslate
|
||||
);
|
||||
@ -93,260 +78,12 @@ class #(-p)
|
||||
protected static $yylen = array(
|
||||
#listvar yylen
|
||||
);
|
||||
|
||||
protected $yyval;
|
||||
protected $yyastk;
|
||||
protected $stackPos;
|
||||
protected $lexer;
|
||||
|
||||
/**
|
||||
* Creates a parser instance.
|
||||
*
|
||||
* @param Lexer $lexer A lexer
|
||||
*/
|
||||
public function __construct(Lexer $lexer) {
|
||||
$this->lexer = $lexer;
|
||||
}
|
||||
#endif
|
||||
#if -t
|
||||
|
||||
protected static $yyproduction = array(
|
||||
#production-strings;
|
||||
);
|
||||
|
||||
protected function yyprintln($msg) {
|
||||
echo $msg, "\n";
|
||||
}
|
||||
|
||||
protected function YYTRACE_NEWSTATE($state, $tokenId) {
|
||||
$this->yyprintln(
|
||||
'% State ' . $state
|
||||
. ', Lookahead ' . ($tokenId == self::TOKEN_NONE ? '--none--' : self::$terminals[$tokenId])
|
||||
);
|
||||
}
|
||||
|
||||
protected function YYTRACE_READ($tokenId) {
|
||||
$this->yyprintln('% Reading ' . self::$terminals[$tokenId]);
|
||||
}
|
||||
|
||||
protected function YYTRACE_SHIFT($tokenId) {
|
||||
$this->yyprintln('% Shift ' . self::$terminals[$tokenId]);
|
||||
}
|
||||
|
||||
protected function YYTRACE_ACCEPT() {
|
||||
$this->yyprintln('% Accepted.');
|
||||
}
|
||||
|
||||
protected function YYTRACE_REDUCE($n) {
|
||||
$this->yyprintln('% Reduce by (' . $n . ') ' . self::$yyproduction[$n]);
|
||||
}
|
||||
|
||||
protected function YYTRACE_POP($state) {
|
||||
$this->yyprintln('% Recovering, uncovers state ' . $state);
|
||||
}
|
||||
|
||||
protected function YYTRACE_DISCARD($tokenId) {
|
||||
$this->yyprintln('% Discard ' . self::$terminals[$tokenId]);
|
||||
}
|
||||
#endif
|
||||
|
||||
/**
|
||||
#ifnot -t
|
||||
* Parses PHP code into a node tree.
|
||||
#endif
|
||||
#if -t
|
||||
* Parses PHP code into a node tree and prints out debugging information.
|
||||
#endif
|
||||
*
|
||||
* @param string $code The source code to parse
|
||||
*
|
||||
* @return Node[] Array of statements
|
||||
*/
|
||||
public function parse($code) {
|
||||
$this->lexer->startLexing($code);
|
||||
|
||||
// We start off with no lookahead-token
|
||||
$tokenId = self::TOKEN_NONE;
|
||||
|
||||
// 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 (+).
|
||||
$startAttributes = array('startLine' => 1);
|
||||
$endAttributes = array();
|
||||
|
||||
// In order to figure out the attributes for the starting token, we have to keep
|
||||
// them in a stack
|
||||
$attributeStack = array($startAttributes);
|
||||
|
||||
// Start off in the initial state and keep a stack of previous states
|
||||
$state = 0;
|
||||
$stateStack = array($state);
|
||||
|
||||
// AST stack (?)
|
||||
$this->yyastk = array();
|
||||
|
||||
// Current position in the stack(s)
|
||||
$this->stackPos = 0;
|
||||
|
||||
for (;;) {
|
||||
#if -t
|
||||
$this->YYTRACE_NEWSTATE($state, $tokenId);
|
||||
|
||||
#endif
|
||||
if (self::$yybase[$state] == 0) {
|
||||
$yyn = self::$yydefault[$state];
|
||||
} else {
|
||||
if ($tokenId === self::TOKEN_NONE) {
|
||||
// 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.
|
||||
$origTokenId = $this->lexer->getNextToken($tokenValue, $startAttributes, $nextEndAttributes);
|
||||
|
||||
// map the lexer token id to the internally used token id's
|
||||
$tokenId = $origTokenId >= 0 && $origTokenId < self::TOKEN_MAP_SIZE
|
||||
? self::$translate[$origTokenId]
|
||||
: self::TOKEN_INVALID;
|
||||
|
||||
if ($tokenId === self::TOKEN_INVALID) {
|
||||
throw new \RangeException(sprintf(
|
||||
'The lexer returned an invalid token (id=%d, value=%s)',
|
||||
$origTokenId, $tokenValue
|
||||
));
|
||||
}
|
||||
|
||||
$attributeStack[$this->stackPos] = $startAttributes;
|
||||
#if -t
|
||||
|
||||
$this->YYTRACE_READ($tokenId);
|
||||
#endif
|
||||
}
|
||||
|
||||
if ((($yyn = self::$yybase[$state] + $tokenId) >= 0
|
||||
&& $yyn < self::YYLAST && self::$yycheck[$yyn] == $tokenId
|
||||
|| ($state < self::YY2TBLSTATE
|
||||
&& ($yyn = self::$yybase[$state + self::YYNLSTATES] + $tokenId) >= 0
|
||||
&& $yyn < self::YYLAST
|
||||
&& self::$yycheck[$yyn] == $tokenId))
|
||||
&& ($yyn = self::$yyaction[$yyn]) != self::YYDEFAULT) {
|
||||
/*
|
||||
* >= YYNLSTATE: shift and reduce
|
||||
* > 0: shift
|
||||
* = 0: accept
|
||||
* < 0: reduce
|
||||
* = -YYUNEXPECTED: error
|
||||
*/
|
||||
if ($yyn > 0) {
|
||||
/* shift */
|
||||
#if -t
|
||||
$this->YYTRACE_SHIFT($tokenId);
|
||||
|
||||
#endif
|
||||
++$this->stackPos;
|
||||
|
||||
$stateStack[$this->stackPos] = $state = $yyn;
|
||||
$this->yyastk[$this->stackPos] = $tokenValue;
|
||||
$attributeStack[$this->stackPos] = $startAttributes;
|
||||
$endAttributes = $nextEndAttributes;
|
||||
$tokenId = self::TOKEN_NONE;
|
||||
|
||||
if ($yyn < self::YYNLSTATES)
|
||||
continue;
|
||||
|
||||
/* $yyn >= YYNLSTATES means shift-and-reduce */
|
||||
$yyn -= self::YYNLSTATES;
|
||||
} else {
|
||||
$yyn = -$yyn;
|
||||
}
|
||||
} else {
|
||||
$yyn = self::$yydefault[$state];
|
||||
}
|
||||
}
|
||||
|
||||
for (;;) {
|
||||
/* reduce/error */
|
||||
if ($yyn == 0) {
|
||||
/* accept */
|
||||
#if -t
|
||||
$this->YYTRACE_ACCEPT();
|
||||
#endif
|
||||
return $this->yyval;
|
||||
} elseif ($yyn != self::YYUNEXPECTED) {
|
||||
/* reduce */
|
||||
#if -t
|
||||
$this->YYTRACE_REDUCE($yyn);
|
||||
#endif
|
||||
try {
|
||||
$this->{'yyn' . $yyn}(
|
||||
$attributeStack[$this->stackPos - self::$yylen[$yyn]]
|
||||
+ $endAttributes
|
||||
);
|
||||
} catch (Error $e) {
|
||||
if (-1 === $e->getRawLine()) {
|
||||
$e->setRawLine($startAttributes['startLine']);
|
||||
}
|
||||
|
||||
throw $e;
|
||||
}
|
||||
|
||||
/* Goto - shift nonterminal */
|
||||
$this->stackPos -= self::$yylen[$yyn];
|
||||
$yyn = self::$yylhs[$yyn];
|
||||
if (($yyp = self::$yygbase[$yyn] + $stateStack[$this->stackPos]) >= 0
|
||||
&& $yyp < self::YYGLAST
|
||||
&& self::$yygcheck[$yyp] == $yyn) {
|
||||
$state = self::$yygoto[$yyp];
|
||||
} else {
|
||||
$state = self::$yygdefault[$yyn];
|
||||
}
|
||||
|
||||
++$this->stackPos;
|
||||
|
||||
$stateStack[$this->stackPos] = $state;
|
||||
$this->yyastk[$this->stackPos] = $this->yyval;
|
||||
$attributeStack[$this->stackPos] = $startAttributes;
|
||||
} else {
|
||||
/* error */
|
||||
$expected = array();
|
||||
|
||||
$base = self::$yybase[$state];
|
||||
for ($i = 0; $i < self::TOKEN_MAP_SIZE; ++$i) {
|
||||
$n = $base + $i;
|
||||
if ($n >= 0 && $n < self::YYLAST && self::$yycheck[$n] == $i
|
||||
|| $state < self::YY2TBLSTATE
|
||||
&& ($n = self::$yybase[$state + self::YYNLSTATES] + $i) >= 0
|
||||
&& $n < self::YYLAST && self::$yycheck[$n] == $i
|
||||
) {
|
||||
if (self::$yyaction[$n] != self::YYUNEXPECTED) {
|
||||
if (count($expected) == 4) {
|
||||
/* Too many expected tokens */
|
||||
$expected = array();
|
||||
break;
|
||||
}
|
||||
|
||||
$expected[] = self::$terminals[$i];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
$expectedString = '';
|
||||
if ($expected) {
|
||||
$expectedString = ', expecting ' . implode(' or ', $expected);
|
||||
}
|
||||
|
||||
throw new Error(
|
||||
'Syntax error, unexpected ' . self::$terminals[$tokenId] . $expectedString,
|
||||
$startAttributes['startLine']
|
||||
);
|
||||
}
|
||||
|
||||
if ($state < self::YYNLSTATES)
|
||||
break;
|
||||
/* >= YYNLSTATES means shift-and-reduce */
|
||||
$yyn = $state - self::YYNLSTATES;
|
||||
}
|
||||
}
|
||||
}
|
||||
#ifnot -t
|
||||
#reduce
|
||||
|
||||
protected function yyn%n($attributes) {
|
||||
@ -358,6 +95,5 @@ class #(-p)
|
||||
$this->yyval = $this->yyastk[$this->stackPos];
|
||||
}
|
||||
#endreduce
|
||||
#endif
|
||||
}
|
||||
#tailcode;
|
||||
|
@ -5,7 +5,6 @@ $skeletonFile = __DIR__ . '/kmyacc.php.parser';
|
||||
$tmpGrammarFile = __DIR__ . '/tmp_parser.phpy';
|
||||
$tmpResultFile = __DIR__ . '/tmp_parser.php';
|
||||
$parserResultFile = __DIR__ . '/../lib/PhpParser/Parser.php';
|
||||
$debugParserResultFile = __DIR__ . '/../lib/PhpParser/Parser/Debug.php';
|
||||
|
||||
// check for kmyacc.exe binary in this directory, otherwise fall back to global name
|
||||
$kmyacc = __DIR__ . '/kmyacc.exe';
|
||||
@ -47,20 +46,14 @@ $grammarCode = resolveArrays($grammarCode);
|
||||
|
||||
file_put_contents($tmpGrammarFile, $grammarCode);
|
||||
|
||||
$additionalArgs = $optionDebug ? '-t -v' : '';
|
||||
|
||||
echo "Building parser.\n";
|
||||
$output = trim(shell_exec("$kmyacc -l -m $skeletonFile -p Parser $tmpGrammarFile 2>&1"));
|
||||
$output = trim(shell_exec("$kmyacc $additionalArgs -l -m $skeletonFile $tmpGrammarFile 2>&1"));
|
||||
echo "Output: \"$output\"\n";
|
||||
|
||||
moveFileWithDirCheck($tmpResultFile, $parserResultFile);
|
||||
|
||||
if ($optionDebug) {
|
||||
echo "Building debug parser.\n";
|
||||
$output = trim(shell_exec("$kmyacc -t -v -l -m $skeletonFile -p Parser $tmpGrammarFile 2>&1"));
|
||||
echo "Output: \"$output\"\n";
|
||||
|
||||
moveFileWithDirCheck($tmpResultFile, $debugParserResultFile);
|
||||
}
|
||||
|
||||
if (!$optionKeepTmpGrammar) {
|
||||
unlink($tmpGrammarFile);
|
||||
}
|
||||
|
@ -5,17 +5,12 @@ namespace PhpParser;
|
||||
/* This is an automatically GENERATED file, which should not be manually edited.
|
||||
* Instead edit one of the following:
|
||||
* * the grammar file grammar/zend_language_parser.phpy
|
||||
* * the parser skeleton grammar/kymacc.php.parser
|
||||
* * the skeleton file grammar/kymacc.php.parser
|
||||
* * the preprocessing script grammar/rebuildParser.php
|
||||
*
|
||||
* The skeleton for this parser was written by Moriyoshi Koizumi and is based on
|
||||
* the work by Masato Bito and is in the PUBLIC DOMAIN.
|
||||
*/
|
||||
class Parser
|
||||
class Parser extends ParserAbstract
|
||||
{
|
||||
const TOKEN_NONE = -1;
|
||||
const TOKEN_INVALID = 154;
|
||||
|
||||
const TOKEN_MAP_SIZE = 389;
|
||||
|
||||
const YYLAST = 1091;
|
||||
@ -25,7 +20,6 @@ class Parser
|
||||
const YYUNEXPECTED = 32767;
|
||||
const YYDEFAULT = -32766;
|
||||
|
||||
// {{{ Tokens
|
||||
const YYERRTOK = 256;
|
||||
const T_INCLUDE = 257;
|
||||
const T_INCLUDE_ONCE = 258;
|
||||
@ -159,9 +153,7 @@ class Parser
|
||||
const T_DIR = 386;
|
||||
const T_NS_SEPARATOR = 387;
|
||||
const T_ELLIPSIS = 388;
|
||||
// }}}
|
||||
|
||||
/* @var array Map of token ids to their respective names */
|
||||
protected static $terminals = array(
|
||||
"EOF",
|
||||
"error",
|
||||
@ -320,7 +312,6 @@ class Parser
|
||||
, "???"
|
||||
);
|
||||
|
||||
/* @var array Map which translates lexer tokens to internal tokens */
|
||||
protected static $translate = array(
|
||||
0, 154, 154, 154, 154, 154, 154, 154, 154, 154,
|
||||
154, 154, 154, 154, 154, 154, 154, 154, 154, 154,
|
||||
@ -1005,195 +996,6 @@ class Parser
|
||||
4, 3, 3, 3, 6, 3, 1, 1, 1
|
||||
);
|
||||
|
||||
protected $yyval;
|
||||
protected $yyastk;
|
||||
protected $stackPos;
|
||||
protected $lexer;
|
||||
|
||||
/**
|
||||
* Creates a parser instance.
|
||||
*
|
||||
* @param Lexer $lexer A lexer
|
||||
*/
|
||||
public function __construct(Lexer $lexer) {
|
||||
$this->lexer = $lexer;
|
||||
}
|
||||
|
||||
/**
|
||||
* Parses PHP code into a node tree.
|
||||
*
|
||||
* @param string $code The source code to parse
|
||||
*
|
||||
* @return Node[] Array of statements
|
||||
*/
|
||||
public function parse($code) {
|
||||
$this->lexer->startLexing($code);
|
||||
|
||||
// We start off with no lookahead-token
|
||||
$tokenId = self::TOKEN_NONE;
|
||||
|
||||
// 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 (+).
|
||||
$startAttributes = array('startLine' => 1);
|
||||
$endAttributes = array();
|
||||
|
||||
// In order to figure out the attributes for the starting token, we have to keep
|
||||
// them in a stack
|
||||
$attributeStack = array($startAttributes);
|
||||
|
||||
// Start off in the initial state and keep a stack of previous states
|
||||
$state = 0;
|
||||
$stateStack = array($state);
|
||||
|
||||
// AST stack (?)
|
||||
$this->yyastk = array();
|
||||
|
||||
// Current position in the stack(s)
|
||||
$this->stackPos = 0;
|
||||
|
||||
for (;;) {
|
||||
if (self::$yybase[$state] == 0) {
|
||||
$yyn = self::$yydefault[$state];
|
||||
} else {
|
||||
if ($tokenId === self::TOKEN_NONE) {
|
||||
// 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.
|
||||
$origTokenId = $this->lexer->getNextToken($tokenValue, $startAttributes, $nextEndAttributes);
|
||||
|
||||
// map the lexer token id to the internally used token id's
|
||||
$tokenId = $origTokenId >= 0 && $origTokenId < self::TOKEN_MAP_SIZE
|
||||
? self::$translate[$origTokenId]
|
||||
: self::TOKEN_INVALID;
|
||||
|
||||
if ($tokenId === self::TOKEN_INVALID) {
|
||||
throw new \RangeException(sprintf(
|
||||
'The lexer returned an invalid token (id=%d, value=%s)',
|
||||
$origTokenId, $tokenValue
|
||||
));
|
||||
}
|
||||
|
||||
$attributeStack[$this->stackPos] = $startAttributes;
|
||||
}
|
||||
|
||||
if ((($yyn = self::$yybase[$state] + $tokenId) >= 0
|
||||
&& $yyn < self::YYLAST && self::$yycheck[$yyn] == $tokenId
|
||||
|| ($state < self::YY2TBLSTATE
|
||||
&& ($yyn = self::$yybase[$state + self::YYNLSTATES] + $tokenId) >= 0
|
||||
&& $yyn < self::YYLAST
|
||||
&& self::$yycheck[$yyn] == $tokenId))
|
||||
&& ($yyn = self::$yyaction[$yyn]) != self::YYDEFAULT) {
|
||||
/*
|
||||
* >= YYNLSTATE: shift and reduce
|
||||
* > 0: shift
|
||||
* = 0: accept
|
||||
* < 0: reduce
|
||||
* = -YYUNEXPECTED: error
|
||||
*/
|
||||
if ($yyn > 0) {
|
||||
/* shift */
|
||||
++$this->stackPos;
|
||||
|
||||
$stateStack[$this->stackPos] = $state = $yyn;
|
||||
$this->yyastk[$this->stackPos] = $tokenValue;
|
||||
$attributeStack[$this->stackPos] = $startAttributes;
|
||||
$endAttributes = $nextEndAttributes;
|
||||
$tokenId = self::TOKEN_NONE;
|
||||
|
||||
if ($yyn < self::YYNLSTATES)
|
||||
continue;
|
||||
|
||||
/* $yyn >= YYNLSTATES means shift-and-reduce */
|
||||
$yyn -= self::YYNLSTATES;
|
||||
} else {
|
||||
$yyn = -$yyn;
|
||||
}
|
||||
} else {
|
||||
$yyn = self::$yydefault[$state];
|
||||
}
|
||||
}
|
||||
|
||||
for (;;) {
|
||||
/* reduce/error */
|
||||
if ($yyn == 0) {
|
||||
/* accept */
|
||||
return $this->yyval;
|
||||
} elseif ($yyn != self::YYUNEXPECTED) {
|
||||
/* reduce */
|
||||
try {
|
||||
$this->{'yyn' . $yyn}(
|
||||
$attributeStack[$this->stackPos - self::$yylen[$yyn]]
|
||||
+ $endAttributes
|
||||
);
|
||||
} catch (Error $e) {
|
||||
if (-1 === $e->getRawLine()) {
|
||||
$e->setRawLine($startAttributes['startLine']);
|
||||
}
|
||||
|
||||
throw $e;
|
||||
}
|
||||
|
||||
/* Goto - shift nonterminal */
|
||||
$this->stackPos -= self::$yylen[$yyn];
|
||||
$yyn = self::$yylhs[$yyn];
|
||||
if (($yyp = self::$yygbase[$yyn] + $stateStack[$this->stackPos]) >= 0
|
||||
&& $yyp < self::YYGLAST
|
||||
&& self::$yygcheck[$yyp] == $yyn) {
|
||||
$state = self::$yygoto[$yyp];
|
||||
} else {
|
||||
$state = self::$yygdefault[$yyn];
|
||||
}
|
||||
|
||||
++$this->stackPos;
|
||||
|
||||
$stateStack[$this->stackPos] = $state;
|
||||
$this->yyastk[$this->stackPos] = $this->yyval;
|
||||
$attributeStack[$this->stackPos] = $startAttributes;
|
||||
} else {
|
||||
/* error */
|
||||
$expected = array();
|
||||
|
||||
$base = self::$yybase[$state];
|
||||
for ($i = 0; $i < self::TOKEN_MAP_SIZE; ++$i) {
|
||||
$n = $base + $i;
|
||||
if ($n >= 0 && $n < self::YYLAST && self::$yycheck[$n] == $i
|
||||
|| $state < self::YY2TBLSTATE
|
||||
&& ($n = self::$yybase[$state + self::YYNLSTATES] + $i) >= 0
|
||||
&& $n < self::YYLAST && self::$yycheck[$n] == $i
|
||||
) {
|
||||
if (self::$yyaction[$n] != self::YYUNEXPECTED) {
|
||||
if (count($expected) == 4) {
|
||||
/* Too many expected tokens */
|
||||
$expected = array();
|
||||
break;
|
||||
}
|
||||
|
||||
$expected[] = self::$terminals[$i];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
$expectedString = '';
|
||||
if ($expected) {
|
||||
$expectedString = ', expecting ' . implode(' or ', $expected);
|
||||
}
|
||||
|
||||
throw new Error(
|
||||
'Syntax error, unexpected ' . self::$terminals[$tokenId] . $expectedString,
|
||||
$startAttributes['startLine']
|
||||
);
|
||||
}
|
||||
|
||||
if ($state < self::YYNLSTATES)
|
||||
break;
|
||||
/* >= YYNLSTATES means shift-and-reduce */
|
||||
$yyn = $state - self::YYNLSTATES;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
protected function yyn0() {
|
||||
$this->yyval = $this->yyastk[$this->stackPos];
|
||||
}
|
||||
|
263
lib/PhpParser/ParserAbstract.php
Normal file
263
lib/PhpParser/ParserAbstract.php
Normal file
@ -0,0 +1,263 @@
|
||||
<?php
|
||||
|
||||
namespace PhpParser;
|
||||
|
||||
/*
|
||||
* This parser is based on a skeleton written by Moriyoshi Koizumi, which in
|
||||
* turn is based on work by Masato Bito.
|
||||
*/
|
||||
abstract class ParserAbstract
|
||||
{
|
||||
/* The following dummy data must be provided by the extending class: */
|
||||
|
||||
const TOKEN_INVALID = 0;
|
||||
const TOKEN_MAP_SIZE = 0;
|
||||
|
||||
const YYLAST = 0;
|
||||
const YY2TBLSTATE = 0;
|
||||
const YYGLAST = 0;
|
||||
const YYNLSTATES = 0;
|
||||
const YYUNEXPECTED = 0;
|
||||
const YYDEFAULT = 0;
|
||||
|
||||
/* @var array Map of token ids to their respective names */
|
||||
protected static $terminals;
|
||||
/* @var array Map which translates lexer tokens to internal tokens */
|
||||
protected static $translate;
|
||||
|
||||
protected static $yyaction;
|
||||
protected static $yycheck;
|
||||
protected static $yybase;
|
||||
protected static $yydefault;
|
||||
protected static $yygoto;
|
||||
protected static $yygcheck;
|
||||
protected static $yygbase;
|
||||
protected static $yygdefault;
|
||||
protected static $yylhs;
|
||||
protected static $yylen;
|
||||
|
||||
/* This is optional data only necessary when debugging */
|
||||
protected static $yyproduction;
|
||||
|
||||
/* End of dummy data */
|
||||
|
||||
const TOKEN_NONE = -1;
|
||||
|
||||
protected $yyval;
|
||||
protected $yyastk;
|
||||
protected $stackPos;
|
||||
protected $lexer;
|
||||
|
||||
/**
|
||||
* Creates a parser instance.
|
||||
*
|
||||
* @param Lexer $lexer A lexer
|
||||
*/
|
||||
public function __construct(Lexer $lexer) {
|
||||
$this->lexer = $lexer;
|
||||
}
|
||||
|
||||
/**
|
||||
* Parses PHP code into a node tree.
|
||||
*
|
||||
* @param string $code The source code to parse
|
||||
*
|
||||
* @return Node[] Array of statements
|
||||
*/
|
||||
public function parse($code) {
|
||||
$this->lexer->startLexing($code);
|
||||
|
||||
// We start off with no lookahead-token
|
||||
$tokenId = self::TOKEN_NONE;
|
||||
|
||||
// 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 (+).
|
||||
$startAttributes = array('startLine' => 1);
|
||||
$endAttributes = array();
|
||||
|
||||
// In order to figure out the attributes for the starting token, we have to keep
|
||||
// them in a stack
|
||||
$attributeStack = array($startAttributes);
|
||||
|
||||
// Start off in the initial state and keep a stack of previous states
|
||||
$state = 0;
|
||||
$stateStack = array($state);
|
||||
|
||||
// AST stack (?)
|
||||
$this->yyastk = array();
|
||||
|
||||
// Current position in the stack(s)
|
||||
$this->stackPos = 0;
|
||||
|
||||
for (;;) {
|
||||
//$this->traceNewState($state, $tokenId);
|
||||
|
||||
if (static::$yybase[$state] == 0) {
|
||||
$yyn = static::$yydefault[$state];
|
||||
} else {
|
||||
if ($tokenId === self::TOKEN_NONE) {
|
||||
// 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.
|
||||
$origTokenId = $this->lexer->getNextToken($tokenValue, $startAttributes, $nextEndAttributes);
|
||||
|
||||
// map the lexer token id to the internally used token id's
|
||||
$tokenId = $origTokenId >= 0 && $origTokenId < static::TOKEN_MAP_SIZE
|
||||
? static::$translate[$origTokenId]
|
||||
: static::TOKEN_INVALID;
|
||||
|
||||
if ($tokenId === static::TOKEN_INVALID) {
|
||||
throw new \RangeException(sprintf(
|
||||
'The lexer returned an invalid token (id=%d, value=%s)',
|
||||
$origTokenId, $tokenValue
|
||||
));
|
||||
}
|
||||
|
||||
$attributeStack[$this->stackPos] = $startAttributes;
|
||||
|
||||
//$this->traceRead($tokenId);
|
||||
}
|
||||
|
||||
if ((($yyn = static::$yybase[$state] + $tokenId) >= 0
|
||||
&& $yyn < static::YYLAST && static::$yycheck[$yyn] == $tokenId
|
||||
|| ($state < static::YY2TBLSTATE
|
||||
&& ($yyn = static::$yybase[$state + static::YYNLSTATES] + $tokenId) >= 0
|
||||
&& $yyn < static::YYLAST
|
||||
&& static::$yycheck[$yyn] == $tokenId))
|
||||
&& ($yyn = static::$yyaction[$yyn]) != static::YYDEFAULT) {
|
||||
/*
|
||||
* >= YYNLSTATE: shift and reduce
|
||||
* > 0: shift
|
||||
* = 0: accept
|
||||
* < 0: reduce
|
||||
* = -YYUNEXPECTED: error
|
||||
*/
|
||||
if ($yyn > 0) {
|
||||
/* shift */
|
||||
//$this->traceShift($tokenId);
|
||||
|
||||
++$this->stackPos;
|
||||
$stateStack[$this->stackPos] = $state = $yyn;
|
||||
$this->yyastk[$this->stackPos] = $tokenValue;
|
||||
$attributeStack[$this->stackPos] = $startAttributes;
|
||||
$endAttributes = $nextEndAttributes;
|
||||
$tokenId = self::TOKEN_NONE;
|
||||
|
||||
if ($yyn < static::YYNLSTATES)
|
||||
continue;
|
||||
|
||||
/* $yyn >= YYNLSTATES means shift-and-reduce */
|
||||
$yyn -= static::YYNLSTATES;
|
||||
} else {
|
||||
$yyn = -$yyn;
|
||||
}
|
||||
} else {
|
||||
$yyn = static::$yydefault[$state];
|
||||
}
|
||||
}
|
||||
|
||||
for (;;) {
|
||||
/* reduce/error */
|
||||
if ($yyn == 0) {
|
||||
/* accept */
|
||||
// $this->traceAccept();
|
||||
return $this->yyval;
|
||||
} elseif ($yyn != static::YYUNEXPECTED) {
|
||||
/* reduce */
|
||||
// $this->traceReduce($yyn);
|
||||
|
||||
try {
|
||||
$this->{'yyn' . $yyn}(
|
||||
$attributeStack[$this->stackPos - static::$yylen[$yyn]]
|
||||
+ $endAttributes
|
||||
);
|
||||
} catch (Error $e) {
|
||||
if (-1 === $e->getRawLine()) {
|
||||
$e->setRawLine($startAttributes['startLine']);
|
||||
}
|
||||
|
||||
throw $e;
|
||||
}
|
||||
|
||||
/* Goto - shift nonterminal */
|
||||
$this->stackPos -= static::$yylen[$yyn];
|
||||
$yyn = static::$yylhs[$yyn];
|
||||
if (($yyp = static::$yygbase[$yyn] + $stateStack[$this->stackPos]) >= 0
|
||||
&& $yyp < static::YYGLAST
|
||||
&& static::$yygcheck[$yyp] == $yyn) {
|
||||
$state = static::$yygoto[$yyp];
|
||||
} else {
|
||||
$state = static::$yygdefault[$yyn];
|
||||
}
|
||||
|
||||
++$this->stackPos;
|
||||
|
||||
$stateStack[$this->stackPos] = $state;
|
||||
$this->yyastk[$this->stackPos] = $this->yyval;
|
||||
$attributeStack[$this->stackPos] = $startAttributes;
|
||||
} else {
|
||||
/* error */
|
||||
$expected = array();
|
||||
|
||||
$base = static::$yybase[$state];
|
||||
for ($i = 0; $i < static::TOKEN_MAP_SIZE; ++$i) {
|
||||
$n = $base + $i;
|
||||
if ($n >= 0 && $n < static::YYLAST && static::$yycheck[$n] == $i
|
||||
|| $state < static::YY2TBLSTATE
|
||||
&& ($n = static::$yybase[$state + static::YYNLSTATES] + $i) >= 0
|
||||
&& $n < static::YYLAST && static::$yycheck[$n] == $i
|
||||
) {
|
||||
if (static::$yyaction[$n] != static::YYUNEXPECTED) {
|
||||
if (count($expected) == 4) {
|
||||
/* Too many expected tokens */
|
||||
$expected = array();
|
||||
break;
|
||||
}
|
||||
|
||||
$expected[] = static::$terminals[$i];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
$expectedString = '';
|
||||
if ($expected) {
|
||||
$expectedString = ', expecting ' . implode(' or ', $expected);
|
||||
}
|
||||
|
||||
throw new Error(
|
||||
'Syntax error, unexpected ' . static::$terminals[$tokenId] . $expectedString,
|
||||
$startAttributes['startLine']
|
||||
);
|
||||
}
|
||||
|
||||
if ($state < static::YYNLSTATES)
|
||||
break;
|
||||
/* >= YYNLSTATES means shift-and-reduce */
|
||||
$yyn = $state - static::YYNLSTATES;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
protected function traceNewState($state, $tokenId) {
|
||||
echo '% State ' . $state
|
||||
. ', Lookahead ' . ($tokenId == self::TOKEN_NONE ? '--none--' : static::$terminals[$tokenId]) . "\n";
|
||||
}
|
||||
|
||||
protected function traceRead($tokenId) {
|
||||
echo '% Reading ' . static::$terminals[$tokenId] . "\n";
|
||||
}
|
||||
|
||||
protected function traceShift($tokenId) {
|
||||
echo '% Shift ' . static::$terminals[$tokenId] . "\n";
|
||||
}
|
||||
|
||||
protected function traceAccept() {
|
||||
echo "% Accepted.\n";
|
||||
}
|
||||
|
||||
protected function traceReduce($n) {
|
||||
echo '% Reduce by (' . $n . ') ' . static::$yyproduction[$n] . "\n";
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user