List <InternalObject> InternalParseIncremental(
            IncrementalParserState oldParserState, SourceText newTextSource, out IncrementalParserState newParserState,
            bool collapseProperlyNestedElements, CancellationToken cancellationToken)
        {
            var reader   = new TagReader(this, newTextSource, collapseProperlyNestedElements);
            var reuseMap = oldParserState != null?oldParserState.GetReuseMapTo(newTextSource) : null;

            List <InternalObject> internalObjects;

            if (reuseMap != null)
            {
                internalObjects = reader.ReadAllObjectsIncremental(oldParserState.Objects, reuseMap, cancellationToken);
            }
            else
            {
                internalObjects = reader.ReadAllObjects(cancellationToken);
            }

            if (newTextSource != null)
            {
                newParserState = new IncrementalParserState(newTextSource.Length, newTextSource, internalObjects.ToArray());
            }
            else
            {
                newParserState = null;
            }

            return(internalObjects);
        }
		/// <summary>
		/// Parses a document incrementally into a flat list of tags.
		/// </summary>
		/// <param name="oldParserState">The parser state from a previous call to ParseIncremental(). Use null for the first call.</param>
		/// <param name="newTextSource">The text source for the new document version.</param>
		/// <param name="newParserState">Out: the new parser state, pass this to the next ParseIncremental() call.</param>
		/// <param name="cancellationToken">Optional: cancellation token.</param>
		/// <returns>Parsed tag soup.</returns>
		public IList<AXmlObject> ParseTagSoupIncremental(
            IncrementalParserState oldParserState, SourceText newTextSource, out IncrementalParserState newParserState,
			CancellationToken cancellationToken = default(CancellationToken))
		{
			if (newTextSource == null)
				throw new ArgumentNullException("newTextSource");
			var internalObjects = InternalParseIncremental(oldParserState, newTextSource, out newParserState, false, cancellationToken);
			return CreatePublic(internalObjects);
		}
        /// <summary>
        /// Parses a document incrementally into a flat list of tags.
        /// </summary>
        /// <param name="oldParserState">The parser state from a previous call to ParseIncremental(). Use null for the first call.</param>
        /// <param name="newTextSource">The text source for the new document version.</param>
        /// <param name="newParserState">Out: the new parser state, pass this to the next ParseIncremental() call.</param>
        /// <param name="cancellationToken">Optional: cancellation token.</param>
        /// <returns>Parsed tag soup.</returns>
        public IList <AXmlObject> ParseTagSoupIncremental(
            IncrementalParserState oldParserState, SourceText newTextSource, out IncrementalParserState newParserState,
            CancellationToken cancellationToken = default(CancellationToken))
        {
            if (newTextSource == null)
            {
                throw new ArgumentNullException("newTextSource");
            }
            var internalObjects = InternalParseIncremental(oldParserState, newTextSource, out newParserState, false, cancellationToken);

            return(CreatePublic(internalObjects));
        }
        /// <summary>
        /// Parses a document incrementally into a flat list of tags.
        /// </summary>
        /// <param name="oldParserState">The parser state from a previous call to ParseIncremental(). Use null for the first call.</param>
        /// <param name="newTextSource">The text source for the new document version.</param>
        /// <param name="newParserState">Out: the new parser state, pass this to the next ParseIncremental() call.</param>
        /// <param name="cancellationToken">Optional: cancellation token.</param>
        /// <returns>Parsed tag soup.</returns>
        public AXmlDocument ParseIncremental(
            IncrementalParserState oldParserState, SourceText newTextSource, out IncrementalParserState newParserState,
            CancellationToken cancellationToken = default(CancellationToken))
        {
            if (newTextSource == null)
            {
                throw new ArgumentNullException("newTextSource");
            }
            var internalObjects = InternalParseIncremental(oldParserState, newTextSource, out newParserState, true, cancellationToken);
            var heuristic       = new TagMatchingHeuristics(newTextSource);

            return(new AXmlDocument(null, 0, heuristic.CreateDocument(internalObjects, cancellationToken)));
        }
		List<InternalObject> InternalParseIncremental(
            IncrementalParserState oldParserState, SourceText newTextSource, out IncrementalParserState newParserState,
			bool collapseProperlyNestedElements, CancellationToken cancellationToken)
		{
			var reader = new TagReader(this, newTextSource, collapseProperlyNestedElements);
            var reuseMap = oldParserState != null ? oldParserState.GetReuseMapTo(newTextSource) : null;
			
			List<InternalObject> internalObjects;
			if (reuseMap != null)
				internalObjects = reader.ReadAllObjectsIncremental(oldParserState.Objects, reuseMap, cancellationToken);
			else
				internalObjects = reader.ReadAllObjects(cancellationToken);
			
            if (newTextSource != null)
                newParserState = new IncrementalParserState(newTextSource.Length, newTextSource, internalObjects.ToArray());
			else
				newParserState = null;
			
			return internalObjects;
		}
		/// <summary>
		/// Parses a document incrementally into a flat list of tags.
		/// </summary>
		/// <param name="oldParserState">The parser state from a previous call to ParseIncremental(). Use null for the first call.</param>
		/// <param name="newTextSource">The text source for the new document version.</param>
		/// <param name="newParserState">Out: the new parser state, pass this to the next ParseIncremental() call.</param>
		/// <param name="cancellationToken">Optional: cancellation token.</param>
		/// <returns>Parsed tag soup.</returns>
		public AXmlDocument ParseIncremental(
            IncrementalParserState oldParserState, SourceText newTextSource, out IncrementalParserState newParserState,
			CancellationToken cancellationToken = default(CancellationToken))
		{
			if (newTextSource == null)
				throw new ArgumentNullException("newTextSource");
			var internalObjects = InternalParseIncremental(oldParserState, newTextSource, out newParserState, true, cancellationToken);
			var heuristic = new TagMatchingHeuristics(newTextSource);
			return new AXmlDocument(null, 0, heuristic.CreateDocument(internalObjects, cancellationToken));
		}