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, |
| 25 {bool preserveComments: false, TokenMap commentMap}) |
| 26 : _preserveComments = preserveComments, |
| 27 _commentMap = commentMap, |
| 28 super(compiler) { |
| 29 if (_preserveComments && _commentMap == null) { |
| 30 throw new ArgumentError( |
| 31 "commentMap must be provided if preserveComments is true"); |
| 32 } |
| 33 } |
| 34 |
18 String get name => 'Scanner'; | 35 String get name => 'Scanner'; |
19 | 36 |
20 void scanLibrary(LibraryElement library) { | 37 void scanLibrary(LibraryElement library) { |
21 CompilationUnitElement compilationUnit = library.entryCompilationUnit; | 38 CompilationUnitElement compilationUnit = library.entryCompilationUnit; |
22 String canonicalUri = library.canonicalUri.toString(); | 39 String canonicalUri = library.canonicalUri.toString(); |
23 String resolvedUri = compilationUnit.script.resourceUri.toString(); | 40 String resolvedUri = compilationUnit.script.resourceUri.toString(); |
24 if (canonicalUri == resolvedUri) { | 41 if (canonicalUri == resolvedUri) { |
25 reporter.log("Scanning library $canonicalUri"); | 42 reporter.log("Scanning library $canonicalUri"); |
26 } else { | 43 } else { |
27 reporter.log("Scanning library $canonicalUri ($resolvedUri)"); | 44 reporter.log("Scanning library $canonicalUri ($resolvedUri)"); |
28 } | 45 } |
29 scan(compilationUnit); | 46 scan(compilationUnit); |
30 } | 47 } |
31 | 48 |
32 void scan(CompilationUnitElement compilationUnit) { | 49 void scan(CompilationUnitElement compilationUnit) { |
33 measure(() { | 50 measure(() { |
34 scanElements(compilationUnit); | 51 scanElements(compilationUnit); |
35 }); | 52 }); |
36 } | 53 } |
37 | 54 |
38 void scanElements(CompilationUnitElement compilationUnit) { | 55 void scanElements(CompilationUnitElement compilationUnit) { |
39 Script script = compilationUnit.script; | 56 Script script = compilationUnit.script; |
40 Token tokens = new Scanner(script.file, | 57 Token tokens = |
41 includeComments: compiler.options.preserveComments) | 58 new Scanner(script.file, includeComments: _preserveComments).tokenize(); |
42 .tokenize(); | 59 if (_preserveComments) { |
43 if (compiler.options.preserveComments) { | 60 tokens = processAndStripComments(tokens); |
44 tokens = compiler.processAndStripComments(tokens); | |
45 } | 61 } |
46 compiler.dietParser.dietParse(compilationUnit, tokens); | 62 _dietParser.dietParse(compilationUnit, tokens); |
47 } | 63 } |
48 | 64 |
49 /** | 65 /** |
50 * Returns the tokens for the [source]. | 66 * Returns the tokens for the [source]. |
51 * | 67 * |
52 * The [StringScanner] implementation works on strings that end with a '0' | 68 * 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 | 69 * value ('\x00'). If [source] does not end with '0', the string is copied |
54 * before scanning. | 70 * before scanning. |
55 */ | 71 */ |
56 Token tokenize(String source) { | 72 Token tokenize(String source) { |
57 return measure(() { | 73 return measure(() { |
58 return new StringScanner.fromString(source, includeComments: false) | 74 return new StringScanner.fromString(source, includeComments: false) |
59 .tokenize(); | 75 .tokenize(); |
60 }); | 76 }); |
61 } | 77 } |
| 78 |
| 79 Token processAndStripComments(Token currentToken) { |
| 80 Token firstToken = currentToken; |
| 81 Token prevToken; |
| 82 while (currentToken.kind != Tokens.EOF_TOKEN) { |
| 83 if (identical(currentToken.kind, Tokens.COMMENT_TOKEN)) { |
| 84 Token firstCommentToken = currentToken; |
| 85 while (identical(currentToken.kind, Tokens.COMMENT_TOKEN)) { |
| 86 currentToken = currentToken.next; |
| 87 } |
| 88 _commentMap[currentToken] = firstCommentToken; |
| 89 if (prevToken == null) { |
| 90 firstToken = currentToken; |
| 91 } else { |
| 92 prevToken.next = currentToken; |
| 93 } |
| 94 } |
| 95 prevToken = currentToken; |
| 96 currentToken = currentToken.next; |
| 97 } |
| 98 return firstToken; |
| 99 } |
62 } | 100 } |
OLD | NEW |