10000 [ExpressionLanguage] Add a way to hook on each node when dumping the AST · symfony/symfony@66d23db · GitHub
[go: up one dir, main page]

Skip to content

Commit 66d23db

Browse files
[ExpressionLanguage] Add a way to hook on each node when dumping the AST
1 parent f400f01 commit 66d23db

16 files changed

+116
-109
lines changed

src/Symfony/Component/ExpressionLanguage/Node/ArgumentsNode.php

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,14 +25,16 @@ public function compile(Compiler $compiler)
2525
$this->compileArguments($compiler, false);
2626
}
2727

28-
public function dump()
28+
public function toArray()
2929
{
30-
$str = '';
30+
$array = array();
3131

3232
foreach ($this->getKeyValuePairs() as $pair) {
33-
$str .= sprintf('%s, ', $pair['value']->dump());
33+
$array[] = $pair['value'];
34+
$array[] = ', ';
3435
}
36+
array_pop($array);
3537

36-
return rtrim($str, ', ');
38+
return $array;
3739
}
3840
}

src/Symfony/Component/ExpressionLanguage/Node/ArrayNode.php

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -58,34 +58,34 @@ public function evaluate($functions, $values)
5858
return $result;
5959
}
6060

61-
public function dump()
61+
public function toArray()
6262
{
63-
$array = array();
63+
$value = array();
6464
foreach ($this->getKeyValuePairs() as $pair) {
65-
$array[$pair['key']->attributes['value']] = $pair['value']->dump();
65+
$value[$pair['key']->attributes['value']] = $pair['value'];
6666
}
6767

68-
if ($this->isHash($array)) {
69-
$str = '{';
68+
$array = array();
7069

71-
foreach ($array as $key => $value) {
72-
if (is_int($key)) {
73-
$str .= sprintf('%s: %s, ', $key, $value);
74-
} else {
75-
$str .= sprintf('"%s": %s, ', $this->dumpEscaped($key), $value);
76-
}
70+
if ($this->isHash($value)) {
71+
foreach ($value as $k => $v) {
72+
$array[] = ', ';
73+
$array[] = new ConstantNode($k);
74+
$array[] = ': ';
75+
$array[] = $v;
7776
}
78-
79-
return rtrim($str, ', ').'}';
80-
}
81-
82-
$str = '[';
83-
84-
foreach ($array as $key => $value) {
85-
$str .= sprintf('%s, ', $value);
77+
$array[0] = '{';
78+
$array[] = '}';
79+
} else {
80+
foreach ($value as $v) {
81+
$array[] = ', ';
82+
$array[] = $v;
83+
}
84+
$array[0] = '[';
85+
$array[] = ']';
8686
}
8787

88-
return rtrim($str, ', ').']';
88+
return $array;
8989
}
9090

9191
protected function getKeyValuePairs()

src/Symfony/Component/ExpressionLanguage/Node/BinaryNode.php

-2Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -155,8 +155,8 @@ public function evaluate($functions, $values)
155155
}
156156
}
157157

158-
public function dump()
158+
public function toArray()
159159
{
160-
return sprintf('(%s %s %s)', $this->nodes['left']->dump(), $this->attributes['operator'], $this->nodes['right']->dump());
160+
return array('(', $this->nodes['left'], ' '.$this->attributes['operator'].' ', $this->nodes['right'], ')');
161161
}
162162
}

src/Symfony/Component/ExpressionLanguage/Node/ConditionalNode.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,8 +49,8 @@ public function evaluate($functions, $values)
4949
return $this->nodes['expr3']->evaluate($functions, $values);
5050
}
5151

52-
public function dump()
52+
public function toArray()
5353
{
54-
return sprintf('(%s ? %s : %s)', $this->nodes['expr1']->dump(), $this->nodes['expr2']->dump(), $this->nodes['expr3']->dump());
54+
return array('(', $this->nodes['expr1'], ' ? ', $this->nodes['expr2'], ' : ', $this->nodes['expr3'], ')');
5555
}
5656
}

src/Symfony/Component/ExpressionLanguage/Node/ConstantNode.php

Lines changed: 32 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -38,51 +38,39 @@ public function evaluate($functions, $values)
3838
return $this->attributes['value'];
3939
}
4040

