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