OLD | NEW |
1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2012, 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 /** | 5 /// Takes a parsed and composed YAML document (what the spec calls the |
6 * Takes a parsed and composed YAML document (what the spec calls the | 6 /// "representation graph") and creates native Dart objects that represent that |
7 * "representation graph") and creates native Dart objects that represent that | 7 /// document. |
8 * document. | |
9 */ | |
10 class _Constructor extends _Visitor { | 8 class _Constructor extends _Visitor { |
11 /** The root node of the representation graph. */ | 9 /// The root node of the representation graph. |
12 _Node root; | 10 _Node root; |
13 | 11 |
14 /** Map from anchor names to the most recent Dart node with that anchor. */ | 12 /// Map from anchor names to the most recent Dart node with that anchor. |
15 Map<String, dynamic> anchors; | 13 Map<String, dynamic> anchors; |
16 | 14 |
17 _Constructor(this.root) : this.anchors = {}; | 15 _Constructor(this.root) : this.anchors = {}; |
18 | 16 |
19 /** Runs the Constructor to produce a Dart object. */ | 17 /// Runs the Constructor to produce a Dart object. |
20 construct() => root.visit(this); | 18 construct() => root.visit(this); |
21 | 19 |
22 /** Returns the value of a scalar. */ | 20 /// Returns the value of a scalar. |
23 visitScalar(_ScalarNode scalar) => scalar.value; | 21 visitScalar(_ScalarNode scalar) => scalar.value; |
24 | 22 |
25 /** Converts a sequence into a List of Dart objects. */ | 23 /// Converts a sequence into a List of Dart objects. |
26 visitSequence(_SequenceNode seq) { | 24 visitSequence(_SequenceNode seq) { |
27 var anchor = getAnchor(seq); | 25 var anchor = getAnchor(seq); |
28 if (anchor != null) return anchor; | 26 if (anchor != null) return anchor; |
29 var dartSeq = setAnchor(seq, []); | 27 var dartSeq = setAnchor(seq, []); |
30 dartSeq.addAll(super.visitSequence(seq)); | 28 dartSeq.addAll(super.visitSequence(seq)); |
31 return dartSeq; | 29 return dartSeq; |
32 } | 30 } |
33 | 31 |
34 /** Converts a mapping into a Map of Dart objects. */ | 32 /// Converts a mapping into a Map of Dart objects. |
35 visitMapping(_MappingNode map) { | 33 visitMapping(_MappingNode map) { |
36 var anchor = getAnchor(map); | 34 var anchor = getAnchor(map); |
37 if (anchor != null) return anchor; | 35 if (anchor != null) return anchor; |
38 var dartMap = setAnchor(map, new YamlMap()); | 36 var dartMap = setAnchor(map, new YamlMap()); |
39 super.visitMapping(map).forEach((k, v) { dartMap[k] = v; }); | 37 super.visitMapping(map).forEach((k, v) { dartMap[k] = v; }); |
40 return dartMap; | 38 return dartMap; |
41 } | 39 } |
42 | 40 |
43 /** | 41 /// Returns the Dart object that already represents [anchored], if such a |
44 * Returns the Dart object that already represents [anchored], if such a thing | 42 /// thing exists. |
45 * exists. | |
46 */ | |
47 getAnchor(_Node anchored) { | 43 getAnchor(_Node anchored) { |
48 if (anchored.anchor == null) return null; | 44 if (anchored.anchor == null) return null; |
49 if (anchors.containsKey(anchored.anchor)) return anchors[anchored.anchor]; | 45 if (anchors.containsKey(anchored.anchor)) return anchors[anchored.anchor]; |
50 } | 46 } |
51 | 47 |
52 /** Records that [value] is the Dart object representing [anchored]. */ | 48 /// Records that [value] is the Dart object representing [anchored]. |
53 setAnchor(_Node anchored, value) { | 49 setAnchor(_Node anchored, value) { |
54 if (anchored.anchor == null) return value; | 50 if (anchored.anchor == null) return value; |
55 anchors[anchored.anchor] = value; | 51 anchors[anchored.anchor] = value; |
56 return value; | 52 return value; |
57 } | 53 } |
58 } | 54 } |
OLD | NEW |