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

Unified Diff: lib/yaml/constructor.dart

Issue 10153004: Add a basic YAML processor. Much of the language is still unimplemented. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Created 8 years, 8 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
Index: lib/yaml/constructor.dart
diff --git a/lib/yaml/constructor.dart b/lib/yaml/constructor.dart
new file mode 100644
index 0000000000000000000000000000000000000000..71af6e5cd4453a30aca6a947ffc785059621a816
--- /dev/null
+++ b/lib/yaml/constructor.dart
@@ -0,0 +1,58 @@
+// Copyright (c) 2012, 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.
+
+/**
+ * Takes a parsed and composed YAML document (what the spec calls the
+ * "representation graph") and creates native Dart objects that represent that
+ * document.
+ */
+class _Constructor extends _Visitor {
+ /** The root node of the representation graph. */
+ _Node _root;
+
+ /** Map from anchor names to the most recent Dart node with that anchor. */
+ Map<String, Dynamic> _anchors;
+
+ _Constructor(_Node this._root) : this._anchors = {};
+
+ /** Runs the Constructor to produce a Dart object. */
+ construct() => _root.visit(this);
+
+ /** Returns the value of a scalar. */
+ visitScalar(_ScalarNode scalar) => scalar.value;
+
+ /** Converts a sequence into a List of Dart objects. */
+ visitSequence(_SequenceNode seq) {
+ var anchor = _getAnchor(seq);
+ if (anchor != null) return anchor;
+ var dartSeq = _setAnchor(seq, []);
+ dartSeq.addAll(super.visitSequence(seq));
+ return dartSeq;
+ }
+
+ /** Converts a mapping into a Map of Dart objects. */
+ visitMapping(_MappingNode map) {
+ var anchor = _getAnchor(map);
+ if (anchor != null) return anchor;
+ var dartMap = _setAnchor(map, new YamlMap());
+ super.visitMapping(map).forEach((k, v) { dartMap[k] = v; });
+ return dartMap;
+ }
+
+ /**
+ * Returns the Dart object that already represents [anchored], if such a thing
+ * exists.
+ */
+ _getAnchor(_Node anchored) {
+ if (anchored.anchor == null) return null;
+ if (_anchors.containsKey(anchored.anchor)) return _anchors[anchored.anchor];
+ }
+
+ /** Records that [value] is the Dart object representing [anchored]. */
+ _setAnchor(_Node anchored, value) {
+ if (anchored.anchor == null) return value;
+ _anchors[anchored.anchor] = value;
+ return value;
+ }
+}

Powered by Google App Engine
This is Rietveld 408576698