Class: ExcelFormula::Parser

Inherits:
ANTLR3::Parser
  • Object
show all
Includes:
TokenData
Defined in:
lib/surpass/ExcelFormulaParser.rb

Defined Under Namespace

Classes: DFA10, DFA12, DFA13, DFA15, DFA16, DFA18, DFA2, DFA3, DFA5, DFA7, DFA8, DFA9

Constant Summary collapse

RULE_METHODS =
[:formula, :expr, :prec0_expr, :prec1_expr, :prec2_expr, 
:prec3_expr, :prec4_expr, :prec5_expr, :primary, :expr_list, 
:sheet].freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(input, options = {}) ⇒ Parser

Returns a new instance of Parser.



116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
# File 'lib/surpass/ExcelFormulaParser.rb', line 116

def initialize(input, options = {})
  super(input, options)

  # - - - - - - begin action @parser::init - - - - - -
  # ExcelFormula.g


    @rpn = ''
    @sheet_references = []
    @xcall_references = []

  # - - - - - - end action @parser::init - - - - - - -


end

Instance Attribute Details

#rpnObject

Returns the value of attribute rpn.



132
133
134
# File 'lib/surpass/ExcelFormulaParser.rb', line 132

def rpn
  @rpn
end

Instance Method Details

#expr(arg_type) ⇒ Object

parser rule expr

(in ExcelFormula.g) 28:1: expr : prec0_expr ( ( EQ | NE | GT | LT | GE | LE ) prec0_expr )* ;



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
# File 'lib/surpass/ExcelFormulaParser.rb', line 168

def expr(arg_type)
  # -> uncomment the next line to manually enable rule tracing
  # trace_in(__method__, 2)

  begin
    # at line 29:7: prec0_expr[arg_type] ( ( EQ | NE | GT | LT | GE | LE ) prec0_expr[arg_type] )*
    @state.following.push(TOKENS_FOLLOWING_prec0_expr_IN_expr_75)
    prec0_expr(arg_type)
    @state.following.pop
    # at line 30:9: ( ( EQ | NE | GT | LT | GE | LE ) prec0_expr[arg_type] )*
    loop do # decision 2
      alt_2 = 2
      alt_2 = @dfa2.predict(@input)
      case alt_2
      when 1
        # at line 31:13: ( EQ | NE | GT | LT | GE | LE ) prec0_expr[arg_type]
        # at line 31:13: ( EQ | NE | GT | LT | GE | LE )
        alt_1 = 6
        case look_1 = @input.peek(1)
        when EQ then alt_1 = 1
        when NE then alt_1 = 2
        when GT then alt_1 = 3
        when LT then alt_1 = 4
        when GE then alt_1 = 5
        when LE then alt_1 = 6
        else
          nvae = NoViableAlternative("", 1, 0)
          raise nvae
        end
        case alt_1
        when 1
          # at line 32:19: EQ
          match(EQ, TOKENS_FOLLOWING_EQ_IN_expr_120)
          # --> action
           op = [PTGEQ].pack('C') 
          # <-- action

        when 2
          # at line 33:19: NE
          match(NE, TOKENS_FOLLOWING_NE_IN_expr_142)
          # --> action
           op = [PTGNE].pack('C') 
          # <-- action

        when 3
          # at line 34:19: GT
          match(GT, TOKENS_FOLLOWING_GT_IN_expr_164)
          # --> action
           op = [PTGGT].pack('C') 
          # <-- action

        when 4
          # at line 35:19: LT
          match(LT, TOKENS_FOLLOWING_LT_IN_expr_186)
          # --> action
           op = [PTGLT].pack('C') 
          # <-- action

        when 5
          # at line 36:19: GE
          match(GE, TOKENS_FOLLOWING_GE_IN_expr_208)
          # --> action
           op = [PTGGE].pack('C') 
          # <-- action

        when 6
          # at line 37:19: LE
          match(LE, TOKENS_FOLLOWING_LE_IN_expr_230)
          # --> action
           op = [PTGLE].pack('C') 
          # <-- action

        end
        @state.following.push(TOKENS_FOLLOWING_prec0_expr_IN_expr_261)
        prec0_expr(arg_type)
        @state.following.pop
        # --> action
         @rpn += op 
        # <-- action

      else
        break # out of loop for decision 2
      end
    end # loop for decision 2

  rescue ANTLR3::Error::RecognitionError => re
    report_error(re)
    recover(re)

  ensure
    # -> uncomment the next line to manually enable rule tracing
    # trace_out(__method__, 2)

  end
  
  return 
end

#expr_list(arg_type_list, min_argc, max_argc) ⇒ Object

parser rule expr_list

(in ExcelFormula.g) 288:1: expr_list[arg_type_list, min_argc, max_argc] returns [arg_cnt] : ( ( expr ( ( SEMICOLON | COMMA ) ( expr | ) )* ) | );



