Exemple #1
0
        public static bool TryConstructYamlBool(BaseConstructor /*!*/ ctor, Node /*!*/ node, out bool result)
        {
            var b = ResolverScanner.ToBool(ctor.ConstructScalar(node));

            if (b != null)
            {
                result = b.Value;
                return(true);
            }
            else
            {
                result = false;
                return(false);
            }
        }
Exemple #2
0
 public static string Resolve(Type kind, string value, bool @implicit)
 {
     if (kind == typeof(ScalarNode) && @implicit)
     {
         string resolv = ResolverScanner.Recognize(value);
         if (resolv != null)
         {
             return(resolv);
         }
     }
     if (kind == typeof(ScalarNode))
     {
         return("tag:yaml.org,2002:str");
     }
     else if (kind == typeof(SequenceNode))
     {
         return("tag:yaml.org,2002:seq");
     }
     else if (kind == typeof(MappingNode))
     {
         return("tag:yaml.org,2002:map");
     }
     return(null);
 }
        private void SerializeNode(Node node, Node parent, object index)
        {
            while (node is LinkNode)
            {
                node = ((LinkNode)node).Linked;
            }

            string tAlias;

            _anchors.TryGetValue(node, out tAlias);

            if (_serializedNodes.ContainsKey(node) && tAlias != null)
            {
                _emitter.Emit(new AliasEvent(tAlias));
            }
            else
            {
                _serializedNodes[node] = null;
                //_resolver.descendResolver(parent, index);

                ScalarNode   scalar;
                SequenceNode seq;
                MappingNode  map;

                if ((scalar = node as ScalarNode) != null)
                {
                    string             tag   = node.Tag;
                    ScalarQuotingStyle style = scalar.Style;
                    ScalarValueType    type;
                    if (tag == null)
                    {
                        // quote an untagged sctring scalar that might be parsed as a different scalar type if not quoted:
                        if (style == ScalarQuotingStyle.None && ResolverScanner.Recognize(scalar.Value) != null)
                        {
                            style = ScalarQuotingStyle.Double;
                        }
                        type = ScalarValueType.String;
                    }
                    else if (tag == Tags.Str)
                    {
                        // omit the tag for strings that are not recognizable as other scalars:
                        if (ResolverScanner.Recognize(scalar.Value) == null)
                        {
                            tag = null;
                        }
                        type = ScalarValueType.String;
                    }
                    else if (scalar.Value == null)
                    {
                        tag  = null;
                        type = ScalarValueType.Other;
                    }
                    else
                    {
                        // omit the tag for non-string scalars whose type can be recognized from their value:
                        string detectedTag = ResolverScanner.Recognize(scalar.Value);
                        if (detectedTag != null && tag.StartsWith(detectedTag, StringComparison.Ordinal))
                        {
                            tag = null;
                        }
                        type = ScalarValueType.Other;
                    }

                    _emitter.Emit(new ScalarEvent(tAlias, tag, type, scalar.Value, style));
                }
                else if ((seq = node as SequenceNode) != null)
                {
                    _emitter.Emit(new SequenceStartEvent(tAlias, node.Tag, seq.FlowStyle));
                    int ix = 0;
                    foreach (Node n in seq.Nodes)
                    {
                        SerializeNode(n, node, ix++);
                    }
                    _emitter.Emit(SequenceEndEvent.Instance);
                }
                else if ((map = node as MappingNode) != null)
                {
                    _emitter.Emit(new MappingStartEvent(tAlias, node.Tag, map.FlowStyle));
                    foreach (KeyValuePair <Node, Node> e in map.Nodes)
                    {
                        SerializeNode(e.Key, node, null);
                        SerializeNode(e.Value, node, e.Key);
                    }
                    _emitter.Emit(MappingEndEvent.Instance);
                }
            }
        }
        private Node ComposeNode(Node parent, object index) {
            Node result;
            YamlEvent @event = _parser.PeekEvent();
            NodeEvent nodeEvent = @event as NodeEvent;

            string anchor = (nodeEvent != null) ? nodeEvent.Anchor : null;

            if (nodeEvent is AliasEvent) {
                _parser.GetEvent();
                if (!_anchors.TryGetValue(anchor, out result)) {
                    throw new ComposerException("found undefined alias: " + anchor);
                }
                return result;
            }

            result = null;
            //_resolver.descendResolver(parent, index);
            if (@event is ScalarEvent) {
                ScalarEvent ev = (ScalarEvent)_parser.GetEvent();

                string tag = ev.Tag;
                if (ev.Type == ScalarValueType.Unknown) {
                    Debug.Assert(tag == null || tag == "!");
                    tag = ResolverScanner.Recognize(ev.Value) ?? Tags.Str;
                }

                result = new ScalarNode(tag, ev.Value, ev.Style);
                if (anchor != null) {
                    AddAnchor(anchor, result);
                }
            } else if (@event is SequenceStartEvent) {
                SequenceStartEvent start = (SequenceStartEvent)_parser.GetEvent();
                SequenceNode seqResult = new SequenceNode(start.Tag != "!" ? start.Tag : null, new List<Node>(), start.FlowStyle);
                result = seqResult;
                if (anchor != null) {
                    AddAnchor(anchor, seqResult);
                }
                int ix = 0;
                while (!(_parser.PeekEvent() is SequenceEndEvent)) {
                    seqResult.Nodes.Add(ComposeNode(seqResult, ix++));
                }
                _parser.GetEvent();
            } else if (@event is MappingStartEvent) {
                MappingStartEvent start = (MappingStartEvent)_parser.GetEvent();
                MappingNode mapResult = new MappingNode(start.Tag != "!" ? start.Tag : null, new Dictionary<Node, Node>(), start.FlowStyle);
                result = mapResult;
                if (anchor != null) {
                    AddAnchor(anchor, result);
                }
                while (!(_parser.PeekEvent() is MappingEndEvent)) {
                    YamlEvent key = _parser.PeekEvent();
                    Node itemKey = ComposeNode(mapResult, key);
                    Node composed = ComposeNode(mapResult, itemKey);
                    if (!mapResult.Nodes.ContainsKey(itemKey)) {
                        mapResult.Nodes.Add(itemKey, composed);
                    }
                }
                _parser.GetEvent();
            }
            //_resolver.ascendResolver();
            return result;
        }