OLD | NEW |
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 // This file contains the node classes for the internal representations of YAML | 5 // This file contains the node classes for the internal representations of YAML |
6 // documents. These nodes are used for both the serialization tree and the | 6 // documents. These nodes are used for both the serialization tree and the |
7 // representation graph. | 7 // representation graph. |
8 | 8 |
9 /** A tag that indicates the type of a YAML node. */ | 9 /// A tag that indicates the type of a YAML node. |
10 class _Tag { | 10 class _Tag { |
11 // TODO(nweiz): it would better match the semantics of the spec if there were | 11 // TODO(nweiz): it would better match the semantics of the spec if there were |
12 // a singleton instance of this class for each tag. | 12 // a singleton instance of this class for each tag. |
13 | 13 |
14 static const SCALAR_KIND = 0; | 14 static const SCALAR_KIND = 0; |
15 static const SEQUENCE_KIND = 1; | 15 static const SEQUENCE_KIND = 1; |
16 static const MAPPING_KIND = 2; | 16 static const MAPPING_KIND = 2; |
17 | 17 |
18 static const String YAML_URI_PREFIX = 'tag:yaml.org,2002:'; | 18 static const String YAML_URI_PREFIX = 'tag:yaml.org,2002:'; |
19 | 19 |
20 /** The name of the tag, either a URI or a local tag beginning with "!". */ | 20 /// The name of the tag, either a URI or a local tag beginning with "!". |
21 final String name; | 21 final String name; |
22 | 22 |
23 /** The kind of the tag: SCALAR_KIND, SEQUENCE_KIND, or MAPPING_KIND. */ | 23 /// The kind of the tag: SCALAR_KIND, SEQUENCE_KIND, or MAPPING_KIND. |
24 final int kind; | 24 final int kind; |
25 | 25 |
26 _Tag(this.name, this.kind); | 26 _Tag(this.name, this.kind); |
27 | 27 |
28 _Tag.scalar(String name) : this(name, SCALAR_KIND); | 28 _Tag.scalar(String name) : this(name, SCALAR_KIND); |
29 _Tag.sequence(String name) : this(name, SEQUENCE_KIND); | 29 _Tag.sequence(String name) : this(name, SEQUENCE_KIND); |
30 _Tag.mapping(String name) : this(name, MAPPING_KIND); | 30 _Tag.mapping(String name) : this(name, MAPPING_KIND); |
31 | 31 |
32 /** Returns the standard YAML tag URI for [type]. */ | 32 /// Returns the standard YAML tag URI for [type]. |
33 static String yaml(String type) => "tag:yaml.org,2002:$type"; | 33 static String yaml(String type) => "tag:yaml.org,2002:$type"; |
34 | 34 |
35 /** Two tags are equal if their URIs are equal. */ | 35 /// Two tags are equal if their URIs are equal. |
36 operator ==(other) { | 36 operator ==(other) { |
37 if (other is! _Tag) return false; | 37 if (other is! _Tag) return false; |
38 return name == other.name; | 38 return name == other.name; |
39 } | 39 } |
40 | 40 |
41 String toString() { | 41 String toString() { |
42 if (name.startsWith(YAML_URI_PREFIX)) { | 42 if (name.startsWith(YAML_URI_PREFIX)) { |
43 return '!!${name.substring(YAML_URI_PREFIX.length)}'; | 43 return '!!${name.substring(YAML_URI_PREFIX.length)}'; |
44 } else { | 44 } else { |
45 return '!<$name>'; | 45 return '!<$name>'; |
46 } | 46 } |
47 } | 47 } |
48 | 48 |
49 int get hashCode => name.hashCode; | 49 int get hashCode => name.hashCode; |
50 } | 50 } |
51 | 51 |
52 /** The abstract class for YAML nodes. */ | 52 /// The abstract class for YAML nodes. |
53 abstract class _Node { | 53 abstract class _Node { |
54 /** Every YAML node has a tag that describes its type. */ | 54 /// Every YAML node has a tag that describes its type. |
55 _Tag tag; | 55 _Tag tag; |
56 | 56 |
57 /** Any YAML node can have an anchor associated with it. */ | 57 /// Any YAML node can have an anchor associated with it. |
58 String anchor; | 58 String anchor; |
59 | 59 |
60 _Node(this.tag, [this.anchor]); | 60 _Node(this.tag, [this.anchor]); |
61 | 61 |
62 bool operator ==(other) { | 62 bool operator ==(other) { |
63 if (other is! _Node) return false; | 63 if (other is! _Node) return false; |
64 return tag == other.tag; | 64 return tag == other.tag; |
65 } | 65 } |
66 | 66 |
67 int get hashCode => _hashCode([tag, anchor]); | 67 int get hashCode => _hashCode([tag, anchor]); |
68 | 68 |
69 visit(_Visitor v); | 69 visit(_Visitor v); |
70 } | 70 } |
71 | 71 |
72 /** A sequence node represents an ordered list of nodes. */ | 72 /// A sequence node represents an ordered list of nodes. |
73 class _SequenceNode extends _Node { | 73 class _SequenceNode extends _Node { |
74 /** The nodes in the sequence. */ | 74 /// The nodes in the sequence. |
75 List<_Node> content; | 75 List<_Node> content; |
76 | 76 |
77 _SequenceNode(String tagName, this.content) | 77 _SequenceNode(String tagName, this.content) |
78 : super(new _Tag.sequence(tagName)); | 78 : super(new _Tag.sequence(tagName)); |
79 | 79 |
80 /** Two sequences are equal if their tags and contents are equal. */ | 80 /// Two sequences are equal if their tags and contents are equal. |
81 bool operator ==(other) { | 81 bool operator ==(other) { |
82 // Should be super != other; bug 2554 | 82 // Should be super != other; bug 2554 |
83 if (!(super == other) || other is! _SequenceNode) return false; | 83 if (!(super == other) || other is! _SequenceNode) return false; |
84 if (content.length != other.content.length) return false; | 84 if (content.length != other.content.length) return false; |
85 for (var i = 0; i < content.length; i++) { | 85 for (var i = 0; i < content.length; i++) { |
86 if (content[i] != other.content[i]) return false; | 86 if (content[i] != other.content[i]) return false; |
87 } | 87 } |
88 return true; | 88 return true; |
89 } | 89 } |
90 | 90 |
91 String toString() => '$tag [${Strings.join(content.map((e) => '$e'), ', ')}]'; | 91 String toString() => '$tag [${Strings.join(content.map((e) => '$e'), ', ')}]'; |
92 | 92 |
93 int get hashCode => super.hashCode ^ _hashCode(content); | 93 int get hashCode => super.hashCode ^ _hashCode(content); |
94 | 94 |
95 visit(_Visitor v) => v.visitSequence(this); | 95 visit(_Visitor v) => v.visitSequence(this); |
96 } | 96 } |
97 | 97 |
98 /** An alias node is a reference to an anchor. */ | 98 /// An alias node is a reference to an anchor. |
99 class _AliasNode extends _Node { | 99 class _AliasNode extends _Node { |
100 _AliasNode(String anchor) : super(new _Tag.scalar(_Tag.yaml("str")), anchor); | 100 _AliasNode(String anchor) : super(new _Tag.scalar(_Tag.yaml("str")), anchor); |
101 | 101 |
102 visit(_Visitor v) => v.visitAlias(this); | 102 visit(_Visitor v) => v.visitAlias(this); |
103 } | 103 } |
104 | 104 |
105 /** A scalar node represents all YAML nodes that have a single value. */ | 105 /// A scalar node represents all YAML nodes that have a single value. |
106 class _ScalarNode extends _Node { | 106 class _ScalarNode extends _Node { |
107 /** The string value of the scalar node, if it was created by the parser. */ | 107 /// The string value of the scalar node, if it was created by the parser. |
108 final String _content; | 108 final String _content; |
109 | 109 |
110 /** The Dart value of the scalar node, if it was created by the composer. */ | 110 /// The Dart value of the scalar node, if it was created by the composer. |
111 final value; | 111 final value; |
112 | 112 |
113 /** | 113 /// Creates a new Scalar node. |
114 * Creates a new Scalar node. | 114 /// |
115 * | 115 /// Exactly one of [content] and [value] should be specified. Content should |
116 * Exactly one of [content] and [value] should be specified. Content should be | 116 /// be specified for a newly-parsed scalar that hasn't yet been composed. |
117 * specified for a newly-parsed scalar that hasn't yet been composed. Value | 117 /// Value should be specified for a composed scalar, although `null` is a |
118 * should be specified for a composed scalar, although `null` is a valid | 118 /// valid value. |
119 * value. | |
120 */ | |
121 _ScalarNode(String tagName, {String content, this.value}) | 119 _ScalarNode(String tagName, {String content, this.value}) |
122 : _content = content, | 120 : _content = content, |
123 super(new _Tag.scalar(tagName)); | 121 super(new _Tag.scalar(tagName)); |
124 | 122 |
125 /** Two scalars are equal if their string representations are equal. */ | 123 /// Two scalars are equal if their string representations are equal. |
126 bool operator ==(other) { | 124 bool operator ==(other) { |
127 // Should be super != other; bug 2554 | 125 // Should be super != other; bug 2554 |
128 if (!(super == other) || other is! _ScalarNode) return false; | 126 if (!(super == other) || other is! _ScalarNode) return false; |
129 return content == other.content; | 127 return content == other.content; |
130 } | 128 } |
131 | 129 |
132 /** | 130 /// Returns the string representation of the scalar. After composition, this |
133 * Returns the string representation of the scalar. After composition, this is | 131 /// is equal to the canonical serialization of the value of the scalar. |
134 * equal to the canonical serialization of the value of the scalar. | |
135 */ | |
136 String get content => _content != null ? _content : canonicalContent; | 132 String get content => _content != null ? _content : canonicalContent; |
137 | 133 |
138 /** | 134 /// Returns the canonical serialization of the value of the scalar. If the |
139 * Returns the canonical serialization of the value of the scalar. If the | 135 /// value isn't given, the result of this will be "null". |
140 * value isn't given, the result of this will be "null". | |
141 */ | |
142 String get canonicalContent { | 136 String get canonicalContent { |
143 if (value == null || value is bool || value is int) return '$value'; | 137 if (value == null || value is bool || value is int) return '$value'; |
144 | 138 |
145 if (value is num) { | 139 if (value is num) { |
146 // 20 is the maximum value for this argument, which we use since YAML | 140 // 20 is the maximum value for this argument, which we use since YAML |
147 // doesn't specify a maximum. | 141 // doesn't specify a maximum. |
148 return value.toStringAsExponential(20). | 142 return value.toStringAsExponential(20). |
149 replaceFirst(new RegExp("0+e"), "e"); | 143 replaceFirst(new RegExp("0+e"), "e"); |
150 } | 144 } |
151 | 145 |
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
183 } | 177 } |
184 }); | 178 }); |
185 return '"${Strings.join(escapedValue, '')}"'; | 179 return '"${Strings.join(escapedValue, '')}"'; |
186 } | 180 } |
187 | 181 |
188 throw new YamlException("unknown scalar value: $value"); | 182 throw new YamlException("unknown scalar value: $value"); |
189 } | 183 } |
190 | 184 |
191 String toString() => '$tag "$content"'; | 185 String toString() => '$tag "$content"'; |
192 | 186 |
193 /** | 187 /// Left-pads [str] with zeros so that it's at least [length] characters |
194 * Left-pads [str] with zeros so that it's at least [length] characters | 188 /// long. |
195 * long. | |
196 */ | |
197 String zeroPad(String str, int length) { | 189 String zeroPad(String str, int length) { |
198 assert(length >= str.length); | 190 assert(length >= str.length); |
199 var prefix = []; | 191 var prefix = []; |
200 prefix.insertRange(0, length - str.length, '0'); | 192 prefix.insertRange(0, length - str.length, '0'); |
201 return '${Strings.join(prefix, '')}$str'; | 193 return '${Strings.join(prefix, '')}$str'; |
202 } | 194 } |
203 | 195 |
204 int get hashCode => super.hashCode ^ content.hashCode; | 196 int get hashCode => super.hashCode ^ content.hashCode; |
205 | 197 |
206 visit(_Visitor v) => v.visitScalar(this); | 198 visit(_Visitor v) => v.visitScalar(this); |
207 } | 199 } |
208 | 200 |
209 /** A mapping node represents an unordered map of nodes to nodes. */ | 201 /// A mapping node represents an unordered map of nodes to nodes. |
210 class _MappingNode extends _Node { | 202 class _MappingNode extends _Node { |
211 /** The node map. */ | 203 /// The node map. |
212 Map<_Node, _Node> content; | 204 Map<_Node, _Node> content; |
213 | 205 |
214 _MappingNode(String tagName, this.content) | 206 _MappingNode(String tagName, this.content) |
215 : super(new _Tag.mapping(tagName)); | 207 : super(new _Tag.mapping(tagName)); |
216 | 208 |
217 /** Two mappings are equal if their tags and contents are equal. */ | 209 /// Two mappings are equal if their tags and contents are equal. |
218 bool operator ==(other) { | 210 bool operator ==(other) { |
219 // Should be super != other; bug 2554 | 211 // Should be super != other; bug 2554 |
220 if (!(super == other) || other is! _MappingNode) return false; | 212 if (!(super == other) || other is! _MappingNode) return false; |
221 if (content.length != other.content.length) return false; | 213 if (content.length != other.content.length) return false; |
222 for (var key in content.keys) { | 214 for (var key in content.keys) { |
223 if (!other.content.containsKey(key)) return false; | 215 if (!other.content.containsKey(key)) return false; |
224 if (content[key] != other.content[key]) return false; | 216 if (content[key] != other.content[key]) return false; |
225 } | 217 } |
226 return true; | 218 return true; |
227 } | 219 } |
228 | 220 |
229 String toString() { | 221 String toString() { |
230 var strContent = Strings.join(content.keys. | 222 var strContent = Strings.join(content.keys. |
231 map((k) => '${k}: ${content[k]}'), ', '); | 223 map((k) => '${k}: ${content[k]}'), ', '); |
232 return '$tag {$strContent}'; | 224 return '$tag {$strContent}'; |
233 } | 225 } |
234 | 226 |
235 int get hashCode => super.hashCode ^ _hashCode(content); | 227 int get hashCode => super.hashCode ^ _hashCode(content); |
236 | 228 |
237 visit(_Visitor v) => v.visitMapping(this); | 229 visit(_Visitor v) => v.visitMapping(this); |
238 } | 230 } |
OLD | NEW |