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

Unified Diff: pkg/polymer/lib/src/dart_parser.dart

Issue 23224003: move polymer.dart into dart svn (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: add --deploy to todomvc sample 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 | « pkg/polymer/lib/src/custom_tag_name.dart ('k') | pkg/polymer/lib/src/emitters.dart » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: pkg/polymer/lib/src/dart_parser.dart
diff --git a/pkg/polymer/lib/src/dart_parser.dart b/pkg/polymer/lib/src/dart_parser.dart
new file mode 100644
index 0000000000000000000000000000000000000000..a2f44c35284a0cded7b651c1ed635588f44272eb
--- /dev/null
+++ b/pkg/polymer/lib/src/dart_parser.dart
@@ -0,0 +1,132 @@
+// Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file
+// for details. All rights reserved. Use of this source code is governed by a
+// BSD-style license that can be found in the LICENSE file.
+
+/**
+ * Parser for Dart code based on the experimental analyzer.
+ */
+library dart_parser;
+
+import 'package:analyzer_experimental/src/generated/ast.dart';
+import 'package:analyzer_experimental/src/generated/error.dart';
+import 'package:analyzer_experimental/src/generated/parser.dart';
+import 'package:analyzer_experimental/src/generated/scanner.dart';
+import 'package:source_maps/span.dart' show SourceFile, SourceFileSegment, Location;
+import 'utils.dart' show escapeDartString;
+
+/** Information extracted from a source Dart file. */
+class DartCodeInfo {
+
+ /** Library qualified identifier, if any. */
+ final String libraryName;
+
+ /** Library which the code is part-of, if any. */
+ final String partOf;
+
+ /** Declared imports, exports, and parts. */
+ final List<Directive> directives;
+
+ /** Source file representation used to compute source map information. */
+ final SourceFile sourceFile;
+
+ /** The parsed code. */
+ final CompilationUnit compilationUnit;
+
+ /** The full source code. */
+ final String code;
+
+ DartCodeInfo(this.libraryName, this.partOf, this.directives, code,
+ this.sourceFile, [compilationUnit])
+ : this.code = code,
+ this.compilationUnit = compilationUnit == null
+ ? _parseCompilationUnit(code) : compilationUnit;
+
+ bool get isPart =>
+ compilationUnit.directives.any((d) => d is PartOfDirective);
+
+ int get directivesEnd {
+ if (compilationUnit.directives.length == 0) return 0;
+ return compilationUnit.directives.last.end;
+ }
+
+ /**
+ * The position of the first "part" directive. If none is found,
+ * this behaves like [directivesEnd].
+ */
+ int get firstPartOffset {
+ for (var directive in compilationUnit.directives) {
+ if (directive is PartDirective) return directive.offset;
+ }
+ // No part directives, just return directives end.
+ return directivesEnd;
+ }
+
+ /** Gets the code after the [directives]. */
+ String codeAfterDirectives() => code.substring(directivesEnd);
+
+ ClassDeclaration findClass(String name) {
+ for (var decl in compilationUnit.declarations) {
+ if (decl is ClassDeclaration) {
+ if (decl.name.name == name) return decl;
+ }
+ }
+ return null;
+ }
+}
+
+SimpleStringLiteral createStringLiteral(String contents) {
+ var lexeme = "'${escapeDartString(contents)}'";
+ var token = new StringToken(TokenType.STRING, lexeme, null);
+ return new SimpleStringLiteral.full(token, contents);
+}
+
+
+/**
+ * Parse and extract top-level directives from [code].
+ *
+ */
+// TODO(sigmund): log emitted error/warning messages
+DartCodeInfo parseDartCode(String path, String code, [Location offset]) {
+ var unit = _parseCompilationUnit(code);
+
+ // Extract some information from the compilation unit.
+ String libraryName, partName;
+ var directives = [];
+ int directiveEnd = 0;
+ for (var directive in unit.directives) {
+ if (directive is LibraryDirective) {
+ libraryName = directive.name.name;
+ } else if (directive is PartOfDirective) {
+ partName = directive.libraryName.name;
+ } else {
+ assert(directive is UriBasedDirective);
+ // Normalize the library URI.
+ var uriNode = directive.uri;
+ if (uriNode is! SimpleStringLiteral) {
+ String uri = uriNode.accept(new ConstantEvaluator());
+ directive.uri = createStringLiteral(uri);
+ }
+ directives.add(directive);
+ }
+ }
+
+ var sourceFile = offset == null
+ ? new SourceFile.text(path, code)
+ : new SourceFileSegment(path, code, offset);
+
+ return new DartCodeInfo(libraryName, partName, directives, code,
+ sourceFile, unit);
+}
+
+CompilationUnit _parseCompilationUnit(String code) {
+ var errorListener = new _ErrorCollector();
+ var scanner = new StringScanner(null, code, errorListener);
+ var token = scanner.tokenize();
+ var parser = new Parser(null, errorListener);
+ return parser.parseCompilationUnit(token);
+}
+
+class _ErrorCollector extends AnalysisErrorListener {
+ final errors = new List<AnalysisError>();
+ onError(error) => errors.add(error);
+}
« no previous file with comments | « pkg/polymer/lib/src/custom_tag_name.dart ('k') | pkg/polymer/lib/src/emitters.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698