Skip to content

Commit 41d91e1

Browse files
committed
Made the AttributeListener add module variable attributes to the correct scope, added MembersAllowingAttributes and fixed the failing tests caused by the necessary grammar change.
1 parent 055d911 commit 41d91e1

File tree

7 files changed

+74
-5
lines changed

7 files changed

+74
-5
lines changed

Rubberduck.CodeAnalysis/Inspections/Concrete/EmptyModuleInspection.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ public override bool VisitModuleDeclarations(VBAParser.ModuleDeclarationsContext
8787

8888
public override bool VisitModuleDeclarationsElement(VBAParser.ModuleDeclarationsElementContext context)
8989
{
90-
return context.variableStmt() == null
90+
return context.moduleVariableStmt() == null
9191
&& context.constStmt() == null
9292
&& context.enumerationStmt() == null
9393
&& context.udtDeclaration() == null

Rubberduck.CodeAnalysis/Inspections/Concrete/ModuleScopeDimKeywordInspection.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ public void ClearContexts()
4545

4646
public override void ExitVariableStmt([NotNull] VBAParser.VariableStmtContext context)
4747
{
48-
if (context.DIM() != null && context.Parent is VBAParser.ModuleDeclarationsElementContext)
48+
if (context.DIM() != null && context.TryGetAncestor<VBAParser.ModuleDeclarationsElementContext>(out _))
4949
{
5050
_contexts.Add(new QualifiedContext<ParserRuleContext>(CurrentModuleName, context));
5151
}

Rubberduck.Parsing/Grammar/VBAParser.g4

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,11 +97,16 @@ moduleDeclarationsElement :
9797
| eventStmt
9898
| constStmt
9999
| implementsStmt
100-
| variableStmt
100+
| moduleVariableStmt
101101
| moduleOption
102102
| udtDeclaration)
103103
;
104104

105+
moduleVariableStmt :
106+
variableStmt
107+
(endOfLine attributeStmt)*
108+
;
109+
105110
moduleBody :
106111
whiteSpace?
107112
(moduleBodyElement endOfStatement)*;

Rubberduck.Parsing/ParserRuleContextExtensions.cs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,15 @@ public static TContext GetAncestor<TContext>(this ParserRuleContext context)
131131
return GetAncestor_Recursive<TContext>(context);
132132
}
133133

134+
/// <summary>
135+
/// Tries to return the context's first ancestor of the generic Type.
136+
/// </summary>
137+
public static bool TryGetAncestor<TContext>(this ParserRuleContext context, out TContext ancestor)
138+
{
139+
ancestor = context.GetAncestor<TContext>();
140+
return ancestor != null;
141+
}
142+
134143
private static TContext GetAncestor_Recursive<TContext>(ParserRuleContext context)
135144
{
136145
if (context == null)

Rubberduck.Parsing/Rewriter/RewriterInfo/VariableRewriterInfoFinder.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ private static RewriterInfo GetRewriterInfo(VBAParser.VariableSubStmtContext var
2424
var itemIndex = items.ToList().IndexOf(variable);
2525
var count = items.Length;
2626

27-
if (context.Parent.Parent is VBAParser.ModuleDeclarationsElementContext element)
27+
if (context.TryGetAncestor<VBAParser.ModuleDeclarationsElementContext>(out var element))
2828
{
2929
return GetModuleVariableRemovalInfo(variable, element, count, itemIndex, items);
3030
}

Rubberduck.Parsing/Symbols/Identifier.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,12 @@ public static string GetName(VBAParser.VariableSubStmtContext context, out Inter
3535
return GetName(nameContext, out tokenInterval);
3636
}
3737

38+
public static string GetName(VBAParser.VariableSubStmtContext context)
39+
{
40+
var nameContext = context.identifier();
41+
return GetName(nameContext);
42+
}
43+
3844
public static string GetName(VBAParser.ConstSubStmtContext context, out Interval tokenInterval)
3945
{
4046
var nameContext = context.identifier();

Rubberduck.Parsing/VBA/Parsing/AttributeListener.cs

Lines changed: 50 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,18 +11,23 @@ public class AttributeListener : VBAParserBaseListener
1111
{
1212
private readonly Dictionary<(string scopeIdentifier, DeclarationType scopeType), Attributes> _attributes
1313
= new Dictionary<(string scopeIdentifier, DeclarationType scopeType), Attributes>();
14+
private readonly Dictionary<(string scopeIdentifier, DeclarationType scopeType), ParserRuleContext> _membersAllowingAttributes
15+
= new Dictionary<(string scopeIdentifier, DeclarationType scopeType), ParserRuleContext>();
1416

15-
private IAnnotatedContext _currentAnnotatedContext;
17+
private readonly (string scopeIdentifier, DeclarationType scopeType) _initialScope;
1618
private (string scopeIdentifier, DeclarationType scopeType) _currentScope;
19+
private IAnnotatedContext _currentAnnotatedContext;
1720
private Attributes _currentScopeAttributes;
1821

1922
public AttributeListener((string scopeIdentifier, DeclarationType scopeType) initialScope)
2023
{
24+
_initialScope = initialScope;
2125
_currentScope = initialScope;
2226
_currentScopeAttributes = new Attributes();
2327
}
2428

2529
public IDictionary<(string scopeIdentifier, DeclarationType scopeType), Attributes> Attributes => _attributes;
30+
public IDictionary<(string scopeIdentifier, DeclarationType scopeType), ParserRuleContext> MembersAllowingAttributes => _membersAllowingAttributes;
2631

2732
public override void ExitAnnotation(VBAParser.AnnotationContext context)
2833
{
@@ -38,11 +43,41 @@ public override void ExitModuleAttributes(VBAParser.ModuleAttributesContext cont
3843
}
3944
}
4045

46+
public override void EnterModuleVariableStmt(VBAParser.ModuleVariableStmtContext context)
47+
{
48+
_currentScopeAttributes = new Attributes();
49+
var annotatedContext = context.variableStmt().variableListStmt().variableSubStmt().Last();
50+
_currentScope = (Identifier.GetName(annotatedContext), DeclarationType.Variable);
51+
_currentAnnotatedContext = annotatedContext;
52+
_membersAllowingAttributes[_currentScope] = context;
53+
}
54+
55+
public override void ExitModuleVariableStmt(VBAParser.ModuleVariableStmtContext context)
56+
{
57+
if (_currentScopeAttributes.Any())
58+
{
59+
_attributes.Add(_currentScope, _currentScopeAttributes);
60+
var annotatedContext = context.variableStmt().variableListStmt().variableSubStmt().Last();
61+
annotatedContext.AddAttributes(_currentScopeAttributes);
62+
}
63+
64+
ResetScope();
65+
}
66+
67+
private void ResetScope()
68+
{
69+
_currentScope = _initialScope;
70+
_currentScopeAttributes = _attributes.TryGetValue(_currentScope, out var attributes)
71+
? attributes
72+
: new Attributes();
73+
}
74+
4175
public override void EnterSubStmt(VBAParser.SubStmtContext context)
4276
{
4377
_currentScopeAttributes = new Attributes();
4478
_currentScope = (Identifier.GetName(context.subroutineName()), DeclarationType.Procedure);
4579
_currentAnnotatedContext = context;
80+
_membersAllowingAttributes[_currentScope] = context;
4681
}
4782

4883
public override void ExitSubStmt(VBAParser.SubStmtContext context)
@@ -52,13 +87,16 @@ public override void ExitSubStmt(VBAParser.SubStmtContext context)
5287
_attributes.Add(_currentScope, _currentScopeAttributes);
5388
context.AddAttributes(_currentScopeAttributes);
5489
}
90+
91+
ResetScope();
5592
}
5693

5794
public override void EnterFunctionStmt(VBAParser.FunctionStmtContext context)
5895
{
5996
_currentScopeAttributes = new Attributes();
6097
_currentScope = (Identifier.GetName(context.functionName()), DeclarationType.Function);
6198
_currentAnnotatedContext = context;
99+
_membersAllowingAttributes[_currentScope] = context;
62100
}
63101

64102
public override void ExitFunctionStmt(VBAParser.FunctionStmtContext context)
@@ -68,13 +106,16 @@ public override void ExitFunctionStmt(VBAParser.FunctionStmtContext context)
68106
_attributes.Add(_currentScope, _currentScopeAttributes);
69107
context.AddAttributes(_currentScopeAttributes);
70108
}
109+
110+
ResetScope();
71111
}
72112

73113
public override void EnterPropertyGetStmt(VBAParser.PropertyGetStmtContext context)
74114
{
75115
_currentScopeAttributes = new Attributes();
76116
_currentScope = (Identifier.GetName(context.functionName()), DeclarationType.PropertyGet);
77117
_currentAnnotatedContext = context;
118+
_membersAllowingAttributes[_currentScope] = context;
78119
}
79120

80121
public override void ExitPropertyGetStmt(VBAParser.PropertyGetStmtContext context)
@@ -84,13 +125,16 @@ public override void ExitPropertyGetStmt(VBAParser.PropertyGetStmtContext contex
84125
_attributes.Add(_currentScope, _currentScopeAttributes);
85126
context.AddAttributes(_currentScopeAttributes);
86127
}
128+
129+
ResetScope();
87130
}
88131

