| 1 |
<?php |
| 2 |
/** |
| 3 |
* This file is part of the Peast package |
| 4 |
* |
| 5 |
* (c) Marco Marchiò <[email protected]> |
| 6 |
* |
| 7 |
* For the full copyright and license information refer to the LICENSE file |
| 8 |
* distributed with this source code |
| 9 |
*/ |
| 10 |
namespace Peast\Syntax; |
| 11 |
|
| 12 |
/** |
| 13 |
* Comments registry class. Internal class used to manage comments |
| 14 |
* |
| 15 |
* @author Marco Marchiò <[email protected]> |
| 16 |
*/ |
| 17 |
class CommentsRegistry |
| 18 |
{ |
| 19 |
/** |
| 20 |
* Map of the indices where nodes start |
| 21 |
* |
| 22 |
* @var int |
| 23 |
*/ |
| 24 |
protected $nodesStartMap = array(); |
| 25 |
|
| 26 |
/** |
| 27 |
* Map of the indices where nodes end |
| 28 |
* |
| 29 |
* @var int |
| 30 |
*/ |
| 31 |
protected $nodesEndMap = array(); |
| 32 |
|
| 33 |
/** |
| 34 |
* Comments buffer |
| 35 |
* |
| 36 |
* @var array |
| 37 |
*/ |
| 38 |
protected $buffer = null; |
| 39 |
|
| 40 |
/** |
| 41 |
* Last token index |
| 42 |
* |
| 43 |
* @var int |
| 44 |
*/ |
| 45 |
protected $lastTokenIndex = null; |
| 46 |
|
| 47 |
/** |
| 48 |
* Comments registry |
| 49 |
* |
| 50 |
* @var array |
| 51 |
*/ |
| 52 |
protected $registry = array(); |
| 53 |
|
| 54 |
/** |
| 55 |
* Class constructor |
| 56 |
* |
| 57 |
* @param Parser $parser Parser |
| 58 |
*/ |
| 59 |
public function __construct(Parser $parser) |
| 60 |
{ |
| 61 |
$parser->getEventsEmitter() |
| 62 |
->addListener("NodeCompleted", array($this, "onNodeCompleted")) |
| 63 |
->addListener("EndParsing", array($this, "onEndParsing")); |
| 64 |
|
| 65 |
$parser->getScanner()->getEventsEmitter() |
| 66 |
->addListener("TokenConsumed", array($this, "onTokenConsumed")) |
| 67 |
->addListener("EndReached", array($this, "onTokenConsumed")) |
| 68 |
->addListener("FreezeState", array($this, "onScannerFreezeState")) |
| 69 |
->addListener("ResetState", array($this, "onScannerResetState")); |
| 70 |
} |
| 71 |
|
| 72 |
/** |
| 73 |
* Listener called every time the scanner compose the array that represents |
| 74 |
* its current state |
| 75 |
* |
| 76 |
* @param array $state State |
| 77 |
* |
| 78 |
* @return void |
| 79 |
*/ |
| 80 |
public function onScannerFreezeState(&$state) |
| 81 |
{ |
| 82 |
//Register the current last token index |
| 83 |
$state["commentsLastTokenIndex"] = $this->lastTokenIndex; |
| 84 |
} |
| 85 |
|
| 86 |
/** |
| 87 |
* Listener called every time the scanner reset its state using the given |
| 88 |
* array |
| 89 |
* |
| 90 |
* @param array $state State |
| 91 |
* |
| 92 |
* @return void |
| 93 |
*/ |
| 94 |
public function onScannerResetState(&$state) |
| 95 |
{ |
| 96 |
//Reset the last token index and delete it from the state array |
| 97 |
$this->lastTokenIndex = $state["commentsLastTokenIndex"]; |
| 98 |
unset($state["commentsLastTokenIndex"]); |
| 99 |
} |
| 100 |
|
| 101 |
/** |
| 102 |
* Listener called every time a token is consumed and when the scanner |
| 103 |
* reaches the end of the source |
| 104 |
* |
| 105 |
* @param Token|null $token Consumed token or null if the end has |
| 106 |
* been reached |
| 107 |
* |
| 108 |
* @return void |
| 109 |
*/ |
| 110 |
public function onTokenConsumed($token = null) |
| 111 |
{ |
| 112 |
//Check if it's a comment |
| 113 |
if ($token && $token->type === Token::TYPE_COMMENT) { |
| 114 |
//If there is not an open comments buffer, create it |
| 115 |
if (!$this->buffer) { |
| 116 |
$this->buffer = array( |
| 117 |
"prev" => $this->lastTokenIndex, |
| 118 |
"next" => null, |
| 119 |
"comments" => array() |
| 120 |
); |
| 121 |
} |
| 122 |
//Add the comment token to the buffer |
| 123 |
$this->buffer["comments"][] = $token; |
| 124 |
} else { |
| 125 |
|
| 126 |
if ($token) { |
| 127 |
$loc = $token->location; |
| 128 |
//Store the token end position |
| 129 |
$this->lastTokenIndex = $loc->end->getIndex(); |
| 130 |
if ($this->buffer) { |
| 131 |
//Fill the "next" key on the comments buffer with the token |
| 132 |
//start position |
| 133 |
$this->buffer["next"] = $loc->start->getIndex(); |
| 134 |
} |
| 135 |
} |
| 136 |
|
| 137 |
//If there is an open comment buffer, close it and move it to the |
| 138 |
//registry |
| 139 |
if ($buffer = $this->buffer) { |
| 140 |
//Use the location as key to add the group of comments to the |
| 141 |
//registry, in this way if comments are reprocessed they won't |
| 142 |
//be duplicated |
| 143 |
$key = implode("-", array( |
| 144 |
$buffer["prev"] !== null ? $buffer["prev"] : "", |
| 145 |
$buffer["next"] !== null ? $buffer["next"] : "" |
| 146 |
)); |
| 147 |
$this->registry[$key] = $this->buffer; |
| 148 |
$this->buffer = null; |
| 149 |
} |
| 150 |
|
| 151 |
} |
| 152 |
} |
| 153 |
|
| 154 |
/** |
| 155 |
* Listener called every time a node is completed by the parser |
| 156 |
* |
| 157 |
* @param Node\Node $node Completed node |
| 158 |
* |
| 159 |
* @return void |
| 160 |
*/ |
| 161 |
public function onNodeCompleted(Node\Node $node) |
| 162 |
{ |
| 163 |
//Every time a node is completed, register its start and end indices |
| 164 |
//in the relative properties |
| 165 |
$loc = $node->location; |
| 166 |
foreach (array("Start", "End") as $pos) { |
| 167 |
$val = $loc->{"get$pos"}()->getIndex(); |
| 168 |
$map = &$this->{"nodes{$pos}Map"}; |
| 169 |
if (!isset($map[$val])) { |
| 170 |
$map[$val] = array(); |
| 171 |
} |
| 172 |
$map[$val][] = $node; |
| 173 |
} |
| 174 |
} |
| 175 |
|
| 176 |
/** |
| 177 |
* Listener called when parsing process ends |
| 178 |
* |
| 179 |
* @return void |
| 180 |
*/ |
| 181 |
public function onEndParsing() |
| 182 |
{ |
| 183 |
//Return if there are no comments to process |
| 184 |
if ($this->registry) { |
| 185 |
|
| 186 |
//Make sure nodes start indices map is sorted |
| 187 |
ksort($this->nodesStartMap); |
| 188 |
|
| 189 |
//Loop all comment groups in the registry |
| 190 |
foreach ($this->registry as $group) { |
| 191 |
$this->findNodeForCommentsGroup($group); |
| 192 |
} |
| 193 |
} |
| 194 |
} |
| 195 |
|
| 196 |
/** |
| 197 |
* Finds the node to attach the given comments group |
| 198 |
* |
| 199 |
* @param array $group Comments group |
| 200 |
* |
| 201 |
* @return void |
| 202 |
*/ |
| 203 |
public function findNodeForCommentsGroup($group) |
| 204 |
{ |
| 205 |
$next = $group["next"]; |
| 206 |
$prev = $group["prev"]; |
| 207 |
$comments = $group["comments"]; |
| 208 |
$leading = true; |
| 209 |
|
| 210 |
//If the group of comments has a next token index that appears |
| 211 |
//in the map of start node indices, add the group to the |
| 212 |
//corresponding node's leading comments. This associates |
| 213 |
//comments that appear immediately before a node. |
| 214 |
//For example: /*comment*/ for (;;){} |
| 215 |
if (isset($this->nodesStartMap[$next])) { |
| 216 |
$nodes = $this->nodesStartMap[$next]; |
| 217 |
} |
| 218 |
//If the group of comments has a previous token index that appears |
| 219 |
//in the map of end node indices, add the group to the |
| 220 |
//corresponding node's trailing comments. This associates |
| 221 |
//comments that appear immediately after a node. |
| 222 |
//For example: for (;;){} /*comment*/ |
| 223 |
elseif (isset($this->nodesEndMap[$prev])) { |
| 224 |
$nodes = $this->nodesEndMap[$prev]; |
| 225 |
$leading = false; |
| 226 |
} |
| 227 |
//Otherwise, find a node that wraps the comments position. |
| 228 |
//This associates inner comments: |
| 229 |
//For example: for /*comment*/ (;;){} |
| 230 |
else { |
| 231 |
//Calculate comments group boundaries |
| 232 |
$start = $comments[0]->location->start->getIndex(); |
| 233 |
$end = $comments[count($comments) -1]->location->end->getIndex(); |
| 234 |
$nodes = array(); |
| 235 |
|
| 236 |
//Loop all the entries in the start index map |
| 237 |
foreach ($this->nodesStartMap as $idx => $ns) { |
| 238 |
//If the index is higher than the start index of the comments |
| 239 |
//group, stop |
| 240 |
if ($idx > $start) { |
| 241 |
break; |
| 242 |
} |
| 243 |
foreach ($ns as $node) { |
| 244 |
//Check if the comments group is inside node indices range |
| 245 |
if ($node->location->end->getIndex() >= $end) { |
| 246 |
$nodes[] = $node; |
| 247 |
} |
| 248 |
} |
| 249 |
} |
| 250 |
|
| 251 |
//If comments can't be associated with any node, associate it as |
| 252 |
//leading comments of the program, this happens when the source is |
| 253 |
//empty |
| 254 |
if (!$nodes) { |
| 255 |
$firstNode = array_values($this->nodesStartMap); |
| 256 |
$nodes = array($firstNode[0][0]); |
| 257 |
} |
| 258 |
} |
| 259 |
|
| 260 |
//If there are multiple possible nodes to associate the comments to, |
| 261 |
//find the shortest one |
| 262 |
if (count($nodes) > 1) { |
| 263 |
usort($nodes, array($this, "compareNodesLength")); |
| 264 |
} |
| 265 |
$this->associateComments($nodes[0], $comments, $leading); |
| 266 |
} |
| 267 |
|
| 268 |
/** |
| 269 |
* Compares node length |
| 270 |
* |
| 271 |
* @param Node\Node $node1 First node |
| 272 |
* @param Node\Node $node2 Second node |
| 273 |
* |
| 274 |
* @return int |
| 275 |
* |
| 276 |
* @codeCoverageIgnore |
| 277 |
*/ |
| 278 |
public function compareNodesLength($node1, $node2) |
| 279 |
{ |
| 280 |
$loc1 = $node1->location; |
| 281 |
$length1 = $loc1->end->getIndex() - $loc1->start->getIndex(); |
| 282 |
$loc2 = $node2->location; |
| 283 |
$length2 = $loc2->end->getIndex() - $loc2->start->getIndex(); |
| 284 |
//If the nodes have the same length make sure to choose nodes |
| 285 |
//different from Program nodes |
| 286 |
if ($length1 === $length2) { |
| 287 |
if ($node1 instanceof Node\Program) { |
| 288 |
$length1 += 1000; |
| 289 |
} elseif ($node2 instanceof Node\Program) { |
| 290 |
$length2 += 1000; |
| 291 |
} |
| 292 |
} |
| 293 |
return $length1 < $length2 ? -1 : 1; |
| 294 |
} |
| 295 |
|
| 296 |
/** |
| 297 |
* Adds comments to the given node |
| 298 |
* |
| 299 |
* @param Node\Node $node Node |
| 300 |
* @param array $comments Array of comments to add |
| 301 |
* @param bool $leading True to add comments as leading comments |
| 302 |
* or false to add them as trailing comments |
| 303 |
* |
| 304 |
* @return void |
| 305 |
*/ |
| 306 |
public function associateComments($node, $comments, $leading) |
| 307 |
{ |
| 308 |
$fn = ($leading ? "Leading" : "Trailing") . "Comments"; |
| 309 |
$currentComments = $node->{"get$fn"}(); |
| 310 |
foreach ($comments as $comment) { |
| 311 |
$loc = $comment->location; |
| 312 |
$commentNode = new Node\Comment; |
| 313 |
$commentNode->location->start = $loc->start; |
| 314 |
$commentNode->location->end = $loc->end; |
| 315 |
$commentNode->setRawText($comment->value); |
| 316 |
$currentComments[] = $commentNode; |
| 317 |
} |
| 318 |
$node->{"set$fn"}($currentComments); |
| 319 |
} |
| 320 |
} |