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 library model; | 8 library yaml.model; |
9 | 9 |
10 import 'parser.dart'; | 10 import 'parser.dart'; |
11 import 'utils.dart'; | 11 import 'utils.dart'; |
12 import 'visitor.dart'; | 12 import 'visitor.dart'; |
13 import 'yaml_exception.dart'; | 13 import 'yaml_exception.dart'; |
14 | 14 |
| 15 /// The prefix for tag types defined by the YAML spec. |
| 16 const _YAML_URI_PREFIX = "tag:yaml.org,2002:"; |
| 17 |
15 /// A tag that indicates the type of a YAML node. | 18 /// A tag that indicates the type of a YAML node. |
16 class Tag { | 19 class Tag { |
17 // TODO(nweiz): it would better match the semantics of the spec if there were | |
18 // a singleton instance of this class for each tag. | |
19 | |
20 static const SCALAR_KIND = 0; | |
21 static const SEQUENCE_KIND = 1; | |
22 static const MAPPING_KIND = 2; | |
23 | |
24 static const String YAML_URI_PREFIX = 'tag:yaml.org,2002:'; | |
25 | |
26 /// 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 "!". |
27 final String name; | 21 final String name; |
28 | 22 |
29 /// The kind of the tag: SCALAR_KIND, SEQUENCE_KIND, or MAPPING_KIND. | 23 /// The kind of the tag. |
30 final int kind; | 24 final TagKind kind; |
31 | |
32 Tag(this.name, this.kind); | |
33 | |
34 Tag.scalar(String name) : this(name, SCALAR_KIND); | |
35 Tag.sequence(String name) : this(name, SEQUENCE_KIND); | |
36 Tag.mapping(String name) : this(name, MAPPING_KIND); | |
37 | 25 |
38 /// Returns the standard YAML tag URI for [type]. | 26 /// Returns the standard YAML tag URI for [type]. |
39 static String yaml(String type) => "tag:yaml.org,2002:$type"; | 27 static String yaml(String type) => "tag:yaml.org,2002:$type"; |
40 | 28 |
| 29 const Tag(this.name, this.kind); |
| 30 |
| 31 const Tag.scalar(String name) |
| 32 : this(name, TagKind.SCALAR); |
| 33 |
| 34 const Tag.sequence(String name) |
| 35 : this(name, TagKind.SEQUENCE); |
| 36 |
| 37 const Tag.mapping(String name) |
| 38 : this(name, TagKind.MAPPING); |
| 39 |
41 /// Two tags are equal if their URIs are equal. | 40 /// Two tags are equal if their URIs are equal. |
42 operator ==(other) { | 41 operator ==(other) { |
43 if (other is! Tag) return false; | 42 if (other is! Tag) return false; |
44 return name == other.name; | 43 return name == other.name; |
45 } | 44 } |
46 | 45 |
47 String toString() { | 46 String toString() { |
48 if (name.startsWith(YAML_URI_PREFIX)) { | 47 if (name.startsWith(_YAML_URI_PREFIX)) { |
49 return '!!${name.substring(YAML_URI_PREFIX.length)}'; | 48 return '!!${name.substring(_YAML_URI_PREFIX.length)}'; |
50 } else { | 49 } else { |
51 return '!<$name>'; | 50 return '!<$name>'; |
52 } | 51 } |
53 } | 52 } |
54 | 53 |
55 int get hashCode => name.hashCode; | 54 int get hashCode => name.hashCode; |
56 } | 55 } |
57 | 56 |
| 57 /// An enum for kinds of tags. |
| 58 class TagKind { |
| 59 /// A tag indicating that the value is a scalar. |
| 60 static const SCALAR = const TagKind._("scalar"); |
| 61 |
| 62 /// A tag indicating that the value is a sequence. |
| 63 static const SEQUENCE = const TagKind._("sequence"); |
| 64 |
| 65 /// A tag indicating that the value is a mapping. |
| 66 static const MAPPING = const TagKind._("mapping"); |
| 67 |
| 68 final String name; |
| 69 |
| 70 const TagKind._(this.name); |
| 71 |
| 72 String toString() => name; |
| 73 } |
| 74 |
58 /// The abstract class for YAML nodes. | 75 /// The abstract class for YAML nodes. |
59 abstract class Node { | 76 abstract class Node { |
60 /// Every YAML node has a tag that describes its type. | 77 /// Every YAML node has a tag that describes its type. |
61 Tag tag; | 78 Tag tag; |
62 | 79 |
63 /// Any YAML node can have an anchor associated with it. | 80 /// Any YAML node can have an anchor associated with it. |
64 String anchor; | 81 String anchor; |
65 | 82 |
66 Node(this.tag, [this.anchor]); | 83 Node(this.tag, [this.anchor]); |
67 | 84 |
(...skipping 110 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
178 } else if (c >= 0x10000) { | 195 } else if (c >= 0x10000) { |
179 return "\\u${zeroPad(c.toRadixString(16).toUpperCase(), 8)}"; | 196 return "\\u${zeroPad(c.toRadixString(16).toUpperCase(), 8)}"; |
180 } else { | 197 } else { |
181 return new String.fromCharCodes([c]); | 198 return new String.fromCharCodes([c]); |
182 } | 199 } |
183 } | 200 } |
184 }); | 201 }); |
185 return '"${escapedValue.join()}"'; | 202 return '"${escapedValue.join()}"'; |
186 } | 203 } |
187 | 204 |
188 throw new YamlException("unknown scalar value: $value"); | 205 throw new YamlException('Unknown scalar value: "$value".'); |
189 } | 206 } |
190 | 207 |
191 String toString() => '$tag "$content"'; | 208 String toString() => '$tag "$content"'; |
192 | 209 |
193 /// Left-pads [str] with zeros so that it's at least [length] characters | 210 /// Left-pads [str] with zeros so that it's at least [length] characters |
194 /// long. | 211 /// long. |
195 String zeroPad(String str, int length) { | 212 String zeroPad(String str, int length) { |
196 assert(length >= str.length); | 213 assert(length >= str.length); |
197 var prefix = new List.filled(length - str.length, '0'); | 214 var prefix = new List.filled(length - str.length, '0'); |
198 return '${prefix.join()}$str'; | 215 return '${prefix.join()}$str'; |
(...skipping 28 matching lines...) Expand all Loading... |
227 var strContent = content.keys | 244 var strContent = content.keys |
228 .map((k) => '${k}: ${content[k]}') | 245 .map((k) => '${k}: ${content[k]}') |
229 .join(', '); | 246 .join(', '); |
230 return '$tag {$strContent}'; | 247 return '$tag {$strContent}'; |
231 } | 248 } |
232 | 249 |
233 int get hashCode => super.hashCode ^ hashCodeFor(content); | 250 int get hashCode => super.hashCode ^ hashCodeFor(content); |
234 | 251 |
235 visit(Visitor v) => v.visitMapping(this); | 252 visit(Visitor v) => v.visitMapping(this); |
236 } | 253 } |
OLD | NEW |