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

Side by Side Diff: pkg/yaml/lib/composer.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 | « no previous file | pkg/yaml/lib/constructor.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 /// Takes a parsed YAML document (what the spec calls the "serialization tree")
8 /// and resolves aliases, resolves tags, and parses scalars to produce the
9 /// "representation graph".
10 class _Composer extends _Visitor {
11 /// The root node of the serialization tree.
12 _Node root;
13
14 /// Map from anchor names to the most recent representation graph node with
15 /// that anchor.
16 Map<String, _Node> anchors;
17
18 /// The next id to use for the represenation graph's anchors. The spec doesn't
19 /// use anchors in the representation graph, but we do so that the constructor
20 /// can ensure that the same node in the representation graph produces the
21 /// same native object.
22 int idCounter;
23
24 _Composer(this.root) : this.anchors = <String, _Node>{}, this.idCounter = 0;
25
26 /// Runs the Composer to produce a representation graph.
27 _Node compose() => root.visit(this);
28
29 /// Returns the anchor to which an alias node refers.
30 _Node visitAlias(_AliasNode alias) {
31 if (!anchors.containsKey(alias.anchor)) {
32 throw new YamlException("no anchor for alias ${alias.anchor}");
33 }
34 return anchors[alias.anchor];
35 }
36
37 /// Parses a scalar node according to its tag, or auto-detects the type if no
38 /// tag exists. Currently this only supports the YAML core type schema.
39 _Node visitScalar(_ScalarNode scalar) {
40 if (scalar.tag.name == "!") {
41 return setAnchor(scalar, parseString(scalar.content));
42 } else if (scalar.tag.name == "?") {
43 for (var fn in [parseNull, parseBool, parseInt, parseFloat]) {
44 var result = fn(scalar.content);
45 if (result != null) return result;
46 }
47 return setAnchor(scalar, parseString(scalar.content));
48 }
49
50 // TODO(nweiz): support the full YAML type repository
51 var tagParsers = {
52 'null': parseNull, 'bool': parseBool, 'int': parseInt,
53 'float': parseFloat, 'str': parseString
54 };
55
56 for (var key in tagParsers.keys) {
57 if (scalar.tag.name != _Tag.yaml(key)) continue;
58 var result = tagParsers[key](scalar.content);
59 if (result != null) return setAnchor(scalar, result);
60 throw new YamlException('invalid literal for $key: "${scalar.content}"');
61 }
62
63 throw new YamlException('undefined tag: "${scalar.tag.name}"');
64 }
65
66 /// Assigns a tag to the sequence and recursively composes its contents.
67 _Node visitSequence(_SequenceNode seq) {
68 var tagName = seq.tag.name;
69 if (tagName != "!" && tagName != "?" && tagName != _Tag.yaml("seq")) {
70 throw new YamlException("invalid tag for sequence: ${tagName}");
71 }
72
73 var result = setAnchor(seq, new _SequenceNode(_Tag.yaml("seq"), null));
74 result.content = super.visitSequence(seq);
75 return result;
76 }
77
78 /// Assigns a tag to the mapping and recursively composes its contents.
79 _Node visitMapping(_MappingNode map) {
80 var tagName = map.tag.name;
81 if (tagName != "!" && tagName != "?" && tagName != _Tag.yaml("map")) {
82 throw new YamlException("invalid tag for mapping: ${tagName}");
83 }
84
85 var result = setAnchor(map, new _MappingNode(_Tag.yaml("map"), null));
86 result.content = super.visitMapping(map);
87 return result;
88 }
89
90 /// If the serialization tree node [anchored] has an anchor, records that
91 /// that anchor is pointing to the representation graph node [result].
92 _Node setAnchor(_Node anchored, _Node result) {
93 if (anchored.anchor == null) return result;
94 result.anchor = '${idCounter++}';
95 anchors[anchored.anchor] = result;
96 return result;
97 }
98
99 /// Parses a null scalar.
100 _ScalarNode parseNull(String content) {
101 if (!new RegExp(r"^(null|Null|NULL|~|)$").hasMatch(content)) return null;
102 return new _ScalarNode(_Tag.yaml("null"), value: null);
103 }
104
105 /// Parses a boolean scalar.
106 _ScalarNode parseBool(String content) {
107 var match = new RegExp(r"^(?:(true|True|TRUE)|(false|False|FALSE))$").
108 firstMatch(content);
109 if (match == null) return null;
110 return new _ScalarNode(_Tag.yaml("bool"), value: match.group(1) != null);
111 }
112
113 /// Parses an integer scalar.
114 _ScalarNode parseInt(String content) {
115 var match = new RegExp(r"^[-+]?[0-9]+$").firstMatch(content);
116 if (match != null) {
117 return new _ScalarNode(_Tag.yaml("int"),
118 value: int.parse(match.group(0)));
119 }
120
121 match = new RegExp(r"^0o([0-7]+)$").firstMatch(content);
122 if (match != null) {
123 int n = int.parse(match.group(1), radix: 8);
124 return new _ScalarNode(_Tag.yaml("int"), value: n);
125 }
126
127 match = new RegExp(r"^0x[0-9a-fA-F]+$").firstMatch(content);
128 if (match != null) {
129 return new _ScalarNode(_Tag.yaml("int"),
130 value: int.parse(match.group(0)));
131 }
132
133 return null;
134 }
135
136 /// Parses a floating-point scalar.
137 _ScalarNode parseFloat(String content) {
138 var match = new RegExp(
139 r"^[-+]?(\.[0-9]+|[0-9]+(\.[0-9]*)?)([eE][-+]?[0-9]+)?$").
140 firstMatch(content);
141 if (match != null) {
142 // YAML allows floats of the form "0.", but Dart does not. Fix up those
143 // floats by removing the trailing dot.
144 var matchStr = match.group(0).replaceAll(new RegExp(r"\.$"), "");
145 return new _ScalarNode(_Tag.yaml("float"),
146 value: double.parse(matchStr));
147 }
148
149 match = new RegExp(r"^([+-]?)\.(inf|Inf|INF)$").firstMatch(content);
150 if (match != null) {
151 var value = match.group(1) == "-" ? -double.INFINITY : double.INFINITY;
152 return new _ScalarNode(_Tag.yaml("float"), value: value);
153 }
154
155 match = new RegExp(r"^\.(nan|NaN|NAN)$").firstMatch(content);
156 if (match != null) {
157 return new _ScalarNode(_Tag.yaml("float"), value: double.NAN);
158 }
159
160 return null;
161 }
162
163 /// Parses a string scalar.
164 _ScalarNode parseString(String content) =>
165 new _ScalarNode(_Tag.yaml("str"), value: content);
166 }
OLDNEW
« no previous file with comments | « no previous file | pkg/yaml/lib/constructor.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698