Beispiel #1
0
        /// <summary>
        /// Parses the node represented by the next event in <paramref name="events" />.
        /// </summary>
        /// <param name="events">The events.</param>
        /// <param name="state">The state.</param>
        /// <returns>Returns the node that has been parsed.</returns>
        static internal YamlNode ParseNode(EventReader events, DocumentLoadingState state)
        {
            if (events.Accept <Scalar>())
            {
                return(new YamlScalarNode(events, state));
            }

            if (events.Accept <SequenceStart>())
            {
                return(new YamlSequenceNode(events, state));
            }

            if (events.Accept <MappingStart>())
            {
                return(new YamlMappingNode(events, state));
            }

            if (events.Accept <AnchorAlias>())
            {
                AnchorAlias alias = events.Expect <AnchorAlias>();
                return(state.GetNode(alias.Value, false, alias.Start, alias.End) ?? new YamlAliasNode(alias.Value));
            }

            throw new ArgumentException("The current event is of an unsupported type.", "events");
        }
 /// <summary>
 /// Initializes a new instance of the <see cref="YamlScalarNode"/> class.
 /// </summary>
 /// <param name="events">The events.</param>
 /// <param name="state">The state.</param>
 internal YamlScalarNode(EventReader events, DocumentLoadingState state)
 {
     Scalar scalar = events.Expect<Scalar>();
     Load(scalar, state);
     Value = scalar.Value;
     Style = scalar.Style;
 }
Beispiel #3
0
        /// <summary>
        /// Initializes a new instance of the <see cref="YamlSequenceNode"/> class.
        /// </summary>
        /// <param name="events">The events.</param>
        /// <param name="state">The state.</param>
        internal YamlSequenceNode(EventReader events, DocumentLoadingState state)
        {
            SequenceStart sequence = events.Expect <SequenceStart>();

            Load(sequence, state);

            bool hasUnresolvedAliases = false;

            while (!events.Accept <SequenceEnd>())
            {
                YamlNode child = ParseNode(events, state);
                children.Add(child);
                hasUnresolvedAliases |= child is YamlAliasNode;
            }

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

            events.Expect <SequenceEnd>();
        }
Beispiel #4
0
        /// <summary>
        /// Initializes a new instance of the <see cref="YamlDocument"/> class.
        /// </summary>
        /// <param name="events">The events.</param>
        internal YamlDocument(EventReader events)
        {
            DocumentLoadingState state = new DocumentLoadingState();

            events.Expect <DocumentStart>();

            while (!events.Accept <DocumentEnd>())
            {
                Debug.Assert(RootNode == null);
                RootNode = YamlNode.ParseNode(events, state);

                if (RootNode is YamlAliasNode)
                {
                    throw new YamlException();
                }
            }

            state.ResolveAliases();

#if DEBUG
            foreach (var node in AllNodes)
            {
                if (node is YamlAliasNode)
                {
                    throw new InvalidOperationException("Error in alias resolution.");
                }
            }
#endif

            events.Expect <DocumentEnd>();
        }
Beispiel #5
0
        /// <summary>
        /// Initializes a new instance of the <see cref="YamlScalarNode"/> class.
        /// </summary>
        /// <param name="events">The events.</param>
        /// <param name="state">The state.</param>
        internal YamlScalarNode(EventReader events, DocumentLoadingState state)
        {
            Scalar scalar = events.Expect <Scalar>();

            Load(scalar, state);
            Value = scalar.Value;
            Style = scalar.Style;
        }
Beispiel #6
0
 /// <summary>
 /// Resolves the aliases that could not be resolved when the node was created.
 /// </summary>
 /// <param name="state">The state of the document.</param>
 internal override void ResolveAliases(DocumentLoadingState state)
 {
     for (int i = 0; i < children.Count; ++i)
     {
         if (children[i] is YamlAliasNode)
         {
             children[i] = state.GetNode(children[i].Anchor, true, children[i].Start, children[i].End);
         }
     }
 }
Beispiel #7
0
 /// <summary>
 /// Loads the specified event.
 /// </summary>
 /// <param name="yamlEvent">The event.</param>
 /// <param name="state">The state of the document.</param>
 internal void Load(NodeEvent yamlEvent, DocumentLoadingState state)
 {
     Tag = yamlEvent.Tag;
     if (yamlEvent.Anchor != null)
     {
         Anchor = yamlEvent.Anchor;
         state.AddAnchor(this);
     }
     Start = yamlEvent.Start;
     End   = yamlEvent.End;
 }
