Skip to content

Commit 69bf21a

Browse files
committed
Some refactoring of the AttributeListener
1 parent fa62a55 commit 69bf21a

File tree

1 file changed

+36
-46
lines changed

1 file changed

+36
-46
lines changed

Rubberduck.Parsing/VBA/Parsing/AttributeListener.cs

Lines changed: 36 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -44,98 +44,88 @@ public override void EnterModuleVariableStmt(VBAParser.ModuleVariableStmtContext
4444

4545
public override void EnterSubStmt(VBAParser.SubStmtContext context)
4646
{
47+
var attributeScope = (Identifier.GetName(context.subroutineName()), DeclarationType.Procedure);
48+
PushNewScope(attributeScope);
49+
_membersAllowingAttributes[attributeScope] = context;
50+
}
51+
52+
private void PushNewScope((string scopeIdentifier, DeclarationType scopeType) attributeScope)
53+
{
54+
_currentScope = attributeScope;
4755
_currentScopeAttributes = new Attributes();
48-
_currentScope = (Identifier.GetName(context.subroutineName()), DeclarationType.Procedure);
49-
_membersAllowingAttributes[_currentScope] = context;
5056
}
5157

5258
public override void ExitSubStmt(VBAParser.SubStmtContext context)
5359
{
54-
if(_currentScopeAttributes.Any())
60+
SaveCurrentScopeAttributes(context);
61+
PopScope();
62+
}
63+
64+
private void SaveCurrentScopeAttributes(IAnnotatedContext context)
65+
{
66+
if (_currentScopeAttributes.Any())
5567
{
5668
_attributes.Add(_currentScope, _currentScopeAttributes);
5769
context.AddAttributes(_currentScopeAttributes);
5870
}
59-
60-
ResetScope();
6171
}
6272

63-
private void ResetScope()
73+
private void PopScope()
6474
{
6575
_currentScope = _moduleScope;
6676
_currentScopeAttributes = null;
6777
}
6878

6979
public override void EnterFunctionStmt(VBAParser.FunctionStmtContext context)
7080
{
71-
_currentScopeAttributes = new Attributes();
72-
_currentScope = (Identifier.GetName(context.functionName()), DeclarationType.Function);
73-
_membersAllowingAttributes[_currentScope] = context;
81+
var attributeScope = (Identifier.GetName(context.functionName()), DeclarationType.Function);
82+
PushNewScope(attributeScope);
83+
_membersAllowingAttributes[attributeScope] = context;
7484
}
7585

7686
public override void ExitFunctionStmt(VBAParser.FunctionStmtContext context)
7787
{
78-
if(_currentScopeAttributes.Any())
79-
{
80-
_attributes.Add(_currentScope, _currentScopeAttributes);
81-
context.AddAttributes(_currentScopeAttributes);
82-
}
83-
84-
ResetScope();
88+
SaveCurrentScopeAttributes(context);
89+
PopScope();
8590
}
8691

8792
public override void EnterPropertyGetStmt(VBAParser.PropertyGetStmtContext context)
8893
{
89-
_currentScopeAttributes = new Attributes();
90-
_currentScope = (Identifier.GetName(context.functionName()), DeclarationType.PropertyGet);
91-
_membersAllowingAttributes[_currentScope] = context;
94+
var attributeScope = (Identifier.GetName(context.functionName()), DeclarationType.PropertyGet);
95+
PushNewScope(attributeScope);
96+
_membersAllowingAttributes[attributeScope] = context;
9297
}
9398

9499
public override void ExitPropertyGetStmt(VBAParser.PropertyGetStmtContext context)
95100
{
96-
if(_currentScopeAttributes.Any())
97-
{
98-
_attributes.Add(_currentScope, _currentScopeAttributes);
99-
context.AddAttributes(_currentScopeAttributes);
100-
}
101-
102-
ResetScope();
101+
SaveCurrentScopeAttributes(context);
102+
PopScope();
103103
}
104104

105105
public override void EnterPropertyLetStmt(VBAParser.PropertyLetStmtContext context)
106106
{
107-
_currentScopeAttributes = new Attributes();
108-
_currentScope = (Identifier.GetName(context.subroutineName()), DeclarationType.PropertyLet);
109-
_membersAllowingAttributes[_currentScope] = context;
107+
var attributeScope = (Identifier.GetName(context.subroutineName()), DeclarationType.PropertyLet);
108+
PushNewScope(attributeScope);
109+
_membersAllowingAttributes[attributeScope] = context;
110110
}
111111

112112
public override void ExitPropertyLetStmt(VBAParser.PropertyLetStmtContext context)
113113
{
114-
if(_currentScopeAttributes.Any())
115-
{
116-
_attributes.Add(_currentScope, _currentScopeAttributes);
117-
context.AddAttributes(_currentScopeAttributes);
118-
}
119-
120-
ResetScope();
114+
SaveCurrentScopeAttributes(context);
115+
PopScope();
121116
}
122117

123118
public override void EnterPropertySetStmt(VBAParser.PropertySetStmtContext context)
124119
{
125-
_currentScopeAttributes = new Attributes();
126-
_currentScope = (Identifier.GetName(context.subroutineName()), DeclarationType.PropertySet);
127-
_membersAllowingAttributes[_currentScope] = context;
120+
var attributeScope = (Identifier.GetName(context.subroutineName()), DeclarationType.PropertySet);
121+
PushNewScope(attributeScope);
122+
_membersAllowingAttributes[attributeScope] = context;
128123
}
129124

130125
public override void ExitPropertySetStmt(VBAParser.PropertySetStmtContext context)
131126
{
132-
if(_currentScopeAttributes.Any())
133-
{
134-
_attributes.Add(_currentScope, _currentScopeAttributes);
135-
context.AddAttributes(_currentScopeAttributes);
136-
}
137-
138-
ResetScope();
127+
SaveCurrentScopeAttributes(context);
128+
PopScope();
139129
}
140130

141131
public override void ExitAttributeStmt(VBAParser.AttributeStmtContext context)

0 commit comments

Comments
 (0)