| 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 scan(CompilationUnitElement compilationUnit) { | 9 void scan(CompilationUnitElement compilationUnit) { |
| 10 measure(() { | 10 measure(() { |
| 11 if (compilationUnit.kind === ElementKind.LIBRARY) { | 11 if (compilationUnit.kind === ElementKind.LIBRARY) { |
| 12 compiler.log("scanning library ${compilationUnit.script.name}"); | 12 compiler.log("scanning library ${compilationUnit.script.name}"); |
| 13 } | 13 } |
| 14 scanElements(compilationUnit); | 14 scanElements(compilationUnit); |
| 15 if (compilationUnit.kind === ElementKind.LIBRARY) { | 15 if (compilationUnit.kind === ElementKind.LIBRARY) { |
| 16 processScriptTags(compilationUnit); | 16 processScriptTags(compilationUnit); |
| 17 } | 17 } |
| 18 }); | 18 }); |
| 19 } | 19 } |
| 20 | 20 |
| 21 void processScriptTags(LibraryElement library) { | 21 void processScriptTags(LibraryElement library) { |
| 22 int tagState = TagState.NO_TAG_SEEN; |
| 23 |
| 24 /** |
| 25 * If [value] is less than [tagState] complain and return |
| 26 * [tagState]. Otherwise return the new value for [tagState] |
| 27 * (transition function for state machine). |
| 28 */ |
| 29 int checkTag(int value, ScriptTag tag) { |
| 30 if (tagState > value) { |
| 31 compiler.reportError(tag, 'out of order'); |
| 32 return tagState; |
| 33 } |
| 34 return TagState.NEXT[value]; |
| 35 } |
| 36 |
| 22 LinkBuilder<ScriptTag> imports = new LinkBuilder<ScriptTag>(); | 37 LinkBuilder<ScriptTag> imports = new LinkBuilder<ScriptTag>(); |
| 23 Uri cwd = new Uri(scheme: 'file', path: compiler.currentDirectory); | 38 Uri cwd = new Uri(scheme: 'file', path: compiler.currentDirectory); |
| 24 Uri base = cwd.resolve(library.script.name.toString()); | 39 Uri base = cwd.resolve(library.script.name.toString()); |
| 25 for (ScriptTag tag in library.tags.reverse()) { | 40 for (ScriptTag tag in library.tags.reverse()) { |
| 26 StringNode argument = tag.argument; | 41 StringNode argument = tag.argument; |
| 27 // TODO(lrn): Support interpolations here. We need access to the | 42 // TODO(lrn): Support interpolations here. We need access to the |
| 28 // special constants that can be inserted into script tag strings. | 43 // special constants that can be inserted into script tag strings. |
| 29 Uri resolved = base.resolve(argument.dartString.slowToString()); | 44 Uri resolved = base.resolve(argument.dartString.slowToString()); |
| 30 if (tag.isImport()) { | 45 if (tag.isImport()) { |
| 46 tagState = checkTag(TagState.IMPORT, tag); |
| 31 // 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 |
| 32 // another library could then observe the current library | 48 // another library could then observe the current library |
| 33 // before it fully declares all the members that are sourced | 49 // before it fully declares all the members that are sourced |
| 34 // in. | 50 // in. |
| 35 imports.addLast(tag); | 51 imports.addLast(tag); |
| 36 } else if (tag.isLibrary()) { | 52 } else if (tag.isLibrary()) { |
| 53 tagState = checkTag(TagState.LIBRARY, tag); |
| 37 if (library.libraryTag !== null) { | 54 if (library.libraryTag !== null) { |
| 38 compiler.cancel("duplicated library declaration", node: tag); | 55 compiler.cancel("duplicated library declaration", node: tag); |
| 39 } else { | 56 } else { |
| 40 library.libraryTag = tag; | 57 library.libraryTag = tag; |
| 41 } | 58 } |
| 42 } else if (tag.isSource()) { | 59 } else if (tag.isSource()) { |
| 60 tagState = checkTag(TagState.SOURCE, tag); |
| 43 Script script = compiler.readScript(resolved, tag); | 61 Script script = compiler.readScript(resolved, tag); |
| 44 CompilationUnitElement unit = | 62 CompilationUnitElement unit = |
| 45 new CompilationUnitElement(script, library); | 63 new CompilationUnitElement(script, library); |
| 46 compiler.withCurrentElement(unit, () => scan(unit)); | 64 compiler.withCurrentElement(unit, () => scan(unit)); |
| 65 } else if (tag.isResource()) { |
| 66 tagState = checkTag(TagState.RESOURCE, tag); |
| 67 compiler.reportWarning(tag, 'ignoring resource tag'); |
| 47 } else { | 68 } else { |
| 48 compiler.cancel("illegal script tag: ${tag.tag}", node: tag); | 69 compiler.cancel("illegal script tag: ${tag.tag}", node: tag); |
| 49 } | 70 } |
| 50 } | 71 } |
| 51 // TODO(ahe): During Compiler.scanBuiltinLibraries, | 72 // TODO(ahe): During Compiler.scanBuiltinLibraries, |
| 52 // compiler.coreLibrary is null. Clean this up when there is a | 73 // compiler.coreLibrary is null. Clean this up when there is a |
| 53 // better way to access "dart:core". | 74 // better way to access "dart:core". |
| 54 bool implicitlyImportCoreLibrary = compiler.coreLibrary !== null; | 75 bool implicitlyImportCoreLibrary = compiler.coreLibrary !== null; |
| 55 for (ScriptTag tag in imports.toLink()) { | 76 for (ScriptTag tag in imports.toLink()) { |
| 56 // Now that we have processed all the source tags, it is safe to | 77 // Now that we have processed all the source tags, it is safe to |
| (...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 98 }); | 119 }); |
| 99 if (newLibrary) { | 120 if (newLibrary) { |
| 100 compiler.withCurrentElement(library, () => scan(library)); | 121 compiler.withCurrentElement(library, () => scan(library)); |
| 101 compiler.onLibraryLoaded(library, uri); | 122 compiler.onLibraryLoaded(library, uri); |
| 102 } | 123 } |
| 103 return library; | 124 return library; |
| 104 } | 125 } |
| 105 | 126 |
| 106 void importLibrary(LibraryElement library, LibraryElement imported, | 127 void importLibrary(LibraryElement library, LibraryElement imported, |
| 107 ScriptTag tag) { | 128 ScriptTag tag) { |
| 129 if (!imported.hasLibraryName()) { |
| 130 compiler.withCurrentElement(library, () { |
| 131 compiler.reportError(tag, |
| 132 'no #library tag found in ${imported.script.uri}'); |
| 133 }); |
| 134 } |
| 108 if (tag !== null && tag.prefix !== null) { | 135 if (tag !== null && tag.prefix !== null) { |
| 109 SourceString prefix = | 136 SourceString prefix = |
| 110 new SourceString(tag.prefix.dartString.slowToString()); | 137 new SourceString(tag.prefix.dartString.slowToString()); |
| 111 Element e = library.find(prefix); | 138 Element e = library.find(prefix); |
| 112 if (e === null) { | 139 if (e === null) { |
| 113 e = new PrefixElement(prefix, library, tag.getBeginToken()); | 140 e = new PrefixElement(prefix, library, tag.getBeginToken()); |
| 114 library.define(e, compiler); | 141 library.define(e, compiler); |
| 115 } | 142 } |
| 116 if (e.kind !== ElementKind.PREFIX) { | 143 if (e.kind !== ElementKind.PREFIX) { |
| 117 compiler.withCurrentElement(e, () { | 144 compiler.withCurrentElement(e, () { |
| (...skipping 30 matching lines...) Expand all Loading... |
| 148 final String name = 'Diet Parser'; | 175 final String name = 'Diet Parser'; |
| 149 | 176 |
| 150 dietParse(CompilationUnitElement compilationUnit, Token tokens) { | 177 dietParse(CompilationUnitElement compilationUnit, Token tokens) { |
| 151 measure(() { | 178 measure(() { |
| 152 ElementListener listener = new ElementListener(compiler, compilationUnit); | 179 ElementListener listener = new ElementListener(compiler, compilationUnit); |
| 153 PartialParser parser = new PartialParser(listener); | 180 PartialParser parser = new PartialParser(listener); |
| 154 parser.parseUnit(tokens); | 181 parser.parseUnit(tokens); |
| 155 }); | 182 }); |
| 156 } | 183 } |
| 157 } | 184 } |
| 185 |
| 186 /** |
| 187 * The fields of this class models a state machine for checking script |
| 188 * tags come in the correct order. |
| 189 */ |
| 190 class TagState { |
| 191 static final int NO_TAG_SEEN = 0; |
| 192 static final int LIBRARY = 1; |
| 193 static final int IMPORT = 2; |
| 194 static final int SOURCE = 3; |
| 195 static final int RESOURCE = 4; |
| 196 |
| 197 /** Next state. */ |
| 198 static final List<int> NEXT = |
| 199 const <int>[NO_TAG_SEEN, |
| 200 IMPORT, // Only one library tag is allowed. |
| 201 IMPORT, |
| 202 SOURCE, |
| 203 RESOURCE]; |
| 204 } |
| OLD | NEW |