示例#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");
        }
示例#2
0
        private bool HandleAnchorAlias(LinkedListNode <ParsingEvent> node, AnchorAlias anchorAlias)
        {
            var mergedEvents = GetMappingEvents(anchorAlias.Value);

            events.AddAfter(node, mergedEvents);
            events.MarkDeleted(node);

            return(true);
        }
        private bool HandleAnchorAlias(LinkedListNode <ParsingEvent> node)
        {
            if (node == null || !(node.Value is AnchorAlias))
            {
                return(false);
            }
            AnchorAlias anchorAlias = (AnchorAlias)node.Value;
            IEnumerable <ParsingEvent> mappingEvents = GetMappingEvents(anchorAlias.Value);

            _events.AddAfter(node, mappingEvents);
            _events.MarkDeleted(node);
            return(true);
        }
        public object DeserializeValue(EventReader reader, Type expectedType, SerializerState state, IValueDeserializer nestedObjectDeserializer)
        {
            AnchorAlias alias = reader.Allow <AnchorAlias>();

            if (alias != null)
            {
                ValuePromise promise;
                AliasState   state2 = state.Get <AliasState>();
                if (!state2.TryGetValue(alias.Value, out promise))
                {
                    promise = new ValuePromise(alias);
                    state2.Add(alias.Value, promise);
                }
                return(!promise.HasValue ? promise : promise.Value);
            }
            string    key    = null;
            NodeEvent event2 = reader.Peek <NodeEvent>();

            if ((event2 != null) && !string.IsNullOrEmpty(event2.Anchor))
            {
                key = event2.Anchor;
            }
            object obj2 = this.innerDeserializer.DeserializeValue(reader, expectedType, state, nestedObjectDeserializer);

            if (key != null)
            {
                ValuePromise promise2;
                AliasState   state3 = state.Get <AliasState>();
                if (!state3.TryGetValue(key, out promise2))
                {
                    state3.Add(key, new ValuePromise(obj2));
                }
                else
                {
                    if (promise2.HasValue)
                    {
                        throw new DuplicateAnchorException(event2.Start, event2.End, $"Anchor '{key}' already defined");
                    }
                    promise2.Value = obj2;
                }
            }
            return(obj2);
        }
        public object DeserializeValue(IParser parser, Type expectedType, SerializerState state, IValueDeserializer nestedObjectDeserializer)
        {
            AnchorAlias anchorAlias = parser.Allow <AnchorAlias>();

            if (anchorAlias != null)
            {
                AliasState aliasState = state.Get <AliasState>();
                if (!aliasState.TryGetValue(anchorAlias.Value, out ValuePromise value))
                {
                    value = new ValuePromise(anchorAlias);
                    aliasState.Add(anchorAlias.Value, value);
                }
                return((!value.HasValue) ? value : value.Value);
            }
            string    text      = null;
            NodeEvent nodeEvent = parser.Peek <NodeEvent>();

            if (nodeEvent != null && !string.IsNullOrEmpty(nodeEvent.Anchor))
            {
                text = nodeEvent.Anchor;
            }
            object obj = innerDeserializer.DeserializeValue(parser, expectedType, state, nestedObjectDeserializer);

            if (text != null)
            {
                AliasState aliasState2 = state.Get <AliasState>();
                if (!aliasState2.TryGetValue(text, out ValuePromise value2))
                {
                    aliasState2.Add(text, new ValuePromise(obj));
                }
                else if (!value2.HasValue)
                {
                    value2.Value = obj;
                }
                else
                {
                    aliasState2[text] = new ValuePromise(obj);
                }
            }
            return(obj);
        }
示例#6
0
 public ValuePromise(AnchorAlias alias)
 {
     this.Alias = alias;
 }
