/protected/components/ezcomponents/Template/src/syntax_trees/tst/interfaces/operator_tst.php

https://github.com/kamarulismail/kamarul-playground · PHP · 344 lines · 117 code · 25 blank · 202 comment · 11 complexity · b9ba56409369b6fd28c7be3a661e28f4 MD5 · raw file

  1. <?php
  2. /**
  3. * File containing the ezcTemplateOperatorTstNode class
  4. *
  5. * @package Template
  6. * @version 1.4.2
  7. * @copyright Copyright (C) 2005-2009 eZ Systems AS. All rights reserved.
  8. * @license http://ez.no/licenses/new_bsd New BSD License
  9. * @access private
  10. */
  11. /**
  12. * Interface for operator elements in parser trees.
  13. *
  14. * This contains the required information to build a localized operator tree
  15. * based on operator precedence.
  16. *
  17. * === =============== ====================
  18. * Lvl Associativity Operators
  19. * === =============== ====================
  20. * 11 right [ .(left)
  21. * 10 non-associative ++ --
  22. * 9 non-associative ! - instanceof
  23. * 8 left * / %
  24. * 7 left + - .
  25. * 6 non-associative < <= > >=
  26. * 5 non-associative == != === !==
  27. * 4 left &&
  28. * 3 left ||
  29. * 2 left ? :
  30. * 1 right = += -= *= /= .= %=
  31. * === =============== ====================
  32. * @package Template
  33. * @version 1.4.2
  34. * @access private
  35. */
  36. abstract class ezcTemplateOperatorTstNode extends ezcTemplateExpressionTstNode
  37. {
  38. /**
  39. * The associativity order is not significant.
  40. */
  41. const NON_ASSOCIATIVE = 1;
  42. /**
  43. * The associativity order is left to right. This means the leftmost
  44. * parameter is evaluated first, then the next and so on.
  45. */
  46. const LEFT_ASSOCIATIVE = 2;
  47. /**
  48. * The associativity order is right to left.This means the rightmost
  49. * parameter is evaluated first, then the previous and so on.
  50. */
  51. const RIGHT_ASSOCIATIVE = 3;
  52. /**
  53. * List of parameters for the current operator, each entry is either another
  54. * operator, type, variable lookup or other parser element.
  55. * Each parameter that is an operator will have the $parentOperator member
  56. * set to this object.
  57. *
  58. * @var array(ezcTemplateTstNode)
  59. * @see prependParameter()
  60. * @see appendParameter()
  61. * @see getLastParameter()
  62. * @see setLastParameter()
  63. * @see mergeParameters()
  64. */
  65. public $parameters;
  66. /**
  67. * The operator element which has this as child (parameter) or null if no parent.
  68. *
  69. * @var ezcTemplateOperatorTstNode
  70. * @see getRoot()
  71. */
  72. public $parentOperator;
  73. /**
  74. * The precedence level for this operator.
  75. *
  76. * This determines which of two operator must be processed first and is
  77. * used to build the resulting operator tree.
  78. * The value starts at 1 (low precedence) to 12 (high).
  79. *
  80. * @var int
  81. * @see ezcTemplateParser::handleOperatorPrecedence()
  82. */
  83. public $precedence;
  84. /**
  85. * The precedence order for operators with same precedence level.
  86. *
  87. * This determines the correct precedence for operators having the exact
  88. * same precedence level.
  89. * @var int
  90. */
  91. public $order;
  92. /**
  93. * The associativity for the operator.
  94. *
  95. * This determines in which order parameters are processed, can be one of:
  96. * - NON_ASSOCIATIVE - The order is not significant.
  97. * - LEFT_ASSOCIATIVE - The order is left to right.
  98. * - RIGHT_ASSOCIATIVE - The order is right to left.
  99. * @var int
  100. */
  101. public $associativity;
  102. /**
  103. * Controls the maximum number of parameters the operator can handle.
  104. * This is either an integer or false which means there is no limit (the default).
  105. *
  106. * @var int|false
  107. */
  108. public $maxParameterCount;
  109. /**
  110. * The symbol representing this operator.
  111. *
  112. * Note: This is a read-only property.
  113. *
  114. * @access public
  115. * @var string
  116. */
  117. private $symbol;
  118. /**
  119. * Initialize element with source and cursor positions.
  120. *
  121. * @param ezcTemplateSourceCode $source
  122. * @param ezcTemplateCursor $start
  123. * @param ezcTemplateCursor $end
  124. * @param int $precedence
  125. * @param int $order
  126. * @param int $associativity
  127. * @param string|bool $symbol
  128. */
  129. public function __construct( ezcTemplateSourceCode $source, /*ezcTemplateCursor*/ $start, /*ezcTemplateCursor*/ $end,
  130. $precedence, $order, $associativity, $symbol = false )
  131. {
  132. parent::__construct( $source, $start, $end );
  133. $this->precedence = $precedence;
  134. $this->order = $order;
  135. $this->associativity = $associativity;
  136. $this->symbol = $symbol;
  137. $this->parameters = array();
  138. $this->parentOperator = null;
  139. $this->maxParameterCount = false;
  140. }
  141. /**
  142. * Returns the tree properties of this node.
  143. *
  144. * @return array(string=>mixed)
  145. */
  146. public function getTreeProperties()
  147. {
  148. return array( 'symbol' => $this->symbol,
  149. 'parameters' => $this->parameters );
  150. }
  151. /**
  152. * Property get
  153. *
  154. * @param string $name
  155. * @return mixed
  156. */
  157. public function __get( $name )
  158. {
  159. switch ( $name )
  160. {
  161. case 'symbol':
  162. return $this->symbol;
  163. default:
  164. return parent::__get( $name );
  165. }
  166. }
  167. /**
  168. * Property set
  169. *
  170. * @param string $name
  171. * @param mixed $value
  172. * @return void
  173. */
  174. public function __set( $name, $value )
  175. {
  176. switch ( $name )
  177. {
  178. case 'symbol':
  179. throw new ezcBasePropertyPermissionException( $name, ezcBasePropertyPermissionException::READ );
  180. default:
  181. return parent::__set( $name, $value );
  182. }
  183. }
  184. /**
  185. * Property isset
  186. *
  187. * @param string $name
  188. * $return bool
  189. */
  190. public function __isset( $name )
  191. {
  192. switch ( $name )
  193. {
  194. case 'symbol':
  195. return true;
  196. default:
  197. return parent::__isset( $name );
  198. }
  199. }
  200. /**
  201. * Prepends the element $element as a parameter to the current operator.
  202. *
  203. * @param ezcTemplateTstNode $element
  204. * @return void
  205. */
  206. public function prependParameter( $element )
  207. {
  208. if ( !is_object( $element ) )
  209. throw new ezcTemplateInternalException( "Non-object <" . gettype( $element ) . "> add as parameter to <" . get_class( $this ) . ">" );
  210. $this->parameters = array_merge( array( $element ),
  211. $this->parameters );
  212. }
  213. /**
  214. * Appends the element $element as a parameter to the current operator.
  215. *
  216. * @param ezcTemplateTstNode $element
  217. * @return void
  218. */
  219. public function appendParameter( $element )
  220. {
  221. if ( !is_object( $element ) )
  222. throw new ezcTemplateInternalException( "Non-object <" . gettype( $element ) . "> add as parameter to <" . get_class( $this ) . ">" );
  223. $this->parameters[] = $element;
  224. }
  225. /**
  226. * Returns the last parameter (if set) object of the current operator.
  227. *
  228. * @return ezcTemplateTstNode
  229. */
  230. public function getLastParameter()
  231. {
  232. if ( count( $this->parameters ) > 0 )
  233. return $this->parameters[count( $this->parameters ) - 1];
  234. return null;
  235. }
  236. /**
  237. * Returns the number of parameters the operator has.
  238. *
  239. * @return int
  240. */
  241. public function getParameterCount()
  242. {
  243. return count( $this->parameters );
  244. }
  245. /**
  246. * Overwrites the last parameter for the current operator to point to $element.
  247. * If there are no parameters it is simply appended to the list.
  248. *
  249. * @param ezcTemplateTstNode $parameter
  250. * @return void
  251. */
  252. public function setLastParameter( ezcTemplateTstNode $parameter )
  253. {
  254. if ( count( $this->parameters ) > 0 )
  255. $this->parameters[count( $this->parameters ) - 1] = $parameter;
  256. else
  257. $this->parameters[] = $parameter;
  258. }
  259. /**
  260. * Removes the last parameter from the parameter list.
  261. *
  262. * @return void
  263. */
  264. public function removeLastParameter()
  265. {
  266. if ( count( $this->parameters ) > 0 )
  267. unset( $this->parameters[count( $this->parameters ) - 1] );
  268. }
  269. /**
  270. * Copies all parameters from operator $operator into the parameter list
  271. * for the current operator.
  272. * @see canMergeParameters()
  273. *
  274. * @param ezcTemplateOperatorTstNode $operator
  275. * @return void
  276. */
  277. public function mergeParameters( ezcTemplateOperatorTstNode $operator )
  278. {
  279. foreach ( $operator->parameters as $parameter )
  280. {
  281. if ( $parameter instanceof ezcTemplateOperatorTstNode )
  282. $parameter->parentOperator = $this;
  283. $this->parameters[] = $parameter;
  284. }
  285. }
  286. /**
  287. * Checks if the current operator can merge parameters from the specificed
  288. * operator and returns true if it can.
  289. * If this is possible the two operators can be merged together into one
  290. * entity using mergeParameters().
  291. *
  292. * The default implementation allows this as long as it is the same class.
  293. *
  294. * @param ezcTemplateOperatorTstNode $operator
  295. * @return bool
  296. */
  297. public function canMergeParametersOf( ezcTemplateOperatorTstNode $operator )
  298. {
  299. return get_class( $operator ) == get_class( $this );
  300. }
  301. /**
  302. * Finds the top-most operator of the current expression tree and returns it.
  303. *
  304. * The top-most operator is found by checking the parent operator until there
  305. * are no more parent operators.
  306. *
  307. * Note: If the current operator is the top-most operator $this is returned.
  308. *
  309. * @return ezcTemplateOperatorTstNode
  310. */
  311. public function getRoot()
  312. {
  313. $operator = $this;
  314. while ( $operator->parentOperator !== null )
  315. $operator= $operator->parentOperator;
  316. return $operator;
  317. }
  318. }
  319. ?>