Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(692)

Side by Side Diff: lib/parser.dart

Issue 2560623003: Support a new source map bundle format useful for the Dart Dev Compiler. A source map bundle is a J… (Closed)
Patch Set: fix strong mode errors Created 4 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file 1 // Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file
2 // for details. All rights reserved. Use of this source code is governed by a 2 // for details. All rights reserved. Use of this source code is governed by a
3 // BSD-style license that can be found in the LICENSE file. 3 // BSD-style license that can be found in the LICENSE file.
4 4
5 /// Contains the top-level function to parse source maps version 3. 5 /// Contains the top-level function to parse source maps version 3.
6 library source_maps.parser; 6 library source_maps.parser;
7 7
8 import 'dart:collection'; 8 import 'dart:collection';
9 import 'dart:convert'; 9 import 'dart:convert';
10 10
11 import 'package:path/path.dart' as path;
11 import 'package:source_span/source_span.dart'; 12 import 'package:source_span/source_span.dart';
12 13
13 import 'builder.dart' as builder; 14 import 'builder.dart' as builder;
14 import 'src/source_map_span.dart'; 15 import 'src/source_map_span.dart';
15 import 'src/utils.dart'; 16 import 'src/utils.dart';
16 import 'src/vlq.dart'; 17 import 'src/vlq.dart';
17 18
18 /// Parses a source map directly from a json string. 19 /// Parses a source map directly from a json string.
19 /// 20 ///
20 /// [mapUrl], which may be either a [String] or a [Uri], indicates the URL of 21 /// [mapUrl], which may be either a [String] or a [Uri], indicates the URL of
21 /// the source map file itself. If it's passed, any URLs in the source 22 /// the source map file itself. If it's passed, any URLs in the source
22 /// map will be interpreted as relative to this URL when generating spans. 23 /// map will be interpreted as relative to this URL when generating spans.
23 // TODO(sigmund): evaluate whether other maps should have the json parsed, or 24 // TODO(sigmund): evaluate whether other maps should have the json parsed, or
24 // the string represenation. 25 // the string represenation.
25 // TODO(tjblasi): Ignore the first line of [jsonMap] if the JSON safety string 26 // TODO(tjblasi): Ignore the first line of [jsonMap] if the JSON safety string
26 // `)]}'` begins the string representation of the map. 27 // `)]}'` begins the string representation of the map.
27 Mapping parse(String jsonMap, {Map<String, Map> otherMaps, mapUrl}) => 28 Mapping parse(String jsonMap, {Map<String, Map> otherMaps, mapUrl}) =>
28 parseJson(JSON.decode(jsonMap), otherMaps: otherMaps, mapUrl: mapUrl); 29 parseJson(JSON.decode(jsonMap), otherMaps: otherMaps, mapUrl: mapUrl);
29 30
30 /// Parses a source map directly from a json map object. 31 /// Parses a source map or source map bundle directly from a json object.
31 /// 32 ///
32 /// [mapUrl], which may be either a [String] or a [Uri], indicates the URL of 33 /// [mapUrl], which may be either a [String] or a [Uri], indicates the URL of
33 /// the source map file itself. If it's passed, any URLs in the source 34 /// the source map file itself. If it's passed, any URLs in the source
34 /// map will be interpreted as relative to this URL when generating spans. 35 /// map will be interpreted as relative to this URL when generating spans.
35 Mapping parseJson(Map map, {Map<String, Map> otherMaps, mapUrl}) { 36 Mapping parseJson(/*Map|List*/ json, {Map<String, Map> otherMaps, mapUrl}) {
Siggi Cherem (dart-lang) 2016/12/07 18:06:22 Since this is not part of the standard source-map
Jacob 2016/12/07 20:40:31 added parseJsonExtended method that parses the ext
37 if (json is List) {
38 return new MappingBundle.fromJson(json, mapUrl: mapUrl);
39 }
40 Map map = json;
36 if (map['version'] != 3) { 41 if (map['version'] != 3) {
37 throw new ArgumentError( 42 throw new ArgumentError('unexpected source map version: ${map["version"]}. '
38 'unexpected source map version: ${map["version"]}. '
39 'Only version 3 is supported.'); 43 'Only version 3 is supported.');
40 } 44 }
41 45
42 if (map.containsKey('sections')) { 46 if (map.containsKey('sections')) {
43 if (map.containsKey('mappings') || map.containsKey('sources') || 47 if (map.containsKey('mappings') ||
48 map.containsKey('sources') ||
44 map.containsKey('names')) { 49 map.containsKey('names')) {
45 throw new FormatException('map containing "sections" ' 50 throw new FormatException('map containing "sections" '
46 'cannot contain "mappings", "sources", or "names".'); 51 'cannot contain "mappings", "sources", or "names".');
47 } 52 }
48 return new MultiSectionMapping.fromJson(map['sections'], otherMaps, 53 return new MultiSectionMapping.fromJson(map['sections'], otherMaps,
49 mapUrl: mapUrl); 54 mapUrl: mapUrl);
50 } 55 }
51 return new SingleMapping.fromJson(map, mapUrl: mapUrl); 56 return new SingleMapping.fromJson(map, mapUrl: mapUrl);
52 } 57 }
53 58
54
55 /// A mapping parsed out of a source map. 59 /// A mapping parsed out of a source map.
56 abstract class Mapping { 60 abstract class Mapping {
57 /// Returns the span associated with [line] and [column]. 61 /// Returns the span associated with [line] and [column].
58 SourceMapSpan spanFor(int line, int column, {Map<String, SourceFile> files}); 62 /// [uri] is the optional location of the output file to find the span for
Siggi Cherem (dart-lang) 2016/12/07 18:06:22 Assuming we agree with splitting the APIs, I'd rem
Jacob 2016/12/07 20:40:31 discussed offline. kept but checked.
63 /// to disambiguate cases where a mapping may have different mappings for
64 /// different output files.
65 SourceMapSpan spanFor(int line, int column,
66 {Map<String, SourceFile> files, String uri});
59 67
60 /// Returns the span associated with [location]. 68 /// Returns the span associated with [location].
61 SourceMapSpan spanForLocation(SourceLocation location, 69 SourceMapSpan spanForLocation(SourceLocation location,
62 {Map<String, SourceFile> files}) { 70 {Map<String, SourceFile> files, String uri}) {
Jacob 2016/12/07 20:40:31 removed uri from this method as SourceLocation spe
63 return spanFor(location.line, location.column, files: files); 71 return spanFor(location.line, location.column, files: files, uri: uri);
64 } 72 }
65 } 73 }
66 74
67 /// A meta-level map containing sections. 75 /// A meta-level map containing sections.
68 class MultiSectionMapping extends Mapping { 76 class MultiSectionMapping extends Mapping {
69 /// For each section, the start line offset. 77 /// For each section, the start line offset.
70 final List<int> _lineStart = <int>[]; 78 final List<int> _lineStart = <int>[];
71 79
72 /// For each section, the start column offset. 80 /// For each section, the start column offset.
73 final List<int> _columnStart = <int>[]; 81 final List<int> _columnStart = <int>[];
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after
109 } else { 117 } else {
110 throw new FormatException('section missing url or map'); 118 throw new FormatException('section missing url or map');
111 } 119 }
112 } 120 }
113 if (_lineStart.length == 0) { 121 if (_lineStart.length == 0) {
114 throw new FormatException('expected at least one section'); 122 throw new FormatException('expected at least one section');
115 } 123 }
116 } 124 }
117 125
118 int _indexFor(line, column) { 126 int _indexFor(line, column) {
119 for(int i = 0; i < _lineStart.length; i++) { 127 for (int i = 0; i < _lineStart.length; i++) {
120 if (line < _lineStart[i]) return i - 1; 128 if (line < _lineStart[i]) return i - 1;
121 if (line == _lineStart[i] && column < _columnStart[i]) return i - 1; 129 if (line == _lineStart[i] && column < _columnStart[i]) return i - 1;
122 } 130 }
123 return _lineStart.length - 1; 131 return _lineStart.length - 1;
124 } 132 }
125 133
126 SourceMapSpan spanFor(int line, int column, {Map<String, SourceFile> files}) { 134 SourceMapSpan spanFor(int line, int column,
135 {Map<String, SourceFile> files, String uri}) {
136 // TODO(jacobr): perhaps verify that targetUrl matches the actual uri
137 // or at least ends in the same file name.
127 int index = _indexFor(line, column); 138 int index = _indexFor(line, column);
128 return _maps[index].spanFor( 139 return _maps[index].spanFor(
129 line - _lineStart[index], column - _columnStart[index], files: files); 140 line - _lineStart[index], column - _columnStart[index],
141 files: files);
130 } 142 }
131 143
132 String toString() { 144 String toString() {
133 var buff = new StringBuffer("$runtimeType : ["); 145 var buff = new StringBuffer("$runtimeType : [");
134 for (int i = 0; i < _lineStart.length; i++) { 146 for (int i = 0; i < _lineStart.length; i++) {
135 buff..write('(') 147 buff
136 ..write(_lineStart[i]) 148 ..write('(')
137 ..write(',') 149 ..write(_lineStart[i])
138 ..write(_columnStart[i]) 150 ..write(',')
139 ..write(':') 151 ..write(_columnStart[i])
140 ..write(_maps[i]) 152 ..write(':')
141 ..write(')'); 153 ..write(_maps[i])
154 ..write(')');
142 } 155 }
143 buff.write(']'); 156 buff.write(']');
144 return buff.toString(); 157 return buff.toString();
145 } 158 }
146 } 159 }
147 160
161 class MappingBundle extends Mapping {
162 Map<String, SingleMapping> _mappings = {};
163
164 MappingBundle.fromJson(List json, {String mapUrl}) {
165 for (var map in json) {
166 var mapping = new SingleMapping.fromJson(map, mapUrl: mapUrl);
Siggi Cherem (dart-lang) 2016/12/07 18:06:22 maybe use `parseJson` so we get the validation we
Jacob 2016/12/07 20:40:31 Done. Note we cast to SingleMapping as only Single
167 var targetUrl = mapping.targetUrl;
168 _mappings[targetUrl] = mapping;
169 }
170 }
171
172 /// Encodes the Mapping mappings as a json map.
173 List toJson() => _mappings.values.map((v) => v.toJson()).toList();
174
175 String toString() {
176 var buff = new StringBuffer();
177 for (var map in _mappings.values) {
178 buff.write(map.toString());
179 }
180 return buff.toString();
181 }
182
183 SourceMapSpan spanFor(int line, int column,
184 {Map<String, SourceFile> files, String uri}) {
Siggi Cherem (dart-lang) 2016/12/07 18:06:22 2 suggestions: - make uri mandatory - rename it
Jacob 2016/12/07 20:40:31 discussed offline. added check that it is not null
185 if (_mappings.containsKey(uri)) {
Siggi Cherem (dart-lang) 2016/12/07 18:06:22 check that `uri` is not null?
Jacob 2016/12/07 20:40:31 Done.
186 return _mappings[uri].spanFor(line, column, files: files, uri: uri);
187 }
188 // Fall back to looking up the source map on just the basename.
189 var name = path.basename(uri.toString());
190 if (_mappings.containsKey(name)) {
191 return _mappings[name].spanFor(line, column, files: files, uri: name);
192 }
193 return null;
194 }
195 }
196
148 /// A map containing direct source mappings. 197 /// A map containing direct source mappings.
149 class SingleMapping extends Mapping { 198 class SingleMapping extends Mapping {
150 /// Source urls used in the mapping, indexed by id. 199 /// Source urls used in the mapping, indexed by id.
151 final List<String> urls; 200 final List<String> urls;
152 201
153 /// Source names used in the mapping, indexed by id. 202 /// Source names used in the mapping, indexed by id.
154 final List<String> names; 203 final List<String> names;
155 204
156 /// Entries indicating the beginning of each span. 205 /// Entries indicating the beginning of each span.
157 final List<TargetLineEntry> lines; 206 final List<TargetLineEntry> lines;
158 207
159 /// Url of the target file. 208 /// Url of the target file.
160 String targetUrl; 209 String targetUrl;
161 210
162 /// Source root prepended to all entries in [urls]. 211 /// Source root prepended to all entries in [urls].
163 String sourceRoot; 212 String sourceRoot;
164 213
165 final Uri _mapUrl; 214 final Uri _mapUrl;
166 215
167 SingleMapping._(this.targetUrl, this.urls, this.names, this.lines) 216 SingleMapping._(this.targetUrl, this.urls, this.names, this.lines)
168 : _mapUrl = null; 217 : _mapUrl = null;
169 218
170 factory SingleMapping.fromEntries( 219 factory SingleMapping.fromEntries(Iterable<builder.Entry> entries,
171 Iterable<builder.Entry> entries, [String fileUrl]) { 220 [String fileUrl]) {
172 // The entries needs to be sorted by the target offsets. 221 // The entries needs to be sorted by the target offsets.
173 var sourceEntries = new List.from(entries)..sort(); 222 var sourceEntries = new List.from(entries)..sort();
174 var lines = <TargetLineEntry>[]; 223 var lines = <TargetLineEntry>[];
175 224
176 // Indices associated with file urls that will be part of the source map. We 225 // Indices associated with file urls that will be part of the source map. We
177 // use a linked hash-map so that `_urls.keys[_urls[u]] == u` 226 // use a linked hash-map so that `_urls.keys[_urls[u]] == u`
178 var urls = new LinkedHashMap<String, int>(); 227 var urls = new LinkedHashMap<String, int>();
179 228
180 // Indices associated with identifiers that will be part of the source map. 229 // Indices associated with identifiers that will be part of the source map.
181 // We use a linked hash-map so that `_names.keys[_names[n]] == n` 230 // We use a linked hash-map so that `_names.keys[_names[n]] == n`
182 var names = new LinkedHashMap<String, int>(); 231 var names = new LinkedHashMap<String, int>();
183 232
184 var lineNum; 233 var lineNum;
185 List<TargetEntry> targetEntries; 234 List<TargetEntry> targetEntries;
186 for (var sourceEntry in sourceEntries) { 235 for (var sourceEntry in sourceEntries) {
187 if (lineNum == null || sourceEntry.target.line > lineNum) { 236 if (lineNum == null || sourceEntry.target.line > lineNum) {
188 lineNum = sourceEntry.target.line; 237 lineNum = sourceEntry.target.line;
189 targetEntries = <TargetEntry>[]; 238 targetEntries = <TargetEntry>[];
190 lines.add(new TargetLineEntry(lineNum, targetEntries)); 239 lines.add(new TargetLineEntry(lineNum, targetEntries));
191 } 240 }
192 241
193 if (sourceEntry.source == null) { 242 if (sourceEntry.source == null) {
194 targetEntries.add(new TargetEntry(sourceEntry.target.column)); 243 targetEntries.add(new TargetEntry(sourceEntry.target.column));
195 } else { 244 } else {
196 var sourceUrl = sourceEntry.source.sourceUrl; 245 var sourceUrl = sourceEntry.source.sourceUrl;
197 var urlId = urls.putIfAbsent( 246 var urlId = urls.putIfAbsent(
198 sourceUrl == null ? '' : sourceUrl.toString(), () => urls.length); 247 sourceUrl == null ? '' : sourceUrl.toString(), () => urls.length);
199 var srcNameId = sourceEntry.identifierName == null ? null : 248 var srcNameId = sourceEntry.identifierName == null
200 names.putIfAbsent(sourceEntry.identifierName, () => names.length); 249 ? null
201 targetEntries.add(new TargetEntry( 250 : names.putIfAbsent(sourceEntry.identifierName, () => names.length);
202 sourceEntry.target.column, 251 targetEntries.add(new TargetEntry(sourceEntry.target.column, urlId,
203 urlId, 252 sourceEntry.source.line, sourceEntry.source.column, srcNameId));
204 sourceEntry.source.line,
205 sourceEntry.source.column,
206 srcNameId));
207 } 253 }
208 } 254 }
209 return new SingleMapping._( 255 return new SingleMapping._(
210 fileUrl, urls.keys.toList(), names.keys.toList(), lines); 256 fileUrl, urls.keys.toList(), names.keys.toList(), lines);
211 } 257 }
212 258
213 SingleMapping.fromJson(Map map, {mapUrl}) 259 SingleMapping.fromJson(Map map, {mapUrl})
214 : targetUrl = map['file'], 260 : targetUrl = map['file'],
215 urls = new List<String>.from(map['sources']), 261 urls = new List<String>.from(map['sources']),
216 names = new List<String>.from(map['names']), 262 names = new List<String>.from(map['names']),
(...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after
264 if (!tokenizer.nextKind.isValue) throw _segmentError(3, line); 310 if (!tokenizer.nextKind.isValue) throw _segmentError(3, line);
265 srcColumn += tokenizer._consumeValue(); 311 srcColumn += tokenizer._consumeValue();
266 if (!tokenizer.nextKind.isValue) { 312 if (!tokenizer.nextKind.isValue) {
267 entries.add(new TargetEntry(column, srcUrlId, srcLine, srcColumn)); 313 entries.add(new TargetEntry(column, srcUrlId, srcLine, srcColumn));
268 } else { 314 } else {
269 srcNameId += tokenizer._consumeValue(); 315 srcNameId += tokenizer._consumeValue();
270 if (srcNameId >= names.length) { 316 if (srcNameId >= names.length) {
271 throw new StateError( 317 throw new StateError(
272 'Invalid name id: $targetUrl, $line, $srcNameId'); 318 'Invalid name id: $targetUrl, $line, $srcNameId');
273 } 319 }
274 entries.add(new TargetEntry(column, srcUrlId, srcLine, srcColumn, 320 entries.add(
275 srcNameId)); 321 new TargetEntry(column, srcUrlId, srcLine, srcColumn, srcNameId));
276 } 322 }
277 } 323 }
278 if (tokenizer.nextKind.isNewSegment) tokenizer._consumeNewSegment(); 324 if (tokenizer.nextKind.isNewSegment) tokenizer._consumeNewSegment();
279 } 325 }
280 if (!entries.isEmpty) { 326 if (!entries.isEmpty) {
281 lines.add(new TargetLineEntry(line, entries)); 327 lines.add(new TargetLineEntry(line, entries));
282 } 328 }
283 } 329 }
284 330
285 /// Encodes the Mapping mappings as a json map. 331 /// Encodes the Mapping mappings as a json map.
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after
319 365
320 if (segment.sourceNameId == null) continue; 366 if (segment.sourceNameId == null) continue;
321 srcNameId = _append(buff, srcNameId, segment.sourceNameId); 367 srcNameId = _append(buff, srcNameId, segment.sourceNameId);
322 } 368 }
323 } 369 }
324 370
325 var result = { 371 var result = {
326 'version': 3, 372 'version': 3,
327 'sourceRoot': sourceRoot == null ? '' : sourceRoot, 373 'sourceRoot': sourceRoot == null ? '' : sourceRoot,
328 'sources': urls, 374 'sources': urls,
329 'names' : names, 375 'names': names,
330 'mappings' : buff.toString() 376 'mappings': buff.toString()
331 }; 377 };
332 if (targetUrl != null) { 378 if (targetUrl != null) {
333 result['file'] = targetUrl; 379 result['file'] = targetUrl;
334 } 380 }
335 return result; 381 return result;
336 } 382 }
337 383
338 /// Appends to [buff] a VLQ encoding of [newValue] using the difference 384 /// Appends to [buff] a VLQ encoding of [newValue] using the difference
339 /// between [oldValue] and [newValue] 385 /// between [oldValue] and [newValue]
340 static int _append(StringBuffer buff, int oldValue, int newValue) { 386 static int _append(StringBuffer buff, int oldValue, int newValue) {
341 buff.writeAll(encodeVlq(newValue - oldValue)); 387 buff.writeAll(encodeVlq(newValue - oldValue));
342 return newValue; 388 return newValue;
343 } 389 }
344 390
345 _segmentError(int seen, int line) => new StateError( 391 _segmentError(int seen, int line) =>
346 'Invalid entry in sourcemap, expected 1, 4, or 5' 392 new StateError('Invalid entry in sourcemap, expected 1, 4, or 5'
347 ' values, but got $seen.\ntargeturl: $targetUrl, line: $line'); 393 ' values, but got $seen.\ntargeturl: $targetUrl, line: $line');
348 394
349 /// Returns [TargetLineEntry] which includes the location in the target [line] 395 /// Returns [TargetLineEntry] which includes the location in the target [line]
350 /// number. In particular, the resulting entry is the last entry whose line 396 /// number. In particular, the resulting entry is the last entry whose line
351 /// number is lower or equal to [line]. 397 /// number is lower or equal to [line].
352 TargetLineEntry _findLine(int line) { 398 TargetLineEntry _findLine(int line) {
353 int index = binarySearch(lines, (e) => e.line > line); 399 int index = binarySearch(lines, (e) => e.line > line);
354 return (index <= 0) ? null : lines[index - 1]; 400 return (index <= 0) ? null : lines[index - 1];
355 } 401 }
356 402
357 /// Returns [TargetEntry] which includes the location denoted by 403 /// Returns [TargetEntry] which includes the location denoted by
358 /// [line], [column]. If [lineEntry] corresponds to [line], then this will be 404 /// [line], [column]. If [lineEntry] corresponds to [line], then this will be
359 /// the last entry whose column is lower or equal than [column]. If 405 /// the last entry whose column is lower or equal than [column]. If
360 /// [lineEntry] corresponds to a line prior to [line], then the result will be 406 /// [lineEntry] corresponds to a line prior to [line], then the result will be
361 /// the very last entry on that line. 407 /// the very last entry on that line.
362 TargetEntry _findColumn(int line, int column, TargetLineEntry lineEntry) { 408 TargetEntry _findColumn(int line, int column, TargetLineEntry lineEntry) {
363 if (lineEntry == null || lineEntry.entries.length == 0) return null; 409 if (lineEntry == null || lineEntry.entries.length == 0) return null;
364 if (lineEntry.line != line) return lineEntry.entries.last; 410 if (lineEntry.line != line) return lineEntry.entries.last;
365 var entries = lineEntry.entries; 411 var entries = lineEntry.entries;
366 int index = binarySearch(entries, (e) => e.column > column); 412 int index = binarySearch(entries, (e) => e.column > column);
367 return (index <= 0) ? null : entries[index - 1]; 413 return (index <= 0) ? null : entries[index - 1];
368 } 414 }
369 415
370 SourceMapSpan spanFor(int line, int column, {Map<String, SourceFile> files}) { 416 SourceMapSpan spanFor(int line, int column,
417 {Map<String, SourceFile> files, String uri}) {
371 var entry = _findColumn(line, column, _findLine(line)); 418 var entry = _findColumn(line, column, _findLine(line));
372 if (entry == null || entry.sourceUrlId == null) return null; 419 if (entry == null || entry.sourceUrlId == null) return null;
373 var url = urls[entry.sourceUrlId]; 420 var url = urls[entry.sourceUrlId];
374 if (sourceRoot != null) { 421 if (sourceRoot != null) {
375 url = '${sourceRoot}${url}'; 422 url = '${sourceRoot}${url}';
376 } 423 }
377 if (files != null && files[url] != null) { 424 if (files != null && files[url] != null) {
378 var file = files[url]; 425 var file = files[url];
379 var start = file.getOffset(entry.sourceLine, entry.sourceColumn); 426 var start = file.getOffset(entry.sourceLine, entry.sourceColumn);
380 if (entry.sourceNameId != null) { 427 if (entry.sourceNameId != null) {
(...skipping 14 matching lines...) Expand all
395 if (entry.sourceNameId != null) { 442 if (entry.sourceNameId != null) {
396 return new SourceMapSpan.identifier(start, names[entry.sourceNameId]); 443 return new SourceMapSpan.identifier(start, names[entry.sourceNameId]);
397 } else { 444 } else {
398 return new SourceMapSpan(start, start, ''); 445 return new SourceMapSpan(start, start, '');
399 } 446 }
400 } 447 }
401 } 448 }
402 449
403 String toString() { 450 String toString() {
404 return (new StringBuffer("$runtimeType : [") 451 return (new StringBuffer("$runtimeType : [")
405 ..write('targetUrl: ') 452 ..write('targetUrl: ')
406 ..write(targetUrl) 453 ..write(targetUrl)
407 ..write(', sourceRoot: ') 454 ..write(', sourceRoot: ')
408 ..write(sourceRoot) 455 ..write(sourceRoot)
409 ..write(', urls: ') 456 ..write(', urls: ')
410 ..write(urls) 457 ..write(urls)
411 ..write(', names: ') 458 ..write(', names: ')
412 ..write(names) 459 ..write(names)
413 ..write(', lines: ') 460 ..write(', lines: ')
414 ..write(lines) 461 ..write(lines)
415 ..write(']')).toString(); 462 ..write(']'))
463 .toString();
416 } 464 }
417 465
418 String get debugString { 466 String get debugString {
419 var buff = new StringBuffer(); 467 var buff = new StringBuffer();
420 for (var lineEntry in lines) { 468 for (var lineEntry in lines) {
421 var line = lineEntry.line; 469 var line = lineEntry.line;
422 for (var entry in lineEntry.entries) { 470 for (var entry in lineEntry.entries) {
423 buff..write(targetUrl) 471 buff
472 ..write(targetUrl)
473 ..write(': ')
474 ..write(line)
475 ..write(':')
476 ..write(entry.column);
477 if (entry.sourceUrlId != null) {
478 buff
479 ..write(' --> ')
480 ..write(sourceRoot)
481 ..write(urls[entry.sourceUrlId])
424 ..write(': ') 482 ..write(': ')
425 ..write(line) 483 ..write(entry.sourceLine)
426 ..write(':') 484 ..write(':')
427 ..write(entry.column); 485 ..write(entry.sourceColumn);
428 if (entry.sourceUrlId != null) {
429 buff..write(' --> ')
430 ..write(sourceRoot)
431 ..write(urls[entry.sourceUrlId])
432 ..write(': ')
433 ..write(entry.sourceLine)
434 ..write(':')
435 ..write(entry.sourceColumn);
436 } 486 }
437 if (entry.sourceNameId != null) { 487 if (entry.sourceNameId != null) {
438 buff..write(' (') 488 buff..write(' (')..write(names[entry.sourceNameId])..write(')');
439 ..write(names[entry.sourceNameId])
440 ..write(')');
441 } 489 }
442 buff.write('\n'); 490 buff.write('\n');
443 } 491 }
444 } 492 }
445 return buff.toString(); 493 return buff.toString();
446 } 494 }
447 } 495 }
448 496
449 /// A line entry read from a source map. 497 /// A line entry read from a source map.
450 class TargetLineEntry { 498 class TargetLineEntry {
451 final int line; 499 final int line;
452 List<TargetEntry> entries; 500 List<TargetEntry> entries;
453 TargetLineEntry(this.line, this.entries); 501 TargetLineEntry(this.line, this.entries);
454 502
455 String toString() => '$runtimeType: $line $entries'; 503 String toString() => '$runtimeType: $line $entries';
456 } 504 }
457 505
458 /// A target segment entry read from a source map 506 /// A target segment entry read from a source map
459 class TargetEntry { 507 class TargetEntry {
460 final int column; 508 final int column;
461 final int sourceUrlId; 509 final int sourceUrlId;
462 final int sourceLine; 510 final int sourceLine;
463 final int sourceColumn; 511 final int sourceColumn;
464 final int sourceNameId; 512 final int sourceNameId;
465 513
466 TargetEntry(this.column, [this.sourceUrlId, this.sourceLine, 514 TargetEntry(this.column,
467 this.sourceColumn, this.sourceNameId]); 515 [this.sourceUrlId,
516 this.sourceLine,
517 this.sourceColumn,
518 this.sourceNameId]);
468 519
469 String toString() => '$runtimeType: ' 520 String toString() => '$runtimeType: '
470 '($column, $sourceUrlId, $sourceLine, $sourceColumn, $sourceNameId)'; 521 '($column, $sourceUrlId, $sourceLine, $sourceColumn, $sourceNameId)';
471 } 522 }
472 523
473 /** A character iterator over a string that can peek one character ahead. */ 524 /** A character iterator over a string that can peek one character ahead. */
474 class _MappingTokenizer implements Iterator<String> { 525 class _MappingTokenizer implements Iterator<String> {
475 final String _internal; 526 final String _internal;
476 final int _length; 527 final int _length;
477 int index = -1; 528 int index = -1;
478 _MappingTokenizer(String internal) 529 _MappingTokenizer(String internal)
479 : _internal = internal, 530 : _internal = internal,
480 _length = internal.length; 531 _length = internal.length;
481 532
482 // Iterator API is used by decodeVlq to consume VLQ entries. 533 // Iterator API is used by decodeVlq to consume VLQ entries.
483 bool moveNext() => ++index < _length; 534 bool moveNext() => ++index < _length;
484 String get current => 535 String get current =>
485 (index >= 0 && index < _length) ? _internal[index] : null; 536 (index >= 0 && index < _length) ? _internal[index] : null;
486 537
487 bool get hasTokens => index < _length - 1 && _length > 0; 538 bool get hasTokens => index < _length - 1 && _length > 0;
488 539
489 _TokenKind get nextKind { 540 _TokenKind get nextKind {
490 if (!hasTokens) return _TokenKind.EOF; 541 if (!hasTokens) return _TokenKind.EOF;
491 var next = _internal[index + 1]; 542 var next = _internal[index + 1];
492 if (next == ';') return _TokenKind.LINE; 543 if (next == ';') return _TokenKind.LINE;
493 if (next == ',') return _TokenKind.SEGMENT; 544 if (next == ',') return _TokenKind.SEGMENT;
494 return _TokenKind.VALUE; 545 return _TokenKind.VALUE;
495 } 546 }
496 547
497 int _consumeValue() => decodeVlq(this); 548 int _consumeValue() => decodeVlq(this);
498 void _consumeNewLine() { ++index; } 549 void _consumeNewLine() {
499 void _consumeNewSegment() { ++index; } 550 ++index;
551 }
552
553 void _consumeNewSegment() {
554 ++index;
555 }
500 556
501 // Print the state of the iterator, with colors indicating the current 557 // Print the state of the iterator, with colors indicating the current
502 // position. 558 // position.
503 String toString() { 559 String toString() {
504 var buff = new StringBuffer(); 560 var buff = new StringBuffer();
505 for (int i = 0; i < index; i++) { 561 for (int i = 0; i < index; i++) {
506 buff.write(_internal[i]); 562 buff.write(_internal[i]);
507 } 563 }
508 buff.write(''); 564 buff.write('');
509 buff.write(current == null ? '' : current); 565 buff.write(current == null ? '' : current);
(...skipping 12 matching lines...) Expand all
522 static const _TokenKind EOF = const _TokenKind(isEof: true); 578 static const _TokenKind EOF = const _TokenKind(isEof: true);
523 static const _TokenKind VALUE = const _TokenKind(); 579 static const _TokenKind VALUE = const _TokenKind();
524 final bool isNewLine; 580 final bool isNewLine;
525 final bool isNewSegment; 581 final bool isNewSegment;
526 final bool isEof; 582 final bool isEof;
527 bool get isValue => !isNewLine && !isNewSegment && !isEof; 583 bool get isValue => !isNewLine && !isNewSegment && !isEof;
528 584
529 const _TokenKind( 585 const _TokenKind(
530 {this.isNewLine: false, this.isNewSegment: false, this.isEof: false}); 586 {this.isNewLine: false, this.isNewSegment: false, this.isEof: false});
531 } 587 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698