| OLD | NEW |
| 1 // Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2013, 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 analyzer; | 5 library analyzer; |
| 6 | 6 |
| 7 import 'dart:io'; | 7 import 'dart:io'; |
| 8 | 8 |
| 9 import 'package:path/path.dart' as pathos; | 9 import 'package:path/path.dart' as pathos; |
| 10 | 10 |
| (...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 45 | 45 |
| 46 if (errorCollector.hasErrors) throw errorCollector.group; | 46 if (errorCollector.hasErrors) throw errorCollector.group; |
| 47 | 47 |
| 48 return unit; | 48 return unit; |
| 49 } | 49 } |
| 50 | 50 |
| 51 /// Parses a string of Dart code into an AST. | 51 /// Parses a string of Dart code into an AST. |
| 52 /// | 52 /// |
| 53 /// If [name] is passed, it's used in error messages as the name of the code | 53 /// If [name] is passed, it's used in error messages as the name of the code |
| 54 /// being parsed. | 54 /// being parsed. |
| 55 CompilationUnit parseCompilationUnit(String contents, {String name}) { | 55 CompilationUnit parseCompilationUnit(String contents, |
| 56 {String name, bool suppressErrors: false}) { |
| 56 if (name == null) name = '<unknown source>'; | 57 if (name == null) name = '<unknown source>'; |
| 57 var source = new StringSource(contents, name); | 58 var source = new StringSource(contents, name); |
| 58 var errorCollector = new _ErrorCollector(); | 59 var errorCollector = new _ErrorCollector(); |
| 59 var reader = new CharSequenceReader(contents); | 60 var reader = new CharSequenceReader(contents); |
| 60 var scanner = new Scanner(source, reader, errorCollector); | 61 var scanner = new Scanner(source, reader, errorCollector); |
| 61 var token = scanner.tokenize(); | 62 var token = scanner.tokenize(); |
| 62 var parser = new Parser(source, errorCollector); | 63 var parser = new Parser(source, errorCollector); |
| 63 var unit = parser.parseCompilationUnit(token); | 64 var unit = parser.parseCompilationUnit(token); |
| 64 unit.lineInfo = new LineInfo(scanner.lineStarts); | 65 unit.lineInfo = new LineInfo(scanner.lineStarts); |
| 65 | 66 |
| 66 if (errorCollector.hasErrors) throw errorCollector.group; | 67 if (errorCollector.hasErrors && !suppressErrors) throw errorCollector.group; |
| 67 | 68 |
| 68 return unit; | 69 return unit; |
| 69 } | 70 } |
| 70 | 71 |
| 71 /// Converts an AST node representing a string literal into a [String]. | 72 /// Converts an AST node representing a string literal into a [String]. |
| 72 String stringLiteralToString(StringLiteral literal) { | 73 String stringLiteralToString(StringLiteral literal) { |
| 73 return literal.stringValue; | 74 return literal.stringValue; |
| 74 } | 75 } |
| 75 | 76 |
| 76 /// A simple error listener that collects errors into an [AnalysisErrorGroup]. | 77 /// A simple error listener that collects errors into an [AnalysisErrorGroup]. |
| 77 class _ErrorCollector extends AnalysisErrorListener { | 78 class _ErrorCollector extends AnalysisErrorListener { |
| 78 final _errors = <AnalysisError>[]; | 79 final _errors = <AnalysisError>[]; |
| 79 | 80 |
| 80 /// Whether any errors where collected. | 81 /// Whether any errors where collected. |
| 81 bool get hasErrors => !_errors.isEmpty; | 82 bool get hasErrors => !_errors.isEmpty; |
| 82 | 83 |
| 83 /// The group of errors collected. | 84 /// The group of errors collected. |
| 84 AnalyzerErrorGroup get group => | 85 AnalyzerErrorGroup get group => |
| 85 new AnalyzerErrorGroup.fromAnalysisErrors(_errors); | 86 new AnalyzerErrorGroup.fromAnalysisErrors(_errors); |
| 86 | 87 |
| 87 _ErrorCollector(); | 88 _ErrorCollector(); |
| 88 | 89 |
| 89 void onError(AnalysisError error) => _errors.add(error); | 90 void onError(AnalysisError error) => _errors.add(error); |
| 90 } | 91 } |
| OLD | NEW |