-
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathPapyrusVarCleanerSF1.g
More file actions
355 lines (324 loc) · 12.8 KB
/
PapyrusVarCleanerSF1.g
File metadata and controls
355 lines (324 loc) · 12.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
* MIT License
*
* Copyright 2026 Open Papyrus Project
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to
* deal in the Software without restriction, including without limitation the
* rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
* sell copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
* IN THE SOFTWARE.
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
tree grammar PapyrusVarCleanerSF1;
options {
tokenVocab = PapyrusLexerSF1;
ASTLabelType = CommonTree;
language = CSharp3;
output = AST;
rewrite = true;
backtrack = true;
filter = true;
}
@header {
using Antlr.Runtime;
using Antlr.Runtime.Misc;
using Antlr.Runtime.Tree;
}
@members {
// -------------------------------------------------------------------------
// Runtime state (set before each pass via SetUp)
// -------------------------------------------------------------------------
private CleanupPass ePassType;
private ScriptObjectType pObjType;
private ScopeManager pScopeManager = new ScopeManager();
/// <summary>
/// True while the walker is inside a VAR definition's own header subtree.
/// Suppresses flagging the variable's own name as "used" during SCAN.
/// </summary>
private bool bInVarDefinition;
// -------------------------------------------------------------------------
// Initialization
// -------------------------------------------------------------------------
/// <summary>
/// Configures the cleaner before walking the AST.
/// Must be called before each pass (SCAN and CLEANUP separately).
/// </summary>
/// <param name="apObjType">The script being compiled.</param>
/// <param name="aePassType">SCAN to collect usage info; CLEANUP to remove unused vars.</param>
public void SetUp(ScriptObjectType apObjType, CleanupPass aePassType)
{
ePassType = aePassType;
pObjType = apObjType;
bInVarDefinition = false;
pScopeManager.Reset(pObjType);
}
// -------------------------------------------------------------------------
// Usage tracking helpers
// -------------------------------------------------------------------------
/// <summary>
/// Returns true if the given variable name was seen in at least one
/// non-definition usage during the SCAN pass.
///
/// During the CLEANUP pass, if the scope has no usage data for this name
/// (i.e., it was never defined in scope), returns true (keep it).
/// </summary>
public bool IsVarUsed(string asVarName)
{
bool arUsed = true;
if (ePassType == CleanupPass.CLEANUP && pScopeManager.CurrentScope != null)
pScopeManager.CurrentScope.TryGetVarUsed(asVarName, out arUsed);
return arUsed;
}
/// <summary>
/// Records that the given identifier was referenced (not as a definition).
/// Called during SCAN pass only, and only when NOT inside a VAR definition.
/// </summary>
public void HandleIDUse(string asID)
{
if (bInVarDefinition || pScopeManager.CurrentScope == null)
return;
pScopeManager.CurrentScope.TryFlagVarAsUsed(asID);
}
// -------------------------------------------------------------------------
// Pass type enum
// -------------------------------------------------------------------------
/// <summary>
/// Controls which rules are active during a given walk.
/// SCAN: enterVarDefinition + flagVarAsUsed active (collect usage)
/// CLEANUP: leaveVarDefinition active (remove unused vars)
/// </summary>
public enum CleanupPass
{
SCAN,
CLEANUP
}
}
// =============================================================================
// TreeRewriter entry points (filter = true)
// =============================================================================
//
// ANTLR3 TreeRewriter invokes topdown() when entering each node and bottomup()
// when leaving each node. Both passes are active on each walker invocation;
// the semantic predicates {ePassType == CleanupPass.SCAN}? and
// {ePassType == CleanupPass.CLEANUP}? gate which rules fire.
topdown
: enterFunction
| enterState
| enterProperty
| enterBlock
| enterVarDefinition
;
bottomup
: leaveFunction
| leaveState
| leaveProperty
| leaveBlock
| leaveVarDefinition
| flagVarAsUsed
;
// =============================================================================
// Scope-enter rules (topdown)
// =============================================================================
// -----------------------------------------------------------------------------
// enterFunction
//
// SF1 change: EnterFunction() takes ScriptFunctionName instead of a raw string.
//
// Called when the walker first enters a FUNCTION, EVENT, or REMOTEEVENT node.
// During SCAN: additionally clears the scope's used-var set so the next pass
// can detect which variables were referenced.
//
// Tree structure matched:
// ^( (FUNCTION | EVENT | REMOTEEVENT)
// ^(HEADER type funcName=ID .*) ; HEADER subtree: return type + name + params
// .? ) ; optional flags child after HEADER
// -----------------------------------------------------------------------------
enterFunction
: ^( (FUNCTION | EVENT | REMOTEEVENT)
^(HEADER type funcName=ID .*)
.? )
{
pScopeManager.EnterFunction(new ScriptFunctionName($funcName.text));
if (ePassType == CleanupPass.SCAN && pScopeManager.CurrentScope != null)
pScopeManager.CurrentScope.ClearUsedVars();
}
;
// -----------------------------------------------------------------------------
// enterState
//
// SF1 change: EnterState() takes ScriptObjectStateName instead of a raw string.
//
// Called when the walker first enters a STATE node.
//
// Tree structure matched:
// ^(STATE stateName=ID .*)
// -----------------------------------------------------------------------------
enterState
: ^(STATE stateName=ID .*)
{ pScopeManager.EnterState(new ScriptObjectStateName($stateName.text)); }
;
// -----------------------------------------------------------------------------
// enterProperty
//
// Called when the walker first enters a PROPERTY node.
//
// Tree structure matched:
// ^( PROPERTY
// ^(HEADER type propName=ID .*)
// .* )
// -----------------------------------------------------------------------------
enterProperty
: ^(PROPERTY
^(HEADER type propName=ID .*)
.*)
{ pScopeManager.EnterProperty($propName.text); }
;
// -----------------------------------------------------------------------------
// enterBlock
//
// Called when the walker first enters a BLOCK node (if/else/while body).
// Pushes a new variable scope block onto the scope stack.
// -----------------------------------------------------------------------------
enterBlock
: BLOCK
{ pScopeManager.EnterBlock(); }
;
// -----------------------------------------------------------------------------
// enterVarDefinition
//
// Active during SCAN pass only.
//
// Sets bInVarDefinition = true so that the variable's own name (seen as an ID
// child of the VAR node) is NOT flagged as a usage reference. This prevents
// the variable definition from counting as a use of itself.
//
// Tree structure matched:
// ^(VAR type varName=.) ; varName is consumed as wildcard (name not needed here)
//
// Semantic predicate: only active during SCAN pass.
// -----------------------------------------------------------------------------
enterVarDefinition
: ^(VAR type .)
{ePassType == CleanupPass.SCAN}?
{ bInVarDefinition = true; }
;
// =============================================================================
// Scope-leave rules (bottomup)
// =============================================================================
// -----------------------------------------------------------------------------
// leaveFunction
//
// Called when the walker exits a FUNCTION, EVENT, or REMOTEEVENT node.
// -----------------------------------------------------------------------------
leaveFunction
: FUNCTION { pScopeManager.LeaveFunction(); }
| EVENT { pScopeManager.LeaveFunction(); }
| REMOTEEVENT { pScopeManager.LeaveFunction(); }
;
// -----------------------------------------------------------------------------
// leaveState
// -----------------------------------------------------------------------------
leaveState
: STATE
{ pScopeManager.LeaveState(); }
;
// -----------------------------------------------------------------------------
// leaveProperty
// -----------------------------------------------------------------------------
leaveProperty
: PROPERTY
{ pScopeManager.LeaveProperty(); }
;
// -----------------------------------------------------------------------------
// leaveBlock
// -----------------------------------------------------------------------------
leaveBlock
: BLOCK
{ pScopeManager.LeaveBlock(); }
;
// -----------------------------------------------------------------------------
// leaveVarDefinition
//
// Active during both SCAN and CLEANUP passes (no pass predicate — it applies
// the rewrite unconditionally, but the rewrite logic checks IsVarUsed() which
// only removes nodes when ePassType == CLEANUP).
//
// During SCAN: clears bInVarDefinition and determines whether the variable
// will be cleaned up in the next pass (sets bCleanup scope flag).
// During CLEANUP: removes the VAR node if the variable was unused.
//
// Rewrite logic:
// - If bCleanup (variable was not referenced during SCAN):
// Replace ^(VAR type varName) with ID[$varName.token, "unused var"]
// This is a marker node that a subsequent pass may use for diagnostics.
// - If !bCleanup (variable is used):
// Reconstruct the original ^(VAR type varName) unchanged.
//
// Grammar scope: leaveVarDefinition_scope tracks bCleanup across action/rewrite.
//
// Tree structure matched:
// ^(VAR type varName=ID)
// -----------------------------------------------------------------------------
leaveVarDefinition
scope { bool bCleanup; }
: ^(VAR type varName=ID)
{
bInVarDefinition = false;
$leaveVarDefinition::bCleanup = !IsVarUsed($varName.text);
}
-> { $leaveVarDefinition::bCleanup }? ID[$varName.token, "unused var"]
-> ^(VAR type $varName)
;
// -----------------------------------------------------------------------------
// flagVarAsUsed
//
// Active during SCAN pass only.
//
// Called for every ID token encountered during the bottomup walk. If we are
// outside a variable definition (bInVarDefinition == false), records this ID
// as a reference in the current scope's usage map.
//
// The predicate {ePassType == CleanupPass.SCAN}? restricts this rule to the
// SCAN pass. During CLEANUP, ID tokens are not individually visited.
//
// Semantic predicate: only active during SCAN pass.
// -----------------------------------------------------------------------------
flagVarAsUsed
: idToken=ID
{ePassType == CleanupPass.SCAN}?
{ HandleIDUse($idToken.text); }
;
// =============================================================================
// Helper rules
// =============================================================================
// -----------------------------------------------------------------------------
// type
//
// Matches a Papyrus type reference: a base type token optionally followed by
// array brackets [].
//
// Examples:
// Int -> matches '.'
// Int[] -> matches '. LBRACKET RBRACKET'
// Actor -> matches '.'
// Actor[] -> matches '. LBRACKET RBRACKET'
//
// Note: the lookahead checks LA(2) == RBRACKET before committing to the
// optional brackets, matching the generated code's two-token lookahead.
// Unchanged from FO4 — only token numbers differ (LBRACKET=89, RBRACKET=123).
// -----------------------------------------------------------------------------
type
: . (LBRACKET RBRACKET)?
;