Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(51)

Side by Side Diff: pkg/yaml/lib/src/yaml_node.dart

Issue 689513002: Rewrite the pkg/yaml parser. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Code review changes Created 6 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « pkg/yaml/lib/src/yaml_document.dart ('k') | pkg/yaml/lib/src/yaml_node_wrapper.dart » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file 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 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. 3 // BSD-style license that can be found in the LICENSE file.
4 4
5 library yaml.yaml_node; 5 library yaml.yaml_node;
6 6
7 import 'dart:collection' as collection; 7 import 'dart:collection' as collection;
8 8
9 import 'package:collection/collection.dart'; 9 import 'package:collection/collection.dart';
10 import 'package:source_span/source_span.dart'; 10 import 'package:source_span/source_span.dart';
11 11
12 import 'null_span.dart'; 12 import 'null_span.dart';
13 import 'style.dart';
13 import 'yaml_node_wrapper.dart'; 14 import 'yaml_node_wrapper.dart';
14 15
15 /// An interface for parsed nodes from a YAML source tree. 16 /// An interface for parsed nodes from a YAML source tree.
16 /// 17 ///
17 /// [YamlMap]s and [YamlList]s implement this interface in addition to the 18 /// [YamlMap]s and [YamlList]s implement this interface in addition to the
18 /// normal [Map] and [List] interfaces, so any maps and lists will be 19 /// normal [Map] and [List] interfaces, so any maps and lists will be
19 /// [YamlNode]s regardless of how they're accessed. 20 /// [YamlNode]s regardless of how they're accessed.
20 /// 21 ///
21 /// Scalars values like strings and numbers, on the other hand, don't have this 22 /// Scalars values like strings and numbers, on the other hand, don't have this
22 /// interface by default. Instead, they can be accessed as [YamlScalar]s via 23 /// interface by default. Instead, they can be accessed as [YamlScalar]s via
23 /// [YamlMap.nodes] or [YamlList.nodes]. 24 /// [YamlMap.nodes] or [YamlList.nodes].
24 abstract class YamlNode { 25 abstract class YamlNode {
25 /// The source span for this node. 26 /// The source span for this node.
26 /// 27 ///
27 /// [SourceSpan.message] can be used to produce a human-friendly message about 28 /// [SourceSpan.message] can be used to produce a human-friendly message about
28 /// this node. 29 /// this node.
29 SourceSpan get span; 30 SourceSpan get span => _span;
31
32 SourceSpan _span;
30 33
31 /// The inner value of this node. 34 /// The inner value of this node.
32 /// 35 ///
33 /// For [YamlScalar]s, this will return the wrapped value. For [YamlMap] and 36 /// For [YamlScalar]s, this will return the wrapped value. For [YamlMap] and
34 /// [YamlList], it will return [this], since they already implement [Map] and 37 /// [YamlList], it will return [this], since they already implement [Map] and
35 /// [List], respectively. 38 /// [List], respectively.
36 get value; 39 get value;
37 } 40 }
38 41
39 /// A read-only [Map] parsed from YAML. 42 /// A read-only [Map] parsed from YAML.
40 class YamlMap extends YamlNode with collection.MapMixin, UnmodifiableMapMixin { 43 class YamlMap extends YamlNode with collection.MapMixin, UnmodifiableMapMixin {
41 final SourceSpan span;
42
43 /// A view of [this] where the keys and values are guaranteed to be 44 /// A view of [this] where the keys and values are guaranteed to be
44 /// [YamlNode]s. 45 /// [YamlNode]s.
45 /// 46 ///
46 /// The key type is `dynamic` to allow values to be accessed using 47 /// The key type is `dynamic` to allow values to be accessed using
47 /// non-[YamlNode] keys, but [Map.keys] and [Map.forEach] will always expose 48 /// non-[YamlNode] keys, but [Map.keys] and [Map.forEach] will always expose
48 /// them as [YamlNode]s. For example, for `{"foo": [1, 2, 3]}` [nodes] will be 49 /// them as [YamlNode]s. For example, for `{"foo": [1, 2, 3]}` [nodes] will be
49 /// a map from a [YamlScalar] to a [YamlList], but since the key type is 50 /// a map from a [YamlScalar] to a [YamlList], but since the key type is
50 /// `dynamic` `map.nodes["foo"]` will still work. 51 /// `dynamic` `map.nodes["foo"]` will still work.
51 final Map<dynamic, YamlNode> nodes; 52 final Map<dynamic, YamlNode> nodes;
52 53
54 /// The style used for the map in the original document.
55 final CollectionStyle style;
56
53 Map get value => this; 57 Map get value => this;
54 58
55 Iterable get keys => nodes.keys.map((node) => node.value); 59 Iterable get keys => nodes.keys.map((node) => node.value);
56 60
57 /// Creates an empty YamlMap. 61 /// Creates an empty YamlMap.
58 /// 62 ///
59 /// This map's [span] won't have useful location information. However, it will 63 /// This map's [span] won't have useful location information. However, it will
60 /// have a reasonable implementation of [SourceSpan.message]. If [sourceUrl] 64 /// have a reasonable implementation of [SourceSpan.message]. If [sourceUrl]
61 /// is passed, it's used as the [SourceSpan.sourceUrl]. 65 /// is passed, it's used as the [SourceSpan.sourceUrl].
62 /// 66 ///
63 /// [sourceUrl] may be either a [String], a [Uri], or `null`. 67 /// [sourceUrl] may be either a [String], a [Uri], or `null`.
64 factory YamlMap({sourceUrl}) => 68 factory YamlMap({sourceUrl}) =>
65 new YamlMapWrapper(const {}, sourceUrl); 69 new YamlMapWrapper(const {}, sourceUrl);
66 70
67 /// Wraps a Dart map so that it can be accessed (recursively) like a 71 /// Wraps a Dart map so that it can be accessed (recursively) like a
68 /// [YamlMap]. 72 /// [YamlMap].
69 /// 73 ///
70 /// Any [SourceSpan]s returned by this map or its children will be dummies 74 /// Any [SourceSpan]s returned by this map or its children will be dummies
71 /// without useful location information. However, they will have a reasonable 75 /// without useful location information. However, they will have a reasonable
72 /// implementation of [SourceSpan.getLocationMessage]. If [sourceUrl] is 76 /// implementation of [SourceSpan.getLocationMessage]. If [sourceUrl] is
73 /// passed, it's used as the [SourceSpan.sourceUrl]. 77 /// passed, it's used as the [SourceSpan.sourceUrl].
74 /// 78 ///
75 /// [sourceUrl] may be either a [String], a [Uri], or `null`. 79 /// [sourceUrl] may be either a [String], a [Uri], or `null`.
76 factory YamlMap.wrap(Map dartMap, {sourceUrl}) => 80 factory YamlMap.wrap(Map dartMap, {sourceUrl}) =>
77 new YamlMapWrapper(dartMap, sourceUrl); 81 new YamlMapWrapper(dartMap, sourceUrl);
78 82
79 /// Users of the library should not use this constructor. 83 /// Users of the library should not use this constructor.
80 YamlMap.internal(Map<dynamic, YamlNode> nodes, this.span) 84 YamlMap.internal(Map<dynamic, YamlNode> nodes, SourceSpan span, this.style)
81 : nodes = new UnmodifiableMapView<dynamic, YamlNode>(nodes); 85 : nodes = new UnmodifiableMapView<dynamic, YamlNode>(nodes) {
86 _span = span;
87 }
82 88
83 operator [](key) { 89 operator [](key) {
84 var node = nodes[key]; 90 var node = nodes[key];
85 return node == null ? null : node.value; 91 return node == null ? null : node.value;
86 } 92 }
87 } 93 }
88 94
89 // TODO(nweiz): Use UnmodifiableListMixin when issue 18970 is fixed. 95 // TODO(nweiz): Use UnmodifiableListMixin when issue 18970 is fixed.
90 /// A read-only [List] parsed from YAML. 96 /// A read-only [List] parsed from YAML.
91 class YamlList extends YamlNode with collection.ListMixin { 97 class YamlList extends YamlNode with collection.ListMixin {
92 final SourceSpan span; 98 final List<YamlNode> nodes;
93 99
94 final List<YamlNode> nodes; 100 /// The style used for the list in the original document.
101 final CollectionStyle style;
95 102
96 List get value => this; 103 List get value => this;
97 104
98 int get length => nodes.length; 105 int get length => nodes.length;
99 106
100 set length(int index) { 107 set length(int index) {
101 throw new UnsupportedError("Cannot modify an unmodifiable List"); 108 throw new UnsupportedError("Cannot modify an unmodifiable List");
102 } 109 }
103 110
104 /// Creates an empty YamlList. 111 /// Creates an empty YamlList.
(...skipping 12 matching lines...) Expand all
117 /// Any [SourceSpan]s returned by this list or its children will be dummies 124 /// Any [SourceSpan]s returned by this list or its children will be dummies
118 /// without useful location information. However, they will have a reasonable 125 /// without useful location information. However, they will have a reasonable
119 /// implementation of [SourceSpan.getLocationMessage]. If [sourceUrl] is 126 /// implementation of [SourceSpan.getLocationMessage]. If [sourceUrl] is
120 /// passed, it's used as the [SourceSpan.sourceUrl]. 127 /// passed, it's used as the [SourceSpan.sourceUrl].
121 /// 128 ///
122 /// [sourceUrl] may be either a [String], a [Uri], or `null`. 129 /// [sourceUrl] may be either a [String], a [Uri], or `null`.
123 factory YamlList.wrap(List dartList, {sourceUrl}) => 130 factory YamlList.wrap(List dartList, {sourceUrl}) =>
124 new YamlListWrapper(dartList, sourceUrl); 131 new YamlListWrapper(dartList, sourceUrl);
125 132
126 /// Users of the library should not use this constructor. 133 /// Users of the library should not use this constructor.
127 YamlList.internal(List<YamlNode> nodes, this.span) 134 YamlList.internal(List<YamlNode> nodes, SourceSpan span, this.style)
128 : nodes = new UnmodifiableListView<YamlNode>(nodes); 135 : nodes = new UnmodifiableListView<YamlNode>(nodes) {
136 _span = span;
137 }
129 138
130 operator [](int index) => nodes[index].value; 139 operator [](int index) => nodes[index].value;
131 140
132 operator []=(int index, value) { 141 operator []=(int index, value) {
133 throw new UnsupportedError("Cannot modify an unmodifiable List"); 142 throw new UnsupportedError("Cannot modify an unmodifiable List");
134 } 143 }
135 } 144 }
136 145
137 /// A wrapped scalar value parsed from YAML. 146 /// A wrapped scalar value parsed from YAML.
138 class YamlScalar extends YamlNode { 147 class YamlScalar extends YamlNode {
139 final SourceSpan span; 148 final value;
140 149
141 final value; 150 /// The style used for the scalar in the original document.
151 final ScalarStyle style;
142 152
143 /// Wraps a Dart value in a [YamlScalar]. 153 /// Wraps a Dart value in a [YamlScalar].
144 /// 154 ///
145 /// This scalar's [span] won't have useful location information. However, it 155 /// This scalar's [span] won't have useful location information. However, it
146 /// will have a reasonable implementation of [SourceSpan.message]. If 156 /// will have a reasonable implementation of [SourceSpan.message]. If
147 /// [sourceUrl] is passed, it's used as the [SourceSpan.sourceUrl]. 157 /// [sourceUrl] is passed, it's used as the [SourceSpan.sourceUrl].
148 /// 158 ///
149 /// [sourceUrl] may be either a [String], a [Uri], or `null`. 159 /// [sourceUrl] may be either a [String], a [Uri], or `null`.
150 YamlScalar.wrap(this.value, {sourceUrl}) 160 YamlScalar.wrap(this.value, {sourceUrl})
151 : span = new NullSpan(sourceUrl); 161 : style = ScalarStyle.ANY {
162 _span = new NullSpan(sourceUrl);
163 }
152 164
153 /// Users of the library should not use this constructor. 165 /// Users of the library should not use this constructor.
154 YamlScalar.internal(this.value, this.span); 166 YamlScalar.internal(this.value, SourceSpan span, this.style) {
167 _span = span;
168 }
155 169
156 String toString() => value.toString(); 170 String toString() => value.toString();
157 } 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 }
OLDNEW
« no previous file with comments | « pkg/yaml/lib/src/yaml_document.dart ('k') | pkg/yaml/lib/src/yaml_node_wrapper.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698