-
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathPapyrusParserSF1.g4
More file actions
296 lines (230 loc) · 11 KB
/
PapyrusParserSF1.g4
File metadata and controls
296 lines (230 loc) · 11 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
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
* 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.
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
parser grammar PapyrusParserSF1;
options { tokenVocab=PapyrusLexerSF1; }
script : terminator? header definitionOrBlock* EOF
;
header : SCRIPTNAME scriptType ( EXTENDS scriptType )? userFlags terminator (DOCSTRING terminator)?
;
userFlags : userFlag*
;
// SF1 has 14 user flag types (13 system flags + custom ID)
// AUTO and AUTOREADONLY are structural keywords, not user flags — they appear in autoPropertyHeader and readOnlyPropertyHeader
userFlag : NATIVE // 0x0001
| GLOBAL // 0x0002
| DEBUGONLY // 0x0010
| BETAONLY // 0x0040
| CONST // 0x0020
| REQUIRESGUARD LPAREN ID (COMMA ID)* RPAREN // 0x0080 (NEW - takes guard name parameters)
| PROTECTSFUNCTIONLOGIC // 0x0100 (NEW - for guard definitions)
| SELFONLY // 0x0200 (NEW - access modifier)
| PRIVATE // 0x0400 (NEW - access modifier)
| PROTECTED // 0x0800 (NEW - access modifier)
| INTERNAL // 0x1000 (NEW - access modifier)
| ID // Custom user flags (Hidden, Conditional, Mandatory, CollapsedOnRef, Default, etc.)
;
// SF1 has 10 definition types (all 9 from FO4 + guardDefinition)
definitionOrBlock : fieldDefinition
| guardDefinition // NEW - guard variable for thread safety
| customEventDefinition
| import_obj
| function
| eventFunc
| stateBlock
| propertyBlock
| groupBlock
| structBlock
;
// NEW: Guard definition for thread safety/concurrency
guardDefinition : GUARD ID userFlags terminator
;
fieldDefinition : anyType ID ( EQUALS signedConstant )? userFlags terminator (DOCSTRING terminator)?
;
customEventDefinition : CUSTOMEVENT ID terminator
;
import_obj : IMPORT scriptType terminator
;
function : functionHeader functionBlock
;
functionHeader : anyType? FUNCTION ID LPAREN callParameters? RPAREN userFlags terminator (DOCSTRING terminator)?
;
functionBlock : terminator
| terminator statement* ENDFUNCTION terminator
;
eventFunc : eventHeader eventBlock
;
eventHeader : EVENT eventName LPAREN callParameters? RPAREN userFlags terminator (DOCSTRING terminator)?
;
eventName : scriptType DOT ID
| ID DOT ID
| ID
;
eventBlock : terminator ENDEVENT
| terminator statement* ENDEVENT terminator
;
callParameters : callParameter ( COMMA callParameter )*
;
callParameter : anyType ID? ( EQUALS signedConstant )?
| SCRIPTEVENTNAME ID
| CUSTOMEVENTNAME ID
| STRUCTVARNAME ID
;
stateBlock : AUTO? STATE ID terminator stateFuncOrEvent* ENDSTATE terminator
;
stateFuncOrEvent : function
| eventFunc
| propertyBlock
| groupBlock
;
propertyBlock : propertyHeader propertyFunc ( propertyFunc )* ENDPROPERTY terminator
| autoPropertyHeader
| readOnlyPropertyHeader
;
propertyHeader : anyType PROPERTY ID userFlags terminator (DOCSTRING terminator)?
;
autoPropertyHeader : anyType PROPERTY ID ( EQUALS signedConstant )? userFlags AUTO userFlags terminator (DOCSTRING terminator)?
;
readOnlyPropertyHeader : BASETYPE PROPERTY ID EQUALS signedConstant AUTOREADONLY userFlags terminator (DOCSTRING terminator)?
;
propertyFunc : function
;
groupBlock : groupHeader propertyBlock* ENDGROUP terminator
;
groupHeader : GROUP ID userFlags terminator (DOCSTRING terminator)?
;
structBlock : structHeader fieldDefinition* ENDSTRUCT terminator
;
structHeader : STRUCT ID terminator
;
statement : localDefinition
| l_value PLUSEQUALS expression terminator
| l_value MINUSEQUALS expression terminator
| l_value MULTEQUALS expression terminator
| l_value DIVEQUALS expression terminator
| l_value MODEQUALS expression terminator
| l_value EQUALS expression terminator
| expression terminator
| return_stat
| lockBlock // NEW - lock guard for thread safety
| tryLockBlock // NEW - try lock guard with else clause
| ifBlock
| whileBlock
;
l_value : LPAREN expression RPAREN DOT ID
| LPAREN expression RPAREN LBRACKET expression RBRACKET
| basic_l_value
;
basic_l_value : array_func_or_id DOT basic_l_value
| func_or_id LBRACKET expression RBRACKET
| ID
;
localDefinition : anyType ID (EQUALS expression CONST?)? terminator (DOCSTRING terminator)?
;
expression : and_expression ( OR and_expression )*
;
and_expression : bool_expression ( AND bool_expression )*
;
bool_expression : add_expression ( ( EQ | GT | GTE | LT | LTE | NE ) add_expression )*
;
add_expression : mult_expression ( ( PLUS | MINUS ) mult_expression )*
;
mult_expression : unary_expression ( ( MULT | DIVIDE | MOD ) unary_expression )*
;
unary_expression : MINUS cast_atom
| NOT cast_atom
| cast_atom
;
cast_atom : dot_atom ( ( AS | IS ) anyType )?
;
dot_atom : array_atom ( DOT array_func_or_id )*
| unsignedConstant
;
array_atom : atom ( LBRACKET expression RBRACKET )
| atom
;
atom : LPAREN expression RPAREN
| new_expr
| func_or_id
;
new_expr : NEW BASETYPE LBRACKET expression RBRACKET
| NEW scriptType ( LBRACKET expression RBRACKET )?
;
array_func_or_id : function_call LBRACKET expression RBRACKET
| ID LBRACKET expression RBRACKET
| func_or_id
;
func_or_id : function_call
| scriptType
| LENGTH
;
return_stat : RETURN expression? terminator
;
ifBlock : IF expression terminator statement* elseIfBlock* elseBlock? ENDIF terminator
;
elseIfBlock : ELSEIF expression terminator statement*
;
elseBlock : ELSE terminator statement*
;
whileBlock : WHILE expression terminator statement* ENDWHILE terminator
;
// NEW: Lock guard blocks for thread safety
// LockGuard supports: LockGuard GuardName, LockGuard(GuardName), LockGuard Guard1, Guard2
lockBlock : LOCKGUARD (LPAREN ID (COMMA ID)* RPAREN | ID (COMMA ID)*) terminator statement* ENDLOCKGUARD terminator
;
// TryLockGuard uses Else (not ElseTryLockGuard) for the else clause
tryLockBlock : TRYLOCKGUARD ID terminator statement* elseTryLockBlock? ENDTRYLOCKGUARD terminator
;
elseTryLockBlock : ELSE terminator statement*
;
function_call : ID LPAREN parameters? RPAREN
;
parameters : parameter ( COMMA parameter )*
;
parameter : ( ID EQUALS )? expression
;
signedConstant : signedNumber
| STRING
| BOOL
| NONE
;
unsignedConstant : unsignedNumber
| STRING
| BOOL
| NONE
;
signedNumber : MINUS unsignedNumber
| unsignedNumber
;
unsignedNumber : INTEGER
| FLOAT
;
anyType : scriptType
| scriptType LBRACKET RBRACKET
| BASETYPE
| BASETYPE LBRACKET RBRACKET
;
scriptType : ID ( COLON ID )*
;
terminator : WS? EOL*
;