Esempio n. 1
0
        /// <summary>
        /// Parse directives.
        /// </summary>
        private VersionDirective ProcessDirectives(TagDirectiveCollection tags)
        {
            VersionDirective version          = null;
            bool             hasOwnDirectives = false;

            while (true)
            {
                VersionDirective currentVersion;
                TagDirective     tag;

                if ((currentVersion = GetCurrentToken() as VersionDirective) != null)
                {
                    if (version != null)
                    {
                        throw new SemanticErrorException(currentVersion.Start, currentVersion.End, "Found duplicate %YAML directive.");
                    }

                    if (currentVersion.Version.Major != Constants.MajorVersion || currentVersion.Version.Minor != Constants.MinorVersion)
                    {
                        throw new SemanticErrorException(currentVersion.Start, currentVersion.End, "Found incompatible YAML document.");
                    }

                    version          = currentVersion;
                    hasOwnDirectives = true;
                }
                else if ((tag = GetCurrentToken() as TagDirective) != null)
                {
                    if (tags.Contains(tag.Handle))
                    {
                        throw new SemanticErrorException(tag.Start, tag.End, "Found duplicate %TAG directive.");
                    }
                    tags.Add(tag);
                    hasOwnDirectives = true;
                }
                else
                {
                    break;
                }

                Skip();
            }

            AddTagDirectives(tags, Constants.DefaultTagDirectives);

            if (hasOwnDirectives)
            {
                tagDirectives.Clear();
            }

            AddTagDirectives(tagDirectives, tags);

            return(version);
        }
        private VersionDirective ProcessDirectives(TagDirectiveCollection tags)
        {
            VersionDirective versionDirective = null;
            bool             flag             = false;

            while (true)
            {
                VersionDirective versionDirective2;
                if ((versionDirective2 = (GetCurrentToken() as VersionDirective)) != null)
                {
                    if (versionDirective != null)
                    {
                        throw new SemanticErrorException(versionDirective2.Start, versionDirective2.End, "Found duplicate %YAML directive.");
                    }
                    if (versionDirective2.Version.Major != 1 || versionDirective2.Version.Minor != 1)
                    {
                        throw new SemanticErrorException(versionDirective2.Start, versionDirective2.End, "Found incompatible YAML document.");
                    }
                    versionDirective = versionDirective2;
                    flag             = true;
                }
                else
                {
                    TagDirective tagDirective;
                    if ((tagDirective = (GetCurrentToken() as TagDirective)) == null)
                    {
                        break;
                    }
                    if (tags.Contains(tagDirective.Handle))
                    {
                        throw new SemanticErrorException(tagDirective.Start, tagDirective.End, "Found duplicate %TAG directive.");
                    }
                    tags.Add(tagDirective);
                    flag = true;
                }
                Skip();
            }
            AddTagDirectives(tags, Constants.DefaultTagDirectives);
            if (flag)
            {
                tagDirectives.Clear();
            }
            AddTagDirectives(tagDirectives, tags);
            return(versionDirective);
        }
Esempio n. 3
0
        /// <summary>
        /// Parse the productions:
        /// implicit_document    ::= block_node DOCUMENT-END*
        ///                                     *************
        /// explicit_document    ::= DIRECTIVE* DOCUMENT-START block_node? DOCUMENT-END*
        ///                                                                *************
        /// </summary>

        private Event ParseDocumentEnd()
        {
            bool isImplicit = true;
            Mark start      = GetCurrentToken().Start;
            Mark end        = start;

            if (GetCurrentToken() is DocumentEnd)
            {
                end = GetCurrentToken().End;
                Skip();
                isImplicit = false;
            }

            tagDirectives.Clear();

            state = ParserState.YAML_PARSE_DOCUMENT_START_STATE;
            return(new Events.DocumentEnd(isImplicit, start, end));
        }
Esempio n. 4
0
        /// <summary>
        /// Parse directives.
        /// </summary>
        private VersionDirective?ProcessDirectives(TagDirectiveCollection tags)
        {
            bool             hasOwnDirectives = false;
            VersionDirective?localVersion     = null;

            while (true)
            {
                if (GetCurrentToken() is VersionDirective currentVersion)
                {
                    if (version != null)
                    {
                        throw new SemanticErrorException(currentVersion.Start, currentVersion.End, "Found duplicate %YAML directive.");
                    }

                    if (currentVersion.Version.Major != Constants.MajorVersion || currentVersion.Version.Minor > Constants.MinorVersion)
                    {
                        throw new SemanticErrorException(currentVersion.Start, currentVersion.End, "Found incompatible YAML document.");
                    }

                    localVersion     = version = currentVersion;
                    hasOwnDirectives = true;
                }
                else if (GetCurrentToken() is TagDirective tag)
                {
                    if (tags.Contains(tag.Handle))
                    {
                        throw new SemanticErrorException(tag.Start, tag.End, "Found duplicate %TAG directive.");
                    }
                    tags.Add(tag);
                    hasOwnDirectives = true;
                }

                // Starting from v1.2, it is not permitted to use tag shorthands for multiple documents in a stream.
                else if (GetCurrentToken() is DocumentStart && (version == null || (version.Version.Major == 1 && version.Version.Minor > 1)))
                {
                    if (GetCurrentToken() is DocumentStart && (version == null))
                    {
                        version = new VersionDirective(new Version(1, 2));
                    }

                    hasOwnDirectives = true;
                    break;
                }
                else
                {
                    break;
                }

                Skip();
            }

            AddTagDirectives(tags, Constants.DefaultTagDirectives);

            if (hasOwnDirectives)
            {
                tagDirectives.Clear();
            }

            AddTagDirectives(tagDirectives, tags);

            return(localVersion);
        }