示例#7
0
        /// <summary>
        /// Parse the productions:
        /// block_node_or_indentless_sequence    ::=
        ///                          ALIAS
        ///                          *****
        ///                          | properties (block_content | indentless_block_sequence)?
        ///                            **********  *
        ///                          | block_content | indentless_block_sequence
        ///                            *
        /// block_node           ::= ALIAS
        ///                          *****
        ///                          | properties block_content?
        ///                            ********** *
        ///                          | block_content
        ///                            *
        /// flow_node            ::= ALIAS
        ///                          *****
        ///                          | properties flow_content?
        ///                            ********** *
        ///                          | flow_content
        ///                            *
        /// properties           ::= TAG ANCHOR? | ANCHOR TAG?
        ///                          *************************
        /// block_content        ::= block_collection | flow_collection | SCALAR
        ///                                                               ******
        /// flow_content         ::= flow_collection | SCALAR
        ///                                            ******
        /// </summary>
        private Event ParseNode(bool isBlock, bool isIndentlessSequence)
        {
            AnchorAlias alias = GetCurrentToken() as AnchorAlias;

            if (alias != null)
            {
                state = states.Pop();
                Event evt = new Events.AnchorAlias(alias.Value, alias.Start, alias.End);
                Skip();
                return(evt);
            }

            Mark start = GetCurrentToken().Start;

            Anchor anchor = null;
            Tag    tag    = null;

            // The anchor and the tag can be in any order. This loop repeats at most twice.
            while (true)
            {
                if (anchor == null && (anchor = GetCurrentToken() as Anchor) != null)
                {
                    Skip();
                }
                else if (tag == null && (tag = GetCurrentToken() as Tag) != null)
                {
                    Skip();
                }
                else
                {
                    break;
                }
            }

            string tagName = null;

            if (tag != null)
            {
                if (string.IsNullOrEmpty(tag.Handle))
                {
                    tagName = tag.Suffix;
                }
                else if (tagDirectives.Contains(tag.Handle))
                {
                    tagName = string.Concat(tagDirectives[tag.Handle].Prefix, tag.Suffix);
                }
                else
                {
                    throw new SemanticErrorException(tag.Start, tag.End, "While parsing a node, find undefined tag handle.");
                }
            }
            if (string.IsNullOrEmpty(tagName))
            {
                tagName = null;
            }

            string anchorName = anchor != null?string.IsNullOrEmpty(anchor.Value) ? null : anchor.Value : null;

            bool isImplicit = string.IsNullOrEmpty(tagName);

            if (isIndentlessSequence && GetCurrentToken() is BlockEntry)
            {
                state = ParserState.YAML_PARSE_INDENTLESS_SEQUENCE_ENTRY_STATE;

                return(new Events.SequenceStart(
                           anchorName,
                           tagName,
                           isImplicit,
                           SequenceStyle.Block,
                           start,
                           GetCurrentToken().End
                           ));
            }
            else
            {
                Scalar scalar = GetCurrentToken() as Scalar;
                if (scalar != null)
                {
                    bool isPlainImplicit  = false;
                    bool isQuotedImplicit = false;
                    if ((scalar.Style == ScalarStyle.Plain && tagName == null) || tagName == Constants.DefaultHandle)
                    {
                        isPlainImplicit = true;
                    }
                    else if (tagName == null)
                    {
                        isQuotedImplicit = true;
                    }

                    state = states.Pop();
                    Event evt = new Events.Scalar(anchorName, tagName, scalar.Value, scalar.Style, isPlainImplicit, isQuotedImplicit, start, scalar.End);

                    Skip();
                    return(evt);
                }

                FlowSequenceStart flowSequenceStart = GetCurrentToken() as FlowSequenceStart;
                if (flowSequenceStart != null)
                {
                    state = ParserState.YAML_PARSE_FLOW_SEQUENCE_FIRST_ENTRY_STATE;
                    return(new Events.SequenceStart(anchorName, tagName, isImplicit, SequenceStyle.Flow, start, flowSequenceStart.End));
                }

                FlowMappingStart flowMappingStart = GetCurrentToken() as FlowMappingStart;
                if (flowMappingStart != null)
                {
                    state = ParserState.YAML_PARSE_FLOW_MAPPING_FIRST_KEY_STATE;
                    return(new Events.MappingStart(anchorName, tagName, isImplicit, MappingStyle.Flow, start, flowMappingStart.End));
                }

                if (isBlock)
                {
                    BlockSequenceStart blockSequenceStart = GetCurrentToken() as BlockSequenceStart;
                    if (blockSequenceStart != null)
                    {
                        state = ParserState.YAML_PARSE_BLOCK_SEQUENCE_FIRST_ENTRY_STATE;
                        return(new Events.SequenceStart(anchorName, tagName, isImplicit, SequenceStyle.Block, start, blockSequenceStart.End));
                    }

                    BlockMappingStart blockMappingStart = GetCurrentToken() as BlockMappingStart;
                    if (blockMappingStart != null)
                    {
                        state = ParserState.YAML_PARSE_BLOCK_MAPPING_FIRST_KEY_STATE;
                        return(new Events.MappingStart(anchorName, tagName, isImplicit, MappingStyle.Block, start, GetCurrentToken().End));
                    }
                }

                if (anchorName != null || tag != null)
                {
                    state = states.Pop();
                    return(new Events.Scalar(anchorName, tagName, string.Empty, ScalarStyle.Plain, isImplicit, false, start, GetCurrentToken().End));
                }

                var current = GetCurrentToken();
                throw new SemanticErrorException(current.Start, current.End, "While parsing a node, did not find expected node content.");
            }
        }
示例#8
0
 void IParsingEventVisitor.Visit(AnchorAlias e)
 {
     clonedEvent = new AnchorAlias(e.Value, e.Start, e.End);
 }
