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

Side by Side 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 unified diff | Download patch | Annotate | Revision Log
OLDNEW
(Empty)
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
3 // BSD-style license that can be found in the LICENSE file.
4
5 library dart2js.source_information;
6
7 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.
8 import '../elements/elements.dart' show AstElement;
9 import '../scanner/scannerlib.dart' show Token;
10 import '../tree/tree.dart' show Node;
11 import '../util/util.dart';
12 import '../util/uri_extras.dart' show relativize;
13 import 'code_output.dart';
14 import 'line_column_provider.dart';
15 import 'source_file.dart';
16
17 abstract class SourceInformation {
floitsch 2015/02/09 13:16:22 comments.
Johnni Winther 2015/02/09 14:58:35 Done.
18 SourceSpan get sourceSpan;
19 void beginMapping(CodeOutput output);
20 void endMapping(CodeOutput output);
21 }
22
23 class StartEndSourceInformation implements SourceInformation {
floitsch 2015/02/09 13:16:22 comments.
Johnni Winther 2015/02/09 14:58:35 Done.
24 final SourceFileLocation startPosition;
25 final SourceFileLocation endPosition;
26
27 StartEndSourceInformation(this.startPosition, [this.endPosition]);
28
29 SourceSpan get sourceSpan {
30 Uri uri = Uri.parse(startPosition.sourceFile.filename);
31 int begin = startPosition.offset;
32 int end = endPosition == null ? begin : endPosition.offset;
33 return new SourceSpan(uri, begin, end);
34 }
35
36 void beginMapping(CodeBuffer output) {
37 output.beginMappedRange();
38 output.setSourceLocation(startPosition);
39 }
40
41 void endMapping(CodeBuffer output) {
42 if (endPosition != null) {
43 output.setSourceLocation(endPosition);
44 }
45 output.endMappedRange();
46 }
47
48 int get hashCode {
49 return startPosition.hashCode * 17 +
50 endPosition.hashCode * 19;
floitsch 2015/02/09 13:16:22 & 0x7FFFFFFF
Johnni Winther 2015/02/09 14:58:36 Done.
51 }
52
53 bool operator ==(other) {
54 if (identical(this, other)) return true;
55 if (other is! StartEndSourceInformation) return false;
56 return startPosition == other.startPosition &&
57 endPosition == other.endPosition;
58 }
59
60 // TODO(johnniwinther): Remove this method. Source information should be
61 // computed based on the element by provided from statements and expressions.
62 static StartEndSourceInformation computeSourceInformation(
63 AstElement element) {
64
65 AstElement implementation = element.implementation;
66 SourceFile sourceFile = implementation.compilationUnit.script.file;
67 // 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.
68 // was on a wrapping node.
69 String name = element.name;
70 Node node = implementation.node;
71 Token beginToken;
72 Token endToken;
73 if (node == null) {
74 // Synthesized node. Use the enclosing element for the location.
75 beginToken = endToken = element.position;
76 } else {
77 beginToken = node.getBeginToken();
78 endToken = node.getEndToken();
79 }
80 // TODO(podivilov): find the right sourceFile here and remove offset
81 // checks below.
82 SourceFileLocation sourcePosition, endSourcePosition;
83 if (beginToken.charOffset < sourceFile.length) {
84 sourcePosition =
85 new TokenSourceFileLocation(sourceFile, beginToken, name);
86 }
87 if (endToken.charOffset < sourceFile.length) {
88 endSourcePosition =
89 new TokenSourceFileLocation(sourceFile, endToken, name);
90 }
91 return new StartEndSourceInformation(sourcePosition, endSourcePosition);
92 }
93
94 String toString() {
95 StringBuffer sb = new StringBuffer();
96 sb.write('${startPosition.getSourceUrl()}:');
97 sb.write('[${startPosition.getLine()},${startPosition.getColumn()}]');
98 if (endPosition != null) {
99 sb.write('-[${endPosition.getLine()},${endPosition.getColumn()}]');
100 }
101 return sb.toString();
102 }
103 }
104
105 abstract class SourceFileLocation {
106 SourceFile sourceFile;
107
108 SourceFileLocation(this.sourceFile) {
109 assert(isValid());
110 }
111
112 int line;
113
114 int get offset;
115
116 String getSourceUrl() => sourceFile.filename;
117
118 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.
119 if (line == null) line = sourceFile.getLine(offset);
120 return line;
121 }
122
123 int getColumn() => sourceFile.getColumn(getLine(), offset);
124
125 String getSourceName();
126
127 bool isValid() => offset < sourceFile.length;
128
129 int get hashCode {
130 return getSourceUrl().hashCode * 17 +
131 offset.hashCode * 17 +
132 getSourceName().hashCode * 23;
133 }
134
135 bool operator ==(other) {
136 if (identical(this, other)) return true;
137 if (other is! SourceFileLocation) return false;
138 return getSourceUrl() == other.getSourceUrl() &&
139 offset == other.offset &&
140 getSourceName() == other.getSourceName();
141 }
142
143 String toString() => '${getSourceUrl()}:[${getLine()},${getColumn()}]';
144 }
145
146 class TokenSourceFileLocation extends SourceFileLocation {
147 final Token token;
148 final String name;
149
150 TokenSourceFileLocation(SourceFile sourceFile, this.token, this.name)
151 : super(sourceFile);
152
153 int get offset => token.charOffset;
154
155 String getSourceName() {
156 return name;
157 }
158
159 String toString() {
160 return '${super.toString()}:$name';
161 }
162 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698