private static void OrderBy(int count, int run, Func <int, int> keySelector)
        {
            int[]     source    = EnumerableX.RandomInt32().Take(count).ToArray();
            Stopwatch stopwatch = Stopwatch.StartNew();

            Enumerable.Range(0, run).ForEach(_ =>
            {
                int[] sequential = source.OrderBy(keySelector).ToArray();
            });
            stopwatch.Stop();
            $"Sequential:{stopwatch.ElapsedMilliseconds}".WriteLine();

            stopwatch.Restart();
            Enumerable.Range(0, run).ForEach(_ =>
            {
                int[] parallel1 = source.AsParallel().OrderBy(keySelector).ToArray();
            });
            stopwatch.Stop();
            $"Parallel:{stopwatch.ElapsedMilliseconds}".WriteLine();
        }
Exemple #2
0
        public void LoadPlugins()
        {
            Stopwatch benchmark = Stopwatch.StartNew();

            if (packages.Count > 0)
            {
                // Quick load
                foreach (PluginPackage package in packages)
                {
                    if (!package.settings.IsEnabled)
                    {
                        continue;
                    }

                    package.plugin.Initialize(ctx);
                }
            }
            benchmark.Stop();
            Debugger.Module($"Loaded {packages.Count} module(s) in {benchmark.ElapsedMilliseconds}ms");
        }
Exemple #3
0
        private async Task GenerateStandardReport(
            string reportName,
            DateTime reportGenerationTime,
            CloudBlobContainer destinationContainer,
            ILogger <ReportBuilder> reportBuilderLogger,
            ILogger <ReportDataCollector> reportCollectorLogger)
        {
            var stopwatch = Stopwatch.StartNew();

            var reportBuilder       = new ReportBuilder(reportBuilderLogger, reportName);
            var reportDataCollector = new ReportDataCollector(reportCollectorLogger, _storedProcedures[reportName], OpenSqlConnectionAsync <StatisticsDbConfiguration>, _sqlCommandTimeoutSeconds);

            await ProcessReport(LoggerFactory, destinationContainer, reportBuilder, reportDataCollector, reportGenerationTime);

            stopwatch.Stop();

            var reportMetricName = reportName + " report";

            _applicationInsightsHelper.TrackMetric(reportMetricName + " Generation Time (ms)", stopwatch.ElapsedMilliseconds);
            _applicationInsightsHelper.TrackReportProcessed(reportMetricName);
        }
        public void Analyze()
        {
            bool isValid = false;

            if (DungeonFlow == null)
            {
                Debug.LogError("No DungeonFlow assigned to analyzer");
            }
            else if (Iterations <= 0)
            {
                Debug.LogError("Iteration count must be greater than 0");
            }
            else if (MaxFailedAttempts <= 0)
            {
                Debug.LogError("Max failed attempt count must be greater than 0");
            }
            else
            {
                isValid = true;
            }

            if (!isValid)
            {
                return;
            }

            prevShouldRandomizeSeed = generator.ShouldRandomizeSeed;

            generator.IsAnalysis          = true;
            generator.DungeonFlow         = DungeonFlow;
            generator.MaxAttemptCount     = MaxFailedAttempts;
            generator.ShouldRandomizeSeed = true;
            analysis            = new GenerationAnalysis(Iterations);
            analysisTime        = Stopwatch.StartNew();
            remainingIterations = targetIterations = Iterations;

            generator.OnGenerationStatusChanged += OnGenerationStatusChanged;
            generator.Generate();
        }
        internal static void OrderByTest(Func <int, int> keySelector, int sourceCount, int testRepeatCount)
        {
            int[] source = EnumerableX
                           .RandomInt32(min: int.MinValue, max: int.MaxValue, count: sourceCount)
                           .ToArray();
            Stopwatch stopwatch = Stopwatch.StartNew();

            Enumerable.Range(0, testRepeatCount).ForEach(_ =>
            {
                int[] sequentialResults = source.OrderBy(keySelector).ToArray();
            });
            stopwatch.Stop();
            $"Sequential:{stopwatch.ElapsedMilliseconds}".WriteLine();

            stopwatch.Restart();
            Enumerable.Range(0, testRepeatCount).ForEach(_ =>
            {
                int[] parallel1Results = source.AsParallel().OrderBy(keySelector).ToArray();
            });
            stopwatch.Stop();
            $"Parallel:{stopwatch.ElapsedMilliseconds}".WriteLine();
        }
Exemple #6
0
        public async Task <bool> PreloadPlugins()
        {
            Stopwatch benchmark = Stopwatch.StartNew();

            Debugger.Module("Pre loading modules");
            foreach (string module in IterateModules())
            {
                try
                {
                    string            serializedModule = File.ReadAllText(Path.Combine(module, "module.json"));
                    PluginInformation modInformation   = JsonConvert.DeserializeObject <PluginInformation>(serializedModule);

                    try
                    {
                        await PreloadPlugin(module, modInformation);

                        failedPlugins.Remove(modInformation.Name);
                    }
                    catch
                    {
                        failedPlugins.Add(modInformation.Name);
                        throw;
                    }
                }
                catch (Exception err)
                {
                    Debugger.Error(err);
                }
            }

            benchmark.Stop();
            Debugger.Module($"Pre loaded {packages.Count} module(s) in {benchmark.ElapsedMilliseconds}ms");

            if (!tsc.Task.IsCompleted)
            {
                tsc.SetResult(null);
            }
            return(true);
        }
        protected override void ReParseImpl()
        {
            try
            {
                var stopwatch = Stopwatch.StartNew();

                var           snapshot = TextBuffer.CurrentSnapshot;
                ITextDocument textDocument;
                if (!TextDocumentFactoryService.TryGetTextDocument(_documentBuffer, out textDocument))
                {
                    return;
                }

                var diff   = _commands.GetGitDiffFor(textDocument, snapshot);
                var result = new DiffParseResultEventArgs(snapshot, stopwatch.Elapsed, diff.ToList());
                OnParseComplete(result);
            }
            catch (InvalidOperationException)
            {
                MarkDirty(true);
                throw;
            }
        }
Exemple #8
0
        /*public void UpdateProgress (object sender, IterationEventArgs e)
         * {
         *  if (ProgressUpdate == null)
         *      return;
         *
         *  int iteration = e.IterationIndex + 1;
         *
         *  if (iteration == 1)
         *      stopwatch = System.Diagnostics.Stopwatch.StartNew();
         *
         *  var progressUpdate = new ProgressUpdateEventArgs();
         *
         *  if (e.IterationCount > 0)
         *  {
         *      progressUpdate.Total = 1000;
         *      progressUpdate.Current = e.IterationCount == 0 ? 0 : Math.Min(progressUpdate.Total, (int) Math.Round((double) iteration / e.IterationCount * 1000.0));
         *      double progressRate = stopwatch.Elapsed.TotalSeconds > 0 ? iteration / stopwatch.Elapsed.TotalSeconds : 0;
         *      long iterationsRemaining = e.IterationCount - iteration;
         *      TimeSpan timeRemaining = progressRate == 0 ? TimeSpan.Zero
         *                                                 : TimeSpan.FromSeconds(iterationsRemaining / progressRate);
         *      progressUpdate.Message = String.Format("{0} ({1}/{2}) - {3} per second, {4}h{5}m{6}s remaining",
         *                                             e.Message,
         *                                             iteration,
         *                                             e.IterationCount,
         *                                             (long) progressRate,
         *                                             timeRemaining.Hours,
         *                                             timeRemaining.Minutes,
         *                                             timeRemaining.Seconds);
         *  }
         *  else
         *  {
         *      progressUpdate.Total = 0;
         *      progressUpdate.Current = iteration;
         *      progressUpdate.Message = String.Format("{0} ({1})",
         *                                             e.Message,
         *                                             iteration);
         *  }
         *
         *  ProgressUpdate(this, progressUpdate);
         *  e.Cancel = progressUpdate.Cancel;
         * }*/

        public void UpdateProgress(object sender, MergerWrapper.MergingProgressEventArgs e)
        {
            if (e.MergingException != null)
            {
                Program.HandleException(e.MergingException);
            }

            if (ProgressUpdate == null)
            {
                return;
            }

            if (e.MergedFiles == 0)
            {
                stopwatch = Stopwatch.StartNew();
            }

            var progressUpdate = new ProgressUpdateEventArgs();

            progressUpdate.Total   = e.TotalFiles;
            progressUpdate.Current = e.MergedFiles;
            double   progressRate   = stopwatch.Elapsed.TotalSeconds > 0 ? e.MergedFiles / stopwatch.Elapsed.TotalSeconds : 0;
            long     bytesRemaining = e.TotalFiles - e.MergedFiles;
            TimeSpan timeRemaining  = progressRate == 0 ? TimeSpan.Zero
                                                       : TimeSpan.FromSeconds(bytesRemaining / progressRate);

            progressUpdate.Message = String.Format("Merging results... ({0}/{1}) - {2} per second, {3}h{4}m{5}s remaining",
                                                   e.MergedFiles,
                                                   e.TotalFiles,
                                                   Math.Round(progressRate, 1),
                                                   timeRemaining.Hours,
                                                   timeRemaining.Minutes,
                                                   timeRemaining.Seconds);

            ProgressUpdate(this, progressUpdate);
            e.Cancel = progressUpdate.Cancel;
        }
Exemple #9
0
        private void VoxelizeSprite()
        {
            /////////////////////
            var timer = Stopwatch.StartNew();

            Mesh mesh = VoxelUtil.VoxelizeTexture2D(_sprite.texture, _applyColorPerVertex, _scale);

            Texture2D texture = VoxelUtil.GenerateTextureMap(ref mesh, _sprite.texture);

            if (_useMeshOptimizer)
            {
                MeshUtility.Optimize(mesh);
            }

            timer.Stop();
            ///////////////////////

            string meshName = _sprite.name + VOXEL_NAME_POST_FIX;

            mesh.name = meshName;
            Debug.Log(string.Format("[Voxelizer] {0}: Mesh created after {1} milliseconds", meshName, timer.ElapsedMilliseconds));

            if (_createNewGameObject)
            {
                CreateVoxelGameObject(mesh, texture);
            }

            if (_saveMesh)
            {
                SaveMeshToFile(mesh);
            }

            if (_saveTexture)
            {
                SaveTextureToFile(texture);
            }
        }
        protected override void ReParseImpl()
        {
            var outputWindow = OutputWindowService.TryGetPane(PredefinedOutputWindowPanes.TvlIntellisense);

            Stopwatch stopwatch = Stopwatch.StartNew();

            string        filename     = "<Unknown File>";
            ITextDocument textDocument = TextDocument;

            if (textDocument != null)
            {
                filename = textDocument.FilePath;
            }

            var snapshot = TextBuffer.CurrentSnapshot;
            var input    = new SnapshotCharStream(snapshot, new Span(0, snapshot.Length));
            var lexer    = new PhpLexer(input);

            lexer.TokenFactory = new SnapshotTokenFactory(snapshot, lexer);
            var tokens = new CommonTokenStream(lexer);

            var parser = new PhpParser(tokens);

            parser.BuildParseTree = true;

            List <ParseErrorEventArgs> errors = new List <ParseErrorEventArgs>();

            parser.AddErrorListener(new ErrorListener(filename, errors, outputWindow));
            var result = parser.compileUnit();

            OutliningTreesListener listener = new OutliningTreesListener();

            ParseTreeWalker.Default.Walk(listener, result);

            OnParseComplete(new PhpOutliningParseResultEventArgs(snapshot, errors, stopwatch.Elapsed, tokens.GetTokens(), result, listener.OutliningTrees));
        }
Exemple #11
0
        public async Task LoadPlugins()
        {
            await PreloadTask;

            Stopwatch benchmark = Stopwatch.StartNew();

            if (packages.Count > 0)
            {
                // Quick load
                foreach (PluginPackage package in packages.Where(p => p.settings.IsEnabled))
                {
                    try
                    {
                        package.plugin.Initialize(ctx);
                    }
                    catch (Exception err)
                    {
                        Debugger.Error(err);
                    }
                }
            }
            benchmark.Stop();
            Debugger.Module($"Loaded {packages.Count} module(s) in {benchmark.ElapsedMilliseconds}ms");
        }
Exemple #12
0
            private void CollectInfrequently(long privateBytes)
            {
                // not thread-safe, only invoke from timer callback
                Debug.Assert(_inPBytesMonitorThread == 1);

                // The Server GC on x86 can traverse ~200mb per CPU per second, and the maximum heap size
                // is about 3400mb, so the worst case scenario on x86 would take about 8 seconds to collect
                // on a dual CPU box.
                //
                // The Server GC on x64 can traverse ~300mb per CPU per second, so a 6000 MB heap will take
                // about 10 seconds to collect on a dual CPU box.  The worst case scenario on x64 would make
                // you want to return your hardware for a refund.

                long timeSinceInducedGC = DateTime.UtcNow.Subtract(_inducedGCFinishTime).Ticks;
                bool infrequent         = (timeSinceInducedGC > _inducedGCMinInterval);
                RecycleLimitNotificationFrequency frequency = RecycleLimitNotificationFrequency.Medium;

                // if we haven't collected recently, or if the trim percent is low (less than 50%),
                // we need to collect again
                if (infrequent || _howFrequent < 5)
                {
                    // if we're inducing GC too frequently, increase the trim percentage, but don't go above 50%
                    if (!infrequent)
                    {
                        _howFrequent = Math.Min(5, _howFrequent + 1);
                        frequency    = RecycleLimitNotificationFrequency.High;
                    }
                    // if we're inducing GC infrequently, we may want to decrease the trim percentage
                    else if (_howFrequent > 1 && timeSinceInducedGC > 2 * _inducedGCMinInterval)
                    {
                        _howFrequent = Math.Max(1, _howFrequent - 1);
                        frequency    = RecycleLimitNotificationFrequency.Low;
                    }

                    Stopwatch sw1       = Stopwatch.StartNew();
                    bool      requestGC = AlertProxyMonitors(privateBytes, _limit, frequency);
                    sw1.Stop();

                    //

                    if (!requestGC || _appManager.ShutdownInProgress)
                    {
                        return;
                    }

                    // collect and record statistics
                    Stopwatch sw2 = Stopwatch.StartNew();
                    GC.Collect();
                    sw2.Stop();

                    _inducedGCCount++; // only used for debugging
                    _inducedGCFinishTime         = DateTime.UtcNow;
                    _inducedGCDurationTicks      = sw2.Elapsed.Ticks;
                    _inducedGCPostPrivateBytes   = NextSample();
                    _inducedGCPrivateBytesChange = privateBytes - _inducedGCPostPrivateBytes;
                    // target 3.3% Time in GC, but don't induce a GC more than once every 5 seconds
                    // Notes on calculation below:  If G is duration of garbage collection and T is duration
                    // between starting the next collection, then G/T is % Time in GC.  If we target 3.3%,
                    // then G/T = 3.3% = 33/1000, so T = G * 1000/33.
                    _inducedGCMinInterval = Math.Max(_inducedGCDurationTicks * 1000 / 33, 5 * TimeSpan.TicksPerSecond);
                    // no more frequently than every 60 seconds if change is less than 1%
                    if (_inducedGCPrivateBytesChange * 100 <= privateBytes)
                    {
                        _inducedGCMinInterval = Math.Max(_inducedGCMinInterval, 60 * TimeSpan.TicksPerSecond);
                    }
#if DBG
                    Debug.Trace("RecycleLimitMonitorSingleton", "GC.COLLECT STATS "
                                + "TrimCaches(" + frequency + ")"
                                + ", trimDurationSeconds=" + (sw1.Elapsed.Ticks / TimeSpan.TicksPerSecond)
                                + ", requestGC=" + requestGC
                                + ", #secondsSinceInducedGC=" + (timeSinceInducedGC / TimeSpan.TicksPerSecond)
                                + ", InducedGCCount=" + _inducedGCCount
                                + ", gcDurationSeconds=" + (_inducedGCDurationTicks / TimeSpan.TicksPerSecond)
                                + ", PrePrivateBytes=" + privateBytes
                                + ", PostPrivateBytes=" + _inducedGCPostPrivateBytes
                                + ", PrivateBytesChange=" + _inducedGCPrivateBytesChange
                                + ", gcMinIntervalSeconds=" + (_inducedGCMinInterval / TimeSpan.TicksPerSecond));
#endif

#if PERF
                    SafeNativeMethods.OutputDebugString("  ** COLLECT **: "
                                                        + percent + "%, "
                                                        + (sw1.Elapsed.Ticks / TimeSpan.TicksPerSecond) + " seconds"
                                                        + ", infrequent=" + infrequent
                                                        + ", removed=" + trimmedOrExpired
                                                        + ", sinceIGC=" + (timeSinceInducedGC / TimeSpan.TicksPerSecond)
                                                        + ", IGCCount=" + _inducedGCCount
                                                        + ", IGCDuration=" + (_inducedGCDurationTicks / TimeSpan.TicksPerSecond)
                                                        + ", preBytes=" + privateBytes
                                                        + ", postBytes=" + _inducedGCPostPrivateBytes
                                                        + ", byteChange=" + _inducedGCPrivateBytesChange
                                                        + ", IGCMinInterval=" + (_inducedGCMinInterval / TimeSpan.TicksPerSecond) + "\n");
#endif
                }
            }
