| 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 yaml.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 |
| (...skipping 16 matching lines...) Expand all Loading... |
| 27 var _idCounter = 0; | 27 var _idCounter = 0; |
| 28 | 28 |
| 29 Composer(this._root); | 29 Composer(this._root); |
| 30 | 30 |
| 31 /// Runs the Composer to produce a representation graph. | 31 /// Runs the Composer to produce a representation graph. |
| 32 Node compose() => _root.visit(this); | 32 Node compose() => _root.visit(this); |
| 33 | 33 |
| 34 /// Returns the anchor to which an alias node refers. | 34 /// Returns the anchor to which an alias node refers. |
| 35 Node visitAlias(AliasNode alias) { | 35 Node visitAlias(AliasNode alias) { |
| 36 if (!_anchors.containsKey(alias.anchor)) { | 36 if (!_anchors.containsKey(alias.anchor)) { |
| 37 throw new YamlException("No anchor for alias ${alias.anchor}."); | 37 throw new YamlException("No anchor for alias ${alias.anchor}.", |
| 38 alias.span); |
| 38 } | 39 } |
| 39 return _anchors[alias.anchor]; | 40 return _anchors[alias.anchor]; |
| 40 } | 41 } |
| 41 | 42 |
| 42 /// Parses a scalar node according to its tag, or auto-detects the type if no | 43 /// Parses a scalar node according to its tag, or auto-detects the type if no |
| 43 /// tag exists. | 44 /// tag exists. |
| 44 /// | 45 /// |
| 45 /// Currently this only supports the YAML core type schema. | 46 /// Currently this only supports the YAML core type schema. |
| 46 Node visitScalar(ScalarNode scalar) { | 47 Node visitScalar(ScalarNode scalar) { |
| 47 if (scalar.tag.name == "!") { | 48 if (scalar.tag.name == "!") { |
| 48 return setAnchor(scalar, parseString(scalar)); | 49 return setAnchor(scalar, parseString(scalar)); |
| 49 } else if (scalar.tag.name == "?") { | 50 } else if (scalar.tag.name == "?") { |
| 50 for (var fn in [parseNull, parseBool, parseInt, parseFloat]) { | 51 for (var fn in [parseNull, parseBool, parseInt, parseFloat]) { |
| 51 var result = fn(scalar); | 52 var result = fn(scalar); |
| 52 if (result != null) return result; | 53 if (result != null) return result; |
| 53 } | 54 } |
| 54 return setAnchor(scalar, parseString(scalar)); | 55 return setAnchor(scalar, parseString(scalar)); |
| 55 } | 56 } |
| 56 | 57 |
| 57 var result = _parseByTag(scalar); | 58 var result = _parseByTag(scalar); |
| 58 if (result != null) return setAnchor(scalar, result); | 59 if (result != null) return setAnchor(scalar, result); |
| 59 throw new YamlException('Invalid literal for ${scalar.tag}: ' | 60 throw new YamlException('Invalid literal for ${scalar.tag}.', |
| 60 '"${scalar.content}".'); | 61 scalar.span); |
| 61 } | 62 } |
| 62 | 63 |
| 63 ScalarNode _parseByTag(ScalarNode scalar) { | 64 ScalarNode _parseByTag(ScalarNode scalar) { |
| 64 switch (scalar.tag.name) { | 65 switch (scalar.tag.name) { |
| 65 case "null": return parseNull(scalar); | 66 case "null": return parseNull(scalar); |
| 66 case "bool": return parseBool(scalar); | 67 case "bool": return parseBool(scalar); |
| 67 case "int": return parseInt(scalar); | 68 case "int": return parseInt(scalar); |
| 68 case "float": return parseFloat(scalar); | 69 case "float": return parseFloat(scalar); |
| 69 case "str": return parseString(scalar); | 70 case "str": return parseString(scalar); |
| 70 } | 71 } |
| 71 throw new YamlException('Undefined tag: ${scalar.tag}.'); | 72 throw new YamlException('Undefined tag: ${scalar.tag}.', scalar.span); |
| 72 } | 73 } |
| 73 | 74 |
| 74 /// Assigns a tag to the sequence and recursively composes its contents. | 75 /// Assigns a tag to the sequence and recursively composes its contents. |
| 75 Node visitSequence(SequenceNode seq) { | 76 Node visitSequence(SequenceNode seq) { |
| 76 var tagName = seq.tag.name; | 77 var tagName = seq.tag.name; |
| 77 if (tagName != "!" && tagName != "?" && tagName != Tag.yaml("seq")) { | 78 if (tagName != "!" && tagName != "?" && tagName != Tag.yaml("seq")) { |
| 78 throw new YamlException("Invalid tag for sequence: ${seq.tag}."); | 79 throw new YamlException("Invalid tag for sequence: ${seq.tag}.", |
| 80 seq.span); |
| 79 } | 81 } |
| 80 | 82 |
| 81 var result = setAnchor(seq, | 83 var result = setAnchor(seq, |
| 82 new SequenceNode(Tag.yaml('seq'), null, seq.span)); | 84 new SequenceNode(Tag.yaml('seq'), null, seq.span)); |
| 83 result.content = super.visitSequence(seq); | 85 result.content = super.visitSequence(seq); |
| 84 return result; | 86 return result; |
| 85 } | 87 } |
| 86 | 88 |
| 87 /// Assigns a tag to the mapping and recursively composes its contents. | 89 /// Assigns a tag to the mapping and recursively composes its contents. |
| 88 Node visitMapping(MappingNode map) { | 90 Node visitMapping(MappingNode map) { |
| 89 var tagName = map.tag.name; | 91 var tagName = map.tag.name; |
| 90 if (tagName != "!" && tagName != "?" && tagName != Tag.yaml("map")) { | 92 if (tagName != "!" && tagName != "?" && tagName != Tag.yaml("map")) { |
| 91 throw new YamlException("Invalid tag for mapping: ${map.tag}."); | 93 throw new YamlException("Invalid tag for mapping: ${map.tag}.", |
| 94 map.span); |
| 92 } | 95 } |
| 93 | 96 |
| 94 var result = setAnchor(map, | 97 var result = setAnchor(map, |
| 95 new MappingNode(Tag.yaml('map'), null, map.span)); | 98 new MappingNode(Tag.yaml('map'), null, map.span)); |
| 96 result.content = super.visitMapping(map); | 99 result.content = super.visitMapping(map); |
| 97 return result; | 100 return result; |
| 98 } | 101 } |
| 99 | 102 |
| 100 /// If the serialization tree node [anchored] has an anchor, records that | 103 /// If the serialization tree node [anchored] has an anchor, records that |
| 101 /// that anchor is pointing to the representation graph node [result]. | 104 /// that anchor is pointing to the representation graph node [result]. |
| (...skipping 69 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 171 return new ScalarNode(Tag.yaml("float"), scalar.span, value: double.NAN); | 174 return new ScalarNode(Tag.yaml("float"), scalar.span, value: double.NAN); |
| 172 } | 175 } |
| 173 | 176 |
| 174 return null; | 177 return null; |
| 175 } | 178 } |
| 176 | 179 |
| 177 /// Parses a string scalar. | 180 /// Parses a string scalar. |
| 178 ScalarNode parseString(ScalarNode scalar) => | 181 ScalarNode parseString(ScalarNode scalar) => |
| 179 new ScalarNode(Tag.yaml("str"), scalar.span, value: scalar.content); | 182 new ScalarNode(Tag.yaml("str"), scalar.span, value: scalar.content); |
| 180 } | 183 } |
| OLD | NEW |