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