exalted_math 0.1.2 → 0.1.3
Sign up to get free protection for your applications and to get access to all the features.
- data/Rakefile +6 -0
- data/VERSION +1 -1
- data/exalted_math.gemspec +2 -2
- data/lib/exalted_math.rb +5 -2
- data/lib/exalted_math/math.rb +261 -409
- data/lib/exalted_math/math.treetop +13 -13
- data/spec/parser_spec.rb +20 -6
- metadata +4 -4
data/Rakefile
CHANGED
@@ -29,3 +29,9 @@ rescue LoadError
|
|
29
29
|
puts "Jeweler (or a dependency) not available. Install it with: gem install jeweler"
|
30
30
|
end
|
31
31
|
|
32
|
+
file 'lib/exalted_math/math.rb' => 'lib/exalted_math/math.treetop' do
|
33
|
+
sh "tt lib/exalted_math/math.treetop"
|
34
|
+
end
|
35
|
+
|
36
|
+
desc "Regenerate the treetop file"
|
37
|
+
task :treetop => 'lib/exalted_math/math.rb'
|
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.1.
|
1
|
+
0.1.3
|
data/exalted_math.gemspec
CHANGED
@@ -5,11 +5,11 @@
|
|
5
5
|
|
6
6
|
Gem::Specification.new do |s|
|
7
7
|
s.name = %q{exalted_math}
|
8
|
-
s.version = "0.1.
|
8
|
+
s.version = "0.1.3"
|
9
9
|
|
10
10
|
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
11
11
|
s.authors = ["Jonathan Stott"]
|
12
|
-
s.date = %q{2010-06-
|
12
|
+
s.date = %q{2010-06-09}
|
13
13
|
s.description = %q{Parsing and evaluation of simple maths expressions for Exalted
|
14
14
|
|
15
15
|
This intended to aid in evaluating simple calculations which appear on character sheets, especially for Exalted.}
|
data/lib/exalted_math.rb
CHANGED
@@ -1,13 +1,16 @@
|
|
1
1
|
#!/usr/bin/ruby
|
2
2
|
# Jonathan D. Stott <jonathan.stott@gmail.com>
|
3
|
-
require 'treetop'
|
3
|
+
require 'treetop/runtime'
|
4
4
|
require 'exalted_math/math'
|
5
5
|
require 'exalted_math/ast'
|
6
6
|
|
7
7
|
module Exalted
|
8
8
|
class MathsParser
|
9
9
|
def ast(text)
|
10
|
-
|
10
|
+
txt = text.dup
|
11
|
+
txt.strip!
|
12
|
+
txt.gsub!(/\s+/," ")
|
13
|
+
result = parse(txt)
|
11
14
|
if result
|
12
15
|
[true, result.ast]
|
13
16
|
else
|
data/lib/exalted_math/math.rb
CHANGED
@@ -14,8 +14,12 @@ module Maths
|
|
14
14
|
elements[0]
|
15
15
|
end
|
16
16
|
|
17
|
+
def space
|
18
|
+
elements[2]
|
19
|
+
end
|
20
|
+
|
17
21
|
def additive
|
18
|
-
elements[
|
22
|
+
elements[3]
|
19
23
|
end
|
20
24
|
end
|
21
25
|
|
@@ -30,8 +34,12 @@ module Maths
|
|
30
34
|
elements[0]
|
31
35
|
end
|
32
36
|
|
37
|
+
def space
|
38
|
+
elements[2]
|
39
|
+
end
|
40
|
+
|
33
41
|
def additive
|
34
|
-
elements[
|
42
|
+
elements[3]
|
35
43
|
end
|
36
44
|
end
|
37
45
|
|
@@ -57,42 +65,20 @@ module Maths
|
|
57
65
|
r2 = _nt_multitive
|
58
66
|
s1 << r2
|
59
67
|
if r2
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
break
|
67
|
-
end
|
68
|
+
if has_terminal?('+', false, index)
|
69
|
+
r3 = instantiate_node(SyntaxNode,input, index...(index + 1))
|
70
|
+
@index += 1
|
71
|
+
else
|
72
|
+
terminal_parse_failure('+')
|
73
|
+
r3 = nil
|
68
74
|
end
|
69
|
-
r3 = instantiate_node(SyntaxNode,input, i3...index, s3)
|
70
75
|
s1 << r3
|
71
76
|
if r3
|
72
|
-
|
73
|
-
|
74
|
-
|
75
|
-
|
76
|
-
|
77
|
-
r5 = nil
|
78
|
-
end
|
79
|
-
s1 << r5
|
80
|
-
if r5
|
81
|
-
s6, i6 = [], index
|
82
|
-
loop do
|
83
|
-
r7 = _nt_space
|
84
|
-
if r7
|
85
|
-
s6 << r7
|
86
|
-
else
|
87
|
-
break
|
88
|
-
end
|
89
|
-
end
|
90
|
-
r6 = instantiate_node(SyntaxNode,input, i6...index, s6)
|
91
|
-
s1 << r6
|
92
|
-
if r6
|
93
|
-
r8 = _nt_additive
|
94
|
-
s1 << r8
|
95
|
-
end
|
77
|
+
r4 = _nt_space
|
78
|
+
s1 << r4
|
79
|
+
if r4
|
80
|
+
r5 = _nt_additive
|
81
|
+
s1 << r5
|
96
82
|
end
|
97
83
|
end
|
98
84
|
end
|
@@ -107,63 +93,41 @@ module Maths
|
|
107
93
|
if r1
|
108
94
|
r0 = r1
|
109
95
|
else
|
110
|
-
|
111
|
-
|
112
|
-
|
113
|
-
if
|
114
|
-
|
115
|
-
|
116
|
-
|
117
|
-
|
118
|
-
|
119
|
-
|
120
|
-
break
|
121
|
-
end
|
96
|
+
i6, s6 = index, []
|
97
|
+
r7 = _nt_multitive
|
98
|
+
s6 << r7
|
99
|
+
if r7
|
100
|
+
if has_terminal?('-', false, index)
|
101
|
+
r8 = instantiate_node(SyntaxNode,input, index...(index + 1))
|
102
|
+
@index += 1
|
103
|
+
else
|
104
|
+
terminal_parse_failure('-')
|
105
|
+
r8 = nil
|
122
106
|
end
|
123
|
-
|
124
|
-
|
125
|
-
|
126
|
-
|
127
|
-
|
128
|
-
|
129
|
-
|
130
|
-
terminal_parse_failure('-')
|
131
|
-
r13 = nil
|
132
|
-
end
|
133
|
-
s9 << r13
|
134
|
-
if r13
|
135
|
-
s14, i14 = [], index
|
136
|
-
loop do
|
137
|
-
r15 = _nt_space
|
138
|
-
if r15
|
139
|
-
s14 << r15
|
140
|
-
else
|
141
|
-
break
|
142
|
-
end
|
143
|
-
end
|
144
|
-
r14 = instantiate_node(SyntaxNode,input, i14...index, s14)
|
145
|
-
s9 << r14
|
146
|
-
if r14
|
147
|
-
r16 = _nt_additive
|
148
|
-
s9 << r16
|
149
|
-
end
|
107
|
+
s6 << r8
|
108
|
+
if r8
|
109
|
+
r9 = _nt_space
|
110
|
+
s6 << r9
|
111
|
+
if r9
|
112
|
+
r10 = _nt_additive
|
113
|
+
s6 << r10
|
150
114
|
end
|
151
115
|
end
|
152
116
|
end
|
153
|
-
if
|
154
|
-
|
155
|
-
|
156
|
-
|
117
|
+
if s6.last
|
118
|
+
r6 = instantiate_node(SyntaxNode,input, i6...index, s6)
|
119
|
+
r6.extend(Additive2)
|
120
|
+
r6.extend(Additive3)
|
157
121
|
else
|
158
|
-
@index =
|
159
|
-
|
122
|
+
@index = i6
|
123
|
+
r6 = nil
|
160
124
|
end
|
161
|
-
if
|
162
|
-
r0 =
|
125
|
+
if r6
|
126
|
+
r0 = r6
|
163
127
|
else
|
164
|
-
|
165
|
-
if
|
166
|
-
r0 =
|
128
|
+
r11 = _nt_multitive
|
129
|
+
if r11
|
130
|
+
r0 = r11
|
167
131
|
else
|
168
132
|
@index = i0
|
169
133
|
r0 = nil
|
@@ -181,8 +145,12 @@ module Maths
|
|
181
145
|
elements[0]
|
182
146
|
end
|
183
147
|
|
148
|
+
def space
|
149
|
+
elements[2]
|
150
|
+
end
|
151
|
+
|
184
152
|
def multitive
|
185
|
-
elements[
|
153
|
+
elements[3]
|
186
154
|
end
|
187
155
|
end
|
188
156
|
|
@@ -197,8 +165,12 @@ module Maths
|
|
197
165
|
elements[0]
|
198
166
|
end
|
199
167
|
|
168
|
+
def space
|
169
|
+
elements[2]
|
170
|
+
end
|
171
|
+
|
200
172
|
def multitive
|
201
|
-
elements[
|
173
|
+
elements[3]
|
202
174
|
end
|
203
175
|
end
|
204
176
|
|
@@ -224,42 +196,20 @@ module Maths
|
|
224
196
|
r2 = _nt_primary
|
225
197
|
s1 << r2
|
226
198
|
if r2
|
227
|
-
|
228
|
-
|
229
|
-
|
230
|
-
|
231
|
-
|
232
|
-
|
233
|
-
break
|
234
|
-
end
|
199
|
+
if has_terminal?('*', false, index)
|
200
|
+
r3 = instantiate_node(SyntaxNode,input, index...(index + 1))
|
201
|
+
@index += 1
|
202
|
+
else
|
203
|
+
terminal_parse_failure('*')
|
204
|
+
r3 = nil
|
235
205
|
end
|
236
|
-
r3 = instantiate_node(SyntaxNode,input, i3...index, s3)
|
237
206
|
s1 << r3
|
238
207
|
if r3
|
239
|
-
|
240
|
-
|
241
|
-
|
242
|
-
|
243
|
-
|
244
|
-
r5 = nil
|
245
|
-
end
|
246
|
-
s1 << r5
|
247
|
-
if r5
|
248
|
-
s6, i6 = [], index
|
249
|
-
loop do
|
250
|
-
r7 = _nt_space
|
251
|
-
if r7
|
252
|
-
s6 << r7
|
253
|
-
else
|
254
|
-
break
|
255
|
-
end
|
256
|
-
end
|
257
|
-
r6 = instantiate_node(SyntaxNode,input, i6...index, s6)
|
258
|
-
s1 << r6
|
259
|
-
if r6
|
260
|
-
r8 = _nt_multitive
|
261
|
-
s1 << r8
|
262
|
-
end
|
208
|
+
r4 = _nt_space
|
209
|
+
s1 << r4
|
210
|
+
if r4
|
211
|
+
r5 = _nt_multitive
|
212
|
+
s1 << r5
|
263
213
|
end
|
264
214
|
end
|
265
215
|
end
|
@@ -274,63 +224,41 @@ module Maths
|
|
274
224
|
if r1
|
275
225
|
r0 = r1
|
276
226
|
else
|
277
|
-
|
278
|
-
|
279
|
-
|
280
|
-
if
|
281
|
-
|
282
|
-
|
283
|
-
|
284
|
-
|
285
|
-
|
286
|
-
|
287
|
-
break
|
288
|
-
end
|
227
|
+
i6, s6 = index, []
|
228
|
+
r7 = _nt_primary
|
229
|
+
s6 << r7
|
230
|
+
if r7
|
231
|
+
if has_terminal?('/', false, index)
|
232
|
+
r8 = instantiate_node(SyntaxNode,input, index...(index + 1))
|
233
|
+
@index += 1
|
234
|
+
else
|
235
|
+
terminal_parse_failure('/')
|
236
|
+
r8 = nil
|
289
237
|
end
|
290
|
-
|
291
|
-
|
292
|
-
|
293
|
-
|
294
|
-
|
295
|
-
|
296
|
-
|
297
|
-
terminal_parse_failure('/')
|
298
|
-
r13 = nil
|
299
|
-
end
|
300
|
-
s9 << r13
|
301
|
-
if r13
|
302
|
-
s14, i14 = [], index
|
303
|
-
loop do
|
304
|
-
r15 = _nt_space
|
305
|
-
if r15
|
306
|
-
s14 << r15
|
307
|
-
else
|
308
|
-
break
|
309
|
-
end
|
310
|
-
end
|
311
|
-
r14 = instantiate_node(SyntaxNode,input, i14...index, s14)
|
312
|
-
s9 << r14
|
313
|
-
if r14
|
314
|
-
r16 = _nt_multitive
|
315
|
-
s9 << r16
|
316
|
-
end
|
238
|
+
s6 << r8
|
239
|
+
if r8
|
240
|
+
r9 = _nt_space
|
241
|
+
s6 << r9
|
242
|
+
if r9
|
243
|
+
r10 = _nt_multitive
|
244
|
+
s6 << r10
|
317
245
|
end
|
318
246
|
end
|
319
247
|
end
|
320
|
-
if
|
321
|
-
|
322
|
-
|
323
|
-
|
248
|
+
if s6.last
|
249
|
+
r6 = instantiate_node(SyntaxNode,input, i6...index, s6)
|
250
|
+
r6.extend(Multitive2)
|
251
|
+
r6.extend(Multitive3)
|
324
252
|
else
|
325
|
-
@index =
|
326
|
-
|
253
|
+
@index = i6
|
254
|
+
r6 = nil
|
327
255
|
end
|
328
|
-
if
|
329
|
-
r0 =
|
256
|
+
if r6
|
257
|
+
r0 = r6
|
330
258
|
else
|
331
|
-
|
332
|
-
if
|
333
|
-
r0 =
|
259
|
+
r11 = _nt_primary
|
260
|
+
if r11
|
261
|
+
r0 = r11
|
334
262
|
else
|
335
263
|
@index = i0
|
336
264
|
r0 = nil
|
@@ -344,10 +272,17 @@ module Maths
|
|
344
272
|
end
|
345
273
|
|
346
274
|
module Primary0
|
275
|
+
def space1
|
276
|
+
elements[1]
|
277
|
+
end
|
278
|
+
|
347
279
|
def additive
|
348
280
|
elements[2]
|
349
281
|
end
|
350
282
|
|
283
|
+
def space2
|
284
|
+
elements[4]
|
285
|
+
end
|
351
286
|
end
|
352
287
|
|
353
288
|
module Primary1
|
@@ -378,41 +313,23 @@ module Maths
|
|
378
313
|
end
|
379
314
|
s1 << r2
|
380
315
|
if r2
|
381
|
-
|
382
|
-
loop do
|
383
|
-
r4 = _nt_space
|
384
|
-
if r4
|
385
|
-
s3 << r4
|
386
|
-
else
|
387
|
-
break
|
388
|
-
end
|
389
|
-
end
|
390
|
-
r3 = instantiate_node(SyntaxNode,input, i3...index, s3)
|
316
|
+
r3 = _nt_space
|
391
317
|
s1 << r3
|
392
318
|
if r3
|
393
|
-
|
394
|
-
s1 <<
|
395
|
-
if
|
396
|
-
|
397
|
-
|
398
|
-
|
399
|
-
|
400
|
-
|
401
|
-
|
402
|
-
break
|
403
|
-
end
|
319
|
+
r4 = _nt_additive
|
320
|
+
s1 << r4
|
321
|
+
if r4
|
322
|
+
if has_terminal?(')', false, index)
|
323
|
+
r5 = instantiate_node(SyntaxNode,input, index...(index + 1))
|
324
|
+
@index += 1
|
325
|
+
else
|
326
|
+
terminal_parse_failure(')')
|
327
|
+
r5 = nil
|
404
328
|
end
|
405
|
-
|
406
|
-
|
407
|
-
|
408
|
-
|
409
|
-
r8 = instantiate_node(SyntaxNode,input, index...(index + 1))
|
410
|
-
@index += 1
|
411
|
-
else
|
412
|
-
terminal_parse_failure(')')
|
413
|
-
r8 = nil
|
414
|
-
end
|
415
|
-
s1 << r8
|
329
|
+
s1 << r5
|
330
|
+
if r5
|
331
|
+
r6 = _nt_space
|
332
|
+
s1 << r6
|
416
333
|
end
|
417
334
|
end
|
418
335
|
end
|
@@ -428,25 +345,25 @@ module Maths
|
|
428
345
|
if r1
|
429
346
|
r0 = r1
|
430
347
|
else
|
431
|
-
|
432
|
-
if
|
433
|
-
r0 =
|
348
|
+
r7 = _nt_number
|
349
|
+
if r7
|
350
|
+
r0 = r7
|
434
351
|
else
|
435
|
-
|
436
|
-
if
|
437
|
-
r0 =
|
352
|
+
r8 = _nt_spec
|
353
|
+
if r8
|
354
|
+
r0 = r8
|
438
355
|
else
|
439
|
-
|
440
|
-
if
|
441
|
-
r0 =
|
356
|
+
r9 = _nt_max
|
357
|
+
if r9
|
358
|
+
r0 = r9
|
442
359
|
else
|
443
|
-
|
444
|
-
if
|
445
|
-
r0 =
|
360
|
+
r10 = _nt_min
|
361
|
+
if r10
|
362
|
+
r0 = r10
|
446
363
|
else
|
447
|
-
|
448
|
-
if
|
449
|
-
r0 =
|
364
|
+
r11 = _nt_stat
|
365
|
+
if r11
|
366
|
+
r0 = r11
|
450
367
|
else
|
451
368
|
@index = i0
|
452
369
|
r0 = nil
|
@@ -467,6 +384,9 @@ module Maths
|
|
467
384
|
elements[4]
|
468
385
|
end
|
469
386
|
|
387
|
+
def space
|
388
|
+
elements[6]
|
389
|
+
end
|
470
390
|
end
|
471
391
|
|
472
392
|
module Spec1
|
@@ -561,6 +481,10 @@ module Maths
|
|
561
481
|
r8 = nil
|
562
482
|
end
|
563
483
|
s0 << r8
|
484
|
+
if r8
|
485
|
+
r9 = _nt_space
|
486
|
+
s0 << r9
|
487
|
+
end
|
564
488
|
end
|
565
489
|
end
|
566
490
|
end
|
@@ -593,7 +517,7 @@ module Maths
|
|
593
517
|
end
|
594
518
|
|
595
519
|
def list
|
596
|
-
elements[
|
520
|
+
elements[3]
|
597
521
|
end
|
598
522
|
|
599
523
|
end
|
@@ -697,43 +621,17 @@ module Maths
|
|
697
621
|
end
|
698
622
|
s0 << r9
|
699
623
|
if r9
|
700
|
-
|
701
|
-
loop do
|
702
|
-
r11 = _nt_space
|
703
|
-
if r11
|
704
|
-
s10 << r11
|
705
|
-
else
|
706
|
-
break
|
707
|
-
end
|
708
|
-
end
|
709
|
-
r10 = instantiate_node(SyntaxNode,input, i10...index, s10)
|
624
|
+
r10 = _nt_list
|
710
625
|
s0 << r10
|
711
626
|
if r10
|
712
|
-
|
713
|
-
|
714
|
-
|
715
|
-
|
716
|
-
|
717
|
-
|
718
|
-
if r14
|
719
|
-
s13 << r14
|
720
|
-
else
|
721
|
-
break
|
722
|
-
end
|
723
|
-
end
|
724
|
-
r13 = instantiate_node(SyntaxNode,input, i13...index, s13)
|
725
|
-
s0 << r13
|
726
|
-
if r13
|
727
|
-
if has_terminal?(')', false, index)
|
728
|
-
r15 = instantiate_node(SyntaxNode,input, index...(index + 1))
|
729
|
-
@index += 1
|
730
|
-
else
|
731
|
-
terminal_parse_failure(')')
|
732
|
-
r15 = nil
|
733
|
-
end
|
734
|
-
s0 << r15
|
735
|
-
end
|
627
|
+
if has_terminal?(')', false, index)
|
628
|
+
r11 = instantiate_node(SyntaxNode,input, index...(index + 1))
|
629
|
+
@index += 1
|
630
|
+
else
|
631
|
+
terminal_parse_failure(')')
|
632
|
+
r11 = nil
|
736
633
|
end
|
634
|
+
s0 << r11
|
737
635
|
end
|
738
636
|
end
|
739
637
|
end
|
@@ -765,7 +663,7 @@ module Maths
|
|
765
663
|
end
|
766
664
|
|
767
665
|
def list
|
768
|
-
elements[
|
666
|
+
elements[3]
|
769
667
|
end
|
770
668
|
|
771
669
|
end
|
@@ -868,43 +766,17 @@ module Maths
|
|
868
766
|
end
|
869
767
|
s0 << r9
|
870
768
|
if r9
|
871
|
-
|
872
|
-
loop do
|
873
|
-
r11 = _nt_space
|
874
|
-
if r11
|
875
|
-
s10 << r11
|
876
|
-
else
|
877
|
-
break
|
878
|
-
end
|
879
|
-
end
|
880
|
-
r10 = instantiate_node(SyntaxNode,input, i10...index, s10)
|
769
|
+
r10 = _nt_list
|
881
770
|
s0 << r10
|
882
771
|
if r10
|
883
|
-
|
884
|
-
|
885
|
-
|
886
|
-
|
887
|
-
|
888
|
-
|
889
|
-
if r14
|
890
|
-
s13 << r14
|
891
|
-
else
|
892
|
-
break
|
893
|
-
end
|
894
|
-
end
|
895
|
-
r13 = instantiate_node(SyntaxNode,input, i13...index, s13)
|
896
|
-
s0 << r13
|
897
|
-
if r13
|
898
|
-
if has_terminal?(')', false, index)
|
899
|
-
r15 = instantiate_node(SyntaxNode,input, index...(index + 1))
|
900
|
-
@index += 1
|
901
|
-
else
|
902
|
-
terminal_parse_failure(')')
|
903
|
-
r15 = nil
|
904
|
-
end
|
905
|
-
s0 << r15
|
906
|
-
end
|
772
|
+
if has_terminal?(')', false, index)
|
773
|
+
r11 = instantiate_node(SyntaxNode,input, index...(index + 1))
|
774
|
+
@index += 1
|
775
|
+
else
|
776
|
+
terminal_parse_failure(')')
|
777
|
+
r11 = nil
|
907
778
|
end
|
779
|
+
s0 << r11
|
908
780
|
end
|
909
781
|
end
|
910
782
|
end
|
@@ -924,9 +796,17 @@ module Maths
|
|
924
796
|
end
|
925
797
|
|
926
798
|
module List0
|
799
|
+
def space
|
800
|
+
elements[1]
|
801
|
+
end
|
802
|
+
|
927
803
|
end
|
928
804
|
|
929
805
|
module List1
|
806
|
+
def space
|
807
|
+
elements[0]
|
808
|
+
end
|
809
|
+
|
930
810
|
end
|
931
811
|
|
932
812
|
module List2
|
@@ -957,101 +837,70 @@ module Maths
|
|
957
837
|
end
|
958
838
|
|
959
839
|
i0, s0 = index, []
|
960
|
-
|
961
|
-
|
962
|
-
|
963
|
-
|
964
|
-
|
965
|
-
if
|
966
|
-
|
840
|
+
r1 = _nt_space
|
841
|
+
s0 << r1
|
842
|
+
if r1
|
843
|
+
i2 = index
|
844
|
+
r3 = _nt_number
|
845
|
+
if r3
|
846
|
+
r2 = r3
|
967
847
|
else
|
968
|
-
|
969
|
-
if
|
970
|
-
|
848
|
+
r4 = _nt_stat
|
849
|
+
if r4
|
850
|
+
r2 = r4
|
971
851
|
else
|
972
|
-
@index =
|
973
|
-
|
852
|
+
@index = i2
|
853
|
+
r2 = nil
|
974
854
|
end
|
975
855
|
end
|
976
|
-
|
977
|
-
if
|
978
|
-
|
856
|
+
s0 << r2
|
857
|
+
if r2
|
858
|
+
s5, i5 = [], index
|
979
859
|
loop do
|
980
|
-
|
981
|
-
if r7
|
982
|
-
s6 << r7
|
983
|
-
else
|
984
|
-
break
|
985
|
-
end
|
986
|
-
end
|
987
|
-
r6 = instantiate_node(SyntaxNode,input, i6...index, s6)
|
988
|
-
s2 << r6
|
989
|
-
if r6
|
860
|
+
i6, s6 = index, []
|
990
861
|
if has_terminal?(',', false, index)
|
991
|
-
|
862
|
+
r7 = instantiate_node(SyntaxNode,input, index...(index + 1))
|
992
863
|
@index += 1
|
993
864
|
else
|
994
865
|
terminal_parse_failure(',')
|
995
|
-
|
866
|
+
r7 = nil
|
996
867
|
end
|
997
|
-
|
998
|
-
if
|
999
|
-
|
1000
|
-
|
1001
|
-
|
868
|
+
s6 << r7
|
869
|
+
if r7
|
870
|
+
r8 = _nt_space
|
871
|
+
s6 << r8
|
872
|
+
if r8
|
873
|
+
i9 = index
|
874
|
+
r10 = _nt_number
|
1002
875
|
if r10
|
1003
|
-
|
876
|
+
r9 = r10
|
1004
877
|
else
|
1005
|
-
|
878
|
+
r11 = _nt_stat
|
879
|
+
if r11
|
880
|
+
r9 = r11
|
881
|
+
else
|
882
|
+
@index = i9
|
883
|
+
r9 = nil
|
884
|
+
end
|
1006
885
|
end
|
886
|
+
s6 << r9
|
1007
887
|
end
|
1008
|
-
r9 = instantiate_node(SyntaxNode,input, i9...index, s9)
|
1009
|
-
s2 << r9
|
1010
888
|
end
|
1011
|
-
|
1012
|
-
|
1013
|
-
|
1014
|
-
|
1015
|
-
|
1016
|
-
|
1017
|
-
|
1018
|
-
|
1019
|
-
|
1020
|
-
if r2
|
1021
|
-
s1 << r2
|
1022
|
-
else
|
1023
|
-
break
|
1024
|
-
end
|
1025
|
-
end
|
1026
|
-
r1 = instantiate_node(SyntaxNode,input, i1...index, s1)
|
1027
|
-
s0 << r1
|
1028
|
-
if r1
|
1029
|
-
s11, i11 = [], index
|
1030
|
-
loop do
|
1031
|
-
r12 = _nt_space
|
1032
|
-
if r12
|
1033
|
-
s11 << r12
|
1034
|
-
else
|
1035
|
-
break
|
1036
|
-
end
|
1037
|
-
end
|
1038
|
-
r11 = instantiate_node(SyntaxNode,input, i11...index, s11)
|
1039
|
-
s0 << r11
|
1040
|
-
if r11
|
1041
|
-
i13 = index
|
1042
|
-
r14 = _nt_number
|
1043
|
-
if r14
|
1044
|
-
r13 = r14
|
1045
|
-
else
|
1046
|
-
r15 = _nt_stat
|
1047
|
-
if r15
|
1048
|
-
r13 = r15
|
889
|
+
if s6.last
|
890
|
+
r6 = instantiate_node(SyntaxNode,input, i6...index, s6)
|
891
|
+
r6.extend(List0)
|
892
|
+
else
|
893
|
+
@index = i6
|
894
|
+
r6 = nil
|
895
|
+
end
|
896
|
+
if r6
|
897
|
+
s5 << r6
|
1049
898
|
else
|
1050
|
-
|
1051
|
-
r13 = nil
|
899
|
+
break
|
1052
900
|
end
|
1053
901
|
end
|
1054
|
-
|
902
|
+
r5 = instantiate_node(SyntaxNode,input, i5...index, s5)
|
903
|
+
s0 << r5
|
1055
904
|
end
|
1056
905
|
end
|
1057
906
|
if s0.last
|
@@ -1079,44 +928,17 @@ module Maths
|
|
1079
928
|
return cached
|
1080
929
|
end
|
1081
930
|
|
1082
|
-
|
1083
|
-
|
1084
|
-
|
1085
|
-
|
1086
|
-
|
1087
|
-
|
1088
|
-
else
|
1089
|
-
terminal_parse_failure(" ")
|
1090
|
-
r2 = nil
|
1091
|
-
end
|
1092
|
-
if r2
|
1093
|
-
r1 = r2
|
1094
|
-
else
|
1095
|
-
if has_terminal?("\t", false, index)
|
1096
|
-
r3 = instantiate_node(SyntaxNode,input, index...(index + 1))
|
1097
|
-
@index += 1
|
1098
|
-
else
|
1099
|
-
terminal_parse_failure("\t")
|
1100
|
-
r3 = nil
|
1101
|
-
end
|
1102
|
-
if r3
|
1103
|
-
r1 = r3
|
1104
|
-
else
|
1105
|
-
@index = i1
|
1106
|
-
r1 = nil
|
1107
|
-
end
|
1108
|
-
end
|
1109
|
-
if r1
|
1110
|
-
s0 << r1
|
1111
|
-
else
|
1112
|
-
break
|
1113
|
-
end
|
931
|
+
if has_terminal?(" ", false, index)
|
932
|
+
r1 = instantiate_node(SyntaxNode,input, index...(index + 1))
|
933
|
+
@index += 1
|
934
|
+
else
|
935
|
+
terminal_parse_failure(" ")
|
936
|
+
r1 = nil
|
1114
937
|
end
|
1115
|
-
if
|
1116
|
-
|
1117
|
-
r0 = nil
|
938
|
+
if r1
|
939
|
+
r0 = r1
|
1118
940
|
else
|
1119
|
-
r0 = instantiate_node(SyntaxNode,input,
|
941
|
+
r0 = instantiate_node(SyntaxNode,input, index...index)
|
1120
942
|
end
|
1121
943
|
|
1122
944
|
node_cache[:space][start_index] = r0
|
@@ -1129,6 +951,9 @@ module Maths
|
|
1129
951
|
elements[0]
|
1130
952
|
end
|
1131
953
|
|
954
|
+
def space
|
955
|
+
elements[3]
|
956
|
+
end
|
1132
957
|
end
|
1133
958
|
|
1134
959
|
module Number1
|
@@ -1194,6 +1019,10 @@ module Maths
|
|
1194
1019
|
end
|
1195
1020
|
r4 = instantiate_node(SyntaxNode,input, i4...index, s4)
|
1196
1021
|
s0 << r4
|
1022
|
+
if r4
|
1023
|
+
r6 = _nt_space
|
1024
|
+
s0 << r6
|
1025
|
+
end
|
1197
1026
|
end
|
1198
1027
|
end
|
1199
1028
|
if s0.last
|
@@ -1211,8 +1040,18 @@ module Maths
|
|
1211
1040
|
end
|
1212
1041
|
|
1213
1042
|
module Stat0
|
1043
|
+
def statistic
|
1044
|
+
elements[0]
|
1045
|
+
end
|
1046
|
+
|
1047
|
+
def space
|
1048
|
+
elements[1]
|
1049
|
+
end
|
1050
|
+
end
|
1051
|
+
|
1052
|
+
module Stat1
|
1214
1053
|
def value
|
1215
|
-
text_value.downcase
|
1054
|
+
statistic.text_value.downcase
|
1216
1055
|
end
|
1217
1056
|
def ast
|
1218
1057
|
Ast.stat(value)
|
@@ -1230,26 +1069,39 @@ module Maths
|
|
1230
1069
|
return cached
|
1231
1070
|
end
|
1232
1071
|
|
1233
|
-
|
1072
|
+
i0, s0 = index, []
|
1073
|
+
s1, i1 = [], index
|
1234
1074
|
loop do
|
1235
1075
|
if has_terminal?('\G[A-Za-z]', true, index)
|
1236
|
-
|
1076
|
+
r2 = true
|
1237
1077
|
@index += 1
|
1238
1078
|
else
|
1239
|
-
|
1079
|
+
r2 = nil
|
1240
1080
|
end
|
1241
|
-
if
|
1242
|
-
|
1081
|
+
if r2
|
1082
|
+
s1 << r2
|
1243
1083
|
else
|
1244
1084
|
break
|
1245
1085
|
end
|
1246
1086
|
end
|
1247
|
-
if
|
1248
|
-
@index =
|
1249
|
-
|
1087
|
+
if s1.empty?
|
1088
|
+
@index = i1
|
1089
|
+
r1 = nil
|
1250
1090
|
else
|
1091
|
+
r1 = instantiate_node(SyntaxNode,input, i1...index, s1)
|
1092
|
+
end
|
1093
|
+
s0 << r1
|
1094
|
+
if r1
|
1095
|
+
r3 = _nt_space
|
1096
|
+
s0 << r3
|
1097
|
+
end
|
1098
|
+
if s0.last
|
1251
1099
|
r0 = instantiate_node(SyntaxNode,input, i0...index, s0)
|
1252
1100
|
r0.extend(Stat0)
|
1101
|
+
r0.extend(Stat1)
|
1102
|
+
else
|
1103
|
+
@index = i0
|
1104
|
+
r0 = nil
|
1253
1105
|
end
|
1254
1106
|
|
1255
1107
|
node_cache[:stat][start_index] = r0
|
@@ -2,12 +2,12 @@ module Exalted
|
|
2
2
|
grammar Maths
|
3
3
|
|
4
4
|
rule additive
|
5
|
-
multitive
|
5
|
+
multitive '+' space additive {
|
6
6
|
def ast
|
7
7
|
Ast.add(multitive.ast, additive.ast )
|
8
8
|
end
|
9
9
|
}
|
10
|
-
/ multitive
|
10
|
+
/ multitive '-' space additive {
|
11
11
|
def ast
|
12
12
|
Ast.sub( multitive.ast, additive.ast )
|
13
13
|
end
|
@@ -16,13 +16,13 @@ grammar Maths
|
|
16
16
|
end
|
17
17
|
|
18
18
|
rule multitive
|
19
|
-
primary
|
19
|
+
primary '*' space multitive {
|
20
20
|
def ast
|
21
21
|
Ast.mul(primary.ast, multitive.ast )
|
22
22
|
end
|
23
23
|
}
|
24
24
|
/
|
25
|
-
primary
|
25
|
+
primary '/' space multitive {
|
26
26
|
def ast
|
27
27
|
Ast.div( primary.ast, multitive.ast )
|
28
28
|
end
|
@@ -31,7 +31,7 @@ grammar Maths
|
|
31
31
|
end
|
32
32
|
|
33
33
|
rule primary
|
34
|
-
'(' space
|
34
|
+
'(' space additive ')' space {
|
35
35
|
def ast
|
36
36
|
additive.ast
|
37
37
|
end
|
@@ -40,7 +40,7 @@ grammar Maths
|
|
40
40
|
end
|
41
41
|
|
42
42
|
rule spec
|
43
|
-
[Ss] 'pec' 'iality'? ':"' speciality:[^"]+ '"' {
|
43
|
+
[Ss] 'pec' 'iality'? ':"' speciality:[^"]+ '"' space {
|
44
44
|
def value
|
45
45
|
speciality.text_value
|
46
46
|
end
|
@@ -52,7 +52,7 @@ grammar Maths
|
|
52
52
|
end
|
53
53
|
|
54
54
|
rule max
|
55
|
-
( 'max' / 'highest' ) num:( '[' number ']' )? '('
|
55
|
+
( 'max' / 'highest' ) num:( '[' number ']' )? '(' list ')' {
|
56
56
|
def count
|
57
57
|
if num.elements
|
58
58
|
num.elements.detect { |e| e.respond_to?(:value) }.value
|
@@ -68,7 +68,7 @@ grammar Maths
|
|
68
68
|
end
|
69
69
|
|
70
70
|
rule min
|
71
|
-
( 'min' / 'lowest' ) num:( '[' number ']' )? '('
|
71
|
+
( 'min' / 'lowest' ) num:( '[' number ']' )? '(' list ')' {
|
72
72
|
def count
|
73
73
|
if num.elements
|
74
74
|
num.elements.detect { |e| e.respond_to?(:value) }.value
|
@@ -83,7 +83,7 @@ grammar Maths
|
|
83
83
|
end
|
84
84
|
|
85
85
|
rule list
|
86
|
-
|
86
|
+
space ( number / stat ) ( ',' space ( number / stat ) )* {
|
87
87
|
def dig(values, elements)
|
88
88
|
elements.each do |elem|
|
89
89
|
values << elem if elem.respond_to?(:value)
|
@@ -101,11 +101,11 @@ grammar Maths
|
|
101
101
|
end
|
102
102
|
|
103
103
|
rule space
|
104
|
-
|
104
|
+
" "?
|
105
105
|
end
|
106
106
|
|
107
107
|
rule number
|
108
|
-
negative:('-')? [1-9] [0-9]* {
|
108
|
+
negative:('-')? [1-9] [0-9]* space {
|
109
109
|
def value
|
110
110
|
text_value.to_i*neg
|
111
111
|
end
|
@@ -120,9 +120,9 @@ grammar Maths
|
|
120
120
|
end
|
121
121
|
|
122
122
|
rule stat
|
123
|
-
[A-Za-z]+ {
|
123
|
+
statistic:[A-Za-z]+ space {
|
124
124
|
def value
|
125
|
-
text_value.downcase
|
125
|
+
statistic.text_value.downcase
|
126
126
|
end
|
127
127
|
def ast
|
128
128
|
Ast.stat(value)
|
data/spec/parser_spec.rb
CHANGED
@@ -4,18 +4,25 @@ require 'spec_helper'
|
|
4
4
|
require 'exalted_math'
|
5
5
|
|
6
6
|
include Exalted
|
7
|
+
|
8
|
+
class ParserFailure < Bacon::Error
|
9
|
+
def initialize(failure)
|
10
|
+
super :failure, failure
|
11
|
+
end
|
12
|
+
end
|
7
13
|
describe "Exalted::MathParser" do
|
8
14
|
before do
|
9
15
|
@parser = Exalted::MathsParser.new
|
10
16
|
end
|
11
17
|
|
12
18
|
[
|
13
|
-
['4', Ast.num(4)],
|
14
|
-
['-1', Ast.num(-1)],
|
15
|
-
['3 * 4', Ast.mul(Ast.num(3), Ast.num(4) )],
|
16
|
-
['3 - 4', Ast.sub(Ast.num(3), Ast.num(4) )],
|
17
|
-
['3 + 4', Ast.add(Ast.num(3), Ast.num(4) )],
|
18
|
-
['6 / 3', Ast.div(Ast.num(6), Ast.num(3) )],
|
19
|
+
[' 4 ', Ast.num(4)],
|
20
|
+
[' -1 ', Ast.num(-1)],
|
21
|
+
[' 3 * 4 ', Ast.mul(Ast.num(3), Ast.num(4) )],
|
22
|
+
[' 3 - 4 ', Ast.sub(Ast.num(3), Ast.num(4) )],
|
23
|
+
[' 3 + 4 ', Ast.add(Ast.num(3), Ast.num(4) )],
|
24
|
+
[' 6 / 3 ', Ast.div(Ast.num(6), Ast.num(3) )],
|
25
|
+
[' spec:"Beating stuff" ', Ast.spec("Beating stuff")],
|
19
26
|
['Essence * 4', Ast.mul(Ast.stat('essence'), Ast.num(4) )],
|
20
27
|
['(Essence * 4) + Willpower', Ast.add(Ast.mul(Ast.stat('essence'), Ast.num(4) ), Ast.stat('willpower'))],
|
21
28
|
['highest[2](compassion,conviction,temperance,valor)', Ast.max(2, [Ast.stat('compassion'),Ast.stat('conviction'),Ast.stat('temperance'),Ast.stat('valor') ])],
|
@@ -23,9 +30,16 @@ describe "Exalted::MathParser" do
|
|
23
30
|
].each do |string, ast|
|
24
31
|
it "parses '#{string}'" do
|
25
32
|
success, result = @parser.ast(string)
|
33
|
+
raise ParserFailure, result unless success
|
26
34
|
success.should.be.true
|
27
35
|
result.should == ast
|
28
36
|
end
|
29
37
|
end
|
38
|
+
|
39
|
+
it "Doesn't care about spaces" do
|
40
|
+
success, result = @parser.ast(' ( Essence * 4 ) + Willpower + highest[2]( compassion , conviction ) ')
|
41
|
+
raise ParserFailure, result unless success
|
42
|
+
success.should.be.true
|
43
|
+
end
|
30
44
|
end
|
31
45
|
|
metadata
CHANGED
@@ -1,13 +1,13 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: exalted_math
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
hash:
|
4
|
+
hash: 29
|
5
5
|
prerelease: false
|
6
6
|
segments:
|
7
7
|
- 0
|
8
8
|
- 1
|
9
|
-
-
|
10
|
-
version: 0.1.
|
9
|
+
- 3
|
10
|
+
version: 0.1.3
|
11
11
|
platform: ruby
|
12
12
|
authors:
|
13
13
|
- Jonathan Stott
|
@@ -15,7 +15,7 @@ autorequire:
|
|
15
15
|
bindir: bin
|
16
16
|
cert_chain: []
|
17
17
|
|
18
|
-
date: 2010-06-
|
18
|
+
date: 2010-06-09 00:00:00 +01:00
|
19
19
|
default_executable:
|
20
20
|
dependencies:
|
21
21
|
- !ruby/object:Gem::Dependency
|