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 YamlMap(Map<dynamic, YamlNode> nodes, this.span) | |
47 : nodes = new UnmodifiableMapView<dynamic, YamlNode>(nodes); | |
48 | |
49 operator [](key) { | |
50 var node = nodes[key]; | |
51 return node == null ? null : node.value; | |
52 } | |
53 } | |
54 | |
55 // TODO(nweiz): Use UnmodifiableListMixin when issue 18970 is fixed. | |
56 /// A read-only [List] parsed from YAML. | |
57 class YamlList extends YamlNode with collection.ListMixin { | |
58 final Span span; | |
59 | |
60 final List<YamlNode> nodes; | |
61 | |
62 List get value => this; | |
63 | |
64 int get length => nodes.length; | |
65 | |
66 set length(int index) { | |
67 throw new UnsupportedError("Cannot modify an unmodifiable List"); | |
68 } | |
69 | |
70 YamlList(List<YamlNode> nodes, this.span) | |
71 : nodes = new UnmodifiableListView<YamlNode>(nodes); | |
72 | |
73 operator [](int index) => nodes[index].value; | |
74 | |
75 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
| |
76 throw new UnsupportedError("Cannot modify an unmodifiable List"); | |
77 } | |
78 } | |
79 | |
80 /// A wrapped scalar value parsed from YAML. | |
81 class YamlScalar extends YamlNode { | |
82 final Span span; | |
83 | |
84 final value; | |
85 | |
86 YamlScalar(this.value, this.span); | |
87 | |
88 String toString() => value.toString(); | |
89 } | |
OLD | NEW |