| 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; |
| 11 compiler.log("scanning library ${compilationUnit.script.name}"); | 11 compiler.log("scanning library ${compilationUnit.script.name}"); |
| 12 scan(compilationUnit); | 12 scan(compilationUnit); |
| 13 processLibraryTags(library); | 13 processScriptTags(library); |
| 14 } | 14 } |
| 15 | 15 |
| 16 void scan(CompilationUnitElement compilationUnit) { | 16 void scan(CompilationUnitElement compilationUnit) { |
| 17 measure(() { | 17 measure(() { |
| 18 scanElements(compilationUnit); | 18 scanElements(compilationUnit); |
| 19 }); | 19 }); |
| 20 } | 20 } |
| 21 | 21 |
| 22 void processLibraryTags(LibraryElement library) { | 22 void processScriptTags(LibraryElement library) { |
| 23 int tagState = TagState.NO_TAG_SEEN; | 23 int tagState = TagState.NO_TAG_SEEN; |
| 24 | 24 |
| 25 /** | 25 /** |
| 26 * If [value] is less than [tagState] complain and return | 26 * If [value] is less than [tagState] complain and return |
| 27 * [tagState]. Otherwise return the new value for [tagState] | 27 * [tagState]. Otherwise return the new value for [tagState] |
| 28 * (transition function for state machine). | 28 * (transition function for state machine). |
| 29 */ | 29 */ |
| 30 int checkTag(int value, LibraryTag 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<Import> imports = new LinkBuilder<Import>(); | 38 LinkBuilder<ScriptTag> imports = new LinkBuilder<ScriptTag>(); |
| 39 Uri base = library.entryCompilationUnit.script.uri; | 39 Uri base = library.entryCompilationUnit.script.uri; |
| 40 for (LibraryTag tag in library.tags.reverse()) { | 40 for (ScriptTag tag in library.tags.reverse()) { |
| 41 if (tag.isImport) { | 41 StringNode argument = tag.argument; |
| 42 // TODO(lrn): Support interpolations here. We need access to the |
| 43 // special constants that can be inserted into script tag strings. |
| 44 Uri resolved = base.resolve(argument.dartString.slowToString()); |
| 45 if (tag.isImport()) { |
| 42 tagState = checkTag(TagState.IMPORT, tag); | 46 tagState = checkTag(TagState.IMPORT, tag); |
| 43 // 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 |
| 44 // another library could then observe the current library | 48 // another library could then observe the current library |
| 45 // before it fully declares all the members that are sourced | 49 // before it fully declares all the members that are sourced |
| 46 // in. | 50 // in. |
| 47 imports.addLast(tag); | 51 imports.addLast(tag); |
| 48 } else if (tag.isLibraryName) { | 52 } else if (tag.isLibrary()) { |
| 49 tagState = checkTag(TagState.LIBRARY, tag); | 53 tagState = checkTag(TagState.LIBRARY, tag); |
| 50 if (library.libraryTag !== null) { | 54 if (library.libraryTag !== null) { |
| 51 compiler.cancel("duplicated library declaration", node: tag); | 55 compiler.cancel("duplicated library declaration", node: tag); |
| 52 } else { | 56 } else { |
| 53 library.libraryTag = tag; | 57 library.libraryTag = tag; |
| 54 } | 58 } |
| 55 } else if (tag.isPart) { | 59 } else if (tag.isSource()) { |
| 56 StringNode uri = tag.uri; | |
| 57 Uri resolved = base.resolve(uri.dartString.slowToString()); | |
| 58 tagState = checkTag(TagState.SOURCE, tag); | 60 tagState = checkTag(TagState.SOURCE, tag); |
| 59 loadPart(tag, resolved, library); | 61 importSourceFromTag(tag, resolved, library); |
| 60 } else { | 62 } else { |
| 61 compiler.cancel("illegal script tag: ${tag.tag}", node: tag); | 63 compiler.cancel("illegal script tag: ${tag.tag}", node: tag); |
| 62 } | 64 } |
| 63 } | 65 } |
| 64 | 66 |
| 65 // Apply patch, if any. | 67 // Apply patch, if any. |
| 66 if (library.uri.scheme == 'dart') { | 68 if (library.uri.scheme == 'dart') { |
| 67 compiler.patchDartLibrary(library, library.uri.path); | 69 compiler.patchDartLibrary(library, library.uri.path); |
| 68 } | 70 } |
| 69 | 71 |
| 70 // Now that we have processed all the source tags, it is safe to | 72 // Now that we have processed all the source tags, it is safe to |
| 71 // start loading other libraries. | 73 // start loading other libraries. |
| 72 | 74 |
| 73 if (library.uri.scheme != 'dart' || library.uri.path != 'core') { | 75 if (library.uri.scheme != 'dart' || library.uri.path != 'core') { |
| 74 compiler.importCoreLibrary(library); | 76 compiler.importCoreLibrary(library); |
| 75 } | 77 } |
| 76 | 78 |
| 77 for (Import tag in imports.toLink()) { | 79 for (ScriptTag tag in imports.toLink()) { |
| 78 importLibraryFromTag(tag, library.entryCompilationUnit); | 80 importLibraryFromTag(tag, library.entryCompilationUnit); |
| 79 } | 81 } |
| 80 } | 82 } |
| 81 | 83 |
| 82 /** | 84 /** |
| 83 * Handle a part tag in the scope of [library]. The [path] given is used as | 85 * Handle a source tag in the scope of [library]. The [path] given is used as |
| 84 * is, any resolution should be done beforehand. | 86 * is, any resolution should be done beforehand. |
| 85 */ | 87 */ |
| 86 void loadPart(Part part, Uri path, LibraryElement library) { | 88 void importSourceFromTag(ScriptTag tag, Uri path, LibraryElement library) { |
| 87 Script sourceScript = compiler.readScript(path, part); | 89 Script sourceScript = compiler.readScript(path, tag); |
| 88 CompilationUnitElement unit = | 90 CompilationUnitElement unit = |
| 89 new CompilationUnitElement(sourceScript, library); | 91 new CompilationUnitElement(sourceScript, library); |
| 90 compiler.withCurrentElement(unit, () => compiler.scanner.scan(unit)); | 92 compiler.withCurrentElement(unit, () => compiler.scanner.scan(unit)); |
| 91 } | 93 } |
| 92 | 94 |
| 93 /** | 95 /** |
| 94 * Handle an import script tag by importing the referenced library into the | 96 * Handle an import script tag by importing the referenced library into the |
| 95 * current library. | 97 * current library. |
| 96 * Returns the resolved library [Uri]. | 98 * Returns the resolved library [Uri]. |
| 97 */ | 99 */ |
| 98 Uri importLibraryFromTag(Import tag, | 100 Uri importLibraryFromTag(ScriptTag tag, |
| 99 CompilationUnitElement compilationUnit) { | 101 CompilationUnitElement compilationUnit) { |
| 100 Uri base = compilationUnit.script.uri; | 102 Uri base = compilationUnit.script.uri; |
| 101 Uri resolved = base.resolve(tag.uri.dartString.slowToString()); | 103 StringNode argument = tag.argument; |
| 102 LibraryElement importedLibrary = loadLibrary(resolved, tag.uri, resolved); | 104 Uri resolved = base.resolve(argument.dartString.slowToString()); |
| 105 LibraryElement importedLibrary = loadLibrary(resolved, argument, resolved); |
| 103 importLibrary(compilationUnit.getLibrary(), | 106 importLibrary(compilationUnit.getLibrary(), |
| 104 importedLibrary, | 107 importedLibrary, |
| 105 tag, | 108 tag, |
| 106 compilationUnit); | 109 compilationUnit); |
| 107 return resolved; | 110 return resolved; |
| 108 } | 111 } |
| 109 | 112 |
| 110 void scanElements(CompilationUnitElement compilationUnit) { | 113 void scanElements(CompilationUnitElement compilationUnit) { |
| 111 Script script = compilationUnit.script; | 114 Script script = compilationUnit.script; |
| 112 Token tokens = new StringScanner(script.text).tokenize(); | 115 Token tokens = new StringScanner(script.text).tokenize(); |
| (...skipping 13 matching lines...) Expand all Loading... |
| 126 if (newLibrary) { | 129 if (newLibrary) { |
| 127 compiler.withCurrentElement(library, () { | 130 compiler.withCurrentElement(library, () { |
| 128 scanLibrary(library); | 131 scanLibrary(library); |
| 129 compiler.onLibraryLoaded(library, uri); | 132 compiler.onLibraryLoaded(library, uri); |
| 130 }); | 133 }); |
| 131 } | 134 } |
| 132 return library; | 135 return library; |
| 133 } | 136 } |
| 134 | 137 |
| 135 void importLibrary(LibraryElement library, LibraryElement imported, | 138 void importLibrary(LibraryElement library, LibraryElement imported, |
| 136 Import tag, [CompilationUnitElement compilationUnit]) { | 139 ScriptTag tag, [CompilationUnitElement compilationUnit]) { |
| 137 if (!imported.hasLibraryName()) { | 140 if (!imported.hasLibraryName()) { |
| 138 compiler.withCurrentElement(library, () { | 141 compiler.withCurrentElement(library, () { |
| 139 compiler.reportError(tag === null ? null : tag.uri, | 142 compiler.reportError(tag === null ? null : tag.argument, |
| 140 'no #library tag found in ${imported.uri}'); | 143 'no #library tag found in ${imported.uri}'); |
| 141 }); | 144 }); |
| 142 } | 145 } |
| 143 if (tag !== null && tag.prefix !== null) { | 146 if (tag !== null && tag.prefix !== null) { |
| 144 SourceString prefix = tag.prefix.source; | 147 SourceString prefix = |
| 148 new SourceString(tag.prefix.dartString.slowToString()); |
| 145 Element e = library.find(prefix); | 149 Element e = library.find(prefix); |
| 146 if (e === null) { | 150 if (e === null) { |
| 147 if (compilationUnit === null) { | 151 if (compilationUnit === null) { |
| 148 compilationUnit = library.entryCompilationUnit; | 152 compilationUnit = library.entryCompilationUnit; |
| 149 } | 153 } |
| 150 e = new PrefixElement(prefix, compilationUnit, tag.getBeginToken()); | 154 e = new PrefixElement(prefix, compilationUnit, tag.getBeginToken()); |
| 151 library.addToScope(e, compiler); | 155 library.addToScope(e, compiler); |
| 152 } | 156 } |
| 153 if (e.kind !== ElementKind.PREFIX) { | 157 if (e.kind !== ElementKind.PREFIX) { |
| 154 compiler.withCurrentElement(e, () { | 158 compiler.withCurrentElement(e, () { |
| (...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 209 static const int RESOURCE = 4; | 213 static const int RESOURCE = 4; |
| 210 | 214 |
| 211 /** Next state. */ | 215 /** Next state. */ |
| 212 static const List<int> NEXT = | 216 static const List<int> NEXT = |
| 213 const <int>[NO_TAG_SEEN, | 217 const <int>[NO_TAG_SEEN, |
| 214 IMPORT, // Only one library tag is allowed. | 218 IMPORT, // Only one library tag is allowed. |
| 215 IMPORT, | 219 IMPORT, |
| 216 SOURCE, | 220 SOURCE, |
| 217 RESOURCE]; | 221 RESOURCE]; |
| 218 } | 222 } |
| OLD | NEW |