Exemple #1
0
        private void Load(IParser parser, DocumentLoadingState state)
        {
            MappingStart mappingStart = parser.Expect <MappingStart>();

            Load(mappingStart, state);
            Style = mappingStart.Style;
            bool flag = false;

            while (!parser.Accept <MappingEnd>())
            {
                YamlNode yamlNode  = YamlNode.ParseNode(parser, state);
                YamlNode yamlNode2 = YamlNode.ParseNode(parser, state);
                try
                {
                    children.Add(yamlNode, yamlNode2);
                }
                catch (ArgumentException innerException)
                {
                    throw new YamlException(yamlNode.Start, yamlNode.End, "Duplicate key", innerException);
                }
                flag |= (yamlNode is YamlAliasNode || yamlNode2 is YamlAliasNode);
            }
            if (flag)
            {
                state.AddNodeWithUnresolvedAliases(this);
            }
            parser.Expect <MappingEnd>();
        }
Exemple #2
0
 public YamlMapping()
 {
     _mappingStart = new MappingStart();
     _mappingEnd   = new MappingEnd();
     _keys         = new List <YamlElement>();
     _contents     = new Dictionary <YamlElement, YamlElement>();
 }
        internal YamlMappingNode(EventReader events, DocumentLoadingState state)
        {
            this.children = new Dictionary <YamlNode, YamlNode>();
            MappingStart yamlEvent = events.Expect <MappingStart>();

            base.Load(yamlEvent, state);
            bool flag = false;

            while (!events.Accept <MappingEnd>())
            {
                YamlNode key   = ParseNode(events, state);
                YamlNode node2 = ParseNode(events, state);
                try
                {
                    this.children.Add(key, node2);
                }
                catch (ArgumentException exception)
                {
                    throw new YamlException(key.Start, key.End, "Duplicate key", exception);
                }
                flag |= (key is YamlAliasNode) || (node2 is YamlAliasNode);
            }
            if (flag)
            {
                state.AddNodeWithUnresolvedAliases(this);
            }
            events.Expect <MappingEnd>();
        }
Exemple #4
0
 public void Emit(ParsingEvent @event)
 {
     if (@event is MappingStart mapping)
     {
         @event = new MappingStart(mapping.Anchor, mapping.Tag, false, mapping.Style, mapping.Start, mapping.End);
     }
     _next.Emit(@event);
 }
    public void Emit(ParsingEvent @event)
    {
        var mapping = @event as MappingStart;

        if (mapping != null)
        {
            @event = new MappingStart(mapping.Anchor, mapping.Tag, false, mapping.Style, mapping.Start, mapping.End);
        }
        next.Emit(@event);
    }
Exemple #6
0
        /// <summary>
        /// Initializes a new instance of the <see cref="YamlMappingNode"/> class.
        /// </summary>
        /// <param name="events">The events.</param>
        /// <param name="state">The state.</param>
        internal YamlMappingNode(EventReader events, DocumentLoadingState state, bool overrideKeys)
        {
            MappingStart mapping = events.Expect <MappingStart>();

            Load(mapping, state);

            bool hasUnresolvedAliases = false;

            while (!events.Accept <MappingEnd>())
            {
                YamlNode key   = ParseNode(events, state, overrideKeys);
                YamlNode value = ParseNode(events, state, overrideKeys);

                try
                {
                    if (children.ContainsKey(key) && overrideKeys)
                    {
                        children[key] = value;
                    }
                    else
                    {
                        children.Add(key, value);
                    }
                }
                catch (ArgumentException err)
                {
                    throw new YamlException(key.Start, key.End, "Duplicate key", err);
                }

                hasUnresolvedAliases |= key is YamlAliasNode || value is YamlAliasNode;
            }

            if (hasUnresolvedAliases)
            {
                state.AddNodeWithUnresolvedAliases(this);
            }
#if DEBUG
            else
            {
                foreach (var child in children)
                {
                    if (child.Key is YamlAliasNode)
                    {
                        throw new InvalidOperationException("Error in alias resolution.");
                    }
                    if (child.Value is YamlAliasNode)
                    {
                        throw new InvalidOperationException("Error in alias resolution.");
                    }
                }
            }
#endif

            events.Expect <MappingEnd>();
        }
 private void AddReference(ParsingEvent item, LinkedListNode <ParsingEvent> node)
 {
     if (item is MappingStart)
     {
         MappingStart mappingStart = (MappingStart)item;
         string       anchor       = mappingStart.Anchor;
         if (!string.IsNullOrEmpty(anchor))
         {
             _references[anchor] = node;
         }
     }
 }
