mirror of
https://github.com/danog/PHP-Parser.git
synced 2024-11-26 20:04:48 +01:00
Decouple NodeDumper from NodeAbstract
This commit is contained in:
parent
22ea3d6a70
commit
8a3074db38
7
README
7
README
@ -17,8 +17,7 @@ Usage:
|
||||
// the return value of Parser->yyparse will either be false (which
|
||||
// signifies that an error occured) or an array of statements (Nodes)
|
||||
if (false !== $stmts) {
|
||||
// dump the AST
|
||||
foreach ($stmts as $stmt) {
|
||||
echo $stmt, "\n";
|
||||
}
|
||||
// dump nodes
|
||||
$nodeDumper = new NodeDumper();
|
||||
echo $nodeDumper->dump($stmts);
|
||||
}
|
@ -25,39 +25,4 @@ abstract class NodeAbstract implements IteratorAggregate
|
||||
public function getIterator() {
|
||||
return new ArrayIterator($this->subNodes);
|
||||
}
|
||||
|
||||
public function __toString() {
|
||||
$r = '[' . $this->getType() . ']';
|
||||
|
||||
foreach ($this->subNodes as $key => $value) {
|
||||
$r .= "\n" . ' ' . $key . ': ';
|
||||
|
||||
if (null === $value) {
|
||||
$r .= 'null';
|
||||
} elseif (false === $value) {
|
||||
$r .= 'false';
|
||||
} elseif (true === $value) {
|
||||
$r .= 'true';
|
||||
} elseif (is_scalar($value)) {
|
||||
$r .= $value;
|
||||
} elseif ($value instanceof NodeAbstract) {
|
||||
$lines = explode("\n", $value);
|
||||
$r .= array_shift($lines);
|
||||
foreach ($lines as $line) {
|
||||
$r .= "\n" . ' ' . $line;
|
||||
}
|
||||
} elseif (is_array($value)) {
|
||||
foreach ($value as $v) {
|
||||
$lines = explode("\n", $v);
|
||||
foreach ($lines as $line) {
|
||||
$r .= "\n" . ' ' . $line;
|
||||
}
|
||||
}
|
||||
} else {
|
||||
die('UNEXPECTED!!!');
|
||||
}
|
||||
}
|
||||
|
||||
return $r;
|
||||
}
|
||||
}
|
43
lib/NodeDumper.php
Normal file
43
lib/NodeDumper.php
Normal file
@ -0,0 +1,43 @@
|
||||
<?php
|
||||
|
||||
class NodeDumper
|
||||
{
|
||||
/**
|
||||
* Dumps a Node, scalar or array to a string.
|
||||
*
|
||||
* @param mixed $node Value to dump
|
||||
* @return string Dumped value
|
||||
*/
|
||||
public function dump($node) {
|
||||
if (is_array($node) || $node instanceof NodeAbstract) {
|
||||
if (is_array($node)) {
|
||||
$r = 'array(';
|
||||
} else {
|
||||
$r = $node->getType() . '(';
|
||||
}
|
||||
|
||||
foreach ($node as $key => $value) {
|
||||
$r .= "\n" . ' ' . $key . ': ';
|
||||
|
||||
$lines = explode("\n", $this->dump($value));
|
||||
|
||||
$r .= array_shift($lines);
|
||||
foreach ($lines as $line) {
|
||||
$r .= "\n" . ' ' . $line;
|
||||
}
|
||||
}
|
||||
|
||||
return $r . "\n" . ')';
|
||||
} elseif (null === $node) {
|
||||
return 'null';
|
||||
} elseif (false === $node) {
|
||||
return 'false';
|
||||
} elseif (true === $node) {
|
||||
return 'true';
|
||||
} elseif (is_scalar($node)) {
|
||||
return $node;
|
||||
} else {
|
||||
throw new InvalidArgumentException('Unexpected node type.');
|
||||
}
|
||||
}
|
||||
}
|
@ -632,10 +632,16 @@ class PrettyPrinter_Zend extends PrettyPrinterAbstract
|
||||
|
||||
public function pEncapsList(array $encapsList) {
|
||||
$return = '';
|
||||
foreach ($encapsList as $element) {
|
||||
foreach ($encapsList as $i => $element) {
|
||||
if (is_string($element)) {
|
||||
$return .= addcslashes($element, "\n\r\t\f\v$\"\\");
|
||||
} elseif ($element instanceof Node_Variable && is_string($element->name)) {
|
||||
} elseif ($element instanceof Node_Variable
|
||||
&& is_string($element->name)
|
||||
&& (!isset($encapsList[$i + 1])
|
||||
|| !is_string($encapsList[$i + 1])
|
||||
|| '[' !== $encapsList[$i + 1][0]
|
||||
)
|
||||
) {
|
||||
$return .= '$' . $element->name;
|
||||
} else {
|
||||
$return .= '{' . $this->p($element) . '}';
|
||||
|
9
test.php
9
test.php
@ -6,7 +6,9 @@ function __autoload($class) {
|
||||
|
||||
echo '<pre>';
|
||||
|
||||
$parser = new Parser();
|
||||
$parser = new Parser;
|
||||
$prettyPrinter = new PrettyPrinter_Zend;
|
||||
$nodeDumper = new NodeDumper;
|
||||
|
||||
// Output Demo
|
||||
$stmts = $parser->yyparse(new Lexer(
|
||||
@ -24,14 +26,11 @@ $stmts = $parser->yyparse(new Lexer(
|
||||
);
|
||||
|
||||
if (false !== $stmts) {
|
||||
foreach ($stmts as $stmt) {
|
||||
echo htmlspecialchars($stmt), "\n";
|
||||
}
|
||||
echo htmlspecialchars($nodeDumper->dump($stmts));
|
||||
}
|
||||
|
||||
echo "\n\n";
|
||||
|
||||
$prettyPrinter = new PrettyPrinter_Zend;
|
||||
$code = $prettyPrinter->pStmts(
|
||||
$parser->yyparse(
|
||||
new Lexer(file_get_contents(
|
||||
|
@ -6,9 +6,9 @@ function __autoload($class) {
|
||||
is_file($file = '../lib/' . strtr($class, '_', '/') . '.php') && require_once $file;
|
||||
}
|
||||
|
||||
$parser = new Parser();
|
||||
|
||||
$parser = new Parser;
|
||||
$prettyPrinter = new PrettyPrinter_Zend;
|
||||
$nodeDumper = new NodeDumper;
|
||||
|
||||
echo '<!DOCTYPE html>
|
||||
<style>
|
||||
@ -86,7 +86,7 @@ foreach (new RecursiveIteratorIterator(
|
||||
++$ppCount;
|
||||
if (false !== $ppStmts) {
|
||||
++$compareCount;
|
||||
if ($stmts == $ppStmts) {
|
||||
if ($nodeDumper->dump($stmts) == $nodeDumper->dump($ppStmts)) {
|
||||
echo '
|
||||
<td class="pass">PASS</td>
|
||||
<td>' . $time . 's</td>
|
||||
|
Loading…
Reference in New Issue
Block a user