ILIAS  release_5-4 Revision v5.4.26-12-gabc799a52e6
SimpleTokenParser.php
Go to the documentation of this file.
1<?php
2
3/*
4 * This file is part of Twig.
5 *
6 * (c) 2010 Fabien Potencier
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@trigger_error('The grammar feature is deprecated since version 1.5 and will be removed in 2.0.', E_USER_DEPRECATED);
13
18{
26 public function parse(Twig_Token $token)
27 {
28 $grammar = $this->getGrammar();
29 if (!is_object($grammar)) {
30 $grammar = self::parseGrammar($grammar);
31 }
32
33 $grammar->setParser($this->parser);
34 $values = $grammar->parse($token);
35
36 return $this->getNode($values, $token->getLine());
37 }
38
44 abstract protected function getGrammar();
45
52 abstract protected function getNode(array $values, $line);
53
54 protected function getAttribute($node, $attribute, $arguments = array(), $type = Twig_Node_Expression_GetAttr::TYPE_ANY, $line = -1)
55 {
57 $node instanceof Twig_Node ? $node : new Twig_Node_Expression_Name($node, $line),
58 $attribute instanceof Twig_Node ? $attribute : new Twig_Node_Expression_Constant($attribute, $line),
59 $arguments instanceof Twig_Node ? $arguments : new Twig_Node($arguments),
60 $type,
61 $line
62 );
63 }
64
65 protected function call($node, $attribute, $arguments = array(), $line = -1)
66 {
67 return $this->getAttribute($node, $attribute, $arguments, Twig_Node_Expression_GetAttr::TYPE_METHOD, $line);
68 }
69
70 protected function markAsSafe(Twig_Node $node, $line = -1)
71 {
73 $node,
74 new Twig_Node_Expression_Constant('raw', $line),
75 new Twig_Node(),
76 $line
77 );
78 }
79
80 protected function output(Twig_Node $node, $line = -1)
81 {
82 return new Twig_Node_Print($node, $line);
83 }
84
85 protected function getNodeValues(array $values)
86 {
87 $nodes = array();
88 foreach ($values as $value) {
89 if ($value instanceof Twig_Node) {
90 $nodes[] = $value;
91 }
92 }
93
94 return $nodes;
95 }
96
97 public static function parseGrammar($str, $main = true)
98 {
99 static $cursor;
100
101 if (true === $main) {
102 $cursor = 0;
103 $grammar = new Twig_Extensions_Grammar_Tag();
104 } else {
105 $grammar = new Twig_Extensions_Grammar_Optional();
106 }
107
108 while ($cursor < strlen($str)) {
109 if (preg_match('/\s+/A', $str, $match, null, $cursor)) {
110 $cursor += strlen($match[0]);
111 } elseif (preg_match('/<(\w+)(?:\:(\w+))?>/A', $str, $match, null, $cursor)) {
112 $class = sprintf('Twig_Extensions_Grammar_%s', ucfirst(isset($match[2]) ? $match[2] : 'Expression'));
113 if (!class_exists($class)) {
114 throw new Twig_Error_Runtime(sprintf('Unable to understand "%s" in grammar (%s class does not exist)', $match[0], $class));
115 }
116 $grammar->addGrammar(new $class($match[1]));
117 $cursor += strlen($match[0]);
118 } elseif (preg_match('/\w+/A', $str, $match, null, $cursor)) {
119 $grammar->addGrammar(new Twig_Extensions_Grammar_Constant($match[0]));
120 $cursor += strlen($match[0]);
121 } elseif (preg_match('/,/A', $str, $match, null, $cursor)) {
122 $grammar->addGrammar(new Twig_Extensions_Grammar_Constant($match[0], Twig_Token::PUNCTUATION_TYPE));
123 $cursor += strlen($match[0]);
124 } elseif (preg_match('/\[/A', $str, $match, null, $cursor)) {
125 $cursor += strlen($match[0]);
126 $grammar->addGrammar(self::parseGrammar($str, false));
127 } elseif (true !== $main && preg_match('/\]/A', $str, $match, null, $cursor)) {
128 $cursor += strlen($match[0]);
129
130 return $grammar;
131 } else {
132 throw new Twig_Error_Runtime(sprintf('Unable to parse grammar "%s" near "...%s..."', $str, substr($str, $cursor, 10)));
133 }
134 }
135
136 return $grammar;
137 }
138}
An exception for terminatinating execution or to throw for unit testing.
Exception thrown when an error occurs at runtime.
Definition: Runtime.php:19
getNode(array $values, $line)
Gets the nodes based on the parsed values.
output(Twig_Node $node, $line=-1)
getAttribute($node, $attribute, $arguments=array(), $type=Twig_Node_Expression_GetAttr::TYPE_ANY, $line=-1)
static parseGrammar($str, $main=true)
call($node, $attribute, $arguments=array(), $line=-1)
getGrammar()
Gets the grammar as an object or as a string.
markAsSafe(Twig_Node $node, $line=-1)
parse(Twig_Token $token)
Parses a token and returns a node.
Represents a node that outputs an expression.
Definition: Print.php:19
Represents a node in the AST.
Definition: Node.php:19
Base class for all token parsers.
Definition: TokenParser.php:18
Represents a Token.
Definition: Token.php:21
getLine()
Definition: Token.php:87
const PUNCTUATION_TYPE
Definition: Token.php:36
$type
$values