SimpleTokenParser.php 4.6KB

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