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