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