Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(224)

Side by Side Diff: pkg/yaml/lib/src/constructor.dart

Issue 343063002: Add new constructors to YamlNode subclasses. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: code review Created 6 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « pkg/yaml/CHANGELOG.md ('k') | pkg/yaml/lib/src/null_span.dart » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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 'equality.dart';
8 import 'model.dart'; 8 import 'model.dart';
9 import 'visitor.dart'; 9 import 'visitor.dart';
10 import 'yaml_node.dart'; 10 import 'yaml_node.dart';
11 11
12 /// 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
13 /// "representation graph") and creates native Dart objects that represent that 13 /// "representation graph") and creates native Dart objects that represent that
14 /// document. 14 /// document.
15 class Constructor extends Visitor { 15 class Constructor extends Visitor {
16 /// The root node of the representation graph. 16 /// The root node of the representation graph.
17 final Node _root; 17 final Node _root;
18 18
19 /// 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.
20 final _anchors = <String, YamlNode>{}; 20 final _anchors = <String, YamlNode>{};
21 21
22 Constructor(this._root); 22 Constructor(this._root);
23 23
24 /// Runs the Constructor to produce a Dart object. 24 /// Runs the Constructor to produce a Dart object.
25 YamlNode construct() => _root.visit(this); 25 YamlNode construct() => _root.visit(this);
26 26
27 /// Returns the value of a scalar. 27 /// Returns the value of a scalar.
28 YamlScalar visitScalar(ScalarNode scalar) => 28 YamlScalar visitScalar(ScalarNode scalar) =>
29 new YamlScalar(scalar.value, scalar.span); 29 new YamlScalar.internal(scalar.value, scalar.span);
30 30
31 /// Converts a sequence into a List of Dart objects. 31 /// Converts a sequence into a List of Dart objects.
32 YamlList visitSequence(SequenceNode seq) { 32 YamlList visitSequence(SequenceNode seq) {
33 var anchor = getAnchor(seq); 33 var anchor = getAnchor(seq);
34 if (anchor != null) return anchor; 34 if (anchor != null) return anchor;
35 var nodes = []; 35 var nodes = [];
36 var dartSeq = setAnchor(seq, new YamlList(nodes, seq.span)); 36 var dartSeq = setAnchor(seq, new YamlList.internal(nodes, seq.span));
37 nodes.addAll(super.visitSequence(seq)); 37 nodes.addAll(super.visitSequence(seq));
38 return dartSeq; 38 return dartSeq;
39 } 39 }
40 40
41 /// Converts a mapping into a [Map] of Dart objects. 41 /// Converts a mapping into a [Map] of Dart objects.
42 YamlMap visitMapping(MappingNode map) { 42 YamlMap visitMapping(MappingNode map) {
43 var anchor = getAnchor(map); 43 var anchor = getAnchor(map);
44 if (anchor != null) return anchor; 44 if (anchor != null) return anchor;
45 var nodes = deepEqualsMap(); 45 var nodes = deepEqualsMap();
46 var dartMap = setAnchor(map, new YamlMap(nodes, map.span)); 46 var dartMap = setAnchor(map, new YamlMap.internal(nodes, map.span));
47 super.visitMapping(map).forEach((k, v) => nodes[k] = v); 47 super.visitMapping(map).forEach((k, v) => nodes[k] = v);
48 return dartMap; 48 return dartMap;
49 } 49 }
50 50
51 /// Returns a new Dart object wrapping the object that already represents 51 /// Returns a new Dart object wrapping the object that already represents
52 /// [anchored], if such a thing exists. 52 /// [anchored], if such a thing exists.
53 YamlNode getAnchor(Node anchored) { 53 YamlNode getAnchor(Node anchored) {
54 if (anchored.anchor == null) return null; 54 if (anchored.anchor == null) return null;
55 var value = _anchors[anchored.anchor]; 55 var value = _anchors[anchored.anchor];
56 if (value == null) return null; 56 if (value == null) return null;
57 57
58 // Re-wrap [value]'s contents so that it's associated with the span of the 58 // Re-wrap [value]'s contents so that it's associated with the span of the
59 // anchor rather than its original definition. 59 // anchor rather than its original definition.
60 if (value is YamlMap) return new YamlMap(value.nodes, anchored.span); 60 if (value is YamlMap) {
61 if (value is YamlList) return new YamlList(value.nodes, anchored.span); 61 return new YamlMap.internal(value.nodes, anchored.span);
62 assert(value is YamlScalar); 62 } else if (value is YamlList) {
63 return new YamlScalar(value.value, anchored.span); 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 }
64 } 68 }
65 69
66 /// Records that [value] is the Dart object representing [anchored]. 70 /// Records that [value] is the Dart object representing [anchored].
67 YamlNode setAnchor(Node anchored, YamlNode value) { 71 YamlNode setAnchor(Node anchored, YamlNode value) {
68 if (anchored.anchor == null) return value; 72 if (anchored.anchor == null) return value;
69 _anchors[anchored.anchor] = value; 73 _anchors[anchored.anchor] = value;
70 return value; 74 return value;
71 } 75 }
72 } 76 }
OLDNEW
« no previous file with comments | « pkg/yaml/CHANGELOG.md ('k') | pkg/yaml/lib/src/null_span.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698