Parser.php 8.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321
  1. <?php
  2. /*
  3. * This file is part of Twig.
  4. *
  5. * (c) 2009 Fabien Potencier
  6. * (c) 2009 Armin Ronacher
  7. *
  8. * For the full copyright and license information, please view the LICENSE
  9. * file that was distributed with this source code.
  10. */
  11. /**
  12. * Default parser implementation.
  13. *
  14. * @package twig
  15. * @author Fabien Potencier <fabien@symfony.com>
  16. */
  17. class Twig_Parser implements Twig_ParserInterface
  18. {
  19. protected $stream;
  20. protected $parent;
  21. protected $handlers;
  22. protected $visitors;
  23. protected $expressionParser;
  24. protected $blocks;
  25. protected $blockStack;
  26. protected $macros;
  27. protected $env;
  28. protected $reservedMacroNames;
  29. protected $importedFunctions;
  30. protected $tmpVarCount;
  31. protected $traits;
  32. /**
  33. * Constructor.
  34. *
  35. * @param Twig_Environment $env A Twig_Environment instance
  36. */
  37. public function __construct(Twig_Environment $env)
  38. {
  39. $this->env = $env;
  40. }
  41. public function getVarName()
  42. {
  43. return sprintf('__internal_%s_%d', substr($this->env->getTemplateClass($this->stream->getFilename()), strlen($this->env->getTemplateClassPrefix())), ++$this->tmpVarCount);
  44. }
  45. /**
  46. * Converts a token stream to a node tree.
  47. *
  48. * @param Twig_TokenStream $stream A token stream instance
  49. *
  50. * @return Twig_Node_Module A node tree
  51. */
  52. public function parse(Twig_TokenStream $stream)
  53. {
  54. $this->tmpVarCount = 0;
  55. // tag handlers
  56. $this->handlers = $this->env->getTokenParsers();
  57. $this->handlers->setParser($this);
  58. // node visitors
  59. $this->visitors = $this->env->getNodeVisitors();
  60. if (null === $this->expressionParser) {
  61. $this->expressionParser = new Twig_ExpressionParser($this, $this->env->getUnaryOperators(), $this->env->getBinaryOperators());
  62. }
  63. $this->stream = $stream;
  64. $this->parent = null;
  65. $this->blocks = array();
  66. $this->macros = array();
  67. $this->traits = array();
  68. $this->blockStack = array();
  69. $this->importedFunctions = array(array());
  70. try {
  71. $body = $this->subparse(null);
  72. if (null !== $this->parent) {
  73. if (null === $body = $this->filterBodyNodes($body)) {
  74. $body = new Twig_Node();
  75. }
  76. }
  77. } catch (Twig_Error_Syntax $e) {
  78. if (null === $e->getTemplateFile()) {
  79. $e->setTemplateFile($this->stream->getFilename());
  80. }
  81. throw $e;
  82. }
  83. $node = new Twig_Node_Module($body, $this->parent, new Twig_Node($this->blocks), new Twig_Node($this->macros), new Twig_Node($this->traits), $this->stream->getFilename());
  84. $traverser = new Twig_NodeTraverser($this->env, $this->visitors);
  85. return $traverser->traverse($node);
  86. }
  87. public function subparse($test, $dropNeedle = false)
  88. {
  89. $lineno = $this->getCurrentToken()->getLine();
  90. $rv = array();
  91. while (!$this->stream->isEOF()) {
  92. switch ($this->getCurrentToken()->getType()) {
  93. case Twig_Token::TEXT_TYPE:
  94. $token = $this->stream->next();
  95. $rv[] = new Twig_Node_Text($token->getValue(), $token->getLine());
  96. break;
  97. case Twig_Token::VAR_START_TYPE:
  98. $token = $this->stream->next();
  99. $expr = $this->expressionParser->parseExpression();
  100. $this->stream->expect(Twig_Token::VAR_END_TYPE);
  101. $rv[] = new Twig_Node_Print($expr, $token->getLine());
  102. break;
  103. case Twig_Token::BLOCK_START_TYPE:
  104. $this->stream->next();
  105. $token = $this->getCurrentToken();
  106. if ($token->getType() !== Twig_Token::NAME_TYPE) {
  107. throw new Twig_Error_Syntax('A block must start with a tag name', $token->getLine(), $this->stream->getFilename());
  108. }
  109. if (null !== $test && call_user_func($test, $token)) {
  110. if ($dropNeedle) {
  111. $this->stream->next();
  112. }
  113. if (1 === count($rv)) {
  114. return $rv[0];
  115. }
  116. return new Twig_Node($rv, array(), $lineno);
  117. }
  118. $subparser = $this->handlers->getTokenParser($token->getValue());
  119. if (null === $subparser) {
  120. throw new Twig_Error_Syntax(sprintf('Unknown tag name "%s"', $token->getValue()), $token->getLine(), $this->stream->getFilename());
  121. }
  122. $this->stream->next();
  123. $node = $subparser->parse($token);
  124. if (null !== $node) {
  125. $rv[] = $node;
  126. }
  127. break;
  128. default:
  129. throw new Twig_Error_Syntax('Lexer or parser ended up in unsupported state.', -1, $this->stream->getFilename());
  130. }
  131. }
  132. if (1 === count($rv)) {
  133. return $rv[0];
  134. }
  135. return new Twig_Node($rv, array(), $lineno);
  136. }
  137. public function addHandler($name, $class)
  138. {
  139. $this->handlers[$name] = $class;
  140. }
  141. public function addNodeVisitor(Twig_NodeVisitorInterface $visitor)
  142. {
  143. $this->visitors[] = $visitor;
  144. }
  145. public function getBlockStack()
  146. {
  147. return $this->blockStack;
  148. }
  149. public function peekBlockStack()
  150. {
  151. return $this->blockStack[count($this->blockStack) - 1];
  152. }
  153. public function popBlockStack()
  154. {
  155. array_pop($this->blockStack);
  156. }
  157. public function pushBlockStack($name)
  158. {
  159. $this->blockStack[] = $name;
  160. }
  161. public function hasBlock($name)
  162. {
  163. return isset($this->blocks[$name]);
  164. }
  165. public function setBlock($name, $value)
  166. {
  167. $this->blocks[$name] = $value;
  168. }
  169. public function hasMacro($name)
  170. {
  171. return isset($this->macros[$name]);
  172. }
  173. public function setMacro($name, Twig_Node_Macro $node)
  174. {
  175. if (null === $this->reservedMacroNames) {
  176. $this->reservedMacroNames = array();
  177. $r = new ReflectionClass($this->env->getBaseTemplateClass());
  178. foreach ($r->getMethods() as $method) {
  179. $this->reservedMacroNames[] = $method->getName();
  180. }
  181. }
  182. if (in_array($name, $this->reservedMacroNames)) {
  183. throw new Twig_Error_Syntax(sprintf('"%s" cannot be used as a macro name as it is a reserved keyword', $name), $node->getLine());
  184. }
  185. $this->macros[$name] = $node;
  186. }
  187. public function addTrait($trait)
  188. {
  189. $this->traits[] = $trait;
  190. }
  191. public function addImportedFunction($alias, $name, Twig_Node_Expression $node)
  192. {
  193. $this->importedFunctions[0][$alias] = array('name' => $name, 'node' => $node);
  194. }
  195. public function getImportedFunction($alias)
  196. {
  197. foreach ($this->importedFunctions as $functions) {
  198. if (isset($functions[$alias])) {
  199. return $functions[$alias];
  200. }
  201. }
  202. }
  203. public function pushLocalScope()
  204. {
  205. array_unshift($this->importedFunctions, array());
  206. }
  207. public function popLocalScope()
  208. {
  209. array_shift($this->importedFunctions);
  210. }
  211. /**
  212. * Gets the expression parser.
  213. *
  214. * @return Twig_ExpressionParser The expression parser
  215. */
  216. public function getExpressionParser()
  217. {
  218. return $this->expressionParser;
  219. }
  220. public function getParent()
  221. {
  222. return $this->parent;
  223. }
  224. public function setParent($parent)
  225. {
  226. $this->parent = $parent;
  227. }
  228. /**
  229. * Gets the token stream.
  230. *
  231. * @return Twig_TokenStream The token stream
  232. */
  233. public function getStream()
  234. {
  235. return $this->stream;
  236. }
  237. /**
  238. * Gets the current token.
  239. *
  240. * @return Twig_Token The current token
  241. */
  242. public function getCurrentToken()
  243. {
  244. return $this->stream->getCurrent();
  245. }
  246. protected function filterBodyNodes(Twig_NodeInterface $node)
  247. {
  248. // check that the body does not contain non-empty output nodes
  249. if (
  250. ($node instanceof Twig_Node_Text && !ctype_space($node->getAttribute('data')))
  251. ||
  252. (!$node instanceof Twig_Node_Text && !$node instanceof Twig_Node_BlockReference && $node instanceof Twig_NodeOutputInterface)
  253. ) {
  254. throw new Twig_Error_Syntax(sprintf('A template that extends another one cannot have a body (%s).', $node), $node->getLine(), $this->stream->getFilename());
  255. }
  256. if ($node instanceof Twig_NodeOutputInterface) {
  257. return null;
  258. }
  259. foreach ($node as $k => $n) {
  260. if (null !== $n && null === $n = $this->filterBodyNodes($n)) {
  261. $node->removeNode($k);
  262. }
  263. }
  264. return $node;
  265. }
  266. }