@@ -20,8 +20,6 @@ namespace Rubberduck.Parsing.VBA
20
20
{
21
21
class ComponentParseTask
22
22
{
23
- private readonly IParseTreeListener [ ] _listeners ;
24
-
25
23
private readonly VBComponent _component ;
26
24
private readonly QualifiedModuleName _qualifiedName ;
27
25
private readonly TokenStreamRewriter _rewriter ;
@@ -34,14 +32,6 @@ class ComponentParseTask
34
32
public ComponentParseTask ( VBComponent vbComponent , VBAPreprocessor preprocessor , IAttributeParser attributeParser , TokenStreamRewriter rewriter = null )
35
33
{
36
34
_component = vbComponent ;
37
- _listeners = new IParseTreeListener [ ]
38
- {
39
- new ObsoleteCallStatementListener ( ) ,
40
- new ObsoleteLetStatementListener ( ) ,
41
- new EmptyStringLiteralListener ( ) ,
42
- new ArgListWithOneByRefParamListener ( ) ,
43
- new CommentListener ( ) ,
44
- } ;
45
35
_rewriter = rewriter ;
46
36
_qualifiedName = new QualifiedModuleName ( vbComponent ) ;
47
37
}
@@ -58,38 +48,32 @@ private void ParseInternal(CancellationToken token)
58
48
var code = RewriteAndPreprocess ( ) ;
59
49
token . ThrowIfCancellationRequested ( ) ;
60
50
51
+ // temporal coupling... comments must be acquired before we walk the parse tree for declarations
52
+ // otherwise none of the annotations get associated to their respective Declaration
53
+ var commentListener = new CommentListener ( ) ;
54
+
61
55
var stopwatch = Stopwatch . StartNew ( ) ;
62
56
ITokenStream stream ;
63
- var tree = ParseInternal ( code , _listeners , out stream ) ;
57
+ var tree = ParseInternal ( code , new IParseTreeListener [ ] { commentListener } , out stream ) ;
64
58
stopwatch . Stop ( ) ;
65
59
if ( tree != null )
66
60
{
67
61
Debug . Print ( "IParseTree for component '{0}' acquired in {1}ms (thread {2})" , _component . Name , stopwatch . ElapsedMilliseconds , Thread . CurrentThread . ManagedThreadId ) ;
68
62
}
69
63
64
+ var comments = QualifyAndUnionComments ( _qualifiedName , commentListener . Comments , commentListener . RemComments ) ;
70
65
token . ThrowIfCancellationRequested ( ) ;
71
66
72
67
var attributes = _attributeParser . Parse ( _component ) ;
73
- CommentListener commentListener = _listeners . OfType < CommentListener > ( ) . Single ( ) ;
74
- var comments = ParseComments ( _qualifiedName , commentListener . Comments , commentListener . RemComments ) ;
75
68
76
69
token . ThrowIfCancellationRequested ( ) ;
77
70
78
- var obsoleteCallsListener = _listeners . OfType < ObsoleteCallStatementListener > ( ) . Single ( ) ;
79
- var obsoleteLetListener = _listeners . OfType < ObsoleteLetStatementListener > ( ) . Single ( ) ;
80
- var emptyStringLiteralListener = _listeners . OfType < EmptyStringLiteralListener > ( ) . Single ( ) ;
81
- var argListsWithOneByRefParamListener = _listeners . OfType < ArgListWithOneByRefParamListener > ( ) . Single ( ) ;
82
-
83
71
ParseCompleted . Invoke ( this , new ParseCompletionArgs
84
72
{
85
- Comments = comments ,
86
73
ParseTree = tree ,
87
74
Tokens = stream ,
88
75
Attributes = attributes ,
89
- ObsoleteCallContexts = obsoleteCallsListener . Contexts . Select ( context => new QualifiedContext ( _qualifiedName , context ) ) ,
90
- ObsoleteLetContexts = obsoleteLetListener . Contexts . Select ( context => new QualifiedContext ( _qualifiedName , context ) ) ,
91
- EmptyStringLiterals = emptyStringLiteralListener . Contexts . Select ( context => new QualifiedContext ( _qualifiedName , context ) ) ,
92
- ArgListsWithOneByRefParam = argListsWithOneByRefParamListener . Contexts . Select ( context => new QualifiedContext ( _qualifiedName , context ) ) ,
76
+ Comments = comments ,
93
77
} ) ;
94
78
}
95
79
catch ( COMException exception )
@@ -132,25 +116,24 @@ private string RewriteAndPreprocess()
132
116
return processed ;
133
117
}
134
118
135
- private static IParseTree ParseInternal ( string code , IEnumerable < IParseTreeListener > listeners , out ITokenStream outStream )
119
+ private static IParseTree ParseInternal ( string code , IParseTreeListener [ ] listeners , out ITokenStream outStream )
136
120
{
137
121
var stream = new AntlrInputStream ( code ) ;
138
122
var lexer = new VBALexer ( stream ) ;
139
123
var tokens = new CommonTokenStream ( lexer ) ;
140
124
var parser = new VBAParser ( tokens ) ;
141
125
142
126
parser . AddErrorListener ( new ExceptionErrorListener ( ) ) ;
143
- foreach ( var listener in listeners )
127
+ foreach ( var l in listeners )
144
128
{
145
- parser . AddParseListener ( listener ) ;
129
+ parser . AddParseListener ( l ) ;
146
130
}
147
131
148
132
outStream = tokens ;
149
133
return parser . startRule ( ) ;
150
134
}
151
135
152
-
153
- private IEnumerable < CommentNode > ParseComments ( QualifiedModuleName qualifiedName , IEnumerable < VBAParser . CommentContext > comments , IEnumerable < VBAParser . RemCommentContext > remComments )
136
+ private IEnumerable < CommentNode > QualifyAndUnionComments ( QualifiedModuleName qualifiedName , IEnumerable < VBAParser . CommentContext > comments , IEnumerable < VBAParser . RemCommentContext > remComments )
154
137
{
155
138
var commentNodes = comments . Select ( comment => new CommentNode ( comment . GetComment ( ) , Tokens . CommentMarker , new QualifiedSelection ( qualifiedName , comment . GetSelection ( ) ) ) ) ;
156
139
var remCommentNodes = remComments . Select ( comment => new CommentNode ( comment . GetComment ( ) , Tokens . Rem , new QualifiedSelection ( qualifiedName , comment . GetSelection ( ) ) ) ) ;
@@ -167,107 +150,32 @@ public class ParseCompletionArgs
167
150
{
168
151
public ITokenStream Tokens { get ; internal set ; }
169
152
public IParseTree ParseTree { get ; internal set ; }
170
- public IEnumerable < CommentNode > Comments { get ; internal set ; }
171
- public IEnumerable < QualifiedContext > ObsoleteCallContexts { get ; internal set ; }
172
- public IEnumerable < QualifiedContext > ObsoleteLetContexts { get ; internal set ; }
173
- public IEnumerable < QualifiedContext > EmptyStringLiterals { get ; internal set ; }
174
- public IEnumerable < QualifiedContext > ArgListsWithOneByRefParam { get ; internal set ; }
175
- public IEnumerable < Declaration > Declarations { get ; internal set ; }
176
153
public IDictionary < Tuple < string , DeclarationType > , Attributes > Attributes { get ; internal set ; }
154
+ public IEnumerable < CommentNode > Comments { get ; internal set ; }
177
155
}
178
156
179
157
public class ParseFailureArgs
180
158
{
181
159
public Exception Cause { get ; internal set ; }
182
160
}
183
- }
184
-
185
- #region Listener classes
186
- class ObsoleteCallStatementListener : VBABaseListener
187
- {
188
- private readonly IList < VBAParser . ExplicitCallStmtContext > _contexts = new List < VBAParser . ExplicitCallStmtContext > ( ) ;
189
- public IEnumerable < VBAParser . ExplicitCallStmtContext > Contexts { get { return _contexts ; } }
190
161
191
- public override void ExitExplicitCallStmt ( VBAParser . ExplicitCallStmtContext context )
162
+ private class CommentListener : VBABaseListener
192
163
{
193
- var procedureCall = context . eCS_ProcedureCall ( ) ;
194
- if ( procedureCall != null )
195
- {
196
- if ( procedureCall . CALL ( ) != null )
197
- {
198
- _contexts . Add ( context ) ;
199
- return ;
200
- }
201
- }
164
+ private readonly IList < VBAParser . RemCommentContext > _remComments = new List < VBAParser . RemCommentContext > ( ) ;
165
+ public IEnumerable < VBAParser . RemCommentContext > RemComments { get { return _remComments ; } }
202
166
203
- var memberCall = context . eCS_MemberProcedureCall ( ) ;
204
- if ( memberCall == null ) return ;
205
- if ( memberCall . CALL ( ) == null ) return ;
206
- _contexts . Add ( context ) ;
207
- }
208
- }
167
+ private readonly IList < VBAParser . CommentContext > _comments = new List < VBAParser . CommentContext > ( ) ;
168
+ public IEnumerable < VBAParser . CommentContext > Comments { get { return _comments ; } }
209
169
210
- class ObsoleteLetStatementListener : VBABaseListener
211
- {
212
- private readonly IList < VBAParser . LetStmtContext > _contexts = new List < VBAParser . LetStmtContext > ( ) ;
213
- public IEnumerable < VBAParser . LetStmtContext > Contexts { get { return _contexts ; } }
214
-
215
- public override void ExitLetStmt ( VBAParser . LetStmtContext context )
216
- {
217
- if ( context . LET ( ) != null )
170
+ public override void ExitRemComment ( [ NotNull ] VBAParser . RemCommentContext context )
218
171
{
219
- _contexts . Add ( context ) ;
172
+ _remComments . Add ( context ) ;
220
173
}
221
- }
222
- }
223
174
224
- class EmptyStringLiteralListener : VBABaseListener
225
- {
226
- private readonly IList < VBAParser . LiteralContext > _contexts = new List < VBAParser . LiteralContext > ( ) ;
227
- public IEnumerable < VBAParser . LiteralContext > Contexts { get { return _contexts ; } }
228
-
229
- public override void ExitLiteral ( VBAParser . LiteralContext context )
230
- {
231
- var literal = context . STRINGLITERAL ( ) ;
232
- if ( literal != null && literal . GetText ( ) == "\" \" " )
233
- {
234
- _contexts . Add ( context ) ;
235
- }
236
- }
237
- }
238
-
239
- class ArgListWithOneByRefParamListener : VBABaseListener
240
- {
241
- private readonly IList < VBAParser . ArgListContext > _contexts = new List < VBAParser . ArgListContext > ( ) ;
242
- public IEnumerable < VBAParser . ArgListContext > Contexts { get { return _contexts ; } }
243
-
244
- public override void ExitArgList ( VBAParser . ArgListContext context )
245
- {
246
- if ( context . arg ( ) != null && context . arg ( ) . Count ( a => a . BYREF ( ) != null || ( a . BYREF ( ) == null && a . BYVAL ( ) == null ) ) == 1 )
175
+ public override void ExitComment ( [ NotNull ] VBAParser . CommentContext context )
247
176
{
248
- _contexts . Add ( context ) ;
177
+ _comments . Add ( context ) ;
249
178
}
250
179
}
251
180
}
252
-
253
- class CommentListener : VBABaseListener
254
- {
255
- private readonly IList < VBAParser . RemCommentContext > _remComments = new List < VBAParser . RemCommentContext > ( ) ;
256
- public IEnumerable < VBAParser . RemCommentContext > RemComments { get { return _remComments ; } }
257
-
258
- private readonly IList < VBAParser . CommentContext > _comments = new List < VBAParser . CommentContext > ( ) ;
259
- public IEnumerable < VBAParser . CommentContext > Comments { get { return _comments ; } }
260
-
261
- public override void ExitRemComment ( [ NotNull ] VBAParser . RemCommentContext context )
262
- {
263
- _remComments . Add ( context ) ;
264
- }
265
-
266
- public override void ExitComment ( [ NotNull ] VBAParser . CommentContext context )
267
- {
268
- _comments . Add ( context ) ;
269
- }
270
- }
271
-
272
- #endregion
273
181
}
0 commit comments