-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathMongoShellParser.g4
More file actions
1096 lines (960 loc) · 30.5 KB
/
MongoShellParser.g4
File metadata and controls
1096 lines (960 loc) · 30.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
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
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* MongoDB Shell (mongosh) Parser Grammar
* For use with ANTLR 4
*
* Supports all mongosh commands:
* - Collection methods (48): find, insertOne, updateMany, aggregate, etc.
* - Cursor methods (34): sort, limit, skip, forEach, toArray, etc.
* - Database methods (39): createCollection, dropDatabase, stats, etc.
* - Bulk operations (21): initializeOrderedBulkOp, Bulk.insert, etc.
* - Connection methods (16): Mongo, connect, getDB, setReadPref, etc.
* - Replication methods (14): rs.status, rs.initiate, rs.add, etc.
* - Sharding methods (52): sh.status, sh.enableSharding, sh.shardCollection, etc.
* - User management (12): createUser, dropUser, auth, etc.
* - Role management (10): createRole, dropRole, grantRolesToUser, etc.
* - Encryption methods (17): KeyVault, ClientEncryption, getKeyVault, etc.
* - Native methods (16): cat, load, quit, pwd, etc.
* - Object constructors (18): ObjectId, ISODate, UUID, BinData, etc.
* - Query plan cache (5): getPlanCache, clear, list, etc.
* - Atlas-specific (13): Search Index (4), Stream Processing (9)
* - Shell commands: show dbs, show databases, show collections
* - Document syntax with unquoted keys and trailing commas
*/
parser grammar MongoShellParser;
options { tokenVocab=MongoShellLexer; }
// Entry point - a program can contain multiple statements
program
: statement* EOF
;
// A statement is either a shell command, a database statement, a bulk statement, a connection statement, a replica set statement, a sharding statement, an encryption statement, a plan cache statement, a stream processing statement, or a native function call
statement
: shellCommand SEMI?
| dbStatement SEMI?
| bulkStatement SEMI?
| connectionStatement SEMI?
| rsStatement SEMI?
| shStatement SEMI?
| encryptionStatement SEMI?
| planCacheStatement SEMI?
| spStatement SEMI?
| nativeFunctionCall SEMI?
;
// Shell commands: show dbs, show databases, show collections
shellCommand
: SHOW (DBS | DATABASES) # showDatabases
| SHOW COLLECTIONS # showCollections
;
// Database statements: db.collection.method(...) or db.getCollectionNames() or db.getCollectionInfos()
dbStatement
: DB DOT GET_COLLECTION_NAMES LPAREN RPAREN methodChain? # getCollectionNames
| DB DOT GET_COLLECTION_INFOS LPAREN arguments? RPAREN methodChain? # getCollectionInfos
| DB DOT CREATE_COLLECTION LPAREN arguments RPAREN # createCollection
| DB DOT DROP_DATABASE LPAREN RPAREN # dropDatabase
| DB DOT STATS LPAREN argument? RPAREN # dbStats
| DB DOT SERVER_STATUS LPAREN argument? RPAREN # serverStatus
| DB DOT SERVER_BUILD_INFO LPAREN RPAREN # serverBuildInfo
| DB DOT VERSION LPAREN RPAREN # dbVersion
| DB DOT HOST_INFO LPAREN RPAREN # hostInfo
| DB DOT LIST_COMMANDS LPAREN RPAREN # listCommands
| DB DOT RUN_COMMAND LPAREN arguments RPAREN # runCommand
| DB DOT ADMIN_COMMAND LPAREN arguments RPAREN # adminCommand
| DB DOT GET_NAME LPAREN RPAREN # getName
| DB DOT GET_MONGO LPAREN RPAREN # getMongo
| DB DOT GET_SIBLING_DB LPAREN argument RPAREN # getSiblingDB
| DB DOT AGGREGATE LPAREN arguments? RPAREN # dbAggregate
| DB DOT AUTH LPAREN arguments? RPAREN # dbAuth
| DB DOT CHANGE_USER_PASSWORD LPAREN arguments? RPAREN # dbChangeUserPassword
| DB DOT CLONE_DATABASE LPAREN arguments? RPAREN # dbCloneDatabase
| DB DOT COMMAND_HELP LPAREN arguments? RPAREN # dbCommandHelp
| DB DOT COPY_DATABASE LPAREN arguments? RPAREN # dbCopyDatabase
| DB DOT CREATE_ROLE LPAREN arguments? RPAREN # dbCreateRole
| DB DOT CREATE_USER LPAREN arguments? RPAREN # dbCreateUser
| DB DOT CREATE_VIEW LPAREN arguments? RPAREN # dbCreateView
| DB DOT CURRENT_OP LPAREN arguments? RPAREN # dbCurrentOp
| DB DOT DROP_ALL_ROLES LPAREN arguments? RPAREN # dbDropAllRoles
| DB DOT DROP_ALL_USERS LPAREN arguments? RPAREN # dbDropAllUsers
| DB DOT DROP_ROLE LPAREN arguments? RPAREN # dbDropRole
| DB DOT DROP_USER LPAREN arguments? RPAREN # dbDropUser
| DB DOT FSYNC_LOCK LPAREN arguments? RPAREN # dbFsyncLock
| DB DOT FSYNC_UNLOCK LPAREN arguments? RPAREN # dbFsyncUnlock
| DB DOT GET_LOG_COMPONENTS LPAREN arguments? RPAREN # dbGetLogComponents
| DB DOT GET_PROFILING_LEVEL LPAREN arguments? RPAREN # dbGetProfilingLevel
| DB DOT GET_PROFILING_STATUS LPAREN arguments? RPAREN # dbGetProfilingStatus
| DB DOT GET_REPLICATION_INFO LPAREN arguments? RPAREN # dbGetReplicationInfo
| DB DOT GET_ROLE LPAREN arguments? RPAREN # dbGetRole
| DB DOT GET_ROLES LPAREN arguments? RPAREN # dbGetRoles
| DB DOT GET_USER LPAREN arguments? RPAREN # dbGetUser
| DB DOT GET_USERS LPAREN arguments? RPAREN # dbGetUsers
| DB DOT GRANT_PRIVILEGES_TO_ROLE LPAREN arguments? RPAREN # dbGrantPrivilegesToRole
| DB DOT GRANT_ROLES_TO_ROLE LPAREN arguments? RPAREN # dbGrantRolesToRole
| DB DOT GRANT_ROLES_TO_USER LPAREN arguments? RPAREN # dbGrantRolesToUser
| DB DOT HELLO LPAREN arguments? RPAREN # dbHello
| DB DOT IS_MASTER LPAREN arguments? RPAREN # dbIsMaster
| DB DOT KILL_OP LPAREN arguments? RPAREN # dbKillOp
| DB DOT LOGOUT LPAREN arguments? RPAREN # dbLogout
| DB DOT PRINT_COLLECTION_STATS LPAREN arguments? RPAREN # dbPrintCollectionStats
| DB DOT PRINT_REPLICATION_INFO LPAREN arguments? RPAREN # dbPrintReplicationInfo
| DB DOT PRINT_SECONDARY_REPLICATION_INFO LPAREN arguments? RPAREN # dbPrintSecondaryReplicationInfo
| DB DOT PRINT_SHARDING_STATUS LPAREN arguments? RPAREN # dbPrintShardingStatus
| DB DOT PRINT_SLAVE_REPLICATION_INFO LPAREN arguments? RPAREN # dbPrintSlaveReplicationInfo
| DB DOT REVOKE_PRIVILEGES_FROM_ROLE LPAREN arguments? RPAREN # dbRevokePrivilegesFromRole
| DB DOT REVOKE_ROLES_FROM_ROLE LPAREN arguments? RPAREN # dbRevokeRolesFromRole
| DB DOT REVOKE_ROLES_FROM_USER LPAREN arguments? RPAREN # dbRevokeRolesFromUser
| DB DOT ROTATE_CERTIFICATES LPAREN arguments? RPAREN # dbRotateCertificates
| DB DOT SET_LOG_LEVEL LPAREN arguments? RPAREN # dbSetLogLevel
| DB DOT SET_PROFILING_LEVEL LPAREN arguments? RPAREN # dbSetProfilingLevel
| DB DOT SET_SECONDARY_OK LPAREN arguments? RPAREN # dbSetSecondaryOk
| DB DOT SET_WRITE_CONCERN LPAREN arguments? RPAREN # dbSetWriteConcern
| DB DOT SHUTDOWN_SERVER LPAREN arguments? RPAREN # dbShutdownServer
| DB DOT UPDATE_ROLE LPAREN arguments? RPAREN # dbUpdateRole
| DB DOT UPDATE_USER LPAREN arguments? RPAREN # dbUpdateUser
| DB DOT WATCH LPAREN arguments? RPAREN # dbWatch
| DB collectionAccess methodChain # collectionOperation
;
// Bulk operation statements
// Pattern: db.collection.initializeOrderedBulkOp().find(...).update(...).execute()
bulkStatement
: DB collectionAccess DOT bulkInitMethod bulkMethodChain?
;
bulkInitMethod
: INITIALIZE_ORDERED_BULK_OP LPAREN RPAREN
| INITIALIZE_UNORDERED_BULK_OP LPAREN RPAREN
;
bulkMethodChain
: (DOT bulkMethod)+
;
bulkMethod
: FIND LPAREN argument RPAREN # bulkFind
| INSERT LPAREN argument RPAREN # bulkInsert
| REMOVE LPAREN RPAREN # bulkRemove
| EXECUTE LPAREN argument? RPAREN # bulkExecute
| GET_OPERATIONS LPAREN RPAREN # bulkGetOperations
| TO_STRING LPAREN RPAREN # bulkToString
| identifier LPAREN arguments? RPAREN # bulkGenericMethod
;
// Connection statements - top-level Mongo() constructor and connect() function
connectionStatement
: MONGO LPAREN arguments? RPAREN connectionMethodChain? # mongoConnection
| CONNECT LPAREN arguments? RPAREN connectionMethodChain? # connectCall
| DB DOT GET_MONGO LPAREN RPAREN connectionMethodChain # dbGetMongoChain
;
// Connection method chain for chaining methods on a connection
connectionMethodChain
: (DOT connectionMethod)+
;
// Replication (replica set) statements - rs.method()
rsStatement
: RS DOT identifier LPAREN arguments? RPAREN
;
// Sharding statements - sh.method()
shStatement
: SH DOT identifier LPAREN arguments? RPAREN
;
// Encryption statements - db.getMongo().getKeyVault().xxx() or db.getMongo().getClientEncryption().xxx()
encryptionStatement
: DB DOT GET_MONGO LPAREN RPAREN DOT GET_KEY_VAULT LPAREN RPAREN (DOT identifier LPAREN arguments? RPAREN)* # keyVaultStatement
| DB DOT GET_MONGO LPAREN RPAREN DOT GET_CLIENT_ENCRYPTION LPAREN RPAREN (DOT identifier LPAREN arguments? RPAREN)* # clientEncryptionStatement
;
// Plan cache statements - db.collection.getPlanCache().xxx()
planCacheStatement
: DB collectionAccess DOT GET_PLAN_CACHE LPAREN RPAREN (DOT identifier LPAREN arguments? RPAREN)*
;
// Stream processing statements - sp.method() or sp.processor.method()
spStatement
: SP DOT identifier LPAREN arguments? RPAREN
| SP DOT identifier DOT identifier LPAREN arguments? RPAREN
;
// Native shell function calls - top-level functions like cat(), load(), quit()
nativeFunctionCall
: identifier LPAREN arguments? RPAREN
;
// Connection methods that can be called on a Mongo connection object
connectionMethod
: GET_DB LPAREN argument RPAREN # connGetDB
| GET_READ_CONCERN LPAREN RPAREN # connGetReadConcern
| GET_READ_PREF LPAREN RPAREN # connGetReadPref
| GET_READ_PREF_MODE LPAREN RPAREN # connGetReadPrefMode
| GET_READ_PREF_TAG_SET LPAREN RPAREN # connGetReadPrefTagSet
| GET_WRITE_CONCERN LPAREN RPAREN # connGetWriteConcern
| SET_READ_PREF LPAREN arguments RPAREN # connSetReadPref
| SET_READ_CONCERN LPAREN argument RPAREN # connSetReadConcern
| SET_WRITE_CONCERN LPAREN argument RPAREN # connSetWriteConcern
| START_SESSION LPAREN argument? RPAREN # connStartSession
| WATCH LPAREN arguments? RPAREN # connWatch
| CLOSE LPAREN RPAREN # connClose
| ADMIN_COMMAND LPAREN arguments RPAREN # connAdminCommand
| GET_DB_NAMES LPAREN RPAREN # connGetDBNames
| identifier LPAREN arguments? RPAREN # connGenericMethod
;
// Collection access patterns
collectionAccess
: DOT identifier # dotAccess
| LBRACKET stringLiteral RBRACKET # bracketAccess
| DOT GET_COLLECTION LPAREN stringLiteral RPAREN # getCollectionAccess
;
// Method chain: first call must be a collection method, subsequent calls are cursor methods.
// Special case: explain() returns an explainable object that supports collection methods.
methodChain
: DOT collectionExplainMethod DOT collectionMethodCall (DOT cursorMethodCall)*
| DOT collectionMethodCall (DOT cursorMethodCall)*
;
// Collection method call: methods that operate on a collection directly
collectionMethodCall
: findMethod
| findOneMethod
| countDocumentsMethod
| estimatedDocumentCountMethod
| distinctMethod
| aggregateMethod
| getIndexesMethod
| insertOneMethod
| insertManyMethod
| updateOneMethod
| updateManyMethod
| deleteOneMethod
| deleteManyMethod
| replaceOneMethod
| findOneAndUpdateMethod
| findOneAndReplaceMethod
| findOneAndDeleteMethod
| createIndexMethod
| createIndexesMethod
| dropIndexMethod
| dropIndexesMethod
| dropMethod
| renameCollectionMethod
| statsMethod
| storageSizeMethod
| totalIndexSizeMethod
| totalSizeMethod
| dataSizeMethod
| isCappedMethod
| validateMethod
| latencyStatsMethod
| watchMethod
| bulkWriteMethod
| collectionCountMethod
| collectionInsertMethod
| collectionRemoveMethod
| updateMethod
| mapReduceMethod
| findAndModifyMethod
| collectionExplainMethod
| analyzeShardKeyMethod
| configureQueryAnalyzerMethod
| compactStructuredEncryptionDataMethod
| hideIndexMethod
| unhideIndexMethod
| reIndexMethod
| getShardDistributionMethod
| getShardVersionMethod
| createSearchIndexMethod
| createSearchIndexesMethod
| dropSearchIndexMethod
| updateSearchIndexMethod
;
// Cursor method call: methods that operate on a cursor (chainable after collection methods)
cursorMethodCall
: sortMethod
| limitMethod
| skipMethod
| countMethod
| projectionMethod
| batchSizeMethod
| closeMethod
| collationMethod
| commentMethod
| explainMethod
| forEachMethod
| hasNextMethod
| hintMethod
| isClosedMethod
| isExhaustedMethod
| itcountMethod
| mapMethod
| maxMethod
| maxAwaitTimeMSMethod
| maxTimeMSMethod
| minMethod
| nextMethod
| noCursorTimeoutMethod
| objsLeftInBatchMethod
| prettyMethod
| readConcernMethod
| readPrefMethod
| returnKeyMethod
| showRecordIdMethod
| sizeMethod
| tailableMethod
| toArrayMethod
| tryNextMethod
| allowDiskUseMethod
| addOptionMethod
;
// Specific method rules for better AST structure
// find(filter?, projection?)
findMethod
: FIND LPAREN arguments? RPAREN
;
// findOne(filter?, projection?)
findOneMethod
: FIND_ONE LPAREN arguments? RPAREN
;
// countDocuments(filter?, options?)
countDocumentsMethod
: COUNT_DOCUMENTS LPAREN arguments? RPAREN
;
// estimatedDocumentCount(options?)
estimatedDocumentCountMethod
: ESTIMATED_DOCUMENT_COUNT LPAREN argument? RPAREN
;
// distinct(field, query?, options?)
distinctMethod
: DISTINCT LPAREN arguments RPAREN
;
// aggregate(pipeline?, options?)
aggregateMethod
: AGGREGATE LPAREN arguments? RPAREN
;
// getIndexes()
getIndexesMethod
: GET_INDEXES LPAREN RPAREN
;
// insertOne(document, options?)
insertOneMethod
: INSERT_ONE LPAREN arguments RPAREN
;
// insertMany(documents, options?)
insertManyMethod
: INSERT_MANY LPAREN arguments RPAREN
;
// updateOne(filter, update, options?)
updateOneMethod
: UPDATE_ONE LPAREN arguments RPAREN
;
// updateMany(filter, update, options?)
updateManyMethod
: UPDATE_MANY LPAREN arguments RPAREN
;
// deleteOne(filter, options?)
deleteOneMethod
: DELETE_ONE LPAREN arguments RPAREN
;
// deleteMany(filter, options?)
deleteManyMethod
: DELETE_MANY LPAREN arguments RPAREN
;
// replaceOne(filter, replacement, options?)
replaceOneMethod
: REPLACE_ONE LPAREN arguments RPAREN
;
// findOneAndUpdate(filter, update, options?)
findOneAndUpdateMethod
: FIND_ONE_AND_UPDATE LPAREN arguments RPAREN
;
// findOneAndReplace(filter, replacement, options?)
findOneAndReplaceMethod
: FIND_ONE_AND_REPLACE LPAREN arguments RPAREN
;
// findOneAndDelete(filter, options?)
findOneAndDeleteMethod
: FIND_ONE_AND_DELETE LPAREN arguments RPAREN
;
// createIndex(keys, options?)
createIndexMethod
: CREATE_INDEX LPAREN arguments RPAREN
;
// createIndexes(keyPatterns, options?)
createIndexesMethod
: CREATE_INDEXES LPAREN arguments RPAREN
;
// dropIndex(index)
dropIndexMethod
: DROP_INDEX LPAREN argument RPAREN
;
// dropIndexes(indexes?)
dropIndexesMethod
: DROP_INDEXES LPAREN argument? RPAREN
;
// drop(options?)
dropMethod
: DROP LPAREN argument? RPAREN
;
// renameCollection(newName, dropTarget?)
renameCollectionMethod
: RENAME_COLLECTION LPAREN arguments RPAREN
;
// stats(options?)
statsMethod
: STATS LPAREN argument? RPAREN
;
// storageSize()
storageSizeMethod
: STORAGE_SIZE LPAREN RPAREN
;
// totalIndexSize()
totalIndexSizeMethod
: TOTAL_INDEX_SIZE LPAREN RPAREN
;
// totalSize()
totalSizeMethod
: TOTAL_SIZE LPAREN RPAREN
;
// dataSize()
dataSizeMethod
: DATA_SIZE LPAREN RPAREN
;
// isCapped()
isCappedMethod
: IS_CAPPED LPAREN RPAREN
;
// validate(options?)
validateMethod
: VALIDATE LPAREN argument? RPAREN
;
// latencyStats(options?)
latencyStatsMethod
: LATENCY_STATS LPAREN argument? RPAREN
;
// watch(pipeline?, options?)
watchMethod
: WATCH LPAREN arguments? RPAREN
;
// bulkWrite(operations, options?)
bulkWriteMethod
: BULK_WRITE LPAREN arguments RPAREN
;
// count(filter?, options?) - deprecated collection-level count
collectionCountMethod
: COUNT LPAREN arguments? RPAREN
;
// insert(document/array, options?) - deprecated
collectionInsertMethod
: INSERT LPAREN arguments RPAREN
;
// remove(filter, options?) - deprecated
collectionRemoveMethod
: REMOVE LPAREN arguments RPAREN
;
// update(filter, update, options?) - deprecated
updateMethod
: UPDATE LPAREN arguments RPAREN
;
// mapReduce(map, reduce, options?) - deprecated
mapReduceMethod
: MAP_REDUCE LPAREN arguments RPAREN
;
// findAndModify(document)
findAndModifyMethod
: FIND_AND_MODIFY LPAREN arguments RPAREN
;
// explain(verbosity?) - collection-level explain
collectionExplainMethod
: EXPLAIN LPAREN arguments? RPAREN
;
// analyzeShardKey(key, options?)
analyzeShardKeyMethod
: ANALYZE_SHARD_KEY LPAREN arguments RPAREN
;
// configureQueryAnalyzer(options)
configureQueryAnalyzerMethod
: CONFIGURE_QUERY_ANALYZER LPAREN arguments RPAREN
;
// compactStructuredEncryptionData(options?)
compactStructuredEncryptionDataMethod
: COMPACT_STRUCTURED_ENCRYPTION_DATA LPAREN arguments? RPAREN
;
// hideIndex(indexName/spec)
hideIndexMethod
: HIDE_INDEX LPAREN argument RPAREN
;
// unhideIndex(indexName/spec)
unhideIndexMethod
: UNHIDE_INDEX LPAREN argument RPAREN
;
// reIndex()
reIndexMethod
: RE_INDEX LPAREN RPAREN
;
// getShardDistribution()
getShardDistributionMethod
: GET_SHARD_DISTRIBUTION LPAREN RPAREN
;
// getShardVersion()
getShardVersionMethod
: GET_SHARD_VERSION LPAREN RPAREN
;
// createSearchIndex(definition)
createSearchIndexMethod
: CREATE_SEARCH_INDEX LPAREN arguments RPAREN
;
// createSearchIndexes(definitions)
createSearchIndexesMethod
: CREATE_SEARCH_INDEXES LPAREN arguments RPAREN
;
// dropSearchIndex(name)
dropSearchIndexMethod
: DROP_SEARCH_INDEX LPAREN argument RPAREN
;
// updateSearchIndex(name, definition)
updateSearchIndexMethod
: UPDATE_SEARCH_INDEX LPAREN arguments RPAREN
;
// sort(specification?) - can be called without args
sortMethod
: SORT LPAREN document? RPAREN
;
limitMethod
: LIMIT LPAREN NUMBER RPAREN
;
skipMethod
: SKIP_ LPAREN NUMBER RPAREN
;
// cursor.count() - returns count of documents matching the query
countMethod
: COUNT LPAREN RPAREN
;
// projection(doc?) - can be called without args
projectionMethod
: (PROJECTION | PROJECT) LPAREN document? RPAREN
;
// Cursor methods
batchSizeMethod
: BATCH_SIZE LPAREN NUMBER RPAREN
;
closeMethod
: CLOSE LPAREN RPAREN
;
// collation(doc?) - can be called without args
collationMethod
: COLLATION LPAREN document? RPAREN
;
// comment(str?) - can be called without args
commentMethod
: COMMENT LPAREN stringLiteral? RPAREN
;
explainMethod
: EXPLAIN LPAREN stringLiteral? RPAREN
;
forEachMethod
: FOR_EACH LPAREN argument RPAREN
;
hasNextMethod
: HAS_NEXT LPAREN RPAREN
;
// hint(indexSpec?) - can be called without args
hintMethod
: HINT LPAREN argument? RPAREN
;
isClosedMethod
: IS_CLOSED LPAREN RPAREN
;
isExhaustedMethod
: IS_EXHAUSTED LPAREN RPAREN
;
itcountMethod
: IT_COUNT LPAREN RPAREN
;
mapMethod
: MAP LPAREN argument RPAREN
;
// max(indexBounds?) - can be called without args
maxMethod
: MAX LPAREN document? RPAREN
;
maxAwaitTimeMSMethod
: MAX_AWAIT_TIME_MS LPAREN NUMBER RPAREN
;
maxTimeMSMethod
: MAX_TIME_MS LPAREN NUMBER RPAREN
;
// min(indexBounds?) - can be called without args
minMethod
: MIN LPAREN document? RPAREN
;
nextMethod
: NEXT LPAREN RPAREN
;
noCursorTimeoutMethod
: NO_CURSOR_TIMEOUT LPAREN RPAREN
;
objsLeftInBatchMethod
: OBJS_LEFT_IN_BATCH LPAREN RPAREN
;
prettyMethod
: PRETTY LPAREN RPAREN
;
// readConcern(doc?) - can be called without args
readConcernMethod
: READ_CONCERN LPAREN document? RPAREN
;
readPrefMethod
: READ_PREF LPAREN arguments RPAREN
;
// returnKey(bool?) - can be called without args
returnKeyMethod
: RETURN_KEY LPAREN (TRUE | FALSE)? RPAREN
;
// showRecordId(bool?) - can be called without args
showRecordIdMethod
: SHOW_RECORD_ID LPAREN (TRUE | FALSE)? RPAREN
;
sizeMethod
: SIZE LPAREN RPAREN
;
tailableMethod
: TAILABLE LPAREN (TRUE | FALSE)? RPAREN
;
toArrayMethod
: TO_ARRAY LPAREN RPAREN
;
tryNextMethod
: TRY_NEXT LPAREN RPAREN
;
allowDiskUseMethod
: ALLOW_DISK_USE LPAREN (TRUE | FALSE)? RPAREN
;
addOptionMethod
: ADD_OPTION LPAREN NUMBER RPAREN
;
// Arguments: comma-separated list of values
arguments
: argument (COMMA argument)* COMMA?
;
argument
: value
;
// Document: { key: value, ... } with optional trailing comma
document
: LBRACE (pair (COMMA pair)* COMMA?)? RBRACE
;
// Key-value pair
pair
: key COLON value
;
// Key: can be unquoted identifier or quoted string
key
: identifier # unquotedKey
| stringLiteral # quotedKey
;
// Value: document, array, helper function, regex, or literal
value
: document # documentValue
| array # arrayValue
| helperFunction # helperValue
| REGEX_LITERAL # regexLiteralValue
| regExpConstructor # regexpConstructorValue
| literal # literalValue
| newKeywordError # newKeywordValue
;
// Catch 'new' keyword usage and provide helpful error message
newKeywordError
: NEW (OBJECT_ID | ISO_DATE | DATE | UUID | LONG | NUMBER_LONG | INT32 | NUMBER_INT | DOUBLE | DECIMAL128 | NUMBER_DECIMAL | TIMESTAMP | REG_EXP | BIN_DATA | BINARY | BSON_REG_EXP | HEX_DATA)
{ p.NotifyErrorListeners("'new' keyword is not supported. Use ObjectId(), ISODate(), UUID(), etc. directly without 'new'", nil, nil) }
LPAREN arguments? RPAREN
;
// Array: [ value, ... ] with optional trailing comma
array
: LBRACKET (value (COMMA value)* COMMA?)? RBRACKET
;
// Helper functions - each is a distinct node type for easy AST walking
// Note: 'new' keyword is not supported. Use ObjectId(), ISODate(), Date() directly.
helperFunction
: objectIdHelper
| isoDateHelper
| dateHelper
| uuidHelper
| longHelper
| int32Helper
| doubleHelper
| decimal128Helper
| timestampHelper
| binDataHelper
| binaryHelper
| bsonRegExpHelper
| hexDataHelper
;
// ObjectId("hex") or ObjectId()
objectIdHelper
: OBJECT_ID LPAREN stringLiteral? RPAREN
;
// ISODate("iso-string") or ISODate()
isoDateHelper
: ISO_DATE LPAREN stringLiteral? RPAREN
;
// Date() or Date("string") or Date(timestamp)
dateHelper
: DATE LPAREN (stringLiteral | NUMBER)? RPAREN
;
// UUID("uuid-string")
uuidHelper
: UUID LPAREN stringLiteral RPAREN
;
// Long(n), Long("n"), NumberLong(n), NumberLong("n")
longHelper
: (LONG | NUMBER_LONG) LPAREN (NUMBER | stringLiteral) RPAREN
;
// Int32(n), NumberInt(n)
int32Helper
: (INT32 | NUMBER_INT) LPAREN NUMBER RPAREN
;
// Double(n)
doubleHelper
: DOUBLE LPAREN NUMBER RPAREN
;
// Decimal128("n"), NumberDecimal("n")
decimal128Helper
: (DECIMAL128 | NUMBER_DECIMAL) LPAREN stringLiteral RPAREN
;
// Timestamp({t: n, i: n}) or Timestamp(t, i)
timestampHelper
: TIMESTAMP LPAREN document RPAREN # timestampDocHelper
| TIMESTAMP LPAREN NUMBER COMMA NUMBER RPAREN # timestampArgsHelper
;
// RegExp("pattern", "flags") constructor
regExpConstructor
: REG_EXP LPAREN stringLiteral (COMMA stringLiteral)? RPAREN
;
// BinData(subtype, base64) - Binary data with subtype
binDataHelper
: BIN_DATA LPAREN NUMBER COMMA stringLiteral RPAREN
;
// Binary(buffer, subtype) or Binary.createFromBase64(base64, subtype)
binaryHelper
: BINARY LPAREN arguments RPAREN
| BINARY DOT identifier LPAREN arguments RPAREN
;
// BSONRegExp(pattern, flags)
bsonRegExpHelper
: BSON_REG_EXP LPAREN arguments RPAREN
;
// HexData(subtype, hex)
hexDataHelper
: HEX_DATA LPAREN NUMBER COMMA stringLiteral RPAREN
;
// Literals
literal
: stringLiteral # stringLiteralValue
| NUMBER # numberLiteral
| TRUE # trueLiteral
| FALSE # falseLiteral
| NULL # nullLiteral
;
// String literal - both single and double quoted
stringLiteral
: DOUBLE_QUOTED_STRING
| SINGLE_QUOTED_STRING
;
// Identifier - used for unquoted keys, collection names, method names
// Includes MongoDB operators like $gt, $in, etc.
identifier
: IDENTIFIER
| DOLLAR IDENTIFIER
// Keywords that can also be used as identifiers
| SHOW
| DBS
| DATABASES
| COLLECTIONS
| DB
| NEW
| TRUE
| FALSE
| NULL
| FIND
| FIND_ONE
| COUNT_DOCUMENTS
| ESTIMATED_DOCUMENT_COUNT
| DISTINCT
| AGGREGATE
| GET_INDEXES
| INSERT_ONE
| INSERT_MANY
| UPDATE_ONE
| UPDATE_MANY
| DELETE_ONE
| DELETE_MANY
| REPLACE_ONE
| FIND_ONE_AND_UPDATE
| FIND_ONE_AND_REPLACE
| FIND_ONE_AND_DELETE
| CREATE_INDEX
| CREATE_INDEXES
| DROP_INDEX
| DROP_INDEXES
| DROP
| RENAME_COLLECTION
| STATS
| STORAGE_SIZE
| TOTAL_INDEX_SIZE
| TOTAL_SIZE
| DATA_SIZE
| IS_CAPPED
| VALIDATE
| LATENCY_STATS
| SORT
| LIMIT
| SKIP_
| COUNT
| PROJECTION
| PROJECT
| GET_COLLECTION
| GET_COLLECTION_NAMES
| GET_COLLECTION_INFOS
| CREATE_COLLECTION
| DROP_DATABASE
| HOST_INFO
| LIST_COMMANDS
| SERVER_BUILD_INFO
| SERVER_STATUS
| VERSION
| RUN_COMMAND
| ADMIN_COMMAND
| GET_NAME
| GET_MONGO
| GET_SIBLING_DB
| OBJECT_ID
| ISO_DATE
| DATE
| UUID
| LONG
| NUMBER_LONG
| INT32
| NUMBER_INT
| DOUBLE
| DECIMAL128
| NUMBER_DECIMAL
| TIMESTAMP
| REG_EXP
| BIN_DATA
| BINARY
| BSON_REG_EXP
| HEX_DATA
// Cursor method tokens
| BATCH_SIZE
| CLOSE
| COLLATION
| COMMENT
| EXPLAIN
| FOR_EACH
| HAS_NEXT
| HINT
| IS_CLOSED
| IS_EXHAUSTED
| IT_COUNT
| MAP
| MAX
| MAX_AWAIT_TIME_MS
| MAX_TIME_MS
| MIN
| NEXT
| NO_CURSOR_TIMEOUT
| OBJS_LEFT_IN_BATCH
| PRETTY
| READ_CONCERN
| READ_PREF
| RETURN_KEY
| SHOW_RECORD_ID
| SIZE
| TAILABLE
| TO_ARRAY
| TRY_NEXT
| ALLOW_DISK_USE
| ADD_OPTION
// Bulk operation tokens
| INITIALIZE_ORDERED_BULK_OP