Exemple #13
0
        protected void parseSources(ParserFactory factory, IEnumerable <InputDescriptor> sources)
        {
            Stopwatch startTime = Stopwatch.StartNew();

            Thread.VolatileWrite(ref tokenCount, 0);
            int sourceCount = 0;
            int inputSize   = 0;

#if NET40PLUS
            BlockingCollection <int> threadIdentifiers = new BlockingCollection <int>();
            for (int i = 0; i < NUMBER_OF_THREADS; i++)
            {
                threadIdentifiers.Add(i);
            }

            ICollection <Task <int> > results             = new List <Task <int> >();
            QueuedTaskScheduler       executorServiceHost = new QueuedTaskScheduler(NUMBER_OF_THREADS);
            TaskScheduler             executorService     = executorServiceHost.ActivateNewQueue();
#else
            ICollection <Func <int> > results = new List <Func <int> >();
#endif
            foreach (InputDescriptor inputDescriptor in sources)
            {
                ICharStream input = inputDescriptor.GetInputStream();
                sourceCount++;
                input.Seek(0);
                inputSize += input.Size;
#if NET40PLUS
                Task <int> futureChecksum = Task.Factory.StartNew <int>(new Callable_1(input, factory, threadIdentifiers).call, CancellationToken.None, TaskCreationOptions.None, executorService);
#else
                Func <int> futureChecksum = new Callable_1(input, factory).call;
#endif
                results.Add(futureChecksum);
            }

            Checksum checksum = new CRC32();
            foreach (var future in results)
            {
#if NET40PLUS
                int value = future.Result;
#else
                int value = future();
#endif
                if (COMPUTE_CHECKSUM)
                {
                    updateChecksum(checksum, value);
                }
            }

#if NET40PLUS
            executorServiceHost.Dispose();
#endif

            Console.Out.WriteLine("Total parse time for {0} files ({1} KB, {2} tokens, checksum 0x{3:X8}): {4}ms",
                                  sourceCount,
                                  inputSize / 1024,
                                  Thread.VolatileRead(ref tokenCount),
                                  COMPUTE_CHECKSUM ? checksum.Value : 0,
                                  startTime.ElapsedMilliseconds);

            if (sharedLexers.Length > 0)
            {
                Lexer             lexer            = sharedLexers[0];
                LexerATNSimulator lexerInterpreter = lexer.Interpreter;
                DFA[]             modeToDFA        = lexerInterpreter.atn.modeToDFA;
                if (SHOW_DFA_STATE_STATS)
                {
                    int states  = 0;
                    int configs = 0;
                    HashSet <ATNConfig> uniqueConfigs = new HashSet <ATNConfig>();

                    for (int i = 0; i < modeToDFA.Length; i++)
                    {
                        DFA dfa = modeToDFA[i];
                        if (dfa == null || dfa.states == null)
                        {
                            continue;
                        }

                        states += dfa.states.Count;
                        foreach (DFAState state in dfa.states.Values)
                        {
                            configs += state.configs.Count;
                            uniqueConfigs.UnionWith(state.configs);
                        }
                    }

                    Console.Out.WriteLine("There are {0} lexer DFAState instances, {1} configs ({2} unique), {3} prediction contexts.", states, configs, uniqueConfigs.Count, lexerInterpreter.atn.ContextCacheSize);
                }
            }

            if (RUN_PARSER && sharedParsers.Length > 0)
            {
                Parser parser = sharedParsers[0];
                // make sure the individual DFAState objects actually have unique ATNConfig arrays
                ParserATNSimulator interpreter = parser.Interpreter;
                DFA[] decisionToDFA            = interpreter.atn.decisionToDFA;

                if (SHOW_DFA_STATE_STATS)
                {
                    int states  = 0;
                    int configs = 0;
                    HashSet <ATNConfig> uniqueConfigs = new HashSet <ATNConfig>();

                    for (int i = 0; i < decisionToDFA.Length; i++)
                    {
                        DFA dfa = decisionToDFA[i];
                        if (dfa == null || dfa.states == null)
                        {
                            continue;
                        }

                        states += dfa.states.Count;
                        foreach (DFAState state in dfa.states.Values)
                        {
                            configs += state.configs.Count;
                            uniqueConfigs.UnionWith(state.configs);
                        }
                    }

                    Console.Out.WriteLine("There are {0} parser DFAState instances, {1} configs ({2} unique), {3} prediction contexts.", states, configs, uniqueConfigs.Count, interpreter.atn.ContextCacheSize);
                }

                int   localDfaCount      = 0;
                int   globalDfaCount     = 0;
                int   localConfigCount   = 0;
                int   globalConfigCount  = 0;
                int[] contextsInDFAState = new int[0];

                for (int i = 0; i < decisionToDFA.Length; i++)
                {
                    DFA dfa = decisionToDFA[i];
                    if (dfa == null || dfa.states == null)
                    {
                        continue;
                    }

                    if (SHOW_CONFIG_STATS)
                    {
                        foreach (DFAState state in dfa.states.Keys)
                        {
                            if (state.configs.Count >= contextsInDFAState.Length)
                            {
                                Array.Resize(ref contextsInDFAState, state.configs.Count + 1);
                            }

                            if (state.IsAcceptState)
                            {
                                bool hasGlobal = false;
                                foreach (ATNConfig config in state.configs)
                                {
                                    if (config.ReachesIntoOuterContext)
                                    {
                                        globalConfigCount++;
                                        hasGlobal = true;
                                    }
                                    else
                                    {
                                        localConfigCount++;
                                    }
                                }

                                if (hasGlobal)
                                {
                                    globalDfaCount++;
                                }
                                else
                                {
                                    localDfaCount++;
                                }
                            }

                            contextsInDFAState[state.configs.Count]++;
                        }
                    }

                    if (EXPORT_LARGEST_CONFIG_CONTEXTS)
                    {
                        foreach (DFAState state in dfa.states.Keys)
                        {
                            foreach (ATNConfig config in state.configs)
                            {
                                string configOutput = config.ToDotString();
                                if (configOutput.Length <= configOutputSize)
                                {
                                    continue;
                                }

                                configOutputSize = configOutput.Length;
                                writeFile(tmpdir, "d" + dfa.decision + ".s" + state.stateNumber + ".a" + config.Alt + ".config.dot", configOutput);
                            }
                        }
                    }
                }

                if (SHOW_CONFIG_STATS && currentPass == 0)
                {
                    Console.Out.WriteLine("  DFA accept states: {0} total, {1} with only local context, {2} with a global context", localDfaCount + globalDfaCount, localDfaCount, globalDfaCount);
                    Console.Out.WriteLine("  Config stats: {0} total, {1} local, {2} global", localConfigCount + globalConfigCount, localConfigCount, globalConfigCount);
                    if (SHOW_DFA_STATE_STATS)
                    {
                        for (int i = 0; i < contextsInDFAState.Length; i++)
                        {
                            if (contextsInDFAState[i] != 0)
                            {
                                Console.Out.WriteLine("  {0} configs = {1}", i, contextsInDFAState[i]);
                            }
                        }
                    }
                }
            }
        }
