You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

336 lines
7.1 KiB

  1. <?php
  2. /**
  3. * Hoa
  4. *
  5. *
  6. * @license
  7. *
  8. * New BSD License
  9. *
  10. * Copyright © 2007-2016, Hoa community. All rights reserved.
  11. *
  12. * Redistribution and use in source and binary forms, with or without
  13. * modification, are permitted provided that the following conditions are met:
  14. * * Redistributions of source code must retain the above copyright
  15. * notice, this list of conditions and the following disclaimer.
  16. * * Redistributions in binary form must reproduce the above copyright
  17. * notice, this list of conditions and the following disclaimer in the
  18. * documentation and/or other materials provided with the distribution.
  19. * * Neither the name of the Hoa nor the names of its contributors may be
  20. * used to endorse or promote products derived from this software without
  21. * specific prior written permission.
  22. *
  23. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  24. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  25. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  26. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS AND CONTRIBUTORS BE
  27. * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  28. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  29. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  30. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  31. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  32. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  33. * POSSIBILITY OF SUCH DAMAGE.
  34. */
  35. namespace Hoa\Compiler\Llk;
  36. use Hoa\Visitor;
  37. /**
  38. * Class \Hoa\Compiler\Llk\TreeNode.
  39. *
  40. * Provide a generic node for the AST produced by LL(k) parser.
  41. *
  42. * @copyright Copyright © 2007-2016 Hoa community
  43. * @license New BSD License
  44. */
  45. class TreeNode implements Visitor\Element
  46. {
  47. /**
  48. * ID (should be something like #ruleName or token).
  49. *
  50. * @var string
  51. */
  52. protected $_id = null;
  53. /**
  54. * Value of the node (non-null for token nodes).
  55. *
  56. * @var string
  57. */
  58. protected $_value = null;
  59. /**
  60. * Children.
  61. *
  62. * @var array
  63. */
  64. protected $_children = null;
  65. /**
  66. * Parent.
  67. *
  68. * @var \Hoa\Compiler\Llk\TreeNode
  69. */
  70. protected $_parent = null;
  71. /**
  72. * Attached data.
  73. *
  74. * @var array
  75. */
  76. protected $_data = [];
  77. /**
  78. * Constructor.
  79. *
  80. * @param string $id ID.
  81. * @param string $value Value.
  82. * @param array $children Children.
  83. * @param \Hoa\Compiler\Llk\TreeNode $parent Parent.
  84. * @return void
  85. */
  86. public function __construct(
  87. $id,
  88. $value = null,
  89. array $children = [],
  90. TreeNode $parent = null
  91. ) {
  92. $this->setId($id);
  93. $this->setValue($value);
  94. $this->setChildren($children);
  95. if (null !== $parent) {
  96. $this->setParent($parent);
  97. }
  98. return;
  99. }
  100. /**
  101. * Set ID.
  102. *
  103. * @param string $id ID.
  104. * @return string
  105. */
  106. public function setId($id)
  107. {
  108. $old = $this->_id;
  109. $this->_id = $id;
  110. return $old;
  111. }
  112. /**
  113. * Get ID.
  114. *
  115. * @return string
  116. */
  117. public function getId()
  118. {
  119. return $this->_id;
  120. }
  121. /**
  122. * Set value.
  123. *
  124. * @param array $value Value (token & value).
  125. * @return array
  126. */
  127. public function setValue($value)
  128. {
  129. $old = $this->_value;
  130. $this->_value = $value;
  131. return $old;
  132. }
  133. /**
  134. * Get value.
  135. *
  136. * @return array
  137. */
  138. public function getValue()
  139. {
  140. return $this->_value;
  141. }
  142. /**
  143. * Get value token.
  144. *
  145. * @return string
  146. */
  147. public function getValueToken()
  148. {
  149. return $this->_value['token'];
  150. }
  151. /**
  152. * Get value value.
  153. *
  154. * @return string
  155. */
  156. public function getValueValue()
  157. {
  158. return $this->_value['value'];
  159. }
  160. /**
  161. * Check if the node represents a token or not.
  162. *
  163. * @return bool
  164. */
  165. public function isToken()
  166. {
  167. return null !== $this->_value;
  168. }
  169. /**
  170. * Prepend a child.
  171. *
  172. * @param \Hoa\Compiler\Llk\TreeNode $child Child.
  173. * @return \Hoa\Compiler\Llk\TreeNode
  174. */
  175. public function prependChild(TreeNode $child)
  176. {
  177. array_unshift($this->_children, $child);
  178. return $this;
  179. }
  180. /**
  181. * Append a child.
  182. *
  183. * @param \Hoa\Compiler\Llk\TreeNode $child Child.
  184. * @return \Hoa\Compiler\Llk\TreeNode
  185. */
  186. public function appendChild(TreeNode $child)
  187. {
  188. $this->_children[] = $child;
  189. return $this;
  190. }
  191. /**
  192. * Set children.
  193. *
  194. * @param array $children Children.
  195. * @return array
  196. */
  197. public function setChildren(array $children)
  198. {
  199. $old = $this->_children;
  200. $this->_children = $children;
  201. return $old;
  202. }
  203. /**
  204. * Get child.
  205. *
  206. * @param int $i Index.
  207. * @return \Hoa\Compiler\Llk\TreeNode
  208. */
  209. public function getChild($i)
  210. {
  211. return $this->_children[$i];
  212. }
  213. /**
  214. * Get children.
  215. *
  216. * @return array
  217. */
  218. public function getChildren()
  219. {
  220. return $this->_children;
  221. }
  222. /**
  223. * Get number of children.
  224. *
  225. * @return int
  226. */
  227. public function getChildrenNumber()
  228. {
  229. return count($this->_children);
  230. }
  231. /**
  232. * Check if a child exists.
  233. *
  234. * @param int $i Index.
  235. * @return bool
  236. */
  237. public function childExists($i)
  238. {
  239. return array_key_exists($i, $this->_children);
  240. }
  241. /**
  242. * Set parent.
  243. *
  244. * @param \Hoa\Compiler\Llk\TreeNode $parent Parent.
  245. * @return \Hoa\Compiler\Llk\TreeNode
  246. */
  247. public function setParent(TreeNode $parent)
  248. {
  249. $old = $this->_parent;
  250. $this->_parent = $parent;
  251. return $old;
  252. }
  253. /**
  254. * Get parent.
  255. *
  256. * @return \Hoa\Compiler\Llk\TreeNode
  257. */
  258. public function getParent()
  259. {
  260. return $this->_parent;
  261. }
  262. /**
  263. * Get data.
  264. *
  265. * @return array
  266. */
  267. public function &getData()
  268. {
  269. return $this->_data;
  270. }
  271. /**
  272. * Accept a visitor.
  273. *
  274. * @param \Hoa\Visitor\Visit $visitor Visitor.
  275. * @param mixed &$handle Handle (reference).
  276. * @param mixed $eldnah Handle (no reference).
  277. * @return mixed
  278. */
  279. public function accept(
  280. Visitor\Visit $visitor,
  281. &$handle = null,
  282. $eldnah = null
  283. ) {
  284. return $visitor->visit($this, $handle, $eldnah);
  285. }
  286. /**
  287. * Remove circular reference to the parent (help the garbage collector).
  288. *
  289. * @return void
  290. */
  291. public function __destruct()
  292. {
  293. unset($this->_parent);
  294. return;
  295. }
  296. }