PageRenderTime 39ms CodeModel.GetById 13ms RepoModel.GetById 0ms app.codeStats 1ms

/pkg/vtiger/extensions/Webservices/third-party/ParserGenerator/Symbol.php

https://bitbucket.org/thomashii/vtigercrm-6-for-postgresql
PHP | 260 lines | 78 code | 6 blank | 176 comment | 12 complexity | b762c3b512db9b906d140d754471b6ce MD5 | raw file
Possible License(s): Apache-2.0, LGPL-3.0, LGPL-2.1, GPL-2.0, GPL-3.0
  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: This source file is subject to version 3.01 of the PHP license
  11. * that is available through the world-wide-web at the following URI:
  12. * http://www.php.net/license/3_01.txt. If you did not receive a copy of
  13. * the PHP License and are unable to obtain it through the web, please
  14. * send a note to license@php.net so we can mail you a copy immediately.
  15. *
  16. * @category php
  17. * @package PHP_ParserGenerator
  18. * @author Gregory Beaver <cellog@php.net>
  19. * @copyright 2006 Gregory Beaver
  20. * @license http://www.php.net/license/3_01.txt PHP License 3.01
  21. * @version CVS: $Id$
  22. * @since File available since Release 0.1.0
  23. */
  24. /**
  25. * Symbols (terminals and nonterminals) of the grammar are stored in this class
  26. *
  27. * @package PHP_ParserGenerator
  28. * @author Gregory Beaver <cellog@php.net>
  29. * @copyright 2006 Gregory Beaver
  30. * @license http://www.php.net/license/3_01.txt PHP License 3.01
  31. * @version 0.1.0
  32. * @since Class available since Release 0.1.0
  33. */
  34. class PHP_ParserGenerator_Symbol
  35. {
  36. /**
  37. * Symbols that start with a capital letter like FOO.
  38. *
  39. * These are tokens directly from the lexer
  40. */
  41. const TERMINAL = 1;
  42. /**
  43. * Symbols that start with a lower-case letter like foo.
  44. *
  45. * These are grammar rules like "foo ::= BLAH."
  46. */
  47. const NONTERMINAL = 2;
  48. /**
  49. * Multiple terminal symbols.
  50. *
  51. * These are a grammar rule that consists of several terminals like
  52. * FOO|BAR|BAZ. Note that non-terminals cannot be in a multi-terminal,
  53. * and a multi-terminal acts like a single terminal.
  54. *
  55. * "FOO|BAR FOO|BAZ" is actually two multi-terminals, FOO|BAR and FOO|BAZ.
  56. */
  57. const MULTITERMINAL = 3;
  58. const LEFT = 1;
  59. const RIGHT = 2;
  60. const NONE = 3;
  61. const UNK = 4;
  62. /**
  63. * Name of the symbol
  64. *
  65. * @var string
  66. */
  67. public $name;
  68. /**
  69. * Index of this symbol.
  70. *
  71. * This will ultimately end up representing the symbol in the generated
  72. * parser
  73. * @var int
  74. */
  75. public $index;
  76. /**
  77. * Symbol type
  78. *
  79. * One of PHP_ParserGenerator_Symbol::TERMINAL,
  80. * PHP_ParserGenerator_Symbol::NONTERMINAL or
  81. * PHP_ParserGenerator_Symbol::MULTITERMINAL
  82. * @var int
  83. */
  84. public $type;
  85. /**
  86. * Linked list of rules that use this symbol, if it is a non-terminal.
  87. * @var PHP_ParserGenerator_Rule
  88. */
  89. public $rule;
  90. /**
  91. * Fallback token in case this token doesn't parse
  92. * @var PHP_ParserGenerator_Symbol
  93. */
  94. public $fallback;
  95. /**
  96. * Precendence, if defined.
  97. *
  98. * -1 if no unusual precedence
  99. * @var int
  100. */
  101. public $prec = -1;
  102. /**
  103. * Associativity if precedence is defined.
  104. *
  105. * One of PHP_ParserGenerator_Symbol::LEFT,
  106. * PHP_ParserGenerator_Symbol::RIGHT, PHP_ParserGenerator_Symbol::NONE
  107. * or PHP_ParserGenerator_Symbol::UNK
  108. * @var unknown_type
  109. */
  110. public $assoc;
  111. /**
  112. * First-set for all rules of this symbol
  113. *
  114. * @var array
  115. */
  116. public $firstset;
  117. /**
  118. * True if this symbol is a non-terminal and can generate an empty
  119. * result.
  120. *
  121. * For instance "foo ::= ."
  122. * @var boolean
  123. */
  124. public $lambda;
  125. /**
  126. * Code that executes whenever this symbol is popped from the stack during
  127. * error processing.
  128. *
  129. * @var string|0
  130. */
  131. public $destructor = 0;
  132. /**
  133. * Line number of destructor code
  134. * @var int
  135. */
  136. public $destructorln;
  137. /**
  138. * Unused relic of the C version of Lemon.
  139. *
  140. * The data type of information held by this object. Only used
  141. * if this is a non-terminal
  142. * @var string
  143. */
  144. public $datatype;
  145. /**
  146. * Unused relic of the C version of Lemon.
  147. *
  148. * The data type number. In the parser, the value
  149. * stack is a union. The .yy%d element of this
  150. * union is the correct data type for this object
  151. * @var string
  152. */
  153. public $dtnum;
  154. /**#@+
  155. * The following fields are used by MULTITERMINALs only
  156. */
  157. /**
  158. * Number of terminal symbols in the MULTITERMINAL
  159. *
  160. * This is of course the same as count($this->subsym)
  161. * @var int
  162. */
  163. public $nsubsym;
  164. /**
  165. * Array of terminal symbols in the MULTITERMINAL
  166. * @var array an array of {@link PHP_ParserGenerator_Symbol} objects
  167. */
  168. public $subsym = array();
  169. /**#@-*/
  170. /**
  171. * Singleton storage of symbols
  172. *
  173. * @var array an array of PHP_ParserGenerator_Symbol objects
  174. */
  175. private static $symbol_table = array();
  176. /**
  177. * Return a pointer to the (terminal or nonterminal) symbol "x".
  178. * Create a new symbol if this is the first time "x" has been seen.
  179. * (this is a singleton)
  180. * @param string
  181. * @return PHP_ParserGenerator_Symbol
  182. */
  183. public static function Symbol_new($x)
  184. {
  185. if (isset(self::$symbol_table[$x])) {
  186. return self::$symbol_table[$x];
  187. }
  188. $sp = new PHP_ParserGenerator_Symbol;
  189. $sp->name = $x;
  190. $sp->type = preg_match('/[A-Z]/', $x[0]) ? self::TERMINAL : self::NONTERMINAL;
  191. $sp->rule = 0;
  192. $sp->fallback = 0;
  193. $sp->prec = -1;
  194. $sp->assoc = self::UNK;
  195. $sp->firstset = array();
  196. $sp->lambda = false;
  197. $sp->destructor = 0;
  198. $sp->datatype = 0;
  199. self::$symbol_table[$sp->name] = $sp;
  200. return $sp;
  201. }
  202. /**
  203. * Return the number of unique symbols
  204. * @return int
  205. */
  206. public static function Symbol_count()
  207. {
  208. return count(self::$symbol_table);
  209. }
  210. public static function Symbol_arrayof()
  211. {
  212. return array_values(self::$symbol_table);
  213. }
  214. public static function Symbol_find($x)
  215. {
  216. if (isset(self::$symbol_table[$x])) {
  217. return self::$symbol_table[$x];
  218. }
  219. return 0;
  220. }
  221. /**
  222. * Sort function helper for symbols
  223. *
  224. * Symbols that begin with upper case letters (terminals or tokens)
  225. * must sort before symbols that begin with lower case letters
  226. * (non-terminals). Other than that, the order does not matter.
  227. *
  228. * We find experimentally that leaving the symbols in their original
  229. * order (the order they appeared in the grammar file) gives the
  230. * smallest parser tables in SQLite.
  231. * @param PHP_ParserGenerator_Symbol
  232. * @param PHP_ParserGenerator_Symbol
  233. */
  234. public static function sortSymbols($a, $b)
  235. {
  236. $i1 = $a->index + 10000000*(ord($a->name[0]) > ord('Z'));
  237. $i2 = $b->index + 10000000*(ord($b->name[0]) > ord('Z'));
  238. return $i1 - $i2;
  239. }
  240. /**
  241. * Return true if two symbols are the same.
  242. */
  243. public static function same_symbol(PHP_ParserGenerator_Symbol $a, PHP_ParserGenerator_Symbol $b)
  244. {
  245. if ($a === $b) return 1;
  246. if ($a->type != self::MULTITERMINAL) return 0;
  247. if ($b->type != self::MULTITERMINAL) return 0;
  248. if ($a->nsubsym != $b->nsubsym) return 0;
  249. for ($i = 0; $i < $a->nsubsym; $i++) {
  250. if ($a->subsym[$i] != $b->subsym[$i]) return 0;
  251. }
  252. return 1;
  253. }
  254. }