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 library yaml.constructor; | 5 library yaml.constructor; |
6 | 6 |
| 7 import 'equality.dart'; |
7 import 'model.dart'; | 8 import 'model.dart'; |
8 import 'visitor.dart'; | 9 import 'visitor.dart'; |
9 import 'yaml_map.dart'; | 10 import 'yaml_node.dart'; |
10 | 11 |
11 /// Takes a parsed and composed YAML document (what the spec calls the | 12 /// Takes a parsed and composed YAML document (what the spec calls the |
12 /// "representation graph") and creates native Dart objects that represent that | 13 /// "representation graph") and creates native Dart objects that represent that |
13 /// document. | 14 /// document. |
14 class Constructor extends Visitor { | 15 class Constructor extends Visitor { |
15 /// The root node of the representation graph. | 16 /// The root node of the representation graph. |
16 final Node _root; | 17 final Node _root; |
17 | 18 |
18 /// Map from anchor names to the most recent Dart node with that anchor. | 19 /// Map from anchor names to the most recent Dart node with that anchor. |
19 final _anchors = <String, dynamic>{}; | 20 final _anchors = <String, YamlNode>{}; |
20 | 21 |
21 Constructor(this._root); | 22 Constructor(this._root); |
22 | 23 |
23 /// Runs the Constructor to produce a Dart object. | 24 /// Runs the Constructor to produce a Dart object. |
24 construct() => _root.visit(this); | 25 YamlNode construct() => _root.visit(this); |
25 | 26 |
26 /// Returns the value of a scalar. | 27 /// Returns the value of a scalar. |
27 visitScalar(ScalarNode scalar) => scalar.value; | 28 YamlScalar visitScalar(ScalarNode scalar) => |
| 29 new YamlScalar(scalar.value, scalar.span); |
28 | 30 |
29 /// Converts a sequence into a List of Dart objects. | 31 /// Converts a sequence into a List of Dart objects. |
30 visitSequence(SequenceNode seq) { | 32 YamlList visitSequence(SequenceNode seq) { |
31 var anchor = getAnchor(seq); | 33 var anchor = getAnchor(seq); |
32 if (anchor != null) return anchor; | 34 if (anchor != null) return anchor; |
33 var dartSeq = setAnchor(seq, []); | 35 var nodes = []; |
34 dartSeq.addAll(super.visitSequence(seq)); | 36 var dartSeq = setAnchor(seq, new YamlList(nodes, seq.span)); |
| 37 nodes.addAll(super.visitSequence(seq)); |
35 return dartSeq; | 38 return dartSeq; |
36 } | 39 } |
37 | 40 |
38 /// Converts a mapping into a Map of Dart objects. | 41 /// Converts a mapping into a [Map] of Dart objects. |
39 visitMapping(MappingNode map) { | 42 YamlMap visitMapping(MappingNode map) { |
40 var anchor = getAnchor(map); | 43 var anchor = getAnchor(map); |
41 if (anchor != null) return anchor; | 44 if (anchor != null) return anchor; |
42 var dartMap = setAnchor(map, new YamlMap()); | 45 var nodes = deepEqualsMap(); |
43 super.visitMapping(map).forEach((k, v) { dartMap[k] = v; }); | 46 var dartMap = setAnchor(map, new YamlMap(nodes, map.span)); |
| 47 super.visitMapping(map).forEach((k, v) => nodes[k] = v); |
44 return dartMap; | 48 return dartMap; |
45 } | 49 } |
46 | 50 |
47 /// Returns the Dart object that already represents [anchored], if such a | 51 /// Returns a new Dart object wrapping the object that already represents |
48 /// thing exists. | 52 /// [anchored], if such a thing exists. |
49 getAnchor(Node anchored) { | 53 YamlNode getAnchor(Node anchored) { |
50 if (anchored.anchor == null) return null; | 54 if (anchored.anchor == null) return null; |
51 if (_anchors.containsKey(anchored.anchor)) return _anchors[anchored.anchor]; | 55 var value = _anchors[anchored.anchor]; |
| 56 if (vaule == null) return null; |
| 57 |
| 58 // Re-wrap [value]'s contents so that it's associated with the span of the |
| 59 // anchor rather than its original definition. |
| 60 if (value is YamlMap) return new YamlMap(value.nodes, anchored.span); |
| 61 if (value is YamlList) return new YamlList(value.nodes, anchored.span); |
| 62 assert(value is YamlScalar); |
| 63 return new YamlScalar(value.value, anchored.span); |
52 } | 64 } |
53 | 65 |
54 /// Records that [value] is the Dart object representing [anchored]. | 66 /// Records that [value] is the Dart object representing [anchored]. |
55 setAnchor(Node anchored, value) { | 67 YamlNode setAnchor(Node anchored, YamlNode value) { |
56 if (anchored.anchor == null) return value; | 68 if (anchored.anchor == null) return value; |
57 _anchors[anchored.anchor] = value; | 69 _anchors[anchored.anchor] = value; |
58 return value; | 70 return value; |
59 } | 71 } |
60 } | 72 } |
OLD | NEW |