kmyacc.php.parser 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361
  1. <?php
  2. $meta #
  3. #semval($) $this->yyval
  4. #semval($,%t) $this->yyval
  5. #semval(%n) $this->yyastk[$this->stackPos-(%l-%n)]
  6. #semval(%n,%t) $this->yyastk[$this->stackPos-(%l-%n)]
  7. #include;
  8. /* This is an automatically GENERATED file, which should not be manually edited.
  9. * Instead edit one of the following:
  10. * * the grammar file grammar/zend_language_parser.phpy
  11. * * the parser skeleton grammar/kymacc.php.parser
  12. * * the preprocessing script grammar/rebuildParser.php
  13. *
  14. * The skeleton for this parser was written by Moriyoshi Koizumi and is based on
  15. * the work by Masato Bito and is in the PUBLIC DOMAIN.
  16. */
  17. #if -t
  18. class #(-p)_Debug extends #(-p)
  19. #endif
  20. #ifnot -t
  21. class #(-p)
  22. #endif
  23. {
  24. #ifnot -t
  25. const TOKEN_NONE = -1;
  26. const TOKEN_INVALID = #(YYBADCH);
  27. const TOKEN_MAP_SIZE = #(YYMAXLEX);
  28. const YYLAST = #(YYLAST);
  29. const YY2TBLSTATE = #(YY2TBLSTATE);
  30. const YYGLAST = #(YYGLAST);
  31. const YYNLSTATES = #(YYNLSTATES);
  32. const YYUNEXPECTED = #(YYUNEXPECTED);
  33. const YYDEFAULT = #(YYDEFAULT);
  34. // {{{ Tokens
  35. #tokenval
  36. const %s = %n;
  37. #endtokenval
  38. // }}}
  39. /* @var array Map of token ids to their respective names */
  40. protected static $terminals = array(
  41. #listvar terminals
  42. , "???"
  43. );
  44. /* @var array Map which translates lexer tokens to internal tokens */
  45. protected static $translate = array(
  46. #listvar yytranslate
  47. );
  48. protected static $yyaction = array(
  49. #listvar yyaction
  50. );
  51. protected static $yycheck = array(
  52. #listvar yycheck
  53. );
  54. protected static $yybase = array(
  55. #listvar yybase
  56. );
  57. protected static $yydefault = array(
  58. #listvar yydefault
  59. );
  60. protected static $yygoto = array(
  61. #listvar yygoto
  62. );
  63. protected static $yygcheck = array(
  64. #listvar yygcheck
  65. );
  66. protected static $yygbase = array(
  67. #listvar yygbase
  68. );
  69. protected static $yygdefault = array(
  70. #listvar yygdefault
  71. );
  72. protected static $yylhs = array(
  73. #listvar yylhs
  74. );
  75. protected static $yylen = array(
  76. #listvar yylen
  77. );
  78. protected $yyval;
  79. protected $yyastk;
  80. protected $stackPos;
  81. protected $lexer;
  82. /**
  83. * Creates a parser instance.
  84. *
  85. * @param PHPParser_Lexer $lexer A lexer
  86. */
  87. public function __construct(PHPParser_Lexer $lexer) {
  88. $this->lexer = $lexer;
  89. }
  90. #endif
  91. #if -t
  92. protected static $yyproduction = array(
  93. #production-strings;
  94. );
  95. protected function yyprintln($msg) {
  96. echo $msg, "\n";
  97. }
  98. protected function YYTRACE_NEWSTATE($state, $tokenId) {
  99. $this->yyprintln(
  100. '% State ' . $state
  101. . ', Lookahead ' . ($tokenId == self::TOKEN_NONE ? '--none--' : self::$terminals[$tokenId])
  102. );
  103. }
  104. protected function YYTRACE_READ($tokenId) {
  105. $this->yyprintln('% Reading ' . self::$terminals[$tokenId]);
  106. }
  107. protected function YYTRACE_SHIFT($tokenId) {
  108. $this->yyprintln('% Shift ' . self::$terminals[$tokenId]);
  109. }
  110. protected function YYTRACE_ACCEPT() {
  111. $this->yyprintln('% Accepted.');
  112. }
  113. protected function YYTRACE_REDUCE($n) {
  114. $this->yyprintln('% Reduce by (' . $n . ') ' . self::$yyproduction[$n]);
  115. }
  116. protected function YYTRACE_POP($state) {
  117. $this->yyprintln('% Recovering, uncovers state ' . $state);
  118. }
  119. protected function YYTRACE_DISCARD($tokenId) {
  120. $this->yyprintln('% Discard ' . self::$terminals[$tokenId]);
  121. }
  122. #endif
  123. /**
  124. #ifnot -t
  125. * Parses PHP code into a node tree.
  126. #endif
  127. #if -t
  128. * Parses PHP code into a node tree and prints out debugging information.
  129. #endif
  130. *
  131. * @param string $code The source code to parse
  132. *
  133. * @return PHPParser_Node[] Array of statements
  134. */
  135. public function parse($code) {
  136. $this->lexer->startLexing($code);
  137. // We start off with no lookahead-token
  138. $tokenId = self::TOKEN_NONE;
  139. // The attributes for a node are taken from the first and last token of the node.
  140. // From the first token only the startAttributes are taken and from the last only
  141. // the endAttributes. Both are merged using the array union operator (+).
  142. $startAttributes = array('startLine' => 1);
  143. $endAttributes = array();
  144. // In order to figure out the attributes for the starting token, we have to keep
  145. // them in a stack
  146. $attributeStack = array($startAttributes);
  147. // Start off in the initial state and keep a stack of previous states
  148. $state = 0;
  149. $stateStack = array($state);
  150. // AST stack (?)
  151. $this->yyastk = array();
  152. // Current position in the stack(s)
  153. $this->stackPos = 0;
  154. for (;;) {
  155. #if -t
  156. $this->YYTRACE_NEWSTATE($state, $tokenId);
  157. #endif
  158. if (self::$yybase[$state] == 0) {
  159. $yyn = self::$yydefault[$state];
  160. } else {
  161. if ($tokenId === self::TOKEN_NONE) {
  162. // Fetch the next token id from the lexer and fetch additional info by-ref.
  163. // The end attributes are fetched into a temporary variable and only set once the token is really
  164. // shifted (not during read). Otherwise you would sometimes get off-by-one errors, when a rule is
  165. // reduced after a token was read but not yet shifted.
  166. $origTokenId = $this->lexer->getNextToken($tokenValue, $startAttributes, $nextEndAttributes);
  167. // map the lexer token id to the internally used token id's
  168. $tokenId = $origTokenId >= 0 && $origTokenId < self::TOKEN_MAP_SIZE
  169. ? self::$translate[$origTokenId]
  170. : self::TOKEN_INVALID;
  171. if ($tokenId === self::TOKEN_INVALID) {
  172. throw new RangeException(sprintf(
  173. 'The lexer returned an invalid token (id=%d, value=%s)',
  174. $origTokenId, $tokenValue
  175. ));
  176. }
  177. $attributeStack[$this->stackPos] = $startAttributes;
  178. #if -t
  179. $this->YYTRACE_READ($tokenId);
  180. #endif
  181. }
  182. if ((($yyn = self::$yybase[$state] + $tokenId) >= 0
  183. && $yyn < self::YYLAST && self::$yycheck[$yyn] == $tokenId
  184. || ($state < self::YY2TBLSTATE
  185. && ($yyn = self::$yybase[$state + self::YYNLSTATES] + $tokenId) >= 0
  186. && $yyn < self::YYLAST
  187. && self::$yycheck[$yyn] == $tokenId))
  188. && ($yyn = self::$yyaction[$yyn]) != self::YYDEFAULT) {
  189. /*
  190. * >= YYNLSTATE: shift and reduce
  191. * > 0: shift
  192. * = 0: accept
  193. * < 0: reduce
  194. * = -YYUNEXPECTED: error
  195. */
  196. if ($yyn > 0) {
  197. /* shift */
  198. #if -t
  199. $this->YYTRACE_SHIFT($tokenId);
  200. #endif
  201. ++$this->stackPos;
  202. $stateStack[$this->stackPos] = $state = $yyn;
  203. $this->yyastk[$this->stackPos] = $tokenValue;
  204. $attributeStack[$this->stackPos] = $startAttributes;
  205. $endAttributes = $nextEndAttributes;
  206. $tokenId = self::TOKEN_NONE;
  207. if ($yyn < self::YYNLSTATES)
  208. continue;
  209. /* $yyn >= YYNLSTATES means shift-and-reduce */
  210. $yyn -= self::YYNLSTATES;
  211. } else {
  212. $yyn = -$yyn;
  213. }
  214. } else {
  215. $yyn = self::$yydefault[$state];
  216. }
  217. }
  218. for (;;) {
  219. /* reduce/error */
  220. if ($yyn == 0) {
  221. /* accept */
  222. #if -t
  223. $this->YYTRACE_ACCEPT();
  224. #endif
  225. return $this->yyval;
  226. } elseif ($yyn != self::YYUNEXPECTED) {
  227. /* reduce */
  228. #if -t
  229. $this->YYTRACE_REDUCE($yyn);
  230. #endif
  231. try {
  232. $this->{'yyn' . $yyn}(
  233. $attributeStack[$this->stackPos - self::$yylen[$yyn]]
  234. + $endAttributes
  235. );
  236. } catch (PHPParser_Error $e) {
  237. if (-1 === $e->getRawLine()) {
  238. $e->setRawLine($startAttributes['startLine']);
  239. }
  240. throw $e;
  241. }
  242. /* Goto - shift nonterminal */
  243. $this->stackPos -= self::$yylen[$yyn];
  244. $yyn = self::$yylhs[$yyn];
  245. if (($yyp = self::$yygbase[$yyn] + $stateStack[$this->stackPos]) >= 0
  246. && $yyp < self::YYGLAST
  247. && self::$yygcheck[$yyp] == $yyn) {
  248. $state = self::$yygoto[$yyp];
  249. } else {
  250. $state = self::$yygdefault[$yyn];
  251. }
  252. ++$this->stackPos;
  253. $stateStack[$this->stackPos] = $state;
  254. $this->yyastk[$this->stackPos] = $this->yyval;
  255. $attributeStack[$this->stackPos] = $startAttributes;
  256. } else {
  257. /* error */
  258. $expected = array();
  259. $base = self::$yybase[$state];
  260. for ($i = 0; $i < self::TOKEN_MAP_SIZE; ++$i) {
  261. $n = $base + $i;
  262. if ($n >= 0 && $n < self::YYLAST && self::$yycheck[$n] == $i
  263. || $state < self::YY2TBLSTATE
  264. && ($n = self::$yybase[$state + self::YYNLSTATES] + $i) >= 0
  265. && $n < self::YYLAST && self::$yycheck[$n] == $i
  266. ) {
  267. if (self::$yyaction[$n] != self::YYUNEXPECTED) {
  268. if (count($expected) == 4) {
  269. /* Too many expected tokens */
  270. $expected = array();
  271. break;
  272. }
  273. $expected[] = self::$terminals[$i];
  274. }
  275. }
  276. }
  277. $expectedString = '';
  278. if ($expected) {
  279. $expectedString = ', expecting ' . implode(' or ', $expected);
  280. }
  281. throw new PHPParser_Error(
  282. 'Syntax error, unexpected ' . self::$terminals[$tokenId] . $expectedString,
  283. $startAttributes['startLine']
  284. );
  285. }
  286. if ($state < self::YYNLSTATES)
  287. break;
  288. /* >= YYNLSTATES means shift-and-reduce */
  289. $yyn = $state - self::YYNLSTATES;
  290. }
  291. }
  292. }
  293. #ifnot -t
  294. #reduce
  295. protected function yyn%n($attributes) {
  296. %b
  297. }
  298. #noact
  299. protected function yyn%n() {
  300. $this->yyval = $this->yyastk[$this->stackPos];
  301. }
  302. #endreduce
  303. #endif
  304. }
  305. #tailcode;