| 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 class ScannerTask extends CompilerTask { | 5 class ScannerTask extends CompilerTask { |
| 6 ScannerTask(Compiler compiler) : super(compiler); | 6 ScannerTask(Compiler compiler) : super(compiler); |
| 7 String get name() => 'Scanner'; | 7 String get name() => 'Scanner'; |
| 8 | 8 |
| 9 void scanLibrary(LibraryElement library) { | 9 void scanLibrary(LibraryElement library) { |
| 10 var compilationUnit = library.entryCompilationUnit; | 10 var compilationUnit = library.entryCompilationUnit; |
| (...skipping 18 matching lines...) Expand all Loading... |
| 29 */ | 29 */ |
| 30 int checkTag(int value, ScriptTag tag) { | 30 int checkTag(int value, ScriptTag tag) { |
| 31 if (tagState > value) { | 31 if (tagState > value) { |
| 32 compiler.reportError(tag, 'out of order'); | 32 compiler.reportError(tag, 'out of order'); |
| 33 return tagState; | 33 return tagState; |
| 34 } | 34 } |
| 35 return TagState.NEXT[value]; | 35 return TagState.NEXT[value]; |
| 36 } | 36 } |
| 37 | 37 |
| 38 LinkBuilder<ScriptTag> imports = new LinkBuilder<ScriptTag>(); | 38 LinkBuilder<ScriptTag> imports = new LinkBuilder<ScriptTag>(); |
| 39 Uri base = library.uri; | 39 Uri base = library.entryCompilationUnit.script.uri; |
| 40 for (ScriptTag tag in library.tags.reverse()) { | 40 for (ScriptTag tag in library.tags.reverse()) { |
| 41 StringNode argument = tag.argument; | 41 StringNode argument = tag.argument; |
| 42 // TODO(lrn): Support interpolations here. We need access to the | 42 // TODO(lrn): Support interpolations here. We need access to the |
| 43 // special constants that can be inserted into script tag strings. | 43 // special constants that can be inserted into script tag strings. |
| 44 Uri resolved = base.resolve(argument.dartString.slowToString()); | 44 Uri resolved = base.resolve(argument.dartString.slowToString()); |
| 45 if (tag.isImport()) { | 45 if (tag.isImport()) { |
| 46 tagState = checkTag(TagState.IMPORT, tag); | 46 tagState = checkTag(TagState.IMPORT, tag); |
| 47 // It is not safe to import other libraries at this point as | 47 // It is not safe to import other libraries at this point as |
| 48 // another library could then observe the current library | 48 // another library could then observe the current library |
| 49 // before it fully declares all the members that are sourced | 49 // before it fully declares all the members that are sourced |
| (...skipping 12 matching lines...) Expand all Loading... |
| 62 CompilationUnitElement unit = | 62 CompilationUnitElement unit = |
| 63 new CompilationUnitElement(script, library); | 63 new CompilationUnitElement(script, library); |
| 64 compiler.withCurrentElement(unit, () => scan(unit)); | 64 compiler.withCurrentElement(unit, () => scan(unit)); |
| 65 } else if (tag.isResource()) { | 65 } else if (tag.isResource()) { |
| 66 tagState = checkTag(TagState.RESOURCE, tag); | 66 tagState = checkTag(TagState.RESOURCE, tag); |
| 67 compiler.reportWarning(tag, 'ignoring resource tag'); | 67 compiler.reportWarning(tag, 'ignoring resource tag'); |
| 68 } else { | 68 } else { |
| 69 compiler.cancel("illegal script tag: ${tag.tag}", node: tag); | 69 compiler.cancel("illegal script tag: ${tag.tag}", node: tag); |
| 70 } | 70 } |
| 71 } | 71 } |
| 72 |
| 73 // Apply patch, if any. |
| 74 if (library.uri.scheme == 'dart') { |
| 75 compiler.patchDartLibrary(library, library.uri.path); |
| 76 } |
| 77 |
| 78 // Now that we have processed all the source tags, it is safe to |
| 79 // start loading other libraries. |
| 80 |
| 72 // TODO(ahe): During Compiler.scanBuiltinLibraries, | 81 // TODO(ahe): During Compiler.scanBuiltinLibraries, |
| 73 // compiler.coreLibrary is null. Clean this up when there is a | 82 // compiler.coreLibrary is null. Clean this up when there is a |
| 74 // better way to access "dart:core". | 83 // better way to access "dart:core". |
| 75 bool implicitlyImportCoreLibrary = compiler.coreLibrary !== null; | 84 bool implicitlyImportCoreLibrary = compiler.coreLibrary !== null; |
| 85 |
| 76 for (ScriptTag tag in imports.toLink()) { | 86 for (ScriptTag tag in imports.toLink()) { |
| 77 // Now that we have processed all the source tags, it is safe to | 87 Uri resolvedUri = |
| 78 // start loading other libraries. | 88 importLibraryFromTag(tag, library.entryCompilationUnit); |
| 79 StringNode argument = tag.argument; | 89 if (resolvedUri.toString() == "dart:core") { |
| 80 Uri resolved = base.resolve(argument.dartString.slowToString()); | |
| 81 if (resolved.toString() == "dart:core") { | |
| 82 implicitlyImportCoreLibrary = false; | 90 implicitlyImportCoreLibrary = false; |
| 83 } | 91 } |
| 84 LibraryElement importedLibrary = loadLibrary(resolved, argument); | |
| 85 importLibrary(library, importedLibrary, tag); | |
| 86 if (resolved.scheme == "dart") { | |
| 87 compiler.patchDartLibrary(importedLibrary, resolved.path); | |
| 88 } | |
| 89 } | 92 } |
| 90 if (implicitlyImportCoreLibrary) { | 93 if (implicitlyImportCoreLibrary) { |
| 91 importLibrary(library, compiler.coreLibrary, null); | 94 importLibrary(library, compiler.coreLibrary, null); |
| 92 } | 95 } |
| 93 } | 96 } |
| 94 | 97 |
| 98 /** |
| 99 * Handle an import script tag by importing the referenced library into the |
| 100 * current library. |
| 101 * Returns the resolved library [Uri]. |
| 102 */ |
| 103 Uri importLibraryFromTag(ScriptTag tag, |
| 104 CompilationUnitElement compilationUnit) { |
| 105 Uri base = compilationUnit.script.uri; |
| 106 StringNode argument = tag.argument; |
| 107 Uri resolved = base.resolve(argument.dartString.slowToString()); |
| 108 LibraryElement importedLibrary = loadLibrary(resolved, argument, resolved); |
| 109 importLibrary(compilationUnit.getLibrary(), |
| 110 importedLibrary, |
| 111 tag, |
| 112 compilationUnit); |
| 113 return resolved; |
| 114 } |
| 115 |
| 95 void scanElements(CompilationUnitElement compilationUnit) { | 116 void scanElements(CompilationUnitElement compilationUnit) { |
| 96 Script script = compilationUnit.script; | 117 Script script = compilationUnit.script; |
| 97 Token tokens; | 118 Token tokens; |
| 98 try { | 119 try { |
| 99 tokens = new StringScanner(script.text).tokenize(); | 120 tokens = new StringScanner(script.text).tokenize(); |
| 100 } catch (MalformedInputException ex) { | 121 } catch (MalformedInputException ex) { |
| 101 Token token; | 122 Token token; |
| 102 var message; | 123 var message; |
| 103 if (ex.position is num) { | 124 if (ex.position is num) { |
| 104 // TODO(ahe): Always use tokens in MalformedInputException. | 125 // TODO(ahe): Always use tokens in MalformedInputException. |
| 105 token = new Token(EOF_INFO, ex.position); | 126 token = new Token(EOF_INFO, ex.position); |
| 106 } else { | 127 } else { |
| 107 token = ex.position; | 128 token = ex.position; |
| 108 } | 129 } |
| 109 compiler.cancel(ex.message, token: token); | 130 compiler.cancel(ex.message, token: token); |
| 110 } | 131 } |
| 111 compiler.dietParser.dietParse(compilationUnit, tokens); | 132 compiler.dietParser.dietParse(compilationUnit, tokens); |
| 112 } | 133 } |
| 113 | 134 |
| 114 LibraryElement loadLibrary(Uri uri, Node node) { | 135 LibraryElement loadLibrary(Uri uri, Node node, Uri canonicalUri) { |
| 115 bool newLibrary = false; | 136 bool newLibrary = false; |
| 116 LibraryElement library = | 137 LibraryElement library = |
| 117 compiler.libraries.putIfAbsent(uri.toString(), () { | 138 compiler.libraries.putIfAbsent(uri.toString(), () { |
| 118 newLibrary = true; | 139 newLibrary = true; |
| 119 Script script = compiler.readScript(uri, node); | 140 Script script = compiler.readScript(uri, node); |
| 120 LibraryElement element = new LibraryElement(script); | 141 LibraryElement element = new LibraryElement(script, canonicalUri); |
| 121 native.maybeEnableNative(compiler, element, uri); | 142 native.maybeEnableNative(compiler, element, uri); |
| 122 return element; | 143 return element; |
| 123 }); | 144 }); |
| 124 if (newLibrary) { | 145 if (newLibrary) { |
| 125 compiler.withCurrentElement(library, () { | 146 compiler.withCurrentElement(library, () { |
| 126 scanLibrary(library); | 147 scanLibrary(library); |
| 127 compiler.onLibraryLoaded(library, uri); | 148 compiler.onLibraryLoaded(library, uri); |
| 128 }); | 149 }); |
| 129 } | 150 } |
| 130 return library; | 151 return library; |
| 131 } | 152 } |
| 132 | 153 |
| 133 void importLibrary(LibraryElement library, LibraryElement imported, | 154 void importLibrary(LibraryElement library, LibraryElement imported, |
| 134 ScriptTag tag, [CompilationUnitElement override]) { | 155 ScriptTag tag, [CompilationUnitElement compilationUnit]) { |
| 135 if (!imported.hasLibraryName()) { | 156 if (!imported.hasLibraryName()) { |
| 136 compiler.withCurrentElement(library, () { | 157 compiler.withCurrentElement(library, () { |
| 137 compiler.reportError(tag === null ? null : tag.argument, | 158 compiler.reportError(tag === null ? null : tag.argument, |
| 138 'no #library tag found in ${imported.uri}'); | 159 'no #library tag found in ${imported.uri}'); |
| 139 }); | 160 }); |
| 140 } | 161 } |
| 141 if (tag !== null && tag.prefix !== null) { | 162 if (tag !== null && tag.prefix !== null) { |
| 142 SourceString prefix = | 163 SourceString prefix = |
| 143 new SourceString(tag.prefix.dartString.slowToString()); | 164 new SourceString(tag.prefix.dartString.slowToString()); |
| 144 Element e = library.find(prefix); | 165 Element e = library.find(prefix); |
| 145 if (e === null) { | 166 if (e === null) { |
| 146 Element enclosing = library; | 167 if (compilationUnit === null) { |
| 147 if (override !== null) { | 168 compilationUnit = library.entryCompilationUnit; |
| 148 enclosing = new CompilationUnitOverrideElement(override, library); | |
| 149 } | 169 } |
| 150 e = new PrefixElement(prefix, enclosing, tag.getBeginToken()); | 170 e = new PrefixElement(prefix, compilationUnit, tag.getBeginToken()); |
| 151 library.addToScope(e, compiler); | 171 library.addToScope(e, compiler); |
| 152 } | 172 } |
| 153 if (e.kind !== ElementKind.PREFIX) { | 173 if (e.kind !== ElementKind.PREFIX) { |
| 154 compiler.withCurrentElement(e, () { | 174 compiler.withCurrentElement(e, () { |
| 155 compiler.reportWarning(new Identifier(e.position()), | 175 compiler.reportWarning(new Identifier(e.position()), |
| 156 'duplicated definition'); | 176 'duplicated definition'); |
| 157 }); | 177 }); |
| 158 compiler.reportError(tag.prefix, 'duplicate defintion'); | 178 compiler.reportError(tag.prefix, 'duplicate defintion'); |
| 159 } | 179 } |
| 160 PrefixElement prefixElement = e; | 180 PrefixElement prefixElement = e; |
| (...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 209 static final int RESOURCE = 4; | 229 static final int RESOURCE = 4; |
| 210 | 230 |
| 211 /** Next state. */ | 231 /** Next state. */ |
| 212 static final List<int> NEXT = | 232 static final List<int> NEXT = |
| 213 const <int>[NO_TAG_SEEN, | 233 const <int>[NO_TAG_SEEN, |
| 214 IMPORT, // Only one library tag is allowed. | 234 IMPORT, // Only one library tag is allowed. |
| 215 IMPORT, | 235 IMPORT, |
| 216 SOURCE, | 236 SOURCE, |
| 217 RESOURCE]; | 237 RESOURCE]; |
| 218 } | 238 } |
| OLD | NEW |