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'; |
9 import 'package:analyzer/src/error.dart'; | 10 import 'package:analyzer/src/error.dart'; |
10 import 'package:analyzer/src/generated/ast.dart'; | |
11 import 'package:analyzer/src/generated/error.dart'; | 11 import 'package:analyzer/src/generated/error.dart'; |
12 import 'package:analyzer/src/generated/parser.dart'; | 12 import 'package:analyzer/src/generated/parser.dart'; |
13 import 'package:analyzer/src/generated/scanner.dart'; | 13 import 'package:analyzer/src/generated/scanner.dart'; |
14 import 'package:analyzer/src/generated/source_io.dart'; | 14 import 'package:analyzer/src/generated/source_io.dart'; |
15 import 'package:analyzer/src/string_source.dart'; | 15 import 'package:analyzer/src/string_source.dart'; |
16 import 'package:path/path.dart' as pathos; | 16 import 'package:path/path.dart' as pathos; |
17 | 17 |
| 18 export 'package:analyzer/dart/ast/ast.dart'; |
| 19 export 'package:analyzer/dart/ast/visitor.dart'; |
| 20 export 'package:analyzer/src/dart/ast/utilities.dart'; |
18 export 'package:analyzer/src/error.dart'; | 21 export 'package:analyzer/src/error.dart'; |
19 export 'package:analyzer/src/generated/ast.dart'; | |
20 export 'package:analyzer/src/generated/error.dart'; | 22 export 'package:analyzer/src/generated/error.dart'; |
21 export 'package:analyzer/src/generated/utilities_dart.dart'; | 23 export 'package:analyzer/src/generated/utilities_dart.dart'; |
22 | 24 |
23 /// Parses a string of Dart code into an AST. | 25 /// Parses a string of Dart code into an AST. |
24 /// | 26 /// |
25 /// If [name] is passed, it's used in error messages as the name of the code | 27 /// If [name] is passed, it's used in error messages as the name of the code |
26 /// being parsed. | 28 /// being parsed. |
27 /// | 29 /// |
28 /// Throws an [AnalyzerErrorGroup] if any errors occurred, unless | 30 /// Throws an [AnalyzerErrorGroup] if any errors occurred, unless |
29 /// [suppressErrors] is `true`, in which case any errors are discarded. | 31 /// [suppressErrors] is `true`, in which case any errors are discarded. |
(...skipping 26 matching lines...) Expand all Loading... |
56 throw new ArgumentError("Can't get source for path $path"); | 58 throw new ArgumentError("Can't get source for path $path"); |
57 } | 59 } |
58 if (!source.exists()) { | 60 if (!source.exists()) { |
59 throw new ArgumentError("Source $source doesn't exist"); | 61 throw new ArgumentError("Source $source doesn't exist"); |
60 } | 62 } |
61 | 63 |
62 return _parseSource(contents, source, | 64 return _parseSource(contents, source, |
63 suppressErrors: suppressErrors, parseFunctionBodies: parseFunctionBodies); | 65 suppressErrors: suppressErrors, parseFunctionBodies: parseFunctionBodies); |
64 } | 66 } |
65 | 67 |
66 CompilationUnit _parseSource(String contents, Source source, | |
67 {bool suppressErrors: false, bool parseFunctionBodies: true}) { | |
68 var reader = new CharSequenceReader(contents); | |
69 var errorCollector = new _ErrorCollector(); | |
70 var scanner = new Scanner(source, reader, errorCollector); | |
71 var token = scanner.tokenize(); | |
72 var parser = new Parser(source, errorCollector) | |
73 ..parseFunctionBodies = parseFunctionBodies; | |
74 var unit = parser.parseCompilationUnit(token) | |
75 ..lineInfo = new LineInfo(scanner.lineStarts); | |
76 | |
77 if (errorCollector.hasErrors && !suppressErrors) throw errorCollector.group; | |
78 | |
79 return unit; | |
80 } | |
81 | |
82 /// Parses the script tag and directives in a string of Dart code into an AST. | 68 /// Parses the script tag and directives in a string of Dart code into an AST. |
83 /// | 69 /// |
84 /// Stops parsing when the first non-directive is encountered. The rest of the | 70 /// Stops parsing when the first non-directive is encountered. The rest of the |
85 /// string will not be parsed. | 71 /// string will not be parsed. |
86 /// | 72 /// |
87 /// If [name] is passed, it's used in error messages as the name of the code | 73 /// If [name] is passed, it's used in error messages as the name of the code |
88 /// being parsed. | 74 /// being parsed. |
89 /// | 75 /// |
90 /// Throws an [AnalyzerErrorGroup] if any errors occurred, unless | 76 /// Throws an [AnalyzerErrorGroup] if any errors occurred, unless |
91 /// [suppressErrors] is `true`, in which case any errors are discarded. | 77 /// [suppressErrors] is `true`, in which case any errors are discarded. |
(...skipping 12 matching lines...) Expand all Loading... |
104 if (errorCollector.hasErrors && !suppressErrors) throw errorCollector.group; | 90 if (errorCollector.hasErrors && !suppressErrors) throw errorCollector.group; |
105 | 91 |
106 return unit; | 92 return unit; |
107 } | 93 } |
108 | 94 |
109 /// Converts an AST node representing a string literal into a [String]. | 95 /// Converts an AST node representing a string literal into a [String]. |
110 String stringLiteralToString(StringLiteral literal) { | 96 String stringLiteralToString(StringLiteral literal) { |
111 return literal.stringValue; | 97 return literal.stringValue; |
112 } | 98 } |
113 | 99 |
| 100 CompilationUnit _parseSource(String contents, Source source, |
| 101 {bool suppressErrors: false, bool parseFunctionBodies: true}) { |
| 102 var reader = new CharSequenceReader(contents); |
| 103 var errorCollector = new _ErrorCollector(); |
| 104 var scanner = new Scanner(source, reader, errorCollector); |
| 105 var token = scanner.tokenize(); |
| 106 var parser = new Parser(source, errorCollector) |
| 107 ..parseFunctionBodies = parseFunctionBodies; |
| 108 var unit = parser.parseCompilationUnit(token) |
| 109 ..lineInfo = new LineInfo(scanner.lineStarts); |
| 110 |
| 111 if (errorCollector.hasErrors && !suppressErrors) throw errorCollector.group; |
| 112 |
| 113 return unit; |
| 114 } |
| 115 |
114 /// A simple error listener that collects errors into an [AnalysisErrorGroup]. | 116 /// A simple error listener that collects errors into an [AnalysisErrorGroup]. |
115 class _ErrorCollector extends AnalysisErrorListener { | 117 class _ErrorCollector extends AnalysisErrorListener { |
116 final _errors = <AnalysisError>[]; | 118 final _errors = <AnalysisError>[]; |
117 | 119 |
118 _ErrorCollector(); | 120 _ErrorCollector(); |
119 | 121 |
120 /// The group of errors collected. | 122 /// The group of errors collected. |
121 AnalyzerErrorGroup get group => | 123 AnalyzerErrorGroup get group => |
122 new AnalyzerErrorGroup.fromAnalysisErrors(_errors); | 124 new AnalyzerErrorGroup.fromAnalysisErrors(_errors); |
123 | 125 |
124 /// Whether any errors where collected. | 126 /// Whether any errors where collected. |
125 bool get hasErrors => !_errors.isEmpty; | 127 bool get hasErrors => !_errors.isEmpty; |
126 | 128 |
127 void onError(AnalysisError error) => _errors.add(error); | 129 void onError(AnalysisError error) => _errors.add(error); |
128 } | 130 } |
OLD | NEW |