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