17
17
18
18
namespace Rubberduck . Parsing . VBA
19
19
{
20
- class RubberduckParserReimpl : IRubberduckParser
20
+ public class RubberduckParser : IRubberduckParser
21
21
{
22
22
public RubberduckParserState State
23
23
{
@@ -27,8 +27,8 @@ public RubberduckParserState State
27
27
}
28
28
}
29
29
30
- private readonly CancellationTokenSource _central = new CancellationTokenSource ( ) ;
31
- private readonly CancellationTokenSource _resolverTokenSource ; // linked to _central later
30
+ private CancellationTokenSource _central = new CancellationTokenSource ( ) ;
31
+ private CancellationTokenSource _resolverTokenSource ; // linked to _central later
32
32
private readonly Dictionary < VBComponent , Tuple < Task , CancellationTokenSource > > _currentTasks = new Dictionary < VBComponent , Tuple < Task , CancellationTokenSource > > ( ) ;
33
33
34
34
private readonly Dictionary < VBComponent , IParseTree > _parseTrees = new Dictionary < VBComponent , IParseTree > ( ) ;
@@ -45,7 +45,7 @@ private readonly IDictionary<VBComponent, IDictionary<Tuple<string, DeclarationT
45
45
private readonly RubberduckParserState _state ;
46
46
private readonly IAttributeParser _attributeParser ;
47
47
48
- public RubberduckParserReimpl ( VBE vbe , RubberduckParserState state , IAttributeParser attributeParser )
48
+ public RubberduckParser ( VBE vbe , RubberduckParserState state , IAttributeParser attributeParser )
49
49
{
50
50
_resolverTokenSource = CancellationTokenSource . CreateLinkedTokenSource ( _central . Token ) ;
51
51
_vbe = vbe ;
@@ -60,11 +60,13 @@ public RubberduckParserReimpl(VBE vbe, RubberduckParserState state, IAttributePa
60
60
61
61
private void StateOnStateChanged ( object sender , EventArgs e )
62
62
{
63
- var args = e as ParserStateEventArgs ;
63
+ ParserStateEventArgs args = e as ParserStateEventArgs ;
64
+ Debug . WriteLine ( "RubberduckParser handles OnStateChanged ({0})" , _state . Status ) ;
65
+ // why access _state and not pass through EventArgs?
64
66
if ( args . State == ParserState . Parsed )
65
67
{
68
+ Debug . WriteLine ( "(handling OnStateChanged) Starting resolver task" ) ;
66
69
Task . Run ( ( ) => Resolve ( _central . Token ) ) ;
67
- // Resolving should be triggered.. not our job?
68
70
}
69
71
}
70
72
@@ -79,7 +81,18 @@ private void ReparseRequested(object sender, EventArgs e)
79
81
else
80
82
{
81
83
Cancel ( args . Component ) ;
82
- ParseAsync ( args . Component , CancellationToken . None , _state . GetRewriter ( args . Component ) ) ;
84
+ ParseAsync ( args . Component , CancellationToken . None ) ;
85
+ }
86
+ }
87
+
88
+ public void Parse ( )
89
+ {
90
+ try
91
+ {
92
+ ParseAll ( ) ;
93
+ } catch ( Exception e )
94
+ {
95
+ Debug . WriteLine ( e ) ;
83
96
}
84
97
}
85
98
@@ -103,16 +116,20 @@ private void ParseAll()
103
116
{
104
117
while ( ! _state . ClearDeclarations ( vbComponent ) ) { }
105
118
106
- ParseAsync ( vbComponent , CancellationToken . None , _state . GetRewriter ( vbComponent ) ) ;
119
+ ParseAsync ( vbComponent , CancellationToken . None ) ;
107
120
}
108
121
}
109
122
110
123
public Task ParseAsync ( VBComponent component , CancellationToken token , TokenStreamRewriter rewriter = null )
111
124
{
112
- // FIXME remove invalidated "things"from _state
113
- // this includes: Declarations, Comments, Attributes, "InspectionResults" (ObsoleteCall, ObsoleteLet, EmptyStringLiteral, ArgLists with OneByRef) and possibly more...
125
+ // Remove invalidated "things" from _state
126
+ // this includes: Declarations, Comments, Attributes, Exceptions, ParseTree and TokenStream
127
+ // how that works with the Inspecion results is not quite clear
114
128
_state . ClearDeclarations ( component ) ;
115
- _state . SetModuleState ( component , ParserState . Pending ) ;
129
+ _state . AddParseTree ( component , null ) ;
130
+ _state . AddTokenStream ( component , null ) ;
131
+
132
+ _state . SetModuleState ( component , ParserState . Pending ) ; // also clears module-exceptions
116
133
_state . SetModuleComments ( component , Enumerable . Empty < CommentNode > ( ) ) ;
117
134
_state . SetModuleAttributes ( component , new Dictionary < Tuple < string , DeclarationType > , Attributes > ( ) ) ;
118
135
@@ -127,35 +144,49 @@ public Task ParseAsync(VBComponent component, CancellationToken token, TokenStre
127
144
}
128
145
129
146
public void Cancel ( VBComponent component = null )
130
- {
131
- if ( component == null )
132
- {
133
- _central . Cancel ( false ) ;
134
- }
135
- else
136
- {
137
- _resolverTokenSource . Cancel ( false ) ;
138
- Tuple < Task , CancellationTokenSource > result ;
139
- if ( _currentTasks . TryGetValue ( component , out result ) )
147
+ {
148
+ lock ( _central )
149
+ lock ( _resolverTokenSource )
140
150
{
141
- result . Item2 . Cancel ( false ) ;
151
+ if ( component == null )
152
+ {
153
+ _central . Cancel ( false ) ;
154
+
155
+ _central . Dispose ( ) ;
156
+ _central = new CancellationTokenSource ( ) ;
157
+ _resolverTokenSource = CancellationTokenSource . CreateLinkedTokenSource ( _central . Token ) ;
158
+ }
159
+ else
160
+ {
161
+ _resolverTokenSource . Cancel ( false ) ;
162
+ _resolverTokenSource . Dispose ( ) ;
163
+
164
+ _resolverTokenSource = CancellationTokenSource . CreateLinkedTokenSource ( _central . Token ) ;
165
+ Tuple < Task , CancellationTokenSource > result ;
166
+ if ( _currentTasks . TryGetValue ( component , out result ) )
167
+ {
168
+ result . Item2 . Cancel ( false ) ;
169
+ result . Item2 . Dispose ( ) ;
170
+ }
171
+ }
172
+
142
173
}
143
- }
144
174
}
145
175
146
176
private void ParseAsyncInternal ( VBComponent component , CancellationToken token , TokenStreamRewriter rewriter = null )
147
177
{
148
178
var preprocessor = new VBAPreprocessor ( double . Parse ( _vbe . Version , CultureInfo . InvariantCulture ) ) ;
149
- var parser = new ComponentParseTask ( component , preprocessor , _attributeParser , _state . GetRewriter ( component ) ) ;
179
+ var parser = new ComponentParseTask ( component , preprocessor , _attributeParser , rewriter ) ;
150
180
parser . ParseFailure += ( sender , e ) => _state . SetModuleState ( component , ParserState . Error , e . Cause as SyntaxErrorException ) ;
151
181
parser . ParseCompleted += ( sender , e ) =>
152
182
{
153
183
// possibly lock _state
154
- _state . SetModuleState ( component , ParserState . Parsed ) ;
155
184
_state . SetModuleAttributes ( component , e . Attributes ) ;
156
185
_state . AddParseTree ( component , e . ParseTree ) ;
157
186
_state . AddTokenStream ( component , e . Tokens ) ;
158
187
_state . SetModuleComments ( component , e . Comments ) ;
188
+ // This really needs to go last
189
+ _state . SetModuleState ( component , ParserState . Parsed ) ;
159
190
} ;
160
191
_state . SetModuleState ( component , ParserState . Parsing ) ;
161
192
parser . Start ( token ) ;
@@ -219,7 +250,7 @@ private void ResolveDeclarations(VBComponent component, IParseTree tree)
219
250
_state . ArgListsWithOneByRefParam = argListWithOneByRefParamListener . Contexts . Select ( context => new QualifiedContext ( qualifiedModuleName , context ) ) ;
220
251
_state . EmptyStringLiterals = emptyStringLiteralListener . Contexts . Select ( context => new QualifiedContext ( qualifiedModuleName , context ) ) ;
221
252
_state . ObsoleteLetContexts = obsoleteLetStatementListener . Contexts . Select ( context => new QualifiedContext ( qualifiedModuleName , context ) ) ;
222
- _state . ObsoleteCallContexts = obsoleteCallStatementListener . Contexts . Select ( context => new QualifiedContext ( qualifiedModuleName , context ) )
253
+ _state . ObsoleteCallContexts = obsoleteCallStatementListener . Contexts . Select ( context => new QualifiedContext ( qualifiedModuleName , context ) ) ;
223
254
}
224
255
225
256
private void ResolveReferences ( DeclarationFinder finder , VBComponent component , IParseTree tree )
@@ -255,7 +286,7 @@ private void ResolveReferences(DeclarationFinder finder, VBComponent component,
255
286
}
256
287
257
288
#region Listener classes
258
- private class ObsoleteCallStatementListener : VBABaseListener
289
+ private class ObsoleteCallStatementListener : VBAParserBaseListener
259
290
{
260
291
private readonly IList < VBAParser . ExplicitCallStmtContext > _contexts = new List < VBAParser . ExplicitCallStmtContext > ( ) ;
261
292
public IEnumerable < VBAParser . ExplicitCallStmtContext > Contexts { get { return _contexts ; } }
@@ -279,7 +310,7 @@ public override void ExitExplicitCallStmt(VBAParser.ExplicitCallStmtContext cont
279
310
}
280
311
}
281
312
282
- private class ObsoleteLetStatementListener : VBABaseListener
313
+ private class ObsoleteLetStatementListener : VBAParserBaseListener
283
314
{
284
315
private readonly IList < VBAParser . LetStmtContext > _contexts = new List < VBAParser . LetStmtContext > ( ) ;
285
316
public IEnumerable < VBAParser . LetStmtContext > Contexts { get { return _contexts ; } }
@@ -293,7 +324,7 @@ public override void ExitLetStmt(VBAParser.LetStmtContext context)
293
324
}
294
325
}
295
326
296
- private class EmptyStringLiteralListener : VBABaseListener
327
+ private class EmptyStringLiteralListener : VBAParserBaseListener
297
328
{
298
329
private readonly IList < VBAParser . LiteralContext > _contexts = new List < VBAParser . LiteralContext > ( ) ;
299
330
public IEnumerable < VBAParser . LiteralContext > Contexts { get { return _contexts ; } }
@@ -308,7 +339,7 @@ public override void ExitLiteral(VBAParser.LiteralContext context)
308
339
}
309
340
}
310
341
311
- private class ArgListWithOneByRefParamListener : VBABaseListener
342
+ private class ArgListWithOneByRefParamListener : VBAParserBaseListener
312
343
{
313
344
private readonly IList < VBAParser . ArgListContext > _contexts = new List < VBAParser . ArgListContext > ( ) ;
314
345
public IEnumerable < VBAParser . ArgListContext > Contexts { get { return _contexts ; } }
0 commit comments