Class: Solver::Parser

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

Constant Summary collapse

RULE_METHODS =
[ :evaluate, :expression, :mult, :log, :exp, :atom ].freeze
TOKENS_FOLLOWING_expression_IN_evaluate_139 =
TOKENS_FOLLOWING_mult_IN_expression_160 =
TOKENS_FOLLOWING_PLUS_IN_expression_168 =
TOKENS_FOLLOWING_mult_IN_expression_172 =
TOKENS_FOLLOWING_MINUS_IN_expression_182 =
TOKENS_FOLLOWING_mult_IN_expression_186 =
TOKENS_FOLLOWING_log_IN_mult_216 =
TOKENS_FOLLOWING_MULT_IN_mult_224 =
TOKENS_FOLLOWING_log_IN_mult_228 =
TOKENS_FOLLOWING_DIV_IN_mult_238 =
TOKENS_FOLLOWING_log_IN_mult_242 =
TOKENS_FOLLOWING_T__19_IN_log_268 =
TOKENS_FOLLOWING_exp_IN_log_272 =
TOKENS_FOLLOWING_exp_IN_log_282 =
TOKENS_FOLLOWING_atom_IN_exp_303 =
TOKENS_FOLLOWING_EXP_IN_exp_307 =
TOKENS_FOLLOWING_atom_IN_exp_311 =
TOKENS_FOLLOWING_INT_IN_atom_341 =
TOKENS_FOLLOWING_FLOAT_IN_atom_356 =
TOKENS_FOLLOWING_LPAREN_IN_atom_367 =
TOKENS_FOLLOWING_expression_IN_atom_371 =
TOKENS_FOLLOWING_RPAREN_IN_atom_375 =
TOKENS_FOLLOWING_MODVAR_IN_atom_386 =
TOKENS_FOLLOWING_UNMODVAR_IN_atom_400 =

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

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

Returns a new instance of Parser.



109
110
111
112
113
# File 'lib/plympton/SolverParser.rb', line 109

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


end

Instance Attribute Details

#expressionCacheObject

Returns the value of attribute expressionCache.



115
116
117
# File 'lib/plympton/SolverParser.rb', line 115

def expressionCache
  @expressionCache
end

#objectCacheObject

Returns the value of attribute objectCache.



116
117
118
# File 'lib/plympton/SolverParser.rb', line 116

def objectCache
  @objectCache
end

Instance Method Details

#atomObject

parser rule atom

(in solver.g) 82:1: atom returns [result] : (a= INT | a= FLOAT | LPAREN r= expression RPAREN | a= MODVAR | a= UNMODVAR ) ;



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
# File 'lib/plympton/SolverParser.rb', line 431

def atom
  # -> uncomment the next line to manually enable rule tracing
  # trace_in( __method__, 6 )
  result = nil
  a = nil
  r = nil

  begin
    # at line 83:7: (a= INT | a= FLOAT | LPAREN r= expression RPAREN | a= MODVAR | a= UNMODVAR )
    # at line 83:7: (a= INT | a= FLOAT | LPAREN r= expression RPAREN | a= MODVAR | a= UNMODVAR )
    alt_5 = 5
    case look_5 = @input.peek( 1 )
    when INT then alt_5 = 1
    when FLOAT then alt_5 = 2
    when LPAREN then alt_5 = 3
    when MODVAR then alt_5 = 4
    when UNMODVAR then alt_5 = 5
    else
      raise NoViableAlternative( "", 5, 0 )
    end
    case alt_5
    when 1
      # at line 83:8: a= INT
      a = match( INT, TOKENS_FOLLOWING_INT_IN_atom_341 )
      # --> action
      result = BigDecimal(a.text)
      # <-- action

    when 2
      # at line 84:8: a= FLOAT
      a = match( FLOAT, TOKENS_FOLLOWING_FLOAT_IN_atom_356 )
      # --> action
      result = BigDecimal(a.text)
      # <-- action

    when 3
      # at line 85:8: LPAREN r= expression RPAREN
      match( LPAREN, TOKENS_FOLLOWING_LPAREN_IN_atom_367 )
      @state.following.push( TOKENS_FOLLOWING_expression_IN_atom_371 )
      r = expression
      @state.following.pop
      # --> action
       result = r 
      # <-- action
      match( RPAREN, TOKENS_FOLLOWING_RPAREN_IN_atom_375 )

    when 4
      # at line 86:8: a= MODVAR
      a = match( MODVAR, TOKENS_FOLLOWING_MODVAR_IN_atom_386 )
      # --> action

                  result = @objectCache.send(a.text)
            
      # <-- action

    when 5
      # at line 89:8: a= UNMODVAR
      a = match( UNMODVAR, TOKENS_FOLLOWING_UNMODVAR_IN_atom_400 )
      # --> action
       
                  case a.text 
                    when 'R'
                      result = @objectCache.runtime 
                    when 'U'
                      result = @objectCache.send(a.text)
                    when 'V'
                      result = @objectCache.send(a.text)
                    else
                      result = BigDecimal("0")
                    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__, 6 )

  end
  
  return result
