Example #1
0
        public override void Fix(IInspectionResult result)
        {
            var annotationText = $"'@Ignore {result.Inspection.AnnotationName}";

            int    annotationLine;
            string codeLine;

            using (var module = _state.ProjectsProvider.Component(result.QualifiedSelection.QualifiedName).CodeModule)
            {
                annotationLine = result.QualifiedSelection.Selection.StartLine;
                while (annotationLine != 1 && module.GetLines(annotationLine - 1, 1).EndsWith(" _"))
                {
                    annotationLine--;
                }

                codeLine = annotationLine == 1 ? string.Empty : module.GetLines(annotationLine - 1, 1);
            }

            RuleContext treeRoot = result.Context;

            while (treeRoot.Parent != null)
            {
                treeRoot = treeRoot.Parent;
            }

            if (codeLine.HasComment(out var commentStart) && codeLine.Substring(commentStart).StartsWith("'@Ignore "))
            {
                var listener = new AnnotationListener();
                ParseTreeWalker.Default.Walk(listener, treeRoot);

                var annotationContext = listener.Contexts.Last(i => i.Start.TokenIndex <= result.Context.Start.TokenIndex);

                var rewriter = _state.GetRewriter(result.QualifiedSelection.QualifiedName);
                rewriter.InsertAfter(annotationContext.annotationName().Stop.TokenIndex, $" {result.Inspection.AnnotationName},");
            }
        public void Start(CancellationToken token)
        {
            try
            {
                var code = RewriteAndPreprocess();
                token.ThrowIfCancellationRequested();

                var attributes = _attributeParser.Parse(_component);

                token.ThrowIfCancellationRequested();

                // temporal coupling... comments must be acquired before we walk the parse tree for declarations
                // otherwise none of the annotations get associated to their respective Declaration
                var commentListener    = new CommentListener();
                var annotationListener = new AnnotationListener(new VBAParserAnnotationFactory(), _qualifiedName);

                var          stopwatch = Stopwatch.StartNew();
                ITokenStream stream;
                var          tree = ParseInternal(_component.Name, code, new IParseTreeListener[] { commentListener, annotationListener }, out stream);
                stopwatch.Stop();
                if (tree != null)
                {
                    _logger.Trace("IParseTree for component '{0}' acquired in {1}ms (thread {2})", _component.Name, stopwatch.ElapsedMilliseconds, Thread.CurrentThread.ManagedThreadId);
                }

                var comments = QualifyAndUnionComments(_qualifiedName, commentListener.Comments, commentListener.RemComments);
                token.ThrowIfCancellationRequested();

                ParseCompleted.Invoke(this, new ParseCompletionArgs
                {
                    ParseTree   = tree,
                    Tokens      = stream,
                    Attributes  = attributes,
                    Comments    = comments,
                    Annotations = annotationListener.Annotations
                });
            }
            catch (COMException exception)
            {
                _logger.Error(exception, "Exception thrown in thread {0}.", Thread.CurrentThread.ManagedThreadId);
                ParseFailure.Invoke(this, new ParseFailureArgs
                {
                    Cause = exception
                });
            }
            catch (SyntaxErrorException exception)
            {
                _logger.Error(exception, "Exception thrown in thread {0}.", Thread.CurrentThread.ManagedThreadId);
                ParseFailure.Invoke(this, new ParseFailureArgs
                {
                    Cause = exception
                });
            }
            catch (OperationCanceledException)
            {
                _logger.Debug("Component {0}: Operation was Cancelled", _component.Name);
                // no results to be used, so no results "returned"
                //ParseCompleted.Invoke(this, new ParseCompletionArgs());
            }
        }
Example #3
0
        private (IEnumerable <CommentNode> Comments, IEnumerable <IAnnotation> Annotations) CommentsAndAnnotations(QualifiedModuleName module, IParseTree tree)
        {
            var commentListener    = new CommentListener();
            var annotationListener = new AnnotationListener(new VBAParserAnnotationFactory(), module);
            var combinedListener   = new CombinedParseTreeListener(new IParseTreeListener[] { commentListener, annotationListener });

            ParseTreeWalker.Default.Walk(combinedListener, tree);
            var comments    = QualifyAndUnionComments(module, commentListener.Comments, commentListener.RemComments);
            var annotations = annotationListener.Annotations;

            return(comments, annotations);
        }
Example #4
0
        public void Start(CancellationToken cancellationToken)
        {
            try
            {
                Logger.Trace($"Starting ParseTaskID {_taskId} on thread {Thread.CurrentThread.ManagedThreadId}.");

                var tokenStream = RewriteAndPreprocess(cancellationToken);
                cancellationToken.ThrowIfCancellationRequested();

                // temporal coupling... comments must be acquired before we walk the parse tree for declarations
                // otherwise none of the annotations get associated to their respective Declaration
                var commentListener    = new CommentListener();
                var annotationListener = new AnnotationListener(new VBAParserAnnotationFactory(), _module);

                var stopwatch            = Stopwatch.StartNew();
                var codePaneParseResults = ParseInternal(_module.ComponentName, tokenStream, new IParseTreeListener[] { commentListener, annotationListener });
                stopwatch.Stop();
                cancellationToken.ThrowIfCancellationRequested();

                var comments = QualifyAndUnionComments(_module, commentListener.Comments, commentListener.RemComments);
                cancellationToken.ThrowIfCancellationRequested();

                var attributesPassParseResults = RunAttributesPass(cancellationToken);
                var rewriter = new MemberAttributesRewriter(_exporter, _module.Component.CodeModule, new TokenStreamRewriter(attributesPassParseResults.tokenStream ?? tokenStream));

                var completedHandler = ParseCompleted;
                if (completedHandler != null && !cancellationToken.IsCancellationRequested)
                {
                    completedHandler.Invoke(this, new ParseCompletionArgs
                    {
                        ParseTree          = codePaneParseResults.tree,
                        AttributesTree     = attributesPassParseResults.tree,
                        Tokens             = codePaneParseResults.tokenStream,
                        AttributesRewriter = rewriter,
                        Attributes         = attributesPassParseResults.attributes,
                        Comments           = comments,
                        Annotations        = annotationListener.Annotations
                    });
                }
            }
            catch (COMException exception)
            {
                Logger.Error(exception, $"COM Exception thrown in thread {Thread.CurrentThread.ManagedThreadId} while parsing module {_module.ComponentName}, ParseTaskID {_taskId}.");
                var failedHandler = ParseFailure;
                failedHandler?.Invoke(this, new ParseFailureArgs
                {
                    Cause = exception
                });
            }
            catch (PreprocessorSyntaxErrorException syntaxErrorException)
            {
                var parsePassText = syntaxErrorException.ParsePass == ParsePass.CodePanePass
                    ? "code pane"
                    : "exported";
                Logger.Error($"Syntax error while preprocessing; offending token '{syntaxErrorException.OffendingSymbol.Text}' at line {syntaxErrorException.LineNumber}, column {syntaxErrorException.Position} in the {parsePassText} version of module {_module.ComponentName}.");
                Logger.Debug(syntaxErrorException, $"SyntaxErrorException thrown in thread {Thread.CurrentThread.ManagedThreadId}, ParseTaskID {_taskId}.");

                ReportException(syntaxErrorException);
            }
            catch (ParsePassSyntaxErrorException syntaxErrorException)
            {
                var parsePassText = syntaxErrorException.ParsePass == ParsePass.CodePanePass
                    ? "code pane"
                    : "exported";
                Logger.Error($"Syntax error; offending token '{syntaxErrorException.OffendingSymbol.Text}' at line {syntaxErrorException.LineNumber}, column {syntaxErrorException.Position} in the {parsePassText} version of module {_module.ComponentName}.");
                Logger.Debug(syntaxErrorException, $"SyntaxErrorException thrown in thread {Thread.CurrentThread.ManagedThreadId}, ParseTaskID {_taskId}.");

                ReportException(syntaxErrorException);
            }
            catch (SyntaxErrorException syntaxErrorException)
            {
                Logger.Error($"Syntax error; offending token '{syntaxErrorException.OffendingSymbol.Text}' at line {syntaxErrorException.LineNumber}, column {syntaxErrorException.Position} in module {_module.ComponentName}.");
                Logger.Debug(syntaxErrorException, $"SyntaxErrorException thrown in thread {Thread.CurrentThread.ManagedThreadId}, ParseTaskID {_taskId}.");

                ReportException(syntaxErrorException);
            }
            catch (OperationCanceledException exception)
            {
                //We report this, so that the calling code knows that the operation actually has been cancelled.
                ReportException(exception);
            }
            catch (Exception exception)
            {
                Logger.Error(exception, $" Unexpected exception thrown in thread {Thread.CurrentThread.ManagedThreadId} while parsing module {_module.ComponentName}, ParseTaskID {_taskId}.");

                ReportException(exception);
            }
        }
Example #5
0
        public void Start(CancellationToken token)
        {
            try
            {
                Logger.Trace("Starting ParseTaskID {0} on thread {1}.", _taskId, Thread.CurrentThread.ManagedThreadId);

                var code = RewriteAndPreprocess(token);
                token.ThrowIfCancellationRequested();

                var attributes = _attributeParser.Parse(_component, token);

                // temporal coupling... comments must be acquired before we walk the parse tree for declarations
                // otherwise none of the annotations get associated to their respective Declaration
                var commentListener    = new CommentListener();
                var annotationListener = new AnnotationListener(new VBAParserAnnotationFactory(), _qualifiedName);

                var          stopwatch = Stopwatch.StartNew();
                ITokenStream stream;
                var          tree = ParseInternal(_component.Name, code, new IParseTreeListener[] { commentListener, annotationListener }, out stream);
                stopwatch.Stop();
                token.ThrowIfCancellationRequested();

                var comments = QualifyAndUnionComments(_qualifiedName, commentListener.Comments, commentListener.RemComments);
                token.ThrowIfCancellationRequested();

                var completedHandler = ParseCompleted;
                if (completedHandler != null && !token.IsCancellationRequested)
                {
                    completedHandler.Invoke(this, new ParseCompletionArgs
                    {
                        ParseTree   = tree,
                        Tokens      = stream,
                        Attributes  = attributes,
                        Comments    = comments,
                        Annotations = annotationListener.Annotations
                    });
                }
            }
            catch (COMException exception)
            {
                Logger.Error(exception, "Exception thrown in thread {0}, ParseTaskID {1}.", Thread.CurrentThread.ManagedThreadId, _taskId);
                var failedHandler = ParseFailure;
                if (failedHandler != null)
                {
                    failedHandler.Invoke(this, new ParseFailureArgs
                    {
                        Cause = exception
                    });
                }
            }
            catch (SyntaxErrorException exception)
            {
                Logger.Warn("Syntax error; offending token '{0}' at line {1}, column {2} in module {3}.", exception.OffendingSymbol.Text, exception.LineNumber, exception.Position, _qualifiedName);
                Logger.Error(exception, "Exception thrown in thread {0}, ParseTaskID {1}.", Thread.CurrentThread.ManagedThreadId, _taskId);
                var failedHandler = ParseFailure;
                if (failedHandler != null)
                {
                    failedHandler.Invoke(this, new ParseFailureArgs
                    {
                        Cause = exception
                    });
                }
            }
            catch (OperationCanceledException exception)
            {
                //We return this, so that the calling code knows that the operation actually has been cancelled.
                var failedHandler = ParseFailure;
                if (failedHandler != null)
                {
                    failedHandler.Invoke(this, new ParseFailureArgs
                    {
                        Cause = exception
                    });
                }
            }
            catch (Exception exception)
            {
                Logger.Error(exception, "Exception thrown in thread {0}, ParseTaskID {1}.", Thread.CurrentThread.ManagedThreadId, _taskId);
                var failedHandler = ParseFailure;
                if (failedHandler != null)
                {
                    failedHandler.Invoke(this, new ParseFailureArgs
                    {
                        Cause = exception
                    });
                }
            }
        }
Example #6
0
        public void Start(CancellationToken token)
        {
            try
            {
                Logger.Trace($"Starting ParseTaskID {_taskId} on thread {Thread.CurrentThread.ManagedThreadId}.");

                var tokenStream = RewriteAndPreprocess(token);
                token.ThrowIfCancellationRequested();

                IParseTree attributesTree;
                IDictionary <Tuple <string, DeclarationType>, Attributes> attributes;
                var attributesTokenStream = RunAttributesPass(token, out attributesTree, out attributes);

                var rewriter = new MemberAttributesRewriter(_exporter, _component.CodeModule, new TokenStreamRewriter(attributesTokenStream ?? tokenStream));

                // temporal coupling... comments must be acquired before we walk the parse tree for declarations
                // otherwise none of the annotations get associated to their respective Declaration
                var commentListener    = new CommentListener();
                var annotationListener = new AnnotationListener(new VBAParserAnnotationFactory(), _qualifiedName);

                var          stopwatch = Stopwatch.StartNew();
                ITokenStream stream;
                var          tree = ParseInternal(_component.Name, tokenStream, new IParseTreeListener[] { commentListener, annotationListener }, out stream);
                stopwatch.Stop();
                token.ThrowIfCancellationRequested();

                var comments = QualifyAndUnionComments(_qualifiedName, commentListener.Comments, commentListener.RemComments);
                token.ThrowIfCancellationRequested();

                var completedHandler = ParseCompleted;
                if (completedHandler != null && !token.IsCancellationRequested)
                {
                    completedHandler.Invoke(this, new ParseCompletionArgs
                    {
                        ParseTree          = tree,
                        AttributesTree     = attributesTree,
                        Tokens             = stream,
                        AttributesRewriter = rewriter,
                        Attributes         = attributes,
                        Comments           = comments,
                        Annotations        = annotationListener.Annotations
                    });
                }
            }
            catch (COMException exception)
            {
                Logger.Error(exception, $"Exception thrown in thread {Thread.CurrentThread.ManagedThreadId} while parsing module {_qualifiedName.Name}, ParseTaskID {_taskId}.");
                var failedHandler = ParseFailure;
                failedHandler?.Invoke(this, new ParseFailureArgs
                {
                    Cause = exception
                });
            }
            catch (SyntaxErrorException exception)
            {
                Logger.Warn($"Syntax error; offending token '{exception.OffendingSymbol.Text}' at line {exception.LineNumber}, column {exception.Position} in module {_qualifiedName}.");
                Logger.Error(exception, $"Exception thrown in thread {Thread.CurrentThread.ManagedThreadId}, ParseTaskID {_taskId}.");
                var failedHandler = ParseFailure;
                failedHandler?.Invoke(this, new ParseFailureArgs
                {
                    Cause = exception
                });
            }
            catch (OperationCanceledException exception)
            {
                //We return this, so that the calling code knows that the operation actually has been cancelled.
                var failedHandler = ParseFailure;
                failedHandler?.Invoke(this, new ParseFailureArgs
                {
                    Cause = exception
                });
            }
            catch (Exception exception)
            {
                Logger.Error(exception, $"Exception thrown in thread {Thread.CurrentThread.ManagedThreadId} while parsing module {_qualifiedName.Name}, ParseTaskID {_taskId}.");
                var failedHandler = ParseFailure;
                failedHandler?.Invoke(this, new ParseFailureArgs
                {
                    Cause = exception
                });
            }
        }