Exemple #14
0
        private List <LabelInfo> FindLabelsInScope(SnapshotPoint triggerPoint)
        {
            List <LabelInfo> labels = new List <LabelInfo>();

            /* use the experimental model to locate and process the expression */
            Stopwatch stopwatch = Stopwatch.StartNew();

            // lex the entire document
            var currentSnapshot = triggerPoint.Snapshot;
            var input           = new SnapshotCharStream(currentSnapshot, new Span(0, currentSnapshot.Length));
            var lexer           = new ANTLRLexer(input);
            var tokens          = new CommonTokenStream(lexer);

            tokens.Fill();

            // locate the last token before the trigger point
            while (true)
            {
                IToken nextToken = tokens.Lt(1);
                if (nextToken.Type == CharStreamConstants.EndOfFile)
                {
                    break;
                }

                if (nextToken.StartIndex > triggerPoint.Position)
                {
                    break;
                }

                tokens.Consume();
            }

            bool   inAction     = false;
            IToken triggerToken = tokens.LT(-1);

            switch (triggerToken.Type)
            {
            case ANTLRLexer.RULE_REF:
            case ANTLRLexer.TOKEN_REF:
            case ANTLRLexer.DOLLAR:
                break;

            case ANTLRLexer.ACTION:
            case ANTLRLexer.FORCED_ACTION:
            case ANTLRLexer.SEMPRED:
            case ANTLRLexer.ARG_ACTION:
                inAction = true;
                break;

            default:
                return(labels);
            }

            NetworkInterpreter interpreter = CreateNetworkInterpreter(tokens);

            while (interpreter.TryStepBackward())
            {
                if (interpreter.Contexts.Count == 0 || interpreter.Contexts.Count > 4000)
                {
                    break;
                }

                if (interpreter.Contexts.All(context => context.BoundedStart))
                {
                    break;
                }
            }

            if (interpreter.Failed)
            {
                interpreter.Contexts.Clear();
            }

            interpreter.CombineBoundedStartContexts();

            HashSet <IToken> labelTokens = new HashSet <IToken>(TokenIndexEqualityComparer.Default);

            foreach (var context in interpreter.Contexts)
            {
                var tokenTransitions = context.Transitions.Where(i => i.TokenIndex != null).ToList();
                for (int i = 1; i < tokenTransitions.Count - 1; i++)
                {
                    if (tokenTransitions[i].Symbol != ANTLRLexer.TOKEN_REF && tokenTransitions[i].Symbol != ANTLRLexer.RULE_REF)
                    {
                        continue;
                    }

                    // we add explicit labels, plus implicit labels if we're in an action
                    if (tokenTransitions[i + 1].Symbol == ANTLRLexer.ASSIGN || tokenTransitions[i + 1].Symbol == ANTLRLexer.PLUS_ASSIGN)
                    {
                        RuleBinding rule = interpreter.Network.StateRules[tokenTransitions[i + 1].Transition.SourceState.Id];
                        if (rule.Name == AntlrAtnBuilder.RuleNames.TreeRoot || rule.Name == AntlrAtnBuilder.RuleNames.ElementNoOptionSpec)
                        {
                            labelTokens.Add(tokenTransitions[i].Token);
                        }
                    }
                    else if (inAction && tokenTransitions[i - 1].Symbol != ANTLRLexer.ASSIGN && tokenTransitions[i - 1].Symbol != ANTLRLexer.PLUS_ASSIGN)
                    {
                        RuleBinding rule = interpreter.Network.StateRules[tokenTransitions[i].Transition.SourceState.Id];
                        if (rule.Name == AntlrAtnBuilder.RuleNames.Terminal || rule.Name == AntlrAtnBuilder.RuleNames.NotTerminal || rule.Name == AntlrAtnBuilder.RuleNames.RuleRef)
                        {
                            labelTokens.Add(tokenTransitions[i].Token);
                        }
                    }
                }
            }

            foreach (var token in labelTokens)
            {
                labels.Add(new LabelInfo(token.Text, "(label) " + token.Text, new SnapshotSpan(triggerPoint.Snapshot, Span.FromBounds(token.StartIndex, token.StopIndex + 1)), StandardGlyphGroup.GlyphGroupField, Enumerable.Empty <LabelInfo>()));
            }

            /* add scopes */
            if (inAction)
            {
                /* add global scopes */
                IList <IToken> tokensList = tokens.GetTokens();
                for (int i = 0; i < tokensList.Count - 1; i++)
                {
                    var token = tokensList[i];

                    /* all global scopes appear before the first rule. before the first rule, the only place a ':' can appear is
                     * in the form '::' for things like @lexer::namespace{}
                     */
                    if (token.Type == ANTLRLexer.COLON && tokensList[i + 1].Type == ANTLRLexer.COLON)
                    {
                        break;
                    }

                    if (token.Type == ANTLRLexer.SCOPE)
                    {
                        var nextToken = tokensList.Skip(i + 1).FirstOrDefault(t => t.Channel == TokenChannels.Default);
                        if (nextToken != null && (nextToken.Type == ANTLRLexer.RULE_REF || nextToken.Type == ANTLRLexer.TOKEN_REF))
                        {
                            // TODO: parse scope members
                            IToken actionToken = tokensList.Skip(nextToken.TokenIndex + 1).FirstOrDefault(t => t.Channel == TokenChannels.Default);
                            IEnumerable <LabelInfo> members = Enumerable.Empty <LabelInfo>();

                            if (actionToken != null && actionToken.Type == ANTLRLexer.ACTION)
                            {
                                IEnumerable <IToken> scopeMembers = ExtractScopeAttributes(nextToken);
                                members = scopeMembers.Select(member =>
                                {
                                    string name              = member.Text;
                                    SnapshotSpan definition  = new SnapshotSpan(triggerPoint.Snapshot, Span.FromBounds(member.StartIndex, member.StopIndex + 1));
                                    StandardGlyphGroup glyph = StandardGlyphGroup.GlyphGroupField;
                                    IEnumerable <LabelInfo> nestedMembers = Enumerable.Empty <LabelInfo>();
                                    return(new LabelInfo(name, string.Empty, definition, glyph, nestedMembers));
                                });
                            }

                            labels.Add(new LabelInfo(nextToken.Text, "(global scope) " + nextToken.Text, new SnapshotSpan(triggerPoint.Snapshot, Span.FromBounds(nextToken.StartIndex, nextToken.StopIndex + 1)), StandardGlyphGroup.GlyphGroupNamespace, members));
                        }
                    }
                }

                /* add rule scopes */
                // todo
            }

            /* add arguments and return values */
            if (inAction)
            {
                HashSet <IToken> argumentTokens = new HashSet <IToken>(TokenIndexEqualityComparer.Default);
                foreach (var context in interpreter.Contexts)
                {
                    var tokenTransitions = context.Transitions.Where(i => i.TokenIndex != null).ToList();
                    for (int i = 1; i < tokenTransitions.Count; i++)
                    {
                        if (tokenTransitions[i].Symbol == ANTLRLexer.RETURNS || tokenTransitions[i].Symbol == ANTLRLexer.COLON)
                        {
                            break;
                        }

                        if (tokenTransitions[i].Symbol == ANTLRLexer.ARG_ACTION)
                        {
                            argumentTokens.Add(tokenTransitions[i].Token);
                        }
                    }
                }

                foreach (var token in argumentTokens)
                {
                    IEnumerable <IToken> arguments = ExtractArguments(token);
                    foreach (var argument in arguments)
                    {
                        labels.Add(new LabelInfo(argument.Text, "(parameter) " + argument.Text, new SnapshotSpan(triggerPoint.Snapshot, Span.FromBounds(argument.StartIndex, argument.StopIndex + 1)), StandardGlyphGroup.GlyphGroupVariable, Enumerable.Empty <LabelInfo>()));
                    }
                }
            }

            /* add return values */
            if (inAction)
            {
                HashSet <IToken> returnTokens = new HashSet <IToken>(TokenIndexEqualityComparer.Default);
                foreach (var context in interpreter.Contexts)
                {
                    var tokenTransitions = context.Transitions.Where(i => i.TokenIndex != null).ToList();
                    for (int i = 1; i < tokenTransitions.Count - 1; i++)
                    {
                        if (tokenTransitions[i].Symbol == ANTLRLexer.COLON)
                        {
                            break;
                        }

                        if (tokenTransitions[i].Symbol == ANTLRLexer.RETURNS)
                        {
                            if (tokenTransitions[i + 1].Symbol == ANTLRLexer.ARG_ACTION)
                            {
                                returnTokens.Add(tokenTransitions[i + 1].Token);
                            }

                            break;
                        }
                    }
                }

                foreach (var token in returnTokens)
                {
                    IEnumerable <IToken> returnValues = ExtractArguments(token);
                    foreach (var returnValue in returnValues)
                    {
                        labels.Add(new LabelInfo(returnValue.Text, "(return value) " + returnValue.Text, new SnapshotSpan(triggerPoint.Snapshot, Span.FromBounds(returnValue.StartIndex, returnValue.StopIndex + 1)), StandardGlyphGroup.GlyphGroupVariable, Enumerable.Empty <LabelInfo>()));
                    }
                }
            }

            /* add intrinsic labels ($start, $type, $text, $enclosingRuleName) */
            IToken           ruleNameToken           = null;
            HashSet <IToken> enclosingRuleNameTokens = new HashSet <IToken>(TokenIndexEqualityComparer.Default);

            foreach (var context in interpreter.Contexts)
            {
                var tokenTransitions = context.Transitions.Where(i => i.Symbol == ANTLRLexer.RULE_REF || i.Symbol == ANTLRLexer.TOKEN_REF).ToList();
                if (!tokenTransitions.Any())
                {
                    continue;
                }

                ruleNameToken = tokenTransitions.First().Token;
                if (ruleNameToken != null)
                {
                    enclosingRuleNameTokens.Add(ruleNameToken);
                }
            }

            foreach (var token in enclosingRuleNameTokens)
            {
                // TODO: add members
                labels.Add(new LabelInfo(token.Text, "(enclosing rule) " + token.Text, new SnapshotSpan(triggerPoint.Snapshot, Span.FromBounds(token.StartIndex, token.StopIndex + 1)), StandardGlyphGroup.GlyphGroupIntrinsic, Enumerable.Empty <LabelInfo>()));
            }

            GrammarType grammarType = GrammarType.None;

            int mark = tokens.Mark();

            try
            {
                tokens.Seek(0);
                bool hasGrammarType = false;
                while (!hasGrammarType)
                {
                    int la1 = tokens.LA(1);
                    switch (la1)
                    {
                    case ANTLRLexer.GRAMMAR:
                        IToken previous = tokens.LT(-1);
                        if (previous == null)
                        {
                            grammarType = GrammarType.Combined;
                        }
                        else if (previous.Type == ANTLRLexer.LEXER)
                        {
                            grammarType = GrammarType.Lexer;
                        }
                        else if (previous.Type == ANTLRLexer.PARSER)
                        {
                            grammarType = GrammarType.Parser;
                        }
                        else if (previous.Type == ANTLRLexer.TREE)
                        {
                            grammarType = GrammarType.TreeParser;
                        }
                        else
                        {
                            grammarType = GrammarType.None;
                        }

                        hasGrammarType = true;
                        break;

                    case CharStreamConstants.EndOfFile:
                        hasGrammarType = true;
                        break;

                    default:
                        break;
                    }

                    tokens.Consume();
                }
            }
            finally
            {
                tokens.Rewind(mark);
            }

            if (inAction)
            {
                switch (grammarType)
                {
                case GrammarType.Combined:
                    if (ruleNameToken == null)
                    {
                        goto default;
                    }
                    if (ruleNameToken.Type == ANTLRLexer.RULE_REF)
                    {
                        goto case GrammarType.Parser;
                    }
                    else
                    {
                        goto case GrammarType.Lexer;
                    }

                case GrammarType.Lexer:
                    labels.Add(new LabelInfo("text", string.Empty, new SnapshotSpan(), StandardGlyphGroup.GlyphGroupIntrinsic, Enumerable.Empty <LabelInfo>()));
                    labels.Add(new LabelInfo("type", string.Empty, new SnapshotSpan(), StandardGlyphGroup.GlyphGroupIntrinsic, Enumerable.Empty <LabelInfo>()));
                    labels.Add(new LabelInfo("line", string.Empty, new SnapshotSpan(), StandardGlyphGroup.GlyphGroupIntrinsic, Enumerable.Empty <LabelInfo>()));
                    labels.Add(new LabelInfo("index", string.Empty, new SnapshotSpan(), StandardGlyphGroup.GlyphGroupIntrinsic, Enumerable.Empty <LabelInfo>()));
                    labels.Add(new LabelInfo("pos", string.Empty, new SnapshotSpan(), StandardGlyphGroup.GlyphGroupIntrinsic, Enumerable.Empty <LabelInfo>()));
                    labels.Add(new LabelInfo("channel", string.Empty, new SnapshotSpan(), StandardGlyphGroup.GlyphGroupIntrinsic, Enumerable.Empty <LabelInfo>()));
                    labels.Add(new LabelInfo("start", string.Empty, new SnapshotSpan(), StandardGlyphGroup.GlyphGroupIntrinsic, Enumerable.Empty <LabelInfo>()));
                    labels.Add(new LabelInfo("stop", string.Empty, new SnapshotSpan(), StandardGlyphGroup.GlyphGroupIntrinsic, Enumerable.Empty <LabelInfo>()));
                    labels.Add(new LabelInfo("int", string.Empty, new SnapshotSpan(), StandardGlyphGroup.GlyphGroupIntrinsic, Enumerable.Empty <LabelInfo>()));
                    break;

                case GrammarType.Parser:
                    labels.Add(new LabelInfo("text", string.Empty, new SnapshotSpan(), StandardGlyphGroup.GlyphGroupIntrinsic, Enumerable.Empty <LabelInfo>()));
                    labels.Add(new LabelInfo("start", string.Empty, new SnapshotSpan(), StandardGlyphGroup.GlyphGroupIntrinsic, Enumerable.Empty <LabelInfo>()));
                    labels.Add(new LabelInfo("stop", string.Empty, new SnapshotSpan(), StandardGlyphGroup.GlyphGroupIntrinsic, Enumerable.Empty <LabelInfo>()));
                    labels.Add(new LabelInfo("tree", string.Empty, new SnapshotSpan(), StandardGlyphGroup.GlyphGroupIntrinsic, Enumerable.Empty <LabelInfo>()));
                    labels.Add(new LabelInfo("st", string.Empty, new SnapshotSpan(), StandardGlyphGroup.GlyphGroupIntrinsic, Enumerable.Empty <LabelInfo>()));
                    break;

                case GrammarType.TreeParser:
                    labels.Add(new LabelInfo("text", string.Empty, new SnapshotSpan(), StandardGlyphGroup.GlyphGroupIntrinsic, Enumerable.Empty <LabelInfo>()));
                    labels.Add(new LabelInfo("start", string.Empty, new SnapshotSpan(), StandardGlyphGroup.GlyphGroupIntrinsic, Enumerable.Empty <LabelInfo>()));
                    labels.Add(new LabelInfo("tree", string.Empty, new SnapshotSpan(), StandardGlyphGroup.GlyphGroupIntrinsic, Enumerable.Empty <LabelInfo>()));
                    labels.Add(new LabelInfo("st", string.Empty, new SnapshotSpan(), StandardGlyphGroup.GlyphGroupIntrinsic, Enumerable.Empty <LabelInfo>()));
                    break;

                default:
                    // if we're unsure about the grammar type, include all the possible options to make sure we're covered
                    labels.Add(new LabelInfo("text", string.Empty, new SnapshotSpan(), StandardGlyphGroup.GlyphGroupIntrinsic, Enumerable.Empty <LabelInfo>()));
                    labels.Add(new LabelInfo("type", string.Empty, new SnapshotSpan(), StandardGlyphGroup.GlyphGroupIntrinsic, Enumerable.Empty <LabelInfo>()));
                    labels.Add(new LabelInfo("line", string.Empty, new SnapshotSpan(), StandardGlyphGroup.GlyphGroupIntrinsic, Enumerable.Empty <LabelInfo>()));
                    labels.Add(new LabelInfo("index", string.Empty, new SnapshotSpan(), StandardGlyphGroup.GlyphGroupIntrinsic, Enumerable.Empty <LabelInfo>()));
                    labels.Add(new LabelInfo("pos", string.Empty, new SnapshotSpan(), StandardGlyphGroup.GlyphGroupIntrinsic, Enumerable.Empty <LabelInfo>()));
                    labels.Add(new LabelInfo("channel", string.Empty, new SnapshotSpan(), StandardGlyphGroup.GlyphGroupIntrinsic, Enumerable.Empty <LabelInfo>()));
                    labels.Add(new LabelInfo("start", string.Empty, new SnapshotSpan(), StandardGlyphGroup.GlyphGroupIntrinsic, Enumerable.Empty <LabelInfo>()));
                    labels.Add(new LabelInfo("stop", string.Empty, new SnapshotSpan(), StandardGlyphGroup.GlyphGroupIntrinsic, Enumerable.Empty <LabelInfo>()));
                    labels.Add(new LabelInfo("int", string.Empty, new SnapshotSpan(), StandardGlyphGroup.GlyphGroupIntrinsic, Enumerable.Empty <LabelInfo>()));
                    labels.Add(new LabelInfo("tree", string.Empty, new SnapshotSpan(), StandardGlyphGroup.GlyphGroupIntrinsic, Enumerable.Empty <LabelInfo>()));
                    labels.Add(new LabelInfo("st", string.Empty, new SnapshotSpan(), StandardGlyphGroup.GlyphGroupIntrinsic, Enumerable.Empty <LabelInfo>()));
                    break;
                }
            }

            return(labels);
        }
