Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(12)

Unified Diff: pkg/analyzer/lib/analyzer.dart

Issue 314583007: Only parse the directives when rewriting imports. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Revise! Created 6 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « no previous file | pkg/analyzer/pubspec.yaml » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: pkg/analyzer/lib/analyzer.dart
diff --git a/pkg/analyzer/lib/analyzer.dart b/pkg/analyzer/lib/analyzer.dart
index fc9cfa11e3080626f3bb8446759a0869fb3481b9..dc0b647a361304f9dc88e7f1b026e90b50471e05 100644
--- a/pkg/analyzer/lib/analyzer.dart
+++ b/pkg/analyzer/lib/analyzer.dart
@@ -52,6 +52,9 @@ CompilationUnit parseDartFile(String path) {
///
/// If [name] is passed, it's used in error messages as the name of the code
/// being parsed.
+///
+/// Throws an [AnalyzerErrorGroup] if any errors occurred, unless
+/// [suppressErrors] is `true`, in which case any errors are discarded.
CompilationUnit parseCompilationUnit(String contents,
{String name, bool suppressErrors: false}) {
if (name == null) name = '<unknown source>';
@@ -69,6 +72,33 @@ CompilationUnit parseCompilationUnit(String contents,
return unit;
}
+/// Parses the script tag and directives in a string of Dart code into an AST.
+///
+/// Stops parsing when the first non-directive is encountered. The rest of the
+/// string will not be parsed.
+///
+/// If [name] is passed, it's used in error messages as the name of the code
+/// being parsed.
+///
+/// Throws an [AnalyzerErrorGroup] if any errors occurred, unless
+/// [suppressErrors] is `true`, in which case any errors are discarded.
+CompilationUnit parseDirectives(String contents,
+ {String name, bool suppressErrors: false}) {
+ if (name == null) name = '<unknown source>';
+ var source = new StringSource(contents, name);
+ var errorCollector = new _ErrorCollector();
+ var reader = new CharSequenceReader(contents);
+ var scanner = new Scanner(source, reader, errorCollector);
+ var token = scanner.tokenize();
+ var parser = new Parser(source, errorCollector);
+ var unit = parser.parseDirectives(token);
+ unit.lineInfo = new LineInfo(scanner.lineStarts);
+
+ if (errorCollector.hasErrors && !suppressErrors) throw errorCollector.group;
+
+ return unit;
+}
+
/// Converts an AST node representing a string literal into a [String].
String stringLiteralToString(StringLiteral literal) {
return literal.stringValue;
« no previous file with comments | « no previous file | pkg/analyzer/pubspec.yaml » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698