| 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 /** |
| 6 * 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") |
| 7 * and resolves aliases, resolves tags, and parses scalars to produce the | 7 * and resolves aliases, resolves tags, and parses scalars to produce the |
| 8 * "representation graph". | 8 * "representation graph". |
| 9 */ | 9 */ |
| 10 class _Composer extends _Visitor { | 10 class _Composer extends _Visitor { |
| (...skipping 88 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 99 */ | 99 */ |
| 100 _Node setAnchor(_Node anchored, _Node result) { | 100 _Node setAnchor(_Node anchored, _Node result) { |
| 101 if (anchored.anchor == null) return result; | 101 if (anchored.anchor == null) return result; |
| 102 result.anchor = '${idCounter++}'; | 102 result.anchor = '${idCounter++}'; |
| 103 anchors[anchored.anchor] = result; | 103 anchors[anchored.anchor] = result; |
| 104 return result; | 104 return result; |
| 105 } | 105 } |
| 106 | 106 |
| 107 /** Parses a null scalar. */ | 107 /** Parses a null scalar. */ |
| 108 _ScalarNode parseNull(String content) { | 108 _ScalarNode parseNull(String content) { |
| 109 if (!new RegExp("^(null|Null|NULL|~|)\$").hasMatch(content)) return null; | 109 if (!const RegExp("^(null|Null|NULL|~|)\$").hasMatch(content)) return null; |
| 110 return new _ScalarNode(_Tag.yaml("null"), value: null); | 110 return new _ScalarNode(_Tag.yaml("null"), value: null); |
| 111 } | 111 } |
| 112 | 112 |
| 113 /** Parses a boolean scalar. */ | 113 /** Parses a boolean scalar. */ |
| 114 _ScalarNode parseBool(String content) { | 114 _ScalarNode parseBool(String content) { |
| 115 var match = new RegExp("^(?:(true|True|TRUE)|(false|False|FALSE))\$"). | 115 var match = const RegExp("^(?:(true|True|TRUE)|(false|False|FALSE))\$"). |
| 116 firstMatch(content); | 116 firstMatch(content); |
| 117 if (match == null) return null; | 117 if (match == null) return null; |
| 118 return new _ScalarNode(_Tag.yaml("bool"), value: match.group(1) != null); | 118 return new _ScalarNode(_Tag.yaml("bool"), value: match.group(1) != null); |
| 119 } | 119 } |
| 120 | 120 |
| 121 /** Parses an integer scalar. */ | 121 /** Parses an integer scalar. */ |
| 122 _ScalarNode parseInt(String content) { | 122 _ScalarNode parseInt(String content) { |
| 123 var match = new RegExp("^[-+]?[0-9]+\$").firstMatch(content); | 123 var match = const RegExp("^[-+]?[0-9]+\$").firstMatch(content); |
| 124 if (match != null) { | 124 if (match != null) { |
| 125 return new _ScalarNode(_Tag.yaml("int"), | 125 return new _ScalarNode(_Tag.yaml("int"), |
| 126 value: Math.parseInt(match.group(0))); | 126 value: Math.parseInt(match.group(0))); |
| 127 } | 127 } |
| 128 | 128 |
| 129 match = new RegExp("^0o([0-7]+)\$").firstMatch(content); | 129 match = const RegExp("^0o([0-7]+)\$").firstMatch(content); |
| 130 if (match != null) { | 130 if (match != null) { |
| 131 // TODO(nweiz): clean this up when Dart can parse an octal string | 131 // TODO(nweiz): clean this up when Dart can parse an octal string |
| 132 var n = 0; | 132 var n = 0; |
| 133 for (var c in match.group(1).charCodes) { | 133 for (var c in match.group(1).charCodes) { |
| 134 n *= 8; | 134 n *= 8; |
| 135 n += c - 48; | 135 n += c - 48; |
| 136 } | 136 } |
| 137 return new _ScalarNode(_Tag.yaml("int"), value: n); | 137 return new _ScalarNode(_Tag.yaml("int"), value: n); |
| 138 } | 138 } |
| 139 | 139 |
| 140 match = new RegExp("^0x[0-9a-fA-F]+\$").firstMatch(content); | 140 match = const RegExp("^0x[0-9a-fA-F]+\$").firstMatch(content); |
| 141 if (match != null) { | 141 if (match != null) { |
| 142 return new _ScalarNode(_Tag.yaml("int"), | 142 return new _ScalarNode(_Tag.yaml("int"), |
| 143 value: Math.parseInt(match.group(0))); | 143 value: Math.parseInt(match.group(0))); |
| 144 } | 144 } |
| 145 | 145 |
| 146 return null; | 146 return null; |
| 147 } | 147 } |
| 148 | 148 |
| 149 /** Parses a floating-point scalar. */ | 149 /** Parses a floating-point scalar. */ |
| 150 _ScalarNode parseFloat(String content) { | 150 _ScalarNode parseFloat(String content) { |
| 151 var match = new RegExp( | 151 var match = const RegExp( |
| 152 "^[-+]?(\.[0-9]+|[0-9]+(\.[0-9]*)?)([eE][-+]?[0-9]+)?\$"). | 152 "^[-+]?(\.[0-9]+|[0-9]+(\.[0-9]*)?)([eE][-+]?[0-9]+)?\$"). |
| 153 firstMatch(content); | 153 firstMatch(content); |
| 154 if (match != null) { | 154 if (match != null) { |
| 155 // YAML allows floats of the form "0.", but Dart does not. Fix up those | 155 // YAML allows floats of the form "0.", but Dart does not. Fix up those |
| 156 // floats by removing the trailing dot. | 156 // floats by removing the trailing dot. |
| 157 var matchStr = match.group(0).replaceAll(new RegExp(r"\.$"), ""); | 157 var matchStr = match.group(0).replaceAll(new RegExp(r"\.$"), ""); |
| 158 return new _ScalarNode(_Tag.yaml("float"), | 158 return new _ScalarNode(_Tag.yaml("float"), |
| 159 value: Math.parseDouble(matchStr)); | 159 value: Math.parseDouble(matchStr)); |
| 160 } | 160 } |
| 161 | 161 |
| 162 match = new RegExp("^([+-]?)\.(inf|Inf|INF)\$").firstMatch(content); | 162 match = const RegExp("^([+-]?)\.(inf|Inf|INF)\$").firstMatch(content); |
| 163 if (match != null) { | 163 if (match != null) { |
| 164 var infinityStr = match.group(1) == "-" ? "-Infinity" : "Infinity"; | 164 var infinityStr = match.group(1) == "-" ? "-Infinity" : "Infinity"; |
| 165 return new _ScalarNode(_Tag.yaml("float"), | 165 return new _ScalarNode(_Tag.yaml("float"), |
| 166 value: Math.parseDouble(infinityStr)); | 166 value: Math.parseDouble(infinityStr)); |
| 167 } | 167 } |
| 168 | 168 |
| 169 match = new RegExp("^\.(nan|NaN|NAN)\$").firstMatch(content); | 169 match = const RegExp("^\.(nan|NaN|NAN)\$").firstMatch(content); |
| 170 if (match != null) { | 170 if (match != null) { |
| 171 return new _ScalarNode(_Tag.yaml("float"), | 171 return new _ScalarNode(_Tag.yaml("float"), |
| 172 value: Math.parseDouble("NaN")); | 172 value: Math.parseDouble("NaN")); |
| 173 } | 173 } |
| 174 | 174 |
| 175 return null; | 175 return null; |
| 176 } | 176 } |
| 177 | 177 |
| 178 /** Parses a string scalar. */ | 178 /** Parses a string scalar. */ |
| 179 _ScalarNode parseString(String content) => | 179 _ScalarNode parseString(String content) => |
| 180 new _ScalarNode(_Tag.yaml("str"), value: content); | 180 new _ScalarNode(_Tag.yaml("str"), value: content); |
| 181 } | 181 } |
| OLD | NEW |