| 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 22 matching lines...) Expand all Loading... |
| 33 throw new ArgumentError("Can't get source for path $path"); | 33 throw new ArgumentError("Can't get source for path $path"); |
| 34 } | 34 } |
| 35 if (!source.exists()) { | 35 if (!source.exists()) { |
| 36 throw new ArgumentError("Source $source doesn't exist"); | 36 throw new ArgumentError("Source $source doesn't exist"); |
| 37 } | 37 } |
| 38 | 38 |
| 39 var scanner = new StringScanner(source, contents, errorCollector); | 39 var scanner = new StringScanner(source, contents, errorCollector); |
| 40 var token = scanner.tokenize(); | 40 var token = scanner.tokenize(); |
| 41 var parser = new Parser(source, errorCollector); | 41 var parser = new Parser(source, errorCollector); |
| 42 var unit = parser.parseCompilationUnit(token); | 42 var unit = parser.parseCompilationUnit(token); |
| 43 unit.lineInfo = new LineInfo(scanner.lineStarts); |
| 43 | 44 |
| 44 if (errorCollector.hasErrors) throw errorCollector.group; | 45 if (errorCollector.hasErrors) throw errorCollector.group; |
| 45 | 46 |
| 46 return unit; | 47 return unit; |
| 47 } | 48 } |
| 48 | 49 |
| 49 /// Parses a string of Dart code into an AST. | 50 /// Parses a string of Dart code into an AST. |
| 50 /// | 51 /// |
| 51 /// If [name] is passed, it's used in error messages as the name of the code | 52 /// If [name] is passed, it's used in error messages as the name of the code |
| 52 /// being parsed. | 53 /// being parsed. |
| 53 CompilationUnit parseCompilationUnit(String contents, {String name}) { | 54 CompilationUnit parseCompilationUnit(String contents, {String name}) { |
| 54 if (name == null) name = '<unknown source>'; | 55 if (name == null) name = '<unknown source>'; |
| 55 var source = new StringSource(contents, name); | 56 var source = new StringSource(contents, name); |
| 56 var errorCollector = new _ErrorCollector(); | 57 var errorCollector = new _ErrorCollector(); |
| 57 var scanner = new StringScanner(source, contents, errorCollector); | 58 var scanner = new StringScanner(source, contents, errorCollector); |
| 58 var token = scanner.tokenize(); | 59 var token = scanner.tokenize(); |
| 59 var parser = new Parser(source, errorCollector); | 60 var parser = new Parser(source, errorCollector); |
| 60 var unit = parser.parseCompilationUnit(token); | 61 var unit = parser.parseCompilationUnit(token); |
| 62 unit.lineInfo = new LineInfo(scanner.lineStarts); |
| 61 | 63 |
| 62 if (errorCollector.hasErrors) throw errorCollector.group; | 64 if (errorCollector.hasErrors) throw errorCollector.group; |
| 63 | 65 |
| 64 return unit; | 66 return unit; |
| 65 } | 67 } |
| 66 | 68 |
| 67 /// Converts an AST node representing a string literal into a [String]. | 69 /// Converts an AST node representing a string literal into a [String]. |
| 68 String stringLiteralToString(StringLiteral literal) { | 70 String stringLiteralToString(StringLiteral literal) { |
| 69 return literal.stringValue; | 71 return literal.stringValue; |
| 70 } | 72 } |
| 71 | 73 |
| 72 /// A simple error listener that collects errors into an [AnalysisErrorGroup]. | 74 /// A simple error listener that collects errors into an [AnalysisErrorGroup]. |
| 73 class _ErrorCollector extends AnalysisErrorListener { | 75 class _ErrorCollector extends AnalysisErrorListener { |
| 74 final _errors = <AnalysisError>[]; | 76 final _errors = <AnalysisError>[]; |
| 75 | 77 |
| 76 /// Whether any errors where collected. | 78 /// Whether any errors where collected. |
| 77 bool get hasErrors => !_errors.isEmpty; | 79 bool get hasErrors => !_errors.isEmpty; |
| 78 | 80 |
| 79 /// The group of errors collected. | 81 /// The group of errors collected. |
| 80 AnalyzerErrorGroup get group => | 82 AnalyzerErrorGroup get group => |
| 81 new AnalyzerErrorGroup.fromAnalysisErrors(_errors); | 83 new AnalyzerErrorGroup.fromAnalysisErrors(_errors); |
| 82 | 84 |
| 83 _ErrorCollector(); | 85 _ErrorCollector(); |
| 84 | 86 |
| 85 void onError(AnalysisError error) => _errors.add(error); | 87 void onError(AnalysisError error) => _errors.add(error); |
| 86 } | 88 } |
| OLD | NEW |