plympton 1.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,550 @@
1
+ #!/usr/bin/env ruby
2
+ #
3
+ # solver.g
4
+ # --
5
+ # Generated using ANTLR version: 3.2.1-SNAPSHOT Jul 31, 2010 19:34:52
6
+ # Ruby runtime library version: 1.8.11
7
+ # Input grammar file: solver.g
8
+ # Generated at: 2011-11-02 11:29:53
9
+ #
10
+
11
+ # ~~~> start load path setup
12
+ this_directory = File.expand_path( File.dirname( __FILE__ ) )
13
+ $LOAD_PATH.unshift( this_directory ) unless $LOAD_PATH.include?( this_directory )
14
+
15
+ antlr_load_failed = proc do
16
+ load_path = $LOAD_PATH.map { |dir| ' - ' << dir }.join( $/ )
17
+ raise LoadError, <<-END.strip!
18
+
19
+ Failed to load the ANTLR3 runtime library (version 1.8.11):
20
+
21
+ Ensure the library has been installed on your system and is available
22
+ on the load path. If rubygems is available on your system, this can
23
+ be done with the command:
24
+
25
+ gem install antlr3
26
+
27
+ Current load path:
28
+ #{ load_path }
29
+
30
+ END
31
+ end
32
+
33
+ defined?( ANTLR3 ) or begin
34
+
35
+ # 1: try to load the ruby antlr3 runtime library from the system path
36
+ require 'antlr3'
37
+
38
+ rescue LoadError
39
+
40
+ # 2: try to load rubygems if it isn't already loaded
41
+ defined?( Gem ) or begin
42
+ require 'rubygems'
43
+ rescue LoadError
44
+ antlr_load_failed.call
45
+ end
46
+
47
+ # 3: try to activate the antlr3 gem
48
+ begin
49
+ Gem.activate( 'antlr3', '~> 1.8.11' )
50
+ rescue Gem::LoadError
51
+ antlr_load_failed.call
52
+ end
53
+
54
+ require 'antlr3'
55
+
56
+ end
57
+ # <~~~ end load path setup
58
+
59
+ # - - - - - - begin action @parser::header - - - - - -
60
+ # solver.g
61
+
62
+
63
+ require 'bigdecimal/math'
64
+
65
+ # - - - - - - end action @parser::header - - - - - - -
66
+
67
+
68
+ module Solver
69
+ # TokenData defines all of the token type integer values
70
+ # as constants, which will be included in all
71
+ # ANTLR-generated recognizers.
72
+ const_defined?( :TokenData ) or TokenData = ANTLR3::TokenScheme.new
73
+
74
+ module TokenData
75
+
76
+ # define the token constants
77
+ define_tokens( :MOD => 8, :WHITESPACE => 18, :INT => 12, :FLOAT => 13,
78
+ :MULT => 6, :MINUS => 5, :EOF => -1, :MODIFIER => 17,
79
+ :LPAREN => 10, :T__19 => 19, :RPAREN => 11, :EXP => 9,
80
+ :PLUS => 4, :MODVAR => 14, :DIGIT => 16, :DIV => 7, :UNMODVAR => 15 )
81
+
82
+ # register the proper human-readable name or literal value
83
+ # for each token type
84
+ #
85
+ # this is necessary because anonymous tokens, which are
86
+ # created from literal values in the grammar, do not
87
+ # have descriptive names
88
+ register_names( "PLUS", "MINUS", "MULT", "DIV", "MOD", "EXP", "LPAREN",
89
+ "RPAREN", "INT", "FLOAT", "MODVAR", "UNMODVAR", "DIGIT",
90
+ "MODIFIER", "WHITESPACE", "'ln'" )
91
+
92
+ end
93
+
94
+
95
+ class Parser < ANTLR3::Parser
96
+ @grammar_home = Solver
97
+
98
+ RULE_METHODS = [ :evaluate, :expression, :mult, :log, :exp, :atom ].freeze
99
+
100
+
101
+ include TokenData
102
+
103
+ begin
104
+ generated_using( "solver.g", "3.2.1-SNAPSHOT Jul 31, 2010 19:34:52", "1.8.11" )
105
+ rescue NoMethodError => error
106
+ # ignore
107
+ end
108
+
109
+ def initialize( input, options = {} )
110
+ super( input, options )
111
+
112
+
113
+ end
114
+
115
+ attr_accessor :expressionCache
116
+ attr_accessor :objectCache
117
+
118
+ # Recovery function handling errors
119
+ def recover( error = $! )
120
+ puts "Parser Recovering: #{error}"
121
+ exit(1)
122
+ end
123
+
124
+ # Reporting function handling errors
125
+ def report_error( error = $! )
126
+ puts "Parser Reporting: #{error}"
127
+ exit(1)
128
+ end
129
+
130
+ # - - - - - - - - - - - - Rules - - - - - - - - - - - - -
131
+
132
+ #
133
+ # parser rule evaluate
134
+ #
135
+ # (in solver.g)
136
+ # 57:1: evaluate returns [result] : r= expression ;
137
+ #
138
+ def evaluate
139
+ # -> uncomment the next line to manually enable rule tracing
140
+ # trace_in( __method__, 1 )
141
+ result = nil
142
+ r = nil
143
+
144
+ begin
145
+ # at line 58:6: r= expression
146
+ @state.following.push( TOKENS_FOLLOWING_expression_IN_evaluate_139 )
147
+ r = expression
148
+ @state.following.pop
149
+ # --> action
150
+ result = r
151
+ # <-- action
152
+
153
+ rescue ANTLR3::Error::RecognitionError => re
154
+ report_error(re)
155
+ recover(re)
156
+
157
+ ensure
158
+ # -> uncomment the next line to manually enable rule tracing
159
+ # trace_out( __method__, 1 )
160
+
161
+ end
162
+
163
+ return result
164
+ end
165
+
166
+
167
+ #
168
+ # parser rule expression
169
+ #
170
+ # (in solver.g)
171
+ # 60:1: expression returns [result] : r= mult ( '+' r2= mult | '-' r2= mult )* ;
172
+ #
173
+ def expression
174
+ # -> uncomment the next line to manually enable rule tracing
175
+ # trace_in( __method__, 2 )
176
+ result = nil
177
+ r = nil
178
+ r2 = nil
179
+
180
+ begin
181
+ # at line 61:5: r= mult ( '+' r2= mult | '-' r2= mult )*
182
+ @state.following.push( TOKENS_FOLLOWING_mult_IN_expression_160 )
183
+ r = mult
184
+ @state.following.pop
185
+ # at line 62:5: ( '+' r2= mult | '-' r2= mult )*
186
+ while true # decision 1
187
+ alt_1 = 3
188
+ look_1_0 = @input.peek( 1 )
189
+
190
+ if ( look_1_0 == PLUS )
191
+ alt_1 = 1
192
+ elsif ( look_1_0 == MINUS )
193
+ alt_1 = 2
194
+
195
+ end
196
+ case alt_1
197
+ when 1
198
+ # at line 62:7: '+' r2= mult
199
+ match( PLUS, TOKENS_FOLLOWING_PLUS_IN_expression_168 )
200
+ @state.following.push( TOKENS_FOLLOWING_mult_IN_expression_172 )
201
+ r2 = mult
202
+ @state.following.pop
203
+ # --> action
204
+ r += r2
205
+ # <-- action
206
+
207
+ when 2
208
+ # at line 63:7: '-' r2= mult
209
+ match( MINUS, TOKENS_FOLLOWING_MINUS_IN_expression_182 )
210
+ @state.following.push( TOKENS_FOLLOWING_mult_IN_expression_186 )
211
+ r2 = mult
212
+ @state.following.pop
213
+ # --> action
214
+ r -= r2
215
+ # <-- action
216
+
217
+ else
218
+ break # out of loop for decision 1
219
+ end
220
+ end # loop for decision 1
221
+ # --> action
222
+ result = r
223
+ # <-- action
224
+
225
+ rescue ANTLR3::Error::RecognitionError => re
226
+ report_error(re)
227
+ recover(re)
228
+
229
+ ensure
230
+ # -> uncomment the next line to manually enable rule tracing
231
+ # trace_out( __method__, 2 )
232
+
233
+ end
234
+
235
+ return result
236
+ end
237
+
238
+
239
+ #
240
+ # parser rule mult
241
+ #
242
+ # (in solver.g)
243
+ # 67:1: mult returns [result] : r= log ( '*' r2= log | '/' r2= log )* ;
244
+ #
245
+ def mult
246
+ # -> uncomment the next line to manually enable rule tracing
247
+ # trace_in( __method__, 3 )
248
+ result = nil
249
+ r = nil
250
+ r2 = nil
251
+
252
+ begin
253
+ # at line 68:5: r= log ( '*' r2= log | '/' r2= log )*
254
+ @state.following.push( TOKENS_FOLLOWING_log_IN_mult_216 )
255
+ r = log
256
+ @state.following.pop
257
+ # at line 69:5: ( '*' r2= log | '/' r2= log )*
258
+ while true # decision 2
259
+ alt_2 = 3
260
+ look_2_0 = @input.peek( 1 )
261
+
262
+ if ( look_2_0 == MULT )
263
+ alt_2 = 1
264
+ elsif ( look_2_0 == DIV )
265
+ alt_2 = 2
266
+
267
+ end
268
+ case alt_2
269
+ when 1
270
+ # at line 69:7: '*' r2= log
271
+ match( MULT, TOKENS_FOLLOWING_MULT_IN_mult_224 )
272
+ @state.following.push( TOKENS_FOLLOWING_log_IN_mult_228 )
273
+ r2 = log
274
+ @state.following.pop
275
+ # --> action
276
+ r *= r2
277
+ # <-- action
278
+
279
+ when 2
280
+ # at line 70:7: '/' r2= log
281
+ match( DIV, TOKENS_FOLLOWING_DIV_IN_mult_238 )
282
+ @state.following.push( TOKENS_FOLLOWING_log_IN_mult_242 )
283
+ r2 = log
284
+ @state.following.pop
285
+ # --> action
286
+ r /= r2
287
+ # <-- action
288
+
289
+ else
290
+ break # out of loop for decision 2
291
+ end
292
+ end # loop for decision 2
293
+ # --> action
294
+ result = r
295
+ # <-- action
296
+
297
+ rescue ANTLR3::Error::RecognitionError => re
298
+ report_error(re)
299
+ recover(re)
300
+
301
+ ensure
302
+ # -> uncomment the next line to manually enable rule tracing
303
+ # trace_out( __method__, 3 )
304
+
305
+ end
306
+
307
+ return result
308
+ end
309
+
310
+
311
+ #
312
+ # parser rule log
313
+ #
314
+ # (in solver.g)
315
+ # 73:1: log returns [result] : ( 'ln' r= exp | r= exp );
316
+ #
317
+ def log
318
+ # -> uncomment the next line to manually enable rule tracing
319
+ # trace_in( __method__, 4 )
320
+ result = nil
321
+ r = nil
322
+
323
+ begin
324
+ # at line 74:3: ( 'ln' r= exp | r= exp )
325
+ alt_3 = 2
326
+ look_3_0 = @input.peek( 1 )
327
+
328
+ if ( look_3_0 == T__19 )
329
+ alt_3 = 1
330
+ elsif ( look_3_0 == LPAREN || look_3_0.between?( INT, UNMODVAR ) )
331
+ alt_3 = 2
332
+ else
333
+ raise NoViableAlternative( "", 3, 0 )
334
+ end
335
+ case alt_3
336
+ when 1
337
+ # at line 74:5: 'ln' r= exp
338
+ match( T__19, TOKENS_FOLLOWING_T__19_IN_log_268 )
339
+ @state.following.push( TOKENS_FOLLOWING_exp_IN_log_272 )
340
+ r = exp
341
+ @state.following.pop
342
+ # --> action
343
+ result = BigMath.log(r, 2)
344
+ # <-- action
345
+
346
+ when 2
347
+ # at line 75:5: r= exp
348
+ @state.following.push( TOKENS_FOLLOWING_exp_IN_log_282 )
349
+ r = exp
350
+ @state.following.pop
351
+ # --> action
352
+ result = r
353
+ # <-- action
354
+
355
+ end
356
+ rescue ANTLR3::Error::RecognitionError => re
357
+ report_error(re)
358
+ recover(re)
359
+
360
+ ensure
361
+ # -> uncomment the next line to manually enable rule tracing
362
+ # trace_out( __method__, 4 )
363
+
364
+ end
365
+
366
+ return result
367
+ end
368
+
369
+
370
+ #
371
+ # parser rule exp
372
+ #
373
+ # (in solver.g)
374
+ # 78:1: exp returns [result] : r= atom ( '^' r2= atom )? ;
375
+ #
376
+ def exp
377
+ # -> uncomment the next line to manually enable rule tracing
378
+ # trace_in( __method__, 5 )
379
+ result = nil
380
+ r = nil
381
+ r2 = nil
382
+
383
+ begin
384
+ # at line 79:5: r= atom ( '^' r2= atom )?
385
+ @state.following.push( TOKENS_FOLLOWING_atom_IN_exp_303 )
386
+ r = atom
387
+ @state.following.pop
388
+ # at line 79:12: ( '^' r2= atom )?
389
+ alt_4 = 2
390
+ look_4_0 = @input.peek( 1 )
391
+
392
+ if ( look_4_0 == EXP )
393
+ alt_4 = 1
394
+ end
395
+ case alt_4
396
+ when 1
397
+ # at line 79:14: '^' r2= atom
398
+ match( EXP, TOKENS_FOLLOWING_EXP_IN_exp_307 )
399
+ @state.following.push( TOKENS_FOLLOWING_atom_IN_exp_311 )
400
+ r2 = atom
401
+ @state.following.pop
402
+ # --> action
403
+ r **= r2.to_i()
404
+ # <-- action
405
+
406
+ end
407
+ # --> action
408
+ result = r
409
+ # <-- action
410
+
411
+ rescue ANTLR3::Error::RecognitionError => re
412
+ report_error(re)
413
+ recover(re)
414
+
415
+ ensure
416
+ # -> uncomment the next line to manually enable rule tracing
417
+ # trace_out( __method__, 5 )
418
+
419
+ end
420
+
421
+ return result
422
+ end
423
+
424
+
425
+ #
426
+ # parser rule atom
427
+ #
428
+ # (in solver.g)
429
+ # 82:1: atom returns [result] : (a= INT | a= FLOAT | LPAREN r= expression RPAREN | a= MODVAR | a= UNMODVAR ) ;
430
+ #
431
+ def atom
432
+ # -> uncomment the next line to manually enable rule tracing
433
+ # trace_in( __method__, 6 )
434
+ result = nil
435
+ a = nil
436
+ r = nil
437
+
438
+ begin
439
+ # at line 83:7: (a= INT | a= FLOAT | LPAREN r= expression RPAREN | a= MODVAR | a= UNMODVAR )
440
+ # at line 83:7: (a= INT | a= FLOAT | LPAREN r= expression RPAREN | a= MODVAR | a= UNMODVAR )
441
+ alt_5 = 5
442
+ case look_5 = @input.peek( 1 )
443
+ when INT then alt_5 = 1
444
+ when FLOAT then alt_5 = 2
445
+ when LPAREN then alt_5 = 3
446
+ when MODVAR then alt_5 = 4
447
+ when UNMODVAR then alt_5 = 5
448
+ else
449
+ raise NoViableAlternative( "", 5, 0 )
450
+ end
451
+ case alt_5
452
+ when 1
453
+ # at line 83:8: a= INT
454
+ a = match( INT, TOKENS_FOLLOWING_INT_IN_atom_341 )
455
+ # --> action
456
+ result = BigDecimal(a.text)
457
+ # <-- action
458
+
459
+ when 2
460
+ # at line 84:8: a= FLOAT
461
+ a = match( FLOAT, TOKENS_FOLLOWING_FLOAT_IN_atom_356 )
462
+ # --> action
463
+ result = BigDecimal(a.text)
464
+ # <-- action
465
+
466
+ when 3
467
+ # at line 85:8: LPAREN r= expression RPAREN
468
+ match( LPAREN, TOKENS_FOLLOWING_LPAREN_IN_atom_367 )
469
+ @state.following.push( TOKENS_FOLLOWING_expression_IN_atom_371 )
470
+ r = expression
471
+ @state.following.pop
472
+ # --> action
473
+ result = r
474
+ # <-- action
475
+ match( RPAREN, TOKENS_FOLLOWING_RPAREN_IN_atom_375 )
476
+
477
+ when 4
478
+ # at line 86:8: a= MODVAR
479
+ a = match( MODVAR, TOKENS_FOLLOWING_MODVAR_IN_atom_386 )
480
+ # --> action
481
+
482
+ result = @objectCache.send(a.text)
483
+
484
+ # <-- action
485
+
486
+ when 5
487
+ # at line 89:8: a= UNMODVAR
488
+ a = match( UNMODVAR, TOKENS_FOLLOWING_UNMODVAR_IN_atom_400 )
489
+ # --> action
490
+
491
+ case a.text
492
+ when 'R'
493
+ result = @objectCache.runtime
494
+ when 'U'
495
+ result = @objectCache.send(a.text)
496
+ when 'V'
497
+ result = @objectCache.send(a.text)
498
+ else
499
+ result = BigDecimal("0")
500
+ end
501
+
502
+ # <-- action
503
+
504
+ end
505
+
506
+ rescue ANTLR3::Error::RecognitionError => re
507
+ report_error(re)
508
+ recover(re)
509
+
510
+ ensure
511
+ # -> uncomment the next line to manually enable rule tracing
512
+ # trace_out( __method__, 6 )
513
+
514
+ end
515
+
516
+ return result
517
+ end
518
+
519
+
520
+
521
+ TOKENS_FOLLOWING_expression_IN_evaluate_139 = Set[ 1 ]
522
+ TOKENS_FOLLOWING_mult_IN_expression_160 = Set[ 1, 4, 5 ]
523
+ TOKENS_FOLLOWING_PLUS_IN_expression_168 = Set[ 10, 12, 13, 14, 15, 19 ]
524
+ TOKENS_FOLLOWING_mult_IN_expression_172 = Set[ 1, 4, 5 ]
525
+ TOKENS_FOLLOWING_MINUS_IN_expression_182 = Set[ 10, 12, 13, 14, 15, 19 ]
526
+ TOKENS_FOLLOWING_mult_IN_expression_186 = Set[ 1, 4, 5 ]
527
+ TOKENS_FOLLOWING_log_IN_mult_216 = Set[ 1, 6, 7 ]
528
+ TOKENS_FOLLOWING_MULT_IN_mult_224 = Set[ 10, 12, 13, 14, 15, 19 ]
529
+ TOKENS_FOLLOWING_log_IN_mult_228 = Set[ 1, 6, 7 ]
530
+ TOKENS_FOLLOWING_DIV_IN_mult_238 = Set[ 10, 12, 13, 14, 15, 19 ]
531
+ TOKENS_FOLLOWING_log_IN_mult_242 = Set[ 1, 6, 7 ]
532
+ TOKENS_FOLLOWING_T__19_IN_log_268 = Set[ 10, 12, 13, 14, 15, 19 ]
533
+ TOKENS_FOLLOWING_exp_IN_log_272 = Set[ 1 ]
534
+ TOKENS_FOLLOWING_exp_IN_log_282 = Set[ 1 ]
535
+ TOKENS_FOLLOWING_atom_IN_exp_303 = Set[ 1, 9 ]
536
+ TOKENS_FOLLOWING_EXP_IN_exp_307 = Set[ 10, 12, 13, 14, 15, 19 ]
537
+ TOKENS_FOLLOWING_atom_IN_exp_311 = Set[ 1 ]
538
+ TOKENS_FOLLOWING_INT_IN_atom_341 = Set[ 1 ]
539
+ TOKENS_FOLLOWING_FLOAT_IN_atom_356 = Set[ 1 ]
540
+ TOKENS_FOLLOWING_LPAREN_IN_atom_367 = Set[ 10, 12, 13, 14, 15, 19 ]
541
+ TOKENS_FOLLOWING_expression_IN_atom_371 = Set[ 11 ]
542
+ TOKENS_FOLLOWING_RPAREN_IN_atom_375 = Set[ 1 ]
543
+ TOKENS_FOLLOWING_MODVAR_IN_atom_386 = Set[ 1 ]
544
+ TOKENS_FOLLOWING_UNMODVAR_IN_atom_400 = Set[ 1 ]
545
+
546
+ end # class Parser < ANTLR3::Parser
547
+
548
+ at_exit { Parser.main( ARGV ) } if __FILE__ == $0
549
+ end
550
+