|
| 1 | +using Antlr4.Runtime; |
| 2 | +using Antlr4.Runtime.Misc; |
| 3 | +using Antlr4.Runtime.Tree; |
| 4 | +using Microsoft.Vbe.Interop; |
| 5 | +using Rubberduck.Parsing.Grammar; |
| 6 | +using Rubberduck.Parsing.Nodes; |
| 7 | +using Rubberduck.Parsing.Preprocessing; |
| 8 | +using Rubberduck.Parsing.Symbols; |
| 9 | +using Rubberduck.VBEditor; |
| 10 | +using Rubberduck.VBEditor.Extensions; |
| 11 | +using System; |
| 12 | +using System.Collections.Generic; |
| 13 | +using System.Diagnostics; |
| 14 | +using System.Linq; |
| 15 | +using System.Runtime.InteropServices; |
| 16 | +using System.Threading; |
| 17 | +using System.Threading.Tasks; |
| 18 | + |
| 19 | +namespace Rubberduck.Parsing.VBA |
| 20 | +{ |
| 21 | + class ComponentParseTask |
| 22 | + { |
| 23 | + private readonly VBComponent _component; |
| 24 | + private readonly QualifiedModuleName _qualifiedName; |
| 25 | + private readonly TokenStreamRewriter _rewriter; |
| 26 | + private readonly IAttributeParser _attributeParser; |
| 27 | + private readonly VBAPreprocessor _preprocessor; |
| 28 | + |
| 29 | + public event EventHandler<ParseCompletionArgs> ParseCompleted; |
| 30 | + public event EventHandler<ParseFailureArgs> ParseFailure; |
| 31 | + |
| 32 | + public ComponentParseTask(VBComponent vbComponent, VBAPreprocessor preprocessor, IAttributeParser attributeParser, TokenStreamRewriter rewriter = null) |
| 33 | + { |
| 34 | + _attributeParser = attributeParser; |
| 35 | + _preprocessor = preprocessor; |
| 36 | + _component = vbComponent; |
| 37 | + _rewriter = rewriter; |
| 38 | + _qualifiedName = new QualifiedModuleName(vbComponent); |
| 39 | + } |
| 40 | + |
| 41 | + public void Start(CancellationToken token) |
| 42 | + { |
| 43 | + try |
| 44 | + { |
| 45 | + var code = RewriteAndPreprocess(); |
| 46 | + token.ThrowIfCancellationRequested(); |
| 47 | + |
| 48 | + var attributes = _attributeParser.Parse(_component); |
| 49 | + |
| 50 | + token.ThrowIfCancellationRequested(); |
| 51 | + |
| 52 | + // temporal coupling... comments must be acquired before we walk the parse tree for declarations |
| 53 | + // otherwise none of the annotations get associated to their respective Declaration |
| 54 | + var commentListener = new CommentListener(); |
| 55 | + |
| 56 | + var stopwatch = Stopwatch.StartNew(); |
| 57 | + ITokenStream stream; |
| 58 | + var tree = ParseInternal(code, new IParseTreeListener[]{ commentListener }, out stream); |
| 59 | + stopwatch.Stop(); |
| 60 | + if (tree != null) |
| 61 | + { |
| 62 | + Debug.Print("IParseTree for component '{0}' acquired in {1}ms (thread {2})", _component.Name, stopwatch.ElapsedMilliseconds, Thread.CurrentThread.ManagedThreadId); |
| 63 | + } |
| 64 | + |
| 65 | + var comments = QualifyAndUnionComments(_qualifiedName, commentListener.Comments, commentListener.RemComments); |
| 66 | + token.ThrowIfCancellationRequested(); |
| 67 | + |
| 68 | + ParseCompleted.Invoke(this, new ParseCompletionArgs |
| 69 | + { |
| 70 | + ParseTree = tree, |
| 71 | + Tokens = stream, |
| 72 | + Attributes = attributes, |
| 73 | + Comments = comments, |
| 74 | + }); |
| 75 | + } |
| 76 | + catch (COMException exception) |
| 77 | + { |
| 78 | + Debug.WriteLine("Exception thrown in thread {0}:\n{1}", Thread.CurrentThread.ManagedThreadId, exception); |
| 79 | + ParseFailure.Invoke(this, new ParseFailureArgs |
| 80 | + { |
| 81 | + Cause = exception |
| 82 | + }); |
| 83 | + } |
| 84 | + catch (SyntaxErrorException exception) |
| 85 | + { |
| 86 | + Debug.WriteLine("Exception thrown in thread {0}:\n{1}", Thread.CurrentThread.ManagedThreadId, exception); |
| 87 | + ParseFailure.Invoke(this, new ParseFailureArgs |
| 88 | + { |
| 89 | + Cause = exception |
| 90 | + }); |
| 91 | + } |
| 92 | + catch (OperationCanceledException cancel) |
| 93 | + { |
| 94 | + Debug.WriteLine("Operation was Cancelled", cancel); |
| 95 | + // no results to be used, so no results "returned" |
| 96 | + //ParseCompleted.Invoke(this, new ParseCompletionArgs()); |
| 97 | + } |
| 98 | + } |
| 99 | + |
| 100 | + private string RewriteAndPreprocess() |
| 101 | + { |
| 102 | + var code = _rewriter == null ? string.Join(Environment.NewLine, _component.CodeModule.GetSanitizedCode()) : _rewriter.GetText(); |
| 103 | + string processed; |
| 104 | + try |
| 105 | + { |
| 106 | + processed = _preprocessor.Execute(code); |
| 107 | + } |
| 108 | + catch (VBAPreprocessorException) |
| 109 | + { |
| 110 | + Debug.WriteLine("Falling back to no preprocessing"); |
| 111 | + processed = code; |
| 112 | + } |
| 113 | + return processed; |
| 114 | + } |
| 115 | + |
| 116 | + private static IParseTree ParseInternal(string code, IParseTreeListener[] listeners, out ITokenStream outStream) |
| 117 | + { |
| 118 | + var stream = new AntlrInputStream(code); |
| 119 | + var lexer = new VBALexer(stream); |
| 120 | + var tokens = new CommonTokenStream(lexer); |
| 121 | + var parser = new VBAParser(tokens); |
| 122 | + |
| 123 | + parser.AddErrorListener(new ExceptionErrorListener()); |
| 124 | + foreach (var l in listeners) |
| 125 | + { |
| 126 | + parser.AddParseListener(l); |
| 127 | + } |
| 128 | + |
| 129 | + outStream = tokens; |
| 130 | + return parser.startRule(); |
| 131 | + } |
| 132 | + |
| 133 | + private IEnumerable<CommentNode> QualifyAndUnionComments(QualifiedModuleName qualifiedName, IEnumerable<VBAParser.CommentContext> comments, IEnumerable<VBAParser.RemCommentContext> remComments) |
| 134 | + { |
| 135 | + var commentNodes = comments.Select(comment => new CommentNode(comment.GetComment(), Tokens.CommentMarker, new QualifiedSelection(qualifiedName, comment.GetSelection()))); |
| 136 | + var remCommentNodes = remComments.Select(comment => new CommentNode(comment.GetComment(), Tokens.Rem, new QualifiedSelection(qualifiedName, comment.GetSelection()))); |
| 137 | + var allCommentNodes = commentNodes.Union(remCommentNodes); |
| 138 | + return allCommentNodes; |
| 139 | + } |
| 140 | + |
| 141 | + public class ParseCompletionArgs |
| 142 | + { |
| 143 | + public ITokenStream Tokens { get; internal set; } |
| 144 | + public IParseTree ParseTree { get; internal set; } |
| 145 | + public IDictionary<Tuple<string, DeclarationType>, Attributes> Attributes { get; internal set; } |
| 146 | + public IEnumerable<CommentNode> Comments { get; internal set; } |
| 147 | + } |
| 148 | + |
| 149 | + public class ParseFailureArgs |
| 150 | + { |
| 151 | + public Exception Cause { get; internal set; } |
| 152 | + } |
| 153 | + |
| 154 | + private class CommentListener : VBAParserBaseListener |
| 155 | + { |
| 156 | + private readonly IList<VBAParser.RemCommentContext> _remComments = new List<VBAParser.RemCommentContext>(); |
| 157 | + public IEnumerable<VBAParser.RemCommentContext> RemComments { get { return _remComments; } } |
| 158 | + |
| 159 | + private readonly IList<VBAParser.CommentContext> _comments = new List<VBAParser.CommentContext>(); |
| 160 | + public IEnumerable<VBAParser.CommentContext> Comments { get { return _comments; } } |
| 161 | + |
| 162 | + public override void ExitRemComment([NotNull] VBAParser.RemCommentContext context) |
| 163 | + { |
| 164 | + _remComments.Add(context); |
| 165 | + } |
| 166 | + |
| 167 | + public override void ExitComment([NotNull] VBAParser.CommentContext context) |
| 168 | + { |
| 169 | + _comments.Add(context); |
| 170 | + } |
| 171 | + } |
| 172 | + } |
| 173 | +} |
0 commit comments