end

#evaluateObject

parser rule evaluate

(in solver.g) 57:1: evaluate returns [result] : r= expression ;



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
# File 'lib/plympton/SolverParser.rb', line 138

def evaluate
  # -> uncomment the next line to manually enable rule tracing
  # trace_in( __method__, 1 )
  result = nil
  r = nil

  begin
    # at line 58:6: r= expression
    @state.following.push( TOKENS_FOLLOWING_expression_IN_evaluate_139 )
    r = expression
    @state.following.pop
    # --> action
    result = r 
    # <-- action

  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 result
end

#expObject

parser rule exp

(in solver.g) 78:1: exp returns [result] : r= atom ( ‘^’ r2= atom )? ;



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
# File 'lib/plympton/SolverParser.rb', line 376

def exp
  # -> uncomment the next line to manually enable rule tracing
  # trace_in( __method__, 5 )
  result = nil
  r = nil
  r2 = nil

  begin
    # at line 79:5: r= atom ( '^' r2= atom )?
    @state.following.push( TOKENS_FOLLOWING_atom_IN_exp_303 )
    r = atom
    @state.following.pop
    # at line 79:12: ( '^' r2= atom )?
    alt_4 = 2
    look_4_0 = @input.peek( 1 )

    if ( look_4_0 == EXP )
      alt_4 = 1
    end
    case alt_4
    when 1
      # at line 79:14: '^' r2= atom
      match( EXP, TOKENS_FOLLOWING_EXP_IN_exp_307 )
      @state.following.push( TOKENS_FOLLOWING_atom_IN_exp_311 )
      r2 = atom
      @state.following.pop
      # --> action
       r **= r2.to_i() 
      # <-- action

    end
    # --> action
     result = r 
    # <-- action

  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 result
end

#expressionObject

parser rule expression

(in solver.g) 60:1: expression returns [result] : r= mult ( ‘+’ r2= mult | ‘-’ r2= mult )* ;



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
# File 'lib/plympton/SolverParser.rb', line 173

def expression
  # -> uncomment the next line to manually enable rule tracing
  # trace_in( __method__, 2 )
  result = nil
  r = nil
  r2 = nil

  begin
    # at line 61:5: r= mult ( '+' r2= mult | '-' r2= mult )*
    @state.following.push( TOKENS_FOLLOWING_mult_IN_expression_160 )
    r = mult
    @state.following.pop
    # at line 62:5: ( '+' r2= mult | '-' r2= mult )*
    while true # decision 1
      alt_1 = 3
      look_1_0 = @input.peek( 1 )

      if ( look_1_0 == PLUS )
        alt_1 = 1
      elsif ( look_1_0 == MINUS )
        alt_1 = 2

      end
      case alt_1
      when 1
        # at line 62:7: '+' r2= mult
        match( PLUS, TOKENS_FOLLOWING_PLUS_IN_expression_168 )
        @state.following.push( TOKENS_FOLLOWING_mult_IN_expression_172 )
        r2 = mult
        @state.following.pop
        # --> action
         r += r2 
        # <-- action

      when 2
        # at line 63:7: '-' r2= mult
        match( MINUS, TOKENS_FOLLOWING_MINUS_IN_expression_182 )
        @state.following.push( TOKENS_FOLLOWING_mult_IN_expression_186 )
        r2 = mult
        @state.following.pop
        # --> action
         r -= r2 
        # <-- action

      else
        break # out of loop for decision 1
      end
    end # loop for decision 1
    # --> action
     result = r 
    # <-- action

  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 result
