Page MenuHomePhabricator
Paste P2046

D17819-#215382.diff
ActivePublic

Authored by richardvanvelzen on May 4 2017, 2:14 PM.
diff --git a/src/parser/xhpast/__tests__/PHPASTParserTestCase.php b/src/parser/xhpast/__tests__/PHPASTParserTestCase.php
index 5a77464..9141c2b 100644
--- a/src/parser/xhpast/__tests__/PHPASTParserTestCase.php
+++ b/src/parser/xhpast/__tests__/PHPASTParserTestCase.php
@@ -130,16 +130,6 @@ final class PHPASTParserTestCase extends PhutilTestCase {
private function newReadableAST(array $data, $source) {
$tree = new XHPASTTree($data['tree'], $data['stream'], $source);
- $out = array();
- $out[] = $this->newReadableTree($tree);
- $out[] = $this->newReadableDivider();
- $out[] = $this->newReadableStream($tree);
- $out = implode('', $out);
-
- return $out;
- }
-
- private function newReadableTree(XHPASTTree $tree) {
$root = $tree->getRootNode();
$depth = 0;
@@ -148,42 +138,6 @@ final class PHPASTParserTestCase extends PhutilTestCase {
return implode('', $list);
}
- private function newReadableDivider() {
- return str_repeat('-', 80)."\n";
- }
-
- private function newReadableStream(XHPASTTree $tree) {
- $tokens = $tree->getRawTokenStream();
-
- // Identify the longest token name this stream uses so we can format the
- // output into two columns.
- $max_len = 0;
- foreach ($tokens as $token) {
- $max_len = max($max_len, strlen($token->getTypeName()));
- }
-
- $out = array();
-
- $tokens = $tree->getRawTokenStream();
- foreach ($tokens as $token) {
- $value = $token->getValue();
- $vector = $this->getEscapedValueVector($value);
-
- $vector = array_merge(
- array(
- str_pad($token->getTypeName(), $max_len),
- ' ',
- ),
- $vector);
-
- $out[] = $this->wrapVector($vector);
- }
-
- $out = implode('', $out);
-
- return $out;
- }
-
private function newReadableTreeLines(AASTNode $node, $depth) {
$out = array();
@@ -195,22 +149,56 @@ final class PHPASTParserTestCase extends PhutilTestCase {
$out[] = str_repeat(' ', $depth).'+ '.$type_name."\n";
+ $tokens = $node->getTokens();
$children = $node->getChildren();
- if ($children) {
- foreach ($children as $child) {
- foreach ($this->newReadableTreeLines($child, $depth + 1) as $line) {
- $out[] = $line;
+ while ($child = head($children)) {
+
+ $use_tokens = array();
+ $child_tokens = array();
+ foreach ($children as $token_child) {
+ $child_tokens += $token_child->getTokens();
+ }
+
+ foreach ($tokens as $token_id => $token) {
+ if (isset($child_tokens[$token_id])) {
+ break;
+ } else {
+ $use_tokens[] = $token;
+ unset($tokens[$token_id]);
}
}
- } else {
- $value = $node->getConcreteString();
- $vector = $this->getEscapedValueVector($value);
- $out[] = $this->wrapVector($vector, $depth + 1);
+ $tokens = array_diff_key($tokens, $child->getTokens());
+
+ foreach ($this->getReadableTokens($use_tokens, $depth + 1) as $line) {
+ $out[] = $line;
+ }
+
+ foreach ($this->newReadableTreeLines($child, $depth + 1) as $line) {
+ $out[] = $line;
+ }
+
+ array_shift($children);
+ }
+
+ foreach ($this->getReadableTokens($tokens, $depth + 1) as $line) {
+ $out[] = $line;
}
return $out;
}
+ private function getReadableTokens(array $tokens, $depth) {
+ $out = array();
+ foreach ($tokens as $token_id => $token) {
+ $value = $token->getValue();
+ $vector = array_merge(
+ array($token->getTypeName(), ' '),
+ $this->getEscapedValueVector($value));
+ $out[] = $this->wrapVector($vector, $depth);
+ }
+ return $out;
+ }
+
private function getEscapedValueVector($value) {
if (!$value) {
return array('<null>');

Event Timeline

richardvanvelzen edited the content of this paste. (Show Details)May 4 2017, 2:14 PM
richardvanvelzen changed the title of this paste from untitled to D17819-#215382.diff.
richardvanvelzen updated the paste's language from autodetect to autodetect.
richardvanvelzen edited the content of this paste. (Show Details)May 4 2017, 2:40 PM
richardvanvelzen updated the paste's language from autodetect to diff.