Beispiel #8
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)
        {
            MappingStart mapping = events.Expect <MappingStart>();

            Load(mapping, state);

            bool hasUnresolvedAliases = false;

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

                try
                {
                    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>();
        }
Beispiel #9
0
        /// <summary>
        /// Resolves the aliases that could not be resolved when the node was created.
        /// </summary>
        /// <param name="state">The state of the document.</param>
        internal override void ResolveAliases(DocumentLoadingState state)
        {
            Dictionary <YamlNode, YamlNode> keysToUpdate   = null;
            Dictionary <YamlNode, YamlNode> valuesToUpdate = null;

            foreach (var entry in children)
            {
                if (entry.Key is YamlAliasNode)
                {
                    if (keysToUpdate == null)
                    {
                        keysToUpdate = new Dictionary <YamlNode, YamlNode>();
                    }
                    keysToUpdate.Add(entry.Key, state.GetNode(entry.Key.Anchor, true, entry.Key.Start, entry.Key.End));
                }
                if (entry.Value is YamlAliasNode)
                {
                    if (valuesToUpdate == null)
                    {
                        valuesToUpdate = new Dictionary <YamlNode, YamlNode>();
                    }
                    valuesToUpdate.Add(entry.Key, state.GetNode(entry.Value.Anchor, true, entry.Value.Start, entry.Value.End));
                }
            }
            if (valuesToUpdate != null)
            {
                foreach (var entry in valuesToUpdate)
                {
                    children[entry.Key] = entry.Value;
                }
            }
            if (keysToUpdate != null)
            {
                foreach (var entry in keysToUpdate)
                {
                    YamlNode value = children[entry.Key];
                    children.Remove(entry.Key);
                    children.Add(entry.Value, value);
                }
            }
        }
 /// <summary>
 /// Resolves the aliases that could not be resolved when the node was created.
 /// </summary>
 /// <param name="state">The state of the document.</param>
 internal override void ResolveAliases(DocumentLoadingState state)
 {
     throw new NotSupportedException("Resolving an alias on a scalar node does not make sense");
 }
Beispiel #11
0
 /// <summary>
 /// Resolves the aliases that could not be resolved when the node was created.
 /// </summary>
 /// <param name="state">The state of the document.</param>
 internal abstract void ResolveAliases(DocumentLoadingState state);
Beispiel #12
0
 /// <summary>
 /// Loads the specified event.
 /// </summary>
 /// <param name="yamlEvent">The event.</param>
 /// <param name="state">The state of the document.</param>
 internal void Load(NodeEvent yamlEvent, DocumentLoadingState state)
 {
     Tag = yamlEvent.Tag;
     if (yamlEvent.Anchor != null)
     {
         Anchor = yamlEvent.Anchor;
         state.AddAnchor(this);
     }
     Start = yamlEvent.Start;
     End = yamlEvent.End;
 }
Beispiel #13
0
        /// <summary>
        /// Parses the node represented by the next event in <paramref name="events" />.
        /// </summary>
        /// <param name="events">The events.</param>
        /// <param name="state">The state.</param>
        /// <returns>Returns the node that has been parsed.</returns>
        internal static YamlNode ParseNode(EventReader events, DocumentLoadingState state)
        {
            if (events.Accept<Scalar>())
            {
                return new YamlScalarNode(events, state);
            }

            if (events.Accept<SequenceStart>())
            {
                return new YamlSequenceNode(events, state);
            }

            if (events.Accept<MappingStart>())
            {
                return new YamlMappingNode(events, state);
            }

            if (events.Accept<AnchorAlias>())
            {
                AnchorAlias alias = events.Expect<AnchorAlias>();
                return state.GetNode(alias.Value, false, alias.Start, alias.End) ?? new YamlAliasNode(alias.Value);
            }

            throw new ArgumentException("The current event is of an unsupported type.", "events");
        }
Beispiel #14
0
 /// <summary>
 /// Resolves the aliases that could not be resolved when the node was created.
 /// </summary>
 /// <param name="state">The state of the document.</param>
 internal override void ResolveAliases(DocumentLoadingState state)
 {
     throw new NotSupportedException("Resolving an alias on a scalar node does not make sense");
 }
Beispiel #15
0
 /// <summary>
 /// Resolves the aliases that could not be resolved when the node was created.
 /// </summary>
 /// <param name="state">The state of the document.</param>
 internal abstract void ResolveAliases(DocumentLoadingState state);