Exemple #1
0
 public void VerifyTokenWithLocalTags()
 {
     AssertSequenceOfEventsFrom(Yaml.ParserForResource("local-tags.yaml"),
                                StreamStart,
                                DocumentStart(Explicit),
                                BlockMappingStart.T("!MyObject").Explicit,
                                PlainScalar("a"),
                                PlainScalar("1.0"),
                                PlainScalar("b"),
                                PlainScalar("42"),
                                PlainScalar("c"),
                                PlainScalar("-7"),
                                MappingEnd,
                                DocumentEnd(Implicit),
                                StreamEnd);
 }
Exemple #2
0
 public void VerifyTokenWithMultiDocTag()
 {
     AssertSequenceOfEventsFrom(Yaml.ParserForResource("multi-doc-tag.yaml"),
                                StreamStart,
                                DocumentStart(Explicit, Version(1, 1),
                                              TagDirective("!x!", "tag:example.com,2014:"),
                                              TagDirective("!", "!"),
                                              TagDirective("!!", TagYaml)),
                                BlockMappingStart.T("tag:example.com,2014:foo").Explicit,
                                PlainScalar("x"),
                                PlainScalar("0"),
                                MappingEnd,
                                DocumentEnd(Implicit),
                                DocumentStart(Explicit),
                                BlockMappingStart.T("tag:example.com,2014:bar").Explicit,
                                PlainScalar("x"),
                                PlainScalar("1"),
                                MappingEnd,
                                DocumentEnd(Implicit),
                                StreamEnd);
 }
Exemple #3
0
		/// <summary>
		/// Push the current indentation level to the stack and set the new level
		/// the current column is greater than the indentation level.  In this case,
		/// append or insert the specified token into the token queue.
		/// </summary>
		private void RollIndent(int column, int number, bool isSequence, Mark position)
		{
			// In the flow context, do nothing.

			if (flowLevel > 0)
			{
				return;
			}

			if (indent < column)
			{

				// Push the current indentation level to the stack and set the new
				// indentation level.


				indents.Push(indent);

				indent = column;

				// Create a token and insert it into the queue.

				Token token;
				if (isSequence)
				{
					token = new BlockSequenceStart(position, position);
				}
				else
				{
					token = new BlockMappingStart(position, position);
				}

				if (number == -1)
				{
					tokens.Enqueue(token);
				}
				else
				{
					tokens.Insert(number - tokensParsed, token);
				}
			}
		}
Exemple #4
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.");
            }
        }
        private ParsingEvent ParseNode(bool isBlock, bool isIndentlessSequence)
        {
            YamlDotNet.Core.Tokens.AnchorAlias anchorAlias = GetCurrentToken() as YamlDotNet.Core.Tokens.AnchorAlias;
            if (anchorAlias != null)
            {
                state = states.Pop();
                ParsingEvent result = new YamlDotNet.Core.Events.AnchorAlias(anchorAlias.Value, anchorAlias.Start, anchorAlias.End);
                Skip();
                return(result);
            }
            Mark   start  = GetCurrentToken().Start;
            Anchor anchor = null;

            YamlDotNet.Core.Tokens.Tag tag = null;
            while (true)
            {
                if (anchor == null && (anchor = (GetCurrentToken() as Anchor)) != null)
                {
                    Skip();
                }
                else
                {
                    if (tag != null || (tag = (GetCurrentToken() as YamlDotNet.Core.Tokens.Tag)) == null)
                    {
                        break;
                    }
                    Skip();
                }
            }
            string text = null;

            if (tag != null)
            {
                if (string.IsNullOrEmpty(tag.Handle))
                {
                    text = tag.Suffix;
                }
                else
                {
                    if (!tagDirectives.Contains(tag.Handle))
                    {
                        throw new SemanticErrorException(tag.Start, tag.End, "While parsing a node, find undefined tag handle.");
                    }
                    text = tagDirectives[tag.Handle].Prefix + tag.Suffix;
                }
            }
            if (string.IsNullOrEmpty(text))
            {
                text = null;
            }
            string text2 = (anchor == null) ? null : ((!string.IsNullOrEmpty(anchor.Value)) ? anchor.Value : null);
            bool   flag  = string.IsNullOrEmpty(text);

            if (isIndentlessSequence && GetCurrentToken() is BlockEntry)
            {
                state = ParserState.IndentlessSequenceEntry;
                return(new SequenceStart(text2, text, flag, SequenceStyle.Block, start, GetCurrentToken().End));
            }
            YamlDotNet.Core.Tokens.Scalar scalar = GetCurrentToken() as YamlDotNet.Core.Tokens.Scalar;
            if (scalar != null)
            {
                bool isPlainImplicit  = false;
                bool isQuotedImplicit = false;
                if ((scalar.Style == ScalarStyle.Plain && text == null) || text == "!")
                {
                    isPlainImplicit = true;
                }
                else if (text == null)
                {
                    isQuotedImplicit = true;
                }
                state = states.Pop();
                ParsingEvent result2 = new YamlDotNet.Core.Events.Scalar(text2, text, scalar.Value, scalar.Style, isPlainImplicit, isQuotedImplicit, start, scalar.End);
                Skip();
                return(result2);
            }
            FlowSequenceStart flowSequenceStart = GetCurrentToken() as FlowSequenceStart;

            if (flowSequenceStart != null)
            {
                state = ParserState.FlowSequenceFirstEntry;
                return(new SequenceStart(text2, text, flag, SequenceStyle.Flow, start, flowSequenceStart.End));
            }
            FlowMappingStart flowMappingStart = GetCurrentToken() as FlowMappingStart;

            if (flowMappingStart != null)
            {
                state = ParserState.FlowMappingFirstKey;
                return(new MappingStart(text2, text, flag, MappingStyle.Flow, start, flowMappingStart.End));
            }
            if (isBlock)
            {
                BlockSequenceStart blockSequenceStart = GetCurrentToken() as BlockSequenceStart;
                if (blockSequenceStart != null)
                {
                    state = ParserState.BlockSequenceFirstEntry;
                    return(new SequenceStart(text2, text, flag, SequenceStyle.Block, start, blockSequenceStart.End));
                }
                BlockMappingStart blockMappingStart = GetCurrentToken() as BlockMappingStart;
                if (blockMappingStart != null)
                {
                    state = ParserState.BlockMappingFirstKey;
                    return(new MappingStart(text2, text, flag, MappingStyle.Block, start, GetCurrentToken().End));
                }
            }
            if (text2 != null || tag != null)
            {
                state = states.Pop();
                return(new YamlDotNet.Core.Events.Scalar(text2, text, string.Empty, ScalarStyle.Plain, flag, false, start, GetCurrentToken().End));
            }
            Token token = GetCurrentToken();

            throw new SemanticErrorException(token.Start, token.End, "While parsing a node, did not find expected node content.");
        }