| OLD | NEW |
| 1 // Copyright (c) 2011, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2011, 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('parser'); | 5 #library('parser'); |
| 6 | 6 |
| 7 #import('../elements/elements.dart'); |
| 7 #import('../scanner/scanner_implementation.dart'); | 8 #import('../scanner/scanner_implementation.dart'); |
| 8 #import('../scanner/scannerlib.dart'); | 9 #import('../scanner/scannerlib.dart'); |
| 9 | 10 |
| 10 #source('../../source.dart'); | 11 #source('../../source.dart'); |
| 12 #source('../scanner/byte_array_scanner.dart'); |
| 11 #source('../scanner/byte_strings.dart'); | 13 #source('../scanner/byte_strings.dart'); |
| 12 #source('../scanner/byte_array_scanner.dart'); | |
| 13 | 14 |
| 14 int charCount = 0; | 15 int charCount = 0; |
| 15 Stopwatch stopwatch; | 16 Stopwatch stopwatch; |
| 16 | 17 |
| 17 void main() { | 18 void main() { |
| 18 stopwatch = new Stopwatch(); | 19 stopwatch = new Stopwatch(); |
| 19 List<String> filenames = new Options().arguments; | |
| 20 MyOptions options = new MyOptions(); | 20 MyOptions options = new MyOptions(); |
| 21 | 21 |
| 22 void printStats() { | 22 void printStats() { |
| 23 int kb = (charCount / 1024).round().toInt(); | 23 int kb = (charCount / 1024).round().toInt(); |
| 24 String stats = | 24 String stats = |
| 25 '$classCount classes (${kb}Kb) in ${stopwatch.elapsedInMs()}ms'; | 25 '$classCount classes (${kb}Kb) in ${stopwatch.elapsedInMs()}ms'; |
| 26 if (errorCount != 0) { | 26 if (errorCount != 0) { |
| 27 stats += ' with $errorCount errors'; | 27 stats += ' with $errorCount errors'; |
| 28 } | 28 } |
| 29 if (options.diet) { | 29 if (options.diet) { |
| 30 print('Diet parsed $stats.'); | 30 print('Diet parsed $stats.'); |
| 31 } else { | 31 } else { |
| 32 print('Parsed $stats.'); | 32 print('Parsed $stats.'); |
| 33 } | 33 } |
| 34 } | 34 } |
| 35 | 35 |
| 36 for (String filename in filenames) { | 36 for (String argument in new Options().arguments) { |
| 37 if (filename == "--diet") { | 37 if (argument == "--diet") { |
| 38 options.diet = true; | 38 options.diet = true; |
| 39 continue; | 39 continue; |
| 40 } | 40 } |
| 41 if (filename == "--throw") { | 41 if (argument == "--throw") { |
| 42 options.throwOnError = true; | 42 options.throwOnError = true; |
| 43 continue; | 43 continue; |
| 44 } | 44 } |
| 45 if (filename == "--scan-only") { | 45 if (argument == "--scan-only") { |
| 46 options.scanOnly = true; | 46 options.scanOnly = true; |
| 47 continue; | 47 continue; |
| 48 } | 48 } |
| 49 if (filename == "--read-only") { | 49 if (argument == "--read-only") { |
| 50 options.readOnly = true; | 50 options.readOnly = true; |
| 51 continue; | 51 continue; |
| 52 } | 52 } |
| 53 if (filename == "-") { | 53 if (argument == "--ast") { |
| 54 options.buildAst = true; |
| 55 continue; |
| 56 } |
| 57 if (argument == "-") { |
| 54 parseFilesFrom(stdin, options, printStats); | 58 parseFilesFrom(stdin, options, printStats); |
| 55 return; | 59 return; |
| 56 } | 60 } |
| 57 charCount += parseFile(filename, options); | 61 charCount += parseFile(argument, options); |
| 58 } | 62 } |
| 59 | 63 |
| 60 printStats(); | 64 printStats(); |
| 61 } | 65 } |
| 62 | 66 |
| 63 int parseFile(String filename, MyOptions options) { | 67 int parseFile(String filename, MyOptions options) { |
| 64 List<int> bytes = read(filename); | 68 List<int> bytes = read(filename); |
| 65 if (options.readOnly) return bytes.length; | 69 if (options.readOnly) return bytes.length; |
| 66 MySourceFile file = new MySourceFile(filename, bytes); | 70 MySourceFile file = new MySourceFile(filename, bytes); |
| 67 Listener listener = new MyListener(file); | 71 final Listener listener = options.buildAst |
| 68 Parser parser = | 72 ? new MyNodeListener(file, options) |
| 69 options.diet ? new PartialParser(listener) : new Parser(listener); | 73 : new MyListener(file); |
| 74 final Parser parser = options.diet |
| 75 ? new PartialParser(listener) |
| 76 : new Parser(listener); |
| 70 try { | 77 try { |
| 71 Token token = scan(file); | 78 Token token = scan(file); |
| 72 if (!options.scanOnly) parser.parseUnit(token); | 79 if (!options.scanOnly) parser.parseUnit(token); |
| 73 } catch (ParserError ex) { | 80 } catch (ParserError ex) { |
| 74 if (options.throwOnError) { | 81 if (options.throwOnError) { |
| 75 throw; | 82 throw; |
| 76 } else { | 83 } else { |
| 77 print(ex); | 84 print(ex); |
| 78 } | 85 } |
| 79 } | 86 } |
| (...skipping 29 matching lines...) Expand all Loading... |
| 109 stringStream.lineHandler = () { | 116 stringStream.lineHandler = () { |
| 110 String line; | 117 String line; |
| 111 while ((line = stringStream.readLine()) !== null) { | 118 while ((line = stringStream.readLine()) !== null) { |
| 112 lineHandler(line); | 119 lineHandler(line); |
| 113 } | 120 } |
| 114 }; | 121 }; |
| 115 stringStream.closeHandler = closeHandler; | 122 stringStream.closeHandler = closeHandler; |
| 116 } | 123 } |
| 117 | 124 |
| 118 List<int> read(String filename) { | 125 List<int> read(String filename) { |
| 119 File file = new File(filename).openSync(); | 126 RandomAccessFile file = new File(filename).openSync(); |
| 120 bool threw = true; | 127 bool threw = true; |
| 121 try { | 128 try { |
| 122 int size = file.lengthSync(); | 129 int size = file.lengthSync(); |
| 123 List<int> bytes = new List<int>(size + 1); | 130 List<int> bytes = new List<int>(size + 1); |
| 124 file.readListSync(bytes, 0, size); | 131 file.readListSync(bytes, 0, size); |
| 125 bytes[size] = $EOF; | 132 bytes[size] = $EOF; |
| 126 threw = false; | 133 threw = false; |
| 127 return bytes; | 134 return bytes; |
| 128 } finally { | 135 } finally { |
| 129 try { | 136 try { |
| (...skipping 10 matching lines...) Expand all Loading... |
| 140 class MyListener extends Listener { | 147 class MyListener extends Listener { |
| 141 final SourceFile file; | 148 final SourceFile file; |
| 142 | 149 |
| 143 MyListener(this.file); | 150 MyListener(this.file); |
| 144 | 151 |
| 145 void beginClassDeclaration(Token token) { | 152 void beginClassDeclaration(Token token) { |
| 146 classCount++; | 153 classCount++; |
| 147 } | 154 } |
| 148 | 155 |
| 149 void error(String message, Token token) { | 156 void error(String message, Token token) { |
| 150 ++errorCount; | 157 parserError(message, token, file); |
| 151 if (token !== null) { | |
| 152 String tokenString = token.toString(); | |
| 153 int begin = token.charOffset; | |
| 154 int end = begin + tokenString.length; | |
| 155 throw new ParserError(file.getLocationMessage(message, begin, end, true)); | |
| 156 } | |
| 157 throw new ParserError(message); | |
| 158 } | 158 } |
| 159 } | 159 } |
| 160 | 160 |
| 161 void parserError(String message, Token token, SourceFile file) { |
| 162 ++errorCount; |
| 163 if (token !== null) { |
| 164 String tokenString = token.toString(); |
| 165 int begin = token.charOffset; |
| 166 int end = begin + tokenString.length; |
| 167 throw new ParserError(file.getLocationMessage(message, begin, end, true)); |
| 168 } |
| 169 throw new ParserError(message); |
| 170 } |
| 171 |
| 172 class MyNodeListener extends NodeListener { |
| 173 MyNodeListener(SourceFile file, MyOptions options) |
| 174 : super(new MyCanceller(file, options), null, |
| 175 new PartialClassElement(const SourceString('dummy'), null, null)); |
| 176 |
| 177 void log(message) { |
| 178 print(message); |
| 179 } |
| 180 } |
| 181 |
| 182 class MyCanceller implements Canceler { |
| 183 final SourceFile file; |
| 184 final MyOptions options; |
| 185 |
| 186 MyCanceller(this.file, this.options); |
| 187 |
| 188 void cancel([String reason, node, token, instruction]) { |
| 189 try { |
| 190 if (token !== null) { |
| 191 parserError(reason, token, file); |
| 192 } else if (node !== null) { |
| 193 parserError(reason, node.getBeginToken(), file); |
| 194 } else { |
| 195 parserError(reason, null, file); |
| 196 } |
| 197 } catch (ParserError ex) { |
| 198 if (options.throwOnError) throw; |
| 199 print(ex); |
| 200 } |
| 201 } |
| 202 } |
| 203 |
| 161 class MyOptions { | 204 class MyOptions { |
| 162 bool diet = false; | 205 bool diet = false; |
| 163 bool throwOnError = false; | 206 bool throwOnError = false; |
| 164 bool scanOnly = false; | 207 bool scanOnly = false; |
| 165 bool readOnly = false; | 208 bool readOnly = false; |
| 209 bool buildAst = false; |
| 166 } | 210 } |
| 167 | 211 |
| 168 class MySourceFile extends SourceFile { | 212 class MySourceFile extends SourceFile { |
| 169 final rawText; | 213 final rawText; |
| 170 var stringText; | 214 var stringText; |
| 171 | 215 |
| 172 MySourceFile(filename, this.rawText) : super(filename, null); | 216 MySourceFile(filename, this.rawText) : super(filename, null); |
| 173 | 217 |
| 174 String get text() { | 218 String get text() { |
| 175 if (rawText is String) { | 219 if (rawText is String) { |
| (...skipping 17 matching lines...) Expand all Loading... |
| 193 String _GREEN_COLOR = '\u001b[32m'; | 237 String _GREEN_COLOR = '\u001b[32m'; |
| 194 String _RED_COLOR = '\u001b[31m'; | 238 String _RED_COLOR = '\u001b[31m'; |
| 195 String _MAGENTA_COLOR = '\u001b[35m'; | 239 String _MAGENTA_COLOR = '\u001b[35m'; |
| 196 String _NO_COLOR = '\u001b[0m'; | 240 String _NO_COLOR = '\u001b[0m'; |
| 197 | 241 |
| 198 class Mock { | 242 class Mock { |
| 199 const Mock(); | 243 const Mock(); |
| 200 bool get useColors() => true; | 244 bool get useColors() => true; |
| 201 internalError(message) { throw message.toString(); } | 245 internalError(message) { throw message.toString(); } |
| 202 } | 246 } |
| OLD | NEW |