diff options
Diffstat (limited to 'inc/Twig/Extensions/Grammar')
-rw-r--r-- | inc/Twig/Extensions/Grammar/Arguments.php | 22 | ||||
-rw-r--r-- | inc/Twig/Extensions/Grammar/Array.php | 22 | ||||
-rw-r--r-- | inc/Twig/Extensions/Grammar/Body.php | 39 | ||||
-rw-r--r-- | inc/Twig/Extensions/Grammar/Boolean.php | 24 | ||||
-rw-r--r-- | inc/Twig/Extensions/Grammar/Constant.php | 37 | ||||
-rw-r--r-- | inc/Twig/Extensions/Grammar/Expression.php | 22 | ||||
-rw-r--r-- | inc/Twig/Extensions/Grammar/Hash.php | 22 | ||||
-rw-r--r-- | inc/Twig/Extensions/Grammar/Number.php | 24 | ||||
-rw-r--r-- | inc/Twig/Extensions/Grammar/Optional.php | 69 | ||||
-rw-r--r-- | inc/Twig/Extensions/Grammar/Switch.php | 24 | ||||
-rw-r--r-- | inc/Twig/Extensions/Grammar/Tag.php | 56 |
11 files changed, 361 insertions, 0 deletions
diff --git a/inc/Twig/Extensions/Grammar/Arguments.php b/inc/Twig/Extensions/Grammar/Arguments.php new file mode 100644 index 00000000..158c05ac --- /dev/null +++ b/inc/Twig/Extensions/Grammar/Arguments.php | |||
@@ -0,0 +1,22 @@ | |||
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 | class Twig_Extensions_Grammar_Arguments extends Twig_Extensions_Grammar | ||
12 | { | ||
13 | public function __toString() | ||
14 | { | ||
15 | return sprintf('<%s:arguments>', $this->name); | ||
16 | } | ||
17 | |||
18 | public function parse(Twig_Token $token) | ||
19 | { | ||
20 | return $this->parser->getExpressionParser()->parseArguments(); | ||
21 | } | ||
22 | } | ||
diff --git a/inc/Twig/Extensions/Grammar/Array.php b/inc/Twig/Extensions/Grammar/Array.php new file mode 100644 index 00000000..34aece0f --- /dev/null +++ b/inc/Twig/Extensions/Grammar/Array.php | |||
@@ -0,0 +1,22 @@ | |||
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 | class Twig_Extensions_Grammar_Array extends Twig_Extensions_Grammar | ||
12 | { | ||
13 | public function __toString() | ||
14 | { | ||
15 | return sprintf('<%s:array>', $this->name); | ||
16 | } | ||
17 | |||
18 | public function parse(Twig_Token $token) | ||
19 | { | ||
20 | return $this->parser->getExpressionParser()->parseArrayExpression(); | ||
21 | } | ||
22 | } | ||
diff --git a/inc/Twig/Extensions/Grammar/Body.php b/inc/Twig/Extensions/Grammar/Body.php new file mode 100644 index 00000000..540cfc75 --- /dev/null +++ b/inc/Twig/Extensions/Grammar/Body.php | |||
@@ -0,0 +1,39 @@ | |||
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 | class Twig_Extensions_Grammar_Body extends Twig_Extensions_Grammar | ||
12 | { | ||
13 | protected $end; | ||
14 | |||
15 | public function __construct($name, $end = null) | ||
16 | { | ||
17 | parent::__construct($name); | ||
18 | |||
19 | $this->end = null === $end ? 'end'.$name : $end; | ||
20 | } | ||
21 | |||
22 | public function __toString() | ||
23 | { | ||
24 | return sprintf('<%s:body>', $this->name); | ||
25 | } | ||
26 | |||
27 | public function parse(Twig_Token $token) | ||
28 | { | ||
29 | $stream = $this->parser->getStream(); | ||
30 | $stream->expect(Twig_Token::BLOCK_END_TYPE); | ||
31 | |||
32 | return $this->parser->subparse(array($this, 'decideBlockEnd'), true); | ||
33 | } | ||
34 | |||
35 | public function decideBlockEnd(Twig_Token $token) | ||
36 | { | ||
37 | return $token->test($this->end); | ||
38 | } | ||
39 | } | ||
diff --git a/inc/Twig/Extensions/Grammar/Boolean.php b/inc/Twig/Extensions/Grammar/Boolean.php new file mode 100644 index 00000000..c0048090 --- /dev/null +++ b/inc/Twig/Extensions/Grammar/Boolean.php | |||
@@ -0,0 +1,24 @@ | |||
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 | class Twig_Extensions_Grammar_Boolean extends Twig_Extensions_Grammar | ||
12 | { | ||
13 | public function __toString() | ||
14 | { | ||
15 | return sprintf('<%s:boolean>', $this->name); | ||
16 | } | ||
17 | |||
18 | public function parse(Twig_Token $token) | ||
19 | { | ||
20 | $this->parser->getStream()->expect(Twig_Token::NAME_TYPE, array('true', 'false')); | ||
21 | |||
22 | return new Twig_Node_Expression_Constant('true' === $token->getValue() ? true : false, $token->getLine()); | ||
23 | } | ||
24 | } | ||
diff --git a/inc/Twig/Extensions/Grammar/Constant.php b/inc/Twig/Extensions/Grammar/Constant.php new file mode 100644 index 00000000..9df60458 --- /dev/null +++ b/inc/Twig/Extensions/Grammar/Constant.php | |||
@@ -0,0 +1,37 @@ | |||
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 | class Twig_Extensions_Grammar_Constant extends Twig_Extensions_Grammar | ||
12 | { | ||
13 | protected $type; | ||
14 | |||
15 | public function __construct($name, $type = null) | ||
16 | { | ||
17 | $this->name = $name; | ||
18 | $this->type = null === $type ? Twig_Token::NAME_TYPE : $type; | ||
19 | } | ||
20 | |||
21 | public function __toString() | ||
22 | { | ||
23 | return $this->name; | ||
24 | } | ||
25 | |||
26 | public function parse(Twig_Token $token) | ||
27 | { | ||
28 | $this->parser->getStream()->expect($this->type, $this->name); | ||
29 | |||
30 | return $this->name; | ||
31 | } | ||
32 | |||
33 | public function getType() | ||
34 | { | ||
35 | return $this->type; | ||
36 | } | ||
37 | } | ||
diff --git a/inc/Twig/Extensions/Grammar/Expression.php b/inc/Twig/Extensions/Grammar/Expression.php new file mode 100644 index 00000000..4c33df0e --- /dev/null +++ b/inc/Twig/Extensions/Grammar/Expression.php | |||
@@ -0,0 +1,22 @@ | |||
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 | class Twig_Extensions_Grammar_Expression extends Twig_Extensions_Grammar | ||
12 | { | ||
13 | public function __toString() | ||
14 | { | ||
15 | return sprintf('<%s>', $this->name); | ||
16 | } | ||
17 | |||
18 | public function parse(Twig_Token $token) | ||
19 | { | ||
20 | return $this->parser->getExpressionParser()->parseExpression(); | ||
21 | } | ||
22 | } | ||
diff --git a/inc/Twig/Extensions/Grammar/Hash.php b/inc/Twig/Extensions/Grammar/Hash.php new file mode 100644 index 00000000..98b07d20 --- /dev/null +++ b/inc/Twig/Extensions/Grammar/Hash.php | |||
@@ -0,0 +1,22 @@ | |||
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 | class Twig_Extensions_Grammar_Hash extends Twig_Extensions_Grammar | ||
12 | { | ||
13 | public function __toString() | ||
14 | { | ||
15 | return sprintf('<%s:hash>', $this->name); | ||
16 | } | ||
17 | |||
18 | public function parse(Twig_Token $token) | ||
19 | { | ||
20 | return $this->parser->getExpressionParser()->parseHashExpression(); | ||
21 | } | ||
22 | } | ||
diff --git a/inc/Twig/Extensions/Grammar/Number.php b/inc/Twig/Extensions/Grammar/Number.php new file mode 100644 index 00000000..f0857d20 --- /dev/null +++ b/inc/Twig/Extensions/Grammar/Number.php | |||
@@ -0,0 +1,24 @@ | |||
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 | class Twig_Extensions_Grammar_Number extends Twig_Extensions_Grammar | ||
12 | { | ||
13 | public function __toString() | ||
14 | { | ||
15 | return sprintf('<%s:number>', $this->name); | ||
16 | } | ||
17 | |||
18 | public function parse(Twig_Token $token) | ||
19 | { | ||
20 | $this->parser->getStream()->expect(Twig_Token::NUMBER_TYPE); | ||
21 | |||
22 | return new Twig_Node_Expression_Constant($token->getValue(), $token->getLine()); | ||
23 | } | ||
24 | } | ||
diff --git a/inc/Twig/Extensions/Grammar/Optional.php b/inc/Twig/Extensions/Grammar/Optional.php new file mode 100644 index 00000000..da427485 --- /dev/null +++ b/inc/Twig/Extensions/Grammar/Optional.php | |||
@@ -0,0 +1,69 @@ | |||
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 | class Twig_Extensions_Grammar_Optional extends Twig_Extensions_Grammar | ||
12 | { | ||
13 | protected $grammar; | ||
14 | |||
15 | public function __construct() | ||
16 | { | ||
17 | $this->grammar = array(); | ||
18 | foreach (func_get_args() as $grammar) { | ||
19 | $this->addGrammar($grammar); | ||
20 | } | ||
21 | } | ||
22 | |||
23 | public function __toString() | ||
24 | { | ||
25 | $repr = array(); | ||
26 | foreach ($this->grammar as $grammar) { | ||
27 | $repr[] = (string) $grammar; | ||
28 | } | ||
29 | |||
30 | return sprintf('[%s]', implode(' ', $repr)); | ||
31 | } | ||
32 | |||
33 | public function addGrammar(Twig_Extensions_GrammarInterface $grammar) | ||
34 | { | ||
35 | $this->grammar[] = $grammar; | ||
36 | } | ||
37 | |||
38 | public function parse(Twig_Token $token) | ||
39 | { | ||
40 | // test if we have the optional element before consuming it | ||
41 | if ($this->grammar[0] instanceof Twig_Extensions_Grammar_Constant) { | ||
42 | if (!$this->parser->getStream()->test($this->grammar[0]->getType(), $this->grammar[0]->getName())) { | ||
43 | return array(); | ||
44 | } | ||
45 | } elseif ($this->grammar[0] instanceof Twig_Extensions_Grammar_Name) { | ||
46 | if (!$this->parser->getStream()->test(Twig_Token::NAME_TYPE)) { | ||
47 | return array(); | ||
48 | } | ||
49 | } elseif ($this->parser->getStream()->test(Twig_Token::BLOCK_END_TYPE)) { | ||
50 | // if this is not a Constant or a Name, it must be the last element of the tag | ||
51 | |||
52 | return array(); | ||
53 | } | ||
54 | |||
55 | $elements = array(); | ||
56 | foreach ($this->grammar as $grammar) { | ||
57 | $grammar->setParser($this->parser); | ||
58 | |||
59 | $element = $grammar->parse($token); | ||
60 | if (is_array($element)) { | ||
61 | $elements = array_merge($elements, $element); | ||
62 | } else { | ||
63 | $elements[$grammar->getName()] = $element; | ||
64 | } | ||
65 | } | ||
66 | |||
67 | return $elements; | ||
68 | } | ||
69 | } | ||
diff --git a/inc/Twig/Extensions/Grammar/Switch.php b/inc/Twig/Extensions/Grammar/Switch.php new file mode 100644 index 00000000..4245f2c8 --- /dev/null +++ b/inc/Twig/Extensions/Grammar/Switch.php | |||
@@ -0,0 +1,24 @@ | |||
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 | class Twig_Extensions_Grammar_Switch extends Twig_Extensions_Grammar | ||
12 | { | ||
13 | public function __toString() | ||
14 | { | ||
15 | return sprintf('<%s:switch>', $this->name); | ||
16 | } | ||
17 | |||
18 | public function parse(Twig_Token $token) | ||
19 | { | ||
20 | $this->parser->getStream()->expect(Twig_Token::NAME_TYPE, $this->name); | ||
21 | |||
22 | return new Twig_Node_Expression_Constant(true, $token->getLine()); | ||
23 | } | ||
24 | } | ||
diff --git a/inc/Twig/Extensions/Grammar/Tag.php b/inc/Twig/Extensions/Grammar/Tag.php new file mode 100644 index 00000000..727f2610 --- /dev/null +++ b/inc/Twig/Extensions/Grammar/Tag.php | |||
@@ -0,0 +1,56 @@ | |||
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 | class Twig_Extensions_Grammar_Tag extends Twig_Extensions_Grammar | ||
12 | { | ||
13 | protected $grammar; | ||
14 | |||
15 | public function __construct() | ||
16 | { | ||
17 | $this->grammar = array(); | ||
18 | foreach (func_get_args() as $grammar) { | ||
19 | $this->addGrammar($grammar); | ||
20 | } | ||
21 | } | ||
22 | |||
23 | public function __toString() | ||
24 | { | ||
25 | $repr = array(); | ||
26 | foreach ($this->grammar as $grammar) { | ||
27 | $repr[] = (string) $grammar; | ||
28 | } | ||
29 | |||
30 | return implode(' ', $repr); | ||
31 | } | ||
32 | |||
33 | public function addGrammar(Twig_Extensions_GrammarInterface $grammar) | ||
34 | { | ||
35 | $this->grammar[] = $grammar; | ||
36 | } | ||
37 | |||
38 | public function parse(Twig_Token $token) | ||
39 | { | ||
40 | $elements = array(); | ||
41 | foreach ($this->grammar as $grammar) { | ||
42 | $grammar->setParser($this->parser); | ||
43 | |||
44 | $element = $grammar->parse($token); | ||
45 | if (is_array($element)) { | ||
46 | $elements = array_merge($elements, $element); | ||
47 | } else { | ||
48 | $elements[$grammar->getName()] = $element; | ||
49 | } | ||
50 | } | ||
51 | |||
52 | $this->parser->getStream()->expect(Twig_Token::BLOCK_END_TYPE); | ||
53 | |||
54 | return $elements; | ||
55 | } | ||
56 | } | ||