| 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('../scanner/scanner_implementation.dart'); | 7 #import('../scanner/scanner_implementation.dart'); |
| 8 #import('../scanner/scannerlib.dart'); | 8 #import('../scanner/scannerlib.dart'); |
| 9 | 9 |
| 10 #source('../../source.dart'); | 10 #source('../../source.dart'); |
| 11 #source('../scanner/byte_strings.dart'); | 11 #source('../scanner/byte_strings.dart'); |
| 12 #source('../scanner/byte_array_scanner.dart'); | 12 #source('../scanner/byte_array_scanner.dart'); |
| 13 | 13 |
| 14 // Hack to allow satisfy sourcing in ../source.dart. | 14 // Hack to allow satisfy sourcing in ../source.dart. |
| 15 var world; | 15 var world; |
| 16 | 16 |
| 17 void main() { | 17 void main() { |
| 18 Stopwatch stopwatch = new Stopwatch(); | 18 Stopwatch stopwatch = new Stopwatch(); |
| 19 stopwatch.start(); | 19 stopwatch.start(); |
| 20 List<String> filenames = new Options().arguments; | 20 List<String> filenames = new Options().arguments; |
| 21 bool diet = false; | 21 bool diet = false; |
| 22 bool throwOnError = false; |
| 23 bool scanOnly = false; |
| 22 int charCount = 0; | 24 int charCount = 0; |
| 23 for (String filename in filenames) { | 25 for (String filename in filenames) { |
| 24 if (filename == "--diet") { | 26 if (filename == "--diet") { |
| 25 diet = true; | 27 diet = true; |
| 26 continue; | 28 continue; |
| 27 } | 29 } |
| 30 if (filename == "--throw") { |
| 31 throwOnError = true; |
| 32 continue; |
| 33 } |
| 34 if (filename == "--scan-only") { |
| 35 scanOnly = true; |
| 36 continue; |
| 37 } |
| 28 String source = new String.fromCharCodes(read(filename)); | 38 String source = new String.fromCharCodes(read(filename)); |
| 29 charCount += source.length; | 39 charCount += source.length; |
| 30 SourceFile file = new SourceFile(filename, source); | 40 SourceFile file = new SourceFile(filename, source); |
| 31 Listener listener = new MyListener(file); | 41 Listener listener = new MyListener(file); |
| 32 Parser parser = diet ? new PartialParser(listener) : new Parser(listener); | 42 Parser parser = diet ? new PartialParser(listener) : new Parser(listener); |
| 33 try { | 43 try { |
| 34 parser.parseUnit(scan(file.text)); | 44 Token token = scan(file); |
| 45 if (!scanOnly) parser.parseUnit(token); |
| 35 } catch (ParserError ex) { | 46 } catch (ParserError ex) { |
| 36 print(ex); | 47 if (throwOnError) { |
| 48 throw; |
| 49 } else { |
| 50 print(ex); |
| 51 } |
| 37 } | 52 } |
| 38 } | 53 } |
| 39 stopwatch.stop(); | 54 stopwatch.stop(); |
| 40 int kb = (charCount/1024).round().toInt(); | 55 int kb = (charCount/1024).round().toInt(); |
| 41 String stats = | 56 String stats = |
| 42 '$classCount classes (${kb}Kb) in ${stopwatch.elapsedInMs()}ms.'; | 57 '$classCount classes (${kb}Kb) in ${stopwatch.elapsedInMs()}ms'; |
| 58 if (errorCount != 0) { |
| 59 stats += ' with $errorCount errors'; |
| 60 } |
| 43 if (diet) { | 61 if (diet) { |
| 44 print('Diet parsed $stats'); | 62 print('Diet parsed $stats.'); |
| 45 } else { | 63 } else { |
| 46 print('Parsed $stats'); | 64 print('Parsed $stats.'); |
| 47 } | 65 } |
| 48 } | 66 } |
| 49 | 67 |
| 50 Token scan(String source) => new StringScanner(source).tokenize(); | 68 Token scan(SourceFile source) { |
| 69 Scanner scanner = new StringScanner(source.text); |
| 70 try { |
| 71 return scanner.tokenize(); |
| 72 } catch (MalformedInputException ex) { |
| 73 var message = ex.message; |
| 74 if (message is !String) message = "unexpected character"; |
| 75 Token fakeToken = new Token($QUESTION, scanner.charOffset); |
| 76 new MyListener(source).error(message, fakeToken); |
| 77 throw; |
| 78 } |
| 79 } |
| 51 | 80 |
| 52 List<int> read(String filename) { | 81 List<int> read(String filename) { |
| 53 File file = new File(filename); | 82 File file = new File(filename); |
| 54 file.openSync(); | 83 file.openSync(); |
| 84 bool threw = true; |
| 55 try { | 85 try { |
| 56 int size = file.lengthSync(); | 86 int size = file.lengthSync(); |
| 57 List<int> bytes = new List<int>(size + 1); | 87 List<int> bytes = new List<int>(size + 1); |
| 58 file.readListSync(bytes, 0, size); | 88 file.readListSync(bytes, 0, size); |
| 59 bytes[size] = $EOF; | 89 bytes[size] = $EOF; |
| 90 threw = false; |
| 60 return bytes; | 91 return bytes; |
| 61 } finally { | 92 } finally { |
| 62 file.closeSync(); | 93 try { |
| 94 file.closeSync(); |
| 95 } catch (var ex) { |
| 96 if (!threw) throw; |
| 97 } |
| 63 } | 98 } |
| 64 } | 99 } |
| 65 | 100 |
| 66 int classCount = 0; | 101 int classCount = 0; |
| 102 int errorCount = 0; |
| 67 | 103 |
| 68 class MyListener extends Listener { | 104 class MyListener extends Listener { |
| 69 final SourceFile file; | 105 final SourceFile file; |
| 70 | 106 |
| 71 MyListener(this.file); | 107 MyListener(this.file); |
| 72 | 108 |
| 73 void beginClassDeclaration(Token token) { | 109 void beginClassDeclaration(Token token) { |
| 74 classCount++; | 110 classCount++; |
| 75 } | 111 } |
| 76 | 112 |
| 77 void error(String message, Token token) { | 113 void error(String message, Token token) { |
| 114 ++errorCount; |
| 78 if (token !== null) { | 115 if (token !== null) { |
| 79 String tokenString = token.toString(); | 116 String tokenString = token.toString(); |
| 80 int begin = token.charOffset; | 117 int begin = token.charOffset; |
| 81 int end = begin + tokenString.length; | 118 int end = begin + tokenString.length; |
| 82 throw new ParserError(file.getLocationMessage(message, begin, end, true)); | 119 throw new ParserError(file.getLocationMessage(message, begin, end, true)); |
| 83 } | 120 } |
| 84 throw new ParserError(message); | 121 throw new ParserError(message); |
| 85 } | 122 } |
| 86 } | 123 } |
| OLD | NEW |