1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
# File 'lib/surpass/ExcelFormulaParser.rb', line 1022

def expr_list(arg_type_list, min_argc, max_argc)
  # -> uncomment the next line to manually enable rule tracing
  # trace_in(__method__, 10)
  arg_cnt = nil
  # - - - - @init action - - - -

    arg_cnt = 0

    # Set these for processing first argument, 
    # it's simpler because first argument type can't be '...'
    arg_type = arg_type_list.first

    # need to check for '-' for a fn with no arguments
    arg_cnt += 1 unless arg_type === '-'


  begin
    # at line 300:5: ( ( expr[arg_type] ( ( SEMICOLON | COMMA ) ( expr[arg_type] | ) )* ) | )
    alt_18 = 2
    alt_18 = @dfa18.predict(@input)
    case alt_18
    when 1
      # at line 301:5: ( expr[arg_type] ( ( SEMICOLON | COMMA ) ( expr[arg_type] | ) )* )
      # at line 301:5: ( expr[arg_type] ( ( SEMICOLON | COMMA ) ( expr[arg_type] | ) )* )
      # at line 301:6: expr[arg_type] ( ( SEMICOLON | COMMA ) ( expr[arg_type] | ) )*
      @state.following.push(TOKENS_FOLLOWING_expr_IN_expr_list_1540)
      expr(arg_type)
      @state.following.pop
      # at line 302:9: ( ( SEMICOLON | COMMA ) ( expr[arg_type] | ) )*
      loop do # decision 17
        alt_17 = 2
        look_17_0 = @input.peek(1)

        if (look_17_0.between?(SEMICOLON, COMMA)) 
          alt_17 = 1

        end
        case alt_17
        when 1
          # at line 303:14: ( SEMICOLON | COMMA ) ( expr[arg_type] | )
          if @input.peek(1).between?(SEMICOLON, COMMA)
            @input.consume
            @state.error_recovery = false
          else
            mse = MismatchedSet(nil)
            raise mse
          end


          # --> action
           arg_cnt += 1 
          # <-- action
          # at line 304:17: ( expr[arg_type] | )
          alt_16 = 2
          alt_16 = @dfa16.predict(@input)
          case alt_16
          when 1
            # at line 306:22: expr[arg_type]
            @state.following.push(TOKENS_FOLLOWING_expr_IN_expr_list_1634)
            expr(arg_type)
            @state.following.pop
            # --> action

                                    if arg_cnt - 2 < arg_type_list.size
                                       arg_type = arg_type_list[arg_cnt - 2]
                                    else
                                      if arg_type_list.last === "..."
                                         # e.g. "V R ..." arbitrary number of args of type R
                                         # this will always be last element in arg_type_list
                                         # 2nd to last element will provide type
                                         arg_type = arg_type_list[arg_type_list.size - 2]
                                      else
                                        # Just read last element normally.
                                        arg_type = arg_type_list[arg_cnt - 2]
                                      end
                                    end
                                 
            # <-- action

          when 2
            # at line 323:19: 
            # --> action
             @rpn += [PTGMISSARG].pack("C") 
            # <-- action

          end

        else
          break # out of loop for decision 17
        end
      end # loop for decision 17


    when 2
      # at line 328:5: 

    end
  rescue ANTLR3::Error::RecognitionError => re
    report_error(re)
    recover(re)

  ensure
    # -> uncomment the next line to manually enable rule tracing
    # trace_out(__method__, 10)

  end
  
  return arg_cnt
end

#formulaObject

parser rule formula

(in ExcelFormula.g) 24:1: formula : expr ;



140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
# File 'lib/surpass/ExcelFormulaParser.rb', line 140

def formula
  # -> uncomment the next line to manually enable rule tracing
  # trace_in(__method__, 1)

  begin
    # at line 25:7: expr[\"V\"]
    @state.following.push(TOKENS_FOLLOWING_expr_IN_formula_56)
    expr("V")
    @state.following.pop

  rescue ANTLR3::Error::RecognitionError => re
    report_error(re)
    recover(re)

  ensure
    # -> uncomment the next line to manually enable rule tracing
    # trace_out(__method__, 1)

  end
  
  return 
end

#prec0_expr(arg_type) ⇒ Object

parser rule prec0_expr

(in ExcelFormula.g) 44:1: prec0_expr : prec1_expr ( ( CONCAT ) prec1_expr )* ;



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
# File 'lib/surpass/ExcelFormulaParser.rb', line 271

