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 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
}

0 commit comments

Comments
 (0)