end

#logObject

parser rule log

(in solver.g) 73:1: log returns [result] : ( ‘ln’ r= exp | r= exp );



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
# File 'lib/plympton/SolverParser.rb', line 317

def log
  # -> uncomment the next line to manually enable rule tracing
  # trace_in( __method__, 4 )
  result = nil
  r = nil

  begin
    # at line 74:3: ( 'ln' r= exp | r= exp )
    alt_3 = 2
    look_3_0 = @input.peek( 1 )

    if ( look_3_0 == T__19 )
      alt_3 = 1
    elsif ( look_3_0 == LPAREN || look_3_0.between?( INT, UNMODVAR ) )
      alt_3 = 2
    else
      raise NoViableAlternative( "", 3, 0 )
    end
    case alt_3
    when 1
      # at line 74:5: 'ln' r= exp
      match( T__19, TOKENS_FOLLOWING_T__19_IN_log_268 )
      @state.following.push( TOKENS_FOLLOWING_exp_IN_log_272 )
      r = exp
      @state.following.pop
      # --> action
       result = BigMath.log(r, 2)
      # <-- action

    when 2
      # at line 75:5: r= exp
      @state.following.push( TOKENS_FOLLOWING_exp_IN_log_282 )
      r = exp
      @state.following.pop
      # --> action
       result = r 
      # <-- 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__, 4 )

  end
  
  return result
end

#multObject

parser rule mult

(in solver.g) 67:1: mult returns [result] : r= log ( ‘*’ r2= log | ‘/’ r2= log )* ;



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
# File 'lib/plympton/SolverParser.rb', line 245

def mult
  # -> uncomment the next line to manually enable rule tracing
  # trace_in( __method__, 3 )
  result = nil
  r = nil
  r2 = nil

  begin
    # at line 68:5: r= log ( '*' r2= log | '/' r2= log )*
    @state.following.push(  )
    r = log
    @state.following.pop
    # at line 69:5: ( '*' r2= log | '/' r2= log )*
    while true # decision 2
      alt_2 = 3
      look_2_0 = @input.peek( 1 )

      if ( look_2_0 == MULT )
        alt_2 = 1
      elsif ( look_2_0 == DIV )
        alt_2 = 2

      end
      case alt_2
      when 1
        # at line 69:7: '*' r2= log
        match( MULT, TOKENS_FOLLOWING_MULT_IN_mult_224 )
        @state.following.push(  )
        r2 = log
        @state.following.pop
        # --> action
         r *= r2 
        # <-- action

      when 2
        # at line 70:7: '/' r2= log
        match( DIV, TOKENS_FOLLOWING_DIV_IN_mult_238 )
        @state.following.push(  )
        r2 = log
        @state.following.pop
        # --> action
         r /= r2 
        # <-- action

      else
        break # out of loop for decision 2
      end
    end # loop for decision 2
    # --> action
     result = r 
    # <-- action

  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 result
end

#recover(error = $!) ⇒ Object

Recovery function handling errors



119
120
121
122
# File 'lib/plympton/SolverParser.rb', line 119

def recover( error = $! )
  puts "Parser Recovering: #{error}"
  exit(1)
end

#report_error(error = $!) ⇒ Object

Reporting function handling errors



125
126
127
128
# File 'lib/plympton/SolverParser.rb', line 125

def report_error( error = $! )
  puts "Parser Reporting: #{error}"
  exit(1)
end