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

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

Issue 22865035: Add a parseDartString method to pkg/analyzer. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Code review changes. Created 7 years, 4 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_experimental/lib/src/string_source.dart » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: pkg/analyzer_experimental/lib/analyzer.dart
diff --git a/pkg/analyzer_experimental/lib/analyzer.dart b/pkg/analyzer_experimental/lib/analyzer.dart
index a88c94c2060b037c77b9500cb1837b6f55fa18ad..6424cbf890510a2d560e13b8b4ce02c9ee55ede2 100644
--- a/pkg/analyzer_experimental/lib/analyzer.dart
+++ b/pkg/analyzer_experimental/lib/analyzer.dart
@@ -14,6 +14,7 @@ import 'src/generated/error.dart';
import 'src/generated/parser.dart';
import 'src/generated/scanner.dart';
import 'src/generated/source_io.dart';
+import 'src/string_source.dart';
export 'src/error.dart';
export 'src/generated/ast.dart';
@@ -46,6 +47,25 @@ CompilationUnit parseDartFile(String path) {
return unit;
}
+/// Parses a string of Dart code into an AST.
+///
+/// If [name] is passed, it's used in error messages as the name of the code
+/// being parsed.
+CompilationUnit parseCompilationUnit(String contents, {String name}) {
+ if (name == null) name = '<unknown source>';
+ var source = new StringSource(contents, name);
+ var errorCollector = new _ErrorCollector();
+ 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);
+
+ if (errorCollector.hasErrors) 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_experimental/lib/src/string_source.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698