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

Side by Side Diff: pkg/yaml/lib/model.dart

Issue 14103026: Restructure the yaml package. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Created 7 years, 8 months 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/deep_equals.dart ('k') | pkg/yaml/lib/parser.dart » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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 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() => '$tag [${content.map((e) => '$e').join(', ')}]';
94
95 int get hashCode => super.hashCode ^ _hashCode(content);
96
97 visit(_Visitor v) => v.visitSequence(this);
98 }
99
100 /// An alias node is a reference to an anchor.
101 class _AliasNode extends _Node {
102 _AliasNode(String anchor) : super(new _Tag.scalar(_Tag.yaml("str")), anchor);
103
104 visit(_Visitor v) => v.visitAlias(this);
105 }
106
107 /// A scalar node represents all YAML nodes that have a single value.
108 class _ScalarNode extends _Node {
109 /// The string value of the scalar node, if it was created by the parser.
110 final String _content;
111
112 /// The Dart value of the scalar node, if it was created by the composer.
113 final value;
114
115 /// Creates a new Scalar node.
116 ///
117 /// Exactly one of [content] and [value] should be specified. Content should
118 /// be specified for a newly-parsed scalar that hasn't yet been composed.
119 /// Value should be specified for a composed scalar, although `null` is a
120 /// valid value.
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 /// Returns the string representation of the scalar. After composition, this
133 /// is equal to the canonical serialization of the value of the scalar.
134 String get content => _content != null ? _content : canonicalContent;
135
136 /// Returns the canonical serialization of the value of the scalar. If the
137 /// value isn't given, the result of this will be "null".
138 String get canonicalContent {
139 if (value == null || value is bool || value is int) return '$value';
140
141 if (value is num) {
142 // 20 is the maximum value for this argument, which we use since YAML
143 // doesn't specify a maximum.
144 return value.toStringAsExponential(20).
145 replaceFirst(new RegExp("0+e"), "e");
146 }
147
148 if (value is String) {
149 // TODO(nweiz): This could be faster if we used a RegExp to check for
150 // special characters and short-circuited if they didn't exist.
151
152 var escapedValue = value.codeUnits.map((c) {
153 switch (c) {
154 case _Parser.TAB: return "\\t";
155 case _Parser.LF: return "\\n";
156 case _Parser.CR: return "\\r";
157 case _Parser.DOUBLE_QUOTE: return '\\"';
158 case _Parser.NULL: return "\\0";
159 case _Parser.BELL: return "\\a";
160 case _Parser.BACKSPACE: return "\\b";
161 case _Parser.VERTICAL_TAB: return "\\v";
162 case _Parser.FORM_FEED: return "\\f";
163 case _Parser.ESCAPE: return "\\e";
164 case _Parser.BACKSLASH: return "\\\\";
165 case _Parser.NEL: return "\\N";
166 case _Parser.NBSP: return "\\_";
167 case _Parser.LINE_SEPARATOR: return "\\L";
168 case _Parser.PARAGRAPH_SEPARATOR: return "\\P";
169 default:
170 if (c < 0x20 || (c >= 0x7f && c < 0x100)) {
171 return "\\x${zeroPad(c.toRadixString(16).toUpperCase(), 2)}";
172 } else if (c >= 0x100 && c < 0x10000) {
173 return "\\u${zeroPad(c.toRadixString(16).toUpperCase(), 4)}";
174 } else if (c >= 0x10000) {
175 return "\\u${zeroPad(c.toRadixString(16).toUpperCase(), 8)}";
176 } else {
177 return new String.fromCharCodes([c]);
178 }
179 }
180 });
181 return '"${escapedValue.join()}"';
182 }
183
184 throw new YamlException("unknown scalar value: $value");
185 }
186
187 String toString() => '$tag "$content"';
188
189 /// Left-pads [str] with zeros so that it's at least [length] characters
190 /// long.
191 String zeroPad(String str, int length) {
192 assert(length >= str.length);
193 var prefix = new List.filled(length - str.length, '0');
194 return '${prefix.join()}$str';
195 }
196
197 int get hashCode => super.hashCode ^ content.hashCode;
198
199 visit(_Visitor v) => v.visitScalar(this);
200 }
201
202 /// A mapping node represents an unordered map of nodes to nodes.
203 class _MappingNode extends _Node {
204 /// The node map.
205 Map<_Node, _Node> content;
206
207 _MappingNode(String tagName, this.content)
208 : super(new _Tag.mapping(tagName));
209
210 /// Two mappings are equal if their tags and contents are equal.
211 bool operator ==(other) {
212 // Should be super != other; bug 2554
213 if (!(super == other) || other is! _MappingNode) return false;
214 if (content.length != other.content.length) return false;
215 for (var key in content.keys) {
216 if (!other.content.containsKey(key)) return false;
217 if (content[key] != other.content[key]) return false;
218 }
219 return true;
220 }
221
222 String toString() {
223 var strContent = content.keys
224 .map((k) => '${k}: ${content[k]}')
225 .join(', ');
226 return '$tag {$strContent}';
227 }
228
229 int get hashCode => super.hashCode ^ _hashCode(content);
230
231 visit(_Visitor v) => v.visitMapping(this);
232 }
OLDNEW
« no previous file with comments | « pkg/yaml/lib/deep_equals.dart ('k') | pkg/yaml/lib/parser.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698