Chromium Code Reviews| OLD | NEW |
|---|---|
| 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 string. |
| 32 /// | |
| 33 /// [mapUrl], which may be either a [String] or a [Uri], indicates the URL of | |
| 34 /// the source map file itself. If it's passed, any URLs in the source | |
| 35 /// map will be interpreted as relative to this URL when generating spans. | |
| 36 Mapping parseExtended(String jsonMap, {Map<String, Map> otherMaps, mapUrl}) => | |
| 37 parseJsonExtended(JSON.decode(jsonMap), | |
| 38 otherMaps: otherMaps, mapUrl: mapUrl); | |
| 39 | |
| 40 /// Parses a source map or source map bundle. | |
| 41 /// | |
| 42 /// [mapUrl], which may be either a [String] or a [Uri], indicates the URL of | |
| 43 /// the source map file itself. If it's passed, any URLs in the source | |
| 44 /// map will be interpreted as relative to this URL when generating spans. | |
| 45 Mapping parseJsonExtended(/*List|Map*/ json, | |
| 46 {Map<String, Map> otherMaps, mapUrl}) { | |
| 47 if (json is List) { | |
| 48 return new MappingBundle.fromJson(json, mapUrl: mapUrl); | |
| 49 } | |
| 50 return parseJson(json as Map); | |
| 51 } | |
| 52 | |
| 53 /// Parses a source map | |
| 31 /// | 54 /// |
| 32 /// [mapUrl], which may be either a [String] or a [Uri], indicates the URL of | 55 /// [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 | 56 /// 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. | 57 /// map will be interpreted as relative to this URL when generating spans. |
| 35 Mapping parseJson(Map map, {Map<String, Map> otherMaps, mapUrl}) { | 58 Mapping parseJson(Map map, {Map<String, Map> otherMaps, mapUrl}) { |
| 36 if (map['version'] != 3) { | 59 if (map['version'] != 3) { |
| 37 throw new ArgumentError( | 60 throw new ArgumentError('unexpected source map version: ${map["version"]}. ' |
| 38 'unexpected source map version: ${map["version"]}. ' | |
| 39 'Only version 3 is supported.'); | 61 'Only version 3 is supported.'); |
| 40 } | 62 } |
| 41 | 63 |
| 42 if (map.containsKey('sections')) { | 64 if (map.containsKey('sections')) { |
| 43 if (map.containsKey('mappings') || map.containsKey('sources') || | 65 if (map.containsKey('mappings') || |
| 66 map.containsKey('sources') || | |
| 44 map.containsKey('names')) { | 67 map.containsKey('names')) { |
| 45 throw new FormatException('map containing "sections" ' | 68 throw new FormatException('map containing "sections" ' |
| 46 'cannot contain "mappings", "sources", or "names".'); | 69 'cannot contain "mappings", "sources", or "names".'); |
| 47 } | 70 } |
| 48 return new MultiSectionMapping.fromJson(map['sections'], otherMaps, | 71 return new MultiSectionMapping.fromJson(map['sections'], otherMaps, |
| 49 mapUrl: mapUrl); | 72 mapUrl: mapUrl); |
| 50 } | 73 } |
| 51 return new SingleMapping.fromJson(map, mapUrl: mapUrl); | 74 return new SingleMapping.fromJson(map, mapUrl: mapUrl); |
| 52 } | 75 } |
| 53 | 76 |
| 54 | |
| 55 /// A mapping parsed out of a source map. | 77 /// A mapping parsed out of a source map. |
| 56 abstract class Mapping { | 78 abstract class Mapping { |
| 57 /// Returns the span associated with [line] and [column]. | 79 /// Returns the span associated with [line] and [column]. |
| 58 SourceMapSpan spanFor(int line, int column, {Map<String, SourceFile> files}); | 80 /// [uri] is the optional location of the output file to find the span for |
|
Siggi Cherem (dart-lang)
2016/12/07 23:27:00
dartdoc nit: add an empty line above this one.
Jacob
2016/12/08 16:20:21
Done.
| |
| 81 /// to disambiguate cases where a mapping may have different mappings for | |
| 82 /// different output files. | |
| 83 SourceMapSpan spanFor(int line, int column, | |
| 84 {Map<String, SourceFile> files, String uri}); | |
| 59 | 85 |
| 60 /// Returns the span associated with [location]. | 86 /// Returns the span associated with [location]. |
| 61 SourceMapSpan spanForLocation(SourceLocation location, | 87 SourceMapSpan spanForLocation(SourceLocation location, |
| 62 {Map<String, SourceFile> files}) { | 88 {Map<String, SourceFile> files}) { |
| 63 return spanFor(location.line, location.column, files: files); | 89 return spanFor(location.line, location.column, |
| 90 uri: location.sourceUrl?.toString(), files: files); | |
| 64 } | 91 } |
| 65 } | 92 } |
| 66 | 93 |
| 67 /// A meta-level map containing sections. | 94 /// A meta-level map containing sections. |
| 68 class MultiSectionMapping extends Mapping { | 95 class MultiSectionMapping extends Mapping { |
| 69 /// For each section, the start line offset. | 96 /// For each section, the start line offset. |
| 70 final List<int> _lineStart = <int>[]; | 97 final List<int> _lineStart = <int>[]; |
| 71 | 98 |
| 72 /// For each section, the start column offset. | 99 /// For each section, the start column offset. |
| 73 final List<int> _columnStart = <int>[]; | 100 final List<int> _columnStart = <int>[]; |
| (...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 109 } else { | 136 } else { |
| 110 throw new FormatException('section missing url or map'); | 137 throw new FormatException('section missing url or map'); |
| 111 } | 138 } |
| 112 } | 139 } |
| 113 if (_lineStart.length == 0) { | 140 if (_lineStart.length == 0) { |
| 114 throw new FormatException('expected at least one section'); | 141 throw new FormatException('expected at least one section'); |
| 115 } | 142 } |
| 116 } | 143 } |
| 117 | 144 |
| 118 int _indexFor(line, column) { | 145 int _indexFor(line, column) { |
| 119 for(int i = 0; i < _lineStart.length; i++) { | 146 for (int i = 0; i < _lineStart.length; i++) { |
| 120 if (line < _lineStart[i]) return i - 1; | 147 if (line < _lineStart[i]) return i - 1; |
| 121 if (line == _lineStart[i] && column < _columnStart[i]) return i - 1; | 148 if (line == _lineStart[i] && column < _columnStart[i]) return i - 1; |
| 122 } | 149 } |
| 123 return _lineStart.length - 1; | 150 return _lineStart.length - 1; |
| 124 } | 151 } |
| 125 | 152 |
| 126 SourceMapSpan spanFor(int line, int column, {Map<String, SourceFile> files}) { | 153 SourceMapSpan spanFor(int line, int column, |
| 154 {Map<String, SourceFile> files, String uri}) { | |
| 155 // TODO(jacobr): perhaps verify that targetUrl matches the actual uri | |
| 156 // or at least ends in the same file name. | |
| 127 int index = _indexFor(line, column); | 157 int index = _indexFor(line, column); |
| 128 return _maps[index].spanFor( | 158 return _maps[index].spanFor( |
| 129 line - _lineStart[index], column - _columnStart[index], files: files); | 159 line - _lineStart[index], column - _columnStart[index], |
| 160 files: files); | |
| 130 } | 161 } |
| 131 | 162 |
| 132 String toString() { | 163 String toString() { |
| 133 var buff = new StringBuffer("$runtimeType : ["); | 164 var buff = new StringBuffer("$runtimeType : ["); |
| 134 for (int i = 0; i < _lineStart.length; i++) { | 165 for (int i = 0; i < _lineStart.length; i++) { |
| 135 buff..write('(') | 166 buff |
| 136 ..write(_lineStart[i]) | 167 ..write('(') |
| 137 ..write(',') | 168 ..write(_lineStart[i]) |
| 138 ..write(_columnStart[i]) | 169 ..write(',') |
| 139 ..write(':') | 170 ..write(_columnStart[i]) |
| 140 ..write(_maps[i]) | 171 ..write(':') |
| 141 ..write(')'); | 172 ..write(_maps[i]) |
| 173 ..write(')'); | |
| 142 } | 174 } |
| 143 buff.write(']'); | 175 buff.write(']'); |
| 144 return buff.toString(); | 176 return buff.toString(); |
| 145 } | 177 } |
| 146 } | 178 } |
| 147 | 179 |
| 180 class MappingBundle extends Mapping { | |
| 181 Map<String, SingleMapping> _mappings = {}; | |
| 182 | |
| 183 MappingBundle.fromJson(List json, {String mapUrl}) { | |
| 184 for (var map in json) { | |
| 185 var mapping = parseJson(map, mapUrl: mapUrl) as SingleMapping; | |
| 186 var targetUrl = mapping.targetUrl; | |
| 187 _mappings[targetUrl] = mapping; | |
| 188 } | |
| 189 } | |
| 190 | |
| 191 /// Encodes the Mapping mappings as a json map. | |
| 192 List toJson() => _mappings.values.map((v) => v.toJson()).toList(); | |
| 193 | |
| 194 String toString() { | |
| 195 var buff = new StringBuffer(); | |
| 196 for (var map in _mappings.values) { | |
| 197 buff.write(map.toString()); | |
| 198 } | |
| 199 return buff.toString(); | |
| 200 } | |
| 201 | |
| 202 SourceMapSpan spanFor(int line, int column, | |
| 203 {Map<String, SourceFile> files, String uri}) { | |
| 204 if (uri == null) { | |
| 205 throw new ArgumentError.notNull('uri'); | |
| 206 } | |
| 207 if (_mappings.containsKey(uri)) { | |
| 208 return _mappings[uri].spanFor(line, column, files: files, uri: uri); | |
| 209 } | |
| 210 // Fall back to looking up the source map on just the basename. | |
| 211 var name = path.basename(uri.toString()); | |
| 212 if (_mappings.containsKey(name)) { | |
| 213 return _mappings[name].spanFor(line, column, files: files, uri: name); | |
| 214 } | |
| 215 return null; | |
| 216 } | |
| 217 } | |
| 218 | |
| 148 /// A map containing direct source mappings. | 219 /// A map containing direct source mappings. |
| 149 class SingleMapping extends Mapping { | 220 class SingleMapping extends Mapping { |
| 150 /// Source urls used in the mapping, indexed by id. | 221 /// Source urls used in the mapping, indexed by id. |
| 151 final List<String> urls; | 222 final List<String> urls; |
| 152 | 223 |
| 153 /// Source names used in the mapping, indexed by id. | 224 /// Source names used in the mapping, indexed by id. |
| 154 final List<String> names; | 225 final List<String> names; |
| 155 | 226 |
| 156 /// Entries indicating the beginning of each span. | 227 /// Entries indicating the beginning of each span. |
| 157 final List<TargetLineEntry> lines; | 228 final List<TargetLineEntry> lines; |
| 158 | 229 |
| 159 /// Url of the target file. | 230 /// Url of the target file. |
| 160 String targetUrl; | 231 String targetUrl; |
| 161 | 232 |
| 162 /// Source root prepended to all entries in [urls]. | 233 /// Source root prepended to all entries in [urls]. |
| 163 String sourceRoot; | 234 String sourceRoot; |
| 164 | 235 |
| 165 final Uri _mapUrl; | 236 final Uri _mapUrl; |
| 166 | 237 |
| 167 SingleMapping._(this.targetUrl, this.urls, this.names, this.lines) | 238 SingleMapping._(this.targetUrl, this.urls, this.names, this.lines) |
| 168 : _mapUrl = null; | 239 : _mapUrl = null; |
| 169 | 240 |
| 170 factory SingleMapping.fromEntries( | 241 factory SingleMapping.fromEntries(Iterable<builder.Entry> entries, |
| 171 Iterable<builder.Entry> entries, [String fileUrl]) { | 242 [String fileUrl]) { |
| 172 // The entries needs to be sorted by the target offsets. | 243 // The entries needs to be sorted by the target offsets. |
| 173 var sourceEntries = new List.from(entries)..sort(); | 244 var sourceEntries = new List.from(entries)..sort(); |
| 174 var lines = <TargetLineEntry>[]; | 245 var lines = <TargetLineEntry>[]; |
| 175 | 246 |
| 176 // Indices associated with file urls that will be part of the source map. We | 247 // 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` | 248 // use a linked hash-map so that `_urls.keys[_urls[u]] == u` |
| 178 var urls = new LinkedHashMap<String, int>(); | 249 var urls = new LinkedHashMap<String, int>(); |
| 179 | 250 |
| 180 // Indices associated with identifiers that will be part of the source map. | 251 // 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` | 252 // We use a linked hash-map so that `_names.keys[_names[n]] == n` |
| 182 var names = new LinkedHashMap<String, int>(); | 253 var names = new LinkedHashMap<String, int>(); |
| 183 | 254 |
| 184 var lineNum; | 255 var lineNum; |
| 185 List<TargetEntry> targetEntries; | 256 List<TargetEntry> targetEntries; |
| 186 for (var sourceEntry in sourceEntries) { | 257 for (var sourceEntry in sourceEntries) { |
| 187 if (lineNum == null || sourceEntry.target.line > lineNum) { | 258 if (lineNum == null || sourceEntry.target.line > lineNum) { |
| 188 lineNum = sourceEntry.target.line; | 259 lineNum = sourceEntry.target.line; |
| 189 targetEntries = <TargetEntry>[]; | 260 targetEntries = <TargetEntry>[]; |
| 190 lines.add(new TargetLineEntry(lineNum, targetEntries)); | 261 lines.add(new TargetLineEntry(lineNum, targetEntries)); |
| 191 } | 262 } |
| 192 | 263 |
| 193 if (sourceEntry.source == null) { | 264 if (sourceEntry.source == null) { |
| 194 targetEntries.add(new TargetEntry(sourceEntry.target.column)); | 265 targetEntries.add(new TargetEntry(sourceEntry.target.column)); |
| 195 } else { | 266 } else { |
| 196 var sourceUrl = sourceEntry.source.sourceUrl; | 267 var sourceUrl = sourceEntry.source.sourceUrl; |
| 197 var urlId = urls.putIfAbsent( | 268 var urlId = urls.putIfAbsent( |
| 198 sourceUrl == null ? '' : sourceUrl.toString(), () => urls.length); | 269 sourceUrl == null ? '' : sourceUrl.toString(), () => urls.length); |
| 199 var srcNameId = sourceEntry.identifierName == null ? null : | 270 var srcNameId = sourceEntry.identifierName == null |
| 200 names.putIfAbsent(sourceEntry.identifierName, () => names.length); | 271 ? null |
| 201 targetEntries.add(new TargetEntry( | 272 : names.putIfAbsent(sourceEntry.identifierName, () => names.length); |
| 202 sourceEntry.target.column, | 273 targetEntries.add(new TargetEntry(sourceEntry.target.column, urlId, |
| 203 urlId, | 274 sourceEntry.source.line, sourceEntry.source.column, srcNameId)); |
| 204 sourceEntry.source.line, | |
| 205 sourceEntry.source.column, | |
| 206 srcNameId)); | |
| 207 } | 275 } |
| 208 } | 276 } |
| 209 return new SingleMapping._( | 277 return new SingleMapping._( |
| 210 fileUrl, urls.keys.toList(), names.keys.toList(), lines); | 278 fileUrl, urls.keys.toList(), names.keys.toList(), lines); |
| 211 } | 279 } |
| 212 | 280 |
| 213 SingleMapping.fromJson(Map map, {mapUrl}) | 281 SingleMapping.fromJson(Map map, {mapUrl}) |
| 214 : targetUrl = map['file'], | 282 : targetUrl = map['file'], |
| 215 urls = new List<String>.from(map['sources']), | 283 urls = new List<String>.from(map['sources']), |
| 216 names = new List<String>.from(map['names']), | 284 names = new List<String>.from(map['names']), |
| (...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 264 if (!tokenizer.nextKind.isValue) throw _segmentError(3, line); | 332 if (!tokenizer.nextKind.isValue) throw _segmentError(3, line); |
| 265 srcColumn += tokenizer._consumeValue(); | 333 srcColumn += tokenizer._consumeValue(); |
| 266 if (!tokenizer.nextKind.isValue) { | 334 if (!tokenizer.nextKind.isValue) { |
| 267 entries.add(new TargetEntry(column, srcUrlId, srcLine, srcColumn)); | 335 entries.add(new TargetEntry(column, srcUrlId, srcLine, srcColumn)); |
| 268 } else { | 336 } else { |
| 269 srcNameId += tokenizer._consumeValue(); | 337 srcNameId += tokenizer._consumeValue(); |
| 270 if (srcNameId >= names.length) { | 338 if (srcNameId >= names.length) { |
| 271 throw new StateError( | 339 throw new StateError( |
| 272 'Invalid name id: $targetUrl, $line, $srcNameId'); | 340 'Invalid name id: $targetUrl, $line, $srcNameId'); |
| 273 } | 341 } |
| 274 entries.add(new TargetEntry(column, srcUrlId, srcLine, srcColumn, | 342 entries.add( |
| 275 srcNameId)); | 343 new TargetEntry(column, srcUrlId, srcLine, srcColumn, srcNameId)); |
| 276 } | 344 } |
| 277 } | 345 } |
| 278 if (tokenizer.nextKind.isNewSegment) tokenizer._consumeNewSegment(); | 346 if (tokenizer.nextKind.isNewSegment) tokenizer._consumeNewSegment(); |
| 279 } | 347 } |
| 280 if (!entries.isEmpty) { | 348 if (!entries.isEmpty) { |
| 281 lines.add(new TargetLineEntry(line, entries)); | 349 lines.add(new TargetLineEntry(line, entries)); |
| 282 } | 350 } |
| 283 } | 351 } |
| 284 | 352 |
| 285 /// Encodes the Mapping mappings as a json map. | 353 /// Encodes the Mapping mappings as a json map. |
| (...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 319 | 387 |
| 320 if (segment.sourceNameId == null) continue; | 388 if (segment.sourceNameId == null) continue; |
| 321 srcNameId = _append(buff, srcNameId, segment.sourceNameId); | 389 srcNameId = _append(buff, srcNameId, segment.sourceNameId); |
| 322 } | 390 } |
| 323 } | 391 } |
| 324 | 392 |
| 325 var result = { | 393 var result = { |
| 326 'version': 3, | 394 'version': 3, |
| 327 'sourceRoot': sourceRoot == null ? '' : sourceRoot, | 395 'sourceRoot': sourceRoot == null ? '' : sourceRoot, |
| 328 'sources': urls, | 396 'sources': urls, |
| 329 'names' : names, | 397 'names': names, |
| 330 'mappings' : buff.toString() | 398 'mappings': buff.toString() |
| 331 }; | 399 }; |
| 332 if (targetUrl != null) { | 400 if (targetUrl != null) { |
| 333 result['file'] = targetUrl; | 401 result['file'] = targetUrl; |
| 334 } | 402 } |
| 335 return result; | 403 return result; |
| 336 } | 404 } |
| 337 | 405 |
| 338 /// Appends to [buff] a VLQ encoding of [newValue] using the difference | 406 /// Appends to [buff] a VLQ encoding of [newValue] using the difference |
| 339 /// between [oldValue] and [newValue] | 407 /// between [oldValue] and [newValue] |
| 340 static int _append(StringBuffer buff, int oldValue, int newValue) { | 408 static int _append(StringBuffer buff, int oldValue, int newValue) { |
| 341 buff.writeAll(encodeVlq(newValue - oldValue)); | 409 buff.writeAll(encodeVlq(newValue - oldValue)); |
| 342 return newValue; | 410 return newValue; |
| 343 } | 411 } |
| 344 | 412 |
| 345 _segmentError(int seen, int line) => new StateError( | 413 _segmentError(int seen, int line) => |
| 346 'Invalid entry in sourcemap, expected 1, 4, or 5' | 414 new StateError('Invalid entry in sourcemap, expected 1, 4, or 5' |
| 347 ' values, but got $seen.\ntargeturl: $targetUrl, line: $line'); | 415 ' values, but got $seen.\ntargeturl: $targetUrl, line: $line'); |
| 348 | 416 |
| 349 /// Returns [TargetLineEntry] which includes the location in the target [line] | 417 /// Returns [TargetLineEntry] which includes the location in the target [line] |
| 350 /// number. In particular, the resulting entry is the last entry whose line | 418 /// number. In particular, the resulting entry is the last entry whose line |
| 351 /// number is lower or equal to [line]. | 419 /// number is lower or equal to [line]. |
| 352 TargetLineEntry _findLine(int line) { | 420 TargetLineEntry _findLine(int line) { |
| 353 int index = binarySearch(lines, (e) => e.line > line); | 421 int index = binarySearch(lines, (e) => e.line > line); |
| 354 return (index <= 0) ? null : lines[index - 1]; | 422 return (index <= 0) ? null : lines[index - 1]; |
| 355 } | 423 } |
| 356 | 424 |
| 357 /// Returns [TargetEntry] which includes the location denoted by | 425 /// Returns [TargetEntry] which includes the location denoted by |
| 358 /// [line], [column]. If [lineEntry] corresponds to [line], then this will be | 426 /// [line], [column]. If [lineEntry] corresponds to [line], then this will be |
| 359 /// the last entry whose column is lower or equal than [column]. If | 427 /// 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 | 428 /// [lineEntry] corresponds to a line prior to [line], then the result will be |
| 361 /// the very last entry on that line. | 429 /// the very last entry on that line. |
| 362 TargetEntry _findColumn(int line, int column, TargetLineEntry lineEntry) { | 430 TargetEntry _findColumn(int line, int column, TargetLineEntry lineEntry) { |
| 363 if (lineEntry == null || lineEntry.entries.length == 0) return null; | 431 if (lineEntry == null || lineEntry.entries.length == 0) return null; |
| 364 if (lineEntry.line != line) return lineEntry.entries.last; | 432 if (lineEntry.line != line) return lineEntry.entries.last; |
| 365 var entries = lineEntry.entries; | 433 var entries = lineEntry.entries; |
| 366 int index = binarySearch(entries, (e) => e.column > column); | 434 int index = binarySearch(entries, (e) => e.column > column); |
| 367 return (index <= 0) ? null : entries[index - 1]; | 435 return (index <= 0) ? null : entries[index - 1]; |
| 368 } | 436 } |
| 369 | 437 |
| 370 SourceMapSpan spanFor(int line, int column, {Map<String, SourceFile> files}) { | 438 SourceMapSpan spanFor(int line, int column, |
| 439 {Map<String, SourceFile> files, String uri}) { | |
| 371 var entry = _findColumn(line, column, _findLine(line)); | 440 var entry = _findColumn(line, column, _findLine(line)); |
| 372 if (entry == null || entry.sourceUrlId == null) return null; | 441 if (entry == null || entry.sourceUrlId == null) return null; |
| 373 var url = urls[entry.sourceUrlId]; | 442 var url = urls[entry.sourceUrlId]; |
| 374 if (sourceRoot != null) { | 443 if (sourceRoot != null) { |
| 375 url = '${sourceRoot}${url}'; | 444 url = '${sourceRoot}${url}'; |
| 376 } | 445 } |
| 377 if (files != null && files[url] != null) { | 446 if (files != null && files[url] != null) { |
| 378 var file = files[url]; | 447 var file = files[url]; |
| 379 var start = file.getOffset(entry.sourceLine, entry.sourceColumn); | 448 var start = file.getOffset(entry.sourceLine, entry.sourceColumn); |
| 380 if (entry.sourceNameId != null) { | 449 if (entry.sourceNameId != null) { |
| (...skipping 14 matching lines...) Expand all Loading... | |
| 395 if (entry.sourceNameId != null) { | 464 if (entry.sourceNameId != null) { |
| 396 return new SourceMapSpan.identifier(start, names[entry.sourceNameId]); | 465 return new SourceMapSpan.identifier(start, names[entry.sourceNameId]); |
| 397 } else { | 466 } else { |
| 398 return new SourceMapSpan(start, start, ''); | 467 return new SourceMapSpan(start, start, ''); |
| 399 } | 468 } |
| 400 } | 469 } |
| 401 } | 470 } |
| 402 | 471 |
| 403 String toString() { | 472 String toString() { |
| 404 return (new StringBuffer("$runtimeType : [") | 473 return (new StringBuffer("$runtimeType : [") |
| 405 ..write('targetUrl: ') | 474 ..write('targetUrl: ') |
| 406 ..write(targetUrl) | 475 ..write(targetUrl) |
| 407 ..write(', sourceRoot: ') | 476 ..write(', sourceRoot: ') |
| 408 ..write(sourceRoot) | 477 ..write(sourceRoot) |
| 409 ..write(', urls: ') | 478 ..write(', urls: ') |
| 410 ..write(urls) | 479 ..write(urls) |
| 411 ..write(', names: ') | 480 ..write(', names: ') |
| 412 ..write(names) | 481 ..write(names) |
| 413 ..write(', lines: ') | 482 ..write(', lines: ') |
| 414 ..write(lines) | 483 ..write(lines) |
| 415 ..write(']')).toString(); | 484 ..write(']')) |
| 485 .toString(); | |
| 416 } | 486 } |
| 417 | 487 |
| 418 String get debugString { | 488 String get debugString { |
| 419 var buff = new StringBuffer(); | 489 var buff = new StringBuffer(); |
| 420 for (var lineEntry in lines) { | 490 for (var lineEntry in lines) { |
| 421 var line = lineEntry.line; | 491 var line = lineEntry.line; |
| 422 for (var entry in lineEntry.entries) { | 492 for (var entry in lineEntry.entries) { |
| 423 buff..write(targetUrl) | 493 buff |
| 494 ..write(targetUrl) | |
| 495 ..write(': ') | |
| 496 ..write(line) | |
| 497 ..write(':') | |
| 498 ..write(entry.column); | |
| 499 if (entry.sourceUrlId != null) { | |
| 500 buff | |
| 501 ..write(' --> ') | |
| 502 ..write(sourceRoot) | |
| 503 ..write(urls[entry.sourceUrlId]) | |
| 424 ..write(': ') | 504 ..write(': ') |
| 425 ..write(line) | 505 ..write(entry.sourceLine) |
| 426 ..write(':') | 506 ..write(':') |
| 427 ..write(entry.column); | 507 ..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 } | 508 } |
| 437 if (entry.sourceNameId != null) { | 509 if (entry.sourceNameId != null) { |
| 438 buff..write(' (') | 510 buff..write(' (')..write(names[entry.sourceNameId])..write(')'); |
| 439 ..write(names[entry.sourceNameId]) | |
| 440 ..write(')'); | |
| 441 } | 511 } |
| 442 buff.write('\n'); | 512 buff.write('\n'); |
| 443 } | 513 } |
| 444 } | 514 } |
| 445 return buff.toString(); | 515 return buff.toString(); |
| 446 } | 516 } |
| 447 } | 517 } |
| 448 | 518 |
| 449 /// A line entry read from a source map. | 519 /// A line entry read from a source map. |
| 450 class TargetLineEntry { | 520 class TargetLineEntry { |
| 451 final int line; | 521 final int line; |
| 452 List<TargetEntry> entries; | 522 List<TargetEntry> entries; |
| 453 TargetLineEntry(this.line, this.entries); | 523 TargetLineEntry(this.line, this.entries); |
| 454 | 524 |
| 455 String toString() => '$runtimeType: $line $entries'; | 525 String toString() => '$runtimeType: $line $entries'; |
| 456 } | 526 } |
| 457 | 527 |
| 458 /// A target segment entry read from a source map | 528 /// A target segment entry read from a source map |
| 459 class TargetEntry { | 529 class TargetEntry { |
| 460 final int column; | 530 final int column; |
| 461 final int sourceUrlId; | 531 final int sourceUrlId; |
| 462 final int sourceLine; | 532 final int sourceLine; |
| 463 final int sourceColumn; | 533 final int sourceColumn; |
| 464 final int sourceNameId; | 534 final int sourceNameId; |
| 465 | 535 |
| 466 TargetEntry(this.column, [this.sourceUrlId, this.sourceLine, | 536 TargetEntry(this.column, |
| 467 this.sourceColumn, this.sourceNameId]); | 537 [this.sourceUrlId, |
| 538 this.sourceLine, | |
| 539 this.sourceColumn, | |
| 540 this.sourceNameId]); | |
| 468 | 541 |
| 469 String toString() => '$runtimeType: ' | 542 String toString() => '$runtimeType: ' |
| 470 '($column, $sourceUrlId, $sourceLine, $sourceColumn, $sourceNameId)'; | 543 '($column, $sourceUrlId, $sourceLine, $sourceColumn, $sourceNameId)'; |
| 471 } | 544 } |
| 472 | 545 |
| 473 /** A character iterator over a string that can peek one character ahead. */ | 546 /** A character iterator over a string that can peek one character ahead. */ |
| 474 class _MappingTokenizer implements Iterator<String> { | 547 class _MappingTokenizer implements Iterator<String> { |
| 475 final String _internal; | 548 final String _internal; |
| 476 final int _length; | 549 final int _length; |
| 477 int index = -1; | 550 int index = -1; |
| 478 _MappingTokenizer(String internal) | 551 _MappingTokenizer(String internal) |
| 479 : _internal = internal, | 552 : _internal = internal, |
| 480 _length = internal.length; | 553 _length = internal.length; |
| 481 | 554 |
| 482 // Iterator API is used by decodeVlq to consume VLQ entries. | 555 // Iterator API is used by decodeVlq to consume VLQ entries. |
| 483 bool moveNext() => ++index < _length; | 556 bool moveNext() => ++index < _length; |
| 484 String get current => | 557 String get current => |
| 485 (index >= 0 && index < _length) ? _internal[index] : null; | 558 (index >= 0 && index < _length) ? _internal[index] : null; |
| 486 | 559 |
| 487 bool get hasTokens => index < _length - 1 && _length > 0; | 560 bool get hasTokens => index < _length - 1 && _length > 0; |
| 488 | 561 |
| 489 _TokenKind get nextKind { | 562 _TokenKind get nextKind { |
| 490 if (!hasTokens) return _TokenKind.EOF; | 563 if (!hasTokens) return _TokenKind.EOF; |
| 491 var next = _internal[index + 1]; | 564 var next = _internal[index + 1]; |
| 492 if (next == ';') return _TokenKind.LINE; | 565 if (next == ';') return _TokenKind.LINE; |
| 493 if (next == ',') return _TokenKind.SEGMENT; | 566 if (next == ',') return _TokenKind.SEGMENT; |
| 494 return _TokenKind.VALUE; | 567 return _TokenKind.VALUE; |
| 495 } | 568 } |
| 496 | 569 |
| 497 int _consumeValue() => decodeVlq(this); | 570 int _consumeValue() => decodeVlq(this); |
| 498 void _consumeNewLine() { ++index; } | 571 void _consumeNewLine() { |
| 499 void _consumeNewSegment() { ++index; } | 572 ++index; |
| 573 } | |
| 574 | |
| 575 void _consumeNewSegment() { | |
| 576 ++index; | |
| 577 } | |
| 500 | 578 |
| 501 // Print the state of the iterator, with colors indicating the current | 579 // Print the state of the iterator, with colors indicating the current |
| 502 // position. | 580 // position. |
| 503 String toString() { | 581 String toString() { |
| 504 var buff = new StringBuffer(); | 582 var buff = new StringBuffer(); |
| 505 for (int i = 0; i < index; i++) { | 583 for (int i = 0; i < index; i++) { |
| 506 buff.write(_internal[i]); | 584 buff.write(_internal[i]); |
| 507 } | 585 } |
| 508 buff.write('[31m'); | 586 buff.write('[31m'); |
| 509 buff.write(current == null ? '' : current); | 587 buff.write(current == null ? '' : current); |
| (...skipping 12 matching lines...) Expand all Loading... | |
| 522 static const _TokenKind EOF = const _TokenKind(isEof: true); | 600 static const _TokenKind EOF = const _TokenKind(isEof: true); |
| 523 static const _TokenKind VALUE = const _TokenKind(); | 601 static const _TokenKind VALUE = const _TokenKind(); |
| 524 final bool isNewLine; | 602 final bool isNewLine; |
| 525 final bool isNewSegment; | 603 final bool isNewSegment; |
| 526 final bool isEof; | 604 final bool isEof; |
| 527 bool get isValue => !isNewLine && !isNewSegment && !isEof; | 605 bool get isValue => !isNewLine && !isNewSegment && !isEof; |
| 528 | 606 |
| 529 const _TokenKind( | 607 const _TokenKind( |
| 530 {this.isNewLine: false, this.isNewSegment: false, this.isEof: false}); | 608 {this.isNewLine: false, this.isNewSegment: false, this.isEof: false}); |
| 531 } | 609 } |
| OLD | NEW |