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

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

Powered by Google App Engine
This is Rietveld 408576698