Permalink
Browse files

[ExpressionLanguage] added the component

  • Loading branch information...
1 parent 091a96c commit 9d98fa25ec904c5702959d7d06b6f556a707f796 @fabpot fabpot committed Sep 1, 2013
Showing with 2,414 additions and 0 deletions.
  1. +1 −0 composer.json
  2. +3 −0 src/Symfony/Component/ExpressionLanguage/.gitignore
  3. +7 −0 src/Symfony/Component/ExpressionLanguage/CHANGELOG.md
  4. +148 −0 src/Symfony/Component/ExpressionLanguage/Compiler.php
  5. +42 −0 src/Symfony/Component/ExpressionLanguage/Expression.php
  6. +103 −0 src/Symfony/Component/ExpressionLanguage/ExpressionLanguage.php
  7. +19 −0 src/Symfony/Component/ExpressionLanguage/LICENSE
  8. +119 −0 src/Symfony/Component/ExpressionLanguage/Lexer.php
  9. +22 −0 src/Symfony/Component/ExpressionLanguage/Node/ArgumentsNode.php
  10. +85 −0 src/Symfony/Component/ExpressionLanguage/Node/ArrayNode.php
  11. +129 −0 src/Symfony/Component/ExpressionLanguage/Node/BinaryNode.php
  12. +44 −0 src/Symfony/Component/ExpressionLanguage/Node/ConditionalNode.php
  13. +32 −0 src/Symfony/Component/ExpressionLanguage/Node/ConstantNode.php
  14. +45 −0 src/Symfony/Component/ExpressionLanguage/Node/FunctionNode.php
  15. +90 −0 src/Symfony/Component/ExpressionLanguage/Node/GetAttrNode.php
  16. +32 −0 src/Symfony/Component/ExpressionLanguage/Node/NameNode.php
  17. +78 −0 src/Symfony/Component/ExpressionLanguage/Node/Node.php
  18. +54 −0 src/Symfony/Component/ExpressionLanguage/Node/UnaryNode.php
  19. +356 −0 src/Symfony/Component/ExpressionLanguage/Parser.php
  20. +43 −0 src/Symfony/Component/ExpressionLanguage/README.md
  21. +20 −0 src/Symfony/Component/ExpressionLanguage/SyntaxError.php
  22. +75 −0 src/Symfony/Component/ExpressionLanguage/Tests/LexerTest.php
  23. +39 −0 src/Symfony/Component/ExpressionLanguage/Tests/Node/AbstractNodeTest.php
  24. +30 −0 src/Symfony/Component/ExpressionLanguage/Tests/Node/ArgumentsNodeTest.php
  25. +46 −0 src/Symfony/Component/ExpressionLanguage/Tests/Node/ArrayNodeTest.php
  26. +113 −0 src/Symfony/Component/ExpressionLanguage/Tests/Node/BinaryNodeTest.php
  27. +34 −0 src/Symfony/Component/ExpressionLanguage/Tests/Node/ConditionalNodeTest.php
  28. +43 −0 src/Symfony/Component/ExpressionLanguage/Tests/Node/ConstantNodeTest.php
  29. +45 −0 src/Symfony/Component/ExpressionLanguage/Tests/Node/FunctionNodeTest.php
  30. +64 −0 src/Symfony/Component/ExpressionLanguage/Tests/Node/GetAttrNodeTest.php
  31. +31 −0 src/Symfony/Component/ExpressionLanguage/Tests/Node/NameNodeTest.php
  32. +30 −0 src/Symfony/Component/ExpressionLanguage/Tests/Node/NodeTest.php
  33. +38 −0 src/Symfony/Component/ExpressionLanguage/Tests/Node/UnaryNodeTest.php
  34. +143 −0 src/Symfony/Component/ExpressionLanguage/Tests/ParserTest.php
  35. +68 −0 src/Symfony/Component/ExpressionLanguage/Token.php
  36. +83 −0 src/Symfony/Component/ExpressionLanguage/TokenStream.php
  37. +31 −0 src/Symfony/Component/ExpressionLanguage/composer.json
  38. +29 −0 src/Symfony/Component/ExpressionLanguage/phpunit.xml.dist
