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 library composer; | 5 library yaml.composer; |
6 | 6 |
7 import 'model.dart'; | 7 import 'model.dart'; |
8 import 'visitor.dart'; | 8 import 'visitor.dart'; |
9 import 'yaml_exception.dart'; | 9 import 'yaml_exception.dart'; |
10 | 10 |
11 /// 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") |
12 /// and resolves aliases, resolves tags, and parses scalars to produce the | 12 /// and resolves aliases, resolves tags, and parses scalars to produce the |
13 /// "representation graph". | 13 /// "representation graph". |
14 class Composer extends Visitor { | 14 class Composer extends Visitor { |
15 /// The root node of the serialization tree. | 15 /// The root node of the serialization tree. |
16 final Node _root; | 16 final Node _root; |
17 | 17 |
18 /// 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 |
19 /// that anchor. | 19 /// that anchor. |
20 final _anchors = <String, Node>{}; | 20 final _anchors = <String, Node>{}; |
21 | 21 |
22 /// 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. |
23 /// use anchors in the representation graph, but we do so that the constructor | 23 /// |
24 /// can ensure that the same node in the representation graph produces the | 24 /// The spec doesn't use anchors in the representation graph, but we do so |
25 /// same native object. | 25 /// that the constructor can ensure that the same node in the representation |
26 /// graph produces the same native object. | |
26 var _idCounter = 0; | 27 var _idCounter = 0; |
27 | 28 |
28 Composer(this._root); | 29 Composer(this._root); |
29 | 30 |
30 /// Runs the Composer to produce a representation graph. | 31 /// Runs the Composer to produce a representation graph. |
31 Node compose() => _root.visit(this); | 32 Node compose() => _root.visit(this); |
32 | 33 |
33 /// Returns the anchor to which an alias node refers. | 34 /// Returns the anchor to which an alias node refers. |
34 Node visitAlias(AliasNode alias) { | 35 Node visitAlias(AliasNode alias) { |
35 if (!_anchors.containsKey(alias.anchor)) { | 36 if (!_anchors.containsKey(alias.anchor)) { |
36 throw new YamlException("no anchor for alias ${alias.anchor}"); | 37 throw new YamlException("No anchor for alias ${alias.anchor}."); |
37 } | 38 } |
38 return _anchors[alias.anchor]; | 39 return _anchors[alias.anchor]; |
39 } | 40 } |
40 | 41 |
41 /// Parses a scalar node according to its tag, or auto-detects the type if no | 42 /// Parses a scalar node according to its tag, or auto-detects the type if no |
42 /// tag exists. Currently this only supports the YAML core type schema. | 43 /// tag exists. |
44 /// | |
45 /// Currently this only supports the YAML core type schema. | |
43 Node visitScalar(ScalarNode scalar) { | 46 Node visitScalar(ScalarNode scalar) { |
44 if (scalar.tag.name == "!") { | 47 if (scalar.tag.name == "!") { |
45 return setAnchor(scalar, parseString(scalar.content)); | 48 return setAnchor(scalar, parseString(scalar.content)); |
46 } else if (scalar.tag.name == "?") { | 49 } else if (scalar.tag.name == "?") { |
47 for (var fn in [parseNull, parseBool, parseInt, parseFloat]) { | 50 for (var fn in [parseNull, parseBool, parseInt, parseFloat]) { |
48 var result = fn(scalar.content); | 51 var result = fn(scalar.content); |
49 if (result != null) return result; | 52 if (result != null) return result; |
50 } | 53 } |
51 return setAnchor(scalar, parseString(scalar.content)); | 54 return setAnchor(scalar, parseString(scalar.content)); |
52 } | 55 } |
53 | 56 |
54 // TODO(nweiz): support the full YAML type repository | 57 var result = _parseByTag(scalar); |
55 var tagParsers = { | 58 if (result != null) return setAnchor(scalar, result); |
56 'null': parseNull, 'bool': parseBool, 'int': parseInt, | 59 throw new YamlException('Invalid literal for ${scalar.tag}: ' |
57 'float': parseFloat, 'str': parseString | 60 '"${scalar.content}".'); |
58 }; | 61 } |
59 | 62 |
60 for (var key in tagParsers.keys) { | 63 ScalarNode _parseByTag(ScalarNode scalar) { |
61 if (scalar.tag.name != Tag.yaml(key)) continue; | 64 switch (scalar.tag.name) { |
62 var result = tagParsers[key](scalar.content); | 65 case "null": return parseNull(scalar.content); |
63 if (result != null) return setAnchor(scalar, result); | 66 case "bool": return parseBool(scalar.content); |
64 throw new YamlException('invalid literal for $key: "${scalar.content}"'); | 67 case "int": return parseInt(scalar.content); |
68 case "float": return parseFloat(scalar.content); | |
69 case "str": return parseString(scalar.content); | |
65 } | 70 } |
66 | 71 throw new YamlException('Undefined tag: ${scalar.tag}.'); |
67 throw new YamlException('undefined tag: "${scalar.tag.name}"'); | |
68 } | 72 } |
69 | 73 |
70 /// Assigns a tag to the sequence and recursively composes its contents. | 74 /// Assigns a tag to the sequence and recursively composes its contents. |
71 Node visitSequence(SequenceNode seq) { | 75 Node visitSequence(SequenceNode seq) { |
72 var tagName = seq.tag.name; | 76 var tagName = seq.tag.name; |
73 if (tagName != "!" && tagName != "?" && tagName != Tag.yaml("seq")) { | 77 if (tagName != "!" && tagName != "?" && tagName != Tag.yaml("seq")) { |
74 throw new YamlException("invalid tag for sequence: ${tagName}"); | 78 throw new YamlException("Invalid tag for sequence: ${seq.tag}."); |
75 } | 79 } |
76 | 80 |
77 var result = setAnchor(seq, new SequenceNode(Tag.yaml("seq"), null)); | 81 var result = setAnchor(seq, new SequenceNode(Tag.yaml('seq'), null)); |
Bob Nystrom
2014/05/09 00:18:56
What's your rule for deciding between ' and "?
nweiz
2014/05/20 00:15:07
None, this is just accidental churn. Changed to th
| |
78 result.content = super.visitSequence(seq); | 82 result.content = super.visitSequence(seq); |
79 return result; | 83 return result; |
80 } | 84 } |
81 | 85 |
82 /// Assigns a tag to the mapping and recursively composes its contents. | 86 /// Assigns a tag to the mapping and recursively composes its contents. |
83 Node visitMapping(MappingNode map) { | 87 Node visitMapping(MappingNode map) { |
84 var tagName = map.tag.name; | 88 var tagName = map.tag.name; |
85 if (tagName != "!" && tagName != "?" && tagName != Tag.yaml("map")) { | 89 if (tagName != "!" && tagName != "?" && tagName != Tag.yaml("map")) { |
86 throw new YamlException("invalid tag for mapping: ${tagName}"); | 90 throw new YamlException("Invalid tag for mapping: ${map.tag}."); |
87 } | 91 } |
88 | 92 |
89 var result = setAnchor(map, new MappingNode(Tag.yaml("map"), null)); | 93 var result = setAnchor(map, new MappingNode(Tag.yaml('map'), null)); |
90 result.content = super.visitMapping(map); | 94 result.content = super.visitMapping(map); |
91 return result; | 95 return result; |
92 } | 96 } |
93 | 97 |
94 /// If the serialization tree node [anchored] has an anchor, records that | 98 /// If the serialization tree node [anchored] has an anchor, records that |
95 /// that anchor is pointing to the representation graph node [result]. | 99 /// that anchor is pointing to the representation graph node [result]. |
96 Node setAnchor(Node anchored, Node result) { | 100 Node setAnchor(Node anchored, Node result) { |
97 if (anchored.anchor == null) return result; | 101 if (anchored.anchor == null) return result; |
98 result.anchor = '${_idCounter++}'; | 102 result.anchor = '${_idCounter++}'; |
99 _anchors[anchored.anchor] = result; | 103 _anchors[anchored.anchor] = result; |
(...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
161 return new ScalarNode(Tag.yaml("float"), value: double.NAN); | 165 return new ScalarNode(Tag.yaml("float"), value: double.NAN); |
162 } | 166 } |
163 | 167 |
164 return null; | 168 return null; |
165 } | 169 } |
166 | 170 |
167 /// Parses a string scalar. | 171 /// Parses a string scalar. |
168 ScalarNode parseString(String content) => | 172 ScalarNode parseString(String content) => |
169 new ScalarNode(Tag.yaml("str"), value: content); | 173 new ScalarNode(Tag.yaml("str"), value: content); |
170 } | 174 } |
OLD | NEW |