示例#9
0
 public void Visit(AnchorAlias e)
 {
     _branches.Peek().Continue();
 }
        public void Visit(AnchorAlias e)
        {
            _emitter.Emit(e);

            _branches.Peek().Continue();
        }
示例#11
0
 public YamlAnchorAlias(AnchorAlias anchorAlias)
 {
     AnchorAlias = anchorAlias;
 }
示例#12
0
        private ParsingEvent ParseNode(bool isBlock, bool isIndentlessSequence)
        {
            AnchorAlias currentToken = this.GetCurrentToken() as AnchorAlias;

            if (currentToken != null)
            {
                this.state = this.states.Pop();
                ParsingEvent event2 = new AnchorAlias(currentToken.Value, currentToken.Start, currentToken.End);
                this.Skip();
                return(event2);
            }
            Mark   mark   = this.GetCurrentToken().Start;
            Anchor anchor = null;
            Tag    tag    = null;

            while (true)
            {
                if ((anchor == null) && ((anchor = this.GetCurrentToken() as Anchor) != null))
                {
                    this.Skip();
                    continue;
                }
                if ((tag != null) || ((tag = this.GetCurrentToken() as Tag) == null))
                {
                    string suffix = null;
                    if (tag != null)
                    {
                        if (string.IsNullOrEmpty(tag.Handle))
                        {
                            suffix = tag.Suffix;
                        }
                        else
                        {
                            if (!this.tagDirectives.Contains(tag.Handle))
                            {
                                throw new SemanticErrorException(tag.Start, tag.End, "While parsing a node, find undefined tag handle.");
                            }
                            suffix = this.tagDirectives[tag.Handle].Prefix + tag.Suffix;
                        }
                    }
                    if (string.IsNullOrEmpty(suffix))
                    {
                        suffix = null;
                    }
                    string str2       = (anchor == null) ? null : (!string.IsNullOrEmpty(anchor.Value) ? anchor.Value : null);
                    bool   isImplicit = string.IsNullOrEmpty(suffix);
                    if (isIndentlessSequence && (this.GetCurrentToken() is BlockEntry))
                    {
                        this.state = ParserState.IndentlessSequenceEntry;
                        return(new SequenceStart(str2, suffix, isImplicit, SequenceStyle.Block, mark, this.GetCurrentToken().End));
                    }
                    Scalar scalar = this.GetCurrentToken() as Scalar;
                    if (scalar != null)
                    {
                        bool isPlainImplicit  = false;
                        bool isQuotedImplicit = false;
                        if (((scalar.Style == ScalarStyle.Plain) && (suffix == null)) || (suffix == "!"))
                        {
                            isPlainImplicit = true;
                        }
                        else if (suffix == null)
                        {
                            isQuotedImplicit = true;
                        }
                        this.state = this.states.Pop();
                        ParsingEvent event3 = new Scalar(str2, suffix, scalar.Value, scalar.Style, isPlainImplicit, isQuotedImplicit, mark, scalar.End);
                        this.Skip();
                        return(event3);
                    }
                    FlowSequenceStart start = this.GetCurrentToken() as FlowSequenceStart;
                    if (start != null)
                    {
                        this.state = ParserState.FlowSequenceFirstEntry;
                        return(new SequenceStart(str2, suffix, isImplicit, SequenceStyle.Flow, mark, start.End));
                    }
                    FlowMappingStart start2 = this.GetCurrentToken() as FlowMappingStart;
                    if (start2 != null)
                    {
                        this.state = ParserState.FlowMappingFirstKey;
                        return(new MappingStart(str2, suffix, isImplicit, MappingStyle.Flow, mark, start2.End));
                    }
                    if (isBlock)
                    {
                        BlockSequenceStart start3 = this.GetCurrentToken() as BlockSequenceStart;
                        if (start3 != null)
                        {
                            this.state = ParserState.BlockSequenceFirstEntry;
                            return(new SequenceStart(str2, suffix, isImplicit, SequenceStyle.Block, mark, start3.End));
                        }
                        if (this.GetCurrentToken() is BlockMappingStart)
                        {
                            this.state = ParserState.BlockMappingFirstKey;
                            return(new MappingStart(str2, suffix, isImplicit, MappingStyle.Block, mark, this.GetCurrentToken().End));
                        }
                    }
                    if ((str2 == null) && (tag == null))
                    {
                        Token token = this.GetCurrentToken();
                        throw new SemanticErrorException(token.Start, token.End, "While parsing a node, did not find expected node content.");
                    }
                    this.state = this.states.Pop();
                    return(new Scalar(str2, suffix, string.Empty, ScalarStyle.Plain, isImplicit, false, mark, this.GetCurrentToken().End));
                }
                this.Skip();
            }
        }