| 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 a code printer that generates code by recording the source maps. | 5 /// Contains a code printer that generates code by recording the source maps. |
| 6 library source_maps.printer; | 6 library source_maps.printer; |
| 7 | 7 |
| 8 import 'package:source_span/source_span.dart' as source_span; |
| 9 |
| 8 import 'builder.dart'; | 10 import 'builder.dart'; |
| 9 import 'span.dart'; | 11 import 'span.dart'; |
| 12 import 'src/span_wrapper.dart'; |
| 10 | 13 |
| 11 const int _LF = 10; | 14 const int _LF = 10; |
| 12 const int _CR = 13; | 15 const int _CR = 13; |
| 13 | 16 |
| 14 /// A simple printer that keeps track of offset locations and records source | 17 /// A simple printer that keeps track of offset locations and records source |
| 15 /// maps locations. | 18 /// maps locations. |
| 16 class Printer { | 19 class Printer { |
| 17 final String filename; | 20 final String filename; |
| 18 final StringBuffer _buff = new StringBuffer(); | 21 final StringBuffer _buff = new StringBuffer(); |
| 19 final SourceMapBuilder _maps = new SourceMapBuilder(); | 22 final SourceMapBuilder _maps = new SourceMapBuilder(); |
| (...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 68 _column += total; | 71 _column += total; |
| 69 } | 72 } |
| 70 | 73 |
| 71 /// Marks that the current point in the target file corresponds to the [mark] | 74 /// Marks that the current point in the target file corresponds to the [mark] |
| 72 /// in the source file, which can be either a [Location] or a [Span]. When the | 75 /// in the source file, which can be either a [Location] or a [Span]. When the |
| 73 /// mark is an identifier's Span, this also records the name of the identifier | 76 /// mark is an identifier's Span, this also records the name of the identifier |
| 74 /// in the source map information. | 77 /// in the source map information. |
| 75 void mark(mark) { | 78 void mark(mark) { |
| 76 var loc; | 79 var loc; |
| 77 var identifier = null; | 80 var identifier = null; |
| 78 if (mark is Location) { | 81 if (mark is Location || mark is source_span.SourceLocation) { |
| 79 loc = mark; | 82 loc = LocationWrapper.wrap(mark); |
| 80 } else if (mark is Span) { | 83 } else if (mark is Span || mark is source_span.SourceSpan) { |
| 84 mark = SpanWrapper.wrap(mark); |
| 81 loc = mark.start; | 85 loc = mark.start; |
| 82 if (mark.isIdentifier) identifier = mark.text; | 86 if (mark.isIdentifier) identifier = mark.text; |
| 83 } | 87 } |
| 84 _maps.addLocation(loc, | 88 _maps.addLocation(loc, |
| 85 new FixedLocation(_buff.length, null, _line, _column), identifier); | 89 new FixedLocation(_buff.length, null, _line, _column), identifier); |
| 86 _loc = loc; | 90 _loc = loc; |
| 87 } | 91 } |
| 88 } | 92 } |
| 89 | 93 |
| 90 /// A more advanced printer that keeps track of offset locations to record | 94 /// A more advanced printer that keeps track of offset locations to record |
| (...skipping 26 matching lines...) Expand all Loading... |
| 117 /// [String], the value is appended directly, without doing any formatting | 121 /// [String], the value is appended directly, without doing any formatting |
| 118 /// changes. If you wish to add a line of code with automatic indentation, use | 122 /// changes. If you wish to add a line of code with automatic indentation, use |
| 119 /// [addLine] instead. [NestedPrinter]s and [NestedItem]s are not processed | 123 /// [addLine] instead. [NestedPrinter]s and [NestedItem]s are not processed |
| 120 /// until [build] gets called later on. We ensure that [build] emits every | 124 /// until [build] gets called later on. We ensure that [build] emits every |
| 121 /// object in the order that they were added to this printer. | 125 /// object in the order that they were added to this printer. |
| 122 /// | 126 /// |
| 123 /// The [location] and [span] parameters indicate the corresponding source map | 127 /// The [location] and [span] parameters indicate the corresponding source map |
| 124 /// location of [object] in the original input. Only one, [location] or | 128 /// location of [object] in the original input. Only one, [location] or |
| 125 /// [span], should be provided at a time. | 129 /// [span], should be provided at a time. |
| 126 /// | 130 /// |
| 131 /// [location] can be either a [Location] or a [SourceLocation]. [span] can be |
| 132 /// either a [Span] or a [SourceSpan]. Using a [Location] or a [Span] is |
| 133 /// deprecated and will be unsupported in version 0.10.0. |
| 134 /// |
| 127 /// Indicate [isOriginal] when [object] is copied directly from the user code. | 135 /// Indicate [isOriginal] when [object] is copied directly from the user code. |
| 128 /// Setting [isOriginal] will make this printer propagate source map locations | 136 /// Setting [isOriginal] will make this printer propagate source map locations |
| 129 /// on every line-break. | 137 /// on every line-break. |
| 130 void add(object, {Location location, Span span, bool isOriginal: false}) { | 138 void add(object, {location, span, bool isOriginal: false}) { |
| 131 if (object is! String || location != null || span != null || isOriginal) { | 139 if (object is! String || location != null || span != null || isOriginal) { |
| 132 _flush(); | 140 _flush(); |
| 133 assert(location == null || span == null); | 141 assert(location == null || span == null); |
| 134 if (location != null) _items.add(location); | 142 if (location != null) _items.add(LocationWrapper.wrap(location)); |
| 135 if (span != null) _items.add(span); | 143 if (span != null) _items.add(SpanWrapper.wrap(span)); |
| 136 if (isOriginal) _items.add(_ORIGINAL); | 144 if (isOriginal) _items.add(_ORIGINAL); |
| 137 } | 145 } |
| 138 | 146 |
| 139 if (object is String) { | 147 if (object is String) { |
| 140 _appendString(object); | 148 _appendString(object); |
| 141 } else { | 149 } else { |
| 142 _items.add(object); | 150 _items.add(object); |
| 143 } | 151 } |
| 144 } | 152 } |
| 145 | 153 |
| 146 /// Append `2 * indent` spaces to this printer. | 154 /// Append `2 * indent` spaces to this printer. |
| 147 void insertIndent() => _indent(indent); | 155 void insertIndent() => _indent(indent); |
| 148 | 156 |
| 149 /// Add a [line], autoindenting to the current value of [indent]. Note, | 157 /// Add a [line], autoindenting to the current value of [indent]. Note, |
| 150 /// indentation is not inferred from the contents added to this printer. If a | 158 /// indentation is not inferred from the contents added to this printer. If a |
| 151 /// line starts or ends an indentation block, you need to also update [indent] | 159 /// line starts or ends an indentation block, you need to also update [indent] |
| 152 /// accordingly. Also, indentation is not adapted for nested printers. If | 160 /// accordingly. Also, indentation is not adapted for nested printers. If |
| 153 /// you add a [NestedPrinter] to this printer, its indentation is set | 161 /// you add a [NestedPrinter] to this printer, its indentation is set |
| 154 /// separately and will not include any the indentation set here. | 162 /// separately and will not include any the indentation set here. |
| 155 /// | 163 /// |
| 156 /// The [location] and [span] parameters indicate the corresponding source map | 164 /// The [location] and [span] parameters indicate the corresponding source map |
| 157 /// location of [object] in the original input. Only one, [location] or | 165 /// location of [object] in the original input. Only one, [location] or |
| 158 /// [span], should be provided at a time. | 166 /// [span], should be provided at a time. |
| 159 void addLine(String line, {Location location, Span span}) { | 167 /// |
| 168 /// [location] can be either a [Location] or a [SourceLocation]. [span] can be |
| 169 /// either a [Span] or a [SourceSpan]. Using a [Location] or a [Span] is |
| 170 /// deprecated and will be unsupported in version 0.10.0. |
| 171 void addLine(String line, {location, span}) { |
| 160 if (location != null || span != null) { | 172 if (location != null || span != null) { |
| 161 _flush(); | 173 _flush(); |
| 162 assert(location == null || span == null); | 174 assert(location == null || span == null); |
| 163 if (location != null) _items.add(location); | 175 if (location != null) _items.add(LocationWrapper.wrap(location)); |
| 164 if (span != null) _items.add(span); | 176 if (span != null) _items.add(SpanWrapper.wrap(span)); |
| 165 } | 177 } |
| 166 if (line == null) return; | 178 if (line == null) return; |
| 167 if (line != '') { | 179 if (line != '') { |
| 168 // We don't indent empty lines. | 180 // We don't indent empty lines. |
| 169 _indent(indent); | 181 _indent(indent); |
| 170 _appendString(line); | 182 _appendString(line); |
| 171 } | 183 } |
| 172 _appendString('\n'); | 184 _appendString('\n'); |
| 173 } | 185 } |
| 174 | 186 |
| (...skipping 60 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 235 } | 247 } |
| 236 } | 248 } |
| 237 } | 249 } |
| 238 } | 250 } |
| 239 | 251 |
| 240 /// An item added to a [NestedPrinter]. | 252 /// An item added to a [NestedPrinter]. |
| 241 abstract class NestedItem { | 253 abstract class NestedItem { |
| 242 /// Write the contents of this item into [printer]. | 254 /// Write the contents of this item into [printer]. |
| 243 void writeTo(Printer printer); | 255 void writeTo(Printer printer); |
| 244 } | 256 } |
| OLD | NEW |