Exemple #15
0
        /// <inheritdoc/>
        protected override void ReParseImpl()
        {
            Stopwatch stopwatch = Stopwatch.StartNew();

            ITextSnapshot snapshot = TextBuffer.CurrentSnapshot;

            try
            {
                ITextDocument   textDocument = TextDocument;
                string          fileName     = textDocument != null ? textDocument.FilePath : null;
                IDECompilerHost host         = new IDECompilerHost();
                IProject        project      = null;

                ILangService languageService;
                LangService_GetInstance(out languageService);
                if (languageService != null)
                {
                    ICSharpTextBuffer csharpBuffer = languageService.FindTextBuffer(fileName);
                    if (csharpBuffer != null)
                    {
                        project = csharpBuffer.Project;
                    }
                }

                List <ITagSpan <IInheritanceTag> > tags = new List <ITagSpan <IInheritanceTag> >();

                if (host != null && project != null && !string.IsNullOrEmpty(fileName))
                {
                    Compilation compilation = host.CreateCompiler(project).GetCompilation();
                    SourceFile  sourceFile;
                    if (!compilation.SourceFiles.TryGetValue(new FileName(fileName), out sourceFile))
                    {
                        InheritanceParseResultEventArgs errorResult = new InheritanceParseResultEventArgs(snapshot, stopwatch.Elapsed, tags);
                        OnParseComplete(errorResult);
                        return;
                    }

                    ParseTree parseTree = sourceFile.GetParseTree();

                    SpecializedMatchingMemberCollector collector = new SpecializedMatchingMemberCollector(host.Compilers.Select(i => i.GetCompilation()), false);

                    IEnumerable <ParseTreeNode> nodes = SelectTypes(parseTree);
                    foreach (var node in nodes)
                    {
                        CSharpType type = null;

                        type = compilation.GetTypeFromTypeDeclaration(node);
                        if (type == null)
                        {
                            MarkDirty(true);
                            return;
                        }

                        if (type.IsSealed)
                        {
                            continue;
                        }

                        // types which implement or derive from this type
                        ISet <CSharpType> derivedClasses = collector.GetDerivedTypes(type.SymbolicIdentifier);

                        if (derivedClasses.Count == 0)
                        {
                            continue;
                        }

                        StringBuilder builder = new StringBuilder();
                        string        elementKindDisplayName =
                            "types";

                        builder.AppendLine("Derived " + elementKindDisplayName + ":");
                        foreach (var derived in derivedClasses)
                        {
                            builder.AppendLine("    " + derived.GetFullTypeName());
                        }

                        int               nameIndex = node.Token;
                        Token             token     = parseTree.LexData.Tokens[nameIndex];
                        ITextSnapshotLine line      = snapshot.GetLineFromLineNumber(token.StartPosition.Line);
                        SnapshotSpan      span      = new SnapshotSpan(snapshot, new Span(line.Start + token.StartPosition.Character, token.EndPosition.Character - token.StartPosition.Character));

                        InheritanceGlyph tag = type.IsInterface ? InheritanceGlyph.HasImplementations : InheritanceGlyph.Overridden;

                        var targets = derivedClasses.Select(i => new TypeTarget(i.GetFullTypeName(), i.SymbolicIdentifier));
                        tags.Add(new TagSpan <IInheritanceTag>(span, _tagFactory.CreateTag(tag, builder.ToString().TrimEnd(), targets)));
                    }

                    nodes = parseTree.SelectMethodsPropertiesAndFields();
                    nodes = nodes.SelectMany(SelectDeclaratorsFromFields);
                    foreach (var node in nodes)
                    {
                        if (node is AccessorDeclarationNode)
                        {
                            // these nodes always result in an ArgumentException in GetMemberFromMemberDeclaration
                            continue;
                        }

                        CSharpMember member;
                        try
                        {
                            member = compilation.GetMemberFromMemberDeclaration(node);
                        }
                        catch (ArgumentException)
                        {
                            continue;
                        }

                        if (member == null)
                        {
                            MarkDirty(true);
                            return;
                        }

                        if (!SpecializedMatchingMemberCollector.IsSupportedMemberType(member))
                        {
                            continue;
                        }

                        // methods which this method implements
                        ISet <CSharpMemberIdentifier> implementedMethods = collector.GetImplementedInterfaceMembers(member.SymbolicIdentifier);

                        // methods which this method overrides
                        ISet <CSharpMemberIdentifier> overriddenMethods = collector.GetOverriddenBaseMembers(member.SymbolicIdentifier);

                        // methods which override this method
                        ISet <CSharpMemberIdentifier> overridingMethods = collector.GetOverridersFromDerivedTypes(member.SymbolicIdentifier);

                        // methods which implement this method
                        ISet <CSharpMemberIdentifier> implementingMethods = collector.GetImplementorsForInterfaceMember(member.SymbolicIdentifier);

                        if (implementingMethods.Count == 0 && implementedMethods.Count == 0 && overriddenMethods.Count == 0 && overridingMethods.Count == 0)
                        {
                            continue;
                        }

                        StringBuilder builder = new StringBuilder();
                        string        elementKindDisplayName =
                            member.IsProperty ? "properties" :
                            member.IsEvent ? "events" :
                            "methods";

                        if (implementedMethods.Count > 0)
                        {
                            builder.AppendLine("Implemented " + elementKindDisplayName + ":");
                            foreach (var methodId in implementedMethods)
                            {
                                builder.AppendLine("    " + methodId.ToString());
                            }
                        }

                        if (overriddenMethods.Count > 0)
                        {
                            builder.AppendLine("Overridden " + elementKindDisplayName + ":");
                            foreach (var methodId in overriddenMethods)
                            {
                                builder.AppendLine("    " + methodId.ToString());
                            }
                        }

                        if (implementingMethods.Count > 0)
                        {
                            builder.AppendLine("Implementing " + elementKindDisplayName + " in derived types:");
                            foreach (var methodId in implementingMethods)
                            {
                                builder.AppendLine("    " + methodId.ToString());
                            }
                        }

                        if (overridingMethods.Count > 0)
                        {
                            builder.AppendLine("Overriding " + elementKindDisplayName + " in derived types:");
                            foreach (var methodId in overridingMethods)
                            {
                                builder.AppendLine("    " + methodId.ToString());
                            }
                        }

                        int               nameIndex = node.Token;
                        Token             token     = parseTree.LexData.Tokens[nameIndex];
                        ITextSnapshotLine line      = snapshot.GetLineFromLineNumber(token.StartPosition.Line);
                        SnapshotSpan      span      = new SnapshotSpan(snapshot, new Span(line.Start + token.StartPosition.Character, token.EndPosition.Character - token.StartPosition.Character));

                        InheritanceGlyph tag;
                        if (implementedMethods.Count > 0)
                        {
                            if (overridingMethods.Count > 0)
                            {
                                tag = InheritanceGlyph.ImplementsAndOverridden;
                            }
                            else if (implementingMethods.Count > 0)
                            {
                                tag = InheritanceGlyph.ImplementsAndHasImplementations;
                            }
                            else
                            {
                                tag = InheritanceGlyph.Implements;
                            }
                        }
                        else if (implementingMethods.Count > 0)
                        {
                            tag = InheritanceGlyph.HasImplementations;
                        }
                        else if (overriddenMethods.Count > 0)
                        {
                            if (overridingMethods.Count > 0)
                            {
                                tag = InheritanceGlyph.OverridesAndOverridden;
                            }
                            else
                            {
                                tag = InheritanceGlyph.Overrides;
                            }
                        }
                        else
                        {
                            tag = InheritanceGlyph.Overridden;
                        }

                        List <CSharpMemberIdentifier> members = new List <CSharpMemberIdentifier>();
                        members.AddRange(implementedMethods);
                        members.AddRange(overriddenMethods);
                        members.AddRange(implementingMethods);
                        members.AddRange(overridingMethods);

                        var targets = members.Select(i => new MemberTarget(i));
                        tags.Add(new TagSpan <IInheritanceTag>(span, _tagFactory.CreateTag(tag, builder.ToString().TrimEnd(), targets)));
                    }
                }

                InheritanceParseResultEventArgs result = new InheritanceParseResultEventArgs(snapshot, stopwatch.Elapsed, tags);
                OnParseComplete(result);
            }
            catch (InvalidOperationException)
            {
                MarkDirty(true);
                throw;
            }
        }
Exemple #16
0
        /// <inheritdoc/>
        protected override void ReParseImpl()
        {
            Stopwatch stopwatch = Stopwatch.StartNew();

            ITextSnapshot snapshot = TextBuffer.CurrentSnapshot;

            try
            {
                ITextDocument       textDocument  = TextDocument;
                string              fileName      = textDocument != null ? textDocument.FilePath : null;
                Document            document      = snapshot.GetOpenDocumentInCurrentContextWithChanges();
                SourceTextContainer textContainer = document != null?document.GetTextAsync().Result.Container : null;

                Project  project  = document != null ? document.Project : null;
                Solution solution = project != null ? project.Solution : null;

                List <ITagSpan <IInheritanceTag> > tags = new List <ITagSpan <IInheritanceTag> >();

                if (document != null && !string.IsNullOrEmpty(fileName))
                {
                    SyntaxTree    syntaxTree    = document.GetSyntaxTreeAsync().Result;
                    SyntaxNode    syntaxRoot    = syntaxTree.GetRoot();
                    SemanticModel semanticModel = document.GetSemanticModelAsync().Result;
                    Compilation   compilation   = semanticModel.Compilation;

                    IDictionary <ISymbol, ISet <ISymbol> > interfaceImplementations = new Dictionary <ISymbol, ISet <ISymbol> >();

                    List <CSharpSyntaxNode> allMembers = new List <CSharpSyntaxNode>();
                    IEnumerable <BaseTypeDeclarationSyntax> typeNodes = syntaxRoot.DescendantNodes().OfType <BaseTypeDeclarationSyntax>();
                    foreach (var typeNode in typeNodes)
                    {
                        ISymbol symbol = semanticModel.GetDeclaredSymbol(typeNode);
                        if (symbol == null)
                        {
                            MarkDirty(true);
                            return;
                        }

                        INamedTypeSymbol typeSymbol = symbol as INamedTypeSymbol;
                        if (typeSymbol == null)
                        {
                            continue;
                        }

                        // get implemented interface symbols
                        foreach (INamedTypeSymbol namedTypeSymbol in typeSymbol.AllInterfaces)
                        {
                            foreach (ISymbol member in namedTypeSymbol.GetMembers())
                            {
                                ISymbol implementation = typeSymbol.FindImplementationForInterfaceMember(member);
                                if (implementation == null || !implementation.ContainingSymbol.Equals(typeSymbol))
                                {
                                    continue;
                                }

                                ISet <ISymbol> symbols;
                                if (!interfaceImplementations.TryGetValue(implementation, out symbols))
                                {
                                    symbols = new HashSet <ISymbol>();
                                    interfaceImplementations[implementation] = symbols;
                                }

                                symbols.Add(member);
                            }
                        }

                        TypeDeclarationSyntax typeDeclarationSyntax = typeNode as TypeDeclarationSyntax;
                        if (typeDeclarationSyntax != null)
                        {
                            allMembers.AddRange(typeDeclarationSyntax.Members);
                        }

                        if (typeSymbol.IsSealed)
                        {
                            continue;
                        }

                        // types which implement or derive from this type
                        ISet <ITypeSymbol> derivedTypes = new HashSet <ITypeSymbol>();
                        derivedTypes.UnionWith(FindDerivedClassesAsync.Value(typeSymbol, solution, null, CancellationToken.None).Result);
                        derivedTypes.UnionWith(FindDerivedInterfacesAsync.Value(typeSymbol, solution, null, CancellationToken.None).Result);
                        derivedTypes.UnionWith(FindImplementingTypesAsync.Value(typeSymbol, solution, null, CancellationToken.None).Result);

                        if (derivedTypes.Count == 0)
                        {
                            continue;
                        }

                        StringBuilder builder = new StringBuilder();
                        string        elementKindDisplayName =
                            "types";

                        builder.AppendLine("Derived " + elementKindDisplayName + ":");
                        foreach (var derived in derivedTypes)
                        {
                            builder.AppendLine("    " + derived.ToString());
                        }

                        SyntaxToken  identifier = typeNode.Accept(IdentifierSyntaxVisitor.Instance);
                        SnapshotSpan span       = new SnapshotSpan(snapshot, new Span(identifier.SpanStart, identifier.Span.Length));

                        InheritanceGlyph tag = typeSymbol.TypeKind == TypeKind.Interface ? InheritanceGlyph.HasImplementations : InheritanceGlyph.Overridden;

                        var targets = derivedTypes.Select(i => new TypeTarget(textContainer, project, solution, i));
                        tags.Add(new TagSpan <IInheritanceTag>(span, _tagFactory.CreateTag(tag, builder.ToString().TrimEnd(), targets)));
                    }

                    foreach (var eventFieldDeclarationSyntax in allMembers.OfType <EventFieldDeclarationSyntax>().ToArray())
                    {
                        allMembers.AddRange(eventFieldDeclarationSyntax.Declaration.Variables);
                    }

                    foreach (CSharpSyntaxNode memberNode in allMembers)
                    {
                        if (!(memberNode is MethodDeclarationSyntax) &&
                            !(memberNode is PropertyDeclarationSyntax) &&
                            !(memberNode is IndexerDeclarationSyntax) &&
                            !(memberNode is EventDeclarationSyntax) &&
                            !(memberNode is EventFieldDeclarationSyntax) &&
                            !(memberNode is VariableDeclaratorSyntax))
                        {
                            continue;
                        }

                        ISymbol symbol = semanticModel.GetDeclaredSymbol(memberNode);
                        if (symbol == null)
                        {
                            MarkDirty(true);
                            return;
                        }

                        // members which this member implements
                        ISet <ISymbol> implementedMethods = new HashSet <ISymbol>();
                        if (!interfaceImplementations.TryGetValue(symbol, out implementedMethods))
                        {
                            implementedMethods = new HashSet <ISymbol>();
                        }

                        ISet <ISymbol> overriddenMethods = new HashSet <ISymbol>();

                        IMethodSymbol methodSymbol = symbol as IMethodSymbol;
                        if (methodSymbol != null)
                        {
                            // methods which this method overrides
                            for (IMethodSymbol current = methodSymbol.OverriddenMethod;
                                 current != null;
                                 current = current.OverriddenMethod)
                            {
                                overriddenMethods.Add(current);
                            }
                        }
                        else
                        {
                            IPropertySymbol propertySymbol = symbol as IPropertySymbol;
                            if (propertySymbol != null)
                            {
                                // properties which this property overrides
                                for (IPropertySymbol current = propertySymbol.OverriddenProperty;
                                     current != null;
                                     current = current.OverriddenProperty)
                                {
                                    overriddenMethods.Add(current);
                                }
                            }
                            else
                            {
                                IEventSymbol eventSymbol = symbol as IEventSymbol;
                                if (eventSymbol != null)
                                {
                                    // events which this event overrides
                                    for (IEventSymbol current = eventSymbol.OverriddenEvent;
                                         current != null;
                                         current = current.OverriddenEvent)
                                    {
                                        overriddenMethods.Add(current);
                                    }
                                }
                            }
                        }

                        ISet <ISymbol> implementingMethods = new HashSet <ISymbol>(SymbolFinder.FindImplementationsAsync(symbol, solution).Result);

                        ISet <ISymbol> overridingMethods = new HashSet <ISymbol>(SymbolFinder.FindOverridesAsync(symbol, solution).Result);

                        if (implementingMethods.Count == 0 && implementedMethods.Count == 0 && overriddenMethods.Count == 0 && overridingMethods.Count == 0)
                        {
                            continue;
                        }

                        StringBuilder builder = new StringBuilder();
                        string        elementKindDisplayName =
                            symbol is IPropertySymbol ? "properties" :
                            symbol is IEventSymbol ? "events" :
                            "methods";

                        if (implementedMethods.Count > 0)
                        {
                            builder.AppendLine("Implemented " + elementKindDisplayName + ":");
                            foreach (var methodId in implementedMethods)
                            {
                                builder.AppendLine("    " + methodId.ToString());
                            }
                        }

                        if (overriddenMethods.Count > 0)
                        {
                            builder.AppendLine("Overridden " + elementKindDisplayName + ":");
                            foreach (var methodId in overriddenMethods)
                            {
                                builder.AppendLine("    " + methodId.ToString());
                            }
                        }

                        if (implementingMethods.Count > 0)
                        {
                            builder.AppendLine("Implementing " + elementKindDisplayName + " in derived types:");
                            foreach (var methodId in implementingMethods)
                            {
                                builder.AppendLine("    " + methodId.ToString());
                            }
                        }

                        if (overridingMethods.Count > 0)
                        {
                            builder.AppendLine("Overriding " + elementKindDisplayName + " in derived types:");
                            foreach (var methodId in overridingMethods)
                            {
                                builder.AppendLine("    " + methodId.ToString());
                            }
                        }

                        SyntaxToken  identifier = memberNode.Accept(IdentifierSyntaxVisitor.Instance);
                        SnapshotSpan span       = new SnapshotSpan(snapshot, new Span(identifier.SpanStart, identifier.Span.Length));

                        InheritanceGlyph tag;
                        if (implementedMethods.Count > 0)
                        {
                            if (overridingMethods.Count > 0)
                            {
                                tag = InheritanceGlyph.ImplementsAndOverridden;
                            }
                            else if (implementingMethods.Count > 0)
                            {
                                tag = InheritanceGlyph.ImplementsAndHasImplementations;
                            }
                            else
                            {
                                tag = InheritanceGlyph.Implements;
                            }
                        }
                        else if (implementingMethods.Count > 0)
                        {
                            tag = InheritanceGlyph.HasImplementations;
                        }
                        else if (overriddenMethods.Count > 0)
                        {
                            if (overridingMethods.Count > 0)
                            {
                                tag = InheritanceGlyph.OverridesAndOverridden;
                            }
                            else
                            {
                                tag = InheritanceGlyph.Overrides;
                            }
                        }
                        else
                        {
                            tag = InheritanceGlyph.Overridden;
                        }

                        List <ISymbol> members = new List <ISymbol>();
                        members.AddRange(implementedMethods);
                        members.AddRange(overriddenMethods);
                        members.AddRange(implementingMethods);
                        members.AddRange(overridingMethods);

                        var targets = members.Select(i => new MemberTarget(textContainer, project, solution, i));
                        tags.Add(new TagSpan <IInheritanceTag>(span, _tagFactory.CreateTag(tag, builder.ToString().TrimEnd(), targets)));
                    }
                }

                InheritanceParseResultEventArgs result = new InheritanceParseResultEventArgs(snapshot, stopwatch.Elapsed, tags);
                OnParseComplete(result);
            }
            catch (InvalidOperationException)
            {
                MarkDirty(true);
                throw;
            }
        }
