Index: pkg/yaml/lib/src/composer.dart |
diff --git a/pkg/yaml/lib/composer.dart b/pkg/yaml/lib/src/composer.dart |
similarity index 71% |
rename from pkg/yaml/lib/composer.dart |
rename to pkg/yaml/lib/src/composer.dart |
index 90ed3308eddc1bdb5a57002aa3b01d2bfbeaa855..40b76677e654a760c25f88ce8c66d393a9012c1e 100644 |
--- a/pkg/yaml/lib/composer.dart |
+++ b/pkg/yaml/lib/src/composer.dart |
@@ -2,41 +2,45 @@ |
// for details. All rights reserved. Use of this source code is governed by a |
// BSD-style license that can be found in the LICENSE file. |
-part of yaml; |
+library composer; |
+ |
+import 'model.dart'; |
+import 'visitor.dart'; |
+import 'yaml_exception.dart'; |
/// Takes a parsed YAML document (what the spec calls the "serialization tree") |
/// and resolves aliases, resolves tags, and parses scalars to produce the |
/// "representation graph". |
-class _Composer extends _Visitor { |
+class Composer extends Visitor { |
/// The root node of the serialization tree. |
- _Node root; |
+ final Node _root; |
/// Map from anchor names to the most recent representation graph node with |
/// that anchor. |
- Map<String, _Node> anchors; |
+ final _anchors = <String, Node>{}; |
/// The next id to use for the represenation graph's anchors. The spec doesn't |
/// use anchors in the representation graph, but we do so that the constructor |
/// can ensure that the same node in the representation graph produces the |
/// same native object. |
- int idCounter; |
+ var _idCounter = 0; |
- _Composer(this.root) : this.anchors = <String, _Node>{}, this.idCounter = 0; |
+ Composer(this._root); |
/// Runs the Composer to produce a representation graph. |
- _Node compose() => root.visit(this); |
+ Node compose() => _root.visit(this); |
/// Returns the anchor to which an alias node refers. |
- _Node visitAlias(_AliasNode alias) { |
- if (!anchors.containsKey(alias.anchor)) { |
+ Node visitAlias(AliasNode alias) { |
+ if (!_anchors.containsKey(alias.anchor)) { |
throw new YamlException("no anchor for alias ${alias.anchor}"); |
} |
- return anchors[alias.anchor]; |
+ return _anchors[alias.anchor]; |
} |
/// Parses a scalar node according to its tag, or auto-detects the type if no |
/// tag exists. Currently this only supports the YAML core type schema. |
- _Node visitScalar(_ScalarNode scalar) { |
+ Node visitScalar(ScalarNode scalar) { |
if (scalar.tag.name == "!") { |
return setAnchor(scalar, parseString(scalar.content)); |
} else if (scalar.tag.name == "?") { |
@@ -54,7 +58,7 @@ class _Composer extends _Visitor { |
}; |
for (var key in tagParsers.keys) { |
- if (scalar.tag.name != _Tag.yaml(key)) continue; |
+ if (scalar.tag.name != Tag.yaml(key)) continue; |
var result = tagParsers[key](scalar.content); |
if (result != null) return setAnchor(scalar, result); |
throw new YamlException('invalid literal for $key: "${scalar.content}"'); |
@@ -64,69 +68,69 @@ class _Composer extends _Visitor { |
} |
/// Assigns a tag to the sequence and recursively composes its contents. |
- _Node visitSequence(_SequenceNode seq) { |
+ Node visitSequence(SequenceNode seq) { |
var tagName = seq.tag.name; |
- if (tagName != "!" && tagName != "?" && tagName != _Tag.yaml("seq")) { |
+ if (tagName != "!" && tagName != "?" && tagName != Tag.yaml("seq")) { |
throw new YamlException("invalid tag for sequence: ${tagName}"); |
} |
- var result = setAnchor(seq, new _SequenceNode(_Tag.yaml("seq"), null)); |
+ var result = setAnchor(seq, new SequenceNode(Tag.yaml("seq"), null)); |
result.content = super.visitSequence(seq); |
return result; |
} |
/// Assigns a tag to the mapping and recursively composes its contents. |
- _Node visitMapping(_MappingNode map) { |
+ Node visitMapping(MappingNode map) { |
var tagName = map.tag.name; |
- if (tagName != "!" && tagName != "?" && tagName != _Tag.yaml("map")) { |
+ if (tagName != "!" && tagName != "?" && tagName != Tag.yaml("map")) { |
throw new YamlException("invalid tag for mapping: ${tagName}"); |
} |
- var result = setAnchor(map, new _MappingNode(_Tag.yaml("map"), null)); |
+ var result = setAnchor(map, new MappingNode(Tag.yaml("map"), null)); |
result.content = super.visitMapping(map); |
return result; |
} |
/// If the serialization tree node [anchored] has an anchor, records that |
/// that anchor is pointing to the representation graph node [result]. |
- _Node setAnchor(_Node anchored, _Node result) { |
+ Node setAnchor(Node anchored, Node result) { |
if (anchored.anchor == null) return result; |
- result.anchor = '${idCounter++}'; |
- anchors[anchored.anchor] = result; |
+ result.anchor = '${_idCounter++}'; |
+ _anchors[anchored.anchor] = result; |
return result; |
} |
/// Parses a null scalar. |
- _ScalarNode parseNull(String content) { |
+ ScalarNode parseNull(String content) { |
if (!new RegExp(r"^(null|Null|NULL|~|)$").hasMatch(content)) return null; |
- return new _ScalarNode(_Tag.yaml("null"), value: null); |
+ return new ScalarNode(Tag.yaml("null"), value: null); |
} |
/// Parses a boolean scalar. |
- _ScalarNode parseBool(String content) { |
+ ScalarNode parseBool(String content) { |
var match = new RegExp(r"^(?:(true|True|TRUE)|(false|False|FALSE))$"). |
firstMatch(content); |
if (match == null) return null; |
- return new _ScalarNode(_Tag.yaml("bool"), value: match.group(1) != null); |
+ return new ScalarNode(Tag.yaml("bool"), value: match.group(1) != null); |
} |
/// Parses an integer scalar. |
- _ScalarNode parseInt(String content) { |
+ ScalarNode parseInt(String content) { |
var match = new RegExp(r"^[-+]?[0-9]+$").firstMatch(content); |
if (match != null) { |
- return new _ScalarNode(_Tag.yaml("int"), |
+ return new ScalarNode(Tag.yaml("int"), |
value: int.parse(match.group(0))); |
} |
match = new RegExp(r"^0o([0-7]+)$").firstMatch(content); |
if (match != null) { |
int n = int.parse(match.group(1), radix: 8); |
- return new _ScalarNode(_Tag.yaml("int"), value: n); |
+ return new ScalarNode(Tag.yaml("int"), value: n); |
} |
match = new RegExp(r"^0x[0-9a-fA-F]+$").firstMatch(content); |
if (match != null) { |
- return new _ScalarNode(_Tag.yaml("int"), |
+ return new ScalarNode(Tag.yaml("int"), |
value: int.parse(match.group(0))); |
} |
@@ -134,7 +138,7 @@ class _Composer extends _Visitor { |
} |
/// Parses a floating-point scalar. |
- _ScalarNode parseFloat(String content) { |
+ ScalarNode parseFloat(String content) { |
var match = new RegExp( |
r"^[-+]?(\.[0-9]+|[0-9]+(\.[0-9]*)?)([eE][-+]?[0-9]+)?$"). |
firstMatch(content); |
@@ -142,25 +146,25 @@ class _Composer extends _Visitor { |
// YAML allows floats of the form "0.", but Dart does not. Fix up those |
// floats by removing the trailing dot. |
var matchStr = match.group(0).replaceAll(new RegExp(r"\.$"), ""); |
- return new _ScalarNode(_Tag.yaml("float"), |
+ return new ScalarNode(Tag.yaml("float"), |
value: double.parse(matchStr)); |
} |
match = new RegExp(r"^([+-]?)\.(inf|Inf|INF)$").firstMatch(content); |
if (match != null) { |
var value = match.group(1) == "-" ? -double.INFINITY : double.INFINITY; |
- return new _ScalarNode(_Tag.yaml("float"), value: value); |
+ return new ScalarNode(Tag.yaml("float"), value: value); |
} |
match = new RegExp(r"^\.(nan|NaN|NAN)$").firstMatch(content); |
if (match != null) { |
- return new _ScalarNode(_Tag.yaml("float"), value: double.NAN); |
+ return new ScalarNode(Tag.yaml("float"), value: double.NAN); |
} |
return null; |
} |
/// Parses a string scalar. |
- _ScalarNode parseString(String content) => |
- new _ScalarNode(_Tag.yaml("str"), value: content); |
+ ScalarNode parseString(String content) => |
+ new ScalarNode(Tag.yaml("str"), value: content); |
} |