-
Notifications
You must be signed in to change notification settings - Fork 296
/
VBAParser.g4
927 lines (788 loc) · 31.1 KB
/
VBAParser.g4
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
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
/*
* Copyright (C) 2014 Ulrich Wolffgang <u.wol@wwu.de>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
/* VBA grammar based on MS VBAL */
parser grammar VBAParser;
options { tokenVocab = VBALexer; }
@header { using System.Text.RegularExpressions; }
startRule : module EOF;
module :
endOfStatement?
moduleAttributes
moduleHeader?
moduleAttributes
moduleConfigReferences?
moduleConfig?
moduleAttributes
moduleDeclarations
moduleAttributes
moduleBody
moduleAttributes
// A module can consist of WS as well as line continuations only.
whiteSpace?
;
moduleHeader : VERSION whiteSpace numberLiteral whiteSpace? CLASS? endOfStatement;
moduleConfigReferences: moduleConfigReferenceElement+;
moduleConfigReferenceElement:
OBJECT whiteSpace? EQ whiteSpace? STRINGLITERAL whiteSpace? SEMICOLON whiteSpace? STRINGLITERAL endOfStatement
;
moduleConfig :
BEGIN (whiteSpace (GUIDLITERAL | expression) whiteSpace unrestrictedIdentifier whiteSpace?)? endOfStatement
(moduleConfig | moduleConfigProperty | moduleConfigElement)+
END endOfStatement
;
moduleConfigProperty :
BEGINPROPERTY whiteSpace unrestrictedIdentifier (whiteSpace GUIDLITERAL)? endOfStatement
(moduleConfigProperty | moduleConfigElement)+
ENDPROPERTY endOfStatement
;
moduleConfigElement :
(unrestrictedIdentifier | lExpression) whiteSpace? EQ whiteSpace? (shortcut | resource | expression) endOfStatement
;
shortcut :
(POW singleLetter)
| ((PERCENT | PLUS? POW?) L_BRACE IDENTIFIER R_BRACE)
;
resource :
DOLLAR? expression COLON (numberLiteral | BARE_HEX_LITERAL | unrestrictedIdentifier)
;
moduleAttributes : (attributeStmt endOfStatement)*;
attributeStmt : ATTRIBUTE whiteSpace attributeName whiteSpace? EQ whiteSpace? attributeValue (whiteSpace? COMMA whiteSpace? attributeValue)*;
attributeName : lExpression;
attributeValue : expression;
moduleDeclarations : (moduleDeclarationsElement endOfStatement)*;
moduleOption :
OPTION_BASE whiteSpace numberLiteral # optionBaseStmt
| OPTION_COMPARE whiteSpace (BINARY | TEXT | DATABASE) # optionCompareStmt
| OPTION_EXPLICIT # optionExplicitStmt
| OPTION_PRIVATE_MODULE # optionPrivateModuleStmt
;
moduleDeclarationsElement :
whiteSpace?
(attributeStmt
| declareStmt
| defDirective
| enumerationStmt
| eventStmt
| constStmt
| implementsStmt
| variableStmt
| moduleOption
| udtDeclaration)
;
moduleBody :
whiteSpace?
(moduleBodyElement endOfStatement)*;
moduleBodyElement :
functionStmt
| propertyGetStmt
| propertySetStmt
| propertyLetStmt
| subStmt
;
block : (blockStmt endOfStatement)*;
unterminatedBlock : blockStmt (endOfStatement blockStmt)*;
blockStmt :
statementLabelDefinition whiteSpace? mainBlockStmt?
| mainBlockStmt
;
mainBlockStmt :
fileStmt
| attributeStmt
| constStmt
| doLoopStmt
| endStmt
| eraseStmt
| errorStmt
| exitStmt
| forEachStmt
| forNextStmt
| goSubStmt
| goToStmt
| ifStmt
| singleLineIfStmt
| implementsStmt
| midStatement
| letStmt
| lsetStmt
| onErrorStmt
| onGoToStmt
| onGoSubStmt
| raiseEventStmt
| redimStmt
| resumeStmt
| returnStmt
| rsetStmt
| selectCaseStmt
| setStmt
| stopStmt
| variableStmt
| whileWendStmt
| withStmt
| lineSpecialForm
| circleSpecialForm
| scaleSpecialForm
| pSetSpecialForm
| callStmt
| nameStmt
;
// 5.4.5 File Statements
fileStmt :
openStmt
| resetStmt
| closeStmt
| seekStmt
| lockStmt
| unlockStmt
| lineInputStmt
| widthStmt
| debugPrintStmt
| printStmt
| writeStmt
| inputStmt
| putStmt
| getStmt
;
// 5.4.5.1 Open Statement
openStmt : OPEN whiteSpace pathName (whiteSpace modeClause)? (whiteSpace accessClause)? (whiteSpace lock)? whiteSpace AS whiteSpace fileNumber (whiteSpace lenClause)?;
pathName : expression;
modeClause : FOR whiteSpace fileMode;
fileMode : APPEND | BINARY | INPUT | OUTPUT | RANDOM;
accessClause : ACCESS whiteSpace access;
access :
READ
| WRITE
| READ_WRITE
;
lock :
SHARED
| LOCK_READ
| LOCK_WRITE
| LOCK_READ_WRITE
;
lenClause : LEN whiteSpace? EQ whiteSpace? recLength;
recLength : expression;
// 5.4.5.1.1 File Numbers
fileNumber : markedFileNumber | unmarkedFileNumber;
markedFileNumber : HASH expression;
unmarkedFileNumber : expression;
// 5.4.5.2 Close and Reset Statements
closeStmt : CLOSE (whiteSpace fileNumberList)?;
resetStmt : RESET;
fileNumberList : fileNumber (whiteSpace? COMMA whiteSpace? fileNumber)*;
// 5.4.5.3 Seek Statement
seekStmt : SEEK whiteSpace fileNumber whiteSpace? COMMA whiteSpace? position;
position : expression;
// 5.4.5.4 Lock Statement
lockStmt : LOCK whiteSpace fileNumber (whiteSpace? COMMA whiteSpace? recordRange)?;
recordRange :
startRecordNumber
| (startRecordNumber whiteSpace)? TO whiteSpace endRecordNumber
;
startRecordNumber : expression;
endRecordNumber : expression;
// 5.4.5.5 Unlock Statement
unlockStmt : UNLOCK whiteSpace fileNumber (whiteSpace? COMMA whiteSpace? recordRange)?;
// 5.4.5.6 Line Input Statement
lineInputStmt : LINE_INPUT whiteSpace markedFileNumber whiteSpace? COMMA whiteSpace? variableName;
variableName : expression;
// 5.4.5.7 Width Statement
widthStmt : WIDTH whiteSpace markedFileNumber whiteSpace? COMMA whiteSpace? lineWidth;
lineWidth : expression;
// 5.4.5.8 Print Statement
// Debug.Print is special because it seems to take an output list as argument.
// To shield the rest of the parsing/binding from this peculiarity, we treat it as a statement
// and let the resolver handle it.
debugPrintStmt : debugPrint (whiteSpace outputList)?;
// We split it up into separate rules so that we have context classes generated that can be used in declarations/references.
debugPrint : debugModule whiteSpace? DOT whiteSpace? debugPrintSub;
debugModule : DEBUG;
debugPrintSub : PRINT;
printStmt : PRINT whiteSpace markedFileNumber whiteSpace? COMMA (whiteSpace? outputList)?;
// 5.4.5.8.1 Output Lists
outputList : outputItem (whiteSpace? outputItem)*;
outputItem :
outputClause
| charPosition
| outputClause whiteSpace? charPosition
;
outputClause : spcClause | tabClause | outputExpression;
charPosition : SEMICOLON | COMMA;
outputExpression : expression;
spcClause : SPC whiteSpace? LPAREN whiteSpace? spcNumber whiteSpace? RPAREN;
spcNumber : expression;
tabClause : TAB (whiteSpace? tabNumberClause)?;
tabNumberClause : LPAREN whiteSpace? tabNumber whiteSpace? RPAREN;
tabNumber : expression;
// 5.4.5.9 Write Statement
writeStmt : WRITE whiteSpace markedFileNumber whiteSpace? COMMA (whiteSpace? outputList)?;
// 5.4.5.10 Input Statement
inputStmt : INPUT whiteSpace markedFileNumber whiteSpace? COMMA whiteSpace? inputList;
inputList : inputVariable (whiteSpace? COMMA whiteSpace? inputVariable)*;
inputVariable : expression;
// 5.4.5.11 Put Statement
putStmt : PUT whiteSpace fileNumber whiteSpace? COMMA whiteSpace? recordNumber? whiteSpace? COMMA whiteSpace? data;
recordNumber : expression;
data : expression;
// 5.4.5.12 Get Statement
getStmt : GET whiteSpace fileNumber whiteSpace? COMMA whiteSpace? recordNumber? whiteSpace? COMMA whiteSpace? variable;
variable : expression;
constStmt : (visibility whiteSpace)? CONST whiteSpace constSubStmt (whiteSpace? COMMA whiteSpace? constSubStmt)*;
constSubStmt : identifier (whiteSpace asTypeClause)? whiteSpace? EQ whiteSpace? expression;
declareStmt : (visibility whiteSpace)? DECLARE whiteSpace (PTRSAFE whiteSpace)? (FUNCTION | SUB) whiteSpace identifier whiteSpace LIB whiteSpace STRINGLITERAL (whiteSpace ALIAS whiteSpace STRINGLITERAL)? (whiteSpace? argList)? (whiteSpace asTypeClause)?;
argList : LPAREN (whiteSpace? arg (whiteSpace? COMMA whiteSpace? arg)*)? whiteSpace? RPAREN;
arg : (OPTIONAL whiteSpace)? ((BYVAL | BYREF) whiteSpace)? (PARAMARRAY whiteSpace)? unrestrictedIdentifier (whiteSpace? LPAREN whiteSpace? RPAREN)? (whiteSpace? asTypeClause)? (whiteSpace? argDefaultValue)?;
argDefaultValue : EQ whiteSpace? expression;
// 5.2.2 Implicit Definition Directives
defDirective : defType whiteSpace letterSpec (whiteSpace? COMMA whiteSpace? letterSpec)*;
defType :
DEFBOOL | DEFBYTE | DEFINT | DEFLNG | DEFLNGLNG | DEFLNGPTR | DEFCUR |
DEFSNG | DEFDBL | DEFDATE |
DEFSTR | DEFOBJ | DEFVAR
;
// universalLetterRange must appear before letterRange because they both match the same amount in the case of A-Z but we prefer the universalLetterRange.
// singleLetter must appear at the end to prevent premature bailout
letterSpec : universalLetterRange | letterRange | singleLetter;
singleLetter : {_input.Lt(1).Text.Length == 1 && Regex.Match(_input.Lt(1).Text, @"[a-zA-Z]").Success}? IDENTIFIER;
// We make a separate universalLetterRange rule because it is treated specially in VBA. This makes it easy for users of the parser
// to identify this case. Quoting MS VBAL:
// "A <universal-letter-range> defines a single implicit declared type for every <IDENTIFIER> within
// a module, even those with a first character that would otherwise fall outside this range if it was
// interpreted as a <letter-range> from A-Z.""
universalLetterRange : {_input.Lt(1).Text.Equals("A") && _input.Lt(3).Text.Equals("Z")}? IDENTIFIER MINUS IDENTIFIER;
letterRange : singleLetter MINUS singleLetter;
doLoopStmt :
DO endOfStatement
block
statementLabelDefinition? whiteSpace? LOOP
|
DO whiteSpace (WHILE | UNTIL) whiteSpace expression endOfStatement
block
statementLabelDefinition? whiteSpace? LOOP
|
DO endOfStatement
block
statementLabelDefinition? whiteSpace? LOOP whiteSpace (WHILE | UNTIL) whiteSpace expression
;
enumerationStmt:
(visibility whiteSpace)? ENUM whiteSpace identifier endOfStatement
enumerationStmt_Constant*
END_ENUM
;
enumerationStmt_Constant : identifier (whiteSpace? EQ whiteSpace? expression)? endOfStatement;
// We add "END" as a statement so that it does not get resolved to some nonsensical property.
endStmt : END;
eraseStmt : ERASE whiteSpace expression (whiteSpace? COMMA whiteSpace? expression)*;
errorStmt : ERROR whiteSpace expression;
eventStmt : (visibility whiteSpace)? EVENT whiteSpace identifier whiteSpace? argList;
exitStmt : EXIT_DO | EXIT_FOR | EXIT_FUNCTION | EXIT_PROPERTY | EXIT_SUB;
forEachStmt :
FOR whiteSpace EACH whiteSpace expression whiteSpace IN whiteSpace expression
(endOfStatement unterminatedBlock)?
(endOfStatement statementLabelDefinition? whiteSpace? NEXT (whiteSpace expression)?
| whiteSpace? COMMA whiteSpace? expression)
;
// expression EQ expression refactored to expression to allow SLL
forNextStmt :
FOR whiteSpace expression whiteSpace TO whiteSpace expression stepStmt? whiteSpace*
(endOfStatement unterminatedBlock)?
(endOfStatement statementLabelDefinition? whiteSpace? NEXT (whiteSpace expression)?
| whiteSpace? COMMA whiteSpace? expression)
;
stepStmt : whiteSpace STEP whiteSpace expression;
functionStmt :
(visibility whiteSpace)? (STATIC whiteSpace)? FUNCTION whiteSpace? functionName (whiteSpace? argList)? (whiteSpace? asTypeClause)? endOfStatement
block
statementLabelDefinition? whiteSpace? END_FUNCTION
(endOfLine attributeStmt)*
;
functionName : identifier;
goSubStmt : GOSUB whiteSpace expression;
goToStmt : GOTO whiteSpace expression;
// 5.4.2.8 If Statement
ifStmt :
IF whiteSpace booleanExpression whiteSpace THEN endOfStatement
block
(statementLabelDefinition? whiteSpace? elseIfBlock)*
(statementLabelDefinition? whiteSpace? elseBlock?)
statementLabelDefinition? whiteSpace? END_IF
;
elseIfBlock :
ELSEIF whiteSpace booleanExpression whiteSpace THEN endOfStatement block
| ELSEIF whiteSpace booleanExpression whiteSpace THEN whiteSpace? block
;
elseBlock :
ELSE endOfStatement block
;
// 5.4.2.9 Single-line If Statement
singleLineIfStmt : ifWithNonEmptyThen | ifWithEmptyThen;
ifWithNonEmptyThen : IF whiteSpace? booleanExpression whiteSpace? THEN whiteSpace? listOrLabel (whiteSpace singleLineElseClause)?;
ifWithEmptyThen : IF whiteSpace? booleanExpression whiteSpace? THEN endOfStatement? whiteSpace? singleLineElseClause;
singleLineElseClause : ELSE whiteSpace? listOrLabel?;
// lineNumberLabel should actually be "statement-label" according to MS VBAL but they only allow lineNumberLabels:
// A <statement-label> that occurs as the first element of a <list-or-label> element has the effect
// as if the <statement-label> was replaced with a <goto-statement> containing the same
// <statement-label>. This <goto-statement> takes the place of <line-number-label> in
// <statement-list>.
listOrLabel :
lineNumberLabel (whiteSpace? COLON whiteSpace? sameLineStatement?)*
| (COLON whiteSpace?)? sameLineStatement (whiteSpace? COLON whiteSpace? sameLineStatement?)*
;
sameLineStatement : mainBlockStmt;
booleanExpression : expression;
implementsStmt : IMPLEMENTS whiteSpace expression;
letStmt : (LET whiteSpace)? lExpression whiteSpace? EQ whiteSpace? expression;
lsetStmt : LSET whiteSpace expression whiteSpace? EQ whiteSpace? expression;
onErrorStmt : (ON_ERROR | ON_LOCAL_ERROR) whiteSpace (GOTO whiteSpace expression | RESUME whiteSpace NEXT);
onGoToStmt : ON whiteSpace expression whiteSpace GOTO whiteSpace expression (whiteSpace? COMMA whiteSpace? expression)*;
onGoSubStmt : ON whiteSpace expression whiteSpace GOSUB whiteSpace expression (whiteSpace? COMMA whiteSpace? expression)*;
propertyGetStmt :
(visibility whiteSpace)? (STATIC whiteSpace)? PROPERTY_GET whiteSpace functionName (whiteSpace? argList)? (whiteSpace asTypeClause)? endOfStatement
block
statementLabelDefinition? whiteSpace? END_PROPERTY
(endOfLine attributeStmt)*
;
propertySetStmt :
(visibility whiteSpace)? (STATIC whiteSpace)? PROPERTY_SET whiteSpace subroutineName (whiteSpace? argList)? endOfStatement
block
statementLabelDefinition? whiteSpace? END_PROPERTY
(endOfLine attributeStmt)*
;
propertyLetStmt :
(visibility whiteSpace)? (STATIC whiteSpace)? PROPERTY_LET whiteSpace subroutineName (whiteSpace? argList)? endOfStatement
block
statementLabelDefinition? whiteSpace? END_PROPERTY
(endOfLine attributeStmt)*
;
// 5.4.2.20 RaiseEvent Statement
raiseEventStmt : RAISEEVENT whiteSpace identifier (whiteSpace? LPAREN whiteSpace? eventArgumentList? whiteSpace? RPAREN)?;
eventArgumentList : eventArgument (whiteSpace? COMMA whiteSpace? eventArgument)*;
eventArgument : (BYVAL whiteSpace)? expression;
// 5.4.3.3 ReDim Statement
// To make the grammar non-ambiguous we treat redim statements as index expressions.
// For this to work the argumentList rule had to be changed to support "lower bound arguments", e.g. "1 To 10".
redimStmt : REDIM whiteSpace (PRESERVE whiteSpace)? redimDeclarationList;
redimDeclarationList : redimVariableDeclaration (whiteSpace? COMMA whiteSpace? redimVariableDeclaration)*;
redimVariableDeclaration : expression (whiteSpace asTypeClause)?;
// 5.4.3.5 Mid/MidB/Mid$/MidB$ Statement
// This needs to be explicitly defined to distinguish between Mid as a function and Mid as a keyword.
midStatement : modeSpecifier
LPAREN whiteSpace?
lExpression whiteSpace? COMMA whiteSpace? expression whiteSpace? (COMMA whiteSpace? expression whiteSpace?)?
RPAREN
whiteSpace? EQ whiteSpace?
expression;
modeSpecifier : (MID | MIDB) DOLLAR? ;
integerExpression : expression;
callStmt :
CALL whiteSpace lExpression
| lExpression (whiteSpace argumentList)?
;
resumeStmt : RESUME (whiteSpace (NEXT | expression))?;
returnStmt : RETURN;
rsetStmt : RSET whiteSpace expression whiteSpace? EQ whiteSpace? expression;
// 5.4.2.11 Stop Statement
stopStmt : STOP;
nameStmt : NAME whiteSpace expression whiteSpace AS whiteSpace expression;
// 5.4.2.10 Select Case Statement
selectCaseStmt :
SELECT whiteSpace? CASE whiteSpace? selectExpression endOfStatement
(statementLabelDefinition? whiteSpace? caseClause)*
statementLabelDefinition? whiteSpace? caseElseClause?
statementLabelDefinition? whiteSpace? END_SELECT
;
selectExpression : expression;
caseClause :
CASE whiteSpace rangeClause (whiteSpace? COMMA whiteSpace? rangeClause)* endOfStatement block
;
caseElseClause : CASE whiteSpace? ELSE endOfStatement block;
rangeClause :
(IS whiteSpace?)? comparisonOperator whiteSpace? expression
| selectStartValue whiteSpace TO whiteSpace selectEndValue
| expression
;
selectStartValue : expression;
selectEndValue : expression;
setStmt : SET whiteSpace lExpression whiteSpace? EQ whiteSpace? expression;
subStmt :
(visibility whiteSpace)? (STATIC whiteSpace)? SUB whiteSpace? subroutineName (whiteSpace? argList)? endOfStatement
block
statementLabelDefinition? whiteSpace? END_SUB
(endOfLine attributeStmt)*
;
subroutineName : identifier;
// 5.2.3.3 User Defined Type Declarations
// member list includes trailing endOfStatement
udtDeclaration : (visibility whiteSpace)? TYPE whiteSpace untypedIdentifier endOfStatement udtMemberList END_TYPE;
udtMemberList : (udtMember endOfStatement)+;
udtMember : reservedNameMemberDeclaration | untypedNameMemberDeclaration;
untypedNameMemberDeclaration : untypedIdentifier whiteSpace? optionalArrayClause;
reservedNameMemberDeclaration : unrestrictedIdentifier whiteSpace asTypeClause;
optionalArrayClause : (arrayDim whiteSpace)? asTypeClause;
// 5.2.3.1.3 Array Dimensions and Bounds
arrayDim : LPAREN whiteSpace? boundsList? whiteSpace? RPAREN;
boundsList : dimSpec (whiteSpace? COMMA whiteSpace? dimSpec)*;
dimSpec : (lowerBound whiteSpace?)? upperBound;
lowerBound : constantExpression whiteSpace? TO;
upperBound : constantExpression;
constantExpression : expression;
variableStmt : (DIM | STATIC | visibility) whiteSpace (WITHEVENTS whiteSpace)? variableListStmt;
variableListStmt : variableSubStmt (whiteSpace? COMMA whiteSpace? variableSubStmt)*;
variableSubStmt : identifier (whiteSpace? LPAREN whiteSpace? (subscripts whiteSpace?)? RPAREN)? (whiteSpace asTypeClause)?;
whileWendStmt :
WHILE whiteSpace expression endOfStatement
block
statementLabelDefinition? whiteSpace? WEND
;
withStmt :
WITH whiteSpace expression endOfStatement
block
statementLabelDefinition? whiteSpace? END_WITH
;
// Special forms with special syntax, only available in VBA reports or VB6 forms and pictureboxes.
lineSpecialForm : expression whiteSpace ((STEP whiteSpace?)? tuple)? MINUS (STEP whiteSpace?)? tuple whiteSpace? (COMMA whiteSpace? expression)? whiteSpace? (COMMA whiteSpace? lineSpecialFormOption)?;
circleSpecialForm : (expression whiteSpace? DOT whiteSpace?)? CIRCLE whiteSpace (STEP whiteSpace?)? tuple (whiteSpace? COMMA whiteSpace? expression)+;
scaleSpecialForm : (expression whiteSpace? DOT whiteSpace?)? SCALE whiteSpace tuple whiteSpace? MINUS whiteSpace? tuple;
pSetSpecialForm : (expression whiteSpace? DOT whiteSpace?)? PSET (whiteSpace STEP)? whiteSpace? tuple whiteSpace? (COMMA whiteSpace? expression)?;
tuple : LPAREN whiteSpace? expression whiteSpace? COMMA whiteSpace? expression whiteSpace? RPAREN;
lineSpecialFormOption : {_input.Lt(1).Text.ToLower().Equals("b") || _input.Lt(1).Text.ToLower().Equals("bf")}? unrestrictedIdentifier;
subscripts : subscript (whiteSpace? COMMA whiteSpace? subscript)*;
subscript : (expression whiteSpace TO whiteSpace)? expression;
unrestrictedIdentifier : identifier | statementKeyword | markerKeyword;
legalLabelIdentifier : { !(new[]{DOEVENTS,END,CLOSE,ELSE,LOOP,NEXT,RANDOMIZE,REM,RESUME,RETURN,STOP,WEND}).Contains(_input.La(1))}? identifier | markerKeyword;
identifier : typedIdentifier | untypedIdentifier;
untypedIdentifier : identifierValue;
typedIdentifier : untypedIdentifier typeHint;
identifierValue : IDENTIFIER | keyword | foreignName;
foreignName : L_SQUARE_BRACKET foreignIdentifier* R_SQUARE_BRACKET;
foreignIdentifier : ~(L_SQUARE_BRACKET | R_SQUARE_BRACKET) | foreignName;
asTypeClause : AS whiteSpace? (NEW whiteSpace)? type (whiteSpace? fieldLength)?;
baseType : BOOLEAN | BYTE | CURRENCY | DATE | DOUBLE | INTEGER | LONG | LONGLONG | LONGPTR | SINGLE | STRING | VARIANT | ANY;
comparisonOperator : LT | LEQ | GT | GEQ | EQ | NEQ | IS | LIKE;
complexType :
// Literal Expression has to come before lExpression, otherwise it'll be classified as simple name expression instead.
literalExpression # ctLiteralExpr
| lExpression # ctLExpr
| builtInType # ctBuiltInTypeExpr
| LPAREN whiteSpace? complexType whiteSpace? RPAREN # ctParenthesizedExpr
| TYPEOF whiteSpace complexType # ctTypeofexpr // To make the grammar SLL, the type-of-is-expression is actually the child of an IS relational op.
| NEW whiteSpace complexType # ctNewExpr
| HASH expression # ctMarkedFileNumberExpr // Added to support special forms such as Input(file1, #file1)
;
fieldLength : MULT whiteSpace? (numberLiteral | identifierValue);
//Statement labels can only appear at the start of a line.
statementLabelDefinition : {_input.La(-1) == NEWLINE || _input.La(-1) == LINE_CONTINUATION}? (combinedLabels | identifierStatementLabel | standaloneLineNumberLabel);
identifierStatementLabel : legalLabelIdentifier whiteSpace? COLON;
standaloneLineNumberLabel :
lineNumberLabel whiteSpace? COLON
| lineNumberLabel;
combinedLabels : lineNumberLabel whiteSpace identifierStatementLabel;
lineNumberLabel : numberLiteral;
numberLiteral : HEXLITERAL | OCTLITERAL | FLOATLITERAL | INTEGERLITERAL;
type : (baseType | complexType) (whiteSpace? LPAREN whiteSpace? RPAREN)?;
typeHint : PERCENT | AMPERSAND | POW | EXCLAMATIONPOINT | HASH | AT | DOLLAR;
visibility : PRIVATE | PUBLIC | FRIEND | GLOBAL;
// 5.6 Expressions
expression :
// Literal Expression has to come before lExpression, otherwise it'll be classified as simple name expression instead.
whiteSpace? LPAREN whiteSpace? expression whiteSpace? RPAREN # parenthesizedExpr
| TYPEOF whiteSpace expression # typeofexpr // To make the grammar SLL, the type-of-is-expression is actually the child of an IS relational op.
| HASH expression # markedFileNumberExpr // Added to support special forms such as Input(file1, #file1)
| NEW whiteSpace expression # newExpr
| expression whiteSpace? POW whiteSpace? expression # powOp
| MINUS whiteSpace? expression # unaryMinusOp
| expression whiteSpace? (MULT | DIV) whiteSpace? expression # multOp
| expression whiteSpace? INTDIV whiteSpace? expression # intDivOp
| expression whiteSpace? MOD whiteSpace? expression # modOp
| expression whiteSpace? (PLUS | MINUS) whiteSpace? expression # addOp
| expression whiteSpace? AMPERSAND whiteSpace? expression # concatOp
| expression whiteSpace? (EQ | NEQ | LT | GT | LEQ | GEQ | LIKE | IS) whiteSpace? expression # relationalOp
| NOT whiteSpace? expression # logicalNotOp
| expression whiteSpace? AND whiteSpace? expression # logicalAndOp
| expression whiteSpace? OR whiteSpace? expression # logicalOrOp
| expression whiteSpace? XOR whiteSpace? expression # logicalXorOp
| expression whiteSpace? EQV whiteSpace? expression # logicalEqvOp
| expression whiteSpace? IMP whiteSpace? expression # logicalImpOp
| literalExpression # literalExpr
| lExpression # lExpr
| builtInType # builtInTypeExpr
;
// 5.6.5 Literal Expressions
literalExpression :
numberLiteral
| DATELITERAL
| STRINGLITERAL
| literalIdentifier typeHint?
;
literalIdentifier : booleanLiteralIdentifier | objectLiteralIdentifier | variantLiteralIdentifier;
booleanLiteralIdentifier : TRUE | FALSE;
objectLiteralIdentifier : NOTHING;
variantLiteralIdentifier : EMPTY | NULL;
lExpression :
lExpression LPAREN whiteSpace? argumentList? whiteSpace? RPAREN # indexExpr
| lExpression mandatoryLineContinuation? DOT mandatoryLineContinuation? unrestrictedIdentifier # memberAccessExpr
| lExpression mandatoryLineContinuation? EXCLAMATIONPOINT mandatoryLineContinuation? unrestrictedIdentifier # dictionaryAccessExpr
| ME # instanceExpr
| identifier # simpleNameExpr
| DOT mandatoryLineContinuation? unrestrictedIdentifier # withMemberAccessExpr
| EXCLAMATIONPOINT mandatoryLineContinuation? unrestrictedIdentifier # withDictionaryAccessExpr
| lExpression mandatoryLineContinuation whiteSpace? LPAREN whiteSpace? argumentList? whiteSpace? RPAREN # whitespaceIndexExpr
;
// 3.3.5.3 Special Identifier Forms
builtInType :
baseType
| L_SQUARE_BRACKET whiteSpace? baseType whiteSpace? R_SQUARE_BRACKET
| OBJECT
| L_SQUARE_BRACKET whiteSpace? OBJECT whiteSpace? R_SQUARE_BRACKET
;
// 5.6.13.1 Argument Lists
argumentList :
whiteSpace? (argument? (whiteSpace? COMMA whiteSpace? argument)*)??
;
requiredArgument : argument;
argument :
positionalArgument
| namedArgument
| missingArgument
;
positionalArgument : argumentExpression;
namedArgument : unrestrictedIdentifier whiteSpace? ASSIGN whiteSpace? argumentExpression;
missingArgument : ;
argumentExpression :
(BYVAL whiteSpace)? expression
| addressOfExpression
// Special case for redim statements. The resolver doesn't have to deal with this because it is "picked apart" in the redim statement.
| lowerBoundArgumentExpression whiteSpace TO whiteSpace upperBoundArgumentExpression
;
lowerBoundArgumentExpression : expression;
upperBoundArgumentExpression : expression;
// 5.6.16.8 AddressOf Expressions
addressOfExpression : ADDRESSOF whiteSpace expression;
keyword :
ABS
| ADDRESSOF
| ALIAS
| AND
| ANY
| ARRAY
| ATTRIBUTE
| BEGIN
| BEGINPROPERTY
| BOOLEAN
| BYREF
| BYTE
| BYVAL
| CBOOL
| CBYTE
| CCUR
| CDATE
| CDBL
| CDEC
| CINT
| CLASS
| CLNG
| CLNGLNG
| CLNGPTR
| CSNG
| CSTR
| CURRENCY
| CVAR
| CVERR
| DATABASE
| DATE
| DEBUG
| DOEVENTS
| DOUBLE
| END
| ENDPROPERTY
| EQV
| FALSE
| FIX
| IMP
| IN
| INPUTB
| INT
| INTEGER
| IS
| LBOUND
| LEN
| LEN
| LENB
| LIB
| LIKE
| LONG
| LONGLONG
| LONGPTR
| ME
| MID
| MIDB
| MOD
| NEW
| NOT
| NOTHING
| NULL
| OBJECT
| OPTIONAL
| OR
| PARAMARRAY
| PRESERVE
| PSET
| PTRSAFE
| REM
| SGN
| SINGLE
| SPC
| STRING
| TAB
| TEXT
| THEN
| TO
| TRUE
| TYPEOF
| UBOUND
| UNTIL
| VARIANT
| VERSION
| WITHEVENTS
| XOR
| STEP
| ON_ERROR
| ERROR
| APPEND
| BINARY
| OUTPUT
| RANDOM
| RANDOMIZE
| ACCESS
| READ
| WRITE
| READ_WRITE
| SHARED
| LOCK_READ
| LOCK_WRITE
| LOCK_READ_WRITE
| LINE_INPUT
| RESET
| WIDTH
| PRINT
| GET
| PUT
| CLOSE
| INPUT
| LOCK
| OPEN
| SEEK
| UNLOCK
| WRITE
| NAME
;
markerKeyword : AS;
statementKeyword :
CALL
| CASE
| CONST
| DECLARE
| DEFBOOL
| DEFBYTE
| DEFCUR
| DEFDATE
| DEFDBL
| DEFINT
| DEFLNG
| DEFLNGLNG
| DEFLNGPTR
| DEFOBJ
| DEFSNG
| DEFSTR
| DEFVAR
| DIM
| DO
| ELSE
| ELSEIF
| ENUM
| ERASE
| EVENT
| EXIT
| EXIT_DO
| EXIT_FOR
| EXIT_FUNCTION
| EXIT_PROPERTY
| EXIT_SUB
| END_SELECT
| END_WITH
| FOR
| FRIEND
| FUNCTION
| GLOBAL
| GOSUB
| GOTO
| IF
| IMPLEMENTS
| LET
| LOOP
| LSET
| NEXT
| ON
| OPTION
| PRIVATE
| PUBLIC
| RAISEEVENT
| REDIM
| RESUME
| RETURN
| RSET
| SELECT
| SET
| STATIC
| STOP
| SUB
| TYPE
| WEND
| WHILE
| WITH
;
endOfLine :
whiteSpace? NEWLINE whiteSpace?
| whiteSpace? commentOrAnnotation
;
// we expect endOfStatement to consume all trailing whitespace
endOfStatement :
(endOfLine whiteSpace? | (whiteSpace? COLON whiteSpace?))+
| whiteSpace? EOF
;
// Annotations must come before comments because of precedence. ANTLR4 matches as much as possible then chooses the one that comes first.
commentOrAnnotation :
(annotationList
| remComment
| comment)
// all comments must end with a logical line. See VBA Language Spec 3.3.1
(NEWLINE | EOF)
;
remComment : REM whiteSpace? commentBody;
comment : SINGLEQUOTE commentBody;
commentBody : (~NEWLINE)*;
annotationList : SINGLEQUOTE (AT annotation whiteSpace?)+ (COLON commentBody)?;
annotation : annotationName annotationArgList?;
annotationName : unrestrictedIdentifier;
annotationArgList :
whiteSpace annotationArg
| whiteSpace annotationArg (whiteSpace? COMMA whiteSpace? annotationArg)+
| whiteSpace? LPAREN whiteSpace? RPAREN
| whiteSpace? LPAREN whiteSpace? annotationArg whiteSpace? RPAREN
| whiteSpace? LPAREN annotationArg (whiteSpace? COMMA whiteSpace? annotationArg)+ whiteSpace? RPAREN;
annotationArg : expression;
mandatoryLineContinuation : LINE_CONTINUATION WS*;
whiteSpace : (WS | LINE_CONTINUATION)+;