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