Permalink
Browse files

Revert "Revert "merged branch arnaud-lb/hash-key-expr (PR #514)""

This reverts commit 99b2c5b.
  • Loading branch information...
1 parent 941ce5b commit 591f982b47d84f7df05544eb9182743f2a6e87b7 @fabpot committed Dec 24, 2011
View
@@ -1,5 +1,6 @@
* 1.5.0
+ * allowed hash keys to be any expression
* added a do tag
* added a flush tag
* added support for dynamically named filters and functions
@@ -198,6 +198,7 @@ public function parseArrayExpression()
$stream = $this->parser->getStream();
$stream->expect(Twig_Token::PUNCTUATION_TYPE, '[', 'An array element was expected');
$elements = array();
+ $index = 0;
while (!$stream->test(Twig_Token::PUNCTUATION_TYPE, ']')) {
if (!empty($elements)) {
$stream->expect(Twig_Token::PUNCTUATION_TYPE, ',', 'An array element must be followed by a comma');
@@ -208,7 +209,12 @@ public function parseArrayExpression()
}
}
- $elements[] = $this->parseExpression();
+ $value = $this->parseExpression();
+ $key = new Twig_Node_Expression_Constant($index, $value->getLine());
+
+ array_push($elements, $key, $value);
+
+ $index += 1;
}
$stream->expect(Twig_Token::PUNCTUATION_TYPE, ']', 'An opened array is not properly closed');
@@ -230,14 +236,11 @@ public function parseHashExpression()
}
}
- if (!$stream->test(Twig_Token::STRING_TYPE) && !$stream->test(Twig_Token::NUMBER_TYPE)) {
- $current = $stream->getCurrent();
- throw new Twig_Error_Syntax(sprintf('A hash key must be a quoted string or a number (unexpected token "%s" of value "%s"', Twig_Token::typeToEnglish($current->getType(), $current->getLine()), $current->getValue()), $current->getLine());
- }
-
- $key = $stream->next()->getValue();
+ $key = $this->parseExpression();
$stream->expect(Twig_Token::PUNCTUATION_TYPE, ':', 'A hash key must be followed by a colon (:)');
- $elements[$key] = $this->parseExpression();
+ $value = $this->parseExpression();
+
+ array_push($elements, $key, $value);
}
$stream->expect(Twig_Token::PUNCTUATION_TYPE, '}', 'An opened hash is not properly closed');
@@ -15,6 +15,20 @@ public function __construct(array $elements, $lineno)
parent::__construct($elements, array(), $lineno);
}
+ public function getKeyValuePairs()
+ {
+ $pairs = array();
+
+ foreach (array_chunk($this->nodes, 2) as $pair) {
+ $pairs[] = array(
+ 'key' => $pair[0],
+ 'value' => $pair[1],
+ );
+ }
+
+ return $pairs;
+ }
+
/**
* Compiles the node to PHP.
*
@@ -24,16 +38,16 @@ public function compile(Twig_Compiler $compiler)
{
$compiler->raw('array(');
$first = true;
- foreach ($this->nodes as $name => $node) {
+ foreach ($this->getKeyValuePairs() as $pair) {
if (!$first) {
$compiler->raw(', ');
}
$first = false;
$compiler
- ->repr($name)
+ ->subcompile($pair['key'])
->raw(' => ')
- ->subcompile($node)
+ ->subcompile($pair['value'])
;
}
$compiler->raw(')');
@@ -64,7 +64,6 @@ public function getFailingTestsForArray()
{
return array(
array('{{ [1, "a": "b"] }}'),
- array('{{ {a: "b"} }}'),
array('{{ {"a": "b", 2} }}'),
);
}
@@ -74,49 +73,72 @@ public function getTestsForArray()
return array(
// simple array
array('{{ [1, 2] }}', new Twig_Node_Expression_Array(array(
+ new Twig_Node_Expression_Constant(0, 1),
+ new Twig_Node_Expression_Constant(1, 1),
+
new Twig_Node_Expression_Constant(1, 1),
new Twig_Node_Expression_Constant(2, 1),
), 1),
),
// array with trailing ,
array('{{ [1, 2, ] }}', new Twig_Node_Expression_Array(array(
+ new Twig_Node_Expression_Constant(0, 1),
+ new Twig_Node_Expression_Constant(1, 1),
+
new Twig_Node_Expression_Constant(1, 1),
new Twig_Node_Expression_Constant(2, 1),
), 1),
),
// simple hash
array('{{ {"a": "b", "b": "c"} }}', new Twig_Node_Expression_Array(array(
- 'a' => new Twig_Node_Expression_Constant('b', 1),
- 'b' => new Twig_Node_Expression_Constant('c', 1),
+ new Twig_Node_Expression_Constant('a', 1),
+ new Twig_Node_Expression_Constant('b', 1),
+
+ new Twig_Node_Expression_Constant('b', 1),
+ new Twig_Node_Expression_Constant('c', 1),
), 1),
),
// hash with trailing ,
array('{{ {"a": "b", "b": "c", } }}', new Twig_Node_Expression_Array(array(
- 'a' => new Twig_Node_Expression_Constant('b', 1),
- 'b' => new Twig_Node_Expression_Constant('c', 1),
+ new Twig_Node_Expression_Constant('a', 1),
+ new Twig_Node_Expression_Constant('b', 1),
+
+ new Twig_Node_Expression_Constant('b', 1),
+ new Twig_Node_Expression_Constant('c', 1),
), 1),
),
// hash in an array
array('{{ [1, {"a": "b", "b": "c"}] }}', new Twig_Node_Expression_Array(array(
+ new Twig_Node_Expression_Constant(0, 1),
+ new Twig_Node_Expression_Constant(1, 1),
+
new Twig_Node_Expression_Constant(1, 1),
new Twig_Node_Expression_Array(array(
- 'a' => new Twig_Node_Expression_Constant('b', 1),
- 'b' => new Twig_Node_Expression_Constant('c', 1),
+ new Twig_Node_Expression_Constant('a', 1),
+ new Twig_Node_Expression_Constant('b', 1),
+
+ new Twig_Node_Expression_Constant('b', 1),
+ new Twig_Node_Expression_Constant('c', 1),
), 1),
), 1),
),
// array in a hash
array('{{ {"a": [1, 2], "b": "c"} }}', new Twig_Node_Expression_Array(array(
- 'a' => new Twig_Node_Expression_Array(array(
+ new Twig_Node_Expression_Constant('a', 1),
+ new Twig_Node_Expression_Array(array(
+ new Twig_Node_Expression_Constant(0, 1),
+ new Twig_Node_Expression_Constant(1, 1),
+
new Twig_Node_Expression_Constant(1, 1),
new Twig_Node_Expression_Constant(2, 1),
), 1),
- 'b' => new Twig_Node_Expression_Constant('c', 1),
+ new Twig_Node_Expression_Constant('b', 1),
+ new Twig_Node_Expression_Constant('c', 1),
), 1),
),
);
@@ -27,6 +27,13 @@ Twig supports array notation
2,
]|join(',')
}}
+
+{# keys can be any expression #}
+{% set a = 1 %}
+{% set b = "foo" %}
+{% set ary = { a: 'a', b: 'b', 'c': 'c', a~b: 'd' } %}
+{{ ary|keys|join(',') }}
+{{ ary|join(',') }}
--DATA--
return array('bar' => 'bar', 'foo' => array('bar' => 'bar'))
--EXPECT--
@@ -43,3 +50,6 @@ bar
FOO,BAR,
1,2
+
+1,foo,c,1foo
+a,b,c,d
@@ -18,10 +18,10 @@ class Twig_Tests_Node_Expression_ArrayTest extends Twig_Tests_Node_TestCase
*/
public function testConstructor()
{
- $elements = array('foo' => $foo = new Twig_Node_Expression_Constant('bar', 0));
+ $elements = array(new Twig_Node_Expression_Constant('foo', 0), $foo = new Twig_Node_Expression_Constant('bar', 0));
$node = new Twig_Node_Expression_Array($elements, 0);
- $this->assertEquals($foo, $node->getNode('foo'));
+ $this->assertEquals($foo, $node->getNode(1));
}
/**
@@ -36,8 +36,11 @@ public function testCompile($node, $source, $environment = null)
public function getTests()
{
$elements = array(
- 'foo' => new Twig_Node_Expression_Constant('bar', 0),
- 'bar' => new Twig_Node_Expression_Constant('foo', 0),
+ new Twig_Node_Expression_Constant('foo', 0),
+ new Twig_Node_Expression_Constant('bar', 0),
+
+ new Twig_Node_Expression_Constant('bar', 0),
+ new Twig_Node_Expression_Constant('foo', 0),
);
$node = new Twig_Node_Expression_Array($elements, 0);
@@ -25,7 +25,7 @@ public function testConstructor()
$this->assertEquals($expr, $node->getNode('expr'));
$this->assertFalse($node->getAttribute('only'));
- $vars = new Twig_Node_Expression_Array(array('foo' => new Twig_Node_Expression_Constant(true, 0)), 0);
+ $vars = new Twig_Node_Expression_Array(array(new Twig_Node_Expression_Constant('foo', 0), new Twig_Node_Expression_Constant(true, 0)), 0);
$node = new Twig_Node_Include($expr, $vars, true, false, 0);
$this->assertEquals($vars, $node->getNode('variables'));
$this->assertTrue($node->getAttribute('only'));
@@ -62,7 +62,7 @@ public function getTests()
);
$expr = new Twig_Node_Expression_Constant('foo.twig', 0);
- $vars = new Twig_Node_Expression_Array(array('foo' => new Twig_Node_Expression_Constant(true, 0)), 0);
+ $vars = new Twig_Node_Expression_Array(array(new Twig_Node_Expression_Constant('foo', 0), new Twig_Node_Expression_Constant(true, 0)), 0);
$node = new Twig_Node_Include($expr, $vars, false, false, 0);
$tests[] = array($node, '$this->env->loadTemplate("foo.twig")->display(array_merge($context, array("foo" => true)));');

0 comments on commit 591f982

Please sign in to comment.