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.map; |
| 6 |
| 7 import 'dart:collection' as collection; |
| 8 |
| 9 import 'package:collection/collection.dart'; |
| 10 import 'package:source_maps/source_maps.dart'; |
| 11 |
| 12 /// An interface for parsed nodes from a YAML source tree. |
| 13 /// |
| 14 /// [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 |
| 16 /// [YamlNode]s regardless of how they're accessed. |
| 17 /// |
| 18 /// 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 |
| 20 /// [YamlMap.nodes] or [YamlList.nodes]. |
| 21 abstract class YamlNode { |
| 22 /// The source span for this node. |
| 23 /// |
| 24 /// [Span.getLocationMessage] can be used to produce a human-friendly message |
| 25 /// about this node. |
| 26 Span get span; |
| 27 |
| 28 /// The inner value of this node. |
| 29 /// |
| 30 /// For [YamlScalar]s, this will return the wrapped value. For [YamlMap] and |
| 31 /// [YamlList], it will return [this], since they already implement [Map] and |
| 32 /// [List], respectively. |
| 33 get value; |
| 34 } |
| 35 |
| 36 /// A read-only [Map] parsed from YAML. |
| 37 class YamlMap extends YamlNode with collection.MapMixin, UnmodifiableMapMixin { |
| 38 final Span span; |
| 39 |
| 40 final Map<dynamic, YamlNode> nodes; |
| 41 |
| 42 Map get value => this; |
| 43 |
| 44 Iterable get keys => nodes.keys.map((node) => node.value); |
| 45 |
| 46 /// Users of the library should not construct [YamlMap]s manually. |
| 47 YamlMap(Map<dynamic, YamlNode> nodes, this.span) |
| 48 : nodes = new UnmodifiableMapView<dynamic, YamlNode>(nodes); |
| 49 |
| 50 operator [](key) { |
| 51 var node = nodes[key]; |
| 52 return node == null ? null : node.value; |
| 53 } |
| 54 } |
| 55 |
| 56 // TODO(nweiz): Use UnmodifiableListMixin when issue 18970 is fixed. |
| 57 /// A read-only [List] parsed from YAML. |
| 58 class YamlList extends YamlNode with collection.ListMixin { |
| 59 final Span span; |
| 60 |
| 61 final List<YamlNode> nodes; |
| 62 |
| 63 List get value => this; |
| 64 |
| 65 int get length => nodes.length; |
| 66 |
| 67 set length(int index) { |
| 68 throw new UnsupportedError("Cannot modify an unmodifiable List"); |
| 69 } |
| 70 |
| 71 /// Users of the library should not construct [YamlList]s manually. |
| 72 YamlList(List<YamlNode> nodes, this.span) |
| 73 : nodes = new UnmodifiableListView<YamlNode>(nodes); |
| 74 |
| 75 operator [](int index) => nodes[index].value; |
| 76 |
| 77 operator []=(int index, value) { |
| 78 throw new UnsupportedError("Cannot modify an unmodifiable List"); |
| 79 } |
| 80 } |
| 81 |
| 82 /// A wrapped scalar value parsed from YAML. |
| 83 class YamlScalar extends YamlNode { |
| 84 final Span span; |
| 85 |
| 86 final value; |
| 87 |
| 88 /// Users of the library should not construct [YamlScalar]s manually. |
| 89 YamlScalar(this.value, this.span); |
| 90 |
| 91 String toString() => value.toString(); |
| 92 } |
OLD | NEW |