Exemple #8
0
        bool INodeDeserializer.Deserialize(IParser parser, Type expectedType, Func <IParser, Type, object> nestedObjectDeserializer, out object value)
        {
            MappingStart mappingStart = parser.Allow <MappingStart>();

            if (mappingStart == null)
            {
                value = null;
                return(false);
            }
            value = _objectFactory.Create(expectedType);
            while (!parser.Accept <MappingEnd>())
            {
                Scalar scalar = parser.Expect <Scalar>();
                IPropertyDescriptor property = _typeDescriptor.GetProperty(expectedType, null, scalar.Value, _ignoreUnmatched);
                if (property == null)
                {
                    if (_unmatchedLogFn != null)
                    {
                        _unmatchedLogFn(string.Format("{2} Found a property '{0}' on a type '{1}', but that type doesn't have that property!", scalar.Value, expectedType.FullName, parser.Current.Start));
                    }
                    parser.SkipThisAndNestedEvents();
                }
                else
                {
                    object        obj          = nestedObjectDeserializer(parser, property.Type);
                    IValuePromise valuePromise = obj as IValuePromise;
                    if (valuePromise == null)
                    {
                        object value2 = TypeConverter.ChangeType(obj, property.Type);
                        property.Write(value, value2);
                    }
                    else
                    {
                        object valueRef = value;
                        valuePromise.ValueAvailable += delegate(object v)
                        {
                            object value3 = TypeConverter.ChangeType(v, property.Type);
                            property.Write(valueRef, value3);
                        };
                    }
                }
            }
            parser.Expect <MappingEnd>();
            return(true);
        }
Exemple #9
0
        YamlMapping(MappingStart mappingStart, MappingEnd mappingEnd, List <YamlElement> keys, Dictionary <YamlElement, YamlElement> contents, YamlNodeTracker tracker)
        {
            Tracker = tracker;

            MappingStart     = mappingStart;
            this._mappingEnd = mappingEnd;

            if (Tracker == null)
            {
                _keys     = keys;
                _contents = contents;
            }
            else
            {
                _keys     = new List <YamlElement>();
                _contents = new Dictionary <YamlElement, YamlElement>();
                foreach (var key in keys)
                {
                    Add(key, contents[key]);
                }
            }
        }
        private ParsingEvent ParseFlowSequenceEntry(bool isFirst)
        {
            if (isFirst)
            {
                GetCurrentToken();
                Skip();
            }
            ParsingEvent result;

            if (!(GetCurrentToken() is FlowSequenceEnd))
            {
                if (!isFirst)
                {
                    if (!(GetCurrentToken() is FlowEntry))
                    {
                        Token token = GetCurrentToken();
                        throw new SemanticErrorException(token.Start, token.End, "While parsing a flow sequence, did not find expected ',' or ']'.");
                    }
                    Skip();
                }
                if (GetCurrentToken() is Key)
                {
                    state  = ParserState.FlowSequenceEntryMappingKey;
                    result = new MappingStart(null, null, true, MappingStyle.Flow);
                    Skip();
                    return(result);
                }
                if (!(GetCurrentToken() is FlowSequenceEnd))
                {
                    states.Push(ParserState.FlowSequenceEntry);
                    return(ParseNode(false, false));
                }
            }
            state  = states.Pop();
            result = new SequenceEnd(GetCurrentToken().Start, GetCurrentToken().End);
            Skip();
            return(result);
        }
