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

Unified Diff: pkg/compiler/lib/src/io/source_information.dart

Issue 893963005: Refactor handling of source map information. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Created 5 years, 10 months 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 side-by-side diff with in-line comments
Download patch
Index: pkg/compiler/lib/src/io/source_information.dart
diff --git a/pkg/compiler/lib/src/io/source_information.dart b/pkg/compiler/lib/src/io/source_information.dart
new file mode 100644
index 0000000000000000000000000000000000000000..0bd1ec1673901b627181098598ad38f9d687aef1
--- /dev/null
+++ b/pkg/compiler/lib/src/io/source_information.dart
@@ -0,0 +1,162 @@
+// Copyright (c) 2015, the Dart project authors. Please see the AUTHORS file
+// for details. All rights reserved. Use of this source code is governed by a
+// BSD-style license that can be found in the LICENSE file.
+
+library dart2js.source_information;
+
+import '../dart2jslib.dart' show SourceSpan;
floitsch 2015/02/09 13:16:22 This file seems to be a copy of another source. I
Johnni Winther 2015/02/09 14:58:35 50% is moved, but from several places.
+import '../elements/elements.dart' show AstElement;
+import '../scanner/scannerlib.dart' show Token;
+import '../tree/tree.dart' show Node;
+import '../util/util.dart';
+import '../util/uri_extras.dart' show relativize;
+import 'code_output.dart';
+import 'line_column_provider.dart';
+import 'source_file.dart';
+
+abstract class SourceInformation {
floitsch 2015/02/09 13:16:22 comments.
Johnni Winther 2015/02/09 14:58:35 Done.
+ SourceSpan get sourceSpan;
+ void beginMapping(CodeOutput output);
+ void endMapping(CodeOutput output);
+}
+
+class StartEndSourceInformation implements SourceInformation {
floitsch 2015/02/09 13:16:22 comments.
Johnni Winther 2015/02/09 14:58:35 Done.
+ final SourceFileLocation startPosition;
+ final SourceFileLocation endPosition;
+
+ StartEndSourceInformation(this.startPosition, [this.endPosition]);
+
+ SourceSpan get sourceSpan {
+ Uri uri = Uri.parse(startPosition.sourceFile.filename);
+ int begin = startPosition.offset;
+ int end = endPosition == null ? begin : endPosition.offset;
+ return new SourceSpan(uri, begin, end);
+ }
+
+ void beginMapping(CodeBuffer output) {
+ output.beginMappedRange();
+ output.setSourceLocation(startPosition);
+ }
+
+ void endMapping(CodeBuffer output) {
+ if (endPosition != null) {
+ output.setSourceLocation(endPosition);
+ }
+ output.endMappedRange();
+ }
+
+ int get hashCode {
+ return startPosition.hashCode * 17 +
+ endPosition.hashCode * 19;
floitsch 2015/02/09 13:16:22 & 0x7FFFFFFF
Johnni Winther 2015/02/09 14:58:36 Done.
+ }
+
+ bool operator ==(other) {
+ if (identical(this, other)) return true;
+ if (other is! StartEndSourceInformation) return false;
+ return startPosition == other.startPosition &&
+ endPosition == other.endPosition;
+ }
+
+ // TODO(johnniwinther): Remove this method. Source information should be
+ // computed based on the element by provided from statements and expressions.
+ static StartEndSourceInformation computeSourceInformation(
+ AstElement element) {
+
+ AstElement implementation = element.implementation;
+ SourceFile sourceFile = implementation.compilationUnit.script.file;
+ // TODO(sra): Attaching positions might be cleaner if the source position
floitsch 2015/02/09 13:16:22 Is this comment still relevant? I don't understand
Johnni Winther 2015/02/09 14:58:36 Removed. I'm taking a different route.
+ // was on a wrapping node.
+ String name = element.name;
+ Node node = implementation.node;
+ Token beginToken;
+ Token endToken;
+ if (node == null) {
+ // Synthesized node. Use the enclosing element for the location.
+ beginToken = endToken = element.position;
+ } else {
+ beginToken = node.getBeginToken();
+ endToken = node.getEndToken();
+ }
+ // TODO(podivilov): find the right sourceFile here and remove offset
+ // checks below.
+ SourceFileLocation sourcePosition, endSourcePosition;
+ if (beginToken.charOffset < sourceFile.length) {
+ sourcePosition =
+ new TokenSourceFileLocation(sourceFile, beginToken, name);
+ }
+ if (endToken.charOffset < sourceFile.length) {
+ endSourcePosition =
+ new TokenSourceFileLocation(sourceFile, endToken, name);
+ }
+ return new StartEndSourceInformation(sourcePosition, endSourcePosition);
+ }
+
+ String toString() {
+ StringBuffer sb = new StringBuffer();
+ sb.write('${startPosition.getSourceUrl()}:');
+ sb.write('[${startPosition.getLine()},${startPosition.getColumn()}]');
+ if (endPosition != null) {
+ sb.write('-[${endPosition.getLine()},${endPosition.getColumn()}]');
+ }
+ return sb.toString();
+ }
+}
+
+abstract class SourceFileLocation {
+ SourceFile sourceFile;
+
+ SourceFileLocation(this.sourceFile) {
+ assert(isValid());
+ }
+
+ int line;
+
+ int get offset;
+
+ String getSourceUrl() => sourceFile.filename;
+
+ int getLine() {
floitsch 2015/02/09 13:16:22 maybe add TODOs, to refactor this code? (unless yo
Johnni Winther 2015/02/09 14:58:36 Done.
+ if (line == null) line = sourceFile.getLine(offset);
+ return line;
+ }
+
+ int getColumn() => sourceFile.getColumn(getLine(), offset);
+
+ String getSourceName();
+
+ bool isValid() => offset < sourceFile.length;
+
+ int get hashCode {
+ return getSourceUrl().hashCode * 17 +
+ offset.hashCode * 17 +
+ getSourceName().hashCode * 23;
+ }
+
+ bool operator ==(other) {
+ if (identical(this, other)) return true;
+ if (other is! SourceFileLocation) return false;
+ return getSourceUrl() == other.getSourceUrl() &&
+ offset == other.offset &&
+ getSourceName() == other.getSourceName();
+ }
+
+ String toString() => '${getSourceUrl()}:[${getLine()},${getColumn()}]';
+}
+
+class TokenSourceFileLocation extends SourceFileLocation {
+ final Token token;
+ final String name;
+
+ TokenSourceFileLocation(SourceFile sourceFile, this.token, this.name)
+ : super(sourceFile);
+
+ int get offset => token.charOffset;
+
+ String getSourceName() {
+ return name;
+ }
+
+ String toString() {
+ return '${super.toString()}:$name';
+ }
+}

Powered by Google App Engine
This is Rietveld 408576698