-
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathPapyrusGenSSE.g
More file actions
748 lines (652 loc) · 25.5 KB
/
PapyrusGenSSE.g
File metadata and controls
748 lines (652 loc) · 25.5 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
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
* 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 PapyrusGenSSE;
options {
tokenVocab = PapyrusParserSSE;
ASTLabelType = CommonTree;
language = CSharp3;
output = template;
}
@header {
using System.Collections;
using PCompiler;
}
@members {
// Template library for code generation
protected StringTemplateGroup templateLib =
new StringTemplateGroup("PapyrusGenTemplates", typeof(AngleBracketTemplateLexer));
// Known user flags dictionary (set externally)
private Dictionary<string, PapyrusFlag> kFlagDict;
internal Dictionary<string, PapyrusFlag> KnownUserFlags { set { kFlagDict = value; } }
// Script object type information (from type walker)
private ScriptObjectType kObjType;
// Variable name mangling suffix counter
private int iCurMangleSuffix = 0;
// Label generation counter
private int iCurLabelSuffix = 0;
// Error event handler
internal event InternalErrorEventHandler ErrorHandler;
// Error handling override
private void OnError(string asError, int aiLineNumber, int aiColumnNumber)
{
if (ErrorHandler != null)
ErrorHandler(this, new InternalErrorEventArgs(asError, aiLineNumber, aiColumnNumber));
}
public override void DisplayRecognitionError(string[] tokenNames, RecognitionException e)
{
OnError(GetErrorMessage(e, tokenNames), e.Line, e.CharPositionInLine);
}
// Variable name mangling (for scope collision resolution)
private string MangleVariableName(string asOriginalName)
{
string mangledName = string.Format("::mangled_{0}_{1}", asOriginalName, iCurMangleSuffix);
++iCurMangleSuffix;
return mangledName;
}
private void MangleFunctionVariables(ScriptFunctionType akFunction)
{
Dictionary<string, bool> definedVars = new Dictionary<string, bool>();
MangleScopeVariables(akFunction.Scope, ref definedVars);
}
private void MangleScopeVariables(ScriptScope akCurrentScope, ref Dictionary<string, bool> akAlreadyDefinedVars)
{
// Recursive scope variable mangling implementation
// Renames variables that shadow outer scope variables to "::mangled_<name>_<suffix>"
foreach (var variable in akCurrentScope.Variables)
{
if (akAlreadyDefinedVars.ContainsKey(variable.Name))
{
variable.MangledName = MangleVariableName(variable.Name);
}
else
{
akAlreadyDefinedVars.Add(variable.Name, true);
}
}
// Recurse into child scopes
foreach (var childScope in akCurrentScope.ChildScopes)
{
MangleScopeVariables(childScope, ref akAlreadyDefinedVars);
}
}
// Label generation for control flow
private string GenerateLabel()
{
string label = string.Format("label{0}", iCurLabelSuffix);
++iCurLabelSuffix;
return label;
}
// Timestamp tracking for compiled output
private static DateTime UnixEpoc { get { return new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc); } }
private static string ToUnixTime(DateTime dt)
{
return ((long)(dt - UnixEpoc).TotalSeconds).ToString();
}
private string GetFileModTimeUnix(string asSourceFilename)
{
return ToUnixTime(File.GetLastWriteTimeUtc(asSourceFilename));
}
private string GetCompileTimeUnix()
{
return ToUnixTime(DateTime.UtcNow);
}
// User flags reference table construction
private Hashtable ConstructUserFlagRefInfo()
{
Hashtable flagTable = new Hashtable();
foreach (var kvp in kFlagDict)
{
flagTable.Add(kvp.Key, kvp.Value.Index);
}
return flagTable;
}
}
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
* SCOPE DECLARATIONS
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
// Script-level scope (16 fields)
scope script {
string sobjName; // Script object name
string sparentName; // Parent script name
IList kobjVarDefinitions; // Variable definitions
IList kobjPropDefinitions; // Property definitions
string sinitialState; // Initial state name
IList kobjEmptyState; // Empty state event list
Hashtable kstates; // State blocks (name -> template)
bool bhasBeginStateEvent; // Has BeginState event
bool bhasEndStateEvent; // Has EndState event
string smodTimeUnix; // File modification time (Unix)
string scompileTimeUnix; // Compilation time (Unix)
string suserName; // User name (environment)
string scomputerName; // Computer name (environment)
string sobjFlags; // User flags string
Hashtable kuserFlagsRef; // Flag reference table
string sdocString; // Documentation string
}
// Field definition scope (1 field)
scope fieldDefinition {
string sinitialValue; // Initial value expression
}
// Function scope (12 fields)
scope function {
string sstate; // State name (empty for script-level)
string sfuncName; // Function name
string spropertyName; // Property name (for property functions)
string sreturnType; // Return type name
bool bisNative; // Is native function
bool bisGlobal; // Is global function
IList kfuncParams; // Parameter templates
IList kfuncVarDefinitions; // Local variable definitions
IList kstatements; // Statement templates
string suserFlags; // User flags string
string sdocString; // Documentation string
ScriptFunctionType kfuncType; // Function type metadata (from type walker)
}
// Event scope (11 fields)
scope eventFunc {
string sstate; // State name (empty for script-level)
string sfuncName; // Event name
string sreturnType; // Return type (always "None" for events)
bool bisNative; // Is native event
bool bisGlobal; // Is global event (always false)
IList kfuncParams; // Parameter templates
IList kfuncVarDefinitions; // Local variable definitions
IList kstatements; // Statement templates
string suserFlags; // User flags string
string sdocString; // Documentation string
ScriptFunctionType kfuncType; // Event type metadata (from type walker)
}
// Property scope (4 fields)
scope propertyBlock {
string spropName; // Property name
string spropType; // Property type
string suserFlags; // User flags string
string sdocString; // Documentation string
}
// Code block scope (3 fields)
scope codeBlock {
IList kvarDefs; // Local variable definitions
ScriptScope kcurrentScope; // Current variable scope (from type walker)
int inextScopeChild; // Next child scope index
}
// Statement scope (1 field)
scope statement {
string smangledName; // Mangled variable name (for locals)
}
// L-value scope (2 fields)
scope l_value {
string ssourceName; // Source expression variable
string sselfName; // Self reference variable
}
// Basic l-value scope (2 fields)
scope basic_l_value {
string ssourceName; // Source expression variable
string sselfName; // Self reference variable
}
// Array atom scope (1 field)
scope array_atom {
string sselfName; // Self reference variable
}
// Array/func/id scope (1 field)
scope array_func_or_id {
string sselfName; // Self reference variable
}
// Func/id scope (1 field)
scope func_or_id {
string sselfName; // Self reference variable
}
// Property set scope (2 fields)
scope property_set {
string sselfName; // Self reference variable
string sparamName; // Parameter variable name
}
// If block scope (3 fields)
scope ifBlock {
IList kBlockStatements; // Block statements
string sEndLabel; // End label for jumps
ScriptScope kchildScope; // Child scope (from type walker)
}
// ElseIf block scope (2 fields)
scope elseIfBlock {
IList kBlockStatements; // Block statements
ScriptScope kchildScope; // Child scope (from type walker)
}
// Else block scope (2 fields)
scope elseBlock {
IList kBlockStatements; // Block statements
ScriptScope kchildScope; // Child scope (from type walker)
}
// While block scope (4 fields)
scope whileBlock {
IList kBlockStatements; // Block statements
string sStartLabel; // Start label for loop
string sEndLabel; // End label for break
ScriptScope kchildScope; // Child scope (from type walker)
}
// Function call scope (1 field)
scope function_call {
string sselfName; // Self reference variable
}
// Auto-cast scope (1 field)
scope autoCast {
string ssource; // Source variable for auto-cast
}
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
* TREE WALKING RULES
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
* Script-Level Rules
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
// Main entry point: script object
// Semantic parameters: asSourceFilename (source file path), akObj (script object type)
script[string asSourceFilename, ScriptObjectType akObj]
scope { script; }
: ^(OBJECT header definitionOrBlock*)
// Template: object(objName, parent, variableDefs, propDefs, initialState, emptyState, states, ...)
;
// Script header: name, parent, flags, docstring
header
: ID USER_FLAGS? ID? DOCSTRING?
// Extracts script name, parent name, user flags, and documentation
;
// Top-level definition dispatcher
definitionOrBlock
: fieldDefinition
| function
| eventFunc
| stateBlock
| propertyBlock
;
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
* Definition Rules
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
// Variable/field definition
fieldDefinition
scope { fieldDefinition; }
: ^(VAR type ID USER_FLAGS constant?)
// Template: variableDef(name, type, flags, initialValue, lineNo)
;
// Function definition
// Semantic parameters: asState (state name, empty for script-level), asPropertyName (property name, empty for non-property)
function[string asState, string asPropertyName]
scope { function; }
: ^(FUNCTION functionHeader codeBlock?)
// Template: functionDef(state, name, returnType, params, varDefs, statements, native, global, flags, docString)
;
// Function header: return type, name, parameters, modifiers
functionHeader
: ^(HEADER (type | NONE) ID USER_FLAGS? callParameters? functionModifier* DOCSTRING?)
// Extracts function signature
;
// Function modifier: NATIVE or GLOBAL
functionModifier
: NATIVE
| GLOBAL
;
// Event definition
// Semantic parameters: asState (state name, empty for script-level)
eventFunc[string asState]
scope { eventFunc; }
: ^(EVENT eventHeader codeBlock?)
// Template: functionDef(state, name, returnType="None", params, varDefs, statements, native, flags, docString)
;
// Event header: name, parameters
eventHeader
: ^(HEADER NONE ID USER_FLAGS? callParameters? NATIVE? DOCSTRING?)
// Extracts event signature
;
// Function/event parameter list
callParameters returns [IList kParams]
: callParameter+
// Returns list of parameter templates
;
// Single parameter
callParameter
: ^(PARAM type ID? constant?)
// Template: funcParam(name, type, defaultValue)
;
// State block definition
stateBlock
: ^(STATE ID AUTO? stateFuncOrEvent*)
// Template: state block with functions/events
;
// Function or event within a state
stateFuncOrEvent[string asStateName]
: function[asStateName, ""]
| eventFunc[asStateName]
;
// Property block definition
propertyBlock
scope { propertyBlock; }
: ^(PROPERTY propertyHeader propertyFunc propertyFunc?)
// Template: fullProp(name, type, flags, docString, getFunc, setFunc)
| ^(AUTOPROP propertyHeader ID)
// Template: autoProp(name, type, flags, docString, autoVarName)
;
// Property header: type, name, flags
propertyHeader
: ^(HEADER type ID USER_FLAGS? DOCSTRING?)
// Extracts property signature
;
// Property getter/setter function
// Semantic parameters: asPropName (property name)
propertyFunc[string asPropName]
: ^(PROPFUNC function[, asPropName])
| PROPFUNC
// Empty property function (no body)
;
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
* Code Block and Statement Rules
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
// Function/event body
// Semantic parameters: akStatements (output accumulator), akVarDefinitions (output accumulator), akCurrentScope (variable scope)
codeBlock[IList akStatements, IList akVarDefinitions, ScriptScope akCurrentScope]
scope { codeBlock; }
: ^(BLOCK statement*)
// Accumulates variable definitions and statements
;
// Statement dispatcher
statement
scope { statement; }
: localDefinition
// Template: localDef(name, type, initialValue, lineNo)
| ^(EQUALS ID autoCast? l_value expression)
// Template: assign(target, source, autoCast, lineNo)
| expression
// Expression statement (e.g., function call)
| return_stat
// Template: return(value, lineNo)
| ifBlock
// Template: ifBlock(condition, statements, elseIfs, else, lineNo)
| whileBlock
// Template: whileBlock(condition, statements, lineNo)
;
// Local variable definition
localDefinition returns [string sVarName, string sExprVar, int iLineNo, StringTemplate kExprST, StringTemplate kAutoCastST]
: ^(VAR type ID autoCast? expression?)
// Template: localDef(name, type, initialValue, autoCast, expressions, lineNo)
;
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
* L-Value Rules (Assignment Targets)
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
// L-value: assignment target
l_value
scope { l_value; }
: ^(DOT ^(PAREXPR expression) property_set)
// Template: propSet(self, propName, value, lineNo)
| ^(ARRAYSET ID ID autoCast? ^(PAREXPR expression) expression)
// Template: arraySet(array, index, value, autoCast, lineNo)
| basic_l_value
// Delegate to basic l-value
;
// Basic l-value: simple assignment target
basic_l_value
scope { basic_l_value; }
: ^(DOT array_func_or_id basic_l_value)
// Dot operator l-value
| function_call
// Function call l-value (not actually assignable, but parsed)
| property_set
// Property setter
| ^(ARRAYSET ID ID autoCast? func_or_id expression)
// Array element assignment
| ID
// Simple identifier
;
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
* Expression Rules
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
// Logical OR expression
expression returns [string sRetValue]
: ^(OR ID expression expression and_expression)
// Template: orExpression(target, left, right, lineNo)
| and_expression
// Delegate to AND expression
;
// Logical AND expression
and_expression returns [string sRetValue]
: ^(AND ID and_expression and_expression bool_expression)
// Template: andExpression(target, left, right, lineNo)
| bool_expression
// Delegate to boolean expression
;
// Boolean/comparison expression
bool_expression returns [string sRetValue]
: ^(EQ ID autoCast? autoCast? bool_expression add_expression)
// Template: twoOpCommand(command="CMPEQ", target, left, right, autoCast, lineNo)
| ^(NE ID autoCast? autoCast? bool_expression add_expression)
// Template: notEqual(target, left, right, autoCast, lineNo)
| ^(GT ID autoCast? autoCast? bool_expression add_expression)
// Template: twoOpCommand(command="CMPGT", ...)
| ^(LT ID autoCast? autoCast? bool_expression add_expression)
// Template: twoOpCommand(command="CMPLT", ...)
| ^(GTE ID autoCast? autoCast? bool_expression add_expression)
// Template: twoOpCommand(command="CMPGE", ...)
| ^(LTE ID autoCast? autoCast? bool_expression add_expression)
// Template: twoOpCommand(command="CMPLE", ...)
| add_expression
// Delegate to addition expression
;
// Addition/subtraction expression
add_expression returns [string sRetValue]
: ^(IADD ID autoCast? autoCast? add_expression mult_expression)
// Template: twoOpCommand(command="IADD", target, left, right, autoCast, lineNo)
| ^(FADD ID autoCast? autoCast? add_expression mult_expression)
// Template: twoOpCommand(command="FADD", ...)
| ^(ISUBTRACT ID autoCast? autoCast? add_expression mult_expression)
// Template: twoOpCommand(command="ISUBTRACT", ...)
| ^(FSUBTRACT ID autoCast? autoCast? add_expression mult_expression)
// Template: twoOpCommand(command="FSUBTRACT", ...)
| ^(STRCAT ID autoCast? autoCast? add_expression mult_expression)
// Template: twoOpCommand(command="STRCAT", ...)
| mult_expression
// Delegate to multiplication expression
;
// Multiplication/division/modulus expression
mult_expression returns [string sRetValue]
: ^(IMULTIPLY ID autoCast? autoCast? mult_expression unary_expression)
// Template: twoOpCommand(command="IMULTIPLY", target, left, right, autoCast, lineNo)
| ^(FMULTIPLY ID autoCast? autoCast? mult_expression unary_expression)
// Template: twoOpCommand(command="FMULTIPLY", ...)
| ^(IDIVIDE ID autoCast? autoCast? mult_expression unary_expression)
// Template: twoOpCommand(command="IDIVIDE", ...)
| ^(FDIVIDE ID autoCast? autoCast? mult_expression unary_expression)
// Template: twoOpCommand(command="FDIVIDE", ...)
| ^(MOD ID autoCast? autoCast? mult_expression unary_expression)
// Template: twoOpCommand(command="MOD", ...)
| unary_expression
// Delegate to unary expression
;
// Unary expression (negation, NOT)
unary_expression returns [string sRetValue]
: ^(INEGATE ID cast_atom)
// Template: singleOpCommand(command="INEGATE", target, source, lineNo)
| ^(FNEGATE ID cast_atom)
// Template: singleOpCommand(command="FNEGATE", ...)
| ^(NOT ID cast_atom)
// Template: singleOpCommand(command="NOT", ...)
| cast_atom
// Delegate to cast expression
;
// Type cast expression (AS operator)
cast_atom returns [string sRetValue]
: ^(AS ID dot_atom type)
// Template: cast(target, source, type, lineNo)
| dot_atom
// Delegate to dot expression
;
// Dot operator expression (member access)
dot_atom returns [string sRetValue]
: ^(DOT array_func_or_id dot_atom)
// Template: dot(target, object, member, lineNo)
| array_atom
// Delegate to array expression
;
// Array access expression
array_atom returns [string sRetValue]
scope { array_atom; }
: ^(ARRAYGET ID array_func_or_id ^(PAREXPR expression))
// Template: arrayGet(target, array, index, lineNo)
| ^(LENGTH ID array_func_or_id)
// Template: arrayLength(target, array, lineNo)
| ^(ARRAYFIND ID array_func_or_id ^(PAREXPR expression) ^(PAREXPR expression)?)
// Template: arrayFind(target, array, element, startIndex, lineNo)
| ^(ARRAYRFIND ID array_func_or_id ^(PAREXPR expression) ^(PAREXPR expression)?)
// Template: arrayRFind(target, array, element, startIndex, lineNo)
| atom
// Delegate to atom
;
// Terminal atom (literals, identifiers, function calls, parenthesized expressions)
atom returns [string sRetValue]
: array_func_or_id
// Array, function, or identifier
| ^(NEW ID type ^(PAREXPR expression))
// Template: newArray(target, type, size, lineNo)
| ^(PAREXPR expression)
// Parenthesized expression
| constant
// Constant literal
;
// Array, function call, or identifier
array_func_or_id returns [string sRetValue]
scope { array_func_or_id; }
: ^(ARRAYGET ID ID ^(PAREXPR expression))
// Template: arrayGet(target, array, index, lineNo)
| ^(LENGTH ID ID)
// Template: arrayLength(target, array, lineNo)
| ^(ARRAYFIND ID ID ^(PAREXPR expression) ^(PAREXPR expression)?)
// Template: arrayFind(target, array, element, startIndex, lineNo)
| ^(ARRAYRFIND ID ID ^(PAREXPR expression) ^(PAREXPR expression)?)
// Template: arrayRFind(target, array, element, startIndex, lineNo)
| func_or_id
// Function call or identifier
;
// Function call or identifier
func_or_id returns [string sRetValue]
scope { func_or_id; }
: ^(CALL ID ID parameters)
// Template: callLocal(target, funcName, params, lineNo)
| ^(CALLPARENT ID ID parameters)
// Template: callParent(target, funcName, params, lineNo)
| ^(CALLGLOBAL ID ID ID parameters)
// Template: callGlobal(target, scriptName, funcName, params, lineNo)
| ^(PROPGET ID ID ID)
// Template: propGet(target, object, propName, lineNo)
| ID
// Simple identifier
;
// Property setter call
property_set
scope { property_set; }
: ^(PROPSET ID autoCast? ID expression)
// Template: propSet(object, propName, value, autoCast, lineNo)
;
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
* Control Flow Rules
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
// Return statement
return_stat
: ^(RETURN expression?)
// Template: return(value, lineNo)
;
// If statement
ifBlock
scope { ifBlock; }
: ^(IF expression statement* elseIfBlock* elseBlock?)
// Template: ifBlock(condition, statements, elseIfs, else, endLabel, lineNo)
;
// ElseIf block
elseIfBlock
scope { elseIfBlock; }
: ^(ELSEIF expression statement*)
// Template: elseIfBlock(condition, statements, lineNo)
;
// Else block
elseBlock
scope { elseBlock; }
: ^(ELSE statement*)
// Template: elseBlock(statements, lineNo)
;
// While loop
whileBlock
scope { whileBlock; }
: ^(WHILE expression statement*)
// Template: whileBlock(condition, statements, startLabel, endLabel, lineNo)
;
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
* Function Call and Parameter Rules
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
// Function call (used within expressions)
function_call returns [string sRetValue]
scope { function_call; }
: ^(CALL ID ID ID ^(CALLPARAMS parameters?))
// Template: callLocal(target, funcName, retValue, params, lineNo)
| ^(CALLPARENT ID ID ID ^(CALLPARAMS parameters?))
// Template: callParent(target, funcName, retValue, params, lineNo)
| ^(CALLGLOBAL ID ID ID ^(CALLPARAMS parameters?))
// Template: callGlobal(target, scriptName, funcName, params, lineNo)
| ^(ARRAYFIND ID ID ^(CALLPARAMS parameters?))
// Template: arrayFind(target, retValue, params, lineNo)
| ^(ARRAYRFIND ID ID ^(CALLPARAMS parameters?))
// Template: arrayRFind(target, retValue, params, lineNo)
;
// Call parameters
parameters returns [IList sParamVars, IList kAutoCastST]
: parameter*
// Template: parameterExpressions(params)
;
// Single call parameter
parameter returns [string sVarName, StringTemplate kAutoCastST]
: autoCast? expression
// Returns parameter variable name and auto-cast template
;
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
* Utility Rules
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
// Automatic type cast
autoCast returns [string sRetValue]
scope { autoCast; }
: ^(AS ID expression type)
// Template: cast(target, source, type, lineNo)
;
// Constant value
constant
: INTEGER
| FLOAT
| STRING
| BOOL
| NONE
;
// Number literal
number
: INTEGER
| FLOAT
;
// Type specification
type
: ID LBRACKET RBRACKET
// Array type: ID[]
| ID
// Simple type: ID
;