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 library dart2js.scanner.task; | 5 library dart2js.scanner.task; |
| 6 | 6 |
| 7 import '../common/tasks.dart' show CompilerTask; | 7 import '../common/tasks.dart' show CompilerTask; |
| 8 import '../compiler.dart' show Compiler; | 8 import '../compiler.dart' show Compiler; |
| 9 import '../elements/elements.dart' show CompilationUnitElement, LibraryElement; | 9 import '../elements/elements.dart' show CompilationUnitElement, LibraryElement; |
| 10 import '../script.dart' show Script; | 10 import '../script.dart' show Script; |
| 11 import '../parser/diet_parser_task.dart' show DietParserTask; | |
| 11 import '../tokens/token.dart' show Token; | 12 import '../tokens/token.dart' show Token; |
| 13 import '../tokens/token_constants.dart' as Tokens show COMMENT_TOKEN, EOF_TOKEN; | |
| 14 import '../tokens/token_map.dart' show TokenMap; | |
| 12 | 15 |
| 13 import 'scanner.dart' show Scanner; | 16 import 'scanner.dart' show Scanner; |
| 14 import 'string_scanner.dart' show StringScanner; | 17 import 'string_scanner.dart' show StringScanner; |
| 15 | 18 |
| 16 class ScannerTask extends CompilerTask { | 19 class ScannerTask extends CompilerTask { |
| 17 ScannerTask(Compiler compiler) : super(compiler); | 20 final DietParserTask _dietParser; |
| 21 final bool _preserveComments; | |
| 22 final TokenMap _commentMap; | |
| 23 | |
| 24 ScannerTask(Compiler compiler, this._dietParser, this._commentMap, | |
|
Siggi Cherem (dart-lang)
2016/04/08 19:37:56
consider making _commentMap an optional argument t
Harry Terkelsen
2016/04/08 20:52:42
Done.
| |
| 25 {bool preserveComments: false}) | |
| 26 : _preserveComments = preserveComments, | |
|
Siggi Cherem (dart-lang)
2016/04/08 19:37:56
... I wish the language just did this public/priva
Harry Terkelsen
2016/04/08 20:52:42
+1! This an annoying and common problem
| |
| 27 super(compiler); | |
| 28 | |
| 18 String get name => 'Scanner'; | 29 String get name => 'Scanner'; |
| 19 | 30 |
| 20 void scanLibrary(LibraryElement library) { | 31 void scanLibrary(LibraryElement library) { |
| 21 CompilationUnitElement compilationUnit = library.entryCompilationUnit; | 32 CompilationUnitElement compilationUnit = library.entryCompilationUnit; |
| 22 String canonicalUri = library.canonicalUri.toString(); | 33 String canonicalUri = library.canonicalUri.toString(); |
| 23 String resolvedUri = compilationUnit.script.resourceUri.toString(); | 34 String resolvedUri = compilationUnit.script.resourceUri.toString(); |
| 24 if (canonicalUri == resolvedUri) { | 35 if (canonicalUri == resolvedUri) { |
| 25 reporter.log("Scanning library $canonicalUri"); | 36 reporter.log("Scanning library $canonicalUri"); |
| 26 } else { | 37 } else { |
| 27 reporter.log("Scanning library $canonicalUri ($resolvedUri)"); | 38 reporter.log("Scanning library $canonicalUri ($resolvedUri)"); |
| 28 } | 39 } |
| 29 scan(compilationUnit); | 40 scan(compilationUnit); |
| 30 } | 41 } |
| 31 | 42 |
| 32 void scan(CompilationUnitElement compilationUnit) { | 43 void scan(CompilationUnitElement compilationUnit) { |
| 33 measure(() { | 44 measure(() { |
| 34 scanElements(compilationUnit); | 45 scanElements(compilationUnit); |
| 35 }); | 46 }); |
| 36 } | 47 } |
| 37 | 48 |
| 38 void scanElements(CompilationUnitElement compilationUnit) { | 49 void scanElements(CompilationUnitElement compilationUnit) { |
| 39 Script script = compilationUnit.script; | 50 Script script = compilationUnit.script; |
| 40 Token tokens = new Scanner(script.file, | 51 Token tokens = |
| 41 includeComments: compiler.options.preserveComments) | 52 new Scanner(script.file, includeComments: _preserveComments).tokenize(); |
| 42 .tokenize(); | 53 if (_preserveComments) { |
| 43 if (compiler.options.preserveComments) { | 54 tokens = processAndStripComments(tokens); |
| 44 tokens = compiler.processAndStripComments(tokens); | |
| 45 } | 55 } |
| 46 compiler.dietParser.dietParse(compilationUnit, tokens); | 56 _dietParser.dietParse(compilationUnit, tokens); |
| 47 } | 57 } |
| 48 | 58 |
| 49 /** | 59 /** |
| 50 * Returns the tokens for the [source]. | 60 * Returns the tokens for the [source]. |
| 51 * | 61 * |
| 52 * The [StringScanner] implementation works on strings that end with a '0' | 62 * The [StringScanner] implementation works on strings that end with a '0' |
| 53 * value ('\x00'). If [source] does not end with '0', the string is copied | 63 * value ('\x00'). If [source] does not end with '0', the string is copied |
| 54 * before scanning. | 64 * before scanning. |
| 55 */ | 65 */ |
| 56 Token tokenize(String source) { | 66 Token tokenize(String source) { |
| 57 return measure(() { | 67 return measure(() { |
| 58 return new StringScanner.fromString(source, includeComments: false) | 68 return new StringScanner.fromString(source, includeComments: false) |
| 59 .tokenize(); | 69 .tokenize(); |
| 60 }); | 70 }); |
| 61 } | 71 } |
| 72 | |
| 73 Token processAndStripComments(Token currentToken) { | |
|
Siggi Cherem (dart-lang)
2016/04/08 19:37:56
I really like what you did here to contain comment
Harry Terkelsen
2016/04/08 20:52:41
Acknowledged.
| |
| 74 Token firstToken = currentToken; | |
| 75 Token prevToken; | |
| 76 while (currentToken.kind != Tokens.EOF_TOKEN) { | |
| 77 if (identical(currentToken.kind, Tokens.COMMENT_TOKEN)) { | |
| 78 Token firstCommentToken = currentToken; | |
| 79 while (identical(currentToken.kind, Tokens.COMMENT_TOKEN)) { | |
| 80 currentToken = currentToken.next; | |
| 81 } | |
| 82 _commentMap[currentToken] = firstCommentToken; | |
| 83 if (prevToken == null) { | |
| 84 firstToken = currentToken; | |
| 85 } else { | |
| 86 prevToken.next = currentToken; | |
| 87 } | |
| 88 } | |
| 89 prevToken = currentToken; | |
| 90 currentToken = currentToken.next; | |
| 91 } | |
| 92 return firstToken; | |
| 93 } | |
| 62 } | 94 } |
| OLD | NEW |