89132
public override void EnterPropertyLetStmt(VBAParser.PropertyLetStmtContext context)
90133
{
91134
_currentScopeAttributes = new Attributes();
92135
_currentScope = (Identifier.GetName(context.subroutineName()), DeclarationType.PropertyLet);
93136
_currentAnnotatedContext = context;
137+
_membersAllowingAttributes[_currentScope] = context;
94138
}
95139

96140
public override void ExitPropertyLetStmt(VBAParser.PropertyLetStmtContext context)
@@ -100,13 +144,16 @@ public override void ExitPropertyLetStmt(VBAParser.PropertyLetStmtContext contex
100144
_attributes.Add(_currentScope, _currentScopeAttributes);
101145
context.AddAttributes(_currentScopeAttributes);
102146
}
147+
148+
ResetScope();
103149
}
104150

105151
public override void EnterPropertySetStmt(VBAParser.PropertySetStmtContext context)
106152
{
107153
_currentScopeAttributes = new Attributes();
108154
_currentScope = (Identifier.GetName(context.subroutineName()), DeclarationType.PropertySet);
109155
_currentAnnotatedContext = context;
156+
_membersAllowingAttributes[_currentScope] = context;
110157
}
111158

112159
public override void ExitPropertySetStmt(VBAParser.PropertySetStmtContext context)
@@ -116,6 +163,8 @@ public override void ExitPropertySetStmt(VBAParser.PropertySetStmtContext contex
116163
_attributes.Add(_currentScope, _currentScopeAttributes);
117164
context.AddAttributes(_currentScopeAttributes);
118165
}
166+
167+
ResetScope();
119168
}
120169

121170
public override void ExitAttributeStmt(VBAParser.AttributeStmtContext context)

0 commit comments

Comments
 (0)