| 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 part of yaml; | 5 part of yaml; |
| 6 | 6 |
| 7 /// Takes a parsed YAML document (what the spec calls the "serialization tree") | 7 /// Takes a parsed YAML document (what the spec calls the "serialization tree") |
| 8 /// and resolves aliases, resolves tags, and parses scalars to produce the | 8 /// and resolves aliases, resolves tags, and parses scalars to produce the |
| 9 /// "representation graph". | 9 /// "representation graph". |
| 10 class _Composer extends _Visitor { | 10 class _Composer extends _Visitor { |
| (...skipping 97 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 108 firstMatch(content); | 108 firstMatch(content); |
| 109 if (match == null) return null; | 109 if (match == null) return null; |
| 110 return new _ScalarNode(_Tag.yaml("bool"), value: match.group(1) != null); | 110 return new _ScalarNode(_Tag.yaml("bool"), value: match.group(1) != null); |
| 111 } | 111 } |
| 112 | 112 |
| 113 /// Parses an integer scalar. | 113 /// Parses an integer scalar. |
| 114 _ScalarNode parseInt(String content) { | 114 _ScalarNode parseInt(String content) { |
| 115 var match = new RegExp("^[-+]?[0-9]+\$").firstMatch(content); | 115 var match = new RegExp("^[-+]?[0-9]+\$").firstMatch(content); |
| 116 if (match != null) { | 116 if (match != null) { |
| 117 return new _ScalarNode(_Tag.yaml("int"), | 117 return new _ScalarNode(_Tag.yaml("int"), |
| 118 value: Math.parseInt(match.group(0))); | 118 value: int.parse(match.group(0))); |
| 119 } | 119 } |
| 120 | 120 |
| 121 match = new RegExp("^0o([0-7]+)\$").firstMatch(content); | 121 match = new RegExp("^0o([0-7]+)\$").firstMatch(content); |
| 122 if (match != null) { | 122 if (match != null) { |
| 123 // TODO(nweiz): clean this up when Dart can parse an octal string | 123 // TODO(nweiz): clean this up when Dart can parse an octal string |
| 124 var n = 0; | 124 var n = 0; |
| 125 for (var c in match.group(1).charCodes) { | 125 for (var c in match.group(1).charCodes) { |
| 126 n *= 8; | 126 n *= 8; |
| 127 n += c - 48; | 127 n += c - 48; |
| 128 } | 128 } |
| 129 return new _ScalarNode(_Tag.yaml("int"), value: n); | 129 return new _ScalarNode(_Tag.yaml("int"), value: n); |
| 130 } | 130 } |
| 131 | 131 |
| 132 match = new RegExp("^0x[0-9a-fA-F]+\$").firstMatch(content); | 132 match = new RegExp("^0x[0-9a-fA-F]+\$").firstMatch(content); |
| 133 if (match != null) { | 133 if (match != null) { |
| 134 return new _ScalarNode(_Tag.yaml("int"), | 134 return new _ScalarNode(_Tag.yaml("int"), |
| 135 value: Math.parseInt(match.group(0))); | 135 value: int.parse(match.group(0))); |
| 136 } | 136 } |
| 137 | 137 |
| 138 return null; | 138 return null; |
| 139 } | 139 } |
| 140 | 140 |
| 141 /// Parses a floating-point scalar. | 141 /// Parses a floating-point scalar. |
| 142 _ScalarNode parseFloat(String content) { | 142 _ScalarNode parseFloat(String content) { |
| 143 var match = new RegExp( | 143 var match = new RegExp( |
| 144 "^[-+]?(\.[0-9]+|[0-9]+(\.[0-9]*)?)([eE][-+]?[0-9]+)?\$"). | 144 "^[-+]?(\.[0-9]+|[0-9]+(\.[0-9]*)?)([eE][-+]?[0-9]+)?\$"). |
| 145 firstMatch(content); | 145 firstMatch(content); |
| 146 if (match != null) { | 146 if (match != null) { |
| 147 // YAML allows floats of the form "0.", but Dart does not. Fix up those | 147 // YAML allows floats of the form "0.", but Dart does not. Fix up those |
| 148 // floats by removing the trailing dot. | 148 // floats by removing the trailing dot. |
| 149 var matchStr = match.group(0).replaceAll(new RegExp(r"\.$"), ""); | 149 var matchStr = match.group(0).replaceAll(new RegExp(r"\.$"), ""); |
| 150 return new _ScalarNode(_Tag.yaml("float"), | 150 return new _ScalarNode(_Tag.yaml("float"), |
| 151 value: Math.parseDouble(matchStr)); | 151 value: double.parse(matchStr)); |
| 152 } | 152 } |
| 153 | 153 |
| 154 match = new RegExp("^([+-]?)\.(inf|Inf|INF)\$").firstMatch(content); | 154 match = new RegExp("^([+-]?)\.(inf|Inf|INF)\$").firstMatch(content); |
| 155 if (match != null) { | 155 if (match != null) { |
| 156 var infinityStr = match.group(1) == "-" ? "-Infinity" : "Infinity"; | 156 var infinityStr = match.group(1) == "-" ? "-Infinity" : "Infinity"; |
| 157 return new _ScalarNode(_Tag.yaml("float"), | 157 return new _ScalarNode(_Tag.yaml("float"), |
| 158 value: Math.parseDouble(infinityStr)); | 158 value: double.parse(infinityStr)); |
| 159 } | 159 } |
| 160 | 160 |
| 161 match = new RegExp("^\.(nan|NaN|NAN)\$").firstMatch(content); | 161 match = new RegExp("^\.(nan|NaN|NAN)\$").firstMatch(content); |
| 162 if (match != null) { | 162 if (match != null) { |
| 163 return new _ScalarNode(_Tag.yaml("float"), | 163 return new _ScalarNode(_Tag.yaml("float"), |
| 164 value: Math.parseDouble("NaN")); | 164 value: double.parse("NaN")); |
| 165 } | 165 } |
| 166 | 166 |
| 167 return null; | 167 return null; |
| 168 } | 168 } |
| 169 | 169 |
| 170 /// Parses a string scalar. | 170 /// Parses a string scalar. |
| 171 _ScalarNode parseString(String content) => | 171 _ScalarNode parseString(String content) => |
| 172 new _ScalarNode(_Tag.yaml("str"), value: content); | 172 new _ScalarNode(_Tag.yaml("str"), value: content); |
| 173 } | 173 } |
| OLD | NEW |