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
14/**
15 * @deprecated since version 1.5
16 */
17abstract class Twig_Extensions_SimpleTokenParser extends Twig_TokenParser
18{
19    /**
20     * Parses a token and returns a node.
21     *
22     * @param Twig_Token $token A Twig_Token instance
23     *
24     * @return Twig_Node A Twig_Node instance
25     */
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
39    /**
40     * Gets the grammar as an object or as a string.
41     *
42     * @return string|Twig_Extensions_Grammar A Twig_Extensions_Grammar instance or a string
43     */
44    abstract protected function getGrammar();
45
46    /**
47     * Gets the nodes based on the parsed values.
48     *
49     * @param array $values An array of values
50     * @param int   $line   The parser line
51     */
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    {
56        return new Twig_Node_Expression_GetAttr(
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    {
72        return new Twig_Node_Expression_Filter(
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}
139