Exemple #11
0
        private ParsingEvent ParseFlowSequenceEntry(bool isFirst)
        {
            ParsingEvent event2;

            if (isFirst)
            {
                this.GetCurrentToken();
                this.Skip();
            }
            if (!(this.GetCurrentToken() is FlowSequenceEnd))
            {
                if (!isFirst)
                {
                    if (!(this.GetCurrentToken() is FlowEntry))
                    {
                        Token currentToken = this.GetCurrentToken();
                        throw new SemanticErrorException(currentToken.Start, currentToken.End, "While parsing a flow sequence, did not find expected ',' or ']'.");
                    }
                    this.Skip();
                }
                if (this.GetCurrentToken() is Key)
                {
                    this.state = ParserState.FlowSequenceEntryMappingKey;
                    event2     = new MappingStart(null, null, true, MappingStyle.Flow);
                    this.Skip();
                    return(event2);
                }
                if (!(this.GetCurrentToken() is FlowSequenceEnd))
                {
                    this.states.Push(ParserState.FlowSequenceEntry);
                    return(this.ParseNode(false, false));
                }
            }
            this.state = this.states.Pop();
            event2     = new SequenceEnd(this.GetCurrentToken().Start, this.GetCurrentToken().End);
            this.Skip();
            return(event2);
        }
Exemple #12
0
 void IParsingEventVisitor.Visit(MappingStart e)
 {
     clonedEvent = new MappingStart(null, e.Tag, e.IsImplicit, e.Style, e.Start, e.End);
 }
Exemple #13
0
 void IParsingEventVisitor.Visit(MappingStart e)
 {
     clonedEvent = new MappingStart(AnchorName.Empty, e.Tag, e.IsImplicit, e.Style, e.Start, e.End);
 }
Exemple #14
0
 public void Visit(MappingStart e)
 {
     AddBranch(new Map());
 }
 public void Visit(MappingStart e)
 {
     _emitter.Emit(e);
     AddBranch(new Map());
 }
Exemple #16
0
 /// <summary>
 /// Gets the default tag for a <see cref="MappingStart"/> event.
 /// </summary>
 /// <param name="nodeEvent">The node event.</param>
 /// <returns>The default tag for a map.</returns>
 protected abstract string GetDefaultTag(MappingStart nodeEvent);
Exemple #17
0
 protected override string GetDefaultTag(MappingStart nodeEvent)
 {
     return(MapShortTag);
 }
Exemple #18
0
        private object DeserializeProperties(EventReader reader, Type type, DeserializationContext context)
        {
            MappingStart mapping = reader.Expect <MappingStart>();

            type = GetType(mapping.Tag, type, context.Options.Mappings);
            object result = Activator.CreateInstance(type);

            IDictionary dictionary = result as IDictionary;

            if (dictionary != null)
            {
                Type keyType   = typeof(object);
                Type valueType = typeof(object);

                foreach (var interfaceType in result.GetType().GetInterfaces())
                {
                    if (interfaceType.IsGenericType && interfaceType.GetGenericTypeDefinition() == typeof(IDictionary <,>))
                    {
                        Type[] genericArguments = interfaceType.GetGenericArguments();
                        Debug.Assert(genericArguments.Length == 2, "IDictionary<,> must contain two generic arguments.");
                        keyType   = genericArguments[0];
                        valueType = genericArguments[1];
                        break;
                    }
                }

                while (!reader.Accept <MappingEnd>())
                {
                    object key   = DeserializeValue(reader, keyType, context);
                    object value = DeserializeValue(reader, valueType, context);
                    dictionary.Add(key, value);
                }
            }
            else
            {
                while (!reader.Accept <MappingEnd>())
                {
                    Scalar key = reader.Expect <Scalar>();

                    bool isOverriden = false;
                    if (context.Options != null)
                    {
                        var deserializer = context.Options.Overrides.GetOverride(type, key.Value);
                        if (deserializer != null)
                        {
                            isOverriden = true;
                            deserializer(result, reader);
                        }
                    }

                    if (!isOverriden)
                    {
                        PropertyInfo property = type.GetProperty(key.Value, BindingFlags.Instance | BindingFlags.Public);
                        if (property == null)
                        {
                            Console.WriteLine(key);

                            throw new SerializationException(
                                      string.Format(
                                          CultureInfo.InvariantCulture,
                                          "Property '{0}' not found on type '{1}'",
                                          key.Value,
                                          type.FullName
                                          )
                                      );
                        }
                        property.SetValue(result, DeserializeValue(reader, property.PropertyType, context), null);
                    }
                }
            }
            reader.Expect <MappingEnd>();

            AddAnchoredObject(mapping, result, context.Anchors);

            return(result);
        }