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, Measurer; | 7 import '../common/tasks.dart' show CompilerTask, Measurer; |
8 import '../diagnostics/diagnostic_listener.dart' show DiagnosticReporter; | 8 import '../diagnostics/diagnostic_listener.dart' show DiagnosticReporter; |
9 import '../elements/elements.dart' show CompilationUnitElement, LibraryElement; | 9 import '../elements/elements.dart' show CompilationUnitElement, LibraryElement; |
10 import '../parser/diet_parser_task.dart' show DietParserTask; | 10 import '../parser/diet_parser_task.dart' show DietParserTask; |
11 import '../script.dart' show Script; | 11 import '../script.dart' show Script; |
12 import '../tokens/token.dart' show Token; | 12 import 'package:dart_scanner/dart_scanner.dart' |
13 import '../tokens/token_constants.dart' as Tokens show COMMENT_TOKEN, EOF_TOKEN; | 13 show Scanner, StringScanner, Token, Utf8BytesScanner; |
| 14 import 'package:dart_scanner/src/token_constants.dart' as Tokens |
| 15 show COMMENT_TOKEN, EOF_TOKEN; |
14 import '../tokens/token_map.dart' show TokenMap; | 16 import '../tokens/token_map.dart' show TokenMap; |
15 import 'scanner.dart' show Scanner; | 17 import '../io/source_file.dart'; |
16 import 'string_scanner.dart' show StringScanner; | |
17 | 18 |
18 class ScannerTask extends CompilerTask { | 19 class ScannerTask extends CompilerTask { |
19 final DietParserTask _dietParser; | 20 final DietParserTask _dietParser; |
20 final bool _preserveComments; | 21 final bool _preserveComments; |
21 final TokenMap _commentMap; | 22 final TokenMap _commentMap; |
22 final DiagnosticReporter reporter; | 23 final DiagnosticReporter reporter; |
23 | 24 |
24 ScannerTask(this._dietParser, this.reporter, Measurer measurer, | 25 ScannerTask(this._dietParser, this.reporter, Measurer measurer, |
25 {bool preserveComments: false, TokenMap commentMap}) | 26 {bool preserveComments: false, TokenMap commentMap}) |
26 : _preserveComments = preserveComments, | 27 : _preserveComments = preserveComments, |
(...skipping 18 matching lines...) Expand all Loading... |
45 } | 46 } |
46 scan(compilationUnit); | 47 scan(compilationUnit); |
47 } | 48 } |
48 | 49 |
49 void scan(CompilationUnitElement compilationUnit) { | 50 void scan(CompilationUnitElement compilationUnit) { |
50 measure(() { | 51 measure(() { |
51 scanElements(compilationUnit); | 52 scanElements(compilationUnit); |
52 }); | 53 }); |
53 } | 54 } |
54 | 55 |
| 56 Token scanFile(SourceFile file, {bool includeComments: false}) { |
| 57 Scanner scanner = file is Utf8BytesSourceFile |
| 58 ? new Utf8BytesScanner(file.slowUtf8ZeroTerminatedBytes(), |
| 59 includeComments: includeComments) |
| 60 : new StringScanner(file.slowText(), includeComments: includeComments); |
| 61 return measure(scanner.tokenize); |
| 62 } |
| 63 |
55 void scanElements(CompilationUnitElement compilationUnit) { | 64 void scanElements(CompilationUnitElement compilationUnit) { |
56 Script script = compilationUnit.script; | 65 Script script = compilationUnit.script; |
57 Token tokens = | 66 Token tokens = scanFile(script.file, includeComments: _preserveComments); |
58 new Scanner(script.file, includeComments: _preserveComments).tokenize(); | |
59 if (_preserveComments) { | 67 if (_preserveComments) { |
60 tokens = processAndStripComments(tokens); | 68 tokens = processAndStripComments(tokens); |
61 } | 69 } |
62 _dietParser.dietParse(compilationUnit, tokens); | 70 _dietParser.dietParse(compilationUnit, tokens); |
63 } | 71 } |
64 | 72 |
65 /** | 73 /** |
66 * Returns the tokens for the [source]. | 74 * Returns the tokens for the [source]. |
67 * | 75 * |
68 * The [StringScanner] implementation works on strings that end with a '0' | 76 * The [StringScanner] implementation works on strings that end with a '0' |
69 * value ('\x00'). If [source] does not end with '0', the string is copied | 77 * value ('\x00'). If [source] does not end with '0', the string is copied |
70 * before scanning. | 78 * before scanning. |
71 */ | 79 */ |
72 Token tokenize(String source) { | 80 Token tokenize(String source) { |
73 return measure(() { | 81 return measure(() { |
74 return new StringScanner.fromString(source, includeComments: false) | 82 return new StringScanner(source, includeComments: false).tokenize(); |
75 .tokenize(); | |
76 }); | 83 }); |
77 } | 84 } |
78 | 85 |
79 Token processAndStripComments(Token currentToken) { | 86 Token processAndStripComments(Token currentToken) { |
80 Token firstToken = currentToken; | 87 Token firstToken = currentToken; |
81 Token prevToken; | 88 Token prevToken; |
82 while (currentToken.kind != Tokens.EOF_TOKEN) { | 89 while (currentToken.kind != Tokens.EOF_TOKEN) { |
83 if (identical(currentToken.kind, Tokens.COMMENT_TOKEN)) { | 90 if (identical(currentToken.kind, Tokens.COMMENT_TOKEN)) { |
84 Token firstCommentToken = currentToken; | 91 Token firstCommentToken = currentToken; |
85 while (identical(currentToken.kind, Tokens.COMMENT_TOKEN)) { | 92 while (identical(currentToken.kind, Tokens.COMMENT_TOKEN)) { |
86 currentToken = currentToken.next; | 93 currentToken = currentToken.next; |
87 } | 94 } |
88 _commentMap[currentToken] = firstCommentToken; | 95 _commentMap[currentToken] = firstCommentToken; |
89 if (prevToken == null) { | 96 if (prevToken == null) { |
90 firstToken = currentToken; | 97 firstToken = currentToken; |
91 } else { | 98 } else { |
92 prevToken.next = currentToken; | 99 prevToken.next = currentToken; |
93 } | 100 } |
94 } | 101 } |
95 prevToken = currentToken; | 102 prevToken = currentToken; |
96 currentToken = currentToken.next; | 103 currentToken = currentToken.next; |
97 } | 104 } |
98 return firstToken; | 105 return firstToken; |
99 } | 106 } |
100 } | 107 } |
OLD | NEW |