php-parser/grammar/kmyacc.php.parser

289 lines
8.0 KiB
Plaintext
Raw Normal View History

2011-04-18 19:02:30 +02:00
<?php
$meta #
#semval($) $this->yyval
#semval($,%t) $this->yyval
#semval(%n) $this->yyastk[$this->yysp-(%l-%n)]
#semval(%n,%t) $this->yyastk[$this->yysp-(%l-%n)]
#include;
2011-04-18 19:02:30 +02:00
/* Prototype file of classed PHP parser.
* Written by Moriyoshi Koizumi, based on the work by Masato Bito.
* This file is PUBLIC DOMAIN.
*/
#if -p
class #(-p)
#endif
#ifnot -p
2011-04-18 19:02:30 +02:00
class YYParser
#endif
2011-04-18 19:02:30 +02:00
{
const YYBADCH = #(YYBADCH);
const YYMAXLEX = #(YYMAXLEX);
const YYTERMS = #(YYTERMS);
const YYNONTERMS = #(YYNONTERMS);
const YYLAST = #(YYLAST);
const YY2TBLSTATE = #(YY2TBLSTATE);
const YYGLAST = #(YYGLAST);
const YYSTATES = #(YYSTATES);
const YYNLSTATES = #(YYNLSTATES);
const YYINTERRTOK = #(YYINTERRTOK);
const YYUNEXPECTED = #(YYUNEXPECTED);
const YYDEFAULT = #(YYDEFAULT);
2011-04-18 19:02:30 +02:00
// {{{ Tokens
#tokenval
2011-04-18 19:02:30 +02:00
const %s = %n;
#endtokenval
2011-04-18 19:02:30 +02:00
// }}}
private static $yyterminals = array(
#listvar terminals
2011-04-18 19:02:30 +02:00
, "???"
);
#if -t
2011-04-18 19:02:30 +02:00
private static $yyproduction = array(
#production-strings;
2011-04-18 19:02:30 +02:00
);
#endif
2011-04-18 19:02:30 +02:00
private static $yytranslate = array(
#listvar yytranslate
2011-04-18 19:02:30 +02:00
);
private static $yyaction = array(
#listvar yyaction
2011-04-18 19:02:30 +02:00
);
private static $yycheck = array(
#listvar yycheck
2011-04-18 19:02:30 +02:00
);
private static $yybase = array(
#listvar yybase
2011-04-18 19:02:30 +02:00
);
private static $yydefault = array(
#listvar yydefault
2011-04-18 19:02:30 +02:00
);
private static $yygoto = array(
#listvar yygoto
2011-04-18 19:02:30 +02:00
);
private static $yygcheck = array(
#listvar yygcheck
2011-04-18 19:02:30 +02:00
);
private static $yygbase = array(
#listvar yygbase
2011-04-18 19:02:30 +02:00
);
private static $yygdefault = array(
#listvar yygdefault
2011-04-18 19:02:30 +02:00
);
private static $yylhs = array(
#listvar yylhs
2011-04-18 19:02:30 +02:00
);
private static $yylen = array(
#listvar yylen
2011-04-18 19:02:30 +02:00
);
#if -t
/* Debug Mode */
2011-04-18 19:02:30 +02:00
protected function yyprintln($msg) {
echo $msg, "\n";
}
private function YYTRACE_NEWSTATE($state, $sym) {
$this->yyprintln(
'% State ' . $state
. ', Lookahead ' . ($sym < 0 ? '--none--' : self::$yyterminals[$sym])
);
2011-04-18 19:02:30 +02:00
}
private function YYTRACE_READ($sym) {
$this->yyprintln('% Reading ' . self::$yyterminals[$sym]);
2011-04-18 19:02:30 +02:00
}
private function YYTRACE_SHIFT($sym) {
$this->yyprintln('% Shift ' . self::$yyterminals[$sym]);
2011-04-18 19:02:30 +02:00
}
private function YYTRACE_ACCEPT() {
$this->yyprintln('% Accepted.');
2011-04-18 19:02:30 +02:00
}
private function YYTRACE_REDUCE($n) {
$this->yyprintln('% Reduce by (' . $n . ') ' . self::$yyproduction[$n]);
2011-04-18 19:02:30 +02:00
}
private function YYTRACE_POP($state) {
$this->yyprintln('% Recovering, uncovers state ' . $state);
2011-04-18 19:02:30 +02:00
}
private function YYTRACE_DISCARD($sym) {
$this->yyprintln('% Discard ' . self::$yyterminals[$sym]);
2011-04-18 19:02:30 +02:00
}
#endif
protected $yyval;
protected $yyastk;
protected $yysp;
protected $yyaccept;
protected $lexer;
2011-04-18 19:02:30 +02:00
/**
#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 PHPParser_Lexer $lexer A lexer
*
* @return array Array of statements
2011-04-18 19:02:30 +02:00
*/
public function parse(PHPParser_Lexer $lexer) {
2011-06-28 14:11:12 +02:00
$this->lexer = $lexer;
2011-06-28 14:11:12 +02:00
$this->yysp = 0; // Stack pos
$yysstk = array($yystate = 0); // State stack
$this->yyastk = array(); // AST stack (?)
$yylstk = array($yyline = 1); // Line stack
$yydstk = array($yyDC = null); // Doc comment stack
2011-04-18 19:02:30 +02:00
2011-06-28 14:11:12 +02:00
$yychar = -1;
2011-04-18 19:02:30 +02:00
for (;;) {
#if -t
2011-04-18 19:02:30 +02:00
$this->YYTRACE_NEWSTATE($yystate, $yychar);
#endif
2011-04-18 19:02:30 +02:00
if (self::$yybase[$yystate] == 0) {
$yyn = self::$yydefault[$yystate];
} else {
if ($yychar < 0) {
if (($yychar = $lexer->lex($yylval, $yyline, $yyDC)) < 0)
2011-04-18 19:02:30 +02:00
$yychar = 0;
$yychar = $yychar < self::YYMAXLEX ?
self::$yytranslate[$yychar] : self::YYBADCH;
2011-06-28 14:11:12 +02:00
$yylstk[$this->yysp] = $yyline;
$yydstk[$this->yysp] = $yyDC;
#if -t
2011-04-18 19:02:30 +02:00
$this->YYTRACE_READ($yychar);
#endif
2011-04-18 19:02:30 +02:00
}
if ((($yyn = self::$yybase[$yystate] + $yychar) >= 0
&& $yyn < self::YYLAST && self::$yycheck[$yyn] == $yychar
|| ($yystate < self::YY2TBLSTATE
&& ($yyn = self::$yybase[$yystate + self::YYNLSTATES]
+ $yychar) >= 0
&& $yyn < self::YYLAST
&& self::$yycheck[$yyn] == $yychar))
&& ($yyn = self::$yyaction[$yyn]) != self::YYDEFAULT) {
/*
* >= YYNLSTATE: shift and reduce
* > 0: shift
* = 0: accept
* < 0: reduce
* = -YYUNEXPECTED: error
*/
if ($yyn > 0) {
/* shift */
#if -t
2011-04-18 19:02:30 +02:00
$this->YYTRACE_SHIFT($yychar);
#endif
++$this->yysp;
2011-04-18 19:02:30 +02:00
2011-06-28 14:11:12 +02:00
$yysstk[$this->yysp] = $yystate = $yyn;
2011-04-18 19:02:30 +02:00
$this->yyastk[$this->yysp] = $yylval;
2011-06-28 14:11:12 +02:00
$yylstk[$this->yysp] = $yyline;
$yydstk[$this->yysp] = $yyDC;
2011-04-18 19:02:30 +02:00
$yychar = -1;
if ($yyn < self::YYNLSTATES)
continue;
/* $yyn >= YYNLSTATES means shift-and-reduce */
$yyn -= self::YYNLSTATES;
} else {
$yyn = -$yyn;
}
} else {
$yyn = self::$yydefault[$yystate];
}
}
for (;;) {
/* reduce/error */
if ($yyn == 0) {
/* accept */
#if -t
2011-04-18 19:02:30 +02:00
$this->YYTRACE_ACCEPT();
#endif
return $this->yyval;
2011-04-18 19:02:30 +02:00
} elseif ($yyn != self::YYUNEXPECTED) {
/* reduce */
#if -t
2011-04-18 19:02:30 +02:00
$this->YYTRACE_REDUCE($yyn);
#endif
try {
$this->{'yyn' . $yyn}(
$yylstk[$this->yysp - self::$yylen[$yyn]],
$yydstk[$this->yysp - self::$yylen[$yyn]]
);
} catch (PHPParser_Error $e) {
2011-06-28 14:11:12 +02:00
$e->setRawLine($yyline);
throw $e;
}
/* Goto - shift nonterminal */
$this->yysp -= self::$yylen[$yyn];
$yyn = self::$yylhs[$yyn];
if (($yyp = self::$yygbase[$yyn] + $yysstk[$this->yysp]) >= 0
&& $yyp < self::YYGLAST
&& self::$yygcheck[$yyp] == $yyn) {
$yystate = self::$yygoto[$yyp];
2011-04-18 19:02:30 +02:00
} else {
$yystate = self::$yygdefault[$yyn];
}
2011-04-18 19:02:30 +02:00
++$this->yysp;
2011-04-18 19:02:30 +02:00
$yysstk[$this->yysp] = $yystate;
$this->yyastk[$this->yysp] = $this->yyval;
2011-06-28 14:11:12 +02:00
$yylstk[$this->yysp] = $yyline;
$yydstk[$this->yysp] = $yyDC;
2011-04-18 19:02:30 +02:00
} else {
/* error */
throw new PHPParser_Error(
'Unexpected token ' . self::$yyterminals[$yychar],
2011-06-28 14:11:12 +02:00
$yyline
);
2011-04-18 19:02:30 +02:00
}
if ($yystate < self::YYNLSTATES)
break;
/* >= YYNLSTATES means shift-and-reduce */
$yyn = $yystate - self::YYNLSTATES;
}
}
}
#reduce
2011-04-18 19:02:30 +02:00
private function yyn%n($line, $docComment) {
2011-04-18 19:02:30 +02:00
%b
}
#noact
private function yyn%n() {
$this->yyval = $this->yyastk[$this->yysp];
}
#endreduce
2011-04-18 19:02:30 +02:00
}
#tailcode;