Debian Codegraph @

src/com/google/debugging/sourcemap/SourceMapGeneratorV2.java

raw symbols SCIP
1 /*
2 * Copyright 2009 The Closure Compiler Authors.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
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 * Collects information mapping the generated (compiled) source back to
36 * its original source for debugging purposes.
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 * A pre-order traversal ordered list of mappings stored in this map.
47 */
48 private List<Mapping> mappings = Lists.newArrayList();
49
50 /**
51 * A map of source names to source name index
52 */
53 private LinkedHashMap<String, Integer> sourceFileMap =
54 Maps.newLinkedHashMap();
55
56 /**
57 * A map of symbol names to symbol name index
58 */
59 private LinkedHashMap<String, Integer> originalNameMap =
60 Maps.newLinkedHashMap();
61
62 /**
63 * Cache of the last mappings source name.
64 */
65 private String lastSourceFile = null;
66
67 /**
68 * Cache of the last mappings source name index.
69 */
70 private int lastSourceFileIndex = -1;
71
72 /**
73 * For validation store the last mapping added.
74 */
75 private Mapping lastMapping;
76
77 /**
78 * The position that the current source map is offset in the
79 * buffer being used to generated the compiled source file.
80 */
81 private FilePosition offsetPosition = new FilePosition(0, 0);
82
83 /**
84 * The position that the current source map is offset in the
85 * generated the compiled source file by the addition of a
86 * an output wrapper prefix.
87 */
88 private FilePosition prefixPosition = new FilePosition(0, 0);
89
90 /**
91 * {@inheritDoc}
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 * @param validate Whether to perform (potentially costly) validation on the
107 * generated source map.
108 */
109 @Override
110 @VisibleForTesting
111 public void validate(boolean validate) {
112 this.validate = validate;
113 }
114
115 /**
116 * Sets the prefix used for wrapping the generated source file before
117 * it is written. This ensures that the source map is adjusted for the
118 * change in character offsets.
119 *
120 * @param prefix The prefix that is added before the generated source code.
121 */
122 @Override
123 public void setWrapperPrefix(String prefix) {
124 // Determine the current line and character position.
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 * Sets the source code that exists in the buffer for which the
142 * generated code is being generated. This ensures that the source map
143 * accurately reflects the fact that the source is being appended to
144 * an existing buffer and as such, does not start at line 0, position 0
145 * but rather some other line and position.
146 *
147 * @param offsetLine The index of the current line being printed.
148 * @param offsetIndex The column index of the current character being printed.
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 * Adds a mapping for the given node. Mappings must be added in order.
159 * @param startPosition The position on the starting line
160 * @param endPosition The position on the ending line.
161 */
162 @Override
163 public void addMapping(
164 String sourceName, @Nullable String symbolName,
165 FilePosition sourceStartPosition,
166 FilePosition startPosition, FilePosition endPosition) {
167
168 // Don't bother if there is not sufficient information to be useful.
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 // If the mapping is found on the first line, we need to offset
179 // its character position by the number of characters found on
180 // the *last* line of the source file to which the code is
181 // being generated.
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 // Create the new mapping.
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 // Validate the mappings are in a proper order.
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 * Writes out the source map in the following format (line numbers are for
230 * reference only and are not part of the format):
231 *
232 * 1. {
233 * 2. version: 2,
234 * 3. file: "out.js"
235 * 4. lineCount: 2
236 * 5. lineMaps: [
237 * 6. "ABAAA",
238 * 7. "ABAA"
239 * 8. ],
240 * 9. sourceRoot: "",
241 * 10. sources: ["foo.js", "bar.js"],
242 * 11. names: ["src", "maps", "are", "fun"],
243 * 12. mappings: [
244 * 13. [1, 1, 2, 4],
245 * 14. [2, 1, 2, "yack"],
246 * 15. ],
247 * 16. }
248 *
249 * Line 1: The entire file is a single JSON object
250 * Line 2: File revision (always the first entry in the object)
251 * Line 3: The name of the file that this source map is associated with.
252 * Line 4: The number of lines represented in the source map.
253 * Line 5: "lineMaps" field is a JSON array, where each entry represents a
254 * line in the generated text.
255 * Line 6: A line entry, representing a series of line segments, where each
256 * segment encodes an mappings-id and repetition count.
257 * Line 9: An optional source root, useful for relocating source files on a
258 * server or removing repeated prefix values in the "sources" entry.
259 * Line 10: A list of sources used by the "mappings" entry relative to the
260 * sourceRoot.
261 * Line 11: A list of symbol names used by the "mapping" entry. This list
262 * may be incomplete.
263 * Line 12: The mappings field.
264 * Line 13: Each entry represent a block of text in the original source, and
265 * consists four fields:
266 * The source file name
267 * The line in the source file the text begins
268 * The column in the line that the text begins
269 * An optional name (from the original source) that this entry represents.
270 * This can either be an string or index into the "names" field.
271 */
272 @Override
273 public void appendTo(Appendable out, String name) throws IOException {
274 int maxLine = prepMappings();
275
276 // Add the header fields.
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 // Add the line character maps.
283 appendFieldStart(out, "lineMaps");
284 out.append("[");
285 (new LineMapper(out)).appendLineMappings();
286 out.append("]");
287 appendFieldEnd(out);
288
289 // Add the mappings themselves.
290 appendFieldStart(out, "mappings");
291 out.append("[");
292 (new MappingWriter()).appendMappings(out);
293 out.append("]");
294 appendFieldEnd(out);
295
296 // Files names
297 appendFieldStart(out, "sources");
298 out.append("[");
299 addSourceNameMap(out);
300 out.append("]");
301 appendFieldEnd(out);
302
303 // Files names
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 * Writes the source name map to 'out'.
315 */
316 private void addSourceNameMap(Appendable out) throws IOException {
317 addMap(out, sourceFileMap);
318 }
319
320 /**
321 * Writes the original name map to 'out'.
322 */
323 private void addOriginalNameMap(Appendable out) throws IOException {
324 addMap(out, originalNameMap);
325 }
326
327 /**
328 * Writes the source name map to 'out'.
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 * Escapes the given string for JSON.
345 */
346 private static String escapeString(String value) {
347 return Util.escapeString(value);
348 }
349
350 // Source map field helpers.
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 * Assigns sequential ids to used mappings, and returns the last line mapped.
385 */
386 private int prepMappings() throws IOException {
387 // Mark any unused mappings.
388 (new MappingTraversal()).traverse(new UsedMappingCheck());
389
390 // Renumber used mappings and keep track of the last line.
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 // Adjust for the prefix.
402 return maxLine + prefixPosition.getLine();
403 }
404
405 /**
406 * Pools source names.
407 * @param sourceName The source location to index.
408 * @return The id to represent the source name in the output.
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 * Pools symbol names
426 * @param symbolName The symbol name to index.
427 * @return The id to represent the symbol name in the output.
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 * A mapping from a given position in an input source file to a given position
443 * in the generated code.
444 */
445 static class Mapping {
446 /**
447 * A unique ID for this mapping for record keeping purposes.
448 */
449 int id = UNMAPPED;
450
451 /**
452 * The source file index.
453 */
454 int sourceFile;
455
456 /**
457 * The position of the code in the input source file. Both
458 * the line number and the character index are indexed by
459 * 1 for legacy reasons via the Rhino Node class.
460 */
461 FilePosition originalPosition;
462
463 /**
464 * The starting position of the code in the generated source
465 * file which this mapping represents. Indexed by 0.
466 */
467 FilePosition startPosition;
468
469 /**
470 * The ending position of the code in the generated source
471 * file which this mapping represents. Indexed by 0.
472 */
473 FilePosition endPosition;
474
475 /**
476 * The original name of the token found at the position
477 * represented by this mapping (if any).
478 */
479 String originalName;
480
481 /**
482 * Whether the mapping is actually used by the source map.
483 */
484 boolean used = false;
485 }
486
487 private class MappingWriter {
488 /**
489 * Cache of escaped source file name.
490 */
491 private int lastLine = 0;
492 private String lastLineString = String.valueOf(0);
493
494 /**
495 * Appends the mapping to the given buffer.
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 * Add used mappings to the supplied Appendable.
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 // The destination.
537 private final Appendable out;
538
539 // Whether the current line has had a value written yet.
540 private int lastId = UNMAPPED;
541
542 LineMapper(Appendable out) {
543 this.out = out;
544 }
545
546 /**
547 * As each segment is visited write out the appropriate line mapping.
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 // Set the starting location for the next line.
565 col = 0;
566 }
567 }
568
569 // Append the line mapping entries.
570 void appendLineMappings() throws IOException {
571 // Start the first line.
572 openLine();
573
574 (new MappingTraversal()).traverse(this);
575
576 // And close the final line.
577 closeLine(true);
578 }
579
580 /**
581 * Begin the entry for a new line.
582 */
583 private void openLine() throws IOException {
584 out.append("\"");
585 // The first id of the line is not relative.
586 this.lastId = 0;
587 }
588
589 /**
590 * End the entry for a line.
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 * The source map line map is consists of a series of entries each
625 * representing a map entry and a repetition count of that entry.
626 *
627 * @param out The entry destination.
628 * @param id The id for the entry.
629 * @param lastId The previous id written, used to generate a relative
630 * map id.
631 * @param reps The number of times the id is repeated in the map.
632 * @throws IOException
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 // If we can, we use a single base64 digit to encode both the id length
643 // and the repetition count. The current best division of the base64
644 // digit (which has 6 bits) is 2 bits for the id length (1-4 digits) and
645 // 4 bit for the repetition count (1-16 repetitions). If either of these
646 // two values are exceeded a "!" is written (a non-base64 character) to
647 // signal the a full base64 character is used for repetition count and
648 // the mapping id length. As the repetition count can exceed 64, we
649 // allow the "escape" ("!") to be repeated to signal additional
650 // repetition count length characters. It is extremely unlikely that
651 // mapping id length will exceed 64 base64 characters in length so
652 // additional "!" don't signal additional id length characters.
653 if (reps > 16 || relativeIdLength > 4) {
654 String repsString = valueToBase64(reps - 1, 1);
655 for (int i = 0; i < repsString.length(); i++) {
656 // TODO(johnlenz): update this to whatever is agreed to.
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 * @param idLength the length relative id, when encoded in as a base64
675 * value. @see #getRelativeMappingIdLength
676 * @return A value relative to the the lastId. Negative value are
677 * represented as a two-complement value.
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 * @return The length of the base64 number needed to include the id.
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 * @return return the base64 number encoding the provided value,
703 * padded if necessary to create a number with the given minimum length.
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; // base64 chars
710 value = value >>> 6; // get the next value;
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 * Mark any visited mapping as "used".
729 */
730 private class UsedMappingCheck implements MappingVisitor {
731 /**
732 * @throws IOException
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 * @param m The mapping for the current code segment. null if the segment
746 * is unmapped.
747 * @param line The starting line for this code segment.
748 * @param col The starting column for this code segment.
749 * @param endLine The ending line
750 * @param endCol The ending column
751 * @throws IOException
752 */
753 void visit(Mapping m, int line, int col, int endLine, int endCol)
754 throws IOException;
755 }
756
757 /**
758 * Walk the mappings and visit each segment of the mappings, unmapped
759 * segments are visited with a null mapping, unused mapping are not visited.
760 */
761 private class MappingTraversal {
762 // The last line and column written
763 private int line;
764 private int col;
765
766 MappingTraversal() {
767 }
768
769 // Append the line mapping entries.
770 void traverse(MappingVisitor v) throws IOException {
771 // The mapping list is ordered as a pre-order traversal. The mapping
772 // positions give us enough information to rebuild the stack and this
773 // allows the building of the source map in O(n) time.
774 Deque<Mapping> stack = new ArrayDeque<Mapping>();
775 for (Mapping m : mappings) {
776 // Find the closest ancestor of the current mapping:
777 // An overlapping mapping is an ancestor of the current mapping, any
778 // non-overlapping mappings are siblings (or cousins) and must be
779 // closed in the reverse order of when they encountered.
780 while (!stack.isEmpty() && !isOverlapped(stack.peek(), m)) {
781 Mapping previous = stack.pop();
782 maybeVisit(v, previous);
783 }
784
785 // Any gaps between the current line position and the start of the
786 // current mapping belong to the parent.
787 Mapping parent = stack.peek();
788 maybeVisitParent(v, parent, m);
789
790 stack.push(m);
791 }
792
793 // There are no more children to be had, simply close the remaining
794 // mappings in the reverse order of when they encountered.
795 while (!stack.isEmpty()) {
796 Mapping m = stack.pop();
797 maybeVisit(v, m);
798 }
799 }
800
801 /**
802 * @return The line adjusted for the prefix position.
803 */
804 private int getAdjustedLine(FilePosition p) {
805 return p.getLine() + prefixPosition.getLine();
806 }
807
808 /**
809 * @return The column adjusted for the prefix position.
810 */
811 private int getAdjustedCol(FilePosition p) {
812 int rawLine = p.getLine();
813 int rawCol = p.getColumn();
814 // Only the first line needs the character position adjusted.
815 return (rawLine != 0)
816 ? rawCol : rawCol + prefixPosition.getColumn();
817 }
818
819 /**
820 * @return Whether m1 ends before m2 starts.
821 */
822 private boolean isOverlapped(Mapping m1, Mapping m2) {
823 // No need to use adjusted values here, relative positions are sufficient.
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 * Write any needed entries from the current position to the end of the
834 * provided mapping.
835 */
836 private void maybeVisit(MappingVisitor v, Mapping m) throws IOException {
837 int nextLine = getAdjustedLine(m.endPosition);
838 int nextCol = getAdjustedCol(m.endPosition);
839 // If this anything remaining in this mapping beyond the
840 // current line and column position, write it out now.
841 if (line < nextLine || (line == nextLine && col < nextCol)) {
842 visit(v, m, nextLine, nextCol);
843 }
844 }
845
846 /**
847 * Write any needed entries to complete the provided mapping.
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 // If the previous value is null, no mapping exists.
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 * Write any entries needed between the current position the next position
862 * and update the current position.
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 // Nothing to do.
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 }