Chromium Code Reviews| Index: pkg/yaml/lib/src/yaml_node.dart |
| diff --git a/pkg/yaml/lib/src/yaml_node.dart b/pkg/yaml/lib/src/yaml_node.dart |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..fec0adaa7d024c80997dd7c3d95430ba880485f2 |
| --- /dev/null |
| +++ b/pkg/yaml/lib/src/yaml_node.dart |
| @@ -0,0 +1,89 @@ |
| +// Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file |
| +// for details. All rights reserved. Use of this source code is governed by a |
| +// BSD-style license that can be found in the LICENSE file. |
| + |
| +library yaml.map; |
| + |
| +import 'dart:collection' as collection; |
| + |
| +import 'package:collection/collection.dart'; |
| +import 'package:source_maps/source_maps.dart'; |
| + |
| +/// An interface for parsed nodes from a YAML source tree. |
| +/// |
| +/// [YamlMap]s and [YamlList]s implement this interface in addition to the |
| +/// normal [Map] and [List] interfaces, so any maps and lists will be |
| +/// [YamlNode]s regardless of how they're accessed. |
| +/// |
| +/// Scalars values like strings and numbers, on the other hand, don't have this |
| +/// interface by default. Instead, they can be accessed as [YamlScalar]s via |
| +/// [YamlMap.nodes] or [YamlList.nodes]. |
| +abstract class YamlNode { |
| + /// The source span for this node. |
| + /// |
| + /// [Span.getLocationMessage] can be used to produce a human-friendly message |
| + /// about this node. |
| + Span get span; |
| + |
| + /// The inner value of this node. |
| + /// |
| + /// For [YamlScalar]s, this will return the wrapped value. For [YamlMap] and |
| + /// [YamlList], it will return [this], since they already implement [Map] and |
| + /// [List], respectively. |
| + get value; |
| +} |
| + |
| +/// A read-only [Map] parsed from YAML. |
| +class YamlMap extends YamlNode with collection.MapMixin, UnmodifiableMapMixin { |
| + final Span span; |
| + |
| + final Map<dynamic, YamlNode> nodes; |
| + |
| + Map get value => this; |
| + |
| + Iterable get keys => nodes.keys.map((node) => node.value); |
| + |
| + YamlMap(Map<dynamic, YamlNode> nodes, this.span) |
| + : nodes = new UnmodifiableMapView<dynamic, YamlNode>(nodes); |
| + |
| + operator [](key) { |
| + var node = nodes[key]; |
| + return node == null ? null : node.value; |
| + } |
| +} |
| + |
| +// TODO(nweiz): Use UnmodifiableListMixin when issue 18970 is fixed. |
| +/// A read-only [List] parsed from YAML. |
| +class YamlList extends YamlNode with collection.ListMixin { |
| + final Span span; |
| + |
| + final List<YamlNode> nodes; |
| + |
| + List get value => this; |
| + |
| + int get length => nodes.length; |
| + |
| + set length(int index) { |
| + throw new UnsupportedError("Cannot modify an unmodifiable List"); |
| + } |
| + |
| + YamlList(List<YamlNode> nodes, this.span) |
| + : nodes = new UnmodifiableListView<YamlNode>(nodes); |
| + |
| + operator [](int index) => nodes[index].value; |
| + |
| + operator []=(int index, value) { |
|
Bob Nystrom
2014/06/03 00:33:14
Do you need to do the same thing with YamlMap? Wha
nweiz
2014/06/03 02:03:21
All the mutating methods of List are overridden he
|
| + throw new UnsupportedError("Cannot modify an unmodifiable List"); |
| + } |
| +} |
| + |
| +/// A wrapped scalar value parsed from YAML. |
| +class YamlScalar extends YamlNode { |
| + final Span span; |
| + |
| + final value; |
| + |
| + YamlScalar(this.value, this.span); |
| + |
| + String toString() => value.toString(); |
| +} |