| 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 17 matching lines...) Expand all Loading... |
| 28 /// | 28 /// |
| 29 /// Throws an [AnalyzerErrorGroup] if any errors occurred, unless | 29 /// Throws an [AnalyzerErrorGroup] if any errors occurred, unless |
| 30 /// [suppressErrors] is `true`, in which case any errors are discarded. | 30 /// [suppressErrors] is `true`, in which case any errors are discarded. |
| 31 /// | 31 /// |
| 32 /// If [parseFunctionBodies] is [false] then only function signatures will be | 32 /// If [parseFunctionBodies] is [false] then only function signatures will be |
| 33 /// parsed. | 33 /// parsed. |
| 34 CompilationUnit parseCompilationUnit(String contents, | 34 CompilationUnit parseCompilationUnit(String contents, |
| 35 {String name, bool suppressErrors: false, bool parseFunctionBodies: true}) { | 35 {String name, bool suppressErrors: false, bool parseFunctionBodies: true}) { |
| 36 if (name == null) name = '<unknown source>'; | 36 if (name == null) name = '<unknown source>'; |
| 37 var source = new StringSource(contents, name); | 37 var source = new StringSource(contents, name); |
| 38 var errorCollector = new _ErrorCollector(); | 38 return _parseSource(contents, source, |
| 39 var reader = new CharSequenceReader(contents); | 39 suppressErrors: suppressErrors, parseFunctionBodies: parseFunctionBodies); |
| 40 var scanner = new Scanner(source, reader, errorCollector); | |
| 41 var token = scanner.tokenize(); | |
| 42 var parser = new Parser(source, errorCollector); | |
| 43 parser.parseFunctionBodies = parseFunctionBodies; | |
| 44 var unit = parser.parseCompilationUnit(token); | |
| 45 unit.lineInfo = new LineInfo(scanner.lineStarts); | |
| 46 | |
| 47 if (errorCollector.hasErrors && !suppressErrors) throw errorCollector.group; | |
| 48 | |
| 49 return unit; | |
| 50 } | 40 } |
| 51 | 41 |
| 52 /// Parses a Dart file into an AST. | 42 /// Parses a Dart file into an AST. |
| 53 CompilationUnit parseDartFile(String path) { | 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}) { |
| 54 String contents = new File(path).readAsStringSync(); | 51 String contents = new File(path).readAsStringSync(); |
| 55 var errorCollector = new _ErrorCollector(); | |
| 56 var sourceFactory = new SourceFactory([new FileUriResolver()]); | 52 var sourceFactory = new SourceFactory([new FileUriResolver()]); |
| 57 | 53 |
| 58 var absolutePath = pathos.absolute(path); | 54 var absolutePath = pathos.absolute(path); |
| 59 var source = sourceFactory.forUri(pathos.toUri(absolutePath).toString()); | 55 var source = sourceFactory.forUri(pathos.toUri(absolutePath).toString()); |
| 60 if (source == null) { | 56 if (source == null) { |
| 61 throw new ArgumentError("Can't get source for path $path"); | 57 throw new ArgumentError("Can't get source for path $path"); |
| 62 } | 58 } |
| 63 if (!source.exists()) { | 59 if (!source.exists()) { |
| 64 throw new ArgumentError("Source $source doesn't exist"); | 60 throw new ArgumentError("Source $source doesn't exist"); |
| 65 } | 61 } |
| 66 | 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}) { |
| 67 var reader = new CharSequenceReader(contents); | 69 var reader = new CharSequenceReader(contents); |
| 70 var errorCollector = new _ErrorCollector(); |
| 68 var scanner = new Scanner(source, reader, errorCollector); | 71 var scanner = new Scanner(source, reader, errorCollector); |
| 69 var token = scanner.tokenize(); | 72 var token = scanner.tokenize(); |
| 70 var parser = new Parser(source, errorCollector); | 73 var parser = new Parser(source, errorCollector) |
| 71 var unit = parser.parseCompilationUnit(token); | 74 ..parseFunctionBodies = parseFunctionBodies; |
| 72 unit.lineInfo = new LineInfo(scanner.lineStarts); | 75 var unit = parser.parseCompilationUnit(token) |
| 76 ..lineInfo = new LineInfo(scanner.lineStarts); |
| 73 | 77 |
| 74 if (errorCollector.hasErrors) throw errorCollector.group; | 78 if (errorCollector.hasErrors && !suppressErrors) throw errorCollector.group; |
| 75 | 79 |
| 76 return unit; | 80 return unit; |
| 77 } | 81 } |
| 78 | 82 |
| 79 /// Parses the script tag and directives in a string of Dart code into an AST. | 83 /// Parses the script tag and directives in a string of Dart code into an AST. |
| 80 /// | 84 /// |
| 81 /// Stops parsing when the first non-directive is encountered. The rest of the | 85 /// Stops parsing when the first non-directive is encountered. The rest of the |
| 82 /// string will not be parsed. | 86 /// string will not be parsed. |
| 83 /// | 87 /// |
| 84 /// If [name] is passed, it's used in error messages as the name of the code | 88 /// If [name] is passed, it's used in error messages as the name of the code |
| (...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 116 | 120 |
| 117 /// The group of errors collected. | 121 /// The group of errors collected. |
| 118 AnalyzerErrorGroup get group => | 122 AnalyzerErrorGroup get group => |
| 119 new AnalyzerErrorGroup.fromAnalysisErrors(_errors); | 123 new AnalyzerErrorGroup.fromAnalysisErrors(_errors); |
| 120 | 124 |
| 121 /// Whether any errors where collected. | 125 /// Whether any errors where collected. |
| 122 bool get hasErrors => !_errors.isEmpty; | 126 bool get hasErrors => !_errors.isEmpty; |
| 123 | 127 |
| 124 void onError(AnalysisError error) => _errors.add(error); | 128 void onError(AnalysisError error) => _errors.add(error); |
| 125 } | 129 } |
| OLD | NEW |