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 /** | 5 /** |
6 * Datatypes holding information extracted by the analyzer and used by later | 6 * Datatypes holding information extracted by the analyzer and used by later |
7 * phases of the compiler. | 7 * phases of the compiler. |
8 */ | 8 */ |
9 library info; | 9 library info; |
10 | 10 |
11 import 'dart:collection' show SplayTreeMap, LinkedHashMap; | 11 import 'dart:collection' show SplayTreeMap, LinkedHashMap; |
12 import 'dart:uri'; | 12 import 'dart:uri'; |
13 | 13 |
14 import 'package:html5lib/dom.dart'; | 14 import 'package:html5lib/dom.dart'; |
15 import 'package:analyzer_experimental/src/generated/ast.dart'; | |
15 import 'package:csslib/parser.dart' as css; | 16 import 'package:csslib/parser.dart' as css; |
16 import 'package:csslib/visitor.dart'; | 17 import 'package:csslib/visitor.dart'; |
17 | 18 |
19 import 'dart_parser.dart' show DartCodeInfo; | |
18 import 'file_system/path.dart'; | 20 import 'file_system/path.dart'; |
19 import 'files.dart'; | 21 import 'files.dart'; |
20 import 'messages.dart'; | 22 import 'messages.dart'; |
21 import 'utils.dart'; | 23 import 'utils.dart'; |
22 | 24 |
23 /** Information about input, base, and output path locations. */ | 25 /** Information about input, base, and output path locations. */ |
24 class PathInfo { | 26 class PathInfo { |
25 /** | 27 /** |
26 * Common prefix to all input paths that are read from the file system. The | 28 * Common prefix to all input paths that are read from the file system. The |
27 * output generated by the compiler will reflect the directory structure | 29 * output generated by the compiler will reflect the directory structure |
(...skipping 77 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
105 * the form `<link rel="component" href="packages/...">`, so that you can | 107 * the form `<link rel="component" href="packages/...">`, so that you can |
106 * refer to components within the packages symlink. Regardless of whether an | 108 * refer to components within the packages symlink. Regardless of whether an |
107 * --out option was given to the compiler, we don't want to generate files | 109 * --out option was given to the compiler, we don't want to generate files |
108 * inside `packages/` for those components. Instead we will generate such | 110 * inside `packages/` for those components. Instead we will generate such |
109 * code in a special directory called `_from_packages/`. | 111 * code in a special directory called `_from_packages/`. |
110 */ | 112 */ |
111 Path _rewritePackages(Path outputPath) { | 113 Path _rewritePackages(Path outputPath) { |
112 if (!outputPath.toString().contains('packages')) return outputPath; | 114 if (!outputPath.toString().contains('packages')) return outputPath; |
113 var segments = outputPath.segments().map( | 115 var segments = outputPath.segments().map( |
114 (segment) => segment == 'packages' ? '_from_packages' : segment); | 116 (segment) => segment == 'packages' ? '_from_packages' : segment); |
115 return new Path(segments.join('/')); | 117 var rewrittenPath = segments.join('/'); |
118 if (outputPath.isAbsolute) { | |
119 // TODO(jmesserly): this is probably broken on Windows | |
120 // We need to switch to package:pathos | |
121 rewrittenPath = '/$rewrittenPath'; | |
122 } | |
123 return new Path(rewrittenPath); | |
116 } | 124 } |
117 | 125 |
118 /** | 126 /** |
119 * Returns a relative path to import/export the output library represented by | 127 * Returns a relative path to import/export the output library represented by |
120 * [target] from the output library of [src]. In other words, a path to import | 128 * [target] from the output library of [src]. In other words, a path to import |
121 * or export `target.outputFilename` from `src.outputFilename`. | 129 * or export `target.outputFilename` from `src.outputFilename`. |
122 */ | 130 */ |
123 Path relativePath(LibraryInfo src, LibraryInfo target) { | 131 Path relativePath(LibraryInfo src, LibraryInfo target) { |
124 var srcDir = src.inputPath.directoryPath; | 132 var srcDir = src.inputPath.directoryPath; |
125 var relDir = target.inputPath.directoryPath.relativeTo(srcDir); | 133 var relDir = target.inputPath.directoryPath.relativeTo(srcDir); |
(...skipping 30 matching lines...) Expand all Loading... | |
156 * Information for any library-like input. We consider each HTML file a library, | 164 * Information for any library-like input. We consider each HTML file a library, |
157 * and each component declaration a library as well. Hence we use this as a base | 165 * and each component declaration a library as well. Hence we use this as a base |
158 * class for both [FileInfo] and [ComponentInfo]. Both HTML files and components | 166 * class for both [FileInfo] and [ComponentInfo]. Both HTML files and components |
159 * can have .dart code provided by the user for top-level user scripts and | 167 * can have .dart code provided by the user for top-level user scripts and |
160 * component-level behavior code. This code can either be inlined in the HTML | 168 * component-level behavior code. This code can either be inlined in the HTML |
161 * file or included in a script tag with the "src" attribute. | 169 * file or included in a script tag with the "src" attribute. |
162 */ | 170 */ |
163 abstract class LibraryInfo { | 171 abstract class LibraryInfo { |
164 | 172 |
165 /** Whether there is any code associated with the page/component. */ | 173 /** Whether there is any code associated with the page/component. */ |
166 bool get codeAttached => inlinedCode != null || externalFile != null; | 174 bool get codeAttached => userCode != null || externalFile != null; |
Siggi Cherem (dart-lang)
2013/02/13 19:28:54
I can see how this works, but it's a bit confusing
Jennifer Messerly
2013/02/14 00:38:09
Good idea! That will simplify things. :)
| |
167 | 175 |
168 /** | 176 /** |
169 * The actual code, either inlined or from an external file, or `null` if none | 177 * The actual code, either inlined or from an external file, or `null` if none |
170 * was defined. | 178 * was defined. |
171 */ | 179 */ |
172 DartCodeInfo userCode; | 180 DartCodeInfo userCode; |
173 | 181 |
174 /** The inlined code, if any. */ | |
175 String inlinedCode; | |
176 | |
177 /** The name of the file sourced in a script tag, if any. */ | 182 /** The name of the file sourced in a script tag, if any. */ |
178 Path externalFile; | 183 Path externalFile; |
179 | 184 |
180 /** Info asscociated with [externalFile], if any. */ | 185 /** Info asscociated with [externalFile], if any. */ |
181 FileInfo externalCode; | 186 FileInfo externalCode; |
182 | 187 |
188 /** | |
189 * The inverse of [externalCode]. If this .dart file was imported via a script | |
190 * tag, this refers to the HTML file that imported it. | |
191 */ | |
192 LibraryInfo htmlFile; | |
193 | |
183 /** File where the top-level code was defined. */ | 194 /** File where the top-level code was defined. */ |
184 Path get inputPath; | 195 Path get inputPath; |
185 | 196 |
186 /** Stylesheet with <style>...</style> */ | 197 /** Stylesheet with <style>...</style> */ |
187 StringBuffer cssSource = new StringBuffer(); | 198 StringBuffer cssSource = new StringBuffer(); |
188 | 199 |
189 /** Parsed cssSource. */ | 200 /** Parsed cssSource. */ |
190 StyleSheet styleSheet; | 201 StyleSheet styleSheet; |
191 | 202 |
192 /** | 203 /** |
193 * Name of the file that will hold any generated Dart code for this library | 204 * Name of the file that will hold any generated Dart code for this library |
194 * unit. | 205 * unit. |
195 */ | 206 */ |
196 String _getOutputFilename(NameMangler mangle); | 207 String _getOutputFilename(NameMangler mangle); |
197 | 208 |
209 /** This is used in transforming Dart code to track modified files. */ | |
210 bool modified = false; | |
211 | |
212 /** | |
213 * This is used in transforming Dart code to compute files that reference | |
214 * [modified] files. | |
215 */ | |
216 List<FileInfo> referencedBy = []; | |
217 | |
198 /** | 218 /** |
199 * Components used within this library unit. For [FileInfo] these are | 219 * Components used within this library unit. For [FileInfo] these are |
200 * components used directly in the page. For [ComponentInfo] these are | 220 * components used directly in the page. For [ComponentInfo] these are |
201 * components used within their shadowed template. | 221 * components used within their shadowed template. |
202 */ | 222 */ |
203 final Map<ComponentInfo, bool> usedComponents = | 223 final Map<ComponentInfo, bool> usedComponents = |
204 new LinkedHashMap<ComponentInfo, bool>(); | 224 new LinkedHashMap<ComponentInfo, bool>(); |
205 } | 225 } |
206 | 226 |
207 /** Information extracted at the file-level. */ | 227 /** Information extracted at the file-level. */ |
(...skipping 417 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
625 | 645 |
626 /** | 646 /** |
627 * Specifies the action to take on a particular event. Some actions need to read | 647 * Specifies the action to take on a particular event. Some actions need to read |
628 * attributes from the DOM element that has the event listener (e.g. two way | 648 * attributes from the DOM element that has the event listener (e.g. two way |
629 * bindings do this). [elementVarName] stores a reference to this element. | 649 * bindings do this). [elementVarName] stores a reference to this element. |
630 * It is generated outside of the analyzer (in the emitter), so it is passed | 650 * It is generated outside of the analyzer (in the emitter), so it is passed |
631 * here as an argument. | 651 * here as an argument. |
632 */ | 652 */ |
633 typedef String ActionDefinition(String elemVarName); | 653 typedef String ActionDefinition(String elemVarName); |
634 | 654 |
635 /** Information extracted from a source Dart file. */ | |
636 class DartCodeInfo { | |
637 /** Library qualified identifier, if any. */ | |
638 final String libraryName; | |
639 | |
640 /** Library which the code is part-of, if any. */ | |
641 final String partOf; | |
642 | |
643 /** Declared imports, exports, and parts. */ | |
644 final List<DartDirectiveInfo> directives; | |
645 | |
646 /** The rest of the code. */ | |
647 final String code; | |
648 | |
649 DartCodeInfo(this.libraryName, this.partOf, this.directives, this.code); | |
650 } | |
651 | |
652 /** Information about a single import/export/part directive. */ | |
653 class DartDirectiveInfo { | |
654 /** Directive's label: import, export, or part. */ | |
655 String label; | |
656 | |
657 /** Referenced uri being imported, exported, or included by a part. */ | |
658 String uri; | |
659 | |
660 /** Prefix used for imports, if any. */ | |
661 String prefix; | |
662 | |
663 /** Hidden identifiers. */ | |
664 List<String> hide; | |
665 | |
666 /** Shown identifiers. */ | |
667 List<String> show; | |
668 | |
669 DartDirectiveInfo(this.label, this.uri, [this.prefix, this.hide, this.show]); | |
670 } | |
671 | |
672 | 655 |
673 /** | 656 /** |
674 * Find ElementInfo that associated with a particular DOM node. | 657 * Find ElementInfo that associated with a particular DOM node. |
675 * Used by [ElementInfo.query]. | 658 * Used by [ElementInfo.query]. |
676 */ | 659 */ |
677 class _QueryInfo extends InfoVisitor { | 660 class _QueryInfo extends InfoVisitor { |
678 final String _tagName; | 661 final String _tagName; |
679 | 662 |
680 _QueryInfo(this._tagName); | 663 _QueryInfo(this._tagName); |
681 | 664 |
682 visitElementInfo(ElementInfo info) { | 665 visitElementInfo(ElementInfo info) { |
683 if (info.node.tagName == _tagName) { | 666 if (info.node.tagName == _tagName) { |
684 return info; | 667 return info; |
685 } | 668 } |
686 | 669 |
687 return super.visitElementInfo(info); | 670 return super.visitElementInfo(info); |
688 } | 671 } |
689 | 672 |
690 visitChildren(ElementInfo info) { | 673 visitChildren(ElementInfo info) { |
691 for (var child in info.children) { | 674 for (var child in info.children) { |
692 var result = visit(child); | 675 var result = visit(child); |
693 if (result != null) return result; | 676 if (result != null) return result; |
694 } | 677 } |
695 return null; | 678 return null; |
696 } | 679 } |
697 } | 680 } |
OLD | NEW |