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

Side by Side Diff: utils/pub/yaml/model.dart

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

Powered by Google App Engine
This is Rietveld 408576698