Exemple #17
0
        public override async Task Run()
        {
            var statisticsDatabase = GetDatabaseRegistration <StatisticsDbConfiguration>();

            var reportGenerationTime = DateTime.UtcNow;
            var destinationContainer = _cloudStorageAccount.CreateCloudBlobClient().GetContainerReference(_statisticsContainerName);

            Logger.LogDebug("Generating reports from {DataSource}/{InitialCatalog} and saving to {AccountName}/{Container}",
                            statisticsDatabase.DataSource, statisticsDatabase.InitialCatalog, _cloudStorageAccount.Credentials.AccountName, destinationContainer.Name);

            var reportBuilderLogger   = LoggerFactory.CreateLogger <ReportBuilder>();
            var reportCollectorLogger = LoggerFactory.CreateLogger <ReportDataCollector>();

            if (string.IsNullOrEmpty(_reportName))
            {
                // generate all reports
                var reportGenerators = new Dictionary <ReportBuilder, ReportDataCollector>
                {
                    {
                        new ReportBuilder(reportBuilderLogger, ReportNames.NuGetClientVersion),
                        new ReportDataCollector(reportCollectorLogger, _storedProcedures[ReportNames.NuGetClientVersion], OpenSqlConnectionAsync <StatisticsDbConfiguration>, _sqlCommandTimeoutSeconds)
                    },

                    {
                        new ReportBuilder(reportBuilderLogger, ReportNames.Last6Weeks),
                        new ReportDataCollector(reportCollectorLogger, _storedProcedures[ReportNames.Last6Weeks], OpenSqlConnectionAsync <StatisticsDbConfiguration>, _sqlCommandTimeoutSeconds)
                    },

                    {
                        new ReportBuilder(reportBuilderLogger, ReportNames.RecentCommunityPopularity),
                        new ReportDataCollector(reportCollectorLogger, _storedProcedures[ReportNames.RecentCommunityPopularity], OpenSqlConnectionAsync <StatisticsDbConfiguration>, _sqlCommandTimeoutSeconds)
                    },

                    {
                        new ReportBuilder(reportBuilderLogger, ReportNames.RecentCommunityPopularityDetail),
                        new ReportDataCollector(reportCollectorLogger, _storedProcedures[ReportNames.RecentCommunityPopularityDetail], OpenSqlConnectionAsync <StatisticsDbConfiguration>, _sqlCommandTimeoutSeconds)
                    },

                    {
                        new ReportBuilder(reportBuilderLogger, ReportNames.RecentPopularity),
                        new ReportDataCollector(reportCollectorLogger, _storedProcedures[ReportNames.RecentPopularity], OpenSqlConnectionAsync <StatisticsDbConfiguration>, _sqlCommandTimeoutSeconds)
                    },

                    {
                        new ReportBuilder(reportBuilderLogger, ReportNames.RecentPopularityDetail),
                        new ReportDataCollector(reportCollectorLogger, _storedProcedures[ReportNames.RecentPopularityDetail], OpenSqlConnectionAsync <StatisticsDbConfiguration>, _sqlCommandTimeoutSeconds)
                    }
                };

                foreach (var reportGenerator in reportGenerators)
                {
                    await ProcessReport(LoggerFactory, destinationContainer, reportGenerator.Key, reportGenerator.Value, reportGenerationTime);

                    ApplicationInsightsHelper.TrackReportProcessed(reportGenerator.Key.ReportName + " report");
                }

                await RebuildPackageReports(destinationContainer, reportGenerationTime);
                await CleanInactiveRecentPopularityDetailByPackageReports(destinationContainer, reportGenerationTime);
            }
            else
            {
                // generate only the specific report
                var reportBuilder       = new ReportBuilder(reportBuilderLogger, _reportName);
                var reportDataCollector = new ReportDataCollector(reportCollectorLogger, _storedProcedures[_reportName], OpenSqlConnectionAsync <StatisticsDbConfiguration>, _sqlCommandTimeoutSeconds);

                await ProcessReport(LoggerFactory, destinationContainer, reportBuilder, reportDataCollector, reportGenerationTime);
            }

            Logger.LogInformation("Generated reports from {DataSource}/{InitialCatalog} and saving to {AccountName}/{Container}",
                                  statisticsDatabase.DataSource, statisticsDatabase.InitialCatalog, _cloudStorageAccount.Credentials.AccountName, destinationContainer.Name);

            // totals reports
            var stopwatch = Stopwatch.StartNew();

            // build downloads.v1.json
            var targets = new List <StorageContainerTarget>();

            targets.Add(new StorageContainerTarget(_cloudStorageAccount, _statisticsContainerName));
            foreach (var dataContainerName in _dataContainerNames)
            {
                targets.Add(new StorageContainerTarget(_dataStorageAccount, dataContainerName));
            }

            var downloadCountReport = new DownloadCountReport(
                LoggerFactory.CreateLogger <DownloadCountReport>(),
                targets,
                OpenSqlConnectionAsync <StatisticsDbConfiguration>,
                OpenSqlConnectionAsync <GalleryDbConfiguration>);
            await downloadCountReport.Run();

            stopwatch.Stop();
            ApplicationInsightsHelper.TrackMetric(DownloadCountReport.ReportName + " Generation Time (ms)", stopwatch.ElapsedMilliseconds);
            ApplicationInsightsHelper.TrackReportProcessed(DownloadCountReport.ReportName);
            stopwatch.Restart();

            // build stats-totals.json
            var galleryTotalsReport = new GalleryTotalsReport(
                LoggerFactory.CreateLogger <GalleryTotalsReport>(),
                _cloudStorageAccount,
                _statisticsContainerName,
                OpenSqlConnectionAsync <StatisticsDbConfiguration>,
                OpenSqlConnectionAsync <GalleryDbConfiguration>);
            await galleryTotalsReport.Run();

            stopwatch.Stop();
            ApplicationInsightsHelper.TrackMetric(GalleryTotalsReport.ReportName + " Generation Time (ms)", stopwatch.ElapsedMilliseconds);
            ApplicationInsightsHelper.TrackReportProcessed(GalleryTotalsReport.ReportName);


            // build tools.v1.json
            var toolsReport = new DownloadsPerToolVersionReport(
                LoggerFactory.CreateLogger <DownloadsPerToolVersionReport>(),
                _cloudStorageAccount,
                _statisticsContainerName,
                OpenSqlConnectionAsync <StatisticsDbConfiguration>,
                OpenSqlConnectionAsync <GalleryDbConfiguration>);
            await toolsReport.Run();

            stopwatch.Stop();
            ApplicationInsightsHelper.TrackMetric(DownloadsPerToolVersionReport.ReportName + " Generation Time (ms)", stopwatch.ElapsedMilliseconds);
            ApplicationInsightsHelper.TrackReportProcessed(DownloadsPerToolVersionReport.ReportName);
            stopwatch.Restart();
        }
Exemple #18
0
        public override async Task Run()
        {
            var statisticsDatabase = GetDatabaseRegistration <StatisticsDbConfiguration>();

            var reportGenerationTime = DateTime.UtcNow;
            var destinationContainer = _cloudStorageAccount.CreateCloudBlobClient().GetContainerReference(_statisticsContainerName);

            Logger.LogDebug("Generating reports from {DataSource}/{InitialCatalog} and saving to {AccountName}/{Container}",
                            statisticsDatabase.DataSource, statisticsDatabase.InitialCatalog, _cloudStorageAccount.Credentials.AccountName, destinationContainer.Name);

            var reportBuilderLogger   = LoggerFactory.CreateLogger <ReportBuilder>();
            var reportCollectorLogger = LoggerFactory.CreateLogger <ReportDataCollector>();

            if (string.IsNullOrEmpty(_reportNameConfig))
            {
                // generate all reports
                foreach (var reportName in ReportNames.StandardReports)
                {
                    await GenerateStandardReport(reportName, reportGenerationTime, destinationContainer, reportBuilderLogger, reportCollectorLogger);
                }

                await RebuildPackageReports(destinationContainer, reportGenerationTime);
                await CleanInactiveRecentPopularityDetailByPackageReports(destinationContainer, reportGenerationTime);
            }
            else if (ReportNames.StandardReports.Contains(_reportNameConfig))
            {
                // generate only the specific standard report
                await GenerateStandardReport(_reportNameConfig, reportGenerationTime, destinationContainer, reportBuilderLogger, reportCollectorLogger);
            }
            else if (ShouldGenerateReport(ReportNames.RecentPopularityDetailByPackageId, _reportNameConfig))
            {
                await RebuildPackageReports(destinationContainer, reportGenerationTime);
                await CleanInactiveRecentPopularityDetailByPackageReports(destinationContainer, reportGenerationTime);
            }

            Logger.LogInformation("Generated reports from {DataSource}/{InitialCatalog} and saving to {AccountName}/{Container}",
                                  statisticsDatabase.DataSource, statisticsDatabase.InitialCatalog, _cloudStorageAccount.Credentials.AccountName, destinationContainer.Name);

            // totals reports
            Stopwatch stopwatch;

            // build downloads.v1.json
            if (ShouldGenerateReport(ReportNames.DownloadCount, _reportNameConfig))
            {
                stopwatch = Stopwatch.StartNew();

                var targets = new List <StorageContainerTarget>();
                targets.Add(new StorageContainerTarget(_cloudStorageAccount, _statisticsContainerName));
                foreach (var dataContainerName in _dataContainerNames)
                {
                    targets.Add(new StorageContainerTarget(_dataStorageAccount, dataContainerName));
                }

                var downloadCountReport = new DownloadCountReport(
                    LoggerFactory.CreateLogger <DownloadCountReport>(),
                    targets,
                    OpenSqlConnectionAsync <StatisticsDbConfiguration>,
                    OpenSqlConnectionAsync <GalleryDbConfiguration>,
                    _sqlCommandTimeoutSeconds);
                await downloadCountReport.Run();

                stopwatch.Stop();
                var reportMetricName = ReportNames.DownloadCount + ReportNames.Extension;
                _applicationInsightsHelper.TrackMetric(reportMetricName + " Generation Time (ms)", stopwatch.ElapsedMilliseconds);
                _applicationInsightsHelper.TrackReportProcessed(reportMetricName);
            }

            // build stats-totals.json
            if (ShouldGenerateReport(ReportNames.GalleryTotals, _reportNameConfig))
            {
                stopwatch = Stopwatch.StartNew();

                var galleryTotalsReport = new GalleryTotalsReport(
                    LoggerFactory.CreateLogger <GalleryTotalsReport>(),
                    _cloudStorageAccount,
                    _statisticsContainerName,
                    OpenSqlConnectionAsync <StatisticsDbConfiguration>,
                    OpenSqlConnectionAsync <GalleryDbConfiguration>,
                    commandTimeoutSeconds: _sqlCommandTimeoutSeconds);
                await galleryTotalsReport.Run();

                stopwatch.Stop();
                var reportMetricName = ReportNames.GalleryTotals + ReportNames.Extension;
                _applicationInsightsHelper.TrackMetric(reportMetricName + " Generation Time (ms)", stopwatch.ElapsedMilliseconds);
                _applicationInsightsHelper.TrackReportProcessed(reportMetricName);
            }

            // build tools.v1.json
            if (ShouldGenerateReport(ReportNames.DownloadsPerToolVersion, _reportNameConfig))
            {
                stopwatch = Stopwatch.StartNew();

                var toolsReport = new DownloadsPerToolVersionReport(
                    LoggerFactory.CreateLogger <DownloadsPerToolVersionReport>(),
                    _cloudStorageAccount,
                    _statisticsContainerName,
                    OpenSqlConnectionAsync <StatisticsDbConfiguration>,
                    OpenSqlConnectionAsync <GalleryDbConfiguration>,
                    _sqlCommandTimeoutSeconds);
                await toolsReport.Run();

                stopwatch.Stop();
                var reportMetricName = ReportNames.DownloadsPerToolVersion + ReportNames.Extension;
                _applicationInsightsHelper.TrackMetric(reportMetricName + " Generation Time (ms)", stopwatch.ElapsedMilliseconds);
                _applicationInsightsHelper.TrackReportProcessed(reportMetricName);
            }
        }
 public XTimer(string actionName, params string[] tags)
 {
     this.actionName = actionName;
     this.tags       = tags;
     sw = Stopwatch.StartNew();
 }
Exemple #20
0
        public bool TryStepForward()
        {
            if (_failedForward || _endOfFile)
            {
                return(false);
            }

            if (_input.Index + _lookAheadPosition >= _input.Count)
            {
                _endOfFile = true;
                return(false);
            }

            IToken token = _input.LT(-1 - _lookBehindPosition);

            if (token == null)
            {
                _endOfFile = true;
                return(false);
            }

            int symbol         = token.Type;
            int symbolPosition = token.TokenIndex;

            Stopwatch updateTimer = Stopwatch.StartNew();

            if (_lookAheadPosition == 0 && _lookBehindPosition == 0 && _contexts.Count == 0)
            {
                HashSet <InterpretTrace> initialContexts = new HashSet <InterpretTrace>(EqualityComparer <InterpretTrace> .Default);

                /* create our initial set of states as the ones at the target end of a match transition
                 * that contains 'symbol' in the match set.
                 */
                List <Transition> transitions = new List <Transition>(_network.Transitions.Where(i => i.MatchesSymbol(symbol)));
                foreach (var transition in transitions)
                {
                    if (ExcludedStartRules.Contains(Network.StateRules[transition.SourceState.Id]))
                    {
                        continue;
                    }

                    if (ExcludedStartRules.Contains(Network.StateRules[transition.TargetState.Id]))
                    {
                        continue;
                    }

                    ContextFrame startContext = new ContextFrame(transition.SourceState, null, null, this);
                    ContextFrame endContext   = new ContextFrame(transition.SourceState, null, null, this);
                    initialContexts.Add(new InterpretTrace(startContext, endContext));
                }

                _contexts.AddRange(initialContexts);
            }

            List <InterpretTrace> existing = new List <InterpretTrace>(_contexts);

            _contexts.Clear();
            SortedSet <int>          states   = new SortedSet <int>();
            HashSet <InterpretTrace> contexts = new HashSet <InterpretTrace>(EqualityComparer <InterpretTrace> .Default);

#if false
            HashSet <ContextFrame> existingUnique = new HashSet <ContextFrame>(existing.Select(i => i.StartContext), EqualityComparer <ContextFrame> .Default);
            Contract.Assert(existingUnique.Count == existing.Count);
#endif

            foreach (var context in existing)
            {
                states.Add(context.EndContext.State.Id);
                StepForward(contexts, states, context, symbol, symbolPosition, PreventContextType.None);
                states.Clear();
            }

            bool success = false;
            if (contexts.Count > 0)
            {
                _contexts.AddRange(contexts);
                if (TrackBoundedContexts)
                {
                    _boundedEndContexts.UnionWith(_contexts.Where(i => i.BoundedEnd));
                }
                success = true;
            }
            else
            {
                _contexts.AddRange(existing);
            }

            long nfaUpdateTime = updateTimer.ElapsedMilliseconds;

            if (success)
            {
                _lookAheadPosition++;
            }

            if (!success)
            {
                _failedForward = true;
            }

            return(success);
        }
