| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
package com.google.debugging.sourcemap; |
| 18 |
|
| 19 |
import com.google.common.annotations.VisibleForTesting; |
| 20 |
import com.google.common.base.Preconditions; |
| 21 |
import com.google.common.collect.Lists; |
| 22 |
import com.google.common.collect.Maps; |
| 23 |
|
| 24 |
import java.io.IOException; |
| 25 |
import java.util.ArrayDeque; |
| 26 |
import java.util.Deque; |
| 27 |
import java.util.LinkedHashMap; |
| 28 |
import java.util.List; |
| 29 |
import java.util.Map; |
| 30 |
import java.util.Map.Entry; |
| 31 |
|
| 32 |
import javax.annotation.Nullable; |
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
public class SourceMapGeneratorV2 implements SourceMapGenerator { |
| 40 |
|
| 41 |
private boolean validate = false; |
| 42 |
|
| 43 |
private static final int UNMAPPED = -1; |
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
private List<Mapping> mappings = Lists.newArrayList(); |
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
private LinkedHashMap<String, Integer> sourceFileMap = |
| 54 |
Maps.newLinkedHashMap(); |
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
private LinkedHashMap<String, Integer> originalNameMap = |
| 60 |
Maps.newLinkedHashMap(); |
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
private String lastSourceFile = null; |
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
private int lastSourceFileIndex = -1; |
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
private Mapping lastMapping; |
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
private FilePosition offsetPosition = new FilePosition(0, 0); |
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
private FilePosition prefixPosition = new FilePosition(0, 0); |
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
@Override |
| 94 |
public void reset() { |
| 95 |
mappings.clear(); |
| 96 |
lastMapping = null; |
| 97 |
sourceFileMap.clear(); |
| 98 |
originalNameMap.clear(); |
| 99 |
lastSourceFile = null; |
| 100 |
lastSourceFileIndex = -1; |
| 101 |
offsetPosition = new FilePosition(0, 0); |
| 102 |
prefixPosition = new FilePosition(0, 0); |
| 103 |
} |
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
@Override |
| 110 |
@VisibleForTesting |
| 111 |
public void validate(boolean validate) { |
| 112 |
this.validate = validate; |
| 113 |
} |
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
@Override |
| 123 |
public void setWrapperPrefix(String prefix) { |
| 124 |
|
| 125 |
int prefixLine = 0; |
| 126 |
int prefixIndex = 0; |
| 127 |
|
| 128 |
for (int i = 0; i < prefix.length(); ++i) { |
| 129 |
if (prefix.charAt(i) == '\n') { |
| 130 |
prefixLine++; |
| 131 |
prefixIndex = 0; |
| 132 |
} else { |
| 133 |
prefixIndex++; |
| 134 |
} |
| 135 |
} |
| 136 |
|
| 137 |
prefixPosition = new FilePosition(prefixLine, prefixIndex); |
| 138 |
} |
| 139 |
|
| 140 |
|
| 141 |
|
| 142 |
|
| 143 |
|
| 144 |
|
| 145 |
|
| 146 |
|
| 147 |
|
| 148 |
|
| 149 |
|
| 150 |
@Override |
| 151 |
public void setStartingPosition(int offsetLine, int offsetIndex) { |
| 152 |
Preconditions.checkState(offsetLine >= 0); |
| 153 |
Preconditions.checkState(offsetIndex >= 0); |
| 154 |
offsetPosition = new FilePosition(offsetLine, offsetIndex); |
| 155 |
} |
| 156 |
|
| 157 |
|
| 158 |
|
| 159 |
|
| 160 |
|
| 161 |
|
| 162 |
@Override |
| 163 |
public void addMapping( |
| 164 |
String sourceName, @Nullable String symbolName, |
| 165 |
FilePosition sourceStartPosition, |
| 166 |
FilePosition startPosition, FilePosition endPosition) { |
| 167 |
|
| 168 |
|
| 169 |
if (sourceName == null || sourceStartPosition.getLine() < 0) { |
| 170 |
return; |
| 171 |
} |
| 172 |
|
| 173 |
FilePosition adjustedStart = startPosition; |
| 174 |
FilePosition adjustedEnd = endPosition; |
| 175 |
|
| 176 |
if (offsetPosition.getLine() != 0 |
| 177 |
|| offsetPosition.getColumn() != 0) { |
| 178 |
|
| 179 |
|
| 180 |
|
| 181 |
|
| 182 |
int offsetLine = offsetPosition.getLine(); |
| 183 |
int startOffsetPosition = offsetPosition.getColumn(); |
| 184 |
int endOffsetPosition = offsetPosition.getColumn(); |
| 185 |
|
| 186 |
if (startPosition.getLine() > 0) { |
| 187 |
startOffsetPosition = 0; |
| 188 |
} |
| 189 |
|
| 190 |
if (endPosition.getLine() > 0) { |
| 191 |
endOffsetPosition = 0; |
| 192 |
} |
| 193 |
|
| 194 |
adjustedStart = new FilePosition( |
| 195 |
startPosition.getLine() + offsetLine, |
| 196 |
startPosition.getColumn() + startOffsetPosition); |
| 197 |
|
| 198 |
adjustedEnd = new FilePosition( |
| 199 |
endPosition.getLine() + offsetLine, |
| 200 |
endPosition.getColumn() + endOffsetPosition); |
| 201 |
} |
| 202 |
|
| 203 |
|
| 204 |
Mapping mapping = new Mapping(); |
| 205 |
mapping.sourceFile = getSourceId(sourceName); |
| 206 |
mapping.originalPosition = sourceStartPosition; |
| 207 |
mapping.originalName = symbolName; |
| 208 |
mapping.startPosition = adjustedStart; |
| 209 |
mapping.endPosition = adjustedEnd; |
| 210 |
|
| 211 |
|
| 212 |
if (lastMapping != null) { |
| 213 |
int lastLine = lastMapping.startPosition.getLine(); |
| 214 |
int lastColumn = lastMapping.startPosition.getColumn(); |
| 215 |
int nextLine = mapping.startPosition.getLine(); |
| 216 |
int nextColumn = mapping.startPosition.getColumn(); |
| 217 |
Preconditions.checkState(nextLine > lastLine |
| 218 |
|| (nextLine == lastLine && nextColumn >= lastColumn), |
| 219 |
"Incorrect source mappings order, previous : (%s,%s)\n" |
| 220 |
+ "new : (%s,%s)\nnode : %s", |
| 221 |
lastLine, lastColumn, nextLine, nextColumn); |
| 222 |
} |
| 223 |
|
| 224 |
lastMapping = mapping; |
| 225 |
mappings.add(mapping); |
| 226 |
} |
| 227 |
|
| 228 |
|
| 229 |
|
| 230 |
|
| 231 |
|
| 232 |
|
| 233 |
|
| 234 |
|
| 235 |
|
| 236 |
|
| 237 |
|
| 238 |
|
| 239 |
|
| 240 |
|
| 241 |
|
| 242 |
|
| 243 |
|
| 244 |
|
| 245 |
|
| 246 |
|
| 247 |
|
| 248 |
|
| 249 |
|
| 250 |
|
| 251 |
|
| 252 |
|
| 253 |
|
| 254 |
|
| 255 |
|
| 256 |
|
| 257 |
|
| 258 |
|
| 259 |
|
| 260 |
|
| 261 |
|
| 262 |
|
| 263 |
|
| 264 |
|
| 265 |
|
| 266 |
|
| 267 |
|
| 268 |
|
| 269 |
|
| 270 |
|
| 271 |
|
| 272 |
@Override |
| 273 |
public void appendTo(Appendable out, String name) throws IOException { |
| 274 |
int maxLine = prepMappings(); |
| 275 |
|
| 276 |
|
| 277 |
out.append("{\n"); |
| 278 |
appendFirstField(out, "version", "2"); |
| 279 |
appendField(out, "file", escapeString(name)); |
| 280 |
appendField(out, "lineCount", String.valueOf(maxLine + 1)); |
| 281 |
|
| 282 |
|
| 283 |
appendFieldStart(out, "lineMaps"); |
| 284 |
out.append("["); |
| 285 |
(new LineMapper(out)).appendLineMappings(); |
| 286 |
out.append("]"); |
| 287 |
appendFieldEnd(out); |
| 288 |
|
| 289 |
|
| 290 |
appendFieldStart(out, "mappings"); |
| 291 |
out.append("["); |
| 292 |
(new MappingWriter()).appendMappings(out); |
| 293 |
out.append("]"); |
| 294 |
appendFieldEnd(out); |
| 295 |
|
| 296 |
|
| 297 |
appendFieldStart(out, "sources"); |
| 298 |
out.append("["); |
| 299 |
addSourceNameMap(out); |
| 300 |
out.append("]"); |
| 301 |
appendFieldEnd(out); |
| 302 |
|
| 303 |
|
| 304 |
appendFieldStart(out, "names"); |
| 305 |
out.append("["); |
| 306 |
addOriginalNameMap(out); |
| 307 |
out.append("]"); |
| 308 |
appendFieldEnd(out); |
| 309 |
|
| 310 |
out.append("\n}\n"); |
| 311 |
} |
| 312 |
|
| 313 |
|
| 314 |
|
| 315 |
|
| 316 |
private void addSourceNameMap(Appendable out) throws IOException { |
| 317 |
addMap(out, sourceFileMap); |
| 318 |
} |
| 319 |
|
| 320 |
|
| 321 |
|
| 322 |
|
| 323 |
private void addOriginalNameMap(Appendable out) throws IOException { |
| 324 |
addMap(out, originalNameMap); |
| 325 |
} |
| 326 |
|
| 327 |
|
| 328 |
|
| 329 |
|
| 330 |
private void addMap(Appendable out, Map<String, Integer> map) |
| 331 |
throws IOException { |
| 332 |
int i = 0; |
| 333 |
for (Entry<String, Integer> entry : map.entrySet()) { |
| 334 |
String key = entry.getKey(); |
| 335 |
if (i != 0) { |
| 336 |
out.append(","); |
| 337 |
} |
| 338 |
out.append(escapeString(key)); |
| 339 |
i++; |
| 340 |
} |
| 341 |
} |
| 342 |
|
| 343 |
|
| 344 |
|
| 345 |
|
| 346 |
private static String escapeString(String value) { |
| 347 |
return Util.escapeString(value); |
| 348 |
} |
| 349 |
|
| 350 |
|
| 351 |
|
| 352 |
private static void appendFirstField( |
| 353 |
Appendable out, String name, CharSequence value) |
| 354 |
throws IOException { |
| 355 |
out.append("\""); |
| 356 |
out.append(name); |
| 357 |
out.append("\""); |
| 358 |
out.append(":"); |
| 359 |
out.append(value); |
| 360 |
} |
| 361 |
|
| 362 |
private static void appendField( |
| 363 |
Appendable out, String name, CharSequence value) |
| 364 |
throws IOException { |
| 365 |
out.append(",\n"); |
| 366 |
out.append("\""); |
| 367 |
out.append(name); |
| 368 |
out.append("\""); |
| 369 |
out.append(":"); |
| 370 |
out.append(value); |
| 371 |
} |
| 372 |
|
| 373 |
private static void appendFieldStart(Appendable out, String name) |
| 374 |
throws IOException { |
| 375 |
appendField(out, name, ""); |
| 376 |
} |
| 377 |
|
| 378 |
@SuppressWarnings("unused") |
| 379 |
private static void appendFieldEnd(Appendable out) |
| 380 |
throws IOException { |
| 381 |
} |
| 382 |
|
| 383 |
|
| 384 |
|
| 385 |
|
| 386 |
private int prepMappings() throws IOException { |
| 387 |
|
| 388 |
(new MappingTraversal()).traverse(new UsedMappingCheck()); |
| 389 |
|
| 390 |
|
| 391 |
int id = 0; |
| 392 |
int maxLine = 0; |
| 393 |
for (Mapping m : mappings) { |
| 394 |
if (m.used) { |
| 395 |
m.id = id++; |
| 396 |
int endPositionLine = m.endPosition.getLine(); |
| 397 |
maxLine = Math.max(maxLine, endPositionLine); |
| 398 |
} |
| 399 |
} |
| 400 |
|
| 401 |
|
| 402 |
return maxLine + prefixPosition.getLine(); |
| 403 |
} |
| 404 |
|
| 405 |
|
| 406 |
|
| 407 |
|
| 408 |
|
| 409 |
|
| 410 |
private int getSourceId(String sourceName) { |
| 411 |
if (sourceName != lastSourceFile) { |
| 412 |
lastSourceFile = sourceName; |
| 413 |
Integer index = sourceFileMap.get(sourceName); |
| 414 |
if (index != null) { |
| 415 |
lastSourceFileIndex = index; |
| 416 |
} else { |
| 417 |
lastSourceFileIndex = sourceFileMap.size(); |
| 418 |
sourceFileMap.put(sourceName, lastSourceFileIndex); |
| 419 |
} |
| 420 |
} |
| 421 |
return lastSourceFileIndex; |
| 422 |
} |
| 423 |
|
| 424 |
|
| 425 |
|
| 426 |
|
| 427 |
|
| 428 |
|
| 429 |
private int getNameId(String symbolName) { |
| 430 |
int originalNameIndex; |
| 431 |
Integer index = originalNameMap.get(symbolName); |
| 432 |
if (index != null) { |
| 433 |
originalNameIndex = index; |
| 434 |
} else { |
| 435 |
originalNameIndex = originalNameMap.size(); |
| 436 |
originalNameMap.put(symbolName, originalNameIndex); |
| 437 |
} |
| 438 |
return originalNameIndex; |
| 439 |
} |
| 440 |
|
| 441 |
|
| 442 |
|
| 443 |
|
| 444 |
|
| 445 |
static class Mapping { |
| 446 |
|
| 447 |
|
| 448 |
|
| 449 |
int id = UNMAPPED; |
| 450 |
|
| 451 |
|
| 452 |
|
| 453 |
|
| 454 |
int sourceFile; |
| 455 |
|
| 456 |
|
| 457 |
|
| 458 |
|
| 459 |
|
| 460 |
|
| 461 |
FilePosition originalPosition; |
| 462 |
|
| 463 |
|
| 464 |
|
| 465 |
|
| 466 |
|
| 467 |
FilePosition startPosition; |
| 468 |
|
| 469 |
|
| 470 |
|
| 471 |
|
| 472 |
|
| 473 |
FilePosition endPosition; |
| 474 |
|
| 475 |
|
| 476 |
|
| 477 |
|
| 478 |
|
| 479 |
String originalName; |
| 480 |
|
| 481 |
|
| 482 |
|
| 483 |
|
| 484 |
boolean used = false; |
| 485 |
} |
| 486 |
|
| 487 |
private class MappingWriter { |
| 488 |
|
| 489 |
|
| 490 |
|
| 491 |
private int lastLine = 0; |
| 492 |
private String lastLineString = String.valueOf(0); |
| 493 |
|
| 494 |
|
| 495 |
|
| 496 |
|
| 497 |
private void appendMappingTo( |
| 498 |
Mapping m, Appendable out) throws IOException { |
| 499 |
out.append("["); |
| 500 |
|
| 501 |
out.append(String.valueOf(m.sourceFile)); |
| 502 |
out.append(","); |
| 503 |
|
| 504 |
int line = m.originalPosition.getLine(); |
| 505 |
if (line != lastLine) { |
| 506 |
lastLineString = String.valueOf(line); |
| 507 |
} |
| 508 |
String lineValue = lastLineString; |
| 509 |
|
| 510 |
out.append(lineValue); |
| 511 |
|
| 512 |
out.append(","); |
| 513 |
out.append(String.valueOf(m.originalPosition.getColumn())); |
| 514 |
|
| 515 |
if (m.originalName != null) { |
| 516 |
out.append(","); |
| 517 |
out.append(String.valueOf(getNameId(m.originalName))); |
| 518 |
} |
| 519 |
|
| 520 |
out.append("],\n"); |
| 521 |
} |
| 522 |
|
| 523 |
|
| 524 |
|
| 525 |
|
| 526 |
void appendMappings(Appendable out) throws IOException { |
| 527 |
for (Mapping m : mappings) { |
| 528 |
if (m.used) { |
| 529 |
appendMappingTo(m, out); |
| 530 |
} |
| 531 |
} |
| 532 |
} |
| 533 |
} |
| 534 |
|
| 535 |
private class LineMapper implements MappingVisitor { |
| 536 |
|
| 537 |
private final Appendable out; |
| 538 |
|
| 539 |
|
| 540 |
private int lastId = UNMAPPED; |
| 541 |
|
| 542 |
LineMapper(Appendable out) { |
| 543 |
this.out = out; |
| 544 |
} |
| 545 |
|
| 546 |
|
| 547 |
|
| 548 |
|
| 549 |
@Override |
| 550 |
public void visit(Mapping m, int line, int col, int nextLine, int nextCol) |
| 551 |
throws IOException { |
| 552 |
|
| 553 |
int id = (m != null) ? m.id : UNMAPPED; |
| 554 |
|
| 555 |
for (int i = line; i <= nextLine; i++) { |
| 556 |
if (i == nextLine) { |
| 557 |
closeEntry(id, nextCol - col); |
| 558 |
break; |
| 559 |
} |
| 560 |
|
| 561 |
closeLine(false); |
| 562 |
openLine(); |
| 563 |
|
| 564 |
|
| 565 |
col = 0; |
| 566 |
} |
| 567 |
} |
| 568 |
|
| 569 |
|
| 570 |
void appendLineMappings() throws IOException { |
| 571 |
|
| 572 |
openLine(); |
| 573 |
|
| 574 |
(new MappingTraversal()).traverse(this); |
| 575 |
|
| 576 |
|
| 577 |
closeLine(true); |
| 578 |
} |
| 579 |
|
| 580 |
|
| 581 |
|
| 582 |
|
| 583 |
private void openLine() throws IOException { |
| 584 |
out.append("\""); |
| 585 |
|
| 586 |
this.lastId = 0; |
| 587 |
} |
| 588 |
|
| 589 |
|
| 590 |
|
| 591 |
|
| 592 |
private void closeLine(boolean finalEntry) throws IOException { |
| 593 |
if (finalEntry) { |
| 594 |
out.append("\""); |
| 595 |
} else { |
| 596 |
out.append("\",\n"); |
| 597 |
} |
| 598 |
} |
| 599 |
|
| 600 |
private void closeEntry(int id, int reps) throws IOException { |
| 601 |
if (reps == 0) { |
| 602 |
return; |
| 603 |
} |
| 604 |
|
| 605 |
StringBuilder sb = new StringBuilder(); |
| 606 |
LineMapEncoder.encodeEntry(sb, id, lastId, reps); |
| 607 |
|
| 608 |
if (validate) { |
| 609 |
SourceMapLineDecoder.LineEntry entry = |
| 610 |
SourceMapLineDecoder.decodeLineEntry(sb.toString(), lastId); |
| 611 |
Preconditions.checkState(entry.id == id && entry.reps == reps, |
| 612 |
"expected (%s,%s) but got (%s,%s)", |
| 613 |
id, reps, entry.id, entry.reps); |
| 614 |
} |
| 615 |
|
| 616 |
out.append(sb); |
| 617 |
lastId = id; |
| 618 |
} |
| 619 |
} |
| 620 |
|
| 621 |
@VisibleForTesting |
| 622 |
public static class LineMapEncoder { |
| 623 |
|
| 624 |
|
| 625 |
|
| 626 |
|
| 627 |
|
| 628 |
|
| 629 |
|
| 630 |
|
| 631 |
|
| 632 |
|
| 633 |
|
| 634 |
public static void encodeEntry(Appendable out, int id, int lastId, int reps) |
| 635 |
throws IOException { |
| 636 |
Preconditions.checkState(reps > 0); |
| 637 |
int relativeIdLength = getRelativeMappingIdLength(id, lastId); |
| 638 |
int relativeId = getRelativeMappingId(id, relativeIdLength, lastId); |
| 639 |
|
| 640 |
String relativeIdString = valueToBase64(relativeId, relativeIdLength); |
| 641 |
|
| 642 |
|
| 643 |
|
| 644 |
|
| 645 |
|
| 646 |
|
| 647 |
|
| 648 |
|
| 649 |
|
| 650 |
|
| 651 |
|
| 652 |
|
| 653 |
if (reps > 16 || relativeIdLength > 4) { |
| 654 |
String repsString = valueToBase64(reps - 1, 1); |
| 655 |
for (int i = 0; i < repsString.length(); i++) { |
| 656 |
|
| 657 |
out.append('!'); |
| 658 |
} |
| 659 |
String sizeId = valueToBase64(relativeIdString.length() - 1, 1); |
| 660 |
|
| 661 |
out.append(sizeId); |
| 662 |
out.append(repsString); |
| 663 |
} else { |
| 664 |
int prefix = ((reps - 1) << 2) + (relativeIdString.length() - 1); |
| 665 |
Preconditions.checkState(prefix < 64 && prefix >= 0, |
| 666 |
"prefix (%s) reps(%s) map id size(%s)", |
| 667 |
prefix, reps, relativeIdString.length()); |
| 668 |
out.append(valueToBase64(prefix, 1)); |
| 669 |
} |
| 670 |
out.append(relativeIdString); |
| 671 |
} |
| 672 |
|
| 673 |
|
| 674 |
|
| 675 |
|
| 676 |
|
| 677 |
|
| 678 |
|
| 679 |
public static int getRelativeMappingId(int id, int idLength, int lastId) { |
| 680 |
int base = 1 << (idLength * 6); |
| 681 |
int relativeId = id - lastId; |
| 682 |
return (relativeId < 0) ? relativeId + base : relativeId; |
| 683 |
} |
| 684 |
|
| 685 |
|
| 686 |
|
| 687 |
|
| 688 |
public static int getRelativeMappingIdLength(int rawId, int lastId) { |
| 689 |
Preconditions.checkState(rawId >= 0 || rawId == UNMAPPED); |
| 690 |
int relativeId = rawId - lastId; |
| 691 |
int id = (relativeId < 0 ? Math.abs(relativeId) - 1 : relativeId) << 1; |
| 692 |
int digits = 1; |
| 693 |
int base = 64; |
| 694 |
while (id >= base) { |
| 695 |
digits++; |
| 696 |
base *= 64; |
| 697 |
} |
| 698 |
return digits; |
| 699 |
} |
| 700 |
|
| 701 |
|
| 702 |
|
| 703 |
|
| 704 |
|
| 705 |
static String valueToBase64(int value, int minimumSize) { |
| 706 |
int size = 0; |
| 707 |
char chars[] = new char[4]; |
| 708 |
do { |
| 709 |
int charValue = value & 63; |
| 710 |
value = value >>> 6; |
| 711 |
chars[size++] = Base64.toBase64(charValue); |
| 712 |
} while (value > 0); |
| 713 |
|
| 714 |
StringBuilder sb = new StringBuilder(size); |
| 715 |
|
| 716 |
while (minimumSize > size) { |
| 717 |
sb.append(Base64.toBase64(0)); |
| 718 |
minimumSize--; |
| 719 |
} |
| 720 |
while (size > 0) { |
| 721 |
sb.append(chars[--size]); |
| 722 |
} |
| 723 |
return sb.toString(); |
| 724 |
} |
| 725 |
} |
| 726 |
|
| 727 |
|
| 728 |
|
| 729 |
|
| 730 |
private class UsedMappingCheck implements MappingVisitor { |
| 731 |
|
| 732 |
|
| 733 |
|
| 734 |
@Override |
| 735 |
public void visit(Mapping m, int line, int col, int nextLine, int nextCol) |
| 736 |
throws IOException { |
| 737 |
if (m != null) { |
| 738 |
m.used = true; |
| 739 |
} |
| 740 |
} |
| 741 |
} |
| 742 |
|
| 743 |
private interface MappingVisitor { |
| 744 |
|
| 745 |
|
| 746 |
|
| 747 |
|
| 748 |
|
| 749 |
|
| 750 |
|
| 751 |
|
| 752 |
|
| 753 |
void visit(Mapping m, int line, int col, int endLine, int endCol) |
| 754 |
throws IOException; |
| 755 |
} |
| 756 |
|
| 757 |
|
| 758 |
|
| 759 |
|
| 760 |
|
| 761 |
private class MappingTraversal { |
| 762 |
|
| 763 |
private int line; |
| 764 |
private int col; |
| 765 |
|
| 766 |
MappingTraversal() { |
| 767 |
} |
| 768 |
|
| 769 |
|
| 770 |
void traverse(MappingVisitor v) throws IOException { |
| 771 |
|
| 772 |
|
| 773 |
|
| 774 |
Deque<Mapping> stack = new ArrayDeque<Mapping>(); |
| 775 |
for (Mapping m : mappings) { |
| 776 |
|
| 777 |
|
| 778 |
|
| 779 |
|
| 780 |
while (!stack.isEmpty() && !isOverlapped(stack.peek(), m)) { |
| 781 |
Mapping previous = stack.pop(); |
| 782 |
maybeVisit(v, previous); |
| 783 |
} |
| 784 |
|
| 785 |
|
| 786 |
|
| 787 |
Mapping parent = stack.peek(); |
| 788 |
maybeVisitParent(v, parent, m); |
| 789 |
|
| 790 |
stack.push(m); |
| 791 |
} |
| 792 |
|
| 793 |
|
| 794 |
|
| 795 |
while (!stack.isEmpty()) { |
| 796 |
Mapping m = stack.pop(); |
| 797 |
maybeVisit(v, m); |
| 798 |
} |
| 799 |
} |
| 800 |
|
| 801 |
|
| 802 |
|
| 803 |
|
| 804 |
private int getAdjustedLine(FilePosition p) { |
| 805 |
return p.getLine() + prefixPosition.getLine(); |
| 806 |
} |
| 807 |
|
| 808 |
|
| 809 |
|
| 810 |
|
| 811 |
private int getAdjustedCol(FilePosition p) { |
| 812 |
int rawLine = p.getLine(); |
| 813 |
int rawCol = p.getColumn(); |
| 814 |
|
| 815 |
return (rawLine != 0) |
| 816 |
? rawCol : rawCol + prefixPosition.getColumn(); |
| 817 |
} |
| 818 |
|
| 819 |
|
| 820 |
|
| 821 |
|
| 822 |
private boolean isOverlapped(Mapping m1, Mapping m2) { |
| 823 |
|
| 824 |
int l1 = m1.endPosition.getLine(); |
| 825 |
int l2 = m2.startPosition.getLine(); |
| 826 |
int c1 = m1.endPosition.getColumn(); |
| 827 |
int c2 = m2.startPosition.getColumn(); |
| 828 |
|
| 829 |
return (l1 == l2 && c1 >= c2) || l1 > l2; |
| 830 |
} |
| 831 |
|
| 832 |
|
| 833 |
|
| 834 |
|
| 835 |
|
| 836 |
private void maybeVisit(MappingVisitor v, Mapping m) throws IOException { |
| 837 |
int nextLine = getAdjustedLine(m.endPosition); |
| 838 |
int nextCol = getAdjustedCol(m.endPosition); |
| 839 |
|
| 840 |
|
| 841 |
if (line < nextLine || (line == nextLine && col < nextCol)) { |
| 842 |
visit(v, m, nextLine, nextCol); |
| 843 |
} |
| 844 |
} |
| 845 |
|
| 846 |
|
| 847 |
|
| 848 |
|
| 849 |
private void maybeVisitParent(MappingVisitor v, Mapping parent, Mapping m) |
| 850 |
throws IOException { |
| 851 |
int nextLine = getAdjustedLine(m.startPosition); |
| 852 |
int nextCol = getAdjustedCol(m.startPosition); |
| 853 |
|
| 854 |
Preconditions.checkState(line < nextLine || col <= nextCol); |
| 855 |
if (line < nextLine || (line == nextLine && col < nextCol)) { |
| 856 |
visit(v, parent, nextLine, nextCol); |
| 857 |
} |
| 858 |
} |
| 859 |
|
| 860 |
|
| 861 |
|
| 862 |
|
| 863 |
|
| 864 |
private void visit(MappingVisitor v, Mapping m, |
| 865 |
int nextLine, int nextCol) |
| 866 |
throws IOException { |
| 867 |
Preconditions.checkState(line <= nextLine); |
| 868 |
Preconditions.checkState(line < nextLine || col < nextCol); |
| 869 |
|
| 870 |
if (line == nextLine && col == nextCol) { |
| 871 |
|
| 872 |
Preconditions.checkState(false); |
| 873 |
return; |
| 874 |
} |
| 875 |
|
| 876 |
v.visit(m, line, col, nextLine, nextCol); |
| 877 |
|
| 878 |
line = nextLine; |
| 879 |
col = nextCol; |
| 880 |
} |
| 881 |
} |
| 882 |
|
| 883 |
@Override |
| 884 |
public void appendIndexMapTo( |
| 885 |
Appendable out, String name, List<SourceMapSection> appSections) { |
| 886 |
throw new UnsupportedOperationException(); |
| 887 |
} |
| 888 |
} |