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