41-
public function dump()
41+
public function toArray()
4242
{
43-
return $this->dumpValue($this->attributes['value']);
44-
}
45-
46-
private function dumpValue($value)
47-
{
48-
switch (true) {
49-
case true === $value:
50-
return 'true';
51-
52-
case false === $value:
53-
return 'false';
54-
55-
case null === $value:
56-
return 'null';
57-
58-
case is_numeric($value):
59-
return $value;
60-
61-
case is_array($value):
62-
if ($this->isHash($value)) {
63-
$str = '{';
64-
65-
foreach ($value as $key => $v) {
66-
if (is_int($key)) {
67-
$str .= sprintf('%s: %s, ', $key, $this->dumpValue($v));
68-
} else {
69-
$str .= sprintf('"%s": %s, ', $this->dumpEscaped($key), $this->dumpValue($v));
70-
}
71-
}
72-
73-
return rtrim($str, ', ').'}';
74-
}
75-
76-
$str = '[';
77-
78< 10000 /td>-
foreach ($value as $key => $v) {
79-
$str .= sprintf('%s, ', $this->dumpValue($v));
80-
}
81-
82-
return rtrim($str, ', ').']';
83-
84-
default:
85-
return sprintf('"%s"', $this->dumpEscaped($value));
43+
$array = array();
44+
$value = $this->attributes['value'];
45+
46+
if (true === $value) {
47+
$array[] = 'true';
48+
} elseif (false === $value) {
49+
$array[] = 'false';
50+
} elseif (null === $value) {
51+
$array[] = 'null';
52+
} elseif (is_numeric($value)) {
53+
$array[] = $value;
54+
} elseif (!is_array($value)) {
55+
$array[] = $this->dumpString($value);
56+
} elseif ($this->isHash($value)) {
57+
foreach ($value as $k => $v) {
58+
$array[] = ', ';
59+
$array[] = new self($k);
60+
$array[] = ': ';
61+
$array[] = new self($v);
62+
}
63+
$array[0] = '{';
64+
$array[] = '}';
65+
} else {
66+
foreach ($value as $v) {
67+
$array[] = ', ';
68+
$array[] = new self($v);
69+
}
70+
$array[0] = '[';
71+
$array[] = ']';
8672
}
73+
74+
return $array;
8775
}
8876
}

src/Symfony/Component/ExpressionLanguage/Node/FunctionNode.php

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -50,16 +50,18 @@ public function evaluate($functions, $values)
5050
return call_user_func_array($functions[$this->attributes['name']]['evaluator'], $arguments);
5151
}
5252

53-
public function dump()
53+
public function toArray()
5454
{
55-
$str = $this->attributes['name'];
56-
57-
$str .= '(';
55+
$array = array();
56+
$array[] = $this->attributes['name'];
5857

5958
foreach ($this->nodes['arguments']->nodes as $node) {
60-
$str .= $node->dump().', ';
59+
$array[] = ', ';
60+
$array[] = $node;
6161
}
62+
$array[1] = '(';
63+
$array[] = ')';
6264

63-
return rtrim($str, ', ').')';
65+
return $array;
6466
}
6567
}

src/Symfony/Component/ExpressionLanguage/Node/GetAttrNode.php

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -39,15 +39,15 @@ public function compile(Compiler $compiler)
3939
$compiler
4040
->compile($this->nodes['node'])
4141
->raw('->')
42-
->raw($this->nodes['attribute']->attributes['value'])
42+
->raw($this->nodes['attribute']->attributes['name'])
4343
;
4444
break;
4545

4646
case self::METHOD_CALL:
4747
$compiler
4848
->compile($this->nodes['node'])
4949
->raw('->')
50-
->raw($this->nodes['attribute']->attributes['value'])
50+
->raw($this->nodes['attribute']->attributes['name'])
5151
->raw('(')
5252
->compile($this->nodes['arguments'])
5353
->raw(')')
@@ -73,7 +73,7 @@ public function evaluate($functions, $values)
7373
throw new \RuntimeException('Unable to get a property on a non-object.');
7474
}
7575

76-
$property = $this->nodes['attribute']->attributes['value'];
76+
$property = $this->nodes['attribute']->attributes['name'];
7777

7878
return $obj->$property;
7979

@@ -83,7 +83,7 @@ public function evaluate($functions, $values)
8383
throw new \RuntimeException('Unable to get a property on a non-object.');
8484
}
8585

86-
return call_user_func_array(array($obj, $this->nodes['attribute']->attributes['value']), $this->nodes['arguments']->evaluate($functions, $values));
86+
return call_user_func_array(array($obj, $this->nodes['attribute']->attributes['name']), $this->nodes['arguments']->evaluate($functions, $values));
8787

8888
case self::ARRAY_CALL:
8989
$array = $this->nodes['node']->evaluate($functions, $values);
@@ -95,17 +95,17 @@ public function evaluate($functions, $values)
9595
}
9696
}
9797

