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