SimpleTokenParser.php 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. <?php
  2. /*
  3. * This file is part of Twig.
  4. *
  5. * (c) 2010 Fabien Potencier
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. @trigger_error('The grammar feature is deprecated since version 1.5 and will be removed in 2.0.', E_USER_DEPRECATED);
  11. /**
  12. * @deprecated since version 1.5
  13. */
  14. abstract class Twig_Extensions_SimpleTokenParser extends Twig_TokenParser
  15. {
  16. /**
  17. * Parses a token and returns a node.
  18. *
  19. * @param Twig_Token $token A Twig_Token instance
  20. *
  21. * @return Twig_Node A Twig_Node instance
  22. */
  23. public function parse(Twig_Token $token)
  24. {
  25. $grammar = $this->getGrammar();
  26. if (!is_object($grammar)) {
  27. $grammar = self::parseGrammar($grammar);
  28. }
  29. $grammar->setParser($this->parser);
  30. $values = $grammar->parse($token);
  31. return $this->getNode($values, $token->getLine());
  32. }
  33. /**
  34. * Gets the grammar as an object or as a string.
  35. *
  36. * @return string|Twig_Extensions_Grammar A Twig_Extensions_Grammar instance or a string
  37. */
  38. abstract protected function getGrammar();
  39. /**
  40. * Gets the nodes based on the parsed values.
  41. *
  42. * @param array $values An array of values
  43. * @param int $line The parser line
  44. */
  45. abstract protected function getNode(array $values, $line);
  46. protected function getAttribute($node, $attribute, $arguments = array(), $type = Twig_Node_Expression_GetAttr::TYPE_ANY, $line = -1)
  47. {
  48. return new Twig_Node_Expression_GetAttr(
  49. $node instanceof Twig_Node ? $node : new Twig_Node_Expression_Name($node, $line),
  50. $attribute instanceof Twig_Node ? $attribute : new Twig_Node_Expression_Constant($attribute, $line),
  51. $arguments instanceof Twig_Node ? $arguments : new Twig_Node($arguments),
  52. $type,
  53. $line
  54. );
  55. }
  56. protected function call($node, $attribute, $arguments = array(), $line = -1)
  57. {
  58. return $this->getAttribute($node, $attribute, $arguments, Twig_Node_Expression_GetAttr::TYPE_METHOD, $line);
  59. }
  60. protected function markAsSafe(Twig_Node $node, $line = -1)
  61. {
  62. return new Twig_Node_Expression_Filter(
  63. $node,
  64. new Twig_Node_Expression_Constant('raw', $line),
  65. new Twig_Node(),
  66. $line
  67. );
  68. }
  69. protected function output(Twig_Node $node, $line = -1)
  70. {
  71. return new Twig_Node_Print($node, $line);
  72. }
  73. protected function getNodeValues(array $values)
  74. {
  75. $nodes = array();
  76. foreach ($values as $value) {
  77. if ($value instanceof Twig_Node) {
  78. $nodes[] = $value;
  79. }
  80. }
  81. return $nodes;
  82. }
  83. public static function parseGrammar($str, $main = true)
  84. {
  85. static $cursor;
  86. if (true === $main) {
  87. $cursor = 0;
  88. $grammar = new Twig_Extensions_Grammar_Tag();
  89. } else {
  90. $grammar = new Twig_Extensions_Grammar_Optional();
  91. }
  92. while ($cursor < strlen($str)) {
  93. if (preg_match('/\s+/A', $str, $match, null, $cursor)) {
  94. $cursor += strlen($match[0]);
  95. } elseif (preg_match('/<(\w+)(?:\:(\w+))?>/A', $str, $match, null, $cursor)) {
  96. $class = sprintf('Twig_Extensions_Grammar_%s', ucfirst(isset($match[2]) ? $match[2] : 'Expression'));
  97. if (!class_exists($class)) {
  98. throw new Twig_Error_Runtime(sprintf('Unable to understand "%s" in grammar (%s class does not exist)', $match[0], $class));
  99. }
  100. $grammar->addGrammar(new $class($match[1]));
  101. $cursor += strlen($match[0]);
  102. } elseif (preg_match('/\w+/A', $str, $match, null, $cursor)) {
  103. $grammar->addGrammar(new Twig_Extensions_Grammar_Constant($match[0]));
  104. $cursor += strlen($match[0]);
  105. } elseif (preg_match('/,/A', $str, $match, null, $cursor)) {
  106. $grammar->addGrammar(new Twig_Extensions_Grammar_Constant($match[0], Twig_Token::PUNCTUATION_TYPE));
  107. $cursor += strlen($match[0]);
  108. } elseif (preg_match('/\[/A', $str, $match, null, $cursor)) {
  109. $cursor += strlen($match[0]);
  110. $grammar->addGrammar(self::parseGrammar($str, false));
  111. } elseif (true !== $main && preg_match('/\]/A', $str, $match, null, $cursor)) {
  112. $cursor += strlen($match[0]);
  113. return $grammar;
  114. } else {
  115. throw new Twig_Error_Runtime(sprintf('Unable to parse grammar "%s" near "...%s..."', $str, substr($str, $cursor, 10)));
  116. }
  117. }
  118. return $grammar;
  119. }
  120. }