State.php 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  1. <?php
  2. /**
  3. * PHP_ParserGenerator, a php 5 parser generator.
  4. *
  5. * This is a direct port of the Lemon parser generator, found at
  6. * {@link http://www.hwaci.com/sw/lemon/}
  7. *
  8. * PHP version 5
  9. *
  10. * LICENSE:
  11. *
  12. * Copyright (c) 2006, Gregory Beaver <cellog@php.net>
  13. * All rights reserved.
  14. *
  15. * Redistribution and use in source and binary forms, with or without
  16. * modification, are permitted provided that the following conditions
  17. * are met:
  18. *
  19. * * Redistributions of source code must retain the above copyright
  20. * notice, this list of conditions and the following disclaimer.
  21. * * Redistributions in binary form must reproduce the above copyright
  22. * notice, this list of conditions and the following disclaimer in
  23. * the documentation and/or other materials provided with the distribution.
  24. * * Neither the name of the PHP_ParserGenerator nor the names of its
  25. * contributors may be used to endorse or promote products derived
  26. * from this software without specific prior written permission.
  27. *
  28. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
  29. * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
  30. * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  31. * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  32. * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  33. * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  34. * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  35. * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
  36. * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  37. * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  38. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  39. *
  40. * @category PHP
  41. * @package PHP_ParserGenerator
  42. * @author Gregory Beaver <cellog@php.net>
  43. * @copyright 2006 Gregory Beaver
  44. * @license http://www.opensource.org/licenses/bsd-license.php New BSD License
  45. * @version CVS: $Id: State.php 302382 2010-08-17 06:08:09Z jespino $
  46. * @link http://pear.php.net/package/PHP_ParserGenerator
  47. * @since File available since Release 0.1.0
  48. */
  49. /**
  50. * The structure used to represent a state in the associative array
  51. * for a PHP_ParserGenerator_Config.
  52. *
  53. * @category PHP
  54. * @package PHP_ParserGenerator
  55. * @author Gregory Beaver <cellog@php.net>
  56. * @copyright 2006 Gregory Beaver
  57. * @license http://www.opensource.org/licenses/bsd-license.php New BSD License
  58. * @version Release: @package_version@
  59. * @link http://pear.php.net/package/PHP_ParserGenerator
  60. * @since Class available since Release 0.1.0
  61. */
  62. class PHP_ParserGenerator_StateNode
  63. {
  64. public $key;
  65. public $data;
  66. public $from = 0;
  67. public $next = 0;
  68. }
  69. /**
  70. * Each state of the generated parser's finite state machine
  71. * is encoded as an instance of this class
  72. *
  73. * @package PHP_ParserGenerator
  74. * @author Gregory Beaver <cellog@php.net>
  75. * @copyright 2006 Gregory Beaver
  76. * @license http://www.php.net/license/3_01.txt PHP License 3.01
  77. * @version Release: @package_version@
  78. * @link http://pear.php.net/package/PHP_ParserGenerator
  79. * @since Class available since Release 0.1.0
  80. */
  81. class PHP_ParserGenerator_State
  82. {
  83. /**
  84. * The basis configurations for this state
  85. * @var PHP_ParserGenerator_Config
  86. */
  87. public $bp;
  88. /**
  89. * All configurations in this state
  90. * @var PHP_ParserGenerator_Config
  91. */
  92. public $cfp;
  93. /**
  94. * Sequential number for this state
  95. *
  96. * @var int
  97. */
  98. public $statenum;
  99. /**
  100. * Linked list of actions for this state.
  101. * @var PHP_ParserGenerator_Action
  102. */
  103. public $ap;
  104. /**
  105. * Number of terminal (token) actions
  106. *
  107. * @var int
  108. */
  109. public $nTknAct,
  110. /**
  111. * Number of non-terminal actions
  112. *
  113. * @var int
  114. */
  115. $nNtAct;
  116. /**
  117. * The offset into the $yy_action table for terminal tokens.
  118. *
  119. * @var int
  120. */
  121. public $iTknOfst,
  122. /**
  123. * The offset into the $yy_action table for non-terminals.
  124. *
  125. * @var int
  126. */
  127. $iNtOfst;
  128. /**
  129. * Default action
  130. *
  131. * @var int
  132. */
  133. public $iDflt;
  134. /**
  135. * Associative array of PHP_ParserGenerator_State objects
  136. *
  137. * @var array
  138. */
  139. public static $x3a = array();
  140. /**
  141. * Array of PHP_ParserGenerator_State objects
  142. *
  143. * @var array
  144. */
  145. public static $states = array();
  146. /**
  147. * Compare two states for sorting purposes. The smaller state is the
  148. * one with the most non-terminal actions. If they have the same number
  149. * of non-terminal actions, then the smaller is the one with the most
  150. * token actions.
  151. */
  152. static function stateResortCompare($a, $b)
  153. {
  154. $n = $b->nNtAct - $a->nNtAct;
  155. if ($n === 0) {
  156. $n = $b->nTknAct - $a->nTknAct;
  157. }
  158. return $n;
  159. }
  160. /**
  161. * Compare two states based on their configurations
  162. *
  163. * @param PHP_ParserGenerator_Config|0 $a
  164. * @param PHP_ParserGenerator_Config|0 $b
  165. * @return int
  166. */
  167. static function statecmp($a, $b)
  168. {
  169. for ($rc = 0; $rc == 0 && $a && $b; $a = $a->bp, $b = $b->bp) {
  170. $rc = $a->rp->index - $b->rp->index;
  171. if ($rc === 0) {
  172. $rc = $a->dot - $b->dot;
  173. }
  174. }
  175. if ($rc == 0) {
  176. if ($a) {
  177. $rc = 1;
  178. }
  179. if ($b) {
  180. $rc = -1;
  181. }
  182. }
  183. return $rc;
  184. }
  185. /**
  186. * Hash a state based on its configuration
  187. *
  188. * @return int
  189. */
  190. private static function statehash(PHP_ParserGenerator_Config $a)
  191. {
  192. $h = 0;
  193. while ($a) {
  194. $h = $h * 571 + $a->rp->index * 37 + $a->dot;
  195. $a = $a->bp;
  196. }
  197. return (int) $h;
  198. }
  199. /**
  200. * Return a pointer to data assigned to the given key. Return NULL
  201. * if no such key.
  202. * @param PHP_ParserGenerator_Config
  203. * @return null|PHP_ParserGenerator_State
  204. */
  205. static function State_find(PHP_ParserGenerator_Config $key)
  206. {
  207. if (!count(self::$x3a)) {
  208. return 0;
  209. }
  210. $h = self::statehash($key);
  211. if (!isset(self::$x3a[$h])) {
  212. return 0;
  213. }
  214. $np = self::$x3a[$h];
  215. while ($np) {
  216. if (self::statecmp($np->key, $key) == 0) {
  217. break;
  218. }
  219. $np = $np->next;
  220. }
  221. return $np ? $np->data : 0;
  222. }
  223. /**
  224. * Insert a new record into the array. Return TRUE if successful.
  225. * Prior data with the same key is NOT overwritten
  226. *
  227. * @param PHP_ParserGenerator_State $state
  228. * @param PHP_ParserGenerator_Config $key
  229. * @return unknown
  230. */
  231. static function State_insert(PHP_ParserGenerator_State $state, PHP_ParserGenerator_Config $key)
  232. {
  233. $h = self::statehash($key);
  234. if (isset(self::$x3a[$h])) {
  235. $np = self::$x3a[$h];
  236. } else {
  237. $np = 0;
  238. }
  239. while ($np) {
  240. if (self::statecmp($np->key, $key) == 0) {
  241. /* An existing entry with the same key is found. */
  242. /* Fail because overwrite is not allows. */
  243. return 0;
  244. }
  245. $np = $np->next;
  246. }
  247. /* Insert the new data */
  248. $np = new PHP_ParserGenerator_StateNode;
  249. $np->key = $key;
  250. $np->data = $state;
  251. self::$states[] = $np;
  252. // the original lemon code sets the from link always to itself
  253. // setting up a faulty double-linked list
  254. // however, the from links are never used, so I suspect a copy/paste
  255. // error from a standard algorithm that was never caught
  256. if (isset(self::$x3a[$h])) {
  257. self::$x3a[$h]->from = $np; // lemon has $np->next here
  258. } else {
  259. self::$x3a[$h] = 0; // dummy to avoid notice
  260. }
  261. $np->next = self::$x3a[$h];
  262. self::$x3a[$h] = $np;
  263. $np->from = self::$x3a[$h];
  264. return 1;
  265. }
  266. /**
  267. * Get an array indexed by state number
  268. *
  269. * @return array
  270. */
  271. static function State_arrayof()
  272. {
  273. return self::$states;
  274. }
  275. }