PageRenderTime 93ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/src/application/libraries/Twig/ExpressionParser.php

https://bitbucket.org/masnug/grc276-blog-laravel
PHP | 382 lines | 298 code | 62 blank | 22 comment | 61 complexity | 85a7797b1bb27c73e6c8d7edb8a7acd5 MD5 | raw file
  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. * Parses expressions.
  13. *
  14. * This parser implements a "Precedence climbing" algorithm.
  15. *
  16. * @see http://www.engr.mun.ca/~theo/Misc/exp_parsing.htm
  17. * @see http://en.wikipedia.org/wiki/Operator-precedence_parser
  18. *
  19. * @package twig
  20. * @author Fabien Potencier <fabien@symfony.com>
  21. */
  22. class Twig_ExpressionParser
  23. {
  24. const OPERATOR_LEFT = 1;
  25. const OPERATOR_RIGHT = 2;
  26. protected $parser;
  27. protected $unaryOperators;
  28. protected $binaryOperators;
  29. public function __construct(Twig_Parser $parser, array $unaryOperators, array $binaryOperators)
  30. {
  31. $this->parser = $parser;
  32. $this->unaryOperators = $unaryOperators;
  33. $this->binaryOperators = $binaryOperators;
  34. }
  35. public function parseExpression($precedence = 0)
  36. {
  37. $expr = $this->getPrimary();
  38. $token = $this->parser->getCurrentToken();
  39. while ($this->isBinary($token) && $this->binaryOperators[$token->getValue()]['precedence'] >= $precedence) {
  40. $op = $this->binaryOperators[$token->getValue()];
  41. $this->parser->getStream()->next();
  42. if (isset($op['callable'])) {
  43. $expr = call_user_func($op['callable'], $this->parser, $expr);
  44. } else {
  45. $expr1 = $this->parseExpression(self::OPERATOR_LEFT === $op['associativity'] ? $op['precedence'] + 1 : $op['precedence']);
  46. $class = $op['class'];
  47. $expr = new $class($expr, $expr1, $token->getLine());
  48. }
  49. $token = $this->parser->getCurrentToken();
  50. }
  51. if (0 === $precedence) {
  52. return $this->parseConditionalExpression($expr);
  53. }
  54. return $expr;
  55. }
  56. protected function getPrimary()
  57. {
  58. $token = $this->parser->getCurrentToken();
  59. if ($this->isUnary($token)) {
  60. $operator = $this->unaryOperators[$token->getValue()];
  61. $this->parser->getStream()->next();
  62. $expr = $this->parseExpression($operator['precedence']);
  63. $class = $operator['class'];
  64. return $this->parsePostfixExpression(new $class($expr, $token->getLine()));
  65. } elseif ($token->test(Twig_Token::PUNCTUATION_TYPE, '(')) {
  66. $this->parser->getStream()->next();
  67. $expr = $this->parseExpression();
  68. $this->parser->getStream()->expect(Twig_Token::PUNCTUATION_TYPE, ')', 'An opened parenthesis is not properly closed');
  69. return $this->parsePostfixExpression($expr);
  70. }
  71. return $this->parsePrimaryExpression();
  72. }
  73. protected function parseConditionalExpression($expr)
  74. {
  75. while ($this->parser->getStream()->test(Twig_Token::PUNCTUATION_TYPE, '?')) {
  76. $this->parser->getStream()->next();
  77. $expr2 = $this->parseExpression();
  78. $this->parser->getStream()->expect(Twig_Token::PUNCTUATION_TYPE, ':', 'The ternary operator must have a default value');
  79. $expr3 = $this->parseExpression();
  80. $expr = new Twig_Node_Expression_Conditional($expr, $expr2, $expr3, $this->parser->getCurrentToken()->getLine());
  81. }
  82. return $expr;
  83. }
  84. protected function isUnary(Twig_Token $token)
  85. {
  86. return $token->test(Twig_Token::OPERATOR_TYPE) && isset($this->unaryOperators[$token->getValue()]);
  87. }
  88. protected function isBinary(Twig_Token $token)
  89. {
  90. return $token->test(Twig_Token::OPERATOR_TYPE) && isset($this->binaryOperators[$token->getValue()]);
  91. }
  92. public function parsePrimaryExpression()
  93. {
  94. $token = $this->parser->getCurrentToken();
  95. switch ($token->getType()) {
  96. case Twig_Token::NAME_TYPE:
  97. $this->parser->getStream()->next();
  98. switch ($token->getValue()) {
  99. case 'true':
  100. case 'TRUE':
  101. $node = new Twig_Node_Expression_Constant(true, $token->getLine());
  102. break;
  103. case 'false':
  104. case 'FALSE':
  105. $node = new Twig_Node_Expression_Constant(false, $token->getLine());
  106. break;
  107. case 'none':
  108. case 'NONE':
  109. case 'null':
  110. case 'NULL':
  111. $node = new Twig_Node_Expression_Constant(null, $token->getLine());
  112. break;
  113. default:
  114. if ('(' === $this->parser->getCurrentToken()->getValue()) {
  115. $node = $this->getFunctionNode($token->getValue(), $token->getLine());
  116. } else {
  117. $node = new Twig_Node_Expression_Name($token->getValue(), $token->getLine());
  118. }
  119. }
  120. break;
  121. case Twig_Token::NUMBER_TYPE:
  122. case Twig_Token::STRING_TYPE:
  123. $this->parser->getStream()->next();
  124. $node = new Twig_Node_Expression_Constant($token->getValue(), $token->getLine());
  125. break;
  126. default:
  127. if ($token->test(Twig_Token::PUNCTUATION_TYPE, '[')) {
  128. $node = $this->parseArrayExpression();
  129. } elseif ($token->test(Twig_Token::PUNCTUATION_TYPE, '{')) {
  130. $node = $this->parseHashExpression();
  131. } else {
  132. throw new Twig_Error_Syntax(sprintf('Unexpected token "%s" of value "%s"', Twig_Token::typeToEnglish($token->getType(), $token->getLine()), $token->getValue()), $token->getLine());
  133. }
  134. }
  135. return $this->parsePostfixExpression($node);
  136. }
  137. public function parseArrayExpression()
  138. {
  139. $stream = $this->parser->getStream();
  140. $stream->expect(Twig_Token::PUNCTUATION_TYPE, '[', 'An array element was expected');
  141. $elements = array();
  142. while (!$stream->test(Twig_Token::PUNCTUATION_TYPE, ']')) {
  143. if (!empty($elements)) {
  144. $stream->expect(Twig_Token::PUNCTUATION_TYPE, ',', 'An array element must be followed by a comma');
  145. // trailing ,?
  146. if ($stream->test(Twig_Token::PUNCTUATION_TYPE, ']')) {
  147. break;
  148. }
  149. }
  150. $elements[] = $this->parseExpression();
  151. }
  152. $stream->expect(Twig_Token::PUNCTUATION_TYPE, ']', 'An opened array is not properly closed');
  153. return new Twig_Node_Expression_Array($elements, $stream->getCurrent()->getLine());
  154. }
  155. public function parseHashExpression()
  156. {
  157. $stream = $this->parser->getStream();
  158. $stream->expect(Twig_Token::PUNCTUATION_TYPE, '{', 'A hash element was expected');
  159. $elements = array();
  160. while (!$stream->test(Twig_Token::PUNCTUATION_TYPE, '}')) {
  161. if (!empty($elements)) {
  162. $stream->expect(Twig_Token::PUNCTUATION_TYPE, ',', 'A hash value must be followed by a comma');
  163. // trailing ,?
  164. if ($stream->test(Twig_Token::PUNCTUATION_TYPE, '}')) {
  165. break;
  166. }
  167. }
  168. if (!$stream->test(Twig_Token::STRING_TYPE) && !$stream->test(Twig_Token::NUMBER_TYPE)) {
  169. $current = $stream->getCurrent();
  170. throw new Twig_Error_Syntax(sprintf('A hash key must be a quoted string or a number (unexpected token "%s" of value "%s"', Twig_Token::typeToEnglish($current->getType(), $current->getLine()), $current->getValue()), $current->getLine());
  171. }
  172. $key = $stream->next()->getValue();
  173. $stream->expect(Twig_Token::PUNCTUATION_TYPE, ':', 'A hash key must be followed by a colon (:)');
  174. $elements[$key] = $this->parseExpression();
  175. }
  176. $stream->expect(Twig_Token::PUNCTUATION_TYPE, '}', 'An opened hash is not properly closed');
  177. return new Twig_Node_Expression_Array($elements, $stream->getCurrent()->getLine());
  178. }
  179. public function parsePostfixExpression($node)
  180. {
  181. while (true) {
  182. $token = $this->parser->getCurrentToken();
  183. if ($token->getType() == Twig_Token::PUNCTUATION_TYPE) {
  184. if ('.' == $token->getValue() || '[' == $token->getValue()) {
  185. $node = $this->parseSubscriptExpression($node);
  186. } elseif ('|' == $token->getValue()) {
  187. $node = $this->parseFilterExpression($node);
  188. } else {
  189. break;
  190. }
  191. } else {
  192. break;
  193. }
  194. }
  195. return $node;
  196. }
  197. public function getFunctionNode($name, $line)
  198. {
  199. $args = $this->parseArguments();
  200. switch ($name) {
  201. case 'parent':
  202. if (!count($this->parser->getBlockStack())) {
  203. throw new Twig_Error_Syntax('Calling "parent" outside a block is forbidden', $line);
  204. }
  205. if (!$this->parser->getParent() && !$this->parser->hasTraits()) {
  206. throw new Twig_Error_Syntax('Calling "parent" on a template that does not extend nor "use" another template is forbidden', $line);
  207. }
  208. return new Twig_Node_Expression_Parent($this->parser->peekBlockStack(), $line);
  209. case 'block':
  210. return new Twig_Node_Expression_BlockReference($args->getNode(0), false, $line);
  211. case 'attribute':
  212. if (count($args) < 2) {
  213. throw new Twig_Error_Syntax('The "attribute" function takes at least two arguments (the variable and the attribute)', $line);
  214. }
  215. return new Twig_Node_Expression_GetAttr($args->getNode(0), $args->getNode(1), count($args) > 2 ? $args->getNode(2) : new Twig_Node_Expression_Array(array(), $line), Twig_TemplateInterface::ANY_CALL, $line);
  216. default:
  217. if (null !== $alias = $this->parser->getImportedFunction($name)) {
  218. return new Twig_Node_Expression_GetAttr($alias['node'], new Twig_Node_Expression_Constant($alias['name'], $line), $args, Twig_TemplateInterface::METHOD_CALL, $line);
  219. }
  220. return new Twig_Node_Expression_Function($name, $args, $line);
  221. }
  222. }
  223. public function parseSubscriptExpression($node)
  224. {
  225. $token = $this->parser->getStream()->next();
  226. $lineno = $token->getLine();
  227. $arguments = new Twig_Node();
  228. $type = Twig_TemplateInterface::ANY_CALL;
  229. if ($token->getValue() == '.') {
  230. $token = $this->parser->getStream()->next();
  231. if (
  232. $token->getType() == Twig_Token::NAME_TYPE
  233. ||
  234. $token->getType() == Twig_Token::NUMBER_TYPE
  235. ||
  236. ($token->getType() == Twig_Token::OPERATOR_TYPE && preg_match(Twig_Lexer::REGEX_NAME, $token->getValue()))
  237. ) {
  238. $arg = new Twig_Node_Expression_Constant($token->getValue(), $lineno);
  239. if ($this->parser->getStream()->test(Twig_Token::PUNCTUATION_TYPE, '(')) {
  240. $type = Twig_TemplateInterface::METHOD_CALL;
  241. $arguments = $this->parseArguments();
  242. } else {
  243. $arguments = new Twig_Node();
  244. }
  245. } else {
  246. throw new Twig_Error_Syntax('Expected name or number', $lineno);
  247. }
  248. } else {
  249. $type = Twig_TemplateInterface::ARRAY_CALL;
  250. $arg = $this->parseExpression();
  251. $this->parser->getStream()->expect(Twig_Token::PUNCTUATION_TYPE, ']');
  252. }
  253. return new Twig_Node_Expression_GetAttr($node, $arg, $arguments, $type, $lineno);
  254. }
  255. public function parseFilterExpression($node)
  256. {
  257. $this->parser->getStream()->next();
  258. return $this->parseFilterExpressionRaw($node);
  259. }
  260. public function parseFilterExpressionRaw($node, $tag = null)
  261. {
  262. while (true) {
  263. $token = $this->parser->getStream()->expect(Twig_Token::NAME_TYPE);
  264. $name = new Twig_Node_Expression_Constant($token->getValue(), $token->getLine());
  265. if (!$this->parser->getStream()->test(Twig_Token::PUNCTUATION_TYPE, '(')) {
  266. $arguments = new Twig_Node();
  267. } else {
  268. $arguments = $this->parseArguments();
  269. }
  270. $node = new Twig_Node_Expression_Filter($node, $name, $arguments, $token->getLine(), $tag);
  271. if (!$this->parser->getStream()->test(Twig_Token::PUNCTUATION_TYPE, '|')) {
  272. break;
  273. }
  274. $this->parser->getStream()->next();
  275. }
  276. return $node;
  277. }
  278. public function parseArguments()
  279. {
  280. $args = array();
  281. $stream = $this->parser->getStream();
  282. $stream->expect(Twig_Token::PUNCTUATION_TYPE, '(', 'A list of arguments must be opened by a parenthesis');
  283. while (!$stream->test(Twig_Token::PUNCTUATION_TYPE, ')')) {
  284. if (!empty($args)) {
  285. $stream->expect(Twig_Token::PUNCTUATION_TYPE, ',', 'Arguments must be separated by a comma');
  286. }
  287. $args[] = $this->parseExpression();
  288. }
  289. $stream->expect(Twig_Token::PUNCTUATION_TYPE, ')', 'A list of arguments must be closed by a parenthesis');
  290. return new Twig_Node($args);
  291. }
  292. public function parseAssignmentExpression()
  293. {
  294. $targets = array();
  295. while (true) {
  296. $token = $this->parser->getStream()->expect(Twig_Token::NAME_TYPE, null, 'Only variables can be assigned to');
  297. if (in_array($token->getValue(), array('true', 'false', 'none'))) {
  298. throw new Twig_Error_Syntax(sprintf('You cannot assign a value to "%s"', $token->getValue()), $token->getLine());
  299. }
  300. $targets[] = new Twig_Node_Expression_AssignName($token->getValue(), $token->getLine());
  301. if (!$this->parser->getStream()->test(Twig_Token::PUNCTUATION_TYPE, ',')) {
  302. break;
  303. }
  304. $this->parser->getStream()->next();
  305. }
  306. return new Twig_Node($targets);
  307. }
  308. public function parseMultitargetExpression()
  309. {
  310. $targets = array();
  311. while (true) {
  312. $targets[] = $this->parseExpression();
  313. if (!$this->parser->getStream()->test(Twig_Token::PUNCTUATION_TYPE, ',')) {
  314. break;
  315. }
  316. $this->parser->getStream()->next();
  317. }
  318. return new Twig_Node($targets);
  319. }
  320. }