View
@@ -33,6 +33,7 @@
"symfony/doctrine-bridge": "self.version",
"symfony/dom-crawler": "self.version",
"symfony/event-dispatcher": "self.version",
+ "symfony/expression-language": "self.version",
"symfony/filesystem": "self.version",
"symfony/finder": "self.version",
"symfony/form": "self.version",
@@ -0,0 +1,3 @@
+vendor/
+composer.lock
+phpunit.xml
@@ -0,0 +1,7 @@
+CHANGELOG
+=========
+
+2.4.0
+-----
+
+ * added the component
@@ -0,0 +1,148 @@
+<?php
+
+/*
+ * This file is part of the Symfony package.
+ *
+ * (c) Fabien Potencier <fabien@symfony.com>
+ *
+ * For the full copyright and license information, please view the LICENSE
+ * file that was distributed with this source code.
+ */
+
+namespace Symfony\Component\ExpressionLanguage;
+
+/**
+ * Compiles a node to PHP code.
+ *
+ * @author Fabien Potencier <fabien@symfony.com>
+ */
+class Compiler
+{
+ private $source;
+ private $functions;
+
+ public function __construct(array $functions)
+ {
+ $this->functions = $functions;
+ }
+
+ public function getFunction($name)
+ {
+ return $this->functions[$name];
+ }
+
+ /**
+ * Gets the current PHP code after compilation.
+ *
+ * @return string The PHP code
+ */
+ public function getSource()
+ {
+ return $this->source;
+ }
+
+ public function reset()
+ {
+ $this->source = '';
+
+ return $this;
+ }
+
+ /**
+ * Compiles a node.
+ *
+ * @param Node\Node $node The node to compile
+ *
+ * @return Compiler The current compiler instance
+ */
+ public function compile(Node\Node $node)
+ {
+ $node->compile($this);
+
+ return $this;
+ }
+
+ public function subcompile(Node\Node $node)
+ {
+ $current = $this->source;
+ $this->source = '';
+
+ $node->compile($this);
+
+ $source = $this->source;
+ $this->source = $current;
+
+ return $source;
+ }
+
+ /**
+ * Adds a raw string to the compiled code.
+ *
+ * @param string $string The string
+ *
+ * @return Compiler The current compiler instance
+ */
+ public function raw($string)
+ {
+ $this->source .= $string;
+
+ return $this;
+ }
+
+ /**
+ * Adds a quoted string to the compiled code.
+ *
+ * @param string $value The string
+ *
+ * @return Compiler The current compiler instance
+ */
+ public function string($value)
+ {
+ $this->source .= sprintf('"%s"', addcslashes($value, "\0\t\"\$\\"));
+
+ return $this;
+ }
+
+ /**
+ * Returns a PHP representation of a given value.
+ *
+ * @param mixed $value The value to convert
+ *
+ * @return Compiler The current compiler instance
+ */
+ public function repr($value)
+ {
+ if (is_int($value) || is_float($value)) {
+ if (false !== $locale = setlocale(LC_NUMERIC, 0)) {
+ setlocale(LC_NUMERIC, 'C');
+ }
+
+ $this->raw($value);
+
+ if (false !== $locale) {
+ setlocale(LC_NUMERIC, $locale);
+ }
+ } elseif (null === $value) {
+ $this->raw('null');
+ } elseif (is_bool($value)) {
+ $this->raw($value ? 'true' : 'false');
+ } elseif (is_array($value)) {
+ $this->raw('array(');
+ $first = true;
+ foreach ($value as $key => $value) {
+ if (!$first) {
+ $this->raw(', ');
+ }
+ $first = false;
+ $this->repr($key);
+ $this->raw(' => ');
+ $this->repr($value);
+ }
+ $this->raw(')');
+ } else {
+ $this->string($value);
+ }
+
+ return $this;
+ }
+}
@@ -0,0 +1,42 @@
+<?php
+
+/*
+ * This file is part of the Symfony package.
+ *
+ * (c) Fabien Potencier <fabien@symfony.com>
+ *
+ * For the full copyright and license information, please view the LICENSE
+ * file that was distributed with this source code.
+ */
+
+namespace Symfony\Component\ExpressionLanguage;
+
+/**
+ * Represents an expression.
+ *
+ * @author Fabien Potencier <fabien@symfony.com>
+ */
+class Expression
+{
+ private $expression;
+
+ /**
+ * Constructor.
+ *
+ * @param string $expression An expression
+ */
+ public function __construct($expression)
+ {
+ $this->expression = (string) $expression;
+ }
+
+ /**
+ * Gets the expression.
+ *
+ * @return string The expression
+ */
+ public function __toString()
+ {
+ return $this->expression;
+ }
+}
@@ -0,0 +1,103 @@
+<?php
+
+/*
+ * This file is part of the Symfony package.
+ *
+ * (c) Fabien Potencier <fabien@symfony.com>
+ *
+ * For the full copyright and license information, please view the LICENSE
+ * file that was distributed with this source code.
+ */
+
+namespace Symfony\Component\ExpressionLanguage;
+
+/**
+ * Allows to compile and evaluate expressions written in your own DSL.
+ *
+ * @author Fabien Potencier <fabien@symfony.com>
+ */
+class ExpressionLanguage
+{
+ private $lexer;
+ private $parser;
+ private $compiler;
+ private $cache;
+
+ protected $functions;
+
+ public function __construct()
+ {
+ $this->functions = array();
+ $this->registerFunctions();
+ }
+
+ /**
+ * Compiles an expression source code.
+ *
+ * @param string $expression The expression to compile
+ * @param array $names An array of valid names
+ *
+ * @return string The compiled PHP source code
+ */
+ public function compile($expression, $names = array())
+ {
+ return $this->getCompiler()->compile($this->parse($expression, $names))->getSource();
+ }
+
+ public function evaluate($expression, $values = array())
+ {
+ return $this->parse($expression, array_keys($values))->evaluate($this->functions, $values);
+ }
+
+ public function addFunction($name, $compiler, $evaluator)
+ {
+ $this->functions[$name] = array('compiler' => $compiler, 'evaluator' => $evaluator);
+ }
+
+ protected function registerFunctions()
+ {
+ $this->addFunction('constant', function ($constant) {
+ return sprintf('constant(%s)', $constant);
+ }, function (array $values, $constant) {
+ return constant($constant);
+ });
+ }
+
+ private function getLexer()
+ {
+ if (null === $this->lexer) {
+ $this->lexer = new Lexer();
+ }
+
+ return $this->lexer;
+ }
+
+ private function getParser()
+ {
+ if (null === $this->parser) {
+ $this->parser = new Parser($this->functions);
+ }
+
+ return $this->parser;
+ }
+
+ private function getCompiler()
+ {
+ if (null === $this->compiler) {
+ $this->compiler = new Compiler($this->functions);
+ }
+
+ return $this->compiler->reset();
+ }
+
+ private function parse($expression, $names)
+ {
+ $key = $expression.'//'.implode('-', $names);
+
+ if (!isset($this->cache[$key])) {
+ $this->cache[$key] = $this->getParser()->parse($this->getLexer()->tokenize((string) $expression), $names);
+ }
+
+ return $this->cache[$key];
+ }
+}
@@ -0,0 +1,19 @@
+Copyright (c) 2004-2013 Fabien Potencier
+
+Permission is hereby granted, free of charge, to any person obtaining a copy
+of this software and associated documentation files (the "Software"), to deal
+in the Software without restriction, including without limitation the rights
+to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+copies of the Software, and to permit persons to whom the Software is furnished
+to do so, subject to the following conditions:
+
+The above copyright notice and this permission notice shall be included in all
+copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+THE SOFTWARE.
Oops, something went wrong.

0 comments on commit 9d98fa2

Please sign in to comment.