Example #1
0
 public Parser(IScanner scanner)
 {
     this.states        = new Stack <ParserState>();
     this.tagDirectives = new TagDirectiveCollection();
     this.pendingEvents = new Queue <ParsingEvent>();
     this.scanner       = scanner;
 }
Example #2
0
 private static void AddTagDirectives(TagDirectiveCollection directives, IEnumerable <TagDirective> source)
 {
     foreach (var directive in source)
     {
         if (!directives.Contains(directive))
         {
             directives.Add(directive);
         }
     }
 }
Example #3
0
 private static void AddDefaultTagDirectives(TagDirectiveCollection directives)
 {
     foreach (var directive in Constants.DefaultTagDirectives)
     {
         if (!directives.Contains(directive))
         {
             directives.Add(directive);
         }
     }
 }
Example #4
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);
        }
Example #6
0
        private VersionDirective ProcessDirectives(TagDirectiveCollection tags)
        {
            VersionDirective directive = null;
            bool             flag      = false;

            while (true)
            {
                VersionDirective currentToken = this.GetCurrentToken() as VersionDirective;
                if (currentToken != null)
                {
                    if (directive != null)
                    {
                        throw new SemanticErrorException(currentToken.Start, currentToken.End, "Found duplicate %YAML directive.");
                    }
                    if ((currentToken.Version.Major != 1) || (currentToken.Version.Minor != 1))
                    {
                        throw new SemanticErrorException(currentToken.Start, currentToken.End, "Found incompatible YAML document.");
                    }
                    directive = currentToken;
                    flag      = true;
                }
                else
                {
                    TagDirective item = this.GetCurrentToken() as TagDirective;
                    if (item == null)
                    {
                        AddTagDirectives(tags, Constants.DefaultTagDirectives);
                        if (flag)
                        {
                            this.tagDirectives.Clear();
                        }
                        AddTagDirectives(this.tagDirectives, tags);
                        return(directive);
                    }
                    if (tags.Contains(item.Handle))
                    {
                        throw new SemanticErrorException(item.Start, item.End, "Found duplicate %TAG directive.");
                    }
                    tags.Add(item);
                    flag = true;
                }
                this.Skip();
            }
        }
Example #7
0
        private ParsingEvent ParseDocumentStart(bool isImplicit)
        {
            if (!isImplicit)
            {
                while (this.GetCurrentToken() is DocumentEnd)
                {
                    this.Skip();
                }
            }
            if (isImplicit && (!(this.GetCurrentToken() is VersionDirective) && (!(this.GetCurrentToken() is TagDirective) && (!(this.GetCurrentToken() is DocumentStart) && !(this.GetCurrentToken() is StreamEnd)))))
            {
                TagDirectiveCollection directives = new TagDirectiveCollection();
                this.ProcessDirectives(directives);
                this.states.Push(ParserState.DocumentEnd);
                this.state = ParserState.BlockNode;
                return(new DocumentStart(null, directives, true, this.GetCurrentToken().Start, this.GetCurrentToken().End));
            }
            if (this.GetCurrentToken() is StreamEnd)
            {
                this.state = ParserState.StreamEnd;
                ParsingEvent event3 = new StreamEnd(this.GetCurrentToken().Start, this.GetCurrentToken().End);
                if (this.scanner.MoveNextWithoutConsuming())
                {
                    throw new InvalidOperationException("The scanner should contain no more tokens.");
                }
                return(event3);
            }
            Mark start = this.GetCurrentToken().Start;
            TagDirectiveCollection tags    = new TagDirectiveCollection();
            VersionDirective       version = this.ProcessDirectives(tags);
            Token currentToken             = this.GetCurrentToken();

            if (!(currentToken is DocumentStart))
            {
                throw new SemanticErrorException(currentToken.Start, currentToken.End, "Did not find expected <document start>.");
            }
            this.states.Push(ParserState.DocumentEnd);
            this.state = ParserState.DocumentContent;
            ParsingEvent event2 = new DocumentStart(version, tags, false, start, currentToken.End);

            this.Skip();
            return(event2);
        }
        private ParsingEvent ParseDocumentStart(bool isImplicit)
        {
            if (!isImplicit)
            {
                while (GetCurrentToken() is YamlDotNet.Core.Tokens.DocumentEnd)
                {
                    Skip();
                }
            }
            if (isImplicit && !(GetCurrentToken() is VersionDirective) && !(GetCurrentToken() is TagDirective) && !(GetCurrentToken() is YamlDotNet.Core.Tokens.DocumentStart) && !(GetCurrentToken() is YamlDotNet.Core.Tokens.StreamEnd))
            {
                TagDirectiveCollection tags = new TagDirectiveCollection();
                ProcessDirectives(tags);
                states.Push(ParserState.DocumentEnd);
                state = ParserState.BlockNode;
                return(new YamlDotNet.Core.Events.DocumentStart(null, tags, true, GetCurrentToken().Start, GetCurrentToken().End));
            }
            if (!(GetCurrentToken() is YamlDotNet.Core.Tokens.StreamEnd))
            {
                Mark start = GetCurrentToken().Start;
                TagDirectiveCollection tags2   = new TagDirectiveCollection();
                VersionDirective       version = ProcessDirectives(tags2);
                Token token = GetCurrentToken();
                if (!(token is YamlDotNet.Core.Tokens.DocumentStart))
                {
                    throw new SemanticErrorException(token.Start, token.End, "Did not find expected <document start>.");
                }
                states.Push(ParserState.DocumentEnd);
                state = ParserState.DocumentContent;
                ParsingEvent result = new YamlDotNet.Core.Events.DocumentStart(version, tags2, false, start, token.End);
                Skip();
                return(result);
            }
            state = ParserState.StreamEnd;
            ParsingEvent result2 = new YamlDotNet.Core.Events.StreamEnd(GetCurrentToken().Start, GetCurrentToken().End);

            if (scanner.MoveNextWithoutConsuming())
            {
                throw new InvalidOperationException("The scanner should contain no more tokens.");
            }
            return(result2);
        }
