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