def prec0_expr(arg_type)
  # -> uncomment the next line to manually enable rule tracing
  # trace_in(__method__, 3)

  begin
    # at line 45:7: prec1_expr[arg_type] ( ( CONCAT ) prec1_expr[arg_type] )*
    @state.following.push(TOKENS_FOLLOWING_prec1_expr_IN_prec0_expr_293)
    prec1_expr(arg_type)
    @state.following.pop
    # at line 46:9: ( ( CONCAT ) prec1_expr[arg_type] )*
    loop do # decision 3
      alt_3 = 2
      alt_3 = @dfa3.predict(@input)
      case alt_3
      when 1
        # at line 47:13: ( CONCAT ) prec1_expr[arg_type]
        # at line 47:13: ( CONCAT )
        # at line 48:17: CONCAT
        match(CONCAT, TOKENS_FOLLOWING_CONCAT_IN_prec0_expr_336)
        # --> action
         op = [PTGCONCAT].pack('C') 
        # <-- action

        @state.following.push(TOKENS_FOLLOWING_prec1_expr_IN_prec0_expr_366)
        prec1_expr(arg_type)
        @state.following.pop
        # --> action
         @rpn += op 
        # <-- action

      else
        break # out of loop for decision 3
      end
    end # loop for decision 3

  rescue ANTLR3::Error::RecognitionError => re
    report_error(re)
    recover(re)

  ensure
    # -> uncomment the next line to manually enable rule tracing
    # trace_out(__method__, 3)

  end
  
  return 
end

#prec1_expr(arg_type) ⇒ Object

parser rule prec1_expr

(in ExcelFormula.g) 54:1: prec1_expr : prec2_expr ( ( ADD | SUB ) prec2_expr )* ;



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
# File 'lib/surpass/ExcelFormulaParser.rb', line 324

def prec1_expr(arg_type)
  # -> uncomment the next line to manually enable rule tracing
  # trace_in(__method__, 4)

  begin
    # at line 55:7: prec2_expr[arg_type] ( ( ADD | SUB ) prec2_expr[arg_type] )*
    @state.following.push(TOKENS_FOLLOWING_prec2_expr_IN_prec1_expr_398)
    prec2_expr(arg_type)
    @state.following.pop
    # at line 56:9: ( ( ADD | SUB ) prec2_expr[arg_type] )*
    loop do # decision 5
      alt_5 = 2
      alt_5 = @dfa5.predict(@input)
      case alt_5
      when 1
        # at line 57:13: ( ADD | SUB ) prec2_expr[arg_type]
        # at line 57:13: ( ADD | SUB )
        alt_4 = 2
        look_4_0 = @input.peek(1)

        if (look_4_0 == ADD) 
          alt_4 = 1
        elsif (look_4_0 == SUB) 
          alt_4 = 2
        else
        nvae = NoViableAlternative("", 4, 0)
          raise nvae
        end
        case alt_4
        when 1
          # at line 58:19: ADD
          match(ADD, TOKENS_FOLLOWING_ADD_IN_prec1_expr_443)
          # --> action
           op = [PTGADD].pack('C') 
          # <-- action

        when 2
          # at line 59:19: SUB
          match(SUB, TOKENS_FOLLOWING_SUB_IN_prec1_expr_465)
          # --> action
           op = [PTGSUB].pack('C') 
          # <-- action

        end
        @state.following.push(TOKENS_FOLLOWING_prec2_expr_IN_prec1_expr_495)
        prec2_expr(arg_type)
        @state.following.pop
        # --> action
         @rpn += op 
        # <-- action

      else
        break # out of loop for decision 5
      end
    end # loop for decision 5

  rescue ANTLR3::Error::RecognitionError => re
    report_error(re)
    recover(re)

  ensure
    # -> uncomment the next line to manually enable rule tracing
    # trace_out(__method__, 4)

  end
  
  return 
end

#prec2_expr(arg_type) ⇒ Object

parser rule prec2_expr

(in ExcelFormula.g) 66:1: prec2_expr : prec3_expr ( ( MUL | DIV ) prec3_expr )* ;



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
# File 'lib/surpass/ExcelFormulaParser.rb', line 398

def prec2_expr(arg_type)
  # -> uncomment the next line to manually enable rule tracing
  # trace_in(__method__, 5)

  begin
    # at line 67:7: prec3_expr[arg_type] ( ( MUL | DIV ) prec3_expr[arg_type] )*
    @state.following.push(TOKENS_FOLLOWING_prec3_expr_IN_prec2_expr_528)
    prec3_expr(arg_type)
    @state.following.pop
    # at line 68:9: ( ( MUL | DIV ) prec3_expr[arg_type] )*
    loop do # decision 7
      alt_7 = 2
      alt_7 = @dfa7.predict(@input)
      case alt_7
      when 1
        # at line 69:13: ( MUL | DIV ) prec3_expr[arg_type]
        # at line 69:13: ( MUL | DIV )
        alt_6 = 2
        look_6_0 = @input.peek(1)

        if (look_6_0 == MUL) 
          alt_6 = 1
        elsif (look_6_0 == DIV) 
          alt_6 = 2
        else
        nvae = NoViableAlternative("", 6, 0)
          raise nvae
        end
        case alt_6
        when 1
          # at line 70:19: MUL
          match(MUL, TOKENS_FOLLOWING_MUL_IN_prec2_expr_573)
          # --> action
           op = [PTGMUL].pack('C') 
          # <-- action

        when 2
          # at line 71:19: DIV
          match(DIV, TOKENS_FOLLOWING_DIV_IN_prec2_expr_595)
          # --> action
           op = [PTGDIV].pack('C') 
          # <-- action

        end
        @state.following.push(TOKENS_FOLLOWING_prec3_expr_IN_prec2_expr_625)
        prec3_expr(arg_type)
        @state.following.pop
        # --> action
         @rpn += op 
        # <-- action

      else
        break # out of loop for decision 7
      end
    end # loop for decision 7

  rescue ANTLR3::Error::RecognitionError => re
    report_error(re)
    recover(re)

  ensure
    # -> uncomment the next line to manually enable rule tracing
    # trace_out(__method__, 5)

  end
  
  return 
