Debian Codegraph @

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

raw symbols SCIP
1 /*
2 * Copyright 2010 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.collect.Lists;
20
21 import java.util.List;
22
23 /**
24 * Class for parsing the line maps in SourceMap v2.
25 *
26 * @author [email protected] (John Lenz)
27 * @author [email protected] (Joseph Schorr)
28 */
29 class SourceMapLineDecoder {
30
31 /**
32 * Decodes a line in a character map into a list of mapping IDs.
33 */
34 static List<Integer> decodeLine(String lineSource) {
35 return decodeLine(new StringParser(lineSource));
36 }
37
38 private SourceMapLineDecoder() {}
39
40 static LineEntry decodeLineEntry(String in, int lastId) {
41 return decodeLineEntry(new StringParser(in), lastId);
42 }
43
44 private static LineEntry decodeLineEntry(StringParser reader, int lastId) {
45 int repDigits = 0;
46
47 // Determine the number of digits used for the repetition count.
48 // Each "!" indicates another base64 digit.
49 for (char peek = reader.peek(); peek == '!'; peek = reader.peek()) {
50 repDigits++;
51 reader.next(); // consume the "!"
52 }
53
54 int idDigits = 0;
55 int reps = 0;
56 if (repDigits == 0) {
57 // No repetition digit escapes, so the next character represents the
58 // number of digits in the id (bottom 2 bits) and the number of
59 // repetitions (top 4 digits).
60 char digit = reader.next();
61 int value = addBase64Digit(digit, 0);
62 reps = (value >> 2);
63 idDigits = (value & 3);
64 } else {
65 char digit = reader.next();
66 idDigits = addBase64Digit(digit, 0);
67
68 int value = 0;
69 for (int i = 0; i < repDigits; i++) {
70 digit = reader.next();
71 value = addBase64Digit(digit, value);
72 }
73 reps = value;
74 }
75
76 // Adjust for 1 offset encoding.
77 reps += 1;
78 idDigits += 1;
79
80 // Decode the id token.
81 int value = 0;
82 for (int i = 0; i < idDigits; i++) {
83 char digit = reader.next();
84 value = addBase64Digit(digit, value);
85 }
86 int mappingId = getIdFromRelativeId(value, idDigits, lastId);
87 return new LineEntry(mappingId, reps);
88 }
89
90 private static List<Integer> decodeLine(StringParser reader) {
91 List<Integer> result = Lists.newArrayListWithCapacity(512);
92 int lastId = 0;
93 while (reader.hasNext()) {
94 LineEntry entry = decodeLineEntry(reader, lastId);
95 lastId = entry.id;
96
97 for (int i=0; i < entry.reps; i++) {
98 result.add(entry.id);
99 }
100 }
101
102 return result;
103 }
104
105 /**
106 * Build base64 number a digit at a time, most significant digit first.
107 */
108 private static int addBase64Digit(char digit, int previousValue) {
109 return (previousValue * 64) + Base64.fromBase64(digit);
110 }
111
112 /**
113 * @return the id from the relative id.
114 */
115 static int getIdFromRelativeId(int rawId, int digits, int lastId) {
116 // The value range depends on the number of digits
117 int base = 1 << (digits * 6);
118 return ((rawId >= base/2) ? rawId - base : rawId) + lastId;
119 }
120
121 /**
122 * Simple class for tracking a single entry in a line map.
123 */
124 static class LineEntry {
125 final int id;
126 final int reps;
127 public LineEntry(int id, int reps) {
128 this.id = id;
129 this.reps = reps;
130 }
131 }
132
133 /**
134 * A simple class for maintaining the current location
135 * in the input.
136 */
137 static class StringParser {
138 final String content;
139 int current = 0;
140
141 StringParser(String content) {
142 this.content = content;
143 }
144
145 char next() {
146 return content.charAt(current++);
147 }
148
149 char peek() {
150 return content.charAt(current);
151 }
152
153 boolean hasNext() {
154 return current < content.length() -1;
155 }
156 }
157 }