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