| OLD | NEW |
| 1 // Copyright (c) 2015, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2015, 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 /// Source information system that maps spans of Dart AST nodes to spans of | 5 /// Source information system that maps spans of Dart AST nodes to spans of |
| 6 /// JavaScript nodes. | 6 /// JavaScript nodes. |
| 7 | 7 |
| 8 library dart2js.source_information.start_end; | 8 library dart2js.source_information.start_end; |
| 9 | 9 |
| 10 import 'package:front_end/src/fasta/scanner.dart' show Token; |
| 11 |
| 10 import '../common.dart'; | 12 import '../common.dart'; |
| 11 import '../diagnostics/messages.dart' show MessageTemplate; | 13 import '../diagnostics/messages.dart' show MessageTemplate; |
| 12 import '../elements/elements.dart' show ResolvedAst, ResolvedAstKind; | 14 import '../elements/elements.dart' show ResolvedAst, ResolvedAstKind; |
| 13 import '../js/js.dart' as js; | 15 import '../js/js.dart' as js; |
| 14 import '../js/js_source_mapping.dart'; | 16 import '../js/js_source_mapping.dart'; |
| 15 import 'package:front_end/src/fasta/scanner.dart' show Token; | |
| 16 import '../tree/tree.dart' show Node; | 17 import '../tree/tree.dart' show Node; |
| 17 import 'source_file.dart'; | 18 import 'source_file.dart'; |
| 18 import 'source_information.dart'; | 19 import 'source_information.dart'; |
| 19 | 20 |
| 20 /// Source information that contains start source position and optionally an | 21 /// Source information that contains start source position and optionally an |
| 21 /// end source position. | 22 /// end source position. |
| 22 class StartEndSourceInformation extends SourceInformation { | 23 class StartEndSourceInformation extends SourceInformation { |
| 23 @override | 24 @override |
| 24 final SourceLocation startPosition; | 25 final SourceLocation startPosition; |
| 25 | 26 |
| (...skipping 84 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 110 class StartEndSourceInformationStrategy | 111 class StartEndSourceInformationStrategy |
| 111 extends JavaScriptSourceInformationStrategy { | 112 extends JavaScriptSourceInformationStrategy { |
| 112 const StartEndSourceInformationStrategy(); | 113 const StartEndSourceInformationStrategy(); |
| 113 | 114 |
| 114 @override | 115 @override |
| 115 SourceInformationBuilder createBuilderForContext(ResolvedAst resolvedAst) { | 116 SourceInformationBuilder createBuilderForContext(ResolvedAst resolvedAst) { |
| 116 return new StartEndSourceInformationBuilder(resolvedAst); | 117 return new StartEndSourceInformationBuilder(resolvedAst); |
| 117 } | 118 } |
| 118 | 119 |
| 119 @override | 120 @override |
| 120 SourceInformationProcessor createProcessor(SourceMapper sourceMapper) { | 121 SourceInformationProcessor createProcessor( |
| 121 return new StartEndSourceInformationProcessor(sourceMapper); | 122 SourceMapperProvider provider, SourceInformationReader reader) { |
| 123 return new StartEndSourceInformationProcessor(provider, reader); |
| 122 } | 124 } |
| 123 } | 125 } |
| 124 | 126 |
| 125 class StartEndSourceInformationProcessor extends SourceInformationProcessor { | 127 class StartEndSourceInformationProcessor extends SourceInformationProcessor { |
| 128 /// The id for this source information engine. |
| 129 /// |
| 130 /// The id is added to the source map file in an extra "engine" property and |
| 131 /// serves as a version number for the engine. |
| 132 /// |
| 133 /// The version history of this engine is: |
| 134 /// |
| 135 /// v1: The initial version with an id. |
| 136 static const String id = 'v1'; |
| 137 |
| 126 final SourceMapper sourceMapper; | 138 final SourceMapper sourceMapper; |
| 139 final SourceInformationReader reader; |
| 127 | 140 |
| 128 /// Used to track whether a terminating source location marker has been | 141 /// Used to track whether a terminating source location marker has been |
| 129 /// registered for the top-most node with source information. | 142 /// registered for the top-most node with source information. |
| 130 bool hasRegisteredRoot = false; | 143 bool hasRegisteredRoot = false; |
| 131 | 144 |
| 132 /// The root of the tree. Used to add a [NoSourceLocationMarker] to the start | 145 /// The root of the tree. Used to add a [NoSourceLocationMarker] to the start |
| 133 /// of the output. | 146 /// of the output. |
| 134 js.Node root; | 147 js.Node root; |
| 135 | 148 |
| 136 /// The root of the current subtree with source information. Used to add | 149 /// The root of the current subtree with source information. Used to add |
| 137 /// [NoSourceLocationMarker] after areas with source information. | 150 /// [NoSourceLocationMarker] after areas with source information. |
| 138 js.Node subRoot; | 151 js.Node subRoot; |
| 139 | 152 |
| 140 StartEndSourceInformationProcessor(this.sourceMapper); | 153 StartEndSourceInformationProcessor(SourceMapperProvider provider, this.reader) |
| 154 : this.sourceMapper = provider.createSourceMapper(id); |
| 141 | 155 |
| 142 void onStartPosition(js.Node node, int startPosition) { | 156 void onStartPosition(js.Node node, int startPosition) { |
| 143 if (root == null) { | 157 if (root == null) { |
| 144 root = node; | 158 root = node; |
| 145 sourceMapper.register( | 159 sourceMapper.register( |
| 146 node, startPosition, const NoSourceLocationMarker()); | 160 node, startPosition, const NoSourceLocationMarker()); |
| 147 } | 161 } |
| 148 if (subRoot == null && node.sourceInformation != null) { | 162 if (subRoot == null && reader.getSourceInformation(node) != null) { |
| 149 subRoot = node; | 163 subRoot = node; |
| 150 } | 164 } |
| 151 } | 165 } |
| 152 | 166 |
| 153 @override | 167 @override |
| 154 void onPositions( | 168 void onPositions( |
| 155 js.Node node, int startPosition, int endPosition, int closingPosition) { | 169 js.Node node, int startPosition, int endPosition, int closingPosition) { |
| 156 if (node.sourceInformation != null) { | 170 StartEndSourceInformation sourceInformation = |
| 157 StartEndSourceInformation sourceInformation = node.sourceInformation; | 171 reader.getSourceInformation(node); |
| 172 if (sourceInformation != null) { |
| 158 sourceMapper.register( | 173 sourceMapper.register( |
| 159 node, startPosition, sourceInformation.startPosition); | 174 node, startPosition, sourceInformation.startPosition); |
| 160 if (sourceInformation.endPosition != null) { | 175 if (sourceInformation.endPosition != null) { |
| 161 sourceMapper.register(node, endPosition, sourceInformation.endPosition); | 176 sourceMapper.register(node, endPosition, sourceInformation.endPosition); |
| 162 } | 177 } |
| 163 if (!hasRegisteredRoot) { | 178 if (!hasRegisteredRoot) { |
| 164 sourceMapper.register(node, endPosition, null); | 179 sourceMapper.register(node, endPosition, null); |
| 165 hasRegisteredRoot = true; | 180 hasRegisteredRoot = true; |
| 166 } | 181 } |
| 167 if (node == subRoot) { | 182 if (node == subRoot) { |
| (...skipping 69 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 237 | 252 |
| 238 @override | 253 @override |
| 239 SourceInformation buildIf(Node node) => buildGeneric(node); | 254 SourceInformation buildIf(Node node) => buildGeneric(node); |
| 240 | 255 |
| 241 @override | 256 @override |
| 242 SourceInformationBuilder forContext(ResolvedAst resolvedAst, | 257 SourceInformationBuilder forContext(ResolvedAst resolvedAst, |
| 243 {SourceInformation sourceInformation}) { | 258 {SourceInformation sourceInformation}) { |
| 244 return new StartEndSourceInformationBuilder(resolvedAst); | 259 return new StartEndSourceInformationBuilder(resolvedAst); |
| 245 } | 260 } |
| 246 } | 261 } |
| OLD | NEW |