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

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

Issue 343063002: Add new constructors to YamlNode subclasses. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: improve DummySpan.getLocationString 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
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.map; 5 library yaml.yaml_node;
6 6
7 import 'dart:collection' as collection; 7 import 'dart:collection' as collection;
8 8
9 import 'package:collection/collection.dart'; 9 import 'package:collection/collection.dart';
10 import 'package:source_maps/source_maps.dart'; 10 import 'package:source_maps/source_maps.dart';
11 11
12 import 'dummy_span.dart';
13 import 'yaml_node_wrapper.dart';
14
12 /// An interface for parsed nodes from a YAML source tree. 15 /// An interface for parsed nodes from a YAML source tree.
13 /// 16 ///
14 /// [YamlMap]s and [YamlList]s implement this interface in addition to the 17 /// [YamlMap]s and [YamlList]s implement this interface in addition to the
15 /// normal [Map] and [List] interfaces, so any maps and lists will be 18 /// normal [Map] and [List] interfaces, so any maps and lists will be
16 /// [YamlNode]s regardless of how they're accessed. 19 /// [YamlNode]s regardless of how they're accessed.
17 /// 20 ///
18 /// Scalars values like strings and numbers, on the other hand, don't have this 21 /// Scalars values like strings and numbers, on the other hand, don't have this
19 /// interface by default. Instead, they can be accessed as [YamlScalar]s via 22 /// interface by default. Instead, they can be accessed as [YamlScalar]s via
20 /// [YamlMap.nodes] or [YamlList.nodes]. 23 /// [YamlMap.nodes] or [YamlList.nodes].
21 abstract class YamlNode { 24 abstract class YamlNode {
22 /// The source span for this node. 25 /// The source span for this node.
23 /// 26 ///
24 /// [Span.getLocationMessage] can be used to produce a human-friendly message 27 /// [Span.getLocationMessage] can be used to produce a human-friendly message
25 /// about this node. 28 /// about this node.
26 Span get span; 29 Span get span;
27 30
28 /// The inner value of this node. 31 /// The inner value of this node.
29 /// 32 ///
30 /// For [YamlScalar]s, this will return the wrapped value. For [YamlMap] and 33 /// For [YamlScalar]s, this will return the wrapped value. For [YamlMap] and
31 /// [YamlList], it will return [this], since they already implement [Map] and 34 /// [YamlList], it will return [this], since they already implement [Map] and
32 /// [List], respectively. 35 /// [List], respectively.
33 get value; 36 get value;
34 } 37 }
35 38
36 /// A read-only [Map] parsed from YAML. 39 /// A read-only [Map] parsed from YAML.
37 class YamlMap extends YamlNode with collection.MapMixin, UnmodifiableMapMixin { 40 class YamlMap extends YamlNode with collection.MapMixin, UnmodifiableMapMixin {
38 final Span span; 41 final Span span;
39 42
43 /// A view of [this] where the keys and values are guaranteed to be
44 /// [YamlNode]s.
45 ///
46 /// The key type is `dynamic` to allow keys to be accessed using
47 /// non-[YamlNode] values, but [Map.keys] and [Map.forEach] will always expose
Bob Nystrom 2014/06/21 00:14:15 I didn't follow what "allow keys to be accessed us
nweiz 2014/06/23 22:23:06 Done.
48 /// them as [YamlNode]s.
40 final Map<dynamic, YamlNode> nodes; 49 final Map<dynamic, YamlNode> nodes;
41 50
42 Map get value => this; 51 Map get value => this;
43 52
44 Iterable get keys => nodes.keys.map((node) => node.value); 53 Iterable get keys => nodes.keys.map((node) => node.value);
45 54
46 /// Users of the library should not construct [YamlMap]s manually. 55 /// Creates an empty YamlMap.
47 YamlMap(Map<dynamic, YamlNode> nodes, this.span) 56 ///
57 /// This map's [span] will be a dummy without useful location information.
58 /// However, it will have a reasonable implementation of
59 /// [Span.getLocationMessage]. If [sourceName] is passed, it's used as
Bob Nystrom 2014/06/21 00:14:15 "as" -> "as the"
nweiz 2014/06/23 22:23:06 Done.
60 /// [Span.sourceUrl].
61 factory YamlMap({String sourceName}) =>
62 new YamlMapWrapper(const {}, sourceName);
63
64 /// Wraps a Dart map so that it can be accessed (recursively) like a
65 /// [YamlMap].
66 ///
67 /// Any [Span]s returned by this map or its children will be dummies without
68 /// useful location information. However, they will have a reasonable
69 /// implementation of [Span.getLocationMessage]. If [sourceName] is passed,
70 /// it's used as [Span.sourceUrl].
71 factory YamlMap.wrap(Map dartMap, {String sourceName}) =>
72 new YamlMapWrapper(dartMap, sourceName);
73
74 /// Users of the library should not use this constructor.
75 YamlMap.internal(Map<dynamic, YamlNode> nodes, this.span)
48 : nodes = new UnmodifiableMapView<dynamic, YamlNode>(nodes); 76 : nodes = new UnmodifiableMapView<dynamic, YamlNode>(nodes);
49 77
50 operator [](key) { 78 operator [](key) {
51 var node = nodes[key]; 79 var node = nodes[key];
52 return node == null ? null : node.value; 80 return node == null ? null : node.value;
53 } 81 }
54 } 82 }
55 83
56 // TODO(nweiz): Use UnmodifiableListMixin when issue 18970 is fixed. 84 // TODO(nweiz): Use UnmodifiableListMixin when issue 18970 is fixed.
57 /// A read-only [List] parsed from YAML. 85 /// A read-only [List] parsed from YAML.
58 class YamlList extends YamlNode with collection.ListMixin { 86 class YamlList extends YamlNode with collection.ListMixin {
59 final Span span; 87 final Span span;
60 88
61 final List<YamlNode> nodes; 89 final List<YamlNode> nodes;
62 90
63 List get value => this; 91 List get value => this;
64 92
65 int get length => nodes.length; 93 int get length => nodes.length;
66 94
67 set length(int index) { 95 set length(int index) {
68 throw new UnsupportedError("Cannot modify an unmodifiable List"); 96 throw new UnsupportedError("Cannot modify an unmodifiable List");
69 } 97 }
70 98
71 /// Users of the library should not construct [YamlList]s manually. 99 /// Creates an empty YamlList.
72 YamlList(List<YamlNode> nodes, this.span) 100 ///
101 /// This list's [span] will be a dummy without useful location information.
102 /// However, it will have a reasonable implementation of
103 /// [Span.getLocationMessage]. If [sourceName] is passed, it's used as
104 /// [Span.sourceUrl].
105 factory YamlList({String sourceName}) =>
106 new YamlListWrapper(const [], sourceName);
107
108 /// Wraps a Dart list so that it can be accessed (recursively) like a
109 /// [YamlList].
110 ///
111 /// Any [Span]s returned by this list or its children will be dummies without
112 /// useful location information. However, they will have a reasonable
113 /// implementation of [Span.getLocationMessage]. If [sourceName] is passed,
114 /// it's used as [Span.sourceUrl].
115 factory YamlList.wrap(List dartList, {String sourceName}) =>
116 new YamlListWrapper(dartList, sourceName);
117
118 /// Users of the library should not use this constructor.
Bob Nystrom 2014/06/21 00:14:15 For cases like this, I like the pattern of making
nweiz 2014/06/23 22:23:06 I disagree. That adds a lot of overhead to interna
Bob Nystrom 2014/06/23 23:11:44 I don't look at it as preventing the user from doi
nweiz 2014/06/24 00:03:09 It would be nice to be able to tell docgen "don't
119 YamlList.internal(List<YamlNode> nodes, this.span)
73 : nodes = new UnmodifiableListView<YamlNode>(nodes); 120 : nodes = new UnmodifiableListView<YamlNode>(nodes);
74 121
75 operator [](int index) => nodes[index].value; 122 operator [](int index) => nodes[index].value;
76 123
77 operator []=(int index, value) { 124 operator []=(int index, value) {
78 throw new UnsupportedError("Cannot modify an unmodifiable List"); 125 throw new UnsupportedError("Cannot modify an unmodifiable List");
79 } 126 }
80 } 127 }
81 128
82 /// A wrapped scalar value parsed from YAML. 129 /// A wrapped scalar value parsed from YAML.
83 class YamlScalar extends YamlNode { 130 class YamlScalar extends YamlNode {
84 final Span span; 131 final Span span;
85 132
86 final value; 133 final value;
87 134
88 /// Users of the library should not construct [YamlScalar]s manually. 135 /// Wraps a Dart value in a [YamlScalar].
89 YamlScalar(this.value, this.span); 136 ///
137 /// This scalar's [span] will be a dummy without useful location information.
138 /// However, it will have a reasonable implementation of
139 /// [Span.getLocationMessage]. If [sourceName] is passed, it's used as
140 /// [Span.sourceUrl].
141 YamlScalar.wrap(this.value, {String sourceName})
142 : span = new DummySpan(sourceName);
143
144 /// Users of the library should not use this constructor.
145 YamlScalar.internal(this.value, this.span);
90 146
91 String toString() => value.toString(); 147 String toString() => value.toString();
92 } 148 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698