end

#prec3_expr(arg_type) ⇒ Object

parser rule prec3_expr

(in ExcelFormula.g) 77:1: prec3_expr : prec4_expr ( ( POWER ) prec4_expr )* ;



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
# File 'lib/surpass/ExcelFormulaParser.rb', line 472

def prec3_expr(arg_type)
  # -> uncomment the next line to manually enable rule tracing
  # trace_in(__method__, 6)

  begin
    # at line 78:7: prec4_expr[arg_type] ( ( POWER ) prec4_expr[arg_type] )*
    @state.following.push(TOKENS_FOLLOWING_prec4_expr_IN_prec3_expr_657)
    prec4_expr(arg_type)
    @state.following.pop
    # at line 79:9: ( ( POWER ) prec4_expr[arg_type] )*
    loop do # decision 8
      alt_8 = 2
      alt_8 = @dfa8.predict(@input)
      case alt_8
      when 1
        # at line 80:13: ( POWER ) prec4_expr[arg_type]
        # at line 80:13: ( POWER )
        # at line 81:17: POWER
        match(POWER, TOKENS_FOLLOWING_POWER_IN_prec3_expr_700)
        # --> action
         op = [PTGPOWER].pack('C') 
        # <-- action

        @state.following.push(TOKENS_FOLLOWING_prec4_expr_IN_prec3_expr_730)
        prec4_expr(arg_type)
        @state.following.pop
        # --> action
         @rpn += op 
        # <-- action

      else
        break # out of loop for decision 8
      end
    end # loop for decision 8

  rescue ANTLR3::Error::RecognitionError => re
    report_error(re)
    recover(re)

  ensure
    # -> uncomment the next line to manually enable rule tracing
    # trace_out(__method__, 6)

  end
  
  return 
end

#prec4_expr(arg_type) ⇒ Object

parser rule prec4_expr

(in ExcelFormula.g) 87:1: prec4_expr : prec5_expr ( PERCENT )? ;



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
# File 'lib/surpass/ExcelFormulaParser.rb', line 525

def prec4_expr(arg_type)
  # -> uncomment the next line to manually enable rule tracing
  # trace_in(__method__, 7)

  begin
    # at line 88:7: prec5_expr[arg_type] ( PERCENT )?
    @state.following.push(TOKENS_FOLLOWING_prec5_expr_IN_prec4_expr_762)
    prec5_expr(arg_type)
    @state.following.pop
    # at line 89:9: ( PERCENT )?
    alt_9 = 2
    alt_9 = @dfa9.predict(@input)
    case alt_9
    when 1
      # at line 90:13: PERCENT
      match(PERCENT, TOKENS_FOLLOWING_PERCENT_IN_prec4_expr_787)
      # --> action
       @rpn += [PTGPERCENT].pack('C') 
      # <-- action

    end

  rescue ANTLR3::Error::RecognitionError => re
    report_error(re)
    recover(re)

  ensure
    # -> uncomment the next line to manually enable rule tracing
    # trace_out(__method__, 7)

  end
  
  return 
end

#prec5_expr(arg_type) ⇒ Object

parser rule prec5_expr

(in ExcelFormula.g) 94:1: prec5_expr : ( primary | SUB primary );



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
# File 'lib/surpass/ExcelFormulaParser.rb', line 565

def prec5_expr(arg_type)
  # -> uncomment the next line to manually enable rule tracing
  # trace_in(__method__, 8)

  begin
    # at line 95:5: ( primary[arg_type] | SUB primary[arg_type] )
    alt_10 = 2
    alt_10 = @dfa10.predict(@input)
    case alt_10
    when 1
      # at line 95:7: primary[arg_type]
      @state.following.push(TOKENS_FOLLOWING_primary_IN_prec5_expr_818)
      primary(arg_type)
      @state.following.pop

    when 2
      # at line 96:7: SUB primary[arg_type]
      match(SUB, TOKENS_FOLLOWING_SUB_IN_prec5_expr_827)
      @state.following.push(TOKENS_FOLLOWING_primary_IN_prec5_expr_829)
      primary(arg_type)
      @state.following.pop
      # --> action
       @rpn += [PTGUMINUS].pack('C') 
      # <-- action

    end
  rescue ANTLR3::Error::RecognitionError => re
    report_error(re)
    recover(re)

  ensure
    # -> uncomment the next line to manually enable rule tracing
    # trace_out(__method__, 8)

  end
  
  return 
