Chromium Code Reviews| 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. |
|
Bob Nystrom
2014/06/03 00:33:14
"Map" -> "[Map]".
nweiz
2014/06/03 02:03:21
Done.
| |
| 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 the Dart object that already represents [anchored], if such a |
|
Bob Nystrom
2014/06/03 00:33:14
This is a bit confusing since it's a new object no
nweiz
2014/06/03 02:03:21
Done.
| |
| 48 /// thing exists. | 52 /// 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 if (!_anchors.containsKey(anchored.anchor)) return null; |
| 56 | |
| 57 var value = _anchors[anchored.anchor]; | |
|
Bob Nystrom
2014/06/03 00:33:14
Can this ever be null now? If not, use it for the
Bob Nystrom
2014/06/03 00:33:14
Add a comment explaining why these are copied. Is
nweiz
2014/06/03 02:03:21
Done.
nweiz
2014/06/03 02:03:21
Done.
| |
| 58 if (value is YamlMap) return new YamlMap(value.nodes, anchored.span); | |
| 59 if (value is YamlList) return new YamlList(value.nodes, anchored.span); | |
| 60 assert(value is YamlScalar); | |
| 61 return new YamlScalar(value.value, anchored.span); | |
| 52 } | 62 } |
| 53 | 63 |
| 54 /// Records that [value] is the Dart object representing [anchored]. | 64 /// Records that [value] is the Dart object representing [anchored]. |
| 55 setAnchor(Node anchored, value) { | 65 YamlNode setAnchor(Node anchored, YamlNode value) { |
| 56 if (anchored.anchor == null) return value; | 66 if (anchored.anchor == null) return value; |
| 57 _anchors[anchored.anchor] = value; | 67 _anchors[anchored.anchor] = value; |
| 58 return value; | 68 return value; |
| 59 } | 69 } |
| 60 } | 70 } |
| OLD | NEW |