Exemple #21
0
        protected override void ReParseImpl()
        {
            var outputWindow = OutputWindowService.TryGetPane(PredefinedOutputWindowPanes.TvlIntellisense);

            try
            {
                Stopwatch stopwatch = Stopwatch.StartNew();

                var snapshot             = TextBuffer.CurrentSnapshot;
                SnapshotCharStream input = new SnapshotCharStream(snapshot, new Span(0, snapshot.Length));
                GoLexer            lexer = new GoLexer(input);
                GoSemicolonInsertionTokenSource tokenSource = new GoSemicolonInsertionTokenSource(lexer);
                CommonTokenStream          tokens           = new CommonTokenStream(tokenSource);
                GoParser                   parser           = new GoParser(tokens);
                List <ParseErrorEventArgs> errors           = new List <ParseErrorEventArgs>();
                parser.ParseError += (sender, e) =>
                {
                    errors.Add(e);

                    string message = e.Message;

                    ITextDocument document;
                    if (TextBuffer.Properties.TryGetProperty(typeof(ITextDocument), out document) && document != null)
                    {
                        string fileName = document.FilePath;
                        var    line     = snapshot.GetLineFromPosition(e.Span.Start);
                        message = string.Format("{0}({1},{2}): {3}", fileName, line.LineNumber + 1, e.Span.Start - line.Start.Position + 1, message);
                    }

                    if (message.Length > 100)
                    {
                        message = message.Substring(0, 100) + " ...";
                    }

                    if (outputWindow != null)
                    {
                        outputWindow.WriteLine(message);
                    }

                    if (errors.Count > 100)
                    {
                        throw new OperationCanceledException();
                    }
                };

                var result = parser.compilationUnit();
                OnParseComplete(new AntlrParseResultEventArgs(snapshot, errors, stopwatch.Elapsed, tokens.GetTokens(), result));
            }
            catch (Exception e)
            {
                if (ErrorHandler.IsCriticalException(e))
                {
                    throw;
                }

                try
                {
                    if (outputWindow != null)
                    {
                        outputWindow.WriteLine(e.Message);
                    }
                }
                catch (Exception ex2)
                {
                    if (ErrorHandler.IsCriticalException(ex2))
                    {
                        throw;
                    }
                }
            }
        }
Exemple #22
0
 public RealtimeClock()
 {
     this.stopwatch = Stopwatch.StartNew();
 }
Exemple #23
0
        public override async Task <bool> Run()
        {
            try
            {
                var reportGenerationTime = DateTime.UtcNow;
                var destinationContainer = _cloudStorageAccount.CreateCloudBlobClient().GetContainerReference(_statisticsContainerName);

                _logger.LogDebug("Generating reports from {DataSource}/{InitialCatalog} and saving to {AccountName}/{Container}", _statisticsDatabase.DataSource, _statisticsDatabase.InitialCatalog, _cloudStorageAccount.Credentials.AccountName, destinationContainer.Name);

                if (string.IsNullOrEmpty(_reportName))
                {
                    // generate all reports
                    var reportGenerators = new Dictionary <ReportBuilder, ReportDataCollector>
                    {
                        { new ReportBuilder(ReportNames.NuGetClientVersion), new ReportDataCollector(_storedProcedures[ReportNames.NuGetClientVersion], _statisticsDatabase) },
                        { new ReportBuilder(ReportNames.Last6Weeks), new ReportDataCollector(_storedProcedures[ReportNames.Last6Weeks], _statisticsDatabase) },
                        { new ReportBuilder(ReportNames.RecentPopularity), new ReportDataCollector(_storedProcedures[ReportNames.RecentPopularity], _statisticsDatabase) },
                        { new ReportBuilder(ReportNames.RecentPopularityDetail), new ReportDataCollector(_storedProcedures[ReportNames.RecentPopularityDetail], _statisticsDatabase) }
                    };

                    foreach (var reportGenerator in reportGenerators)
                    {
                        await ProcessReport(destinationContainer, reportGenerator.Key, reportGenerator.Value, reportGenerationTime);

                        ApplicationInsightsHelper.TrackReportProcessed(reportGenerator.Key.ReportName + " report");
                    }

                    await RebuildPackageReports(destinationContainer, reportGenerationTime);
                    await CleanInactiveRecentPopularityDetailByPackageReports(destinationContainer, reportGenerationTime);
                }
                else
                {
                    // generate only the specific report
                    var reportBuilder       = new ReportBuilder(_reportName);
                    var reportDataCollector = new ReportDataCollector(_storedProcedures[_reportName], _statisticsDatabase);

                    await ProcessReport(destinationContainer, reportBuilder, reportDataCollector, reportGenerationTime);
                }

                _logger.LogInformation("Generated reports from {DataSource}/{InitialCatalog} and saving to {AccountName}/{Container}", _statisticsDatabase.DataSource, _statisticsDatabase.InitialCatalog, _cloudStorageAccount.Credentials.AccountName, destinationContainer.Name);

                // totals reports
                var stopwatch = Stopwatch.StartNew();

                // build downloads.v1.json
                var targets = new List <StorageContainerTarget>();
                targets.Add(new StorageContainerTarget(_cloudStorageAccount, _statisticsContainerName));
                foreach (var dataContainerName in _dataContainerNames)
                {
                    targets.Add(new StorageContainerTarget(_dataStorageAccount, dataContainerName));
                }
                var downloadCountReport = new DownloadCountReport(targets, _statisticsDatabase, _galleryDatabase);
                await downloadCountReport.Run();

                stopwatch.Stop();
                ApplicationInsightsHelper.TrackMetric(DownloadCountReport.ReportName + " Generation Time (ms)", stopwatch.ElapsedMilliseconds);
                ApplicationInsightsHelper.TrackReportProcessed(DownloadCountReport.ReportName);
                stopwatch.Restart();

                // build stats-totals.json
                var galleryTotalsReport = new GalleryTotalsReport(_cloudStorageAccount, _statisticsContainerName, _statisticsDatabase, _galleryDatabase);
                await galleryTotalsReport.Run();

                stopwatch.Stop();
                ApplicationInsightsHelper.TrackMetric(GalleryTotalsReport.ReportName + " Generation Time (ms)", stopwatch.ElapsedMilliseconds);
                ApplicationInsightsHelper.TrackReportProcessed(GalleryTotalsReport.ReportName);


                // build tools.v1.json
                var toolsReport = new DownloadsPerToolVersionReport(_cloudStorageAccount, _statisticsContainerName, _statisticsDatabase, _galleryDatabase);
                await toolsReport.Run();

                stopwatch.Stop();
                ApplicationInsightsHelper.TrackMetric(DownloadsPerToolVersionReport.ReportName + " Generation Time (ms)", stopwatch.ElapsedMilliseconds);
                ApplicationInsightsHelper.TrackReportProcessed(DownloadsPerToolVersionReport.ReportName);
                stopwatch.Restart();

                return(true);
            }
            catch (Exception exception)
            {
                _logger.LogError("Job run failed! {Exception}", exception);

                return(false);
            }
        }
