| OLD | NEW |
| 1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2012, 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 library dart2js.code_output; | 5 library dart2js.code_output; |
| 6 | 6 |
| 7 import 'dart:async'; | 7 import 'dart:async'; |
| 8 | 8 |
| 9 import 'source_information.dart'; | 9 import 'source_information.dart'; |
| 10 | 10 |
| 11 /// Listener interface for [CodeOutput] activity. | 11 /// Listener interface for [CodeOutput] activity. |
| 12 abstract class CodeOutputListener { | 12 abstract class CodeOutputListener { |
| 13 /// Called when [text] is added to the output. | 13 /// Called when [text] is added to the output. |
| 14 void onText(String text); | 14 void onText(String text); |
| 15 | 15 |
| 16 /// Called when the output is closed with a final length of [length]. | 16 /// Called when the output is closed with a final length of [length]. |
| 17 void onDone(int length); | 17 void onDone(int length); |
| 18 } | 18 } |
| 19 | 19 |
| 20 /// Interface for a mapping of target offsets to source locations. | 20 /// Interface for a mapping of target offsets to source locations. |
| 21 abstract class SourceLocations { | 21 abstract class SourceLocations { |
| 22 /// Adds a [sourceLocation] at the specified [targetOffset]. | 22 /// Adds a [sourceLocation] at the specified [targetOffset]. |
| 23 void addSourceLocation(int targetOffset, SourceLocation sourcePosition); | 23 void addSourceLocation(int targetOffset, SourceLocation sourcePosition); |
| 24 | 24 |
| 25 /// Applies [f] to every target offset and associated source location. | 25 /// Applies [f] to every target offset and associated source location. |
| 26 void forEachSourceLocation(void f(int targetOffset, | 26 void forEachSourceLocation( |
| 27 SourceLocation sourceLocation)); | 27 void f(int targetOffset, SourceLocation sourceLocation)); |
| 28 } | 28 } |
| 29 | 29 |
| 30 abstract class CodeOutput implements SourceLocations { | 30 abstract class CodeOutput implements SourceLocations { |
| 31 /// Write [text] to this output. | 31 /// Write [text] to this output. |
| 32 /// | 32 /// |
| 33 /// If the output is closed, a [StateError] is thrown. | 33 /// If the output is closed, a [StateError] is thrown. |
| 34 void add(String text); | 34 void add(String text); |
| 35 | 35 |
| 36 /// Adds the content of [buffer] to the output and adds its markers to | 36 /// Adds the content of [buffer] to the output and adds its markers to |
| 37 /// [markers]. | 37 /// [markers]. |
| (...skipping 21 matching lines...) Expand all Loading... |
| 59 void add(String text) { | 59 void add(String text) { |
| 60 if (isClosed) { | 60 if (isClosed) { |
| 61 throw new StateError("Code output is closed. Trying to write '$text'."); | 61 throw new StateError("Code output is closed. Trying to write '$text'."); |
| 62 } | 62 } |
| 63 _addInternal(text); | 63 _addInternal(text); |
| 64 } | 64 } |
| 65 | 65 |
| 66 @override | 66 @override |
| 67 void addBuffer(CodeBuffer other) { | 67 void addBuffer(CodeBuffer other) { |
| 68 if (other.markers.length > 0) { | 68 if (other.markers.length > 0) { |
| 69 other.markers.forEach( | 69 other.markers |
| 70 (int targetOffset, List<SourceLocation> sourceLocations) { | 70 .forEach((int targetOffset, List<SourceLocation> sourceLocations) { |
| 71 markers.putIfAbsent(length + targetOffset, () => <SourceLocation>[]) | 71 markers |
| 72 .putIfAbsent(length + targetOffset, () => <SourceLocation>[]) |
| 72 .addAll(sourceLocations); | 73 .addAll(sourceLocations); |
| 73 }); | 74 }); |
| 74 } | 75 } |
| 75 if (!other.isClosed) { | 76 if (!other.isClosed) { |
| 76 other.close(); | 77 other.close(); |
| 77 } | 78 } |
| 78 _addInternal(other.getText()); | 79 _addInternal(other.getText()); |
| 79 } | 80 } |
| 80 | 81 |
| 81 void addSourceLocation(int targetOffset, | 82 void addSourceLocation(int targetOffset, SourceLocation sourceLocation) { |
| 82 SourceLocation sourceLocation) { | |
| 83 assert(targetOffset <= length); | 83 assert(targetOffset <= length); |
| 84 List<SourceLocation> sourceLocations = | 84 List<SourceLocation> sourceLocations = |
| 85 markers.putIfAbsent(targetOffset, () => <SourceLocation>[]); | 85 markers.putIfAbsent(targetOffset, () => <SourceLocation>[]); |
| 86 sourceLocations.add(sourceLocation); | 86 sourceLocations.add(sourceLocation); |
| 87 } | 87 } |
| 88 | 88 |
| 89 void forEachSourceLocation(void f(int targetOffset, var sourceLocation)) { | 89 void forEachSourceLocation(void f(int targetOffset, var sourceLocation)) { |
| 90 markers.forEach((int targetOffset, List<SourceLocation> sourceLocations) { | 90 markers.forEach((int targetOffset, List<SourceLocation> sourceLocations) { |
| 91 for (SourceLocation sourceLocation in sourceLocations) { | 91 for (SourceLocation sourceLocation in sourceLocations) { |
| 92 f(targetOffset, sourceLocation); | 92 f(targetOffset, sourceLocation); |
| (...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 145 } | 145 } |
| 146 | 146 |
| 147 void close() { | 147 void close() { |
| 148 output.close(); | 148 output.close(); |
| 149 super.close(); | 149 super.close(); |
| 150 if (_listeners != null) { | 150 if (_listeners != null) { |
| 151 _listeners.forEach((listener) => listener.onDone(length)); | 151 _listeners.forEach((listener) => listener.onDone(length)); |
| 152 } | 152 } |
| 153 } | 153 } |
| 154 } | 154 } |
| OLD | NEW |