end

#primary(arg_type) ⇒ Object

parser rule primary

(in ExcelFormula.g) 99:1: primary : ( TRUE_CONST | FALSE_CONST | str_tok= STR_CONST | int_tok= INT_CONST | num_tok= NUM_CONST | ref2d_tok= REF2D | ref2d1_tok= REF2D COLON ref2d2_tok= REF2D | LP expr RP | sheet1= sheet ( COLON sheet2= sheet )? BANG ref3d_ref2d= REF2D ( COLON ref3d_ref2d2= REF2D )? | FUNC_IF LP expr ( SEMICOLON | COMMA ) expr ( SEMICOLON | COMMA ) expr RP | FUNC_CHOOSE LP expr ( ( SEMICOLON | COMMA ) ( expr | ) )* RP | name_tok= NAME | func_tok= NAME LP arg_count= expr_list[arg_type_list, min_argc, max_argc] RP );



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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
# File 'lib/surpass/ExcelFormulaParser.rb', line 609

def primary(arg_type)
  # -> uncomment the next line to manually enable rule tracing
  # trace_in(__method__, 9)
  str_tok = nil
  int_tok = nil
  num_tok = nil
  ref2d_tok = nil
  ref2d1_tok = nil
  ref2d2_tok = nil
  ref3d_ref2d = nil
  ref3d_ref2d2 = nil
  name_tok = nil
  func_tok = nil
  sheet1 = nil
  sheet2 = nil
  arg_count = nil

  begin
    # at line 100:5: ( TRUE_CONST | FALSE_CONST | str_tok= STR_CONST | int_tok= INT_CONST | num_tok= NUM_CONST | ref2d_tok= REF2D | ref2d1_tok= REF2D COLON ref2d2_tok= REF2D | LP expr[arg_type] RP | sheet1= sheet ( COLON sheet2= sheet )? BANG ref3d_ref2d= REF2D ( COLON ref3d_ref2d2= REF2D )? | FUNC_IF LP expr[\"V\"] ( SEMICOLON | COMMA ) expr[arg_type] ( SEMICOLON | COMMA ) expr[arg_type] RP | FUNC_CHOOSE LP expr[\"V\"] ( ( SEMICOLON | COMMA ) ( expr[arg_type] | ) )* RP | name_tok= NAME | func_tok= NAME LP arg_count= expr_list[arg_type_list, min_argc, max_argc] RP )
    alt_15 = 13
    alt_15 = @dfa15.predict(@input)
    case alt_15
    when 1
      # at line 100:7: TRUE_CONST
      match(TRUE_CONST, TOKENS_FOLLOWING_TRUE_CONST_IN_primary_850)
      # --> action

                  @rpn += [PTGBOOL, 1].pack("C2")
              
      # <-- action

    when 2
      # at line 104:7: FALSE_CONST
      match(FALSE_CONST, TOKENS_FOLLOWING_FALSE_CONST_IN_primary_868)
      # --> action

                  @rpn += [PTGBOOL, 0].pack("C2")
              
      # <-- action

    when 3
      # at line 108:7: str_tok= STR_CONST
      str_tok = match(STR_CONST, TOKENS_FOLLOWING_STR_CONST_IN_primary_890)
      # --> action

                  s = str_tok.text.gsub("\"", "")
                  @rpn += [PTGSTR].pack("C") + [s.length].pack('v') + s
              
      # <-- action

    when 4
      # at line 113:7: int_tok= INT_CONST
      int_tok = match(INT_CONST, TOKENS_FOLLOWING_INT_CONST_IN_primary_912)
      # --> action

              int_value = int_tok.text.to_i
              if int_value <= 65535
                  @rpn += [PTGINT, int_value].pack("Cv")
              else
                  @rpn += [PTGNUM, int_value.to_f].pack("CE")
              end
          
      # <-- action

    when 5
      # at line 122:7: num_tok= NUM_CONST
      num_tok = match(NUM_CONST, TOKENS_FOLLOWING_NUM_CONST_IN_primary_930)
      # --> action

                  @rpn += [PTGNUM, num_tok.text.to_f].pack("CE")
              
      # <-- action

    when 6
      # at line 126:7: ref2d_tok= REF2D
      ref2d_tok = match(REF2D, TOKENS_FOLLOWING_REF2D_IN_primary_952)
      # --> action

                r, c = Utilities.cell_to_packed_rowcol(ref2d_tok.text) 
                ptg = PTGREFR + RVA_DELTA_REF[arg_type]
                @rpn += [ptg, r, c].pack("Cv2")
              
      # <-- action

    when 7
      # at line 132:7: ref2d1_tok= REF2D COLON ref2d2_tok= REF2D
      ref2d1_tok = match(REF2D, TOKENS_FOLLOWING_REF2D_IN_primary_974)
      match(COLON, TOKENS_FOLLOWING_COLON_IN_primary_976)
      ref2d2_tok = match(REF2D, TOKENS_FOLLOWING_REF2D_IN_primary_982)
      # --> action

                  r1, c1 = Utilities.cell_to_packed_rowcol(ref2d1_tok.text) 
                  r2, c2 = Utilities.cell_to_packed_rowcol(ref2d2_tok.text)
                  ptg = PTGAREAR + RVA_DELTA_AREA[arg_type]
                  @rpn += [ptg, r1, r2, c1, c2].pack("Cv4")
              
      # <-- action

    when 8
      # at line 139:7: LP expr[arg_type] RP
      match(LP, TOKENS_FOLLOWING_LP_IN_primary_1000)
      @state.following.push(TOKENS_FOLLOWING_expr_IN_primary_1002)
      expr(arg_type)
      @state.following.pop
      match(RP, TOKENS_FOLLOWING_RP_IN_primary_1005)
      # --> action

                  @rpn += [PTGPAREN].pack('C')
              
      # <-- action

    when 9
      # at line 143:7: sheet1= sheet ( COLON sheet2= sheet )? BANG ref3d_ref2d= REF2D ( COLON ref3d_ref2d2= REF2D )?
      @state.following.push(TOKENS_FOLLOWING_sheet_IN_primary_1028)
      sheet1 = sheet
      @state.following.pop
      # --> action
       
                  sheet2 = sheet1
              
      # <-- action
      # at line 147:9: ( COLON sheet2= sheet )?
      alt_11 = 2
      look_11_0 = @input.peek(1)

      if (look_11_0 == COLON) 
        alt_11 = 1
      end
      case alt_11
      when 1
        # at line 147:11: COLON sheet2= sheet
        match(COLON, TOKENS_FOLLOWING_COLON_IN_primary_1050)
        @state.following.push(TOKENS_FOLLOWING_sheet_IN_primary_1056)
        sheet2 = sheet
        @state.following.pop

      end
      match(BANG, TOKENS_FOLLOWING_BANG_IN_primary_1061)
      ref3d_ref2d = match(REF2D, TOKENS_FOLLOWING_REF2D_IN_primary_1065)
      # --> action

                  ptg = PTGREF3DR + RVA_DELTA_REF[arg_type]
                  r1, c1 = Utilities.cell_to_packed_rowcol(ref3d_ref2d.text)
                  rpn_ref2d = [0x0000, r1, c1].pack("v3")
              
      # <-- action
      # at line 153:9: ( COLON ref3d_ref2d2= REF2D )?
      alt_12 = 2
      alt_12 = @dfa12.predict(@input)
      case alt_12
      when 1
        # at line 153:11: COLON ref3d_ref2d2= REF2D
        match(COLON, TOKENS_FOLLOWING_COLON_IN_primary_1087)
        ref3d_ref2d2 = match(REF2D, TOKENS_FOLLOWING_REF2D_IN_primary_1092)
        # --> action

                        ptg = PTGAREA3DR + RVA_DELTA_AREA[arg_type]
                        r2, c2 = Utilities.cell_to_packed_rowcol(ref3d_ref2d2.text)
                        rpn_ref2d = [0x0000, r1, r2, c1, c2].pack("v5")
                    
        # <-- action

      end
      # --> action

                  @rpn += [ptg].pack("C")
                  @sheet_references << [sheet1, sheet2, @rpn.size]
                  @rpn += rpn_ref2d
              
      # <-- action

    when 10
      # at line 165:7: FUNC_IF LP expr[\"V\"] ( SEMICOLON | COMMA ) expr[arg_type] ( SEMICOLON | COMMA ) expr[arg_type] RP
      match(FUNC_IF, TOKENS_FOLLOWING_FUNC_IF_IN_primary_1135)
      match(LP, TOKENS_FOLLOWING_LP_IN_primary_1145)
      @state.following.push(TOKENS_FOLLOWING_expr_IN_primary_1147)
      expr("V")
      @state.following.pop
      if @input.peek(1).between?(SEMICOLON, COMMA)
        @input.consume
        @state.error_recovery = false
      else
        mse = MismatchedSet(nil)
        raise mse
      end


      # --> action

                  @rpn += [PTGATTR, 0x02, 0].pack("C2v") # tAttrIf
                  pos0 = @rpn.size - 2
              
      # <-- action
      @state.following.push(TOKENS_FOLLOWING_expr_IN_primary_1176)
      expr(arg_type)
      @state.following.pop
      if @input.peek(1).between?(SEMICOLON, COMMA)
        @input.consume
        @state.error_recovery = false
      else
        mse = MismatchedSet(nil)
        raise mse
      end


      # --> action

                  @rpn += [PTGATTR, 0x08, 0].pack("C2v") # tAttrSkip
                  pos1 = @rpn.size - 2

                  @rpn = @rpn[0...pos0] + [pos1-pos0].pack("v") + @rpn[(pos0+2)...(@rpn.size)] 
              
      # <-- action
      @state.following.push(TOKENS_FOLLOWING_expr_IN_primary_1205)
      expr(arg_type)
      @state.following.pop
      match(RP, TOKENS_FOLLOWING_RP_IN_primary_1208)
      # --> action

                  @rpn += [PTGATTR, 0x08, 3].pack("C2v") # tAttrSkip
                  @rpn += [PTGFUNCVARR, 3, 1].pack("C2v") # 3 = nargs, 1 = IF func
                  pos2 = @rpn.size

                  @rpn = @rpn[0...pos1] + [pos2-(pos1+2)-1].pack("v") + @rpn[(pos1+2)...(@rpn.size)]
              
      # <-- action

    when 11
      # at line 186:7: FUNC_CHOOSE LP expr[\"V\"] ( ( SEMICOLON | COMMA ) ( expr[arg_type] | ) )* RP
      match(FUNC_CHOOSE, TOKENS_FOLLOWING_FUNC_CHOOSE_IN_primary_1226)
      # --> action

                  arg_type = "R"
                  rpn_chunks = []
              
      # <-- action
      match(LP, TOKENS_FOLLOWING_LP_IN_primary_1246)
      @state.following.push(TOKENS_FOLLOWING_expr_IN_primary_1248)
      expr("V")
      @state.following.pop
      # --> action

                  rpn_start = @rpn.size
                  ref_markers = [@sheet_references.size]
              
      # <-- action
      # at line 196:9: ( ( SEMICOLON | COMMA ) ( expr[arg_type] | ) )*
      loop do # decision 14
        alt_14 = 2
        look_14_0 = @input.peek(1)

        if (look_14_0.between?(SEMICOLON, COMMA)) 
          alt_14 = 1

        end
        case alt_14
        when 1
          # at line 197:13: ( SEMICOLON | COMMA ) ( expr[arg_type] | )
          if @input.peek(1).between?(SEMICOLON, COMMA)
            @input.consume
            @state.error_recovery = false
          else
            mse = MismatchedSet(nil)
            raise mse
          end


          # --> action
           mark = @rpn.size 
          # <-- action
          # at line 199:17: ( expr[arg_type] | )
          alt_13 = 2
          alt_13 = @dfa13.predict(@input)
          case alt_13
          when 1
            # at line 200:19: expr[arg_type]
            @state.following.push(TOKENS_FOLLOWING_expr_IN_primary_1345)
            expr(arg_type)
            @state.following.pop

          when 2
            # at line 201:19: 
            # --> action
             @rpn += [PTGMISSARG].pack("C") 
            # <-- action

          end
          # --> action

                              rem = @rpn.size - mark
                              rpn_chunks << @rpn[mark, rem]
                              ref_markers << @sheet_references.size
                          
          # <-- action

        else
          break # out of loop for decision 14
        end
      end # loop for decision 14
      match(RP, TOKENS_FOLLOWING_RP_IN_primary_1423)
      # --> action

                  @rpn = @rpn[0...rpn_start]
                  nc = rpn_chunks.length
                  chunklens = rpn_chunks.collect {|c| c.length}
                  skiplens = [0] * nc
                  skiplens[nc-1] = 3

                  (nc-1).downto(1) do |i|
                    skiplens[i-1] = skiplens[i] + chunklens[i] + 4
                  end
                  jump_pos = [2*nc + 2]

                  (0...nc).each do |i|
                    jump_pos << (jump_pos.last + chunklens[i] + 4)
                  end
                  chunk_shift = 2*nc + 6 # size of tAttrChoose

                  (0...nc).each do |i|
                    (ref_markers[i]...ref_markers[i+1]).each do |r|
                      ref = @sheet_references[r]
                      @sheet_references[r] = [ref[0], ref[1], ref[2] + chunk_shift]
                    end
                    chunk_shift += 4 # size of tAttrSkip
                  end

                  choose_rpn = []
                  choose_rpn << [PTGATTR, 0x04, nc].pack("CCv") # 0x04 is tAttrChoose
                  choose_rpn << jump_pos.pack("v*")
                  
                  (0...nc).each do |i|
                    choose_rpn << rpn_chunks[i]
                    choose_rpn << [PTGATTR, 0x08, skiplens[i]].pack("CCv") # 0x08 is tAttrSkip
                  end
                  choose_rpn << [PTGFUNCVARV, nc+1, 100].pack("CCv") # 100 is CHOOSE fn
                  @rpn += choose_rpn.join
              
      # <-- action

    when 12
      # at line 246:7: name_tok= NAME
      name_tok = match(NAME, TOKENS_FOLLOWING_NAME_IN_primary_1445)
      # --> action

                raise "[formula] found unexpected NAME token #{name_tok.text}"
              
      # <-- action

    when 13
      # at line 250:7: func_tok= NAME LP arg_count= expr_list[arg_type_list, min_argc, max_argc] RP
      func_tok = match(NAME, TOKENS_FOLLOWING_NAME_IN_primary_1467)
      # --> action

                func_toku = func_tok.text.upcase
                if STD_FUNC_BY_NAME.has_key?(func_toku)
                    opcode, min_argc, max_argc, func_type, arg_type_str = STD_FUNC_BY_NAME[func_toku]
                    arg_type_list = arg_type_str.split
                else
                    raise "[formula] unknown function #{func_tok.text}"
                end
                xcall = (opcode < 0)

                if xcall
                  @xcall_references << [func_toku, @rpn.size + 1]
                  @rpn += [PTGNAMEXR, 0xadde, 0xefbe, 0x0000].pack("Cvvv")
                end
              
      # <-- action
      match(LP, TOKENS_FOLLOWING_LP_IN_primary_1487)
      @state.following.push(TOKENS_FOLLOWING_expr_list_IN_primary_1493)
      arg_count = expr_list(arg_type_list, min_argc, max_argc)
      @state.following.pop
      match(RP, TOKENS_FOLLOWING_RP_IN_primary_1496)
      # --> action

                if (arg_count > max_argc) || (arg_count < min_argc)
                   raise "incorrect number #{arg_count} of parameters for function: #{func_tok.text}"
                end

                if xcall
                    func_ptg = PTGFUNCVARR + RVA_DELTA[func_type]
                    @rpn += [func_ptg, arg_count + 1, 255].pack("CCv") # 255 is magic XCALL function
                elsif (min_argc == max_argc)
                    func_ptg = PTGFUNCR + RVA_DELTA[func_type]
                    @rpn += [func_ptg, opcode].pack("Cv") 
                elsif (arg_count == 1) && (func_tok.text.upcase === "SUM")
                    @rpn += [PTGATTR, 0x10, 0].pack("CCv") # tAttrSum
                else
                    func_ptg = PTGFUNCVARR + RVA_DELTA[func_type]
                    @rpn += [func_ptg, arg_count, opcode].pack("CCv")
                end
              
      # <-- action

    end
  rescue ANTLR3::Error::RecognitionError => re
    report_error(re)
    recover(re)

  ensure
    # -> uncomment the next line to manually enable rule tracing
    # trace_out(__method__, 9)

  end
  
  return 
