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.yaml_node; | 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_span/source_span.dart'; |
11 | 11 |
12 import 'null_span.dart'; | 12 import 'null_span.dart'; |
13 import 'yaml_node_wrapper.dart'; | 13 import 'yaml_node_wrapper.dart'; |
14 | 14 |
15 /// An interface for parsed nodes from a YAML source tree. | 15 /// An interface for parsed nodes from a YAML source tree. |
16 /// | 16 /// |
17 /// [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 |
18 /// 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 |
19 /// [YamlNode]s regardless of how they're accessed. | 19 /// [YamlNode]s regardless of how they're accessed. |
20 /// | 20 /// |
21 /// 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 |
22 /// 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 |
23 /// [YamlMap.nodes] or [YamlList.nodes]. | 23 /// [YamlMap.nodes] or [YamlList.nodes]. |
24 abstract class YamlNode { | 24 abstract class YamlNode { |
25 /// The source span for this node. | 25 /// The source span for this node. |
26 /// | 26 /// |
27 /// [Span.getLocationMessage] can be used to produce a human-friendly message | 27 /// [SourceSpan.message] can be used to produce a human-friendly message about |
28 /// about this node. | 28 /// this node. |
29 Span get span; | 29 SourceSpan get span; |
30 | 30 |
31 /// The inner value of this node. | 31 /// The inner value of this node. |
32 /// | 32 /// |
33 /// 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 |
34 /// [YamlList], it will return [this], since they already implement [Map] and | 34 /// [YamlList], it will return [this], since they already implement [Map] and |
35 /// [List], respectively. | 35 /// [List], respectively. |
36 get value; | 36 get value; |
37 } | 37 } |
38 | 38 |
39 /// A read-only [Map] parsed from YAML. | 39 /// A read-only [Map] parsed from YAML. |
40 class YamlMap extends YamlNode with collection.MapMixin, UnmodifiableMapMixin { | 40 class YamlMap extends YamlNode with collection.MapMixin, UnmodifiableMapMixin { |
41 final Span span; | 41 final SourceSpan span; |
42 | 42 |
43 /// A view of [this] where the keys and values are guaranteed to be | 43 /// A view of [this] where the keys and values are guaranteed to be |
44 /// [YamlNode]s. | 44 /// [YamlNode]s. |
45 /// | 45 /// |
46 /// The key type is `dynamic` to allow values to be accessed using | 46 /// The key type is `dynamic` to allow values to be accessed using |
47 /// non-[YamlNode] keys, but [Map.keys] and [Map.forEach] will always expose | 47 /// non-[YamlNode] keys, but [Map.keys] and [Map.forEach] will always expose |
48 /// them as [YamlNode]s. For example, for `{"foo": [1, 2, 3]}` [nodes] will be | 48 /// them as [YamlNode]s. For example, for `{"foo": [1, 2, 3]}` [nodes] will be |
49 /// a map from a [YamlScalar] to a [YamlList], but since the key type is | 49 /// a map from a [YamlScalar] to a [YamlList], but since the key type is |
50 /// `dynamic` `map.nodes["foo"]` will still work. | 50 /// `dynamic` `map.nodes["foo"]` will still work. |
51 final Map<dynamic, YamlNode> nodes; | 51 final Map<dynamic, YamlNode> nodes; |
52 | 52 |
53 Map get value => this; | 53 Map get value => this; |
54 | 54 |
55 Iterable get keys => nodes.keys.map((node) => node.value); | 55 Iterable get keys => nodes.keys.map((node) => node.value); |
56 | 56 |
57 /// Creates an empty YamlMap. | 57 /// Creates an empty YamlMap. |
58 /// | 58 /// |
59 /// This map's [span] won't have useful location information. However, it will | 59 /// This map's [span] won't have useful location information. However, it will |
60 /// have a reasonable implementation of [Span.getLocationMessage]. If | 60 /// have a reasonable implementation of [SourceSpan.message]. If [sourceUrl] |
61 /// [sourceName] is passed, it's used as the [Span.sourceUrl]. | 61 /// is passed, it's used as the [SourceSpan.sourceUrl]. |
62 factory YamlMap({String sourceName}) => | 62 /// |
63 new YamlMapWrapper(const {}, sourceName); | 63 /// [sourceUrl] may be either a [String], a [Uri], or `null`. |
| 64 factory YamlMap({sourceUrl}) => |
| 65 new YamlMapWrapper(const {}, sourceUrl); |
64 | 66 |
65 /// Wraps a Dart map so that it can be accessed (recursively) like a | 67 /// Wraps a Dart map so that it can be accessed (recursively) like a |
66 /// [YamlMap]. | 68 /// [YamlMap]. |
67 /// | 69 /// |
68 /// Any [Span]s returned by this map or its children will be dummies without | 70 /// Any [SourceSpan]s returned by this map or its children will be dummies |
69 /// useful location information. However, they will have a reasonable | 71 /// without useful location information. However, they will have a reasonable |
70 /// implementation of [Span.getLocationMessage]. If [sourceName] is passed, | 72 /// implementation of [SourceSpan.getLocationMessage]. If [sourceUrl] is |
71 /// it's used as the [Span.sourceUrl]. | 73 /// passed, it's used as the [SourceSpan.sourceUrl]. |
72 factory YamlMap.wrap(Map dartMap, {String sourceName}) => | 74 /// |
73 new YamlMapWrapper(dartMap, sourceName); | 75 /// [sourceUrl] may be either a [String], a [Uri], or `null`. |
| 76 factory YamlMap.wrap(Map dartMap, {sourceUrl}) => |
| 77 new YamlMapWrapper(dartMap, sourceUrl); |
74 | 78 |
75 /// Users of the library should not use this constructor. | 79 /// Users of the library should not use this constructor. |
76 YamlMap.internal(Map<dynamic, YamlNode> nodes, this.span) | 80 YamlMap.internal(Map<dynamic, YamlNode> nodes, this.span) |
77 : nodes = new UnmodifiableMapView<dynamic, YamlNode>(nodes); | 81 : nodes = new UnmodifiableMapView<dynamic, YamlNode>(nodes); |
78 | 82 |
79 operator [](key) { | 83 operator [](key) { |
80 var node = nodes[key]; | 84 var node = nodes[key]; |
81 return node == null ? null : node.value; | 85 return node == null ? null : node.value; |
82 } | 86 } |
83 } | 87 } |
84 | 88 |
85 // TODO(nweiz): Use UnmodifiableListMixin when issue 18970 is fixed. | 89 // TODO(nweiz): Use UnmodifiableListMixin when issue 18970 is fixed. |
86 /// A read-only [List] parsed from YAML. | 90 /// A read-only [List] parsed from YAML. |
87 class YamlList extends YamlNode with collection.ListMixin { | 91 class YamlList extends YamlNode with collection.ListMixin { |
88 final Span span; | 92 final SourceSpan span; |
89 | 93 |
90 final List<YamlNode> nodes; | 94 final List<YamlNode> nodes; |
91 | 95 |
92 List get value => this; | 96 List get value => this; |
93 | 97 |
94 int get length => nodes.length; | 98 int get length => nodes.length; |
95 | 99 |
96 set length(int index) { | 100 set length(int index) { |
97 throw new UnsupportedError("Cannot modify an unmodifiable List"); | 101 throw new UnsupportedError("Cannot modify an unmodifiable List"); |
98 } | 102 } |
99 | 103 |
100 /// Creates an empty YamlList. | 104 /// Creates an empty YamlList. |
101 /// | 105 /// |
102 /// This list's [span] won't have useful location information. However, it | 106 /// This list's [span] won't have useful location information. However, it |
103 /// will have a reasonable implementation of [Span.getLocationMessage]. If | 107 /// will have a reasonable implementation of [SourceSpan.message]. If |
104 /// [sourceName] is passed, it's used as the [Span.sourceUrl]. | 108 /// [sourceUrl] is passed, it's used as the [SourceSpan.sourceUrl]. |
105 factory YamlList({String sourceName}) => | 109 /// |
106 new YamlListWrapper(const [], sourceName); | 110 /// [sourceUrl] may be either a [String], a [Uri], or `null`. |
| 111 factory YamlList({sourceUrl}) => |
| 112 new YamlListWrapper(const [], sourceUrl); |
107 | 113 |
108 /// Wraps a Dart list so that it can be accessed (recursively) like a | 114 /// Wraps a Dart list so that it can be accessed (recursively) like a |
109 /// [YamlList]. | 115 /// [YamlList]. |
110 /// | 116 /// |
111 /// Any [Span]s returned by this list or its children will be dummies without | 117 /// Any [SourceSpan]s returned by this list or its children will be dummies |
112 /// useful location information. However, they will have a reasonable | 118 /// without useful location information. However, they will have a reasonable |
113 /// implementation of [Span.getLocationMessage]. If [sourceName] is passed, | 119 /// implementation of [SourceSpan.getLocationMessage]. If [sourceUrl] is |
114 /// it's used as the [Span.sourceUrl]. | 120 /// passed, it's used as the [Span.sourceUrl]. |
115 factory YamlList.wrap(List dartList, {String sourceName}) => | 121 /// |
116 new YamlListWrapper(dartList, sourceName); | 122 /// [sourceUrl] may be either a [String], a [Uri], or `null`. |
| 123 factory YamlList.wrap(List dartList, {sourceUrl}) => |
| 124 new YamlListWrapper(dartList, sourceUrl); |
117 | 125 |
118 /// Users of the library should not use this constructor. | 126 /// Users of the library should not use this constructor. |
119 YamlList.internal(List<YamlNode> nodes, this.span) | 127 YamlList.internal(List<YamlNode> nodes, this.span) |
120 : nodes = new UnmodifiableListView<YamlNode>(nodes); | 128 : nodes = new UnmodifiableListView<YamlNode>(nodes); |
121 | 129 |
122 operator [](int index) => nodes[index].value; | 130 operator [](int index) => nodes[index].value; |
123 | 131 |
124 operator []=(int index, value) { | 132 operator []=(int index, value) { |
125 throw new UnsupportedError("Cannot modify an unmodifiable List"); | 133 throw new UnsupportedError("Cannot modify an unmodifiable List"); |
126 } | 134 } |
127 } | 135 } |
128 | 136 |
129 /// A wrapped scalar value parsed from YAML. | 137 /// A wrapped scalar value parsed from YAML. |
130 class YamlScalar extends YamlNode { | 138 class YamlScalar extends YamlNode { |
131 final Span span; | 139 final SourceSpan span; |
132 | 140 |
133 final value; | 141 final value; |
134 | 142 |
135 /// Wraps a Dart value in a [YamlScalar]. | 143 /// Wraps a Dart value in a [YamlScalar]. |
136 /// | 144 /// |
137 /// This scalar's [span] won't have useful location information. However, it | 145 /// This scalar's [span] won't have useful location information. However, it |
138 /// will have a reasonable implementation of [Span.getLocationMessage]. If | 146 /// will have a reasonable implementation of [SourceSpan.message]. If |
139 /// [sourceName] is passed, it's used as the [Span.sourceUrl]. | 147 /// [sourceUrl] is passed, it's used as the [Span.sourceUrl]. |
140 YamlScalar.wrap(this.value, {String sourceName}) | 148 /// |
141 : span = new NullSpan(sourceName); | 149 /// [sourceUrl] may be either a [String], a [Uri], or `null`. |
| 150 YamlScalar.wrap(this.value, {sourceUrl}) |
| 151 : span = new NullSpan(sourceUrl); |
142 | 152 |
143 /// Users of the library should not use this constructor. | 153 /// Users of the library should not use this constructor. |
144 YamlScalar.internal(this.value, this.span); | 154 YamlScalar.internal(this.value, this.span); |
145 | 155 |
146 String toString() => value.toString(); | 156 String toString() => value.toString(); |
147 } | 157 } |
OLD | NEW |