@@ -11,18 +11,23 @@ public class AttributeListener : VBAParserBaseListener
11
11
{
12
12
private readonly Dictionary < ( string scopeIdentifier , DeclarationType scopeType ) , Attributes > _attributes
13
13
= 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 > ( ) ;
14
16
15
- private IAnnotatedContext _currentAnnotatedContext ;
17
+ private readonly ( string scopeIdentifier , DeclarationType scopeType ) _initialScope ;
16
18
private ( string scopeIdentifier , DeclarationType scopeType ) _currentScope ;
19
+ private IAnnotatedContext _currentAnnotatedContext ;
17
20
private Attributes _currentScopeAttributes ;
18
21
19
22
public AttributeListener ( ( string scopeIdentifier , DeclarationType scopeType ) initialScope )
20
23
{
24
+ _initialScope = initialScope ;
21
25
_currentScope = initialScope ;
22
26
_currentScopeAttributes = new Attributes ( ) ;
23
27
}
24
28
25
29
public IDictionary < ( string scopeIdentifier , DeclarationType scopeType ) , Attributes > Attributes => _attributes ;
30
+ public IDictionary < ( string scopeIdentifier , DeclarationType scopeType ) , ParserRuleContext > MembersAllowingAttributes => _membersAllowingAttributes ;
26
31
27
32
public override void ExitAnnotation ( VBAParser . AnnotationContext context )
28
33
{
@@ -38,11 +43,41 @@ public override void ExitModuleAttributes(VBAParser.ModuleAttributesContext cont
38
43
}
39
44
}
40
45
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
+
41
75
public override void EnterSubStmt ( VBAParser . SubStmtContext context )
42
76
{
43
77
_currentScopeAttributes = new Attributes ( ) ;
44
78
_currentScope = ( Identifier . GetName ( context . subroutineName ( ) ) , DeclarationType . Procedure ) ;
45
79
_currentAnnotatedContext = context ;
80
+ _membersAllowingAttributes [ _currentScope ] = context ;
46
81
}
47
82
48
83
public override void ExitSubStmt ( VBAParser . SubStmtContext context )
@@ -52,13 +87,16 @@ public override void ExitSubStmt(VBAParser.SubStmtContext context)
52
87
_attributes . Add ( _currentScope , _currentScopeAttributes ) ;
53
88
context . AddAttributes ( _currentScopeAttributes ) ;
54
89
}
90
+
91
+ ResetScope ( ) ;
55
92
}
56
93
57
94
public override void EnterFunctionStmt ( VBAParser . FunctionStmtContext context )
58
95
{
59
96
_currentScopeAttributes = new Attributes ( ) ;
60
97
_currentScope = ( Identifier . GetName ( context . functionName ( ) ) , DeclarationType . Function ) ;
61
98
_currentAnnotatedContext = context ;
99
+ _membersAllowingAttributes [ _currentScope ] = context ;
62
100
}
63
101
64
102
public override void ExitFunctionStmt ( VBAParser . FunctionStmtContext context )
@@ -68,13 +106,16 @@ public override void ExitFunctionStmt(VBAParser.FunctionStmtContext context)
68
106
_attributes . Add ( _currentScope , _currentScopeAttributes ) ;
69
107
context . AddAttributes ( _currentScopeAttributes ) ;
70
108
}
109
+
110
+ ResetScope ( ) ;
71
111
}
72
112
73
113
public override void EnterPropertyGetStmt ( VBAParser . PropertyGetStmtContext context )
74
114
{
75
115
_currentScopeAttributes = new Attributes ( ) ;
76
116
_currentScope = ( Identifier . GetName ( context . functionName ( ) ) , DeclarationType . PropertyGet ) ;
77
117
_currentAnnotatedContext = context ;
118
+ _membersAllowingAttributes [ _currentScope ] = context ;
78
119
}
79
120
80
121
public override void ExitPropertyGetStmt ( VBAParser . PropertyGetStmtContext context )
@@ -84,13 +125,16 @@ public override void ExitPropertyGetStmt(VBAParser.PropertyGetStmtContext contex
84
125
_attributes . Add ( _currentScope , _currentScopeAttributes ) ;
85
126
context . AddAttributes ( _currentScopeAttributes ) ;
86
127
}
128
+
129
+ ResetScope ( ) ;
87
130
}
88
131
89
132
public override void EnterPropertyLetStmt ( VBAParser . PropertyLetStmtContext context )
90
133
{
91
134
_currentScopeAttributes = new Attributes ( ) ;
92
135
_currentScope = ( Identifier . GetName ( context . subroutineName ( ) ) , DeclarationType . PropertyLet ) ;
93
136
_currentAnnotatedContext = context ;
137
+ _membersAllowingAttributes [ _currentScope ] = context ;
94
138
}
95
139
96
140
public override void ExitPropertyLetStmt ( VBAParser . PropertyLetStmtContext context )
@@ -100,13 +144,16 @@ public override void ExitPropertyLetStmt(VBAParser.PropertyLetStmtContext contex
100
144
_attributes . Add ( _currentScope , _currentScopeAttributes ) ;
101
145
context . AddAttributes ( _currentScopeAttributes ) ;
102
146
}
147
+
148
+ ResetScope ( ) ;
103
149
}
104
150
105
151
public override void EnterPropertySetStmt ( VBAParser . PropertySetStmtContext context )
106
152
{
107
153
_currentScopeAttributes = new Attributes ( ) ;
108
154
_currentScope = ( Identifier . GetName ( context . subroutineName ( ) ) , DeclarationType . PropertySet ) ;
109
155
_currentAnnotatedContext = context ;
156
+ _membersAllowingAttributes [ _currentScope ] = context ;
110
157
}
111
158
112
159
public override void ExitPropertySetStmt ( VBAParser . PropertySetStmtContext context )
@@ -116,6 +163,8 @@ public override void ExitPropertySetStmt(VBAParser.PropertySetStmtContext contex
116
163
_attributes . Add ( _currentScope , _currentScopeAttributes ) ;
117
164
context . AddAttributes ( _currentScopeAttributes ) ;
118
165
}
166
+
167
+ ResetScope ( ) ;
119
168
}
120
169
121
170
public override void ExitAttributeStmt ( VBAParser . AttributeStmtContext context )
0 commit comments