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

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: Updated cf. comments. 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;
8 import '../elements/elements.dart' show AstElement;
9 import '../scanner/scannerlib.dart' show Token;
10 import '../tree/tree.dart' show Node;
11 import 'code_output.dart';
12 import 'source_file.dart';
13
14 /// Interface for passing source information, for instance for use in source
15 /// maps, through the backend.
16 abstract class SourceInformation {
17 SourceSpan get sourceSpan;
18 void beginMapping(CodeOutput output);
19 void endMapping(CodeOutput output);
20 }
21
22 /// Source information that contains start source position and optionally an
23 /// end source position.
24 class StartEndSourceInformation implements SourceInformation {
25 final SourceFileLocation startPosition;
26 final SourceFileLocation endPosition;
27
28 StartEndSourceInformation(this.startPosition, [this.endPosition]);
29
30 SourceSpan get sourceSpan {
31 Uri uri = Uri.parse(startPosition.sourceFile.filename);
32 int begin = startPosition.offset;
33 int end = endPosition == null ? begin : endPosition.offset;
34 return new SourceSpan(uri, begin, end);
35 }
36
37 void beginMapping(CodeBuffer output) {
38 output.beginMappedRange();
39 output.setSourceLocation(startPosition);
40 }
41
42 void endMapping(CodeBuffer output) {
43 if (endPosition != null) {
44 output.setSourceLocation(endPosition);
45 }
46 output.endMappedRange();
47 }
48
49 int get hashCode {
50 return (startPosition.hashCode * 17 +
51 endPosition.hashCode * 19)
52 & 0x7FFFFFFF;
53 }
54
55 bool operator ==(other) {
56 if (identical(this, other)) return true;
57 if (other is! StartEndSourceInformation) return false;
58 return startPosition == other.startPosition &&
59 endPosition == other.endPosition;
60 }
61
62 // TODO(johnniwinther): Remove this method. Source information should be
63 // computed based on the element by provided from statements and expressions.
64 static StartEndSourceInformation computeSourceInformation(
65 AstElement element) {
66
67 AstElement implementation = element.implementation;
68 SourceFile sourceFile = implementation.compilationUnit.script.file;
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 // TODO(johnniwinther): Refactor this class to use getters.
106 abstract class SourceFileLocation {
107 SourceFile sourceFile;
108
109 SourceFileLocation(this.sourceFile) {
110 assert(isValid());
111 }
112
113 int line;
114
115 int get offset;
116
117 String getSourceUrl() => sourceFile.filename;
118
119 int getLine() {
120 if (line == null) line = sourceFile.getLine(offset);
121 return line;
122 }
123
124 int getColumn() => sourceFile.getColumn(getLine(), offset);
125
126 String getSourceName();
127
128 bool isValid() => offset < sourceFile.length;
129
130 int get hashCode {
131 return getSourceUrl().hashCode * 17 +
132 offset.hashCode * 17 +
133 getSourceName().hashCode * 23;
134 }
135
136 bool operator ==(other) {
137 if (identical(this, other)) return true;
138 if (other is! SourceFileLocation) return false;
139 return getSourceUrl() == other.getSourceUrl() &&
140 offset == other.offset &&
141 getSourceName() == other.getSourceName();
142 }
143
144 String toString() => '${getSourceUrl()}:[${getLine()},${getColumn()}]';
145 }
146
147 class TokenSourceFileLocation extends SourceFileLocation {
148 final Token token;
149 final String name;
150
151 TokenSourceFileLocation(SourceFile sourceFile, this.token, this.name)
152 : super(sourceFile);
153
154 int get offset => token.charOffset;
155
156 String getSourceName() {
157 return name;
158 }
159
160 String toString() {
161 return '${super.toString()}:$name';
162 }
163 }
OLDNEW
« no previous file with comments | « pkg/compiler/lib/src/io/code_output.dart ('k') | pkg/compiler/lib/src/io/source_map_builder.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698