98-
public function dump()
98+
public function toArray()
9999
{
100100
switch ($this->attributes['type']) {
101101
case self::PROPERTY_CALL:
102-
return sprintf('%s.%s', $this->nodes['node']->dump(), trim($this->nodes['attribute']->dump(), '"'));
102+
return array($this->nodes['node'], '.', $this->nodes['attribute']);
103103

104104
case self::METHOD_CALL:
105-
return sprintf('%s.%s(%s)', $this->nodes['node']->dump(), trim($this->nodes['attribute']->dump(), '"'), $this->nodes['arguments']->dump());
105+
return array($this->nodes['node'], '.', $this->nodes['attribute'], '(', $this->nodes['arguments'], ')');
106106

107107
case self::ARRAY_CALL:
108-
return sprintf('%s[%s]', $this->nodes['node']->dump(), $this->nodes['attribute']->dump());
108+
return array($this->nodes['node'], '[', $this->nodes['attribute'], ']');
109109
}
110110
}
111111
}

src/Symfony/Component/ExpressionLanguage/Node/NameNode.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,8 @@ public function evaluate($functions, $values)
3838
return $values[$this->attributes['name']];
3939
}
4040

41-
public function dump()
41+
public function toArray()
4242
{
43-
return $this->attributes['name'];
43+
return array($this->attributes['name']);
4444
}
4545
}

src/Symfony/Component/ExpressionLanguage/Node/Node.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -76,14 +76,14 @@ public function evaluate($functions, $values)
7676
return $results;
7777
}
7878

79-
public function dump()
79+
public function toArray()
8080
{
8181
throw new \BadMethodCallException(sprintf('Dumping a "%s" instance is not supported yet.', get_class($this)));
8282
}
8383

84-
protected function dumpEscaped($value)
84+
protected function dumpString($value)
8585
{
86-
return str_replace(array('\\', '"'), array('\\\\', '\"'), $value);
86+
return sprintf('"%s"', addcslashes($value, "\0\t\"\\"));
8787
}
8888

8989
protected function isHash(array $value)

src/Symfony/Component/ExpressionLanguage/Node/UnaryNode.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,8 +59,8 @@ public function evaluate($functions, $values)
5959
return $value;
6060
}
6161

62-
public function dump()
62+
public function toArray()
6363
{
64-
return sprintf('(%s %s)', $this->attributes['operator'], $this->nodes['node']->dump());
64+
return array('(', $this->attributes['operator'].' ', $this->nodes['node'], ')');
6565
}
6666
}

src/Symfony/Component/ExpressionLanguage/ParsedExpression.php

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,17 @@ public function getNodes()
4242

4343
public function dump()
4444
{
45-
return $this->nodes->dump();
45+
return $this->dumpNode($this->nodes);
46+
}
47+
48+
private function dumpNode(Node $node)
49+
{
50+
$dump = '';
51+
52+
foreach($node->toArray() as $v) {
53+
$dump .= is_scalar($v) ? $v : $this->dumpNode($v);
54+
}
55+
56+
return $dump;
4657
}
4758
}

src/Symfony/Component/ExpressionLanguage/Parser.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -330,7 +330,7 @@ public function parsePostfixExpression($node)
330330
throw new SyntaxError('Expected name', $token->cursor);
331331
}
332332

333-
$arg = new Node\ConstantNode($token->value);
333+
$arg = new Node\NameNode($token->value);
334334

