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

Skip to content

Commit 201264d

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

16 files changed

+105
-98
lines changed

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

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,12 +27,14 @@ public function compile(Compiler $compiler)
2727

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

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

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

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

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -62,30 +62,30 @@ public function dump()
6262
{
6363
$array = array();
6464
foreach ($this->getKeyValuePairs() as $pair) {
65-
$array[$pair['key']->attributes['value']] = $pair['value']->dump();
65+
$array[$pair['key']->attributes['value']] = $pair['value'];
6666
}
6767

68+
$tokens = array();
69+
6870
if ($this->isHash($array)) {
69-
$str = '{';
70-
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-
}
71+
foreach ($array as $k => $v) {
72+
$tokens[] = ', ';
73+
$tokens[] = new ConstantNode($k);
74+
$tokens[] = ': ';
75+
$tokens[] = $v;
7776
}
78-
79-
return rtrim($str, ', ').'}';
80-
}
81-
82-
$str = '[';
83-
84-
foreach ($array as $key => $value) {
85-
$str .= sprintf('%s, ', $value);
77+
$tokens[0] = '{';
78+
$tokens[] = '}';
79+
} else {
80+
foreach ($array as $v) {
81+
$tokens[] = ', ';
82+
$tokens[] = $v;
83+
}
84+
$tokens[0] = '[';
85+
$tokens[] = ']';
8686
}
8787

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

9191
protected function getKeyValuePairs()

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,6 @@ public function evaluate($functions, $values)
157157

158158
public function dump()
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: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,6 @@ public function evaluate($functions, $values)
5151

5252
public function dump()
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: 31 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -40,49 +40,37 @@ public function evaluate($functions, $values)
4040

4141
public function dump()
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-
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+
$tokens = array();
44+
$value = $this->attributes['value'];
45+
46+
if (true === $value) {
47+
$tokens[] = 'true';
48+
} elseif (false === $value) {
49+
$tokens[] = 'false';
50+
} elseif (null === $value) {
51+
$tokens[] = 'null';
52+
} elseif (is_numeric($value)) {
53+
$tokens[] = $value;
54+
} elseif (!is_array($value)) {
55+
$tokens[] = $this->dumpString($value);
56+
} elseif ($this->isHash($value)) {
57+
foreach ($value as $k => $v) {
58+
$tokens[] = ', ';
59+
$tokens[] = new self($k);
60+
$tokens[] = ': ';
61+
$tokens[] = new self($v);
62+
}
63+
$tokens[0] = '{';
64+
$tokens[] = '}';
65+
} else {
66+
foreach ($value as $v) {
67+
$tokens[] = ', ';
68+
$tokens[] = new self($v);
69+
}
70+
$tokens[0] = '[';
71+
$tokens[] = ']';
8672
}
73+
74+
return $tokens;
8775
}
8876
}

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

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -52,14 +52,16 @@ public function evaluate($functions, $values)
5252

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

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

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

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

Lines changed: 7 additions & 7 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);
@@ -99,13 +99,13 @@ public function dump()
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: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line 10000 change
@@ -40,6 +40,6 @@ public function evaluate($functions, $values)
4040

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

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -81,9 +81,9 @@ public function dump()
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: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,6 @@ public function evaluate($functions, $values)
6161

6262
public function dump()
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+
protected function dumpNode(Node $node)
49+
{
50+
$dump = '';
51+
52+
foreach($node->dump() as $token) {
53+
$dump .= is_scalar($token) ? $token : $this->dumpNode($token);
54+
}
55+
56+
return $dump;
4657
}
4758
}

src/Symfony/Component/ExpressionLanguage/Parser.php

< 741A button class="Button Button--iconOnly Button--invisible flex-shrink-0 js-expand-all-difflines-button" aria-label="Expand all lines: src/Symfony/Component/ExpressionLanguage/Parser.php" data-file-path="src/Symfony/Component/ExpressionLanguage/Parser.php" aria-describedby=":Rhmpmlab:">
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
}

src/Symfony/Component/ExpressionLanguage/Tests/ParserTest.php

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -98,24 +98,24 @@ public function getParseData()
9898
'(3 - 3) * 2',
9999
),
100100
array(
101-
new Node\GetAttrNode(new Node\NameNode('foo'), new Node\ConstantNode('bar'), new Node\ArgumentsNode(), Node\GetAttrNode::PROPERTY_CALL),
101+
new Node\GetAttrNode(new Node\NameNode('foo'), new Node\NameNode('bar'), new Node\ArgumentsNode(), Node\GetAttrNode::PROPERTY_CALL),
102102
'foo.bar',
103103
array('foo'),
104104
),
105105
array(
106-
new Node\GetAttrNode(new Node\NameNode('foo'), new Node\ConstantNode('bar'), new Node\ArgumentsNode(), Node\GetAttrNode::METHOD_CALL),
106+
new Node\GetAttrNode(new Node\NameNode('foo'), new Node\NameNode('bar'), new Node\ArgumentsNode(), Node\GetAttrNode::METHOD_CALL),
107107
'foo.bar()',
108108
array('foo'),
109109
),
110110
array(
111-
new Node\GetAttrNode(new Node\NameNode('foo'), new Node\ConstantNode('not'), new Node\ArgumentsNode(), Node\GetAttrNode::METHOD_CALL),
111+
new Node\GetAttrNode(new Node\NameNode('foo'), new Node\NameNode('not'), new Node\ArgumentsNode(), Node\GetAttrNode::METHOD_CALL),
112112
'foo.not()',
113113
array('foo'),
114114
),
115115
array(
116116
new Node\GetAttrNode(
117117
new Node\NameNode('foo'),
118-
new Node\ConstantNode('bar'),
118+
new Node\NameNode('bar'),
119119
$arguments,
120120
Node\GetAttrNode::METHOD_CALL
121121
),
@@ -159,7 +159,9 @@ public function getParseData()
159159

160160
private function createGetAttrNode($node, $item, $type)
161161
{
162-
return new Node\GetAttrNode($node, new Node\ConstantNode($item), new Node\ArgumentsNode(), $type);
162+
$attr = Node\GetAttrNode::ARRAY_CALL === $type ? new Node\ConstantNode($item) : new Node\NameNode($item);
163+
164+
return new Node\GetAttrNode($node, $attr, new Node\ArgumentsNode(), $type);
163165
}
164166

165167
/**

0 commit comments

Comments
 (0)
0