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