mongo-query 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,16 @@
1
+ =mongo-query
2
+ Compiles SQL-like where clauses into query hashes acceptable for querying Mongo
3
+ using the Mongo Ruby drivers and other wrappers (like MongoMapper).
4
+
5
+ This is very, very young and may not be complete, stable or work at all. Caveat
6
+ emptor.
7
+
8
+ ==Examples
9
+ MongoQuery.compile("field_name = 1")
10
+ #=>{"field_name"=>1}
11
+
12
+ MongoQuery.compile("id > 5 AND id < 10 AND name='hayes'")
13
+ #=>{"id"=>{"$gt"=>5,"$lt"=>10},"name"=>"hayes"}
14
+
15
+ ==Requires
16
+ * treetop
@@ -0,0 +1,5 @@
1
+ $:.unshift File.dirname(__FILE__)
2
+
3
+ require 'treetop'
4
+ require 'mongo-query/mongo_query_grammar'
5
+ require 'mongo-query/mongo_query'
@@ -0,0 +1,13 @@
1
+ class MongoQuery
2
+
3
+ class << self
4
+
5
+ def compile(where_clause)
6
+ MongoQueryGrammarParser.new.parse(where_clause).to_mongo
7
+ rescue
8
+ nil
9
+ end
10
+
11
+ end
12
+
13
+ end
@@ -0,0 +1,1703 @@
1
+ # Autogenerated from a Treetop grammar. Edits may be lost.
2
+
3
+
4
+ module MongoQueryGrammar
5
+ include Treetop::Runtime
6
+
7
+ def root
8
+ @root || :query
9
+ end
10
+
11
+ module Query0
12
+ def space1
13
+ elements[0]
14
+ end
15
+
16
+ def and_expression
17
+ elements[1]
18
+ end
19
+
20
+ def space2
21
+ elements[2]
22
+ end
23
+ end
24
+
25
+ module Query1
26
+ def to_mongo
27
+ and_expression.to_mongo
28
+ end
29
+ end
30
+
31
+ def _nt_query
32
+ start_index = index
33
+ if node_cache[:query].has_key?(index)
34
+ cached = node_cache[:query][index]
35
+ @index = cached.interval.end if cached
36
+ return cached
37
+ end
38
+
39
+ i0, s0 = index, []
40
+ r1 = _nt_space
41
+ s0 << r1
42
+ if r1
43
+ r2 = _nt_and_expression
44
+ s0 << r2
45
+ if r2
46
+ r3 = _nt_space
47
+ s0 << r3
48
+ end
49
+ end
50
+ if s0.last
51
+ r0 = instantiate_node(SyntaxNode,input, i0...index, s0)
52
+ r0.extend(Query0)
53
+ r0.extend(Query1)
54
+ else
55
+ @index = i0
56
+ r0 = nil
57
+ end
58
+
59
+ node_cache[:query][start_index] = r0
60
+
61
+ r0
62
+ end
63
+
64
+ module AndExpression0
65
+ def binary_expression
66
+ elements[0]
67
+ end
68
+
69
+ def space1
70
+ elements[1]
71
+ end
72
+
73
+ def and_op
74
+ elements[2]
75
+ end
76
+
77
+ def space2
78
+ elements[3]
79
+ end
80
+
81
+ def and_expression
82
+ elements[4]
83
+ end
84
+ end
85
+
86
+ module AndExpression1
87
+ def to_mongo
88
+ res = binary_expression.to_mongo
89
+ new_res = and_expression.to_mongo
90
+ res.each do |key,value|
91
+ if new_res.has_key?(key)
92
+ value.merge!(new_res.delete(key))
93
+ end
94
+ end
95
+ res.merge(new_res)
96
+ end
97
+ end
98
+
99
+ def _nt_and_expression
100
+ start_index = index
101
+ if node_cache[:and_expression].has_key?(index)
102
+ cached = node_cache[:and_expression][index]
103
+ @index = cached.interval.end if cached
104
+ return cached
105
+ end
106
+
107
+ i0 = index
108
+ i1, s1 = index, []
109
+ r2 = _nt_binary_expression
110
+ s1 << r2
111
+ if r2
112
+ r3 = _nt_space
113
+ s1 << r3
114
+ if r3
115
+ r4 = _nt_and_op
116
+ s1 << r4
117
+ if r4
118
+ r5 = _nt_space
119
+ s1 << r5
120
+ if r5
121
+ r6 = _nt_and_expression
122
+ s1 << r6
123
+ end
124
+ end
125
+ end
126
+ end
127
+ if s1.last
128
+ r1 = instantiate_node(SyntaxNode,input, i1...index, s1)
129
+ r1.extend(AndExpression0)
130
+ r1.extend(AndExpression1)
131
+ else
132
+ @index = i1
133
+ r1 = nil
134
+ end
135
+ if r1
136
+ r0 = r1
137
+ else
138
+ r7 = _nt_binary_expression
139
+ if r7
140
+ r0 = r7
141
+ else
142
+ @index = i0
143
+ r0 = nil
144
+ end
145
+ end
146
+
147
+ node_cache[:and_expression][start_index] = r0
148
+
149
+ r0
150
+ end
151
+
152
+ def _nt_and_op
153
+ start_index = index
154
+ if node_cache[:and_op].has_key?(index)
155
+ cached = node_cache[:and_op][index]
156
+ @index = cached.interval.end if cached
157
+ return cached
158
+ end
159
+
160
+ i0 = index
161
+ if has_terminal?('and', false, index)
162
+ r1 = instantiate_node(SyntaxNode,input, index...(index + 3))
163
+ @index += 3
164
+ else
165
+ terminal_parse_failure('and')
166
+ r1 = nil
167
+ end
168
+ if r1
169
+ r0 = r1
170
+ else
171
+ if has_terminal?('AND', false, index)
172
+ r2 = instantiate_node(SyntaxNode,input, index...(index + 3))
173
+ @index += 3
174
+ else
175
+ terminal_parse_failure('AND')
176
+ r2 = nil
177
+ end
178
+ if r2
179
+ r0 = r2
180
+ else
181
+ @index = i0
182
+ r0 = nil
183
+ end
184
+ end
185
+
186
+ node_cache[:and_op][start_index] = r0
187
+
188
+ r0
189
+ end
190
+
191
+ module BinaryExpression0
192
+ def field_name
193
+ elements[0]
194
+ end
195
+
196
+ def space1
197
+ elements[1]
198
+ end
199
+
200
+ def space2
201
+ elements[3]
202
+ end
203
+
204
+ def literal
205
+ elements[4]
206
+ end
207
+ end
208
+
209
+ module BinaryExpression1
210
+ def to_mongo
211
+ {field_name.to_mongo=>literal.to_mongo}
212
+ end
213
+ end
214
+
215
+ module BinaryExpression2
216
+ def field_name
217
+ elements[0]
218
+ end
219
+
220
+ def space1
221
+ elements[1]
222
+ end
223
+
224
+ def operator
225
+ elements[2]
226
+ end
227
+
228
+ def space2
229
+ elements[3]
230
+ end
231
+
232
+ def value
233
+ elements[4]
234
+ end
235
+ end
236
+
237
+ module BinaryExpression3
238
+ def field_name
239
+ elements[0]
240
+ end
241
+
242
+ def space1
243
+ elements[1]
244
+ end
245
+
246
+ def operator
247
+ elements[2]
248
+ end
249
+
250
+ def space2
251
+ elements[3]
252
+ end
253
+
254
+ def value
255
+ elements[4]
256
+ end
257
+ end
258
+
259
+ module BinaryExpression4
260
+ def field_name
261
+ elements[0]
262
+ end
263
+
264
+ def space1
265
+ elements[1]
266
+ end
267
+
268
+ def operator
269
+ elements[2]
270
+ end
271
+
272
+ def space2
273
+ elements[3]
274
+ end
275
+
276
+ def value
277
+ elements[4]
278
+ end
279
+ end
280
+
281
+ module BinaryExpression5
282
+ def field_name
283
+ elements[0]
284
+ end
285
+
286
+ def space1
287
+ elements[1]
288
+ end
289
+
290
+ def operator
291
+ elements[2]
292
+ end
293
+
294
+ def space2
295
+ elements[3]
296
+ end
297
+
298
+ def value
299
+ elements[4]
300
+ end
301
+ end
302
+
303
+ module BinaryExpression6
304
+ def to_mongo
305
+ {field_name.to_mongo=>{ operator.to_mongo => value.to_mongo }}
306
+ end
307
+ end
308
+
309
+ def _nt_binary_expression
310
+ start_index = index
311
+ if node_cache[:binary_expression].has_key?(index)
312
+ cached = node_cache[:binary_expression][index]
313
+ @index = cached.interval.end if cached
314
+ return cached
315
+ end
316
+
317
+ i0 = index
318
+ i1, s1 = index, []
319
+ r2 = _nt_field_name
320
+ s1 << r2
321
+ if r2
322
+ r3 = _nt_space
323
+ s1 << r3
324
+ if r3
325
+ if has_terminal?('=', false, index)
326
+ r4 = instantiate_node(SyntaxNode,input, index...(index + 1))
327
+ @index += 1
328
+ else
329
+ terminal_parse_failure('=')
330
+ r4 = nil
331
+ end
332
+ s1 << r4
333
+ if r4
334
+ r5 = _nt_space
335
+ s1 << r5
336
+ if r5
337
+ r6 = _nt_literal
338
+ s1 << r6
339
+ end
340
+ end
341
+ end
342
+ end
343
+ if s1.last
344
+ r1 = instantiate_node(SyntaxNode,input, i1...index, s1)
345
+ r1.extend(BinaryExpression0)
346
+ r1.extend(BinaryExpression1)
347
+ else
348
+ @index = i1
349
+ r1 = nil
350
+ end
351
+ if r1
352
+ r0 = r1
353
+ else
354
+ i7 = index
355
+ i8, s8 = index, []
356
+ r9 = _nt_field_name
357
+ s8 << r9
358
+ if r9
359
+ r10 = _nt_space
360
+ s8 << r10
361
+ if r10
362
+ r11 = _nt_comp_op
363
+ s8 << r11
364
+ if r11
365
+ r12 = _nt_space
366
+ s8 << r12
367
+ if r12
368
+ r13 = _nt_literal
369
+ s8 << r13
370
+ end
371
+ end
372
+ end
373
+ end
374
+ if s8.last
375
+ r8 = instantiate_node(SyntaxNode,input, i8...index, s8)
376
+ r8.extend(BinaryExpression2)
377
+ else
378
+ @index = i8
379
+ r8 = nil
380
+ end
381
+ if r8
382
+ r7 = r8
383
+ r7.extend(BinaryExpression6)
384
+ else
385
+ i14, s14 = index, []
386
+ r15 = _nt_field_name
387
+ s14 << r15
388
+ if r15
389
+ r16 = _nt_space
390
+ s14 << r16
391
+ if r16
392
+ r17 = _nt_list_op
393
+ s14 << r17
394
+ if r17
395
+ r18 = _nt_space
396
+ s14 << r18
397
+ if r18
398
+ r19 = _nt_list
399
+ s14 << r19
400
+ end
401
+ end
402
+ end
403
+ end
404
+ if s14.last
405
+ r14 = instantiate_node(SyntaxNode,input, i14...index, s14)
406
+ r14.extend(BinaryExpression3)
407
+ else
408
+ @index = i14
409
+ r14 = nil
410
+ end
411
+ if r14
412
+ r7 = r14
413
+ r7.extend(BinaryExpression6)
414
+ else
415
+ i20, s20 = index, []
416
+ r21 = _nt_field_name
417
+ s20 << r21
418
+ if r21
419
+ r22 = _nt_space
420
+ s20 << r22
421
+ if r22
422
+ r23 = _nt_size_op
423
+ s20 << r23
424
+ if r23
425
+ r24 = _nt_space
426
+ s20 << r24
427
+ if r24
428
+ r25 = _nt_integer
429
+ s20 << r25
430
+ end
431
+ end
432
+ end
433
+ end
434
+ if s20.last
435
+ r20 = instantiate_node(SyntaxNode,input, i20...index, s20)
436
+ r20.extend(BinaryExpression4)
437
+ else
438
+ @index = i20
439
+ r20 = nil
440
+ end
441
+ if r20
442
+ r7 = r20
443
+ r7.extend(BinaryExpression6)
444
+ else
445
+ i26, s26 = index, []
446
+ r27 = _nt_field_name
447
+ s26 << r27
448
+ if r27
449
+ r28 = _nt_space
450
+ s26 << r28
451
+ if r28
452
+ r29 = _nt_exists_op
453
+ s26 << r29
454
+ if r29
455
+ r30 = _nt_space
456
+ s26 << r30
457
+ if r30
458
+ r31 = _nt_boolean
459
+ s26 << r31
460
+ end
461
+ end
462
+ end
463
+ end
464
+ if s26.last
465
+ r26 = instantiate_node(SyntaxNode,input, i26...index, s26)
466
+ r26.extend(BinaryExpression5)
467
+ else
468
+ @index = i26
469
+ r26 = nil
470
+ end
471
+ if r26
472
+ r7 = r26
473
+ r7.extend(BinaryExpression6)
474
+ else
475
+ @index = i7
476
+ r7 = nil
477
+ end
478
+ end
479
+ end
480
+ end
481
+ if r7
482
+ r0 = r7
483
+ else
484
+ @index = i0
485
+ r0 = nil
486
+ end
487
+ end
488
+
489
+ node_cache[:binary_expression][start_index] = r0
490
+
491
+ r0
492
+ end
493
+
494
+ module List0
495
+ def space1
496
+ elements[1]
497
+ end
498
+
499
+ def list_items
500
+ elements[2]
501
+ end
502
+
503
+ def space2
504
+ elements[3]
505
+ end
506
+
507
+ end
508
+
509
+ module List1
510
+ def to_mongo
511
+ list_items.to_mongo
512
+ end
513
+ end
514
+
515
+ def _nt_list
516
+ start_index = index
517
+ if node_cache[:list].has_key?(index)
518
+ cached = node_cache[:list][index]
519
+ @index = cached.interval.end if cached
520
+ return cached
521
+ end
522
+
523
+ i0, s0 = index, []
524
+ if has_terminal?('(', false, index)
525
+ r1 = instantiate_node(SyntaxNode,input, index...(index + 1))
526
+ @index += 1
527
+ else
528
+ terminal_parse_failure('(')
529
+ r1 = nil
530
+ end
531
+ s0 << r1
532
+ if r1
533
+ r2 = _nt_space
534
+ s0 << r2
535
+ if r2
536
+ r3 = _nt_list_items
537
+ s0 << r3
538
+ if r3
539
+ r4 = _nt_space
540
+ s0 << r4
541
+ if r4
542
+ if has_terminal?(')', false, index)
543
+ r5 = instantiate_node(SyntaxNode,input, index...(index + 1))
544
+ @index += 1
545
+ else
546
+ terminal_parse_failure(')')
547
+ r5 = nil
548
+ end
549
+ s0 << r5
550
+ end
551
+ end
552
+ end
553
+ end
554
+ if s0.last
555
+ r0 = instantiate_node(SyntaxNode,input, i0...index, s0)
556
+ r0.extend(List0)
557
+ r0.extend(List1)
558
+ else
559
+ @index = i0
560
+ r0 = nil
561
+ end
562
+
563
+ node_cache[:list][start_index] = r0
564
+
565
+ r0
566
+ end
567
+
568
+ module ListItems0
569
+ def literal
570
+ elements[0]
571
+ end
572
+
573
+ def space1
574
+ elements[1]
575
+ end
576
+
577
+ def space2
578
+ elements[3]
579
+ end
580
+
581
+ def list_items
582
+ elements[4]
583
+ end
584
+ end
585
+
586
+ module ListItems1
587
+ def to_mongo
588
+ [literal.to_mongo] + [list_items.to_mongo].flatten
589
+ end
590
+ end
591
+
592
+ def _nt_list_items
593
+ start_index = index
594
+ if node_cache[:list_items].has_key?(index)
595
+ cached = node_cache[:list_items][index]
596
+ @index = cached.interval.end if cached
597
+ return cached
598
+ end
599
+
600
+ i0 = index
601
+ i1, s1 = index, []
602
+ r2 = _nt_literal
603
+ s1 << r2
604
+ if r2
605
+ r3 = _nt_space
606
+ s1 << r3
607
+ if r3
608
+ if has_terminal?(',', false, index)
609
+ r4 = instantiate_node(SyntaxNode,input, index...(index + 1))
610
+ @index += 1
611
+ else
612
+ terminal_parse_failure(',')
613
+ r4 = nil
614
+ end
615
+ s1 << r4
616
+ if r4
617
+ r5 = _nt_space
618
+ s1 << r5
619
+ if r5
620
+ r6 = _nt_list_items
621
+ s1 << r6
622
+ end
623
+ end
624
+ end
625
+ end
626
+ if s1.last
627
+ r1 = instantiate_node(SyntaxNode,input, i1...index, s1)
628
+ r1.extend(ListItems0)
629
+ r1.extend(ListItems1)
630
+ else
631
+ @index = i1
632
+ r1 = nil
633
+ end
634
+ if r1
635
+ r0 = r1
636
+ else
637
+ r7 = _nt_literal
638
+ if r7
639
+ r0 = r7
640
+ else
641
+ @index = i0
642
+ r0 = nil
643
+ end
644
+ end
645
+
646
+ node_cache[:list_items][start_index] = r0
647
+
648
+ r0
649
+ end
650
+
651
+ module CompOp0
652
+ def to_mongo; '$lte'; end
653
+ end
654
+
655
+ module CompOp1
656
+ def to_mongo; '$lt'; end
657
+ end
658
+
659
+ module CompOp2
660
+ def to_mongo; '$gte'; end
661
+ end
662
+
663
+ module CompOp3
664
+ def to_mongo; '$gt'; end
665
+ end
666
+
667
+ module CompOp4
668
+ def to_mongo; '$ne'; end
669
+ end
670
+
671
+ def _nt_comp_op
672
+ start_index = index
673
+ if node_cache[:comp_op].has_key?(index)
674
+ cached = node_cache[:comp_op][index]
675
+ @index = cached.interval.end if cached
676
+ return cached
677
+ end
678
+
679
+ i0 = index
680
+ if has_terminal?('<=', false, index)
681
+ r1 = instantiate_node(SyntaxNode,input, index...(index + 2))
682
+ r1.extend(CompOp0)
683
+ @index += 2
684
+ else
685
+ terminal_parse_failure('<=')
686
+ r1 = nil
687
+ end
688
+ if r1
689
+ r0 = r1
690
+ else
691
+ if has_terminal?('<', false, index)
692
+ r2 = instantiate_node(SyntaxNode,input, index...(index + 1))
693
+ r2.extend(CompOp1)
694
+ @index += 1
695
+ else
696
+ terminal_parse_failure('<')
697
+ r2 = nil
698
+ end
699
+ if r2
700
+ r0 = r2
701
+ else
702
+ if has_terminal?('>=', false, index)
703
+ r3 = instantiate_node(SyntaxNode,input, index...(index + 2))
704
+ r3.extend(CompOp2)
705
+ @index += 2
706
+ else
707
+ terminal_parse_failure('>=')
708
+ r3 = nil
709
+ end
710
+ if r3
711
+ r0 = r3
712
+ else
713
+ if has_terminal?('>', false, index)
714
+ r4 = instantiate_node(SyntaxNode,input, index...(index + 1))
715
+ r4.extend(CompOp3)
716
+ @index += 1
717
+ else
718
+ terminal_parse_failure('>')
719
+ r4 = nil
720
+ end
721
+ if r4
722
+ r0 = r4
723
+ else
724
+ if has_terminal?('!=', false, index)
725
+ r5 = instantiate_node(SyntaxNode,input, index...(index + 2))
726
+ r5.extend(CompOp4)
727
+ @index += 2
728
+ else
729
+ terminal_parse_failure('!=')
730
+ r5 = nil
731
+ end
732
+ if r5
733
+ r0 = r5
734
+ else
735
+ @index = i0
736
+ r0 = nil
737
+ end
738
+ end
739
+ end
740
+ end
741
+ end
742
+
743
+ node_cache[:comp_op][start_index] = r0
744
+
745
+ r0
746
+ end
747
+
748
+ def _nt_list_op
749
+ start_index = index
750
+ if node_cache[:list_op].has_key?(index)
751
+ cached = node_cache[:list_op][index]
752
+ @index = cached.interval.end if cached
753
+ return cached
754
+ end
755
+
756
+ i0 = index
757
+ r1 = _nt_in_op
758
+ if r1
759
+ r0 = r1
760
+ else
761
+ r2 = _nt_not_in_op
762
+ if r2
763
+ r0 = r2
764
+ else
765
+ r3 = _nt_all_op
766
+ if r3
767
+ r0 = r3
768
+ else
769
+ @index = i0
770
+ r0 = nil
771
+ end
772
+ end
773
+ end
774
+
775
+ node_cache[:list_op][start_index] = r0
776
+
777
+ r0
778
+ end
779
+
780
+ module InOp0
781
+ def to_mongo; "$in"; end
782
+ end
783
+
784
+ def _nt_in_op
785
+ start_index = index
786
+ if node_cache[:in_op].has_key?(index)
787
+ cached = node_cache[:in_op][index]
788
+ @index = cached.interval.end if cached
789
+ return cached
790
+ end
791
+
792
+ i0 = index
793
+ if has_terminal?('in', false, index)
794
+ r1 = instantiate_node(SyntaxNode,input, index...(index + 2))
795
+ @index += 2
796
+ else
797
+ terminal_parse_failure('in')
798
+ r1 = nil
799
+ end
800
+ if r1
801
+ r0 = r1
802
+ else
803
+ if has_terminal?('IN', false, index)
804
+ r2 = instantiate_node(SyntaxNode,input, index...(index + 2))
805
+ r2.extend(InOp0)
806
+ @index += 2
807
+ else
808
+ terminal_parse_failure('IN')
809
+ r2 = nil
810
+ end
811
+ if r2
812
+ r0 = r2
813
+ else
814
+ @index = i0
815
+ r0 = nil
816
+ end
817
+ end
818
+
819
+ node_cache[:in_op][start_index] = r0
820
+
821
+ r0
822
+ end
823
+
824
+ module NotInOp0
825
+ def to_mongo; "$nin"; end
826
+ end
827
+
828
+ def _nt_not_in_op
829
+ start_index = index
830
+ if node_cache[:not_in_op].has_key?(index)
831
+ cached = node_cache[:not_in_op][index]
832
+ @index = cached.interval.end if cached
833
+ return cached
834
+ end
835
+
836
+ i0 = index
837
+ if has_terminal?('not in', false, index)
838
+ r1 = instantiate_node(SyntaxNode,input, index...(index + 6))
839
+ @index += 6
840
+ else
841
+ terminal_parse_failure('not in')
842
+ r1 = nil
843
+ end
844
+ if r1
845
+ r0 = r1
846
+ else
847
+ if has_terminal?('NOT IN', false, index)
848
+ r2 = instantiate_node(SyntaxNode,input, index...(index + 6))
849
+ r2.extend(NotInOp0)
850
+ @index += 6
851
+ else
852
+ terminal_parse_failure('NOT IN')
853
+ r2 = nil
854
+ end
855
+ if r2
856
+ r0 = r2
857
+ else
858
+ @index = i0
859
+ r0 = nil
860
+ end
861
+ end
862
+
863
+ node_cache[:not_in_op][start_index] = r0
864
+
865
+ r0
866
+ end
867
+
868
+ module AllOp0
869
+ def to_mongo; "$all"; end
870
+ end
871
+
872
+ def _nt_all_op
873
+ start_index = index
874
+ if node_cache[:all_op].has_key?(index)
875
+ cached = node_cache[:all_op][index]
876
+ @index = cached.interval.end if cached
877
+ return cached
878
+ end
879
+
880
+ i0 = index
881
+ if has_terminal?('all', false, index)
882
+ r1 = instantiate_node(SyntaxNode,input, index...(index + 3))
883
+ @index += 3
884
+ else
885
+ terminal_parse_failure('all')
886
+ r1 = nil
887
+ end
888
+ if r1
889
+ r0 = r1
890
+ else
891
+ if has_terminal?('ALL', false, index)
892
+ r2 = instantiate_node(SyntaxNode,input, index...(index + 3))
893
+ r2.extend(AllOp0)
894
+ @index += 3
895
+ else
896
+ terminal_parse_failure('ALL')
897
+ r2 = nil
898
+ end
899
+ if r2
900
+ r0 = r2
901
+ else
902
+ @index = i0
903
+ r0 = nil
904
+ end
905
+ end
906
+
907
+ node_cache[:all_op][start_index] = r0
908
+
909
+ r0
910
+ end
911
+
912
+ module SizeOp0
913
+ def to_mongo; "$size"; end
914
+ end
915
+
916
+ def _nt_size_op
917
+ start_index = index
918
+ if node_cache[:size_op].has_key?(index)
919
+ cached = node_cache[:size_op][index]
920
+ @index = cached.interval.end if cached
921
+ return cached
922
+ end
923
+
924
+ i0 = index
925
+ if has_terminal?('size', false, index)
926
+ r1 = instantiate_node(SyntaxNode,input, index...(index + 4))
927
+ @index += 4
928
+ else
929
+ terminal_parse_failure('size')
930
+ r1 = nil
931
+ end
932
+ if r1
933
+ r0 = r1
934
+ else
935
+ if has_terminal?('SIZE', false, index)
936
+ r2 = instantiate_node(SyntaxNode,input, index...(index + 4))
937
+ r2.extend(SizeOp0)
938
+ @index += 4
939
+ else
940
+ terminal_parse_failure('SIZE')
941
+ r2 = nil
942
+ end
943
+ if r2
944
+ r0 = r2
945
+ else
946
+ @index = i0
947
+ r0 = nil
948
+ end
949
+ end
950
+
951
+ node_cache[:size_op][start_index] = r0
952
+
953
+ r0
954
+ end
955
+
956
+ module ExistsOp0
957
+ def to_mongo; "$exists"; end
958
+ end
959
+
960
+ def _nt_exists_op
961
+ start_index = index
962
+ if node_cache[:exists_op].has_key?(index)
963
+ cached = node_cache[:exists_op][index]
964
+ @index = cached.interval.end if cached
965
+ return cached
966
+ end
967
+
968
+ i0 = index
969
+ if has_terminal?('exists', false, index)
970
+ r1 = instantiate_node(SyntaxNode,input, index...(index + 6))
971
+ @index += 6
972
+ else
973
+ terminal_parse_failure('exists')
974
+ r1 = nil
975
+ end
976
+ if r1
977
+ r0 = r1
978
+ else
979
+ if has_terminal?('EXISTS', false, index)
980
+ r2 = instantiate_node(SyntaxNode,input, index...(index + 6))
981
+ r2.extend(ExistsOp0)
982
+ @index += 6
983
+ else
984
+ terminal_parse_failure('EXISTS')
985
+ r2 = nil
986
+ end
987
+ if r2
988
+ r0 = r2
989
+ else
990
+ @index = i0
991
+ r0 = nil
992
+ end
993
+ end
994
+
995
+ node_cache[:exists_op][start_index] = r0
996
+
997
+ r0
998
+ end
999
+
1000
+ def _nt_literal
1001
+ start_index = index
1002
+ if node_cache[:literal].has_key?(index)
1003
+ cached = node_cache[:literal][index]
1004
+ @index = cached.interval.end if cached
1005
+ return cached
1006
+ end
1007
+
1008
+ i0 = index
1009
+ r1 = _nt_string
1010
+ if r1
1011
+ r0 = r1
1012
+ else
1013
+ r2 = _nt_number
1014
+ if r2
1015
+ r0 = r2
1016
+ else
1017
+ r3 = _nt_boolean
1018
+ if r3
1019
+ r0 = r3
1020
+ else
1021
+ r4 = _nt_regex
1022
+ if r4
1023
+ r0 = r4
1024
+ else
1025
+ @index = i0
1026
+ r0 = nil
1027
+ end
1028
+ end
1029
+ end
1030
+ end
1031
+
1032
+ node_cache[:literal][start_index] = r0
1033
+
1034
+ r0
1035
+ end
1036
+
1037
+ def _nt_number
1038
+ start_index = index
1039
+ if node_cache[:number].has_key?(index)
1040
+ cached = node_cache[:number][index]
1041
+ @index = cached.interval.end if cached
1042
+ return cached
1043
+ end
1044
+
1045
+ i0 = index
1046
+ r1 = _nt_float
1047
+ if r1
1048
+ r0 = r1
1049
+ else
1050
+ r2 = _nt_integer
1051
+ if r2
1052
+ r0 = r2
1053
+ else
1054
+ @index = i0
1055
+ r0 = nil
1056
+ end
1057
+ end
1058
+
1059
+ node_cache[:number][start_index] = r0
1060
+
1061
+ r0
1062
+ end
1063
+
1064
+ module Float0
1065
+ end
1066
+
1067
+ module Float1
1068
+ def to_mongo
1069
+ text_value.to_f
1070
+ end
1071
+ end
1072
+
1073
+ def _nt_float
1074
+ start_index = index
1075
+ if node_cache[:float].has_key?(index)
1076
+ cached = node_cache[:float][index]
1077
+ @index = cached.interval.end if cached
1078
+ return cached
1079
+ end
1080
+
1081
+ i0, s0 = index, []
1082
+ if has_terminal?('\G[-+]', true, index)
1083
+ r2 = true
1084
+ @index += 1
1085
+ else
1086
+ r2 = nil
1087
+ end
1088
+ if r2
1089
+ r1 = r2
1090
+ else
1091
+ r1 = instantiate_node(SyntaxNode,input, index...index)
1092
+ end
1093
+ s0 << r1
1094
+ if r1
1095
+ s3, i3 = [], index
1096
+ loop do
1097
+ if has_terminal?('\G[0-9]', true, index)
1098
+ r4 = true
1099
+ @index += 1
1100
+ else
1101
+ r4 = nil
1102
+ end
1103
+ if r4
1104
+ s3 << r4
1105
+ else
1106
+ break
1107
+ end
1108
+ end
1109
+ if s3.empty?
1110
+ @index = i3
1111
+ r3 = nil
1112
+ else
1113
+ r3 = instantiate_node(SyntaxNode,input, i3...index, s3)
1114
+ end
1115
+ s0 << r3
1116
+ if r3
1117
+ if has_terminal?('.', false, index)
1118
+ r5 = instantiate_node(SyntaxNode,input, index...(index + 1))
1119
+ @index += 1
1120
+ else
1121
+ terminal_parse_failure('.')
1122
+ r5 = nil
1123
+ end
1124
+ s0 << r5
1125
+ if r5
1126
+ s6, i6 = [], index
1127
+ loop do
1128
+ if has_terminal?('\G[0-9]', true, index)
1129
+ r7 = true
1130
+ @index += 1
1131
+ else
1132
+ r7 = nil
1133
+ end
1134
+ if r7
1135
+ s6 << r7
1136
+ else
1137
+ break
1138
+ end
1139
+ end
1140
+ if s6.empty?
1141
+ @index = i6
1142
+ r6 = nil
1143
+ else
1144
+ r6 = instantiate_node(SyntaxNode,input, i6...index, s6)
1145
+ end
1146
+ s0 << r6
1147
+ end
1148
+ end
1149
+ end
1150
+ if s0.last
1151
+ r0 = instantiate_node(SyntaxNode,input, i0...index, s0)
1152
+ r0.extend(Float0)
1153
+ r0.extend(Float1)
1154
+ else
1155
+ @index = i0
1156
+ r0 = nil
1157
+ end
1158
+
1159
+ node_cache[:float][start_index] = r0
1160
+
1161
+ r0
1162
+ end
1163
+
1164
+ module Integer0
1165
+ end
1166
+
1167
+ module Integer1
1168
+ def to_mongo
1169
+ text_value.to_i
1170
+ end
1171
+ end
1172
+
1173
+ def _nt_integer
1174
+ start_index = index
1175
+ if node_cache[:integer].has_key?(index)
1176
+ cached = node_cache[:integer][index]
1177
+ @index = cached.interval.end if cached
1178
+ return cached
1179
+ end
1180
+
1181
+ i0, s0 = index, []
1182
+ if has_terminal?('\G[-+]', true, index)
1183
+ r2 = true
1184
+ @index += 1
1185
+ else
1186
+ r2 = nil
1187
+ end
1188
+ if r2
1189
+ r1 = r2
1190
+ else
1191
+ r1 = instantiate_node(SyntaxNode,input, index...index)
1192
+ end
1193
+ s0 << r1
1194
+ if r1
1195
+ s3, i3 = [], index
1196
+ loop do
1197
+ if has_terminal?('\G[0-9]', true, index)
1198
+ r4 = true
1199
+ @index += 1
1200
+ else
1201
+ r4 = nil
1202
+ end
1203
+ if r4
1204
+ s3 << r4
1205
+ else
1206
+ break
1207
+ end
1208
+ end
1209
+ if s3.empty?
1210
+ @index = i3
1211
+ r3 = nil
1212
+ else
1213
+ r3 = instantiate_node(SyntaxNode,input, i3...index, s3)
1214
+ end
1215
+ s0 << r3
1216
+ end
1217
+ if s0.last
1218
+ r0 = instantiate_node(SyntaxNode,input, i0...index, s0)
1219
+ r0.extend(Integer0)
1220
+ r0.extend(Integer1)
1221
+ else
1222
+ @index = i0
1223
+ r0 = nil
1224
+ end
1225
+
1226
+ node_cache[:integer][start_index] = r0
1227
+
1228
+ r0
1229
+ end
1230
+
1231
+ module String0
1232
+ def to_mongo
1233
+ text_value[1...-1]
1234
+ end
1235
+ end
1236
+
1237
+ def _nt_string
1238
+ start_index = index
1239
+ if node_cache[:string].has_key?(index)
1240
+ cached = node_cache[:string][index]
1241
+ @index = cached.interval.end if cached
1242
+ return cached
1243
+ end
1244
+
1245
+ i0 = index
1246
+ r1 = _nt_double_quote_string
1247
+ if r1
1248
+ r0 = r1
1249
+ r0.extend(String0)
1250
+ else
1251
+ r2 = _nt_single_quote_string
1252
+ if r2
1253
+ r0 = r2
1254
+ r0.extend(String0)
1255
+ else
1256
+ @index = i0
1257
+ r0 = nil
1258
+ end
1259
+ end
1260
+
1261
+ node_cache[:string][start_index] = r0
1262
+
1263
+ r0
1264
+ end
1265
+
1266
+ module DoubleQuoteString0
1267
+ end
1268
+
1269
+ def _nt_double_quote_string
1270
+ start_index = index
1271
+ if node_cache[:double_quote_string].has_key?(index)
1272
+ cached = node_cache[:double_quote_string][index]
1273
+ @index = cached.interval.end if cached
1274
+ return cached
1275
+ end
1276
+
1277
+ i0, s0 = index, []
1278
+ if has_terminal?('"', false, index)
1279
+ r1 = instantiate_node(SyntaxNode,input, index...(index + 1))
1280
+ @index += 1
1281
+ else
1282
+ terminal_parse_failure('"')
1283
+ r1 = nil
1284
+ end
1285
+ s0 << r1
1286
+ if r1
1287
+ s2, i2 = [], index
1288
+ loop do
1289
+ i3 = index
1290
+ if has_terminal?('\"', false, index)
1291
+ r4 = instantiate_node(SyntaxNode,input, index...(index + 2))
1292
+ @index += 2
1293
+ else
1294
+ terminal_parse_failure('\"')
1295
+ r4 = nil
1296
+ end
1297
+ if r4
1298
+ r3 = r4
1299
+ else
1300
+ if has_terminal?('\\', false, index)
1301
+ r5 = instantiate_node(SyntaxNode,input, index...(index + 1))
1302
+ @index += 1
1303
+ else
1304
+ terminal_parse_failure('\\')
1305
+ r5 = nil
1306
+ end
1307
+ if r5
1308
+ r3 = r5
1309
+ else
1310
+ if has_terminal?('\G[^"]', true, index)
1311
+ r6 = true
1312
+ @index += 1
1313
+ else
1314
+ r6 = nil
1315
+ end
1316
+ if r6
1317
+ r3 = r6
1318
+ else
1319
+ @index = i3
1320
+ r3 = nil
1321
+ end
1322
+ end
1323
+ end
1324
+ if r3
1325
+ s2 << r3
1326
+ else
1327
+ break
1328
+ end
1329
+ end
1330
+ r2 = instantiate_node(SyntaxNode,input, i2...index, s2)
1331
+ s0 << r2
1332
+ if r2
1333
+ if has_terminal?('"', false, index)
1334
+ r7 = instantiate_node(SyntaxNode,input, index...(index + 1))
1335
+ @index += 1
1336
+ else
1337
+ terminal_parse_failure('"')
1338
+ r7 = nil
1339
+ end
1340
+ s0 << r7
1341
+ end
1342
+ end
1343
+ if s0.last
1344
+ r0 = instantiate_node(SyntaxNode,input, i0...index, s0)
1345
+ r0.extend(DoubleQuoteString0)
1346
+ else
1347
+ @index = i0
1348
+ r0 = nil
1349
+ end
1350
+
1351
+ node_cache[:double_quote_string][start_index] = r0
1352
+
1353
+ r0
1354
+ end
1355
+
1356
+ module SingleQuoteString0
1357
+ end
1358
+
1359
+ def _nt_single_quote_string
1360
+ start_index = index
1361
+ if node_cache[:single_quote_string].has_key?(index)
1362
+ cached = node_cache[:single_quote_string][index]
1363
+ @index = cached.interval.end if cached
1364
+ return cached
1365
+ end
1366
+
1367
+ i0, s0 = index, []
1368
+ if has_terminal?("'", false, index)
1369
+ r1 = instantiate_node(SyntaxNode,input, index...(index + 1))
1370
+ @index += 1
1371
+ else
1372
+ terminal_parse_failure("'")
1373
+ r1 = nil
1374
+ end
1375
+ s0 << r1
1376
+ if r1
1377
+ s2, i2 = [], index
1378
+ loop do
1379
+ i3 = index
1380
+ if has_terminal?("\\'", false, index)
1381
+ r4 = instantiate_node(SyntaxNode,input, index...(index + 2))
1382
+ @index += 2
1383
+ else
1384
+ terminal_parse_failure("\\'")
1385
+ r4 = nil
1386
+ end
1387
+ if r4
1388
+ r3 = r4
1389
+ else
1390
+ if has_terminal?('\\', false, index)
1391
+ r5 = instantiate_node(SyntaxNode,input, index...(index + 1))
1392
+ @index += 1
1393
+ else
1394
+ terminal_parse_failure('\\')
1395
+ r5 = nil
1396
+ end
1397
+ if r5
1398
+ r3 = r5
1399
+ else
1400
+ if has_terminal?('\G[^\']', true, index)
1401
+ r6 = true
1402
+ @index += 1
1403
+ else
1404
+ r6 = nil
1405
+ end
1406
+ if r6
1407
+ r3 = r6
1408
+ else
1409
+ @index = i3
1410
+ r3 = nil
1411
+ end
1412
+ end
1413
+ end
1414
+ if r3
1415
+ s2 << r3
1416
+ else
1417
+ break
1418
+ end
1419
+ end
1420
+ r2 = instantiate_node(SyntaxNode,input, i2...index, s2)
1421
+ s0 << r2
1422
+ if r2
1423
+ if has_terminal?("'", false, index)
1424
+ r7 = instantiate_node(SyntaxNode,input, index...(index + 1))
1425
+ @index += 1
1426
+ else
1427
+ terminal_parse_failure("'")
1428
+ r7 = nil
1429
+ end
1430
+ s0 << r7
1431
+ end
1432
+ end
1433
+ if s0.last
1434
+ r0 = instantiate_node(SyntaxNode,input, i0...index, s0)
1435
+ r0.extend(SingleQuoteString0)
1436
+ else
1437
+ @index = i0
1438
+ r0 = nil
1439
+ end
1440
+
1441
+ node_cache[:single_quote_string][start_index] = r0
1442
+
1443
+ r0
1444
+ end
1445
+
1446
+ module Boolean0
1447
+ def to_mongo; true; end
1448
+ end
1449
+
1450
+ module Boolean1
1451
+ def to_mongo; false; end
1452
+ end
1453
+
1454
+ def _nt_boolean
1455
+ start_index = index
1456
+ if node_cache[:boolean].has_key?(index)
1457
+ cached = node_cache[:boolean][index]
1458
+ @index = cached.interval.end if cached
1459
+ return cached
1460
+ end
1461
+
1462
+ i0 = index
1463
+ if has_terminal?('true', false, index)
1464
+ r1 = instantiate_node(SyntaxNode,input, index...(index + 4))
1465
+ r1.extend(Boolean0)
1466
+ @index += 4
1467
+ else
1468
+ terminal_parse_failure('true')
1469
+ r1 = nil
1470
+ end
1471
+ if r1
1472
+ r0 = r1
1473
+ else
1474
+ if has_terminal?('false', false, index)
1475
+ r2 = instantiate_node(SyntaxNode,input, index...(index + 5))
1476
+ r2.extend(Boolean1)
1477
+ @index += 5
1478
+ else
1479
+ terminal_parse_failure('false')
1480
+ r2 = nil
1481
+ end
1482
+ if r2
1483
+ r0 = r2
1484
+ else
1485
+ @index = i0
1486
+ r0 = nil
1487
+ end
1488
+ end
1489
+
1490
+ node_cache[:boolean][start_index] = r0
1491
+
1492
+ r0
1493
+ end
1494
+
1495
+ def _nt_space
1496
+ start_index = index
1497
+ if node_cache[:space].has_key?(index)
1498
+ cached = node_cache[:space][index]
1499
+ @index = cached.interval.end if cached
1500
+ return cached
1501
+ end
1502
+
1503
+ s0, i0 = [], index
1504
+ loop do
1505
+ if has_terminal?('\G[ \\n\\t]', true, index)
1506
+ r1 = true
1507
+ @index += 1
1508
+ else
1509
+ r1 = nil
1510
+ end
1511
+ if r1
1512
+ s0 << r1
1513
+ else
1514
+ break
1515
+ end
1516
+ end
1517
+ r0 = instantiate_node(SyntaxNode,input, i0...index, s0)
1518
+
1519
+ node_cache[:space][start_index] = r0
1520
+
1521
+ r0
1522
+ end
1523
+
1524
+ module Regex0
1525
+ end
1526
+
1527
+ module Regex1
1528
+ def to_mongo
1529
+ Regexp.new(text_value[1...-1])
1530
+ end
1531
+ end
1532
+
1533
+ def _nt_regex
1534
+ start_index = index
1535
+ if node_cache[:regex].has_key?(index)
1536
+ cached = node_cache[:regex][index]
1537
+ @index = cached.interval.end if cached
1538
+ return cached
1539
+ end
1540
+
1541
+ i0, s0 = index, []
1542
+ if has_terminal?('/', false, index)
1543
+ r1 = instantiate_node(SyntaxNode,input, index...(index + 1))
1544
+ @index += 1
1545
+ else
1546
+ terminal_parse_failure('/')
1547
+ r1 = nil
1548
+ end
1549
+ s0 << r1
1550
+ if r1
1551
+ s2, i2 = [], index
1552
+ loop do
1553
+ if has_terminal?('\G[^/]', true, index)
1554
+ r3 = true
1555
+ @index += 1
1556
+ else
1557
+ r3 = nil
1558
+ end
1559
+ if r3
1560
+ s2 << r3
1561
+ else
1562
+ break
1563
+ end
1564
+ end
1565
+ if s2.empty?
1566
+ @index = i2
1567
+ r2 = nil
1568
+ else
1569
+ r2 = instantiate_node(SyntaxNode,input, i2...index, s2)
1570
+ end
1571
+ s0 << r2
1572
+ if r2
1573
+ if has_terminal?('/', false, index)
1574
+ r4 = instantiate_node(SyntaxNode,input, index...(index + 1))
1575
+ @index += 1
1576
+ else
1577
+ terminal_parse_failure('/')
1578
+ r4 = nil
1579
+ end
1580
+ s0 << r4
1581
+ end
1582
+ end
1583
+ if s0.last
1584
+ r0 = instantiate_node(SyntaxNode,input, i0...index, s0)
1585
+ r0.extend(Regex0)
1586
+ r0.extend(Regex1)
1587
+ else
1588
+ @index = i0
1589
+ r0 = nil
1590
+ end
1591
+
1592
+ node_cache[:regex][start_index] = r0
1593
+
1594
+ r0
1595
+ end
1596
+
1597
+ module FieldName0
1598
+ def single_field_name
1599
+ elements[0]
1600
+ end
1601
+
1602
+ def field_name
1603
+ elements[2]
1604
+ end
1605
+ end
1606
+
1607
+ module FieldName1
1608
+ def to_mongo
1609
+ text_value
1610
+ end
1611
+ end
1612
+
1613
+ def _nt_field_name
1614
+ start_index = index
1615
+ if node_cache[:field_name].has_key?(index)
1616
+ cached = node_cache[:field_name][index]
1617
+ @index = cached.interval.end if cached
1618
+ return cached
1619
+ end
1620
+
1621
+ i0 = index
1622
+ i1, s1 = index, []
1623
+ r2 = _nt_single_field_name
1624
+ s1 << r2
1625
+ if r2
1626
+ if has_terminal?('.', false, index)
1627
+ r3 = instantiate_node(SyntaxNode,input, index...(index + 1))
1628
+ @index += 1
1629
+ else
1630
+ terminal_parse_failure('.')
1631
+ r3 = nil
1632
+ end
1633
+ s1 << r3
1634
+ if r3
1635
+ r4 = _nt_field_name
1636
+ s1 << r4
1637
+ end
1638
+ end
1639
+ if s1.last
1640
+ r1 = instantiate_node(SyntaxNode,input, i1...index, s1)
1641
+ r1.extend(FieldName0)
1642
+ else
1643
+ @index = i1
1644
+ r1 = nil
1645
+ end
1646
+ if r1
1647
+ r0 = r1
1648
+ r0.extend(FieldName1)
1649
+ else
1650
+ r5 = _nt_single_field_name
1651
+ if r5
1652
+ r0 = r5
1653
+ r0.extend(FieldName1)
1654
+ else
1655
+ @index = i0
1656
+ r0 = nil
1657
+ end
1658
+ end
1659
+
1660
+ node_cache[:field_name][start_index] = r0
1661
+
1662
+ r0
1663
+ end
1664
+
1665
+ def _nt_single_field_name
1666
+ start_index = index
1667
+ if node_cache[:single_field_name].has_key?(index)
1668
+ cached = node_cache[:single_field_name][index]
1669
+ @index = cached.interval.end if cached
1670
+ return cached
1671
+ end
1672
+
1673
+ s0, i0 = [], index
1674
+ loop do
1675
+ if has_terminal?('\G[a-zA-z0-9_-]', true, index)
1676
+ r1 = true
1677
+ @index += 1
1678
+ else
1679
+ r1 = nil
1680
+ end
1681
+ if r1
1682
+ s0 << r1
1683
+ else
1684
+ break
1685
+ end
1686
+ end
1687
+ if s0.empty?
1688
+ @index = i0
1689
+ r0 = nil
1690
+ else
1691
+ r0 = instantiate_node(SyntaxNode,input, i0...index, s0)
1692
+ end
1693
+
1694
+ node_cache[:single_field_name][start_index] = r0
1695
+
1696
+ r0
1697
+ end
1698
+
1699
+ end
1700
+
1701
+ class MongoQueryGrammarParser < Treetop::Runtime::CompiledParser
1702
+ include MongoQueryGrammar
1703
+ end