@@ -15,21 +15,21 @@ public interface IParseTreeValueVisitor
15
15
IParseTreeVisitorResults VisitChildren ( QualifiedModuleName module , IRuleNode node ) ;
16
16
}
17
17
18
- public class ParseTreeValueVisitor : IParseTreeValueVisitor
18
+ public class EnumMember
19
19
{
20
- private class EnumMember
20
+ public EnumMember ( VBAParser . EnumerationStmt_ConstantContext constContext , long initValue )
21
21
{
22
- public EnumMember ( VBAParser . EnumerationStmt_ConstantContext constContext , long initValue )
23
- {
24
- ConstantContext = constContext ;
25
- Value = initValue ;
26
- HasAssignment = constContext . children . Any ( ch => ch . Equals ( constContext . GetToken ( VBAParser . EQ , 0 ) ) ) ;
27
- }
28
- public VBAParser . EnumerationStmt_ConstantContext ConstantContext { get ; }
29
- public long Value { set ; get ; }
30
- public bool HasAssignment { get ; }
22
+ ConstantContext = constContext ;
23
+ Value = initValue ;
24
+ HasAssignment = constContext . children . Any ( ch => ch . Equals ( constContext . GetToken ( VBAParser . EQ , 0 ) ) ) ;
31
25
}
26
+ public VBAParser . EnumerationStmt_ConstantContext ConstantContext { get ; }
27
+ public long Value { set ; get ; }
28
+ public bool HasAssignment { get ; }
29
+ }
32
30
31
+ public class ParseTreeValueVisitor : IParseTreeValueVisitor
32
+ {
33
33
private readonly IParseTreeValueFactory _valueFactory ;
34
34
private readonly Func < Declaration , ( bool , string , string ) > _valueDeclarationEvaluator ;
35
35
private readonly IReadOnlyList < QualifiedContext < VBAParser . EnumerationStmtContext > > _enumStmtContexts ;
@@ -57,7 +57,7 @@ public IParseTreeVisitorResults VisitChildren(QualifiedModuleName module, IRuleN
57
57
58
58
//The known results get passed along instead of aggregating from the bottom since other contexts can get already visited when resolving the value of other contexts.
59
59
//Passing the results along avoids performing the resolution multiple times.
60
- private IParseTreeVisitorResults VisitChildren ( QualifiedModuleName module , IRuleNode node , IParseTreeVisitorResults knownResults )
60
+ private IMutableParseTreeVisitorResults VisitChildren ( QualifiedModuleName module , IRuleNode node , IMutableParseTreeVisitorResults knownResults )
61
61
{
62
62
if ( ! ( node is ParserRuleContext context ) )
63
63
{
@@ -73,7 +73,7 @@ private IParseTreeVisitorResults VisitChildren(QualifiedModuleName module, IRule
73
73
return valueResults ;
74
74
}
75
75
76
- private IParseTreeVisitorResults Visit ( QualifiedModuleName module , IParseTree tree , IParseTreeVisitorResults knownResults )
76
+ private IMutableParseTreeVisitorResults Visit ( QualifiedModuleName module , IParseTree tree , IMutableParseTreeVisitorResults knownResults )
77
77
{
78
78
var valueResults = knownResults ;
79
79
if ( tree is ParserRuleContext context && ! ( context is VBAParser . WhiteSpaceContext ) )
@@ -84,7 +84,7 @@ private IParseTreeVisitorResults Visit(QualifiedModuleName module, IParseTree tr
84
84
return valueResults ;
85
85
}
86
86
87
- private IParseTreeVisitorResults Visit ( QualifiedModuleName module , ParserRuleContext parserRuleContext , IParseTreeVisitorResults knownResults )
87
+ private IMutableParseTreeVisitorResults Visit ( QualifiedModuleName module , ParserRuleContext parserRuleContext , IMutableParseTreeVisitorResults knownResults )
88
88
{
89
89
switch ( parserRuleContext )
90
90
{
@@ -117,7 +117,7 @@ private IParseTreeVisitorResults Visit(QualifiedModuleName module, ParserRuleCon
117
117
}
118
118
}
119
119
120
- private IParseTreeVisitorResults Visit ( QualifiedModuleName module , VBAParser . LExprContext context , IParseTreeVisitorResults knownResults )
120
+ private IMutableParseTreeVisitorResults Visit ( QualifiedModuleName module , VBAParser . LExprContext context , IMutableParseTreeVisitorResults knownResults )
121
121
{
122
122
if ( knownResults . Contains ( context ) )
123
123
{
@@ -149,7 +149,7 @@ private IParseTreeVisitorResults Visit(QualifiedModuleName module, VBAParser.LEx
149
149
return valueResults ;
150
150
}
151
151
152
- private IParseTreeVisitorResults Visit ( VBAParser . LiteralExprContext context , IParseTreeVisitorResults knownResults )
152
+ private IMutableParseTreeVisitorResults Visit ( VBAParser . LiteralExprContext context , IMutableParseTreeVisitorResults knownResults )
153
153
{
154
154
if ( knownResults . Contains ( context ) )
155
155
{
@@ -163,7 +163,7 @@ private IParseTreeVisitorResults Visit(VBAParser.LiteralExprContext context, IPa
163
163
return valueResults ;
164
164
}
165
165
166
- private IParseTreeVisitorResults VisitBinaryOpEvaluationContext ( QualifiedModuleName module , ParserRuleContext context , IParseTreeVisitorResults knownResults )
166
+ private IMutableParseTreeVisitorResults VisitBinaryOpEvaluationContext ( QualifiedModuleName module , ParserRuleContext context , IMutableParseTreeVisitorResults knownResults )
167
167
{
168
168
var valueResults = VisitChildren ( module , context , knownResults ) ;
169
169
@@ -191,7 +191,7 @@ private IParseTreeVisitorResults VisitBinaryOpEvaluationContext(QualifiedModuleN
191
191
return valueResults ;
192
192
}
193
193
194
- private IParseTreeVisitorResults VisitUnaryOpEvaluationContext ( QualifiedModuleName module , ParserRuleContext context , IParseTreeVisitorResults knownResults )
194
+ private IMutableParseTreeVisitorResults VisitUnaryOpEvaluationContext ( QualifiedModuleName module , ParserRuleContext context , IMutableParseTreeVisitorResults knownResults )
195
195
{
196
196
var valueResults = VisitChildren ( module , context , knownResults ) ;
197
197
@@ -208,7 +208,7 @@ private IParseTreeVisitorResults VisitUnaryOpEvaluationContext(QualifiedModuleNa
208
208
return valueResults ;
209
209
}
210
210
211
- private static ( IParseTreeValue LHS , IParseTreeValue RHS , string Symbol ) RetrieveOpEvaluationElements ( ParserRuleContext context , IParseTreeVisitorResults knownResults )
211
+ private static ( IParseTreeValue LHS , IParseTreeValue RHS , string Symbol ) RetrieveOpEvaluationElements ( ParserRuleContext context , IMutableParseTreeVisitorResults knownResults )
212
212
{
213
213
( IParseTreeValue LHS , IParseTreeValue RHS , string Symbol ) operandElements = ( null , null , string . Empty ) ;
214
214
foreach ( var child in NonWhitespaceChildren ( context ) )
@@ -233,7 +233,7 @@ private static (IParseTreeValue LHS, IParseTreeValue RHS, string Symbol) Retriev
233
233
return operandElements ;
234
234
}
235
235
236
- private IParseTreeVisitorResults VisitUnaryResultContext ( QualifiedModuleName module , ParserRuleContext parserRuleContext , IParseTreeVisitorResults knownResults )
236
+ private IMutableParseTreeVisitorResults VisitUnaryResultContext ( QualifiedModuleName module , ParserRuleContext parserRuleContext , IMutableParseTreeVisitorResults knownResults )
237
237
{
238
238
var valueResults = VisitChildren ( module , parserRuleContext , knownResults ) ;
239
239
@@ -248,7 +248,7 @@ private IParseTreeVisitorResults VisitUnaryResultContext(QualifiedModuleName mod
248
248
return valueResults ;
249
249
}
250
250
251
- private IParseTreeVisitorResults VisitChildren ( QualifiedModuleName module , ParserRuleContext context , IParseTreeVisitorResults knownResults )
251
+ private IMutableParseTreeVisitorResults VisitChildren ( QualifiedModuleName module , ParserRuleContext context , IMutableParseTreeVisitorResults knownResults )
252
252
{
253
253
if ( knownResults . Contains ( context ) )
254
254
{
@@ -270,7 +270,7 @@ private static IEnumerable<ParserRuleContext> ParserRuleContextChildren(ParserRu
270
270
private static IEnumerable < IParseTree > NonWhitespaceChildren ( ParserRuleContext ptParent )
271
271
=> ptParent . children . Where ( ch => ! ( ch is VBAParser . WhiteSpaceContext ) ) ;
272
272
273
- private bool TryGetLExprValue ( QualifiedModuleName module , VBAParser . LExprContext lExprContext , ref IParseTreeVisitorResults knownResults , out string expressionValue , out string declaredTypeName )
273
+ private bool TryGetLExprValue ( QualifiedModuleName module , VBAParser . LExprContext lExprContext , ref IMutableParseTreeVisitorResults knownResults , out string expressionValue , out string declaredTypeName )
274
274
{
275
275
expressionValue = string . Empty ;
276
276
declaredTypeName = string . Empty ;
@@ -315,7 +315,7 @@ private bool TryGetLExprValue(QualifiedModuleName module, VBAParser.LExprContext
315
315
return ( true , valuedDeclaration . Expression , typeName ) ;
316
316
}
317
317
318
- private ( string declarationTypeName , string expressionValue , IParseTreeVisitorResults resultValues ) GetContextValue ( QualifiedModuleName module , ParserRuleContext context , IParseTreeVisitorResults knownResults )
318
+ private ( string declarationTypeName , string expressionValue , IMutableParseTreeVisitorResults resultValues ) GetContextValue ( QualifiedModuleName module , ParserRuleContext context , IMutableParseTreeVisitorResults knownResults )
319
319
{
320
320
if ( ! TryGetIdentifierReferenceForContext ( module , context , out var rangeClauseIdentifierReference ) )
321
321
{
@@ -379,7 +379,7 @@ private bool TryGetIdentifierReferenceForContext(QualifiedModuleName module, Par
379
379
return success ;
380
380
}
381
381
382
- private ( string valueText , IParseTreeVisitorResults valueResults ) GetConstantContextValueToken ( QualifiedModuleName module , ParserRuleContext context , IParseTreeVisitorResults knownResults )
382
+ private ( string valueText , IMutableParseTreeVisitorResults valueResults ) GetConstantContextValueToken ( QualifiedModuleName module , ParserRuleContext context , IMutableParseTreeVisitorResults knownResults )
383
383
{
384
384
if ( context is null )
385
385
{
@@ -443,24 +443,21 @@ private static bool IsBinaryOpEvaluationContext<T>(T context)
443
443
return false ;
444
444
}
445
445
446
- private List < EnumMember > _enumMembers ;
447
- private ( IReadOnlyList < EnumMember > enumMembers , IParseTreeVisitorResults resultValues ) EnumMembers ( IParseTreeVisitorResults knownResults )
446
+ private ( IReadOnlyList < EnumMember > enumMembers , IMutableParseTreeVisitorResults resultValues ) EnumMembers ( IMutableParseTreeVisitorResults knownResults )
448
447
{
449
- if ( _enumMembers != null )
448
+ if ( knownResults . EnumMembers . Count > 0 )
450
449
{
451
- return ( _enumMembers , knownResults ) ;
450
+ return ( knownResults . EnumMembers , knownResults ) ;
452
451
}
453
452
454
453
var resultValues = LoadEnumMemberValues ( _enumStmtContexts , knownResults ) ;
455
- return ( _enumMembers , resultValues ) ;
454
+ return ( resultValues . EnumMembers , resultValues ) ;
456
455
}
457
456
458
- //This procedure loads the list _enumMembers.
459
- //The incrementally added values are used within the call to Visit.
460
- private IParseTreeVisitorResults LoadEnumMemberValues ( IReadOnlyList < QualifiedContext < VBAParser . EnumerationStmtContext > > enumStmtContexts , IParseTreeVisitorResults knownResults )
457
+ //The enum members incrementally to the parse tree visitor result are used within the call to Visit.
458
+ private IMutableParseTreeVisitorResults LoadEnumMemberValues ( IReadOnlyList < QualifiedContext < VBAParser . EnumerationStmtContext > > enumStmtContexts , IMutableParseTreeVisitorResults knownResults )
461
459
{
462
460
var valueResults = knownResults ;
463
- _enumMembers = new List < EnumMember > ( ) ;
464
461
foreach ( var qualifiedEnumStmt in enumStmtContexts )
465
462
{
466
463
var module = qualifiedEnumStmt . ModuleName ;
@@ -484,7 +481,7 @@ private IParseTreeVisitorResults LoadEnumMemberValues(IReadOnlyList<QualifiedCon
484
481
enumAssignedValue = enumMember . Value ;
485
482
}
486
483
}
487
- _enumMembers . Add ( enumMember ) ;
484
+ valueResults . Add ( enumMember ) ;
488
485
}
489
486
}
490
487
0 commit comments