Example #9
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);
        }
Example #10
0
        /// <summary>
        /// Parse the productions:
        /// implicit_document    ::= block_node DOCUMENT-END*
        ///                          *
        /// explicit_document    ::= DIRECTIVE* DOCUMENT-START block_node? DOCUMENT-END*
        ///                          *************************
        /// </summary>
        private ParsingEvent ParseDocumentStart(bool isImplicit)
        {
            if (currentToken is VersionDirective)
            {
                // EB22
                throw new SyntaxErrorException("While parsing a document start node, could not find document end marker before version directive.");
            }

            // Parse extra document end indicators.

            var current = GetCurrentToken();

            if (!isImplicit)
            {
                while (current is DocumentEnd)
                {
                    Skip();
                    current = GetCurrentToken();
                }
            }

            if (current == null)
            {
                throw new SyntaxErrorException("Reached the end of the stream while parsing a document start.");
            }

            if (current is Scalar && (state == ParserState.ImplicitDocumentStart || state == ParserState.DocumentStart))
            {
                isImplicit = true;
            }

            // Parse an isImplicit document.

            if (isImplicit && !(current is VersionDirective || current is TagDirective || current is DocumentStart || current is StreamEnd || current is DocumentEnd) || current is BlockMappingStart)
            {
                var directives = new TagDirectiveCollection();
                ProcessDirectives(directives);

                states.Push(ParserState.DocumentEnd);

                state = ParserState.BlockNode;

                return(new Events.DocumentStart(null, directives, true, current.Start, current.End));
            }

            // Parse an explicit document.

            else if (!(current is StreamEnd || current is DocumentEnd))
            {
                var start            = current.Start;
                var directives       = new TagDirectiveCollection();
                var versionDirective = ProcessDirectives(directives);

                current = GetCurrentToken() ?? throw new SemanticErrorException("Reached the end of the stream while parsing a document start");

                if (!(current is DocumentStart))
                {
                    throw new SemanticErrorException(current.Start, current.End, "Did not find expected <document start>.");
                }

                states.Push(ParserState.DocumentEnd);

                state = ParserState.DocumentContent;

                var end = current.End;
                Skip();
                return(new Events.DocumentStart(versionDirective, directives, false, start, end));
            }

            // Parse the stream end.

            else
            {
                if (current is DocumentEnd)
                {
                    Skip();
                }
                state = ParserState.StreamEnd;

                current = GetCurrentToken() ?? throw new SemanticErrorException("Reached the end of the stream while parsing a document start");

                var evt = new Events.StreamEnd(current.Start, current.End);
                // Do not call skip here because that would throw an exception
                if (scanner.MoveNextWithoutConsuming())
                {
                    throw new InvalidOperationException("The scanner should contain no more tokens.");
                }
                return(evt);
            }
        }
