-
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathPapyrusTypeWalkerSSE.g
More file actions
310 lines (262 loc) · 12.4 KB
/
PapyrusTypeWalkerSSE.g
File metadata and controls
310 lines (262 loc) · 12.4 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
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
* 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 PapyrusTypeWalkerSSE;
options { tokenVocab=PapyrusLexerSSE; ASTLabelType=CommonTree; language=CSharp3; }
// Top-level script structure
// Semantic parameters: ScriptObjectType akObj, Compiler akCompiler,
// Dictionary<string,ScriptObjectType> akKnownTypes, Stack<string> akChildNames
// Initializes type system with built-in types (int, float, string, bool)
// Loads parent class if specified in header
script : ^(OBJECT header definitionOrBlock*)
;
// Script header with optional parent class
// Validates parent class exists and is not a built-in type
// Checks for circular inheritance (cannot extend self or child)
header : ^(ID USER_FLAGS ID? DOCSTRING?)
;
// Top-level definitions
definitionOrBlock : fieldDefinition
| import_obj
| function
| eventFunc
| stateBlock
| propertyBlock
;
// Field definition (script-level variable)
// Validates type exists and is known
// Checks initial value matches declared type
// Ensures variable name doesn't conflict with type names
fieldDefinition : ^(VAR type ID USER_FLAGS constant?)
;
// Import statement
// Loads imported script type and adds to kImportedTypes
import_obj : ^(IMPORT ID)
;
// Function definition
// Semantic parameters: string asStateName, string asPropertyName
// Validates function signature against parent class overrides
// Checks return type consistency
// Validates GLOBAL/NATIVE modifiers
function : ^(FUNCTION functionHeader codeBlock?)
;
functionHeader : ^(HEADER type ID USER_FLAGS callParameters? functionModifier* DOCSTRING?)
| ^(HEADER NONE ID USER_FLAGS callParameters? functionModifier* DOCSTRING?)
;
functionModifier : GLOBAL
| NATIVE
;
// Event definition
// Semantic parameter: string asStateName
// Validates event signature against parent class
// Checks NATIVE modifier validity
eventFunc : ^(EVENT eventHeader codeBlock?)
;
eventHeader : ^(HEADER NONE ID USER_FLAGS callParameters? NATIVE? DOCSTRING?)
;
// Function/event parameters
callParameters : callParameter*
;
// Parameter with optional default value
// Validates default value matches parameter type
callParameter : ^(PARAM type ID constant?)
;
// State block definition
stateBlock : ^(STATE ID AUTO? stateFuncOrEvent*)
;
// Semantic parameter: string asStateName
stateFuncOrEvent : function
| eventFunc
;
// Property block definition
// Can be auto-property or full property with get/set functions
propertyBlock : ^(PROPERTY propertyHeader propertyFunc propertyFunc?)
| ^(AUTOPROP propertyHeader ID)
;
propertyHeader : ^(HEADER type ID USER_FLAGS DOCSTRING?)
;
// Property function (get or set)
// Semantic parameter: string asPropName
// Validates getter returns property type, setter accepts property type
propertyFunc : ^(PROPFUNC function)
;
// Code block (statement list with scope)
// Semantic parameters: ScriptFunctionType akFunctionType, ScriptScope akCurrentScope
// Manages variable scope for local definitions
// Tracks temporary variables created during type checking
codeBlock : ^(BLOCK statement*)
;
// Statements
statement : localDefinition
| ^(EQUALS l_value expression) // Type-checked assignment with auto-cast
| expression // Expression statement (e.g., function call)
| return_stat
| ifBlock
| whileBlock
;
// Local variable definition
// Validates type exists and name doesn't conflict with script variables/properties
// Checks initial value type matches declared type
// Inserts auto-cast if needed
localDefinition : ^(VAR type ID expression?)
;
// L-value (assignment target)
// Returns kType for type checking against r-value
l_value : ^(DOT ^(PAREXPR expression) ID) // Property setter: expr.prop = value
| ^(ARRAYSET ^(PAREXPR expression) expression) // Array element: expr[idx] = value
| basic_l_value
;
// Basic l-value (simple assignment targets)
// Semantic parameters: ScriptVariableType akSelfType, string asSelfName
// Validates property setters exist and are writable
basic_l_value : ^(DOT array_func_or_id basic_l_value?) // Member access chain
| ^(ARRAYSET func_or_id expression) // Array element access
| ID // Simple variable
;
// Expression hierarchy (returns type information)
// All expression rules compute result type and may insert auto-casts
// Logical OR (returns bool)
expression : ^(OR expression and_expression)
| and_expression
;
// Logical AND (returns bool)
and_expression : ^(AND and_expression bool_expression)
| bool_expression
;
// Boolean comparisons (returns bool)
// Auto-casts operands to common type if compatible
bool_expression : ^(EQ bool_expression add_expression) // Equal
| ^(NE bool_expression add_expression) // Not equal
| ^(GT bool_expression add_expression) // Greater than
| ^(LT bool_expression add_expression) // Less than
| ^(GTE bool_expression add_expression) // Greater or equal
| ^(LTE bool_expression add_expression) // Less or equal
| add_expression
;
// Additive operations
// Returns int, float, or string (for concatenation)
// Auto-casts operands to common type (int/float promotion, string concat)
add_expression : ^(PLUS add_expression mult_expression) // Generic addition (rewritten to IADD/FADD/STRCAT)
| ^(MINUS add_expression mult_expression) // Generic subtraction (rewritten to ISUBTRACT/FSUBTRACT)
| mult_expression
;
// Multiplicative operations
// Returns int or float
// Auto-casts operands to common type (int/float promotion)
mult_expression : ^(MULT mult_expression unary_expression) // Generic multiply (rewritten to IMULTIPLY/FMULTIPLY)
| ^(DIVIDE mult_expression unary_expression) // Generic divide (rewritten to IDIVIDE/FDIVIDE)
| ^(MOD mult_expression unary_expression) // Modulo (int only)
| unary_expression
;
// Unary operations
// Validates operand type is numeric (for negation) or any (for NOT)
unary_expression : ^(UNARY_MINUS cast_atom) // Negation (rewritten to INEGATE/FNEGATE)
| ^(NOT cast_atom) // Logical NOT (returns bool)
| cast_atom
;
// Type casting
// Validates cast is valid (compatible types or upcast)
cast_atom : ^(AS dot_atom type)
| dot_atom
;
// Member access (dot notation)
// Validates properties and functions exist on object type
dot_atom : ^(DOT dot_atom array_func_or_id)
| array_atom
| constant
;
// Array element access
// Semantic parameters: ScriptVariableType akSelfType, string asSelfName
// Validates index type is int, returns array element type
array_atom : ^(ARRAYGET atom expression)
| atom
;
// Atomic expressions
// Semantic parameters: ScriptVariableType akSelfType, string asSelfName
atom : ^(PAREXPR expression) // Parenthesized expression
| ^(NEW (BASETYPE | ID) INTEGER) // Array allocation
| func_or_id
;
// Array element access or function/identifier
// Semantic parameters: ScriptVariableType akSelfType, string asSelfName
array_func_or_id : ^(ARRAYGET func_or_id expression)
| func_or_id
;
// Function call, property get, array length, or identifier
// Semantic parameters: ScriptVariableType akSelfType, string asSelfName
// Validates function signatures, property getters, and returns appropriate type
func_or_id : function_call
| ^(PROPGET ID ID) // Property getter access
| ID // Variable reference
| ^(LENGTH ID) // Array.Length property
;
// Return statement
// Validates return type matches function signature
// Inserts auto-cast if needed
return_stat : ^(RETURN expression?)
;
// Control flow statements
ifBlock : ^(IF expression codeBlock elseIfBlock* elseBlock?)
;
elseIfBlock : ^(ELSEIF expression codeBlock)
;
elseBlock : ^(ELSE codeBlock)
;
whileBlock : ^(WHILE expression codeBlock)
;
// Function call (all variants)
// Semantic parameters: ScriptVariableType akSelfType, string asSelfName
// Validates:
// - Function exists on target type (or in parent hierarchy)
// - Parameter types match (with auto-casting)
// - GLOBAL functions not called on variables
// - Member functions not called on types alone
// - Array functions (Find, RFind) called on arrays
// Input is always ^(CALL ...) — the type walker rewrites the CALL token to
// CALLPARENT, CALLGLOBAL, ARRAYFIND, or ARRAYRFIND via CreateCallTree()
// based on semantic analysis (global function, parent caller, array method, etc.)
function_call : ^(CALL ID ^(CALLPARAMS parameters?))
;
parameters : parameter*
;
// Function parameter (actual argument)
// Validates parameter type matches expected type
// Inserts auto-cast if needed
parameter : ^(PARAM ID? expression)
;
// Constant values (literals)
constant : number
| STRING
| BOOL
| NONE
;
number : INTEGER
| FLOAT
;
// Type specification
// Validates type is known (built-in or loaded script)
type : ID // Simple type
| ID LBRACKET RBRACKET // Array type
| BASETYPE // Built-in type reference
| BASETYPE LBRACKET RBRACKET // Built-in array type
;