end

#sheetObject

parser rule sheet

(in ExcelFormula.g) 330:1: sheet returns [ref] : (sheet_ref_name= NAME | sheet_ref_int= INT_CONST | sheet_ref_quote= QUOTENAME );



1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
# File 'lib/surpass/ExcelFormulaParser.rb', line 1137

def sheet
  # -> uncomment the next line to manually enable rule tracing
  # trace_in(__method__, 11)
  ref = nil
  sheet_ref_name = nil
  sheet_ref_int = nil
  sheet_ref_quote = nil

  begin
    # at line 331:5: (sheet_ref_name= NAME | sheet_ref_int= INT_CONST | sheet_ref_quote= QUOTENAME )
    alt_19 = 3
    case look_19 = @input.peek(1)
    when NAME then alt_19 = 1
    when INT_CONST then alt_19 = 2
    when QUOTENAME then alt_19 = 3
    else
      nvae = NoViableAlternative("", 19, 0)
      raise nvae
    end
    case alt_19
    when 1
      # at line 331:7: sheet_ref_name= NAME
      sheet_ref_name = match(NAME, TOKENS_FOLLOWING_NAME_IN_sheet_1768)
      # --> action
       ref = sheet_ref_name.text 
      # <-- action

    when 2
      # at line 333:7: sheet_ref_int= INT_CONST
      sheet_ref_int = match(INT_CONST, TOKENS_FOLLOWING_INT_CONST_IN_sheet_1788)
      # --> action
       ref = sheet_ref_int.text 
      # <-- action

    when 3
      # at line 335:7: sheet_ref_quote= QUOTENAME
      sheet_ref_quote = match(QUOTENAME, TOKENS_FOLLOWING_QUOTENAME_IN_sheet_1808)
      # --> action
       ref = sheet_ref_quote.text[1, len(sheet_ref_quote.text) - 1].replace("''", "'") 
      # <-- action

    end
  rescue ANTLR3::Error::RecognitionError => re
    report_error(re)
    recover(re)

  ensure
    # -> uncomment the next line to manually enable rule tracing
    # trace_out(__method__, 11)

  end
  
  return ref
end