PageRenderTime 49ms CodeModel.GetById 25ms RepoModel.GetById 0ms app.codeStats 0ms

/Template/src/parsers/ast_to_ast/implementations/ast_walker.php

https://github.com/Yannix/zetacomponents
PHP | 749 lines | 314 code | 91 blank | 344 comment | 9 complexity | 21173dc57cab7fa44e7f9c94a46591f0 MD5 | raw file
  1. <?php
  2. /**
  3. * File containing the ezcTemplateAstWalker
  4. *
  5. * Licensed to the Apache Software Foundation (ASF) under one
  6. * or more contributor license agreements. See the NOTICE file
  7. * distributed with this work for additional information
  8. * regarding copyright ownership. The ASF licenses this file
  9. * to you under the Apache License, Version 2.0 (the
  10. * "License"); you may not use this file except in compliance
  11. * with the License. You may obtain a copy of the License at
  12. *
  13. * http://www.apache.org/licenses/LICENSE-2.0
  14. *
  15. * Unless required by applicable law or agreed to in writing,
  16. * software distributed under the License is distributed on an
  17. * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  18. * KIND, either express or implied. See the License for the
  19. * specific language governing permissions and limitations
  20. * under the License.
  21. *
  22. * @package Template
  23. * @version //autogen//
  24. * @license http://www.apache.org/licenses/LICENSE-2.0 Apache License, Version 2.0
  25. * @access private
  26. */
  27. /**
  28. * The entire AST tree, doing nothing.
  29. *
  30. * @package Template
  31. * @version //autogen//
  32. * @access private
  33. */
  34. class ezcTemplateAstWalker implements ezcTemplateAstNodeVisitor
  35. {
  36. /**
  37. * A stack that keeps the path of nodes to the current position. The first element (0) contains the last
  38. * executed node. The second element, the second last node, etc.
  39. *
  40. * @var array(ezcTemplateAstNode)
  41. */
  42. public $nodePath = array();
  43. /**
  44. * The amount of statements in the last, second last, etc. position / level.
  45. *
  46. * @var array(int)
  47. */
  48. public $statements = array();
  49. /**
  50. * The offset of the statements. Default it's 0. When a statement is inserted, the offset should also increase.
  51. *
  52. * @var array(int)
  53. */
  54. public $offset = array();
  55. /**
  56. * Constructs a new ezcTemplateAstWalker
  57. */
  58. public function __construct( )
  59. {
  60. }
  61. /**
  62. * Destructor
  63. */
  64. public function __destruct()
  65. {
  66. }
  67. /**
  68. * visitLiteralAstNode
  69. *
  70. * @param ezcTemplateLiteralAstNode $type
  71. * @return void
  72. */
  73. public function visitLiteralAstNode( ezcTemplateLiteralAstNode $type )
  74. {
  75. }
  76. /**
  77. * visitLiteralArrayAstNode
  78. *
  79. * @param ezcTemplateLiteralArrayAstNode $type
  80. * @return void
  81. */
  82. public function visitLiteralArrayAstNode( ezcTemplateLiteralArrayAstNode $type )
  83. {
  84. }
  85. /**
  86. * visitOutputAstNode
  87. *
  88. * @param ezcTemplateOutputAstNode $type
  89. * @return void
  90. */
  91. public function visitOutputAstNode( ezcTemplateOutputAstNode $type )
  92. {
  93. array_unshift( $this->nodePath, $type );
  94. $this->acceptAndUpdate( $type->expression );
  95. array_shift( $this->nodePath );
  96. }
  97. /**
  98. * visitTypeCastAstNode
  99. *
  100. * @param ezcTemplateTypeCastAstNode $node
  101. * @return void
  102. */
  103. public function visitTypeCastAstNode( ezcTemplateTypeCastAstNode $node )
  104. {
  105. array_unshift( $this->nodePath, $node );
  106. $this->acceptAndUpdate( $node->value );
  107. array_shift( $this->nodePath );
  108. }
  109. /**
  110. * visitConstantAstNode
  111. *
  112. * @param ezcTemplateConstantAstNode $type
  113. * @return void
  114. */
  115. public function visitConstantAstNode( ezcTemplateConstantAstNode $type )
  116. {
  117. }
  118. /**
  119. * visitEolCommentAstNode
  120. *
  121. * @param ezcTemplateEolCommentAstNode $comment
  122. * @return void
  123. */
  124. public function visitEolCommentAstNode( ezcTemplateEolCommentAstNode $comment )
  125. {
  126. }
  127. /**
  128. * visitBlockCommentAstNode
  129. *
  130. * @param ezcTemplateBlockCommentAstNode $comment
  131. * @return void
  132. */
  133. public function visitBlockCommentAstNode( ezcTemplateBlockCommentAstNode $comment )
  134. {
  135. }
  136. /**
  137. * visitVariableAstNode
  138. *
  139. * @param ezcTemplateVariableAstNode $var
  140. * @return void
  141. */
  142. public function visitVariableAstNode( ezcTemplateVariableAstNode $var )
  143. {
  144. }
  145. /**
  146. * visitDynamicVariableAstNode
  147. *
  148. * @param ezcTemplateDynamicVariableAstNode $var
  149. * @return void
  150. */
  151. public function visitDynamicVariableAstNode( ezcTemplateDynamicVariableAstNode $var )
  152. {
  153. array_unshift( $this->nodePath, $var );
  154. $this->acceptAndUpdate( $var->nameExpression );
  155. array_shift( $this->nodePath );
  156. }
  157. /**
  158. * visitDynamicStringAstNode
  159. *
  160. * @param ezcTemplateDynamicStringAstNode $dynamic
  161. * @return void
  162. */
  163. public function visitDynamicStringAstNode( ezcTemplateDynamicStringAstNode $dynamic )
  164. {
  165. throw new ezcTemplateInternalException( "TODO: dynamicstring Ast node , tree walker" );
  166. array_unshift( $this->nodePath, $dynamic );
  167. foreach ( $parameters as $parameter )
  168. {
  169. if ( !$parameter instanceof ezcTemplateLiteralAstNode )
  170. {
  171. $this->acceptAndUpdate( $parameter );
  172. }
  173. }
  174. array_shift( $this->nodePath );
  175. }
  176. /**
  177. * visitArrayFetchOperatorAstNode
  178. *
  179. * @param ezcTemplateArrayFetchOperatorAstNode $operator
  180. * @return void
  181. */
  182. public function visitArrayFetchOperatorAstNode( ezcTemplateArrayFetchOperatorAstNode $operator )
  183. {
  184. array_unshift( $this->nodePath, $operator );
  185. $parameters = $operator->getParameters();
  186. $count = count( $parameters );
  187. for ( $i = 0; $i < $count; ++$i )
  188. {
  189. $this->acceptAndUpdate( $operator->parameters[$i] );
  190. }
  191. array_shift( $this->nodePath );
  192. }
  193. /**
  194. * visitUnaryOperatorAstNode
  195. *
  196. * @param ezcTemplateOperatorAstNode $operator
  197. * @return void
  198. */
  199. public function visitUnaryOperatorAstNode( ezcTemplateOperatorAstNode $operator )
  200. {
  201. $parameters = $operator->getParameters();
  202. if ( count( $parameters ) < $operator->minParameterCount )
  203. {
  204. throw new ezcTemplateInternalException( "The operator <" . get_class( $operator ) . " contains only " . count( $parameters ) . " parameters but should at least have {$operator->minParameterCount} parameters." );
  205. }
  206. array_unshift( $this->nodePath, $operator );
  207. $this->acceptAndUpdate( $operator->parameters[0] );
  208. array_shift( $this->nodePath );
  209. }
  210. /**
  211. * visitBinaryOperatorAstNode
  212. *
  213. * @param ezcTemplateOperatorAstNode $operator
  214. * @return void
  215. */
  216. public function visitBinaryOperatorAstNode( ezcTemplateOperatorAstNode $operator )
  217. {
  218. $parameters = $operator->getParameters();
  219. if ( count( $parameters ) < $operator->minParameterCount )
  220. {
  221. throw new ezcTemplateInternalException( "The operator <" . get_class( $operator ) . " contains only " . count( $parameters ) . " parameters but should at least have {$operator->minParameterCount} parameters." );
  222. }
  223. array_unshift( $this->nodePath, $operator );
  224. $this->acceptAndUpdate( $operator->parameters[0] );
  225. $this->acceptAndUpdate( $operator->parameters[1] );
  226. array_shift( $this->nodePath );
  227. }
  228. /**
  229. * visitFunctionCallAstNode
  230. *
  231. * @param ezcTemplateFunctionCallAstNode $fcall
  232. * @return void
  233. */
  234. public function visitFunctionCallAstNode( ezcTemplateFunctionCallAstNode $fcall )
  235. {
  236. array_unshift( $this->nodePath, $fcall );
  237. foreach ( $fcall->getParameters() as $i => $parameter )
  238. {
  239. $this->acceptAndUpdate( $fcall->parameters[$i] );
  240. }
  241. array_shift( $this->nodePath );
  242. }
  243. /**
  244. * visitBodyAstNode
  245. *
  246. * @param ezcTemplateBodyAstNode $body
  247. * @return void
  248. */
  249. public function visitBodyAstNode( ezcTemplateBodyAstNode $body )
  250. {
  251. array_unshift( $this->nodePath, $body );
  252. array_unshift( $this->statements, 0);
  253. array_unshift( $this->offset, 0);
  254. $b = clone( $body );
  255. for( $i = 0; $i < sizeof( $b->statements ); $i++)
  256. {
  257. $this->statements[0] = $i;
  258. $this->acceptAndUpdate( $b->statements[$i] );
  259. }
  260. $body = $b;
  261. array_shift( $this->offset );
  262. array_shift( $this->statements );
  263. array_shift( $this->nodePath );
  264. }
  265. /**
  266. * visitRootAstNode
  267. *
  268. * @param ezcTemplateRootAstNode $body
  269. * @return void
  270. */
  271. public function visitRootAstNode( ezcTemplateRootAstNode &$body )
  272. {
  273. array_unshift( $this->nodePath, $body );
  274. array_unshift( $this->statements, 0);
  275. array_unshift( $this->offset, 0);
  276. $b = clone( $body );
  277. for( $i = 0; $i < sizeof( $b->statements ); $i++)
  278. {
  279. $this->statements[0] = $i;
  280. $this->acceptAndUpdate( $b->statements[$i] );
  281. }
  282. // XXX Test this, this may be wrong.
  283. // $body = $b;
  284. array_shift( $this->offset );
  285. array_shift( $this->statements );
  286. array_shift( $this->nodePath );
  287. }
  288. /**
  289. * visitGenericStatementAstNode
  290. *
  291. * @param ezcTemplateGenericStatementAstNode $statement
  292. * @return void
  293. */
  294. public function visitGenericStatementAstNode( ezcTemplateGenericStatementAstNode $statement )
  295. {
  296. array_unshift( $this->nodePath, $statement );
  297. $this->acceptAndUpdate( $statement->expression );
  298. array_shift( $this->nodePath );
  299. }
  300. /**
  301. * visitIfAstNode
  302. *
  303. * @param ezcTemplateIfAstNode $if
  304. * @return void
  305. */
  306. public function visitIfAstNode( ezcTemplateIfAstNode $if )
  307. {
  308. array_unshift( $this->nodePath, $if );
  309. foreach ( $if->conditions as $i => $conditionBody )
  310. {
  311. $condition = $conditionBody->condition;
  312. if ( $condition !== null )
  313. {
  314. $this->acceptAndUpdate( $if->conditions[$i]->condition );
  315. }
  316. $this->acceptAndUpdate( $conditionBody->body );
  317. }
  318. array_shift( $this->nodePath );
  319. }
  320. /**
  321. * visitDynamicBlockAstNode
  322. *
  323. * @param ezcTemplateDynamicBlockAstNode $statement
  324. * @return void
  325. */
  326. public function visitDynamicBlockAstNode( ezcTemplateDynamicBlockAstNode $statement )
  327. {
  328. array_unshift( $this->nodePath, $statement );
  329. $this->acceptAndUpdate( $statement->body );
  330. array_shift( $this->nodePath );
  331. }
  332. /**
  333. * Visits a code element containing while control structures.
  334. *
  335. * @param ezcTemplateWhileAstNode $while The code element containing the while control structure.
  336. * @return void
  337. */
  338. public function visitWhileAstNode( ezcTemplateWhileAstNode $while )
  339. {
  340. array_unshift( $this->nodePath, $while );
  341. $conditionBody = $while->conditionBody;
  342. $this->acceptAndUpdate( $conditionBody->condition );
  343. $this->acceptAndUpdate( $conditionBody->body );
  344. array_shift( $this->nodePath );
  345. }
  346. /**
  347. * visitForeachAstNode
  348. *
  349. * @param ezcTemplateForeachAstNode $foreach
  350. * @return void
  351. */
  352. public function visitForeachAstNode( ezcTemplateForeachAstNode $foreach )
  353. {
  354. array_unshift( $this->nodePath, $foreach );
  355. $this->acceptAndUpdate( $foreach->arrayExpression );
  356. if ( $foreach->keyVariable !== null )
  357. {
  358. $this->acceptAndUpdate( $foreach->keyVariable );
  359. }
  360. $this->acceptAndUpdate( $foreach->valueVariable );
  361. $this->acceptAndUpdate( $foreach->body );
  362. array_shift( $this->nodePath );
  363. }
  364. /**
  365. * visitBreakAstNode
  366. *
  367. * @param ezcTemplateBreakAstNode $break
  368. * @return void
  369. */
  370. public function visitBreakAstNode( ezcTemplateBreakAstNode $break )
  371. {
  372. }
  373. /**
  374. * visitContinueAstNode
  375. *
  376. * @param ezcTemplateContinueAstNode $continue
  377. * @return void
  378. */
  379. public function visitContinueAstNode( ezcTemplateContinueAstNode $continue )
  380. {
  381. }
  382. /**
  383. * visitReturnAstNode
  384. *
  385. * @param ezcTemplateReturnAstNode $return
  386. * @return void
  387. */
  388. public function visitReturnAstNode( ezcTemplateReturnAstNode $return )
  389. {
  390. }
  391. /**
  392. * visitRequireAstNode
  393. *
  394. * @param ezcTemplateRequireAstNode $require
  395. * @return void
  396. */
  397. public function visitRequireAstNode( ezcTemplateRequireAstNode $require )
  398. {
  399. }
  400. /**
  401. * visitRequireOnceAstNode
  402. *
  403. * @param ezcTemplateRequireOnceAstNode $require
  404. * @return void
  405. */
  406. public function visitRequireOnceAstNode( ezcTemplateRequireOnceAstNode $require )
  407. {
  408. }
  409. /**
  410. * visitIncludeAstNode
  411. *
  412. * @param ezcTemplateIncludeAstNode $include
  413. * @return void
  414. */
  415. public function visitIncludeAstNode( ezcTemplateIncludeAstNode $include )
  416. {
  417. }
  418. /**
  419. * visitIncludeOnceAstNode
  420. *
  421. * @param ezcTemplateIncludeOnceAstNode $include
  422. * @return void
  423. */
  424. public function visitIncludeOnceAstNode( ezcTemplateIncludeOnceAstNode $include )
  425. {
  426. }
  427. /**
  428. * visitSwitchAstNode
  429. *
  430. * @param ezcTemplateSwitchAstNode $switch
  431. * @return void
  432. */
  433. public function visitSwitchAstNode( ezcTemplateSwitchAstNode $switch )
  434. {
  435. array_unshift( $this->nodePath, $switch );
  436. $this->acceptAndUpdate( $switch->expression );
  437. foreach ( $switch->cases as $key => $case )
  438. {
  439. $this->acceptAndUpdate( $case );
  440. }
  441. array_shift( $this->nodePath );
  442. }
  443. /**
  444. * visitCaseAstNode
  445. *
  446. * @param ezcTemplateCaseAstNode $case
  447. * @return void
  448. */
  449. public function visitCaseAstNode( ezcTemplateCaseAstNode $case )
  450. {
  451. array_unshift( $this->nodePath, $case );
  452. $this->acceptAndUpdate( $case->match );
  453. $this->acceptAndUpdate( $case->body );
  454. array_shift( $this->nodePath );
  455. }
  456. /**
  457. * visitDefaultAstNode
  458. *
  459. * @param ezcTemplateDefaultAstNode $default
  460. * @return void
  461. */
  462. public function visitDefaultAstNode( ezcTemplateDefaultAstNode $default )
  463. {
  464. array_unshift( $this->nodePath, $default );
  465. $this->acceptAndUpdate( $default->body );
  466. array_shift( $this->nodePath );
  467. }
  468. /**
  469. * visitConditionBodyAstNode
  470. *
  471. * @param ezcTemplateConditionBodyAstNode $cond
  472. * @return void
  473. */
  474. public function visitConditionBodyAstNode( ezcTemplateConditionBodyAstNode $cond )
  475. {
  476. }
  477. /**
  478. * visitTryAstNode
  479. *
  480. * @param ezcTemplateTryAstNode $try
  481. * @return void
  482. */
  483. public function visitTryAstNode( ezcTemplateTryAstNode $try )
  484. {
  485. array_unshift( $this->nodePath, $try );
  486. $try->body->accept( $this );
  487. $this->acceptAndUpdate( $try->body );
  488. foreach ( $try->catches as $key => $catch )
  489. {
  490. $this->acceptAndUpdate( $try->catches[$key] );
  491. }
  492. array_shift( $this->nodePath );
  493. }
  494. /**
  495. * visitCatchAstNode
  496. *
  497. * @param ezcTemplateCatchAstNode $catch
  498. * @return void
  499. */
  500. public function visitCatchAstNode( ezcTemplateCatchAstNode $catch )
  501. {
  502. array_unshift( $this->nodePath, $catch );
  503. $this->acceptAndUpdate( $catch->variableExpression );
  504. $this->acceptAndUpdate( $catch->body );
  505. array_shift( $this->nodePath );
  506. }
  507. /**
  508. * visitEchoAstNode
  509. *
  510. * @param ezcTemplateEchoAstNode $echo
  511. * @return void
  512. */
  513. public function visitEchoAstNode( ezcTemplateEchoAstNode $echo )
  514. {
  515. array_unshift( $this->nodePath, $echo );
  516. $outputList = $echo->getOutputList();
  517. foreach ( $outputList as $i => $output )
  518. {
  519. $this->acceptAndUpdate( $echo->outputList[$i] );
  520. }
  521. array_shift( $this->nodePath );
  522. }
  523. /**
  524. * visitPrintAstNode
  525. *
  526. * @param ezcTemplatePrintAstNode $print
  527. * @return void
  528. */
  529. public function visitPrintAstNode( ezcTemplatePrintAstNode $print )
  530. {
  531. array_unshift( $this->nodePath, $print );
  532. $this->acceptAndUpdate( $print->expression );
  533. array_shift( $this->nodePath );
  534. }
  535. /**
  536. * visitIssetAstNode
  537. *
  538. * @param ezcTemplateIssetAstNode $isset
  539. * @return void
  540. */
  541. public function visitIssetAstNode( ezcTemplateIssetAstNode $isset )
  542. {
  543. }
  544. /**
  545. * visitUnsetAstNode
  546. *
  547. * @param ezcTemplateUnsetAstNode $unset
  548. * @return void
  549. */
  550. public function visitUnsetAstNode( ezcTemplateUnsetAstNode $unset )
  551. {
  552. }
  553. /**
  554. * visitEmptyAstNode
  555. *
  556. * @param ezcTemplateEmptyAstNode $empty
  557. * @return void
  558. */
  559. public function visitEmptyAstNode( ezcTemplateEmptyAstNode $empty )
  560. {
  561. array_unshift( $this->nodePath, $empty );
  562. $this->acceptAndUpdate( $empty->expression );
  563. array_shift( $this->nodePath );
  564. }
  565. /**
  566. * visitParenthesisAstNode
  567. *
  568. * @param ezcTemplateParenthesisAstNode $parenthesis
  569. * @return void
  570. */
  571. public function visitParenthesisAstNode( ezcTemplateParenthesisAstNode $parenthesis )
  572. {
  573. array_unshift( $this->nodePath, $parenthesis );
  574. $this->acceptAndUpdate( $parenthesis->expression );
  575. array_shift( $this->nodePath );
  576. }
  577. /**
  578. * visitCurlyBracesAstNode
  579. *
  580. * @param ezcTemplateCurlyBracesAstNode $curly
  581. * @return void
  582. */
  583. public function visitCurlyBracesAstNode( ezcTemplateCurlyBracesAstNode $curly )
  584. {
  585. array_unshift( $this->nodePath, $curly );
  586. $this->acceptAndUpdate( $parenthesis->expression );
  587. array_shift( $this->nodePath );
  588. }
  589. /**
  590. * visitIdentifierAstNode
  591. *
  592. * @param ezcTemplateIdentifierAstNode $node
  593. * @return void
  594. */
  595. public function visitIdentifierAstNode( ezcTemplateIdentifierAstNode $node )
  596. {
  597. }
  598. /**
  599. * visitNewAstNode
  600. *
  601. * @param ezcTemplateNewAstNode $node
  602. * @return void
  603. */
  604. public function visitNewAstNode( ezcTemplateNewAstNode $node )
  605. {
  606. }
  607. /**
  608. * visitCloneAstNode
  609. *
  610. * @param ezcTemplateCloneAstNode $node
  611. * @return void
  612. */
  613. public function visitCloneAstNode( ezcTemplateCloneAstNode $node )
  614. {
  615. }
  616. /**
  617. * visitPhpCodeAstNode
  618. *
  619. * @param ezcTemplatePhpCodeAstNode $node
  620. * @return void
  621. */
  622. public function visitPhpCodeAstNode( ezcTemplatePhpCodeAstNode $node )
  623. {
  624. }
  625. /**
  626. * visitThrowExceptionAstNode
  627. *
  628. * @param ezcTemplateThrowExceptionAstNode $node
  629. * @return void
  630. */
  631. public function visitThrowExceptionAstNode( ezcTemplateThrowExceptionAstNode $node )
  632. {
  633. array_unshift( $this->nodePath, $node );
  634. $this->acceptAndUpdate( $node->message );
  635. array_shift( $this->nodePath );
  636. }
  637. /**
  638. * visitNopAstNode
  639. *
  640. * @param ezcTemplateNopAstNode $node
  641. * @return void
  642. */
  643. public function visitNopAstNode( ezcTemplateNopAstNode $node )
  644. {
  645. }
  646. /**
  647. * Internal function called to call the accept function and change the given node.
  648. *
  649. * @param ezcTemplateAstNode $node Notice that the parameter will be changed.
  650. * @return void
  651. */
  652. protected function acceptAndUpdate( ezcTemplateAstNode &$node )
  653. {
  654. $ret = $node->accept( $this );
  655. if ( $ret !== null ) $node = $ret;
  656. }
  657. }
  658. ?>