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

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

Issue 11622011: Restructure YAML package suitable for pub lish (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: add yaml exclude Created 7 years, 11 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 | « utils/pub/pubspec.dart ('k') | utils/pub/yaml/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("^(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("^(?:(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("^[-+]?[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("^0o([0-7]+)\$").firstMatch(content);
122 if (match != null) {
123 // TODO(nweiz): clean this up when Dart can parse an octal string
124 var n = 0;
125 for (var c in match.group(1).charCodes) {
126 n *= 8;
127 n += c - 48;
128 }
129 return new _ScalarNode(_Tag.yaml("int"), value: n);
130 }
131
132 match = new RegExp("^0x[0-9a-fA-F]+\$").firstMatch(content);
133 if (match != null) {
134 return new _ScalarNode(_Tag.yaml("int"),
135 value: int.parse(match.group(0)));
136 }
137
138 return null;
139 }
140
141 /// Parses a floating-point scalar.
142 _ScalarNode parseFloat(String content) {
143 var match = new RegExp(
144 "^[-+]?(\.[0-9]+|[0-9]+(\.[0-9]*)?)([eE][-+]?[0-9]+)?\$").
145 firstMatch(content);
146 if (match != null) {
147 // YAML allows floats of the form "0.", but Dart does not. Fix up those
148 // floats by removing the trailing dot.
149 var matchStr = match.group(0).replaceAll(new RegExp(r"\.$"), "");
150 return new _ScalarNode(_Tag.yaml("float"),
151 value: double.parse(matchStr));
152 }
153
154 match = new RegExp("^([+-]?)\.(inf|Inf|INF)\$").firstMatch(content);
155 if (match != null) {
156 var infinityStr = match.group(1) == "-" ? "-Infinity" : "Infinity";
157 return new _ScalarNode(_Tag.yaml("float"),
158 value: double.parse(infinityStr));
159 }
160
161 match = new RegExp("^\.(nan|NaN|NAN)\$").firstMatch(content);
162 if (match != null) {
163 return new _ScalarNode(_Tag.yaml("float"),
164 value: double.parse("NaN"));
165 }
166
167 return null;
168 }
169
170 /// Parses a string scalar.
171 _ScalarNode parseString(String content) =>
172 new _ScalarNode(_Tag.yaml("str"), value: content);
173 }
OLDNEW
« no previous file with comments | « utils/pub/pubspec.dart ('k') | utils/pub/yaml/constructor.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698