From a4565e88edbc8e3bd092a475469769c86a4c350c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nicolas=20L=C5=93uillet?= Date: Fri, 2 Aug 2013 22:40:51 +0200 Subject: add Twig & refactor poche --- inc/Twig/Extensions/Grammar/Arguments.php | 22 ++++++++++ inc/Twig/Extensions/Grammar/Array.php | 22 ++++++++++ inc/Twig/Extensions/Grammar/Body.php | 39 +++++++++++++++++ inc/Twig/Extensions/Grammar/Boolean.php | 24 +++++++++++ inc/Twig/Extensions/Grammar/Constant.php | 37 ++++++++++++++++ inc/Twig/Extensions/Grammar/Expression.php | 22 ++++++++++ inc/Twig/Extensions/Grammar/Hash.php | 22 ++++++++++ inc/Twig/Extensions/Grammar/Number.php | 24 +++++++++++ inc/Twig/Extensions/Grammar/Optional.php | 69 ++++++++++++++++++++++++++++++ inc/Twig/Extensions/Grammar/Switch.php | 24 +++++++++++ inc/Twig/Extensions/Grammar/Tag.php | 56 ++++++++++++++++++++++++ 11 files changed, 361 insertions(+) create mode 100644 inc/Twig/Extensions/Grammar/Arguments.php create mode 100644 inc/Twig/Extensions/Grammar/Array.php create mode 100644 inc/Twig/Extensions/Grammar/Body.php create mode 100644 inc/Twig/Extensions/Grammar/Boolean.php create mode 100644 inc/Twig/Extensions/Grammar/Constant.php create mode 100644 inc/Twig/Extensions/Grammar/Expression.php create mode 100644 inc/Twig/Extensions/Grammar/Hash.php create mode 100644 inc/Twig/Extensions/Grammar/Number.php create mode 100644 inc/Twig/Extensions/Grammar/Optional.php create mode 100644 inc/Twig/Extensions/Grammar/Switch.php create mode 100644 inc/Twig/Extensions/Grammar/Tag.php (limited to 'inc/Twig/Extensions/Grammar') 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 @@ +', $this->name); + } + + public function parse(Twig_Token $token) + { + return $this->parser->getExpressionParser()->parseArguments(); + } +} 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 @@ +', $this->name); + } + + public function parse(Twig_Token $token) + { + return $this->parser->getExpressionParser()->parseArrayExpression(); + } +} 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 @@ +end = null === $end ? 'end'.$name : $end; + } + + public function __toString() + { + return sprintf('<%s:body>', $this->name); + } + + public function parse(Twig_Token $token) + { + $stream = $this->parser->getStream(); + $stream->expect(Twig_Token::BLOCK_END_TYPE); + + return $this->parser->subparse(array($this, 'decideBlockEnd'), true); + } + + public function decideBlockEnd(Twig_Token $token) + { + return $token->test($this->end); + } +} 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 @@ +', $this->name); + } + + public function parse(Twig_Token $token) + { + $this->parser->getStream()->expect(Twig_Token::NAME_TYPE, array('true', 'false')); + + return new Twig_Node_Expression_Constant('true' === $token->getValue() ? true : false, $token->getLine()); + } +} 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 @@ +name = $name; + $this->type = null === $type ? Twig_Token::NAME_TYPE : $type; + } + + public function __toString() + { + return $this->name; + } + + public function parse(Twig_Token $token) + { + $this->parser->getStream()->expect($this->type, $this->name); + + return $this->name; + } + + public function getType() + { + return $this->type; + } +} 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 @@ +', $this->name); + } + + public function parse(Twig_Token $token) + { + return $this->parser->getExpressionParser()->parseExpression(); + } +} 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 @@ +', $this->name); + } + + public function parse(Twig_Token $token) + { + return $this->parser->getExpressionParser()->parseHashExpression(); + } +} 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 @@ +', $this->name); + } + + public function parse(Twig_Token $token) + { + $this->parser->getStream()->expect(Twig_Token::NUMBER_TYPE); + + return new Twig_Node_Expression_Constant($token->getValue(), $token->getLine()); + } +} 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 @@ +grammar = array(); + foreach (func_get_args() as $grammar) { + $this->addGrammar($grammar); + } + } + + public function __toString() + { + $repr = array(); + foreach ($this->grammar as $grammar) { + $repr[] = (string) $grammar; + } + + return sprintf('[%s]', implode(' ', $repr)); + } + + public function addGrammar(Twig_Extensions_GrammarInterface $grammar) + { + $this->grammar[] = $grammar; + } + + public function parse(Twig_Token $token) + { + // test if we have the optional element before consuming it + if ($this->grammar[0] instanceof Twig_Extensions_Grammar_Constant) { + if (!$this->parser->getStream()->test($this->grammar[0]->getType(), $this->grammar[0]->getName())) { + return array(); + } + } elseif ($this->grammar[0] instanceof Twig_Extensions_Grammar_Name) { + if (!$this->parser->getStream()->test(Twig_Token::NAME_TYPE)) { + return array(); + } + } elseif ($this->parser->getStream()->test(Twig_Token::BLOCK_END_TYPE)) { + // if this is not a Constant or a Name, it must be the last element of the tag + + return array(); + } + + $elements = array(); + foreach ($this->grammar as $grammar) { + $grammar->setParser($this->parser); + + $element = $grammar->parse($token); + if (is_array($element)) { + $elements = array_merge($elements, $element); + } else { + $elements[$grammar->getName()] = $element; + } + } + + return $elements; + } +} 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 @@ +', $this->name); + } + + public function parse(Twig_Token $token) + { + $this->parser->getStream()->expect(Twig_Token::NAME_TYPE, $this->name); + + return new Twig_Node_Expression_Constant(true, $token->getLine()); + } +} 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 @@ +grammar = array(); + foreach (func_get_args() as $grammar) { + $this->addGrammar($grammar); + } + } + + public function __toString() + { + $repr = array(); + foreach ($this->grammar as $grammar) { + $repr[] = (string) $grammar; + } + + return implode(' ', $repr); + } + + public function addGrammar(Twig_Extensions_GrammarInterface $grammar) + { + $this->grammar[] = $grammar; + } + + public function parse(Twig_Token $token) + { + $elements = array(); + foreach ($this->grammar as $grammar) { + $grammar->setParser($this->parser); + + $element = $grammar->parse($token); + if (is_array($element)) { + $elements = array_merge($elements, $element); + } else { + $elements[$grammar->getName()] = $element; + } + } + + $this->parser->getStream()->expect(Twig_Token::BLOCK_END_TYPE); + + return $elements; + } +} -- cgit v1.2.3