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