| 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:analyzer/dart/ast/ast.dart'; |
| 10 import 'package:analyzer/error/error.dart'; |
| 11 import 'package:analyzer/error/listener.dart'; |
| 12 import 'package:analyzer/file_system/file_system.dart' hide File; |
| 13 import 'package:analyzer/file_system/physical_file_system.dart'; |
| 14 import 'package:analyzer/src/dart/scanner/reader.dart'; |
| 15 import 'package:analyzer/src/dart/scanner/scanner.dart'; |
| 16 import 'package:analyzer/src/error.dart'; |
| 17 import 'package:analyzer/src/generated/parser.dart'; |
| 18 import 'package:analyzer/src/generated/source_io.dart'; |
| 19 import 'package:analyzer/src/string_source.dart'; |
| 9 import 'package:path/path.dart' as pathos; | 20 import 'package:path/path.dart' as pathos; |
| 10 | 21 |
| 11 import 'src/error.dart'; | 22 export 'package:analyzer/dart/ast/ast.dart'; |
| 12 import 'src/generated/ast.dart'; | 23 export 'package:analyzer/dart/ast/visitor.dart'; |
| 13 import 'src/generated/error.dart'; | 24 export 'package:analyzer/error/error.dart'; |
| 14 import 'src/generated/parser.dart'; | 25 export 'package:analyzer/error/listener.dart'; |
| 15 import 'src/generated/scanner.dart'; | 26 export 'package:analyzer/src/dart/ast/utilities.dart'; |
| 16 import 'src/generated/source_io.dart'; | 27 export 'package:analyzer/src/error.dart'; |
| 17 import 'src/string_source.dart'; | 28 export 'package:analyzer/src/error/codes.dart'; |
| 18 | 29 export 'package:analyzer/src/generated/utilities_dart.dart'; |
| 19 export 'src/error.dart'; | |
| 20 export 'src/generated/ast.dart'; | |
| 21 export 'src/generated/error.dart'; | |
| 22 export 'src/generated/utilities_dart.dart'; | |
| 23 | 30 |
| 24 /// Parses a string of Dart code into an AST. | 31 /// Parses a string of Dart code into an AST. |
| 25 /// | 32 /// |
| 26 /// If [name] is passed, it's used in error messages as the name of the code | 33 /// If [name] is passed, it's used in error messages as the name of the code |
| 27 /// being parsed. | 34 /// being parsed. |
| 28 /// | 35 /// |
| 29 /// Throws an [AnalyzerErrorGroup] if any errors occurred, unless | 36 /// Throws an [AnalyzerErrorGroup] if any errors occurred, unless |
| 30 /// [suppressErrors] is `true`, in which case any errors are discarded. | 37 /// [suppressErrors] is `true`, in which case any errors are discarded. |
| 31 /// | 38 /// |
| 32 /// If [parseFunctionBodies] is [false] then only function signatures will be | 39 /// If [parseFunctionBodies] is [false] then only function signatures will be |
| 33 /// parsed. | 40 /// parsed. |
| 34 CompilationUnit parseCompilationUnit(String contents, | 41 CompilationUnit parseCompilationUnit(String contents, |
| 35 {String name, bool suppressErrors: false, bool parseFunctionBodies: true}) { | 42 {String name, bool suppressErrors: false, bool parseFunctionBodies: true}) { |
| 36 if (name == null) name = '<unknown source>'; | 43 Source source = new StringSource(contents, name); |
| 37 var source = new StringSource(contents, name); | |
| 38 return _parseSource(contents, source, | 44 return _parseSource(contents, source, |
| 39 suppressErrors: suppressErrors, parseFunctionBodies: parseFunctionBodies); | 45 suppressErrors: suppressErrors, parseFunctionBodies: parseFunctionBodies); |
| 40 } | 46 } |
| 41 | 47 |
| 42 /// Parses a Dart file into an AST. | 48 /// Parses a Dart file into an AST. |
| 43 /// | 49 /// |
| 44 /// Throws an [AnalyzerErrorGroup] if any errors occurred, unless | 50 /// Throws an [AnalyzerErrorGroup] if any errors occurred, unless |
| 45 /// [suppressErrors] is `true`, in which case any errors are discarded. | 51 /// [suppressErrors] is `true`, in which case any errors are discarded. |
| 46 /// | 52 /// |
| 47 /// If [parseFunctionBodies] is [false] then only function signatures will be | 53 /// If [parseFunctionBodies] is [false] then only function signatures will be |
| 48 /// parsed. | 54 /// parsed. |
| 49 CompilationUnit parseDartFile(String path, | 55 CompilationUnit parseDartFile(String path, |
| 50 {bool suppressErrors: false, bool parseFunctionBodies: true}) { | 56 {bool suppressErrors: false, bool parseFunctionBodies: true}) { |
| 51 String contents = new File(path).readAsStringSync(); | 57 String contents = new File(path).readAsStringSync(); |
| 52 var sourceFactory = new SourceFactory([new FileUriResolver()]); | 58 var sourceFactory = new SourceFactory( |
| 59 [new ResourceUriResolver(PhysicalResourceProvider.INSTANCE)]); |
| 53 | 60 |
| 54 var absolutePath = pathos.absolute(path); | 61 var absolutePath = pathos.absolute(path); |
| 55 var source = sourceFactory.forUri(pathos.toUri(absolutePath).toString()); | 62 var source = sourceFactory.forUri(pathos.toUri(absolutePath).toString()); |
| 56 if (source == null) { | 63 if (source == null) { |
| 57 throw new ArgumentError("Can't get source for path $path"); | 64 throw new ArgumentError("Can't get source for path $path"); |
| 58 } | 65 } |
| 59 if (!source.exists()) { | 66 if (!source.exists()) { |
| 60 throw new ArgumentError("Source $source doesn't exist"); | 67 throw new ArgumentError("Source $source doesn't exist"); |
| 61 } | 68 } |
| 62 | 69 |
| 63 return _parseSource(contents, source, | 70 return _parseSource(contents, source, |
| 64 suppressErrors: suppressErrors, parseFunctionBodies: parseFunctionBodies); | 71 suppressErrors: suppressErrors, parseFunctionBodies: parseFunctionBodies); |
| 65 } | 72 } |
| 66 | 73 |
| 67 CompilationUnit _parseSource(String contents, Source source, | |
| 68 {bool suppressErrors: false, bool parseFunctionBodies: true}) { | |
| 69 var reader = new CharSequenceReader(contents); | |
| 70 var errorCollector = new _ErrorCollector(); | |
| 71 var scanner = new Scanner(source, reader, errorCollector); | |
| 72 var token = scanner.tokenize(); | |
| 73 var parser = new Parser(source, errorCollector) | |
| 74 ..parseFunctionBodies = parseFunctionBodies; | |
| 75 var unit = parser.parseCompilationUnit(token) | |
| 76 ..lineInfo = new LineInfo(scanner.lineStarts); | |
| 77 | |
| 78 if (errorCollector.hasErrors && !suppressErrors) throw errorCollector.group; | |
| 79 | |
| 80 return unit; | |
| 81 } | |
| 82 | |
| 83 /// Parses the script tag and directives in a string of Dart code into an AST. | 74 /// Parses the script tag and directives in a string of Dart code into an AST. |
| 84 /// | 75 /// |
| 85 /// Stops parsing when the first non-directive is encountered. The rest of the | 76 /// Stops parsing when the first non-directive is encountered. The rest of the |
| 86 /// string will not be parsed. | 77 /// string will not be parsed. |
| 87 /// | 78 /// |
| 88 /// If [name] is passed, it's used in error messages as the name of the code | 79 /// If [name] is passed, it's used in error messages as the name of the code |
| 89 /// being parsed. | 80 /// being parsed. |
| 90 /// | 81 /// |
| 91 /// Throws an [AnalyzerErrorGroup] if any errors occurred, unless | 82 /// Throws an [AnalyzerErrorGroup] if any errors occurred, unless |
| 92 /// [suppressErrors] is `true`, in which case any errors are discarded. | 83 /// [suppressErrors] is `true`, in which case any errors are discarded. |
| 93 CompilationUnit parseDirectives(String contents, | 84 CompilationUnit parseDirectives(String contents, |
| 94 {String name, bool suppressErrors: false}) { | 85 {String name, bool suppressErrors: false}) { |
| 95 if (name == null) name = '<unknown source>'; | |
| 96 var source = new StringSource(contents, name); | 86 var source = new StringSource(contents, name); |
| 97 var errorCollector = new _ErrorCollector(); | 87 var errorCollector = new _ErrorCollector(); |
| 98 var reader = new CharSequenceReader(contents); | 88 var reader = new CharSequenceReader(contents); |
| 99 var scanner = new Scanner(source, reader, errorCollector); | 89 var scanner = new Scanner(source, reader, errorCollector); |
| 100 var token = scanner.tokenize(); | 90 var token = scanner.tokenize(); |
| 101 var parser = new Parser(source, errorCollector); | 91 var parser = new Parser(source, errorCollector); |
| 102 var unit = parser.parseDirectives(token); | 92 var unit = parser.parseDirectives(token); |
| 103 unit.lineInfo = new LineInfo(scanner.lineStarts); | 93 unit.lineInfo = new LineInfo(scanner.lineStarts); |
| 104 | 94 |
| 105 if (errorCollector.hasErrors && !suppressErrors) throw errorCollector.group; | 95 if (errorCollector.hasErrors && !suppressErrors) throw errorCollector.group; |
| 106 | 96 |
| 107 return unit; | 97 return unit; |
| 108 } | 98 } |
| 109 | 99 |
| 110 /// Converts an AST node representing a string literal into a [String]. | 100 /// Converts an AST node representing a string literal into a [String]. |
| 111 String stringLiteralToString(StringLiteral literal) { | 101 String stringLiteralToString(StringLiteral literal) { |
| 112 return literal.stringValue; | 102 return literal.stringValue; |
| 113 } | 103 } |
| 114 | 104 |
| 115 /// A simple error listener that collects errors into an [AnalysisErrorGroup]. | 105 CompilationUnit _parseSource(String contents, Source source, |
| 106 {bool suppressErrors: false, bool parseFunctionBodies: true}) { |
| 107 var reader = new CharSequenceReader(contents); |
| 108 var errorCollector = new _ErrorCollector(); |
| 109 var scanner = new Scanner(source, reader, errorCollector); |
| 110 var token = scanner.tokenize(); |
| 111 var parser = new Parser(source, errorCollector) |
| 112 ..parseFunctionBodies = parseFunctionBodies; |
| 113 var unit = parser.parseCompilationUnit(token) |
| 114 ..lineInfo = new LineInfo(scanner.lineStarts); |
| 115 |
| 116 if (errorCollector.hasErrors && !suppressErrors) throw errorCollector.group; |
| 117 |
| 118 return unit; |
| 119 } |
| 120 |
| 121 /// A simple error listener that collects errors into an [AnalyzerErrorGroup]. |
| 116 class _ErrorCollector extends AnalysisErrorListener { | 122 class _ErrorCollector extends AnalysisErrorListener { |
| 117 final _errors = <AnalysisError>[]; | 123 final _errors = <AnalysisError>[]; |
| 118 | 124 |
| 119 _ErrorCollector(); | 125 _ErrorCollector(); |
| 120 | 126 |
| 121 /// The group of errors collected. | 127 /// The group of errors collected. |
| 122 AnalyzerErrorGroup get group => | 128 AnalyzerErrorGroup get group => |
| 123 new AnalyzerErrorGroup.fromAnalysisErrors(_errors); | 129 new AnalyzerErrorGroup.fromAnalysisErrors(_errors); |
| 124 | 130 |
| 125 /// Whether any errors where collected. | 131 /// Whether any errors where collected. |
| 126 bool get hasErrors => !_errors.isEmpty; | 132 bool get hasErrors => !_errors.isEmpty; |
| 127 | 133 |
| 134 @override |
| 128 void onError(AnalysisError error) => _errors.add(error); | 135 void onError(AnalysisError error) => _errors.add(error); |
| 129 } | 136 } |
| OLD | NEW |