Example #11
0
        /// <summary>
        /// Parse the productions:
        /// implicit_document    ::= block_node DOCUMENT-END*
        ///                          *
        /// explicit_document    ::= DIRECTIVE* DOCUMENT-START block_node? DOCUMENT-END*
        ///                          *************************
        /// </summary>
        private Event ParseDocumentStart(bool isImplicit)
        {
            // Parse extra document end indicators.

            if (!isImplicit)
            {
                while (GetCurrentToken() is DocumentEnd)
                {
                    Skip();
                }
            }

            // Parse an isImplicit document.

            if (isImplicit && !(GetCurrentToken() is VersionDirective || GetCurrentToken() is TagDirective || GetCurrentToken() is DocumentStart || GetCurrentToken() is StreamEnd))
            {
                TagDirectiveCollection directives = new TagDirectiveCollection();
                ProcessDirectives(directives);

                states.Push(ParserState.YAML_PARSE_DOCUMENT_END_STATE);

                state = ParserState.YAML_PARSE_BLOCK_NODE_STATE;

                return(new Events.DocumentStart(null, directives, true, GetCurrentToken().Start, GetCurrentToken().End));
            }

            // Parse an explicit document.

            else if (!(GetCurrentToken() is StreamEnd))
            {
                Mark start = GetCurrentToken().Start;
                TagDirectiveCollection directives       = new TagDirectiveCollection();
                VersionDirective       versionDirective = ProcessDirectives(directives);

                var current = GetCurrentToken();
                if (!(current is DocumentStart))
                {
                    throw new SemanticErrorException(current.Start, current.End, "Did not find expected <document start>.");
                }

                states.Push(ParserState.YAML_PARSE_DOCUMENT_END_STATE);

                state = ParserState.YAML_PARSE_DOCUMENT_CONTENT_STATE;

                Event evt = new Events.DocumentStart(versionDirective, directives, false, start, current.End);
                Skip();
                return(evt);
            }

            // Parse the stream end.

            else
            {
                state = ParserState.YAML_PARSE_END_STATE;

                Event evt = new Events.StreamEnd(GetCurrentToken().Start, GetCurrentToken().End);
                // Do not call skip here because that would throw an exception
                if (scanner.InternalMoveNext())
                {
                    throw new InvalidOperationException("The scanner should contain no more tokens.");
                }
                return(evt);
            }
        }
Example #12
0
        /// <summary>
        /// Parse the productions:
        /// implicit_document    ::= block_node DOCUMENT-END*
        ///                          *
        /// explicit_document    ::= DIRECTIVE* DOCUMENT-START block_node? DOCUMENT-END*
        ///                          *************************
        /// </summary>
        private ParsingEvent ParseDocumentStart(bool isImplicit)
        {
            // Parse extra document end indicators.

            if (!isImplicit)
            {
                while (GetCurrentToken() is DocumentEnd)
                {
                    Skip();
                }
            }

            // Parse an isImplicit document.

            if (isImplicit && !(GetCurrentToken() is VersionDirective || GetCurrentToken() is TagDirective || GetCurrentToken() is DocumentStart || GetCurrentToken() is StreamEnd))
            {
                var directives = new TagDirectiveCollection();
                ProcessDirectives(directives);

                states.Push(ParserState.DocumentEnd);

                state = ParserState.BlockNode;

                return(new Events.DocumentStart(null, directives, true, GetCurrentToken().Start, GetCurrentToken().End));
            }

            // Parse an explicit document.

            else if (!(GetCurrentToken() is StreamEnd))
            {
                Mark start            = GetCurrentToken().Start;
                var  directives       = new TagDirectiveCollection();
                var  versionDirective = ProcessDirectives(directives);

                var current = GetCurrentToken();
                if (!(current is DocumentStart))
                {
                    throw new SemanticErrorException(current.Start, current.End, "Did not find expected <document start>.");
                }

                states.Push(ParserState.DocumentEnd);

                state = ParserState.DocumentContent;

                ParsingEvent evt = new Events.DocumentStart(versionDirective, directives, false, start, current.End);
                Skip();
                return(evt);
            }

            // Parse the stream end.

            else
            {
                state = ParserState.StreamEnd;

                ParsingEvent evt = new Events.StreamEnd(GetCurrentToken().Start, GetCurrentToken().End);
                // Do not call skip here because that would throw an exception
                if (scanner.MoveNextWithoutConsuming())
                {
                    throw new InvalidOperationException("The scanner should contain no more tokens.");
                }
                return(evt);
            }
        }