| Index: pkg/analyzer_experimental/lib/analyzer.dart
|
| diff --git a/pkg/analyzer_experimental/lib/analyzer.dart b/pkg/analyzer_experimental/lib/analyzer.dart
|
| index 47f0129246e018a4386d3610ec63803b26f8344f..bad687394d98dbda725edd80a099fb92eedac755 100644
|
| --- a/pkg/analyzer_experimental/lib/analyzer.dart
|
| +++ b/pkg/analyzer_experimental/lib/analyzer.dart
|
| @@ -1,134 +1,64 @@
|
| // Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file
|
| // for details. All rights reserved. Use of this source code is governed by a
|
| // BSD-style license that can be found in the LICENSE file.
|
| -part of analyzer;
|
|
|
| -/// Analyzes single library [File].
|
| -class _AnalyzerImpl {
|
| - final CommandLineOptions options;
|
| - DartSdk sdk;
|
| +library analyzer;
|
|
|
| - ContentCache contentCache = new ContentCache();
|
| - SourceFactory sourceFactory;
|
| - AnalysisContext context;
|
| +import 'dart:io';
|
|
|
| - /// All [Source]s references by the analyzed library.
|
| - final Set<Source> sources = new Set<Source>();
|
| +import 'src/error.dart';
|
| +import 'src/generated/ast.dart';
|
| +import 'src/generated/error.dart';
|
| +import 'src/generated/java_io.dart';
|
| +import 'src/generated/parser.dart';
|
| +import 'src/generated/scanner.dart';
|
| +import 'src/generated/source_io.dart';
|
| +import 'src/utils.dart';
|
|
|
| - /// All [AnalysisErrorInfo]s in the analyzed library.
|
| - final List<AnalysisErrorInfo> errorInfos = new List<AnalysisErrorInfo>();
|
| +export 'src/error.dart';
|
| +export 'src/generated/ast.dart';
|
| +export 'src/generated/error.dart';
|
|
|
| - _AnalyzerImpl(CommandLineOptions this.options) {
|
| - sdk = new DirectoryBasedDartSdk(new JavaFile(options.dartSdkPath));
|
| - }
|
| +/// Parses a Dart file into an AST.
|
| +CompilationUnit parseDartFile(String path) {
|
| + var contents = new File(path).readAsStringSync();
|
| + var errorCollector = new _ErrorCollector();
|
| + var sourceFactory = new SourceFactory.con2([new FileUriResolver()]);
|
| + var source = sourceFactory.forUri(pathToFileUri(path).toString());
|
| + var scanner = new StringScanner(source, contents, errorCollector);
|
| + var token = scanner.tokenize();
|
| + var parser = new Parser(source, errorCollector);
|
| + var unit = parser.parseCompilationUnit(token);
|
| + unit.lineInfo = new LineInfo(scanner.lineStarts);
|
|
|
| - /**
|
| - * Treats the [sourcePath] as the top level library and analyzes it.
|
| - */
|
| - void analyze(String sourcePath) {
|
| - sources.clear();
|
| - errorInfos.clear();
|
| - if (sourcePath == null) {
|
| - throw new ArgumentError("sourcePath cannot be null");
|
| - }
|
| - var sourceFile = new JavaFile(sourcePath);
|
| - var librarySource = new FileBasedSource.con1(contentCache, sourceFile);
|
| - // resolve library
|
| - prepareAnalysisContext(sourceFile);
|
| - var libraryElement = context.computeLibraryElement(librarySource);
|
| - // prepare source and errors
|
| - prepareSources(libraryElement);
|
| - prepareErrors();
|
| - }
|
| + if (errorCollector.hasErrors) throw errorCollector.group;
|
|
|
| - /// Returns the maximal [ErrorSeverity] of the recorded errors.
|
| - ErrorSeverity get maxErrorSeverity {
|
| - var status = ErrorSeverity.NONE;
|
| - for (AnalysisErrorInfo errorInfo in errorInfos) {
|
| - for (AnalysisError error in errorInfo.errors) {
|
| - var severity = error.errorCode.errorSeverity;
|
| - status = status.max(severity);
|
| - }
|
| - }
|
| - return status;
|
| - }
|
| + return unit;
|
| +}
|
|
|
| - void prepareAnalysisContext(JavaFile sourceFile) {
|
| - List<UriResolver> resolvers = [new DartUriResolver(sdk), new FileUriResolver()];
|
| - // may be add package resolver
|
| - {
|
| - var packageDirectory = getPackageDirectoryFor(sourceFile);
|
| - if (packageDirectory != null) {
|
| - resolvers.add(new PackageUriResolver([packageDirectory]));
|
| - }
|
| - }
|
| - sourceFactory = new SourceFactory.con1(contentCache, resolvers);
|
| - context = AnalysisEngine.instance.createAnalysisContext();
|
| - context.sourceFactory = sourceFactory;
|
| +/// Converts an AST node representing a string literal into a [String].
|
| +String stringLiteralToString(StringLiteral literal) {
|
| + if (literal is AdjacentStrings) {
|
| + return literal.strings.map(stringLiteralToString).join();
|
| + } else if (literal is SimpleStringLiteral) {
|
| + return literal.value;
|
| + } else {
|
| + throw new ArgumentError("Can't convert $literal to a Dart string.");
|
| }
|
| +}
|
|
|
| - /// Fills [sources].
|
| - void prepareSources(LibraryElement library) {
|
| - var units = new Set<CompilationUnitElement>();
|
| - var libraries = new Set<LibraryElement>();
|
| - addLibrarySources(library, libraries, units);
|
| - }
|
| +/// A simple error listener that collects errors into an [AnalysisErrorGroup].
|
| +class _ErrorCollector extends AnalysisErrorListener {
|
| + final _errors = <AnalysisError>[];
|
|
|
| - void addCompilationUnitSource(CompilationUnitElement unit, Set<LibraryElement> libraries,
|
| - Set<CompilationUnitElement> units) {
|
| - if (unit == null || units.contains(unit)) {
|
| - return;
|
| - }
|
| - units.add(unit);
|
| - sources.add(unit.source);
|
| - }
|
| + /// Whether any errors where collected.
|
| + bool get hasErrors => !_errors.isEmpty;
|
|
|
| - void addLibrarySources(LibraryElement library, Set<LibraryElement> libraries,
|
| - Set<CompilationUnitElement> units) {
|
| - if (library == null || libraries.contains(library)) {
|
| - return;
|
| - }
|
| - libraries.add(library);
|
| - // may be skip library
|
| - {
|
| - UriKind uriKind = library.source.uriKind;
|
| - // Optionally skip package: libraries.
|
| - if (!options.showPackageWarnings && uriKind == UriKind.PACKAGE_URI) {
|
| - return;
|
| - }
|
| - // Optionally skip SDK libraries.
|
| - if (!options.showSdkWarnings && uriKind == UriKind.DART_URI) {
|
| - return;
|
| - }
|
| - }
|
| - // add compilation units
|
| - addCompilationUnitSource(library.definingCompilationUnit, libraries, units);
|
| - for (CompilationUnitElement child in library.parts) {
|
| - addCompilationUnitSource(child, libraries, units);
|
| - }
|
| - // add referenced libraries
|
| - for (LibraryElement child in library.importedLibraries) {
|
| - addLibrarySources(child, libraries, units);
|
| - }
|
| - for (LibraryElement child in library.exportedLibraries) {
|
| - addLibrarySources(child, libraries, units);
|
| - }
|
| - }
|
| + /// The group of errors collected.
|
| + AnalyzerErrorGroup get group =>
|
| + new AnalyzerErrorGroup.fromAnalysisErrors(_errors);
|
|
|
| - /// Fills [errorInfos].
|
| - void prepareErrors() {
|
| - for (Source source in sources) {
|
| - var sourceErrors = context.getErrors(source);
|
| - errorInfos.add(sourceErrors);
|
| - }
|
| - }
|
| + _ErrorCollector();
|
|
|
| - static JavaFile getPackageDirectoryFor(JavaFile sourceFile) {
|
| - JavaFile sourceFolder = sourceFile.getParentFile();
|
| - JavaFile packagesFolder = new JavaFile.relative(sourceFolder, "packages");
|
| - if (packagesFolder.exists()) {
|
| - return packagesFolder;
|
| - }
|
| - return null;
|
| - }
|
| + void onError(AnalysisError error) => _errors.add(error);
|
| }
|
|
|