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