Exemple #24
0
        protected override void ReParseImpl()
        {
            var outputWindow = OutputWindowService.TryGetPane(PredefinedOutputWindowPanes.TvlIntellisense);

            try
            {
                Stopwatch stopwatch = Stopwatch.StartNew();

                var snapshot = TextBuffer.CurrentSnapshot;
                SnapshotCharStream         input  = new SnapshotCharStream(snapshot, new Span(0, snapshot.Length));
                GroupLexer                 lexer  = new GroupLexer(input);
                CommonTokenStream          tokens = new CommonTokenStream(lexer);
                GroupParserWrapper         parser = new GroupParserWrapper(tokens);
                List <ParseErrorEventArgs> errors = new List <ParseErrorEventArgs>();
                parser.ParseError += (sender, e) =>
                {
                    errors.Add(e);

                    string message = e.Message;

                    ITextDocument document;
                    if (TextBuffer.Properties.TryGetProperty(typeof(ITextDocument), out document) && document != null)
                    {
                        string fileName = document.FilePath;
                        var    line     = snapshot.GetLineFromPosition(e.Span.Start);
                        message = string.Format("{0}({1},{2}): {3}", fileName, line.LineNumber + 1, e.Span.Start - line.Start.Position + 1, message);
                    }

                    if (message.Length > 100)
                    {
                        message = message.Substring(0, 100) + " ...";
                    }

                    if (outputWindow != null)
                    {
                        outputWindow.WriteLine(message);
                    }

                    if (errors.Count > 100)
                    {
                        throw new OperationCanceledException();
                    }
                };

                TemplateGroupWrapper group = new TemplateGroupWrapper('<', '>');
                parser.group(group, "/");
                TemplateGroupRuleReturnScope returnScope = BuiltAstForGroupTemplates(group);

                // Also parse the input using the V4 lexer/parser for downstream operations that make use of it
                IList <Antlr4.Runtime.IToken>   v4tokens;
                TemplateParser.GroupFileContext v4result = ParseWithAntlr4(snapshot, out v4tokens);

                OnParseComplete(new StringTemplateParseResultEventArgs(snapshot, errors, stopwatch.Elapsed, tokens.GetTokens(), returnScope, v4tokens, v4result));
            }
            catch (Exception e) when(!ErrorHandler.IsCriticalException(e))
            {
                try
                {
                    if (outputWindow != null)
                    {
                        outputWindow.WriteLine(e.Message);
                    }
                }
                catch (Exception ex2) when(!ErrorHandler.IsCriticalException(ex2))
                {
                }
            }
        }
        private void UpdateQuickInfoContent(IQuickInfoSession session, SnapshotPoint triggerPoint)
        {
            /* use the experimental model to locate and process the expression */
            Stopwatch stopwatch = Stopwatch.StartNew();

            // lex the entire document
            var currentSnapshot = triggerPoint.Snapshot;
            var input           = new SnapshotCharStream(currentSnapshot, new Span(0, currentSnapshot.Length));
            var unicodeInput    = new JavaUnicodeStream(input);
            var lexer           = new Java2Lexer(unicodeInput);
            var tokens          = new CommonTokenStream(lexer);

            tokens.Fill();

            // locate the last token before the trigger point
            while (true)
            {
                IToken nextToken = tokens.LT(1);
                if (nextToken.Type == CharStreamConstants.EndOfFile)
                {
                    break;
                }

                if (nextToken.StartIndex > triggerPoint.Position)
                {
                    break;
                }

                tokens.Consume();
            }

            IToken triggerToken = tokens.LT(-1);

            if (triggerToken == null)
            {
                return;
            }

            switch (triggerToken.Type)
            {
            // symbol references
            case Java2Lexer.IDENTIFIER:
            case Java2Lexer.THIS:
            case Java2Lexer.SUPER:
            // primitive types
            case Java2Lexer.BOOLEAN:
            case Java2Lexer.CHAR:
            case Java2Lexer.BYTE:
            case Java2Lexer.SHORT:
            case Java2Lexer.INT:
            case Java2Lexer.LONG:
            case Java2Lexer.FLOAT:
            case Java2Lexer.DOUBLE:
            // literals
            case Java2Lexer.INTLITERAL:
            case Java2Lexer.LONGLITERAL:
            case Java2Lexer.FLOATLITERAL:
            case Java2Lexer.DOUBLELITERAL:
            case Java2Lexer.CHARLITERAL:
            case Java2Lexer.STRINGLITERAL:
            case Java2Lexer.TRUE:
            case Java2Lexer.FALSE:
            case Java2Lexer.NULL:
                break;

            default:
                return;
            }

            NetworkInterpreter interpreter = CreateNetworkInterpreter(tokens);

            while (interpreter.TryStepBackward())
            {
                if (interpreter.Contexts.Count == 0 || interpreter.Contexts.Count > 400)
                {
                    break;
                }

                if (interpreter.Contexts.All(context => context.BoundedStart))
                {
                    break;
                }
            }

            interpreter.Contexts.RemoveAll(i => !i.BoundedStart);
            interpreter.CombineBoundedStartContexts();

            IOutputWindowPane pane = Provider.OutputWindowService.TryGetPane(PredefinedOutputWindowPanes.TvlIntellisense);

            if (pane != null)
            {
                pane.WriteLine(string.Format("Located {0} QuickInfo expression(s) in {1}ms.", interpreter.Contexts.Count, stopwatch.ElapsedMilliseconds));
            }

            HashSet <string> intermediateResult = new HashSet <string>();
            HashSet <string> finalResult        = new HashSet <string>();
            List <object>    quickInfoContent   = new List <object>();

            foreach (var context in interpreter.Contexts)
            {
                Span?span = null;

                foreach (var transition in context.Transitions)
                {
                    if (!transition.Transition.IsMatch)
                    {
                        continue;
                    }

                    IToken token     = transition.Token;
                    Span   tokenSpan = new Span(token.StartIndex, token.StopIndex - token.StartIndex + 1);
                    if (span == null)
                    {
                        span = tokenSpan;
                    }
                    else
                    {
                        span = Span.FromBounds(Math.Min(span.Value.Start, tokenSpan.Start), Math.Max(span.Value.End, tokenSpan.End));
                    }
                }

                if (span.HasValue && !span.Value.IsEmpty)
                {
                    string text = currentSnapshot.GetText(span.Value);
                    if (!intermediateResult.Add(text))
                    {
                        continue;
                    }

                    AstParserRuleReturnScope <CommonTree, CommonToken> result = null;

                    try
                    {
                        var expressionInput        = new ANTLRStringStream(text);
                        var expressionUnicodeInput = new JavaUnicodeStream(expressionInput);
                        var expressionLexer        = new Java2Lexer(expressionUnicodeInput);
                        var expressionTokens       = new CommonTokenStream(expressionLexer);
                        var expressionParser       = new Java2Parser(expressionTokens);
                        result = expressionParser.primary();

                        // anchors experiment
                        Contract.Assert(TextBuffer.CurrentSnapshot == triggerPoint.Snapshot);
                        ClassAnchorTracker          tracker     = new ClassAnchorTracker(TextBuffer, null);
                        SnapshotSpan                trackedSpan = new SnapshotSpan(triggerPoint.Snapshot, 0, triggerPoint.Position);
                        ITagSpan <ScopeAnchorTag>[] tags        = tracker.GetTags(new NormalizedSnapshotSpanCollection(trackedSpan)).ToArray();

                        text = result.Tree.ToStringTree();
                    }
                    catch (RecognitionException)
                    {
                        text = "Could not parse: " + text;
                    }

                    text = text.Replace("\n", "\\n").Replace("\r", "\\r");
                    finalResult.Add(text);

                    //if (Regex.IsMatch(text, @"^[A-Za-z_]+(?:\.\w+)*$"))
                    //{
                    //    NameResolutionContext resolutionContext = NameResolutionContext.Global(Provider.IntelliSenseCache);
                    //    resolutionContext = resolutionContext.Filter(text, null, true);
                    //    CodeElement[] matching = resolutionContext.GetMatchingElements();
                    //    if (matching.Length > 0)
                    //    {
                    //        foreach (var element in matching)
                    //        {
                    //            element.AugmentQuickInfoSession(quickInfoContent);
                    //        }
                    //    }
                    //    else
                    //    {
                    //        // check if this is a package
                    //        CodePhysicalFile[] files = Provider.IntelliSenseCache.GetPackageFiles(text, true);
                    //        if (files.Length > 0)
                    //        {
                    //            finalResult.Add(string.Format("package {0}", text));
                    //        }
                    //        else
                    //        {
                    //            // check if this is a type
                    //            string typeName = text.Substring(text.LastIndexOf('.') + 1);
                    //            CodeType[] types = Provider.IntelliSenseCache.GetTypes(typeName, true);
                    //            foreach (var type in types)
                    //            {
                    //                if (type.FullName == text)
                    //                    finalResult.Add(string.Format("{0}: {1}", type.GetType().Name, type.FullName));
                    //            }
                    //        }
                    //    }
                    //}
                    //else
                    //{
                    //    finalResult.Add(text);
                    //}
                }
            }

            ITrackingSpan applicableToSpan = null;

            foreach (var result in finalResult)
            {
                quickInfoContent.Add(result);
            }

            applicableToSpan = currentSnapshot.CreateTrackingSpan(new Span(triggerToken.StartIndex, triggerToken.StopIndex - triggerToken.StartIndex + 1), SpanTrackingMode.EdgeExclusive);

            //try
            //{
            //    Expression currentExpression = Provider.IntellisenseCache.ParseExpression(selection);
            //    if (currentExpression != null)
            //    {
            //        SnapshotSpan? span = currentExpression.Span;
            //        if (span.HasValue)
            //            applicableToSpan = span.Value.Snapshot.CreateTrackingSpan(span.Value, SpanTrackingMode.EdgeExclusive);

            //        quickInfoContent.Add(currentExpression.ToString());
            //    }
            //    else
            //    {
            //        quickInfoContent.Add("Could not parse expression.");
            //    }
            //}
            //catch (Exception ex)
            //{
            //    if (ErrorHandler.IsCriticalException(ex))
            //        throw;

            //    quickInfoContent.Add(ex.Message);
            //}

            lock (_contentUpdateLock)
            {
                _triggerPoint     = triggerPoint;
                _applicableToSpan = applicableToSpan;
                _quickInfoContent = quickInfoContent;
            }

            IWpfTextView wpfTextView = session.TextView as IWpfTextView;

            if (wpfTextView != null && wpfTextView.VisualElement != null)
            {
                ITrackingPoint trackingTriggerPoint = triggerPoint.Snapshot.CreateTrackingPoint(triggerPoint.Position, PointTrackingMode.Negative);
                wpfTextView.VisualElement.Dispatcher.BeginInvoke((Action <IQuickInfoSession, ITrackingPoint, bool>)RetriggerQuickInfo, session, trackingTriggerPoint, true);
            }
        }
        public async Task <bool> PreloadPlugins()
        {
            Stopwatch benchmark = Stopwatch.StartNew();

            Debugger.Module("Pre loading modules");
            string[] modules = Directory.GetDirectories(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Modules"));
            foreach (string module in modules)
            {
                // Skip modules without Module.json
                if (!File.Exists(Path.Combine(module, "module.json")))
                {
                    continue;
                }

                try
                {
                    string            serializedModule = File.ReadAllText(Path.Combine(module, "module.json"));
                    PluginInformation modInformation   = JsonConvert.DeserializeObject <PluginInformation>(serializedModule);

                    if (modInformation.Update.MinimumVersion is null)
                    {
                        Debugger.Error($"{modInformation.Name.ToUpper()} MIGHT BE OUTDATED! CONSIDER UPDATING IT.");
                    }

                    if (PluginUpdate.PluginSupportsUpdate(modInformation))
                    {
                        switch (await PluginUpdate.UpdateAllFiles(modInformation, module))
                        {
                        case UpdateResult.Updated:
                            serializedModule = File.ReadAllText(Path.Combine(module, "module.json"));
                            modInformation   = JsonConvert.DeserializeObject <PluginInformation>(serializedModule);

                            Debugger.Module($"Updated plugin: {modInformation.Name} (ver {modInformation.Version})");
                            break;

                        case UpdateResult.Skipped:
                            break;

                        case UpdateResult.Failed:
                            Debugger.Error($"Failed to update plugin: {modInformation.Name}");
                            continue;

                        case UpdateResult.UpToDate:
                            Debugger.Module($"Plugin {modInformation.Name} is up-to-date (ver {modInformation.Version})");
                            break;
                        }
                    }

                    PluginSettings modSettings = GetPluginSettings(module);

                    if (!string.IsNullOrEmpty(modInformation.EntryPoint) && File.Exists(Path.Combine(module, modInformation.EntryPoint)))
                    {
                        Debugger.Module($"Compiling plugin: {modInformation.Name}");
                        if (CompilePlugin(module, modInformation))
                        {
                            Debugger.Module($"{modInformation.Name} compiled successfully.");
                        }
                        else
                        {
                            continue;
                        }
                    }
                    Stopwatch s = Stopwatch.StartNew();
                    foreach (string required in modInformation.Dependencies)
                    {
                        AppDomain.CurrentDomain.Load(AssemblyName.GetAssemblyName(Path.Combine(module, required)));
                    }
                    Assembly           plugin  = AppDomain.CurrentDomain.Load(AssemblyName.GetAssemblyName(Path.Combine(module, $"{modInformation.Name}.dll")));
                    IEnumerable <Type> entries = plugin.ExportedTypes.Where(exp => exp.GetMethod("Initialize") != null);
                    if (entries.Count() > 0)
                    {
                        dynamic mod = plugin.CreateInstance(entries.First().ToString());
                        packages.Add(new PluginPackage {
                            plugin = mod, information = modInformation, settings = modSettings, path = module
                        });
                    }
                }
                catch (Exception err)
                {
                    Debugger.Error(err);
                    continue;
                }
            }

            IsReady = true;
            benchmark.Stop();
            Debugger.Module($"Pre loaded {packages.Count} module(s) in {benchmark.ElapsedMilliseconds}ms");
            if (QueueLoad)
            {
                LoadPlugins();
            }
            return(true);
        }
        public int EnumChildren(enum_DEBUGPROP_INFO_FLAGS dwFields, uint dwRadix, ref Guid guidFilter, enum_DBG_ATTRIB_FLAGS dwAttribFilter, string pszNameFilter, uint dwTimeout, out IEnumDebugPropertyInfo2 ppEnum)
        {
            Stopwatch stopwatch = Stopwatch.StartNew();
            bool      infinite  = dwTimeout == unchecked ((uint)Timeout.Infinite);
            TimeSpan  timeout   = infinite ? TimeSpan.Zero : TimeSpan.FromMilliseconds(dwTimeout);

            IObjectReference objectReference = _evaluatedExpression.Value as IObjectReference;
            IReferenceType   referenceType   = _evaluatedExpression.ValueType as IReferenceType;

            if (objectReference == null || referenceType == null)
            {
                ppEnum = new EnumDebugPropertyInfo(Enumerable.Empty <DEBUG_PROPERTY_INFO>());
                return(VSConstants.S_OK);
            }

            ppEnum = null;

            bool getFullName   = (dwFields & enum_DEBUGPROP_INFO_FLAGS.DEBUGPROP_INFO_FULLNAME) != 0;
            bool getName       = (dwFields & enum_DEBUGPROP_INFO_FLAGS.DEBUGPROP_INFO_NAME) != 0;
            bool getType       = (dwFields & enum_DEBUGPROP_INFO_FLAGS.DEBUGPROP_INFO_TYPE) != 0;
            bool getValue      = (dwFields & enum_DEBUGPROP_INFO_FLAGS.DEBUGPROP_INFO_VALUE) != 0;
            bool getAttributes = (dwFields & enum_DEBUGPROP_INFO_FLAGS.DEBUGPROP_INFO_ATTRIB) != 0;
            bool getProperty   = (dwFields & enum_DEBUGPROP_INFO_FLAGS.DEBUGPROP_INFO_PROP) != 0;

            bool useAutoExpandValue = (dwFields & enum_DEBUGPROP_INFO_FLAGS.DEBUGPROP_INFO_VALUE_AUTOEXPAND) != 0;
            bool noFormatting       = (dwFields & enum_DEBUGPROP_INFO_FLAGS.DEBUGPROP_INFO_VALUE_RAW) != 0;
            bool noToString         = (dwFields & enum_DEBUGPROP_INFO_FLAGS.DEBUGPROP_INFO_VALUE_NO_TOSTRING) != 0;

            List <DEBUG_PROPERTY_INFO> properties = new List <DEBUG_PROPERTY_INFO>();

            DEBUG_PROPERTY_INFO[] propertyInfo = new DEBUG_PROPERTY_INFO[1];

            bool isCollection         = false;
            int  collectionSize       = 0;
            bool haveCollectionValues = false;
            ReadOnlyCollection <IValue> collectionValues = null;
            IType componentType = null;

            if (!_useRawValues)
            {
                isCollection = TryGetCollectionSize(objectReference, out collectionSize);

                if (isCollection && (getValue || getProperty))
                {
                    haveCollectionValues = TryGetCollectionValues(objectReference, out collectionValues, out componentType);
                }
            }

            if (isCollection)
            {
                for (int i = 0; i < collectionSize; i++)
                {
                    propertyInfo[0] = default(DEBUG_PROPERTY_INFO);

                    if (haveCollectionValues)
                    {
                        string            name         = "[" + i + "]";
                        IType             propertyType = componentType;
                        IValue            value        = collectionValues[i];
                        JavaDebugProperty property     = new JavaDebugProperty(this, name, _evaluatedExpression.FullName + name, propertyType, value, _evaluatedExpression.HasSideEffects);

                        bool timedout = !infinite && timeout < stopwatch.Elapsed;
                        enum_DEBUGPROP_INFO_FLAGS fields = dwFields;
                        if (timedout)
                        {
                            fields |= enum_DEBUGPROP_INFO_FLAGS.DEBUGPROP_INFO_VALUE_NO_TOSTRING;
                        }

                        int hr = property.GetPropertyInfo(fields, dwRadix, dwTimeout, null, 0, propertyInfo);
                        if (ErrorHandler.Failed(hr))
                        {
                            return(hr);
                        }

                        if (timedout)
                        {
                            propertyInfo[0].dwAttrib |= enum_DBG_ATTRIB_FLAGS.DBG_ATTRIB_VALUE_TIMEOUT;
                        }

                        properties.Add(propertyInfo[0]);
                        continue;
                    }
                    else
                    {
                        if (getFullName)
                        {
                            propertyInfo[0].bstrFullName = _evaluatedExpression.FullName + "[" + i + "]";
                            propertyInfo[0].dwFields    |= enum_DEBUGPROP_INFO_FLAGS.DEBUGPROP_INFO_FULLNAME;
                        }

                        if (getName)
                        {
                            propertyInfo[0].bstrName  = "[" + i + "]";
                            propertyInfo[0].dwFields |= enum_DEBUGPROP_INFO_FLAGS.DEBUGPROP_INFO_NAME;
                        }

                        if (getType)
                        {
                            propertyInfo[0].bstrType  = componentType.GetName();
                            propertyInfo[0].dwFields |= enum_DEBUGPROP_INFO_FLAGS.DEBUGPROP_INFO_TYPE;
                        }

                        if (getAttributes)
                        {
                            propertyInfo[0].dwAttrib |= enum_DBG_ATTRIB_FLAGS.DBG_ATTRIB_VALUE_AUTOEXPANDED;
                            propertyInfo[0].dwAttrib |= enum_DBG_ATTRIB_FLAGS.DBG_ATTRIB_VALUE_READONLY;
                            if (_evaluatedExpression.HasSideEffects)
                            {
                                propertyInfo[0].dwAttrib |= enum_DBG_ATTRIB_FLAGS.DBG_ATTRIB_VALUE_SIDE_EFFECT;
                            }

                            propertyInfo[0].dwFields |= enum_DEBUGPROP_INFO_FLAGS.DEBUGPROP_INFO_ATTRIB;
                        }
                    }
                }

                bool useRawValues = referenceType is IClassType;
                if (useRawValues)
                {
                    if (_rawValuesProperty == null)
                    {
                        _rawValuesProperty = new JavaDebugProperty(this, "Raw Values")
                        {
                            _useRawValues = true
                        };
                    }

                    propertyInfo[0] = default(DEBUG_PROPERTY_INFO);
                    ErrorHandler.ThrowOnFailure(_rawValuesProperty.GetPropertyInfo(dwFields, dwRadix, dwTimeout, null, 0, propertyInfo));
                    properties.Add(propertyInfo[0]);
                }
            }
            else
            {
                ReadOnlyCollection <IField> fields = referenceType.GetFields(false);
                List <IField> staticFields         = new List <IField>(fields.Where(i => i.GetIsStatic()));

                bool useMostDerived = !_preventMostDerived && _evaluatedExpression.Value != null && !_evaluatedExpression.Value.GetValueType().Equals(referenceType);
                if (useMostDerived)
                {
                    if (_mostDerivedProperty == null)
                    {
                        _mostDerivedProperty = new JavaDebugProperty(this, string.Format("[{0}]", _evaluatedExpression.Value.GetValueType().GetName()), _evaluatedExpression.Value.GetValueType())
                        {
                            _preventMostDerived = true
                        };
                    }

                    propertyInfo[0] = default(DEBUG_PROPERTY_INFO);
                    ErrorHandler.ThrowOnFailure(_mostDerivedProperty.GetPropertyInfo(dwFields, dwRadix, dwTimeout, null, 0, propertyInfo));
                    properties.Add(propertyInfo[0]);
                }

                bool useSuper = false;
                if (!useMostDerived)
                {
                    IClassType valueType = _evaluatedExpression.ValueType as IClassType;
                    if (valueType != null)
                    {
                        IClassType superClass = valueType.GetSuperclass();
                        useSuper = superClass != null && superClass.GetName() != "java.lang.Object";

                        if (useSuper)
                        {
                            if (_superProperty == null)
                            {
                                _superProperty = new JavaDebugProperty(this, "[super]", superClass)
                                {
                                    _preventMostDerived = true
                                };
                            }

                            propertyInfo[0] = default(DEBUG_PROPERTY_INFO);
                            ErrorHandler.ThrowOnFailure(_superProperty.GetPropertyInfo(dwFields, dwRadix, dwTimeout, null, 0, propertyInfo));
                            properties.Add(propertyInfo[0]);
                        }
                    }
                }

                foreach (var field in fields)
                {
                    if (field.GetIsStatic())
                    {
                        continue;
                    }

                    propertyInfo[0] = default(DEBUG_PROPERTY_INFO);

                    if (getValue || getProperty)
                    {
                        IDebugProperty2 property;
                        try
                        {
                            string name         = field.GetName();
                            IType  propertyType = field.GetFieldType();
                            IValue value        = objectReference.GetValue(field);
                            property = new JavaDebugProperty(this, name, _evaluatedExpression.FullName + "." + name, propertyType, value, _evaluatedExpression.HasSideEffects, field);
                            ErrorHandler.ThrowOnFailure(property.GetPropertyInfo(dwFields, dwRadix, dwTimeout, null, 0, propertyInfo));
                        }
                        catch (Exception e)
                        {
                            if (ErrorHandler.IsCriticalException(e))
                            {
                                throw;
                            }

                            string name         = field.GetName();
                            IType  propertyType = field.GetFieldType();
                            IValue value        = field.GetVirtualMachine().GetMirrorOf(0);
                            property = new JavaDebugProperty(this, name, _evaluatedExpression.FullName + "." + name, propertyType, value, _evaluatedExpression.HasSideEffects, field);
                            ErrorHandler.ThrowOnFailure(property.GetPropertyInfo(dwFields, dwRadix, dwTimeout, null, 0, propertyInfo));
                        }
                    }
                    else
                    {
                        if (getFullName)
                        {
                            propertyInfo[0].bstrFullName = _evaluatedExpression.FullName + "." + field.GetName();
                            propertyInfo[0].dwFields    |= enum_DEBUGPROP_INFO_FLAGS.DEBUGPROP_INFO_FULLNAME;
                        }

                        if (getName)
                        {
                            propertyInfo[0].bstrName  = field.GetName();
                            propertyInfo[0].dwFields |= enum_DEBUGPROP_INFO_FLAGS.DEBUGPROP_INFO_NAME;
                        }

                        if (getType)
                        {
                            propertyInfo[0].bstrType  = field.GetFieldTypeName();
                            propertyInfo[0].dwFields |= enum_DEBUGPROP_INFO_FLAGS.DEBUGPROP_INFO_TYPE;
                        }

                        if (getAttributes)
                        {
                            if (field.GetIsStatic())
                            {
                                propertyInfo[0].dwAttrib |= enum_DBG_ATTRIB_FLAGS.DBG_ATTRIB_STORAGE_STATIC;
                            }
                            if (field.GetIsPrivate())
                            {
                                propertyInfo[0].dwAttrib |= enum_DBG_ATTRIB_FLAGS.DBG_ATTRIB_ACCESS_PRIVATE;
                            }
                            if (field.GetIsProtected())
                            {
                                propertyInfo[0].dwAttrib |= enum_DBG_ATTRIB_FLAGS.DBG_ATTRIB_ACCESS_PROTECTED;
                            }
                            if (field.GetIsPublic())
                            {
                                propertyInfo[0].dwAttrib |= enum_DBG_ATTRIB_FLAGS.DBG_ATTRIB_ACCESS_PUBLIC;
                            }

                            propertyInfo[0].dwAttrib |= enum_DBG_ATTRIB_FLAGS.DBG_ATTRIB_VALUE_READONLY;
                            propertyInfo[0].dwFields |= enum_DEBUGPROP_INFO_FLAGS.DEBUGPROP_INFO_ATTRIB;
#if false
                            bool expandable;
                            bool hasId;
                            bool canHaveId;
                            bool readOnly;
                            bool error;
                            bool sideEffect;
                            bool overloadedContainer;
                            bool boolean;
                            bool booleanTrue;
                            bool invalid;
                            bool notAThing;
                            bool autoExpanded;
                            bool timeout;
                            bool rawString;
                            bool customViewer;

                            bool accessNone;
                            bool accessPrivate;
                            bool accessProtected;
                            bool accessPublic;

                            bool storageNone;
                            bool storageGlobal;
                            bool storageStatic;
                            bool storageRegister;

                            bool noModifiers;
                            bool @virtual;
                            bool constant;
                            bool synchronized;
                            bool @volatile;

                            bool dataField;
                            bool method;
                            bool property;
                            bool @class;
                            bool baseClass;
                            bool @interface;
                            bool innerClass;
                            bool mostDerived;

                            bool multiCustomViewers;
#endif
                        }
                    }

                    properties.Add(propertyInfo[0]);
                    continue;
                }

                if (staticFields.Count > 0)
                {
                    propertyInfo[0] = default(DEBUG_PROPERTY_INFO);

                    JavaDebugStaticMembersPseudoProperty property = new JavaDebugStaticMembersPseudoProperty(this, referenceType, staticFields);
                    ErrorHandler.ThrowOnFailure(property.GetPropertyInfo(dwFields, dwRadix, dwTimeout, null, 0, propertyInfo));
                    properties.Add(propertyInfo[0]);
                }
            }

            ppEnum = new EnumDebugPropertyInfo(properties);
            return(VSConstants.S_OK);
        }
Exemple #28
0
        public bool TryStepBackward()
        {
            if (_failedBackward || _beginningOfFile)
            {
                return(false);
            }

            if (_input.Index - _lookBehindPosition <= 0)
            {
                _beginningOfFile = true;
                return(false);
            }

            IToken token = _input.LT(-1 - _lookBehindPosition);

            if (token == null)
            {
                _beginningOfFile = true;
                return(false);
            }

            int symbol         = token.Type;
            int symbolPosition = token.TokenIndex;

            /*
             * Update the non-deterministic trace
             */

            Stopwatch updateTimer = Stopwatch.StartNew();

            if (_lookAheadPosition == 0 && _lookBehindPosition == 0 && _contexts.Count == 0)
            {
                HashSet <InterpretTrace> initialContexts = new HashSet <InterpretTrace>(EqualityComparer <InterpretTrace> .Default);

                /* create our initial set of states as the ones at the target end of a match transition
                 * that contains 'symbol' in the match set.
                 */
                List <Transition> transitions = new List <Transition>(_network.Transitions.Where(i => i.MatchesSymbol(symbol)));
                foreach (var transition in transitions)
                {
                    if (ExcludedStartRules.Contains(Network.StateRules[transition.SourceState.Id]))
                    {
                        continue;
                    }

                    if (ExcludedStartRules.Contains(Network.StateRules[transition.TargetState.Id]))
                    {
                        continue;
                    }

                    ContextFrame startContext = new ContextFrame(transition.TargetState, null, null, this);
                    ContextFrame endContext   = new ContextFrame(transition.TargetState, null, null, this);
                    initialContexts.Add(new InterpretTrace(startContext, endContext));
                }

                _contexts.AddRange(initialContexts);

#if DFA
                DeterministicState deterministicState = new DeterministicState(_contexts.Select(i => i.StartContext));
                _deterministicTrace = new DeterministicTrace(deterministicState, deterministicState);
#endif
            }

            List <InterpretTrace> existing = new List <InterpretTrace>(_contexts);
            _contexts.Clear();
            SortedSet <int>          states   = new SortedSet <int>();
            HashSet <InterpretTrace> contexts = new HashSet <InterpretTrace>(EqualityComparer <InterpretTrace> .Default);
#if false
            HashSet <ContextFrame> existingUnique = new HashSet <ContextFrame>(existing.Select(i => i.StartContext), EqualityComparer <ContextFrame> .Default);
            Contract.Assert(existingUnique.Count == existing.Count);
#endif

            foreach (var context in existing)
            {
                states.Add(context.StartContext.State.Id);
                StepBackward(contexts, states, context, symbol, symbolPosition, PreventContextType.None);
                states.Clear();
            }

            bool success = false;
            if (contexts.Count > 0)
            {
                _contexts.AddRange(contexts);
                if (TrackBoundedContexts)
                {
                    _boundedStartContexts.UnionWith(_contexts.Where(i => i.BoundedStart));
                }
                success = true;
            }
            else
            {
                _contexts.AddRange(existing);
            }

            long nfaUpdateTime = updateTimer.ElapsedMilliseconds;

#if DFA
            /*
             * Update the deterministic trace
             */

            updateTimer.Restart();

            DeterministicTransition deterministicTransition = _deterministicTrace.StartState.IncomingTransitions.SingleOrDefault(i => i.MatchSet.Contains(symbol));
            if (deterministicTransition == null)
            {
                DeterministicState sourceState = new DeterministicState(contexts.Select(i => i.StartContext));
                DeterministicState targetState = _deterministicTrace.StartState;
                deterministicTransition = targetState.IncomingTransitions.SingleOrDefault(i => i.SourceState.Equals(sourceState));
                if (deterministicTransition == null)
                {
                    deterministicTransition = new DeterministicTransition(targetState);
                    sourceState.AddTransition(deterministicTransition);
                }

                deterministicTransition.MatchSet.Add(symbol);
            }

            IEnumerable <DeterministicTraceTransition> deterministicTransitions = Enumerable.Repeat(new DeterministicTraceTransition(deterministicTransition, symbol, symbolPosition, this), 1);
            deterministicTransitions = deterministicTransitions.Concat(_deterministicTrace.Transitions);
            _deterministicTrace      = new DeterministicTrace(deterministicTransition.SourceState, _deterministicTrace.EndState, deterministicTransitions);

            long dfaUpdateTime = updateTimer.ElapsedMilliseconds;
#endif

            if (success)
            {
                _lookBehindPosition++;
            }

            if (!success)
            {
                _failedBackward = true;
            }

            return(success);
        }
Exemple #29
0
        // Almost same as above but with an added predicate and treating one item at a time.
        // It's used by Scheduler Participate(...) method for special waiting case like
        // Task.WaitAll(someTasks) or Task.WaitAny(someTasks)
        // Predicate should be really fast and not blocking as it is called a good deal of time
        // Also, the method skip tasks that are LongRunning to avoid blocking (Task are not LongRunning by default)
        public static void ParticipativeWorkerMethod(Task self,
                                                     ManualResetEventSlim predicateEvt,
                                                     int millisecondsTimeout,
                                                     IProducerConsumerCollection <Task> sharedWorkQueue,
                                                     ThreadWorker[] others,
                                                     ManualResetEvent evt)
        {
            const int stage1 = 5, stage2 = 0;
            int       tries = 50;

            WaitHandle[] handles = null;
            Watch        watch   = Watch.StartNew();

            if (millisecondsTimeout == -1)
            {
                millisecondsTimeout = int.MaxValue;
            }
            bool          aggressive       = false;
            bool          hasAutoReference = autoReference != null;
            Action <Task> adder            = null;

            while (!predicateEvt.IsSet && watch.ElapsedMilliseconds < millisecondsTimeout && !self.IsCompleted)
            {
                // We try to execute the self task as it may be the simplest way to unlock
                // the situation
                if (self.Status == TaskStatus.WaitingToRun)
                {
                    self.Execute(hasAutoReference ? autoReference.adder : (Action <Task>)null);
                    if (predicateEvt.IsSet || watch.ElapsedMilliseconds > millisecondsTimeout)
                    {
                        return;
                    }
                }

                Task value;

                // If we are in fact a normal ThreadWorker, use our own deque
                if (hasAutoReference)
                {
                    var enumerable = autoReference.dDeque.GetEnumerable();
                    if (adder == null)
                    {
                        adder = hasAutoReference ? autoReference.adder : (Action <Task>)null;
                    }

                    if (enumerable != null)
                    {
                        foreach (var t in enumerable)
                        {
                            if (t == null)
                            {
                                continue;
                            }

                            if (CheckTaskFitness(self, t))
                            {
                                t.Execute(adder);
                            }

                            if (predicateEvt.IsSet || watch.ElapsedMilliseconds > millisecondsTimeout)
                            {
                                return;
                            }
                        }
                    }
                }

                int count = sharedWorkQueue.Count;

                // Dequeue only one item as we have restriction
                while (--count >= 0 && sharedWorkQueue.TryTake(out value) && value != null)
                {
                    evt.Set();
                    if (CheckTaskFitness(self, value) || aggressive)
                    {
                        value.Execute(null);
                    }
                    else
                    {
                        if (autoReference == null)
                        {
                            sharedWorkQueue.TryAdd(value);
                        }
                        else
                        {
                            autoReference.dDeque.PushBottom(value);
                        }
                        evt.Set();
                    }

                    if (predicateEvt.IsSet || watch.ElapsedMilliseconds > millisecondsTimeout)
                    {
                        return;
                    }
                }

                // First check to see if we comply to predicate
                if (predicateEvt.IsSet || watch.ElapsedMilliseconds > millisecondsTimeout)
                {
                    return;
                }

                // Try to complete other work by stealing since our desired tasks may be in other worker
                ThreadWorker other;
                for (int i = 0; i < others.Length; i++)
                {
                    if ((other = others [i]) == autoReference || other == null)
                    {
                        continue;
                    }

                    if (other.dDeque.PopTop(out value) == PopResult.Succeed && value != null)
                    {
                        evt.Set();
                        if (CheckTaskFitness(self, value) || aggressive)
                        {
                            value.Execute(null);
                        }
                        else
                        {
                            if (autoReference == null)
                            {
                                sharedWorkQueue.TryAdd(value);
                            }
                            else
                            {
                                autoReference.dDeque.PushBottom(value);
                            }
                            evt.Set();
                        }
                    }

                    if (predicateEvt.IsSet || watch.ElapsedMilliseconds > millisecondsTimeout)
                    {
                        return;
                    }
                }

                /* Waiting is split in 4 phases
                 *   - until stage 1 we simply yield the thread to let others add data
                 *   - between stage 1 and stage2 we use ManualResetEventSlim light waiting mechanism
                 *   - after stage2 we fall back to the heavier WaitHandle waiting mechanism
                 *   - if really the situation isn't evolving after a couple of sleep, we disable
                 *     task fitness check altogether
                 */
                if (--tries > stage1)
                {
                    Thread.Yield();
                }
                else if (tries >= stage2)
                {
                    predicateEvt.Wait(ComputeTimeout(5, millisecondsTimeout, watch));
                }
                else
                {
                    if (tries == stage2 - 1)
                    {
                        handles = new [] { predicateEvt.WaitHandle, evt }
                    }
                    ;
                    WaitHandle.WaitAny(handles, ComputeTimeout(1000, millisecondsTimeout, watch));
                    if (tries == stage2 - 10)
                    {
                        aggressive = true;
                    }
                }
            }
        }
Exemple #30
0
        public void LoadPlugins(Game ctx)
        {
            Stopwatch benchmark = Stopwatch.StartNew();

            if (plugins.Count > 0)
            {
                // Quick load
                foreach (IPlugin plugin in plugins)
                {
                    plugin.Initialize(ctx);
                }
            }
            else
            {
                string[] modules = Directory.GetDirectories(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Modules"));
                foreach (string module in modules)
                {
                    // Skip modules without Module.json
                    if (!File.Exists(Path.Combine(module, "module.json")))
                    {
                        continue;
                    }

                    try
                    {
                        string            serializedModule = File.ReadAllText(Path.Combine(module, "module.json"));
                        PluginInformation modInformation   = JsonConvert.DeserializeObject <PluginInformation>(serializedModule);

                        if (File.Exists(Path.Combine(module, modInformation.EntryPoint)))
                        {
                            Debugger.Module($"Compiling plugin: {modInformation.Name}");
                            if (CompilePlugin(module, modInformation))
                            {
                                Debugger.Module($"{modInformation.Name} compiled successfully.");
                            }
                            else
                            {
                                continue;
                            }
                        }

                        foreach (string required in modInformation.Dependencies)
                        {
                            AppDomain.CurrentDomain.Load(AssemblyName.GetAssemblyName(Path.Combine(module, required)));
                        }
                        var plugin = AppDomain.CurrentDomain.Load(AssemblyName.GetAssemblyName(Path.Combine(module, $"{modInformation.Name}.dll")));
                        IEnumerable <Type> entries = plugin.ExportedTypes.Where(exp => exp.GetMethod("Initialize") != null);
                        if (entries.Count() > 0)
                        {
                            dynamic mod = plugin.CreateInstance(entries.First().ToString());
                            mod.Initialize(ctx);
                            plugins.Add(mod);
                        }
                    } catch (Exception err)
                    {
                        Debugger.Error(err);
                        continue;
                    }
                }
            }
            benchmark.Stop();
            Debugger.Module($"Loaded {plugins.Count} module(s) in {benchmark.ElapsedMilliseconds}ms");
        }