335335
$arguments = new Node\ArgumentsNode();
336336
if ($this->stream->current->test(Token::PUNCTUATION_TYPE, '(')) {

src/Symfony/Component/ExpressionLanguage/Tests/Node/AbstractNodeTest.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
namespace Symfony\Component\ExpressionLanguage\Tests\Node;
1313

1414
use Symfony\Component\ExpressionLanguage\Compiler;
15+
use Symfony\Component\ExpressionLanguage\ParsedExpression;
1516

1617
abstract class AbstractNodeTest extends \PHPUnit_Framework_TestCase
1718
{
@@ -42,7 +43,8 @@ abstract public function getCompileData();
4243
*/
4344
public function testDump($expected, $node)
4445
{
45-
$this->assertSame($expected, $node->dump());
46+
$expr = new ParsedExpression($expected, $node);
47+
$this->assertSame($expected, $expr->dump());
4648
}
4749

4850
abstract public function getDumpData();

src/Symfony/Component/ExpressionLanguage/Tests/Node/ConstantNodeTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,8 +47,8 @@ public function getDumpData()
4747
array('false', new ConstantNode(false)),
4848
array('true', new ConstantNode(true)),
4949
array('null', new ConstantNode(null)),
50-
array(3, new ConstantNode(3)),
51-
array(3.3, new ConstantNode(3.3)),
50+
array('3', new ConstantNode(3)),
51+
array('3.3', new ConstantNode(3.3)),
5252
array('"foo"', new ConstantNode('foo')),
5353
array('{0: 1, "b": "a", 1: true}', new ConstantNode(array(1, 'b' => 'a', true))),
5454
array('{"a\\"b": "c", "a\\\\b": "d"}', new ConstantNode(array('a"b' => 'c', 'a\\b' => 'd'))),

src/Symfony/Component/ExpressionLanguage/Tests/Node/GetAttrNodeTest.php

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,9 @@ public function getEvaluateData()
2424
array('b', new GetAttrNode(new NameNode('foo'), new ConstantNode(0), $this->getArrayNode(), GetAttrNode::ARRAY_CALL), array('foo' => array('b' => 'a', 'b'))),
2525
array('a', new GetAttrNode(new NameNode('foo'), new ConstantNode('b'), $this->getArrayNode(), GetAttrNode::ARRAY_CALL), array('foo' => array('b' => 'a', 'b'))),
2626

27-
array('bar', new GetAttrNode(new NameNode('foo'), new ConstantNode('foo'), $this->getArrayNode(), GetAttrNode::PROPERTY_CALL), array('foo' => new Obj())),
27+
array('bar', new GetAttrNode(new NameNode('foo'), new NameNode('foo'), $this->getArrayNode(), GetAttrNode::PROPERTY_CALL), array('foo' => new Obj())),
2828

29-
array('baz', new GetAttrNode(new NameNode('foo'), new ConstantNode('foo'), $this->getArrayNode(), GetAttrNode::METHOD_CALL), array('foo' => new Obj())),
29+
array('baz', new GetAttrNode(new NameNode('foo'), new NameNode('foo'), $this->getArrayNode(), GetAttrNode::METHOD_CALL), array('foo' => new Obj())),
3030
array('a', new GetAttrNode(new NameNode('foo'), new NameNode('index'), $this->getArrayNode(), GetAttrNode::ARRAY_CALL), array('foo' => array('b' => 'a', 'b'), 'index' => 'b')),
3131
);
3232
}
@@ -37,9 +37,9 @@ public function getCompileData()
3737
array('$foo[0]', new GetAttrNode(new NameNode('foo'), new ConstantNode(0), $this->getArrayNode(), GetAttrNode::ARRAY_CALL)),
3838
array('$foo["b"]', new GetAttrNode(new NameNode('foo'), new ConstantNode('b'), $this->getArrayNode(), GetAttrNode::ARRAY_CALL)),
3939

40-
array('$foo->foo', new GetAttrNode(new NameNode('foo'), new ConstantNode('foo'), $this->getArrayNode(), GetAttrNode::PROPERTY_CALL), array('foo' => new Obj())),
40+
array('$foo->foo', new GetAttrNode(new NameNode('foo'), new NameNode('foo'), $this->getArrayNode(), GetAttrNode::PROPERTY_CALL), array('foo' => new Obj())),
4141

42-
array('$foo->foo(array("b" => "a", 0 => "b"))', new GetAttrNode(new NameNode('foo'), new ConstantNode('foo'), $this->getArrayNode(), GetAttrNode::METHOD_CALL), array('foo' => new Obj())),
42+
array('$foo->foo(array("b" => "a", 0 => "b"))', new GetAttrNode(new NameNode('foo'), new NameNode('foo'), $this->getArrayNode(), GetAttrNode::METHOD_CALL), array('foo' => new Obj())),
4343
array('$foo[$index]', new GetAttrNode(new NameNode('foo'), new NameNode('index'), $this->getArrayNode(), GetAttrNode::ARRAY_CALL)),
4444
);
4545
}
@@ -50,9 +50,9 @@ public function getDumpData()
5050
array('foo[0]', new GetAttrNode(new NameNode('foo'), new ConstantNode(0), $this->getArrayNode(), GetAttrNode::ARRAY_CALL)),
5151
array('foo["b"]', new GetAttrNode(new NameNode('foo'), new ConstantNode('b'), $this->getArrayNode(), GetAttrNode::ARRAY_CALL)),
5252

53-
array('foo.foo', new GetAttrNode(new NameNode('foo'), new ConstantNode('foo'), $this->getArrayNode(), GetAttrNode::PROPERTY_CALL), array('foo' => new Obj())),
53+
array('foo.foo', new GetAttrNode(new NameNode('foo'), new NameNode('foo'), $this->getArrayNode(), GetAttrNode::PROPERTY_CALL), array('foo' => new Obj())),
5454

55-
array('foo.foo({"b": "a", 0: "b"})', new GetAttrNode(new NameNode('foo'), new ConstantNode('foo'), $this->getArrayNode(), GetAttrNode::METHOD_CALL), array('foo' => new Obj())),
55+
array('foo.foo({"b": "a", 0: "b"})', new GetAttrNode(new NameNode('foo'), new NameNode('foo'), $this->getArrayNode(), GetAttrNode::METHOD_CALL), array('foo' => new Obj())),
5656
array('foo[index]', new GetAttrNode(new NameNode('foo'), new NameNode('index'), $this->getArrayNode(), GetAttrNode::ARRAY_CALL)),
5757
);
5858
}

0 commit comments

Comments
 (0)
0