public ParseInformation Parse(FileName fileName, ITextSource fileContent, bool fullParseInformationRequested, IProject parentProject, CancellationToken cancellationToken) { AXmlParser parser = new AXmlParser(); AXmlDocument document; IncrementalParserState newParserState; if (fileContent.Version is OnDiskTextSourceVersion) { document = parser.Parse(fileContent, cancellationToken); newParserState = null; } else { document = parser.ParseIncremental(parserState, fileContent, out newParserState, cancellationToken); } parserState = newParserState; XamlUnresolvedFile unresolvedFile = XamlUnresolvedFile.Create(fileName, fileContent, document); ParseInformation parseInfo; if (fullParseInformationRequested) { parseInfo = new XamlFullParseInformation(unresolvedFile, document, fileContent.CreateSnapshot()); } else { parseInfo = new ParseInformation(unresolvedFile, fileContent.Version, false); } AddTagComments(document, parseInfo, fileContent); return(parseInfo); }
public static void Run(ITextSource originalXmlFile) { int seed; lock (sharedRnd) { seed = sharedRnd.Next(); } Console.WriteLine(seed); Random rnd = new Random(seed); AXmlParser parser = new AXmlParser(); StringBuilder b = new StringBuilder(originalXmlFile.Text); IncrementalParserState parserState = null; var versionProvider = new TextSourceVersionProvider(); int totalCharactersParsed = 0; int totalCharactersChanged = originalXmlFile.TextLength; TimeSpan incrementalParseTime = TimeSpan.Zero; TimeSpan nonIncrementalParseTime = TimeSpan.Zero; Stopwatch w = new Stopwatch(); for (int iteration = 0; iteration < 100; iteration++) { totalCharactersParsed += b.Length; var textSource = new StringTextSource(b.ToString(), versionProvider.CurrentVersion); w.Restart(); var incrementalResult = parser.ParseIncremental(parserState, textSource, out parserState); w.Stop(); incrementalParseTime += w.Elapsed; w.Restart(); var nonIncrementalResult = parser.Parse(textSource); w.Stop(); nonIncrementalParseTime += w.Elapsed; CompareResults(incrementalResult, nonIncrementalResult); incrementalResult.AcceptVisitor(new ValidationVisitor(textSource)); // Randomly mutate the file: List <TextChangeEventArgs> changes = new List <TextChangeEventArgs>(); int modifications = rnd.Next(0, 25); int offset = 0; for (int i = 0; i < modifications; i++) { if (i == 0 || rnd.Next(0, 10) == 0) { offset = rnd.Next(0, b.Length); } else { offset += rnd.Next(0, Math.Min(10, b.Length - offset)); } int originalOffset = rnd.Next(0, originalXmlFile.TextLength); int insertionLength; int removalLength; switch (rnd.Next(0, 21) / 10) { case 0: removalLength = 0; insertionLength = rnd.Next(0, Math.Min(50, originalXmlFile.TextLength - originalOffset)); break; case 1: removalLength = rnd.Next(0, Math.Min(20, b.Length - offset)); insertionLength = rnd.Next(0, Math.Min(20, originalXmlFile.TextLength - originalOffset)); break; default: removalLength = rnd.Next(0, b.Length - offset); insertionLength = rnd.Next(0, originalXmlFile.TextLength - originalOffset); break; } string removedText = b.ToString(offset, removalLength); b.Remove(offset, removalLength); string insertedText = originalXmlFile.GetText(originalOffset, insertionLength); b.Insert(offset, insertedText); versionProvider.AppendChange(new TextChangeEventArgs(offset, removedText, insertedText)); totalCharactersChanged += insertionLength; } } Console.WriteLine("Incremental parse time: " + incrementalParseTime + " for " + totalCharactersChanged + " characters changed"); Console.WriteLine("Non-Incremental parse time: " + nonIncrementalParseTime + " for " + totalCharactersParsed + " characters"); }