@@ -14,88 +14,64 @@ public class AttributeListener : VBAParserBaseListener
14
14
private readonly Dictionary < ( string scopeIdentifier , DeclarationType scopeType ) , ParserRuleContext > _membersAllowingAttributes
15
15
= new Dictionary < ( string scopeIdentifier , DeclarationType scopeType ) , ParserRuleContext > ( ) ;
16
16
17
- private readonly ( string scopeIdentifier , DeclarationType scopeType ) _initialScope ;
17
+ private readonly ( string scopeIdentifier , DeclarationType scopeType ) _moduleScope ;
18
18
private ( string scopeIdentifier , DeclarationType scopeType ) _currentScope ;
19
- private IAnnotatedContext _currentAnnotatedContext ;
20
19
private Attributes _currentScopeAttributes ;
21
20
22
- public AttributeListener ( ( string scopeIdentifier , DeclarationType scopeType ) initialScope )
21
+ public AttributeListener ( ( string scopeIdentifier , DeclarationType scopeType ) moduleScope )
23
22
{
24
- _initialScope = initialScope ;
25
- _currentScope = initialScope ;
26
- _currentScopeAttributes = new Attributes ( ) ;
23
+ _moduleScope = moduleScope ;
24
+ _currentScope = moduleScope ;
27
25
}
28
26
29
27
public IDictionary < ( string scopeIdentifier , DeclarationType scopeType ) , Attributes > Attributes => _attributes ;
30
28
public IDictionary < ( string scopeIdentifier , DeclarationType scopeType ) , ParserRuleContext > MembersAllowingAttributes => _membersAllowingAttributes ;
31
29
32
- public override void ExitAnnotation ( VBAParser . AnnotationContext context )
30
+ public override void EnterStartRule ( VBAParser . StartRuleContext context )
33
31
{
34
- _currentAnnotatedContext ? . Annotate ( context ) ;
35
- context . AnnotatedContext = _currentAnnotatedContext as ParserRuleContext ;
32
+ _membersAllowingAttributes [ _moduleScope ] = context ;
36
33
}
37
34
38
- public override void ExitModuleAttributes ( VBAParser . ModuleAttributesContext context )
35
+ public override void EnterModuleVariableStmt ( VBAParser . ModuleVariableStmtContext context )
39
36
{
40
- if ( _currentScopeAttributes . Any ( ) && ! _attributes . ContainsKey ( _currentScope ) )
37
+ var variableDeclarationStatemenList = context . variableStmt ( ) . variableListStmt ( ) . variableSubStmt ( ) ;
38
+ foreach ( var variableContext in variableDeclarationStatemenList )
41
39
{
42
- _attributes . Add ( _currentScope , _currentScopeAttributes ) ;
40
+ var variableName = Identifier . GetName ( variableContext ) ;
41
+ _membersAllowingAttributes [ ( variableName , DeclarationType . Variable ) ] = context ;
43
42
}
44
43
}
45
44
46
- public override void EnterModuleVariableStmt ( VBAParser . ModuleVariableStmtContext context )
45
+ public override void EnterSubStmt ( VBAParser . SubStmtContext context )
47
46
{
48
47
_currentScopeAttributes = new Attributes ( ) ;
49
- var annotatedContext = context . variableStmt ( ) . variableListStmt ( ) . variableSubStmt ( ) . Last ( ) ;
50
- _currentScope = ( Identifier . GetName ( annotatedContext ) , DeclarationType . Variable ) ;
51
- _currentAnnotatedContext = annotatedContext ;
48
+ _currentScope = ( Identifier . GetName ( context . subroutineName ( ) ) , DeclarationType . Procedure ) ;
52
49
_membersAllowingAttributes [ _currentScope ] = context ;
53
50
}
54
51
55
- public override void ExitModuleVariableStmt ( VBAParser . ModuleVariableStmtContext context )
52
+ public override void ExitSubStmt ( VBAParser . SubStmtContext context )
56
53
{
57
- if ( _currentScopeAttributes . Any ( ) )
54
+ if ( _currentScopeAttributes . Any ( ) )
58
55
{
59
56
_attributes . Add ( _currentScope , _currentScopeAttributes ) ;
60
- var annotatedContext = context . variableStmt ( ) . variableListStmt ( ) . variableSubStmt ( ) . Last ( ) ;
61
- annotatedContext . AddAttributes ( _currentScopeAttributes ) ;
57
+ context . AddAttributes ( _currentScopeAttributes ) ;
62
58
}
63
59
64
60
ResetScope ( ) ;
65
61
}
66
62
67
63
private void ResetScope ( )
68
64
{
69
- _currentScope = _initialScope ;
65
+ _currentScope = _moduleScope ;
70
66
_currentScopeAttributes = _attributes . TryGetValue ( _currentScope , out var attributes )
71
67
? attributes
72
68
: new Attributes ( ) ;
73
69
}
74
70
75
- public override void EnterSubStmt ( VBAParser . SubStmtContext context )
76
- {
77
- _currentScopeAttributes = new Attributes ( ) ;
78
- _currentScope = ( Identifier . GetName ( context . subroutineName ( ) ) , DeclarationType . Procedure ) ;
79
- _currentAnnotatedContext = context ;
80
- _membersAllowingAttributes [ _currentScope ] = context ;
81
- }
82
-
83
- public override void ExitSubStmt ( VBAParser . SubStmtContext context )
84
- {
85
- if ( _currentScopeAttributes . Any ( ) )
86
- {
87
- _attributes . Add ( _currentScope , _currentScopeAttributes ) ;
88
- context . AddAttributes ( _currentScopeAttributes ) ;
89
- }
90
-
91
- ResetScope ( ) ;
92
- }
93
-
94
71
public override void EnterFunctionStmt ( VBAParser . FunctionStmtContext context )
95
72
{
96
73
_currentScopeAttributes = new Attributes ( ) ;
97
74
_currentScope = ( Identifier . GetName ( context . functionName ( ) ) , DeclarationType . Function ) ;
98
- _currentAnnotatedContext = context ;
99
75
_membersAllowingAttributes [ _currentScope ] = context ;
100
76
}
101
77
@@ -114,7 +90,6 @@ public override void EnterPropertyGetStmt(VBAParser.PropertyGetStmtContext conte
114
90
{
115
91
_currentScopeAttributes = new Attributes ( ) ;
116
92
_currentScope = ( Identifier . GetName ( context . functionName ( ) ) , DeclarationType . PropertyGet ) ;
117
- _currentAnnotatedContext = context ;
118
93
_membersAllowingAttributes [ _currentScope ] = context ;
119
94
}
120
95
@@ -133,7 +108,6 @@ public override void EnterPropertyLetStmt(VBAParser.PropertyLetStmtContext conte
133
108
{
134
109
_currentScopeAttributes = new Attributes ( ) ;
135
110
_currentScope = ( Identifier . GetName ( context . subroutineName ( ) ) , DeclarationType . PropertyLet ) ;
136
- _currentAnnotatedContext = context ;
137
111
_membersAllowingAttributes [ _currentScope ] = context ;
138
112
}
139
113
@@ -152,7 +126,6 @@ public override void EnterPropertySetStmt(VBAParser.PropertySetStmtContext conte
152
126
{
153
127
_currentScopeAttributes = new Attributes ( ) ;
154
128
_currentScope = ( Identifier . GetName ( context . subroutineName ( ) ) , DeclarationType . PropertySet ) ;
155
- _currentAnnotatedContext = context ;
156
129
_membersAllowingAttributes [ _currentScope ] = context ;
157
130
}
158
131
@@ -169,19 +142,54 @@ public override void ExitPropertySetStmt(VBAParser.PropertySetStmtContext contex
169
142
170
143
public override void ExitAttributeStmt ( VBAParser . AttributeStmtContext context )
171
144
{
172
- var values = context . attributeValue ( ) . Select ( e => e . GetText ( ) . Replace ( "\" " , string . Empty ) ) . ToList ( ) ;
145
+ var attributeName = context . attributeName ( ) . GetText ( ) ;
146
+ var attributeNameParts = attributeName . Split ( '.' ) ;
147
+
148
+ //Module attribute
149
+ if ( attributeNameParts . Length == 1 )
150
+ {
151
+ AddOrUpdateAttribute ( _moduleScope , attributeName , context ) ;
152
+ return ;
153
+ }
154
+
155
+ var scopeName = attributeNameParts [ 0 ] ;
156
+
157
+ if ( scopeName . Equals ( _currentScope . scopeIdentifier , StringComparison . OrdinalIgnoreCase ) )
158
+ {
159
+ AddOrUpdateAttribute ( _currentScopeAttributes , attributeName , context ) ;
160
+ return ;
161
+ }
162
+
163
+ //Member variable attributes
164
+ var moduleVariableScope = ( scopeName , DeclarationType . Variable ) ;
165
+ if ( _membersAllowingAttributes . TryGetValue ( moduleVariableScope , out _ ) )
166
+ {
167
+ AddOrUpdateAttribute ( moduleVariableScope , attributeName , context ) ;
168
+ }
169
+ }
170
+
171
+ private void AddOrUpdateAttribute ( ( string scopeName , DeclarationType Variable ) moduleVariableScope ,
172
+ string attributeName , VBAParser . AttributeStmtContext context )
173
+ {
174
+ if ( ! _attributes . TryGetValue ( moduleVariableScope , out var attributes ) )
175
+ {
176
+ attributes = new Attributes ( ) ;
177
+ _attributes . Add ( moduleVariableScope , attributes ) ;
178
+ }
173
179
174
- var attribute = _currentScopeAttributes
175
- . SingleOrDefault ( a => a . Name . Equals ( context . attributeName ( ) . GetText ( ) , StringComparison . OrdinalIgnoreCase ) ) ;
180
+ AddOrUpdateAttribute ( attributes , attributeName , context ) ;
181
+ }
182
+
183
+ private static void AddOrUpdateAttribute ( Attributes attributes , string attributeName , VBAParser . AttributeStmtContext context )
184
+ {
185
+ var attribute = attributes . SingleOrDefault ( a => a . Name . Equals ( attributeName , StringComparison . OrdinalIgnoreCase ) ) ;
176
186
if ( attribute != null )
177
187
{
178
- foreach ( var value in values . Where ( v => ! attribute . HasValue ( v ) ) )
179
- {
180
- attribute . AddValue ( value ) ;
181
- }
188
+ attribute . AddContext ( context ) ;
189
+ return ;
182
190
}
183
191
184
- _currentScopeAttributes . Add ( new AttributeNode ( context ) ) ;
192
+ attributes . Add ( new AttributeNode ( context ) ) ;
185
193
}
186
194
}
187
195
}
0 commit comments