-
Notifications
You must be signed in to change notification settings - Fork 21
/
Copy pathPLSQL.g4
766 lines (651 loc) · 13.9 KB
/
PLSQL.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
grammar PLSQL;
file
: ( create_object ( DIVIDE show_errors )? DIVIDE? )+ EOF
;
show_errors
: kSHOW kERRORS SEMI?
;
create_object
: create_package_body
| create_package_body
| create_function
| create_procedure
;
procedure_heading :
PROCEDURE ID parameter_declarations?
;
function_heading :
FUNCTION ID parameter_declarations? RETURN datatype
;
parameter_declarations :
( LPAREN parameter_declaration ( COMMA parameter_declaration )* RPAREN )
;
parameter_declaration :
ID ( IN | ( ( OUT | IN OUT ) NOCOPY? ) )? datatype
( ( ASSIGN | DEFAULT ) expression )?
;
declare_section :
( type_definition SEMI
| subtype_definition SEMI
| cursor_definition SEMI
| item_declaration SEMI
| function_declaration_or_definition SEMI
| procedure_declaration_or_definition SEMI
| pragma SEMI
)+
;
cursor_definition :
CURSOR ID parameter_declarations? IS select_statement
;
item_declaration
: variable_declaration
| constant_declaration
| exception_declaration
;
variable_declaration :
ID datatype ( ( NOT NULL )? ( ASSIGN | DEFAULT ) expression )?
;
constant_declaration :
ID CONSTANT datatype ( NOT NULL )? ( ASSIGN | DEFAULT ) expression
;
exception_declaration :
ID EXCEPTION
;
type_definition :
kTYPE ID IS ( record_type_definition | collection_type_definition | ref_cursor_type_definition )
;
subtype_definition :
SUBTYPE ID IS datatype ( NOT NULL )?
;
record_type_definition :
RECORD LPAREN record_field_declaration ( COMMA record_field_declaration )* RPAREN
;
record_field_declaration :
ID datatype ( ( NOT NULL )? ( ASSIGN | DEFAULT ) expression )?
;
collection_type_definition
: varray_type_definition
| nested_table_type_definition
;
varray_type_definition
: ( VARYING ARRAY? | VARRAY ) LPAREN numeric_literal RPAREN kOF datatype ( NOT NULL )?
;
nested_table_type_definition
: TABLE kOF datatype ( NOT NULL )? ( INDEX BY associative_index_type )?
;
associative_index_type
: datatype
;
ref_cursor_type_definition
: REF CURSOR ( RETURN datatype )?
;
datatype
: ( REF )? ID ( DOT ID )? ( LPAREN numeric_literal ( COMMA numeric_literal )* RPAREN | PERCENT ( kTYPE | ROWTYPE ) )?
;
function_declaration_or_definition :
function_heading
( DETERMINISTIC | PIPELINED | PARALLEL_ENABLE | RESULT_CACHE )*
( ( IS | AS ) declare_section? body )?
;
function_declaration :
function_heading
( DETERMINISTIC | PIPELINED | PARALLEL_ENABLE | RESULT_CACHE )*
;
function_definition :
function_heading
( DETERMINISTIC | PIPELINED | PARALLEL_ENABLE | RESULT_CACHE )*
( IS | AS ) declare_section? body
;
procedure_declaration_or_definition :
procedure_heading
( ( IS | AS ) declare_section? body )?
;
procedure_declaration :
procedure_heading
;
procedure_definition :
procedure_heading
( IS | AS ) declare_section? body
;
body :
BEGIN statement SEMI ( statement SEMI | pragma SEMI )*
( EXCEPTION exception_handler+ )? END ID?
;
exception_handler
: WHEN ( qual_id ( OR qual_id )* | OTHERS )
THEN ( statement SEMI )+
;
statement :
label*
( assign_or_call_statement
| case_statement
| close_statement
| continue_statement
| basic_loop_statement
| execute_immediate_statement
| exit_statement
| fetch_statement
| for_loop_statement
| forall_statement
| goto_statement
| if_statement
| null_statement
| open_statement
| plsql_block
| raise_statement
| return_statement
| sql_statement
| while_loop_statement
)
;
lvalue
: call ( DOT call )*
;
assign_or_call_statement
: lvalue ( DOT delete_call | ASSIGN expression )?
;
call
: COLON? ID ( LPAREN ( parameter ( COMMA parameter )* )? RPAREN )?
;
delete_call
: DELETE ( LPAREN parameter? RPAREN )?
;
basic_loop_statement :
LOOP ( statement SEMI )+ END LOOP label_name?
;
case_statement :
CASE expression?
( WHEN expression THEN ( statement SEMI )+ )+
( ELSE statement SEMI )?
END CASE label_name?
;
close_statement :
CLOSE ID ( DOT ID )?
;
continue_statement :
CONTINUE ( lbl=ID )? ( WHEN expression )?
;
execute_immediate_statement :
EXECUTE IMMEDIATE expression (
( into_clause | bulk_collect_into_clause) using_clause?
| using_clause dynamic_returning_clause?
| dynamic_returning_clause
)?
;
exit_statement :
EXIT ( lbl=ID )? ( WHEN expression )?
;
fetch_statement :
FETCH qual_id ( into_clause | bulk_collect_into_clause ( LIMIT numeric_expression )? )
;
into_clause :
INTO lvalue ( COMMA lvalue )*
;
bulk_collect_into_clause :
BULK COLLECT INTO lvalue ( COMMA lvalue )*
;
using_clause :
USING param_modifiers? expression ( COMMA param_modifiers? expression )*
;
param_modifiers
: IN OUT? | OUT
;
dynamic_returning_clause :
( RETURNING | RETURN ) ( into_clause | bulk_collect_into_clause )
;
for_loop_statement :
FOR ID IN ( ~(LOOP) )+ LOOP ( statement SEMI )+ END LOOP label_name?
;
forall_statement :
FORALL ID IN bounds_clause sql_statement ( kSAVE kEXCEPTIONS )?
;
bounds_clause
: numeric_expression DOUBLEDOT numeric_expression
| kINDICES kOF atom ( BETWEEN numeric_expression AND numeric_expression )?
| kVALUES kOF atom
;
goto_statement :
GOTO label_name
;
if_statement :
IF expression THEN ( statement SEMI )+
( ELSIF expression THEN ( statement SEMI )+ )*
( ELSE ( statement SEMI )+ )?
END IF
;
null_statement :
NULL
;
open_statement :
OPEN ID ( DOT ID )* call_args? ( FOR select_statement )?
;
pragma :
PRAGMA swallow_to_semi
;
raise_statement :
RAISE ( ID ( DOT ID )* )?
;
return_statement :
RETURN expression?
;
plsql_block :
( DECLARE declare_section )? body
;
label :
LLABEL label RLABEL
;
qual_id :
COLON? ID ( DOT COLON? ID )*
;
sql_statement
: commit_statement
| delete_statement
| insert_statement
| lock_table_statement
| rollback_statement
| savepoint_statement
| select_statement
| set_transaction_statement
| update_statement
;
commit_statement :
COMMIT swallow_to_semi?
;
delete_statement :
DELETE swallow_to_semi
;
insert_statement :
INSERT swallow_to_semi
;
lock_table_statement :
LOCK TABLE swallow_to_semi
;
rollback_statement :
ROLLBACK swallow_to_semi?
;
savepoint_statement :
SAVEPOINT ID
;
select_statement :
SELECT swallow_to_semi
;
set_transaction_statement :
SET TRANSACTION swallow_to_semi
;
update_statement :
UPDATE swallow_to_semi
;
swallow_to_semi :
~( SEMI )+
;
while_loop_statement :
WHILE expression LOOP ( statement SEMI )+ END LOOP label_name?
;
match_parens
: ( ~( RPAREN | LPAREN | SEMI | AS | IS | IN | OUT ) )* //options {greedy=false;} :
| RPAREN match_parens LPAREN
;
label_name: ID;
expression
: or_expr
;
or_expr
: and_expr ( OR and_expr )*
;
and_expr
: not_expr ( AND not_expr )*
;
not_expr
: NOT? compare_expr
;
compare_expr
: is_null_expr ( ( EQ | NOT_EQ | LTH | LEQ | GTH | GEQ ) is_null_expr )?
;
is_null_expr
: like_expr ( IS NOT? NULL)?
;
like_expr
: between_expr ( NOT? LIKE between_expr )?
;
between_expr
: in_expr ( NOT? BETWEEN in_expr AND in_expr )?
;
in_expr
: add_expr ( NOT? IN LPAREN add_expr ( COMMA add_expr )* RPAREN )?
;
numeric_expression
: add_expr
;
add_expr
: mul_expr ( ( MINUS | PLUS | DOUBLEVERTBAR ) mul_expr )*
;
mul_expr
: unary_sign_expr ( ( ASTERISK | DIVIDE | kMOD ) unary_sign_expr )*
;
unary_sign_expr
: ( MINUS | PLUS )? exponent_expr
;
exponent_expr
: atom ( EXPONENT atom )?
;
atom
: variable_or_function_call ( PERCENT attribute )?
| SQL PERCENT attribute
| string_literal
| numeric_atom
| boolean_atom
| NULL
| LPAREN expression RPAREN
;
variable_or_function_call
: call ( DOT call )* ( DOT delete_call )?
;
attribute
: BULK_ROWCOUNT LPAREN expression RPAREN
| kFOUND
| ISOPEN
| NOTFOUND
| kROWCOUNT
;
call_args
: LPAREN ( parameter ( COMMA parameter )* )? RPAREN
;
boolean_atom
: boolean_literal
| collection_exists
| conditional_predicate
;
numeric_atom
: numeric_literal
;
numeric_literal
: INTEGER
| REAL_NUMBER
;
boolean_literal
: TRUE
| FALSE
;
string_literal
: QUOTED_STRING
;
collection_exists
: ID DOT EXISTS LPAREN expression RPAREN
;
conditional_predicate
: INSERTING
| UPDATING ( LPAREN QUOTED_STRING RPAREN )?
| DELETING
;
parameter
: ( ID ARROW )? expression
;
index
: expression
;
create_package :
CREATE ( OR kREPLACE )? PACKAGE ( schema_name=ID DOT )? package_name=ID
( invoker_rights_clause )?
( IS | AS ) ( declare_section )? END ( ID )? SEMI
;
create_package_body :
CREATE ( OR kREPLACE )? PACKAGE BODY ( schema_name=ID DOT )? package_name=ID
( IS | AS ) ( declare_section )?
( initialize_section=body | END ( package_name2=ID )? )
SEMI
;
create_procedure :
CREATE ( OR kREPLACE )? PROCEDURE ( schema_name=ID DOT )? procedure_name=ID
( LPAREN parameter_declaration ( COMMA parameter_declaration )* RPAREN )?
invoker_rights_clause?
( IS | AS )
( declare_section? body
| call_spec
| EXTERNAL
) SEMI
;
create_function :
CREATE ( OR kREPLACE )? FUNCTION ( schema_name=ID DOT )? function_name=ID
( LPAREN parameter_declaration ( COMMA parameter_declaration )* RPAREN )?
RETURN datatype
invoker_rights_clause?
( IS | AS )
( declare_section? body
| call_spec
| EXTERNAL
) SEMI
;
invoker_rights_clause :
AUTHID ( CURRENT_USER | DEFINER )
;
call_spec
: LANGUAGE swallow_to_semi
;
kERRORS : {_input.LT(1).getText().length() >= 3 && "errors".startsWith(_input.LT(1).getText().toLowerCase())}? ID;
kEXCEPTIONS : {_input.LT(1).getText().equalsIgnoreCase("exceptions")}? ID;
kFOUND : {_input.LT(1).getText().equalsIgnoreCase("found")}? ID;
kINDICES : {_input.LT(1).getText().equalsIgnoreCase("indices")}? ID;
kMOD : {_input.LT(1).getText().equalsIgnoreCase("mod")}? ID;
kNAME : {_input.LT(1).getText().equalsIgnoreCase("name")}? ID;
kOF : {_input.LT(1).getText().equalsIgnoreCase("of")}? ID;
kREPLACE : {_input.LT(1).getText().equalsIgnoreCase("replace")}? ID;
kROWCOUNT : {_input.LT(1).getText().equalsIgnoreCase("rowcount")}? ID;
kSAVE : {_input.LT(1).getText().equalsIgnoreCase("save")}? ID;
kSHOW : {_input.LT(1).getText().equalsIgnoreCase("show")}? ID;
kTYPE : {_input.LT(1).getText().equalsIgnoreCase("type")}? ID;
kVALUES : {_input.LT(1).getText().equalsIgnoreCase("values")}? ID;
AND : 'and' ;
ARRAY : 'array' ;
AS : 'as' ;
AUTHID: 'authid';
BETWEEN : 'between' ;
BODY : 'body';
BULK: 'bulk';
BULK_ROWCOUNT: 'bulk_rowcount';
BY : 'by';
CASE: 'case';
CREATE: 'create';
COLLECT: 'collect';
COMMIT : 'commit';
CURRENT_USER: 'current_user';
DEFAULT : 'default' ;
DEFINER: 'definer';
DELETE : 'delete';
ELSE : 'else' ;
ELSIF : 'elsif';
EXTERNAL: 'external';
FALSE : 'false';
FETCH : 'fetch';
FOR : 'for' ;
FORALL : 'forall' ;
GOTO : 'goto';
IF : 'if';
IN : 'in' ;
INDEX : 'index' ;
INSERT : 'insert';
INTO : 'into';
IS : 'is' ;
LANGUAGE: 'language';
LIKE : 'like' ;
LIMIT : 'limit' ;
LOCK : 'lock';
NOT : 'not' ;
NOTFOUND: 'notfound';
NULL : 'null' ;
OPEN : 'open';
OR : 'or' ;
PACKAGE: 'package';
RAISE : 'raise';
ROLLBACK: 'rollback';
SAVEPOINT : 'savepoint';
SELECT : 'select';
SET : 'set';
SQL : 'sql';
TABLE : 'table';
TRANSACTION : 'transaction';
TRUE : 'true';
THEN : 'then' ;
UPDATE : 'update';
WHILE : 'while';
INSERTING
: 'inserting';
UPDATING: 'updating';
DELETING: 'deleting';
ISOPEN : 'isopen';
EXISTS : 'exists';
BEGIN : 'begin' ;
CLOSE : 'close';
CONSTANT : 'constant' ;
CONTINUE: 'continue';
CURSOR : 'cursor' ;
DECLARE : 'declare' ;
DETERMINISTIC : 'deterministic' ;
END : 'end' ;
EXCEPTION : 'exception' ;
EXECUTE : 'execute';
EXIT : 'exit';
FUNCTION : 'function' ;
IMMEDIATE : 'immediate';
LOOP : 'loop';
NOCOPY : 'nocopy' ;
OTHERS : 'others' ;
OUT : 'out' ;
PARALLEL_ENABLE : 'parallel_enable';
PIPELINED : 'pipelined' ;
PRAGMA : 'pragma' ;
PROCEDURE : 'procedure' ;
RECORD : 'record' ;
REF : 'ref' ;
RESULT_CACHE : 'result_cache' ;
RETURN : 'return' ;
RETURNING : 'returning' ;
ROWTYPE : 'rowtype' ;
SUBTYPE : 'subtype' ;
USING: 'using' ;
VARRAY : 'varray' ;
VARYING : 'varying' ;
WHEN : 'when' ;
QUOTED_STRING
: ( 'n' )? '\'' ( '\'\'' | ~('\'') )* '\''
;
ID
: ( 'a' .. 'z' )
( 'a' .. 'z' | '0' .. '9' | '_' | '$' | '#' )*
| DOUBLEQUOTED_STRING
;
SEMI
: ';'
;
COLON
: ':'
;
DOUBLEDOT
: POINT POINT
;
DOT
: POINT
;
fragment
POINT
: '.'
;
COMMA
: ','
;
EXPONENT
: '**'
;
ASTERISK
: '*'
;
AT_SIGN
: '@'
;
RPAREN
: ')'
;
LPAREN
: '('
;
RBRACK
: ']'
;
LBRACK
: '['
;
PLUS
: '+'
;
MINUS
: '-'
;
DIVIDE
: '/'
;
EQ
: '='
;
PERCENT
: '%'
;
LLABEL
: '<<'
;
RLABEL
: '>>'
;
ASSIGN
: ':='
;
ARROW
: '=>'
;
VERTBAR
: '|'
;
DOUBLEVERTBAR
: '||'
;
NOT_EQ
: '<>' | '!=' | '~='| '^='
;
LTH
: '<'
;
LEQ
: '<='
;
GTH
: '>'
;
GEQ
: '>='
;
INTEGER
: N
;
REAL_NUMBER
: NUMBER_VALUE ( 'e' ( PLUS | MINUS )? N )?
;
fragment
NUMBER_VALUE
: N POINT N //{numberDotValid()}?=> N POINT N?
| POINT N
| N
;
fragment
N
: '0' .. '9' ( '0' .. '9' )*
;
fragment
DOUBLEQUOTED_STRING
: '"' ( ~('"') )* '"'
;
WS : (' '|'\r'|'\t'|'\n') {setChannel(HIDDEN);}
;
SL_COMMENT
: '--' ~('\n'|'\r')* '\r'? '\n' {setChannel(HIDDEN);}
;
ML_COMMENT
: '/*' ( . )*? '*/' {setChannel(HIDDEN);} // options {greedy=false;} :
;