plurimath 0.2.1 → 0.2.2

Sign up to get free protection for your applications and to get access to all the features.
Files changed (110) hide show
  1. checksums.yaml +4 -4
  2. data/.github/workflows/rake.yml +13 -0
  3. data/.github/workflows/release.yml +22 -0
  4. data/.hound.yml +5 -0
  5. data/.rubocop.yml +8 -0
  6. data/AsciiMath-Supported-Data.adoc +1994 -274
  7. data/Gemfile +2 -0
  8. data/Latex-Supported-Data.adoc +1875 -1868
  9. data/MathML-Supported-Data.adoc +280 -263
  10. data/README.adoc +22 -20
  11. data/lib/plurimath/asciimath/constants.rb +186 -141
  12. data/lib/plurimath/asciimath/parse.rb +104 -39
  13. data/lib/plurimath/asciimath/parser.rb +3 -1
  14. data/lib/plurimath/asciimath/transform.rb +1074 -238
  15. data/lib/plurimath/html/parse.rb +1 -1
  16. data/lib/plurimath/latex/constants.rb +3228 -1432
  17. data/lib/plurimath/latex/parse.rb +106 -83
  18. data/lib/plurimath/latex/parser.rb +11 -4
  19. data/lib/plurimath/latex/transform.rb +354 -99
  20. data/lib/plurimath/math/base.rb +15 -0
  21. data/lib/plurimath/math/formula.rb +90 -13
  22. data/lib/plurimath/math/function/bar.rb +35 -1
  23. data/lib/plurimath/math/function/base.rb +25 -4
  24. data/lib/plurimath/math/function/binary_function.rb +101 -19
  25. data/lib/plurimath/math/function/cancel.rb +8 -0
  26. data/lib/plurimath/math/function/ceil.rb +3 -0
  27. data/lib/plurimath/math/function/color.rb +15 -5
  28. data/lib/plurimath/math/function/f.rb +8 -0
  29. data/lib/plurimath/math/function/fenced.rb +95 -8
  30. data/lib/plurimath/math/function/floor.rb +15 -0
  31. data/lib/plurimath/math/function/font_style/bold.rb +19 -0
  32. data/lib/plurimath/math/function/font_style/double_struck.rb +19 -0
  33. data/lib/plurimath/math/function/font_style/fraktur.rb +19 -0
  34. data/lib/plurimath/math/function/font_style/italic.rb +37 -0
  35. data/lib/plurimath/math/function/font_style/monospace.rb +19 -0
  36. data/lib/plurimath/math/function/font_style/normal.rb +37 -0
  37. data/lib/plurimath/math/function/font_style/sans-serif.rb +19 -0
  38. data/lib/plurimath/math/function/font_style/script.rb +19 -0
  39. data/lib/plurimath/math/function/font_style.rb +18 -5
  40. data/lib/plurimath/math/function/frac.rb +33 -3
  41. data/lib/plurimath/math/function/g.rb +7 -0
  42. data/lib/plurimath/math/function/hat.rb +12 -0
  43. data/lib/plurimath/math/function/inf.rb +21 -0
  44. data/lib/plurimath/math/function/int.rb +23 -2
  45. data/lib/plurimath/math/function/left.rb +25 -1
  46. data/lib/plurimath/math/function/lim.rb +40 -2
  47. data/lib/plurimath/math/function/limits.rb +9 -0
  48. data/lib/plurimath/math/function/log.rb +55 -4
  49. data/lib/plurimath/math/function/longdiv.rb +12 -0
  50. data/lib/plurimath/math/function/mbox.rb +31 -0
  51. data/lib/plurimath/math/function/menclose.rb +46 -0
  52. data/lib/plurimath/math/function/merror.rb +12 -0
  53. data/lib/plurimath/math/function/mod.rb +19 -4
  54. data/lib/plurimath/math/function/msgroup.rb +37 -0
  55. data/lib/plurimath/math/function/msline.rb +12 -0
  56. data/lib/plurimath/math/function/multiscript.rb +19 -0
  57. data/lib/plurimath/math/function/norm.rb +17 -1
  58. data/lib/plurimath/math/function/obrace.rb +17 -0
  59. data/lib/plurimath/math/function/oint.rb +2 -2
  60. data/lib/plurimath/math/function/over.rb +12 -5
  61. data/lib/plurimath/math/function/overset.rb +34 -5
  62. data/lib/plurimath/math/function/phantom.rb +28 -0
  63. data/lib/plurimath/math/function/power.rb +27 -9
  64. data/lib/plurimath/math/function/power_base.rb +109 -11
  65. data/lib/plurimath/math/function/prod.rb +25 -4
  66. data/lib/plurimath/math/function/right.rb +22 -2
  67. data/lib/plurimath/math/function/root.rb +23 -1
  68. data/lib/plurimath/math/function/rule.rb +33 -0
  69. data/lib/plurimath/math/function/scarries.rb +12 -0
  70. data/lib/plurimath/math/function/scarry.rb +12 -0
  71. data/lib/plurimath/math/function/sqrt.rb +23 -1
  72. data/lib/plurimath/math/function/stackrel.rb +27 -0
  73. data/lib/plurimath/math/function/substack.rb +7 -0
  74. data/lib/plurimath/math/function/sum.rb +50 -2
  75. data/lib/plurimath/math/function/sup.rb +3 -0
  76. data/lib/plurimath/math/function/table/align.rb +5 -5
  77. data/lib/plurimath/math/function/table/array.rb +25 -6
  78. data/lib/plurimath/math/function/table/bmatrix.rb +18 -7
  79. data/lib/plurimath/math/function/table/matrix.rb +13 -5
  80. data/lib/plurimath/math/function/table/multline.rb +5 -5
  81. data/lib/plurimath/math/function/table/pmatrix.rb +5 -5
  82. data/lib/plurimath/math/function/table/split.rb +5 -5
  83. data/lib/plurimath/math/function/table/vmatrix.rb +5 -6
  84. data/lib/plurimath/math/function/table.rb +185 -27
  85. data/lib/plurimath/math/function/td.rb +22 -9
  86. data/lib/plurimath/math/function/ternary_function.rb +74 -9
  87. data/lib/plurimath/math/function/text.rb +36 -11
  88. data/lib/plurimath/math/function/tr.rb +23 -4
  89. data/lib/plurimath/math/function/ubrace.rb +17 -0
  90. data/lib/plurimath/math/function/ul.rb +29 -0
  91. data/lib/plurimath/math/function/unary_function.rb +81 -8
  92. data/lib/plurimath/math/function/underline.rb +12 -0
  93. data/lib/plurimath/math/function/underover.rb +107 -0
  94. data/lib/plurimath/math/function/underset.rb +39 -0
  95. data/lib/plurimath/math/function/vec.rb +7 -1
  96. data/lib/plurimath/math/number.rb +5 -5
  97. data/lib/plurimath/math/symbol.rb +51 -12
  98. data/lib/plurimath/math/unicode.rb +11 -0
  99. data/lib/plurimath/math.rb +7 -3
  100. data/lib/plurimath/mathml/constants.rb +224 -147
  101. data/lib/plurimath/mathml/parser.rb +24 -8
  102. data/lib/plurimath/mathml/transform.rb +249 -153
  103. data/lib/plurimath/omml/parser.rb +24 -4
  104. data/lib/plurimath/omml/transform.rb +219 -157
  105. data/lib/plurimath/utility.rb +342 -20
  106. data/lib/plurimath/version.rb +1 -1
  107. metadata +21 -6
  108. data/.github/workflows/test.yml +0 -33
  109. data/lib/plurimath/mathml/parse.rb +0 -68
  110. data/lib/plurimath/omml/constants.rb +0 -154
@@ -3,177 +3,976 @@
3
3
  module Plurimath
4
4
  class Asciimath
5
5
  class Transform < Parslet::Transform
6
- rule(expr: simple(:expr)) { expr }
7
- rule(base: simple(:base)) { base }
8
- rule(base: sequence(:base)) { base }
9
- rule(fonts: simple(:fonts)) { fonts }
10
- rule(power: simple(:power)) { power }
6
+ rule(mod: simple(:mod)) { mod }
7
+ rule(frac: simple(:frac)) { frac }
11
8
  rule(unary: simple(:unary)) { unary }
12
9
  rule(table: simple(:table)) { table }
13
- rule(power: sequence(:power)) { power }
14
- rule(binary: simple(:binary)) { binary }
15
-
16
- rule(sequence: simple(:sequence)) { sequence }
17
- rule(table_row: simple(:table_row)) { table_row }
10
+ rule(comma: simple(:comma)) { Utility.symbol_object(comma) }
11
+ rule(unary: sequence(:unary)) { Utility.filter_values(unary) }
12
+ rule(rparen: simple(:rparen)) { Utility.symbol_object(rparen) }
13
+ rule(number: simple(:number)) { Math::Number.new(number) }
18
14
 
15
+ rule(sequence: simple(:sequence)) { sequence }
16
+ rule(table_row: simple(:table_row)) { table_row }
17
+ rule(sequence: sequence(:sequence)) { sequence }
19
18
  rule(power_base: simple(:power_base)) { power_base }
20
19
  rule(left_right: simple(:left_right)) { left_right }
21
20
  rule(table_left: simple(:table_left)) { table_left }
22
21
 
23
- rule(table_right: simple(:table_right)) { table_right }
24
- rule(intermediate_exp: simple(:int_exp)) { int_exp }
22
+ rule(left_right: sequence(:left_right)) { left_right }
23
+ rule(power_base: sequence(:power_base)) { power_base }
24
+ rule(table_right: simple(:table_right)) { table_right }
25
+ rule(intermediate_exp: simple(:int_exp)) { int_exp }
26
+ rule(power_value: sequence(:power_value)) { power_value }
27
+ rule(mod: simple(:mod), expr: simple(:expr)) { [mod, expr] }
28
+
29
+ rule(bold_fonts: simple(:font)) do
30
+ Math::Function::FontStyle::DoubleStruck.new(
31
+ Utility.symbol_object(font.to_s[0]),
32
+ "mathbf",
33
+ )
34
+ end
35
+
36
+ rule(unary_class: simple(:unary)) do
37
+ Utility.get_class(unary).new
38
+ end
25
39
 
26
- rule(mod: simple(:mod), expr: simple(:expr)) { [mod, expr] }
27
- rule(base: sequence(:base), expr: simple(:exp)) { base + [exp] }
40
+ rule(binary_class: simple(:binary)) do
41
+ Utility.get_class(binary).new
42
+ end
28
43
 
29
- rule(number: simple(:number)) do
30
- Math::Number.new(number)
44
+ rule(comma_separated: subtree(:comma_separated)) do
45
+ comma_separated.flatten
31
46
  end
32
47
 
33
48
  rule(symbol: simple(:symbol)) do
34
- Math::Symbol.new(symbol)
49
+ Utility.symbol_object(
50
+ (Constants::SYMBOLS[symbol.to_sym] || symbol).to_s,
51
+ )
52
+ end
53
+
54
+ rule(expr: subtree(:expr)) do
55
+ case expr
56
+ when Array
57
+ expr.flatten.compact
58
+ else
59
+ expr
60
+ end
35
61
  end
36
62
 
37
63
  rule(text: simple(:text)) do
38
- text.is_a?(String) ? Utility.get_class("text").new(text) : text
64
+ text.is_a?(Slice) ? Utility.get_class("text").new(text) : text
65
+ end
66
+
67
+ rule(text: sequence(:text)) do
68
+ Utility.get_class("text").new(text.join)
69
+ end
70
+
71
+ rule(power_value: simple(:power_value)) do
72
+ power_value
39
73
  end
40
74
 
41
- rule(expr: sequence(:expr)) do
42
- Math::Formula.new(expr)
75
+ rule(base_value: simple(:base_value)) do
76
+ base_value
77
+ end
78
+
79
+ rule(base_value: sequence(:base_value)) do
80
+ Utility.filter_values(base_value)
81
+ end
82
+
83
+ rule(numerator: simple(:numerator),
84
+ denominator: simple(:denominator)) do
85
+ new_arr = []
86
+ first_value = numerator.value.shift if Utility.frac_values(numerator)
87
+ new_arr << first_value
88
+ first_value = Utility.unfenced_value(numerator)
89
+ second_value = denominator.value.pop if Utility.frac_values(denominator)
90
+ new_arr << second_value
91
+ second_value = Utility.unfenced_value(denominator)
92
+ frac = Math::Function::Frac.new(
93
+ first_value,
94
+ second_value,
95
+ )
96
+ if new_arr.compact.empty?
97
+ frac
98
+ else
99
+ Math::Formula.new(new_arr.insert(1, frac).compact)
100
+ end
101
+ end
102
+
103
+ rule(numerator: simple(:numerator),
104
+ denominator: sequence(:denominator)) do
105
+ new_arr = []
106
+ first_value = numerator.value.shift if Utility.frac_values(numerator)
107
+ new_arr << first_value
108
+ first_value = Utility.unfenced_value(numerator)
109
+ second_value = denominator.pop if Utility.frac_values(denominator)
110
+ new_arr << second_value
111
+ second_value = Utility.unfenced_value(denominator)
112
+ frac = Math::Function::Frac.new(
113
+ first_value,
114
+ second_value,
115
+ )
116
+ if new_arr.compact.empty?
117
+ frac
118
+ else
119
+ Math::Formula.new(new_arr.insert(1, frac).compact)
120
+ end
43
121
  end
44
122
 
45
123
  rule(sequence: simple(:sequence),
46
- expr: simple(:exp)) do
47
- [sequence, exp]
124
+ left_right: simple(:left_right)) do
125
+ new_arr = [sequence]
126
+ new_arr << left_right unless left_right.to_s.strip.empty?
127
+ new_arr
48
128
  end
49
129
 
50
- rule(power: simple(:power),
130
+ rule(sequence: simple(:sequence),
131
+ left_right: sequence(:left_right)) do
132
+ left_right.insert(0, sequence)
133
+ left_right
134
+ end
135
+
136
+ rule(table_row: simple(:table_row),
51
137
  expr: simple(:expr)) do
52
- [power, expr]
138
+ new_arr = [table_row]
139
+ new_arr << expr unless expr.to_s.strip.empty?
140
+ new_arr
53
141
  end
54
142
 
55
- rule(power: simple(:power),
143
+ rule(table_row: simple(:table_row),
56
144
  expr: sequence(:expr)) do
57
- expr.insert(0, power)
145
+ expr.flatten.compact.insert(0, table_row)
58
146
  end
59
147
 
60
- rule(table_row: simple(:table_row),
148
+ rule(comma: simple(:comma),
61
149
  expr: simple(:expr)) do
62
- [table_row, expr]
150
+ new_arr = [Utility.symbol_object(comma)]
151
+ new_arr << expr unless expr.to_s.strip.empty?
152
+ new_arr
153
+ end
154
+
155
+ rule(comma: simple(:comma),
156
+ expr: sequence(:expr)) do
157
+ expr.flatten.compact.insert(0, Utility.symbol_object(comma))
63
158
  end
64
159
 
65
- rule(td: simple(:td),
66
- tds: simple(:tds)) do
160
+ rule(rparen: simple(:rparen),
161
+ expr: simple(:expr)) do
67
162
  [
68
- Math::Function::Td.new([td]),
69
- Math::Function::Td.new([tds]),
163
+ Utility.symbol_object(rparen),
164
+ expr,
70
165
  ]
71
166
  end
72
167
 
168
+ rule(rparen: simple(:rparen),
169
+ expr: sequence(:expr)) do
170
+ expr.flatten.compact.insert(0, Utility.symbol_object(rparen))
171
+ end
172
+
173
+ rule(rparen: simple(:rparen),
174
+ power: simple(:power)) do
175
+ Math::Function::Power.new(
176
+ Utility.symbol_object(rparen),
177
+ Utility.unfenced_value(power),
178
+ )
179
+ end
180
+
181
+ rule(rparen: simple(:rparen),
182
+ power: simple(:power),
183
+ expr: simple(:expr)) do
184
+ power_object = Math::Function::Power.new(
185
+ Utility.symbol_object(rparen),
186
+ Utility.unfenced_value(power),
187
+ )
188
+ new_arr = [power_object]
189
+ new_arr << expr unless expr.to_s.strip.empty?
190
+ new_arr
191
+ end
192
+
193
+ rule(rparen: simple(:rparen),
194
+ power: simple(:power),
195
+ expr: sequence(:expr)) do
196
+ power_object = Math::Function::Power.new(
197
+ Utility.symbol_object(rparen),
198
+ Utility.unfenced_value(power),
199
+ )
200
+ new_arr = [power_object]
201
+ new_arr += expr
202
+ new_arr
203
+ end
204
+
205
+ rule(rparen: simple(:rparen),
206
+ base: simple(:base)) do
207
+ Math::Function::Base.new(
208
+ Utility.symbol_object(rparen),
209
+ Utility.unfenced_value(base),
210
+ )
211
+ end
212
+
213
+ rule(rparen: simple(:rparen),
214
+ base: simple(:base),
215
+ expr: simple(:expr)) do
216
+ base_object = Math::Function::Base.new(
217
+ Utility.symbol_object(rparen),
218
+ Utility.unfenced_value(base),
219
+ )
220
+ new_arr = [base_object]
221
+ new_arr << expr unless expr.to_s.strip.empty?
222
+ new_arr
223
+ end
224
+
225
+ rule(rparen: simple(:rparen),
226
+ power: simple(:power),
227
+ comma: simple(:comma)) do
228
+ [
229
+ Math::Function::Power.new(
230
+ Utility.symbol_object(rparen),
231
+ Utility.unfenced_value(power),
232
+ ),
233
+ Utility.symbol_object(comma),
234
+ ]
235
+ end
236
+
237
+ rule(rparen: simple(:rparen),
238
+ power: simple(:power),
239
+ comma: simple(:comma),
240
+ expr: simple(:expr)) do
241
+ exponent = Math::Function::Power.new(
242
+ Utility.symbol_object(rparen),
243
+ Utility.unfenced_value(power),
244
+ )
245
+ new_arr = [
246
+ exponent,
247
+ Utility.symbol_object(comma),
248
+ ]
249
+ new_arr << expr unless expr.to_s.strip.empty?
250
+ new_arr
251
+ end
252
+
253
+ rule(rparen: simple(:rparen),
254
+ power: simple(:power),
255
+ comma: simple(:comma),
256
+ expr: sequence(:expr)) do
257
+ exponent = Math::Function::Power.new(
258
+ Utility.symbol_object(rparen),
259
+ Utility.unfenced_value(power),
260
+ )
261
+ new_arr = [
262
+ exponent,
263
+ Utility.symbol_object(comma),
264
+ ]
265
+ new_arr += expr
266
+ new_arr
267
+ end
268
+
269
+ rule(rparen: simple(:rparen),
270
+ base: simple(:base),
271
+ comma: simple(:comma)) do
272
+ [
273
+ Math::Function::Base.new(
274
+ Utility.symbol_object(rparen),
275
+ Utility.unfenced_value(base),
276
+ ),
277
+ Utility.symbol_object(comma),
278
+ ]
279
+ end
280
+
281
+ rule(comma: simple(:comma),
282
+ left_right: simple(:left_right)) do
283
+ [
284
+ Utility.symbol_object(comma),
285
+ left_right,
286
+ ]
287
+ end
288
+
289
+ rule(td: simple(:td)) do
290
+ Math::Function::Td.new(
291
+ [
292
+ Utility.td_value(td),
293
+ ],
294
+ )
295
+ end
296
+
297
+ rule(td: sequence(:td)) do
298
+ Utility.td_values(td, ",")
299
+ end
300
+
301
+ rule(open_tr: simple(:tr),
302
+ tds_list: simple(:tds_list)) do
303
+ Math::Function::Tr.new([tds_list])
304
+ end
305
+
73
306
  rule(open_tr: simple(:tr),
74
307
  tds_list: sequence(:tds_list)) do
75
308
  Math::Function::Tr.new(tds_list)
76
309
  end
77
310
 
78
- rule(base: simple(:base),
79
- expr: simple(:expr)) do
80
- [base, expr]
311
+ rule(fonts_class: simple(:font_style),
312
+ fonts_value: simple(:fonts_value)) do
313
+ Utility::FONT_STYLES[font_style.to_sym].new(
314
+ Utility.unfenced_value(fonts_value),
315
+ font_style.to_s,
316
+ )
81
317
  end
82
318
 
83
- rule(fonts: simple(:font_style),
84
- intermediate_exp: simple(:int_exp)) do
85
- Utility::FONT_STYLES[font_style.to_sym].new(
86
- int_exp,
87
- font_style,
319
+ rule(fonts_class: simple(:font_style),
320
+ fonts_value: simple(:fonts_value),
321
+ power: simple(:power)) do
322
+ font_object = Utility::FONT_STYLES[font_style.to_sym].new(
323
+ Utility.unfenced_value(fonts_value),
324
+ font_style.to_s,
325
+ )
326
+ Math::Function::Power.new(
327
+ font_object,
328
+ Utility.unfenced_value(power),
88
329
  )
89
330
  end
90
331
 
91
- rule(fonts: simple(:font_style),
92
- text: simple(:text)) do
93
- Utility::FONT_STYLES[font_style.to_sym].new(
94
- Math::Function::Text.new(text),
95
- font_style,
332
+ rule(unary_class: simple(:function),
333
+ fonts_class: simple(:font_style),
334
+ fonts_value: simple(:fonts_value)) do
335
+ font_object = Utility::FONT_STYLES[font_style.to_sym].new(
336
+ Utility.unfenced_value(fonts_value),
337
+ font_style.to_s,
338
+ )
339
+ first_value = if Utility::UNARY_CLASSES.include?(function)
340
+ font_object
341
+ else
342
+ Utility.unfenced_value(font_object)
343
+ end
344
+ Utility.get_class(function).new(first_value)
345
+ end
346
+
347
+ rule(fonts_class: simple(:font_style),
348
+ fonts_value: simple(:fonts_value),
349
+ base: simple(:base)) do
350
+ font_object = Utility::FONT_STYLES[font_style.to_sym].new(
351
+ Utility.unfenced_value(fonts_value),
352
+ font_style.to_s,
353
+ )
354
+ Math::Function::Base.new(
355
+ Utility.unfenced_value(font_object),
356
+ Utility.unfenced_value(base),
357
+ )
358
+ end
359
+
360
+ rule(power_base: simple(:power_base),
361
+ expr: subtree(:expr)) do
362
+ expr.flatten.compact.insert(0, power_base)
363
+ end
364
+
365
+ rule(frac: simple(:frac),
366
+ expr: subtree(:expr)) do
367
+ case expr
368
+ when Array
369
+ expr.flatten.compact.insert(0, frac)
370
+ else
371
+ new_arr = [frac]
372
+ new_arr << expr unless expr.to_s.strip.empty?
373
+ new_arr
374
+ end
375
+ end
376
+
377
+ rule(mod: simple(:mod),
378
+ expr: sequence(:expr)) do
379
+ expr.flatten.compact.insert(0, mod)
380
+ end
381
+
382
+ rule(frac: simple(:frac),
383
+ left_right: simple(:left_right)) do
384
+ new_arr = [frac]
385
+ new_arr << left_right unless left_right.to_s.strip.empty?
386
+ new_arr
387
+ end
388
+
389
+ rule(sequence: simple(:sequence),
390
+ frac: simple(:frac)) do
391
+ new_arr = [sequence]
392
+ new_arr << frac unless frac.to_s.strip.empty?
393
+ new_arr
394
+ end
395
+
396
+ rule(sequence: simple(:sequence),
397
+ base: simple(:base)) do
398
+ Math::Function::Base.new(
399
+ sequence,
400
+ Utility.unfenced_value(base),
401
+ )
402
+ end
403
+
404
+ rule(power_base: simple(:power_base),
405
+ power: simple(:power)) do
406
+ Math::Function::Power.new(
407
+ power_base,
408
+ Utility.unfenced_value(power),
409
+ )
410
+ end
411
+
412
+ rule(power_base: simple(:power_base),
413
+ power: sequence(:power)) do
414
+ first_value = power.shift if Utility.frac_values(power)
415
+ power_object = Math::Function::Power.new(power_base, first_value)
416
+ if power.empty?
417
+ power_object
418
+ else
419
+ [power_object] + power
420
+ end
421
+ end
422
+
423
+ rule(sequence: simple(:sequence),
424
+ symbol: simple(:symbol)) do
425
+ symbol_object = Utility.symbol_object(
426
+ (Constants::SYMBOLS[symbol.to_sym] || symbol).to_s,
427
+ )
428
+ new_arr = [sequence]
429
+ new_arr << symbol_object unless symbol.to_s.strip.empty?
430
+ new_arr
431
+ end
432
+
433
+ rule(power_base: simple(:power_base),
434
+ left_right: simple(:left_right)) do
435
+ new_arr = [power_base]
436
+ new_arr << left_right unless left_right.to_s.strip.empty?
437
+ new_arr
438
+ end
439
+
440
+ rule(power_base: simple(:power_base),
441
+ power: simple(:power)) do
442
+ Math::Function::Power.new(
443
+ power_base,
444
+ Utility.unfenced_value(power),
96
445
  )
97
446
  end
98
447
 
99
448
  rule(power_base: simple(:power_base),
449
+ power: simple(:power),
100
450
  expr: sequence(:expr)) do
101
- expr.insert(0, power_base)
451
+ power_object = Math::Function::Power.new(
452
+ power_base,
453
+ Utility.unfenced_value(power),
454
+ )
455
+ Math::Formula.new(
456
+ expr.flatten.compact.insert(0, power_object),
457
+ )
458
+ end
459
+
460
+ rule(power_base: simple(:power_base),
461
+ base: simple(:base)) do
462
+ Math::Function::Base.new(
463
+ Utility.unfenced_value(power_base),
464
+ Utility.unfenced_value(base),
465
+ )
466
+ end
467
+
468
+ rule(power_base: simple(:power_base),
469
+ base: simple(:base),
470
+ expr: sequence(:expr)) do
471
+ base_object = Math::Function::Base.new(
472
+ Utility.unfenced_value(power_base),
473
+ Utility.unfenced_value(base),
474
+ )
475
+ Math::Formula.new(
476
+ expr.flatten.compact.insert(0, base_object),
477
+ )
102
478
  end
103
479
 
104
480
  rule(power_base: simple(:power_base),
481
+ base: simple(:base),
105
482
  expr: simple(:expr)) do
106
- [power_base, expr]
483
+ base_object = Math::Function::Base.new(
484
+ Utility.unfenced_value(power_base),
485
+ Utility.unfenced_value(base),
486
+ )
487
+ formula_array = [base_object]
488
+ formula_array << expr unless expr.to_s.strip.empty?
489
+
490
+ Math::Formula.new(formula_array)
107
491
  end
108
492
 
109
- rule(sequence: simple(:sequence),
493
+ rule(power_base: simple(:power_base),
494
+ base_value: simple(:base_value),
495
+ power_value: simple(:power_value)) do
496
+ Math::Function::PowerBase.new(
497
+ Utility.unfenced_value(power_base),
498
+ Utility.unfenced_value(base_value),
499
+ Utility.unfenced_value(power_value),
500
+ )
501
+ end
502
+
503
+ rule(power_base: simple(:power_base),
504
+ base_value: simple(:base_value),
505
+ power_value: sequence(:power_value)) do
506
+ first_value = power_value
507
+ first_value = power_value.shift if Utility.frac_values(power_value)
508
+ power_base_object = Math::Function::PowerBase.new(
509
+ Utility.unfenced_value(power_base),
510
+ Utility.unfenced_value(base_value),
511
+ Utility.filter_values(first_value),
512
+ )
513
+ if power_value.empty?
514
+ power_base_object
515
+ else
516
+ Math::Formula.new(
517
+ power_value.insert(
518
+ 0,
519
+ power_base_object,
520
+ ),
521
+ )
522
+ end
523
+ end
524
+
525
+ rule(power_base: simple(:power_base),
526
+ base_value: simple(:base_value),
527
+ power_value: simple(:power_value),
110
528
  expr: sequence(:expr)) do
111
- expr.insert(0, sequence)
529
+ power_base_object = Math::Function::PowerBase.new(
530
+ Utility.unfenced_value(power_base),
531
+ Utility.unfenced_value(base_value),
532
+ Utility.unfenced_value(power_value),
533
+ )
534
+ Math::Formula.new(
535
+ expr.flatten.compact.insert(0, power_base_object),
536
+ )
112
537
  end
113
538
 
114
- rule(dividend: simple(:dividend),
115
- mod: simple(:mod),
116
- divisor: simple(:divisor)) do
117
- Math::Function::Mod.new(
118
- dividend,
119
- divisor,
539
+ rule(rparen: simple(:rparen),
540
+ base_value: simple(:base_value),
541
+ power_value: simple(:power_value)) do
542
+ Math::Function::PowerBase.new(
543
+ Utility.symbol_object(rparen),
544
+ Utility.unfenced_value(base_value),
545
+ Utility.unfenced_value(power_value),
120
546
  )
121
547
  end
122
548
 
123
- rule(unary: simple(:unary),
124
- "^": simple(:exponent),
125
- number: simple(:number)) do
549
+ rule(rparen: simple(:rparen),
550
+ base_value: simple(:base_value),
551
+ power_value: simple(:power_value),
552
+ expr: simple(:expr)) do
553
+ power_base = Math::Function::PowerBase.new(
554
+ Utility.symbol_object(rparen),
555
+ Utility.unfenced_value(base_value),
556
+ Utility.unfenced_value(power_value),
557
+ )
558
+ new_arr = [power_base]
559
+ new_arr << expr unless expr.to_s.strip.empty?
560
+ new_arr
561
+ end
562
+
563
+ rule(rparen: simple(:rparen),
564
+ base_value: sequence(:base_value),
565
+ power_value: simple(:power_value),
566
+ expr: simple(:expr)) do
567
+ power_base = Math::Function::PowerBase.new(
568
+ Utility.symbol_object(rparen),
569
+ Utility.unfenced_value(base_value),
570
+ Utility.unfenced_value(power_value),
571
+ )
572
+ new_arr = [power_base]
573
+ new_arr << expr unless expr.to_s.strip.empty?
574
+ new_arr
575
+ end
576
+
577
+ rule(rparen: simple(:rparen),
578
+ base_value: simple(:base_value),
579
+ power_value: simple(:power_value),
580
+ expr: sequence(:expr)) do
581
+ power_base = Math::Function::PowerBase.new(
582
+ Utility.symbol_object(rparen),
583
+ Utility.unfenced_value(base_value),
584
+ Utility.unfenced_value(power_value),
585
+ )
586
+ expr.flatten.compact.insert(0, power_base)
587
+ end
588
+
589
+ rule(rparen: simple(:rparen),
590
+ base_value: sequence(:base_value),
591
+ power_value: simple(:power_value),
592
+ expr: sequence(:expr)) do
593
+ power_base = Math::Function::PowerBase.new(
594
+ Utility.symbol_object(rparen),
595
+ Utility.unfenced_value(base_value),
596
+ Utility.unfenced_value(power_value),
597
+ )
598
+ expr.flatten.compact.insert(0, power_base)
599
+ end
600
+
601
+ rule(rparen: simple(:rparen),
602
+ base_value: sequence(:base_value),
603
+ power_value: simple(:power_value),
604
+ comma: simple(:comma),
605
+ expr: sequence(:expr)) do
606
+ coma = Utility.symbol_object(comma)
607
+ power_base = Math::Function::PowerBase.new(
608
+ Utility.symbol_object(rparen),
609
+ Utility.unfenced_value(base_value),
610
+ Utility.unfenced_value(power_value),
611
+ )
612
+ new_arr = [power_base, coma]
613
+ new_arr += expr.flatten.compact unless expr.to_s.strip.empty?
614
+ new_arr
615
+ end
616
+
617
+ rule(rparen: simple(:rparen),
618
+ base_value: simple(:base_value),
619
+ power_value: simple(:power_value),
620
+ comma: simple(:comma),
621
+ expr: sequence(:expr)) do
622
+ coma = Utility.symbol_object(comma)
623
+ power_base = Math::Function::PowerBase.new(
624
+ Utility.symbol_object(rparen),
625
+ Utility.unfenced_value(base_value),
626
+ Utility.unfenced_value(power_value),
627
+ )
628
+ new_arr = [power_base, coma]
629
+ new_arr += expr.flatten.compact unless expr.to_s.strip.empty?
630
+ new_arr
631
+ end
632
+
633
+ rule(intermediate_exp: simple(:int_exp),
634
+ power: simple(:power)) do
126
635
  Math::Function::Power.new(
127
- unary,
128
- Math::Number.new(number),
636
+ int_exp,
637
+ Utility.unfenced_value(power),
638
+ )
639
+ end
640
+
641
+ rule(intermediate_exp: simple(:int_exp),
642
+ power: sequence(:power)) do
643
+ Math::Function::Power.new(
644
+ int_exp,
645
+ Utility.unfenced_value(power),
646
+ )
647
+ end
648
+
649
+ rule(intermediate_exp: simple(:int_exp),
650
+ base: simple(:base)) do
651
+ Math::Function::Base.new(
652
+ Utility.unfenced_value(int_exp),
653
+ Utility.unfenced_value(base),
129
654
  )
130
655
  end
131
656
 
657
+ rule(power_base: simple(:power_base),
658
+ expr: simple(:expr)) do
659
+ new_arr = [power_base]
660
+ new_arr << expr unless expr.to_s.strip.empty?
661
+ new_arr
662
+ end
663
+
664
+ rule(power_base: sequence(:power_base),
665
+ expr: simple(:expr)) do
666
+ new_arr = power_base
667
+ new_arr << expr unless expr.to_s.strip.empty?
668
+ new_arr
669
+ end
670
+
671
+ rule(power_base: sequence(:power_base),
672
+ expr: sequence(:expr)) do
673
+ power_base + expr
674
+ end
675
+
676
+ rule(sequence: simple(:sequence),
677
+ expr: subtree(:expr)) do
678
+ case expr
679
+ when Array
680
+ expr.flatten.compact.insert(0, sequence)
681
+ else
682
+ new_array = [sequence]
683
+ new_array << expr unless expr.to_s.strip.empty?
684
+ new_array
685
+ end
686
+ end
687
+
688
+ rule(sequence: sequence(:sequence),
689
+ expr: simple(:expr)) do
690
+ new_arr = sequence.flatten.compact
691
+ new_arr << expr unless expr.to_s.strip.empty?
692
+ new_arr
693
+ end
694
+
695
+ rule(sequence: sequence(:sequence),
696
+ expr: sequence(:expr)) do
697
+ sequence.flatten.compact + expr.flatten.compact
698
+ end
699
+
132
700
  rule(unary: simple(:unary),
133
- "^": simple(:exponent),
134
- intermediate_exp: simple(:intermediate_exp)) do
701
+ power: simple(:power)) do
135
702
  Math::Function::Power.new(
136
703
  unary,
137
- intermediate_exp,
704
+ Utility.unfenced_value(power),
138
705
  )
139
706
  end
140
707
 
141
708
  rule(unary: simple(:unary),
142
- "^": simple(:exponent),
143
- text: simple(:text)) do
144
- Math::Function::Power.new(
709
+ base: simple(:base)) do
710
+ Math::Function::Base.new(
145
711
  unary,
146
- text.is_a?(String) ? Utility.get_class("text").new(text) : text,
712
+ Utility.unfenced_value(base),
713
+ )
714
+ end
715
+
716
+ rule(binary_class: simple(:function),
717
+ base_value: simple(:base)) do
718
+ Utility.get_class(function).new(
719
+ Utility.unfenced_value(base),
720
+ )
721
+ end
722
+
723
+ rule(binary_class: simple(:function),
724
+ base_value: sequence(:base)) do
725
+ binary = Utility.get_class(function).new(
726
+ Utility.unfenced_value(base.shift),
727
+ Utility.unfenced_value(base.shift),
728
+ )
729
+ new_arr = [binary]
730
+ new_arr += base.flatten.compact unless base.empty?
731
+ new_arr
732
+ end
733
+
734
+ rule(d: simple(:d),
735
+ x: simple(:x)) do
736
+ Math::Formula.new(
737
+ [
738
+ Utility.symbol_object(d),
739
+ Utility.symbol_object(x),
740
+ ],
147
741
  )
148
742
  end
149
743
 
150
744
  rule(symbol: simple(:symbol),
151
- "^": simple(:exponent),
152
- number: simple(:number)) do
745
+ power: simple(:power)) do
153
746
  Math::Function::Power.new(
154
- Math::Symbol.new(symbol),
155
- Math::Number.new(number),
747
+ Utility.symbol_object(
748
+ (Constants::SYMBOLS[symbol.to_sym] || symbol).to_s,
749
+ ),
750
+ Utility.unfenced_value(power),
751
+ )
752
+ end
753
+
754
+ rule(symbol: simple(:symbol),
755
+ power: sequence(:power)) do
756
+ Math::Function::Power.new(
757
+ Utility.symbol_object(
758
+ (Constants::SYMBOLS[symbol.to_sym] || symbol).to_s,
759
+ ),
760
+ Utility.unfenced_value(power),
761
+ )
762
+ end
763
+
764
+ rule(symbol: simple(:sym),
765
+ expr: sequence(:expr)) do
766
+ symbol = Utility.symbol_object(
767
+ (Constants::SYMBOLS[sym.to_sym] || sym).to_s,
768
+ )
769
+ expr.flatten.compact.insert(0, symbol)
770
+ end
771
+
772
+ rule(symbol: simple(:sym),
773
+ expr: simple(:expr)) do
774
+ symbol = Utility.symbol_object(
775
+ (Constants::SYMBOLS[sym.to_sym] || sym).to_s,
156
776
  )
777
+ [symbol, expr]
157
778
  end
158
779
 
159
- rule(binary: simple(:binary),
160
- "^": simple(:under_score),
780
+ rule(symbol: simple(:symbol),
781
+ base: simple(:base)) do
782
+ Math::Function::Base.new(
783
+ Utility.symbol_object(
784
+ (Constants::SYMBOLS[symbol.to_sym] || symbol).to_s,
785
+ ),
786
+ Utility.unfenced_value(base),
787
+ )
788
+ end
789
+
790
+ rule(binary_class: simple(:function),
791
+ base: simple(:base)) do
792
+ Utility.get_class(function).new(
793
+ Utility.unfenced_value(base),
794
+ )
795
+ end
796
+
797
+ rule(binary_class: simple(:function),
798
+ power: simple(:power)) do
799
+ Utility.get_class(function).new(
800
+ nil,
801
+ Utility.unfenced_value(power),
802
+ )
803
+ end
804
+
805
+ rule(sequence: simple(:sequence),
806
+ symbol: simple(:sym),
807
+ expr: simple(:expr)) do
808
+ symbol = Utility.symbol_object(
809
+ (Constants::SYMBOLS[sym.to_sym] || sym).to_s,
810
+ )
811
+ [sequence, symbol, expr]
812
+ end
813
+
814
+ rule(sequence: simple(:sequence),
815
+ symbol: simple(:sym),
816
+ expr: sequence(:expr)) do
817
+ symbol = Utility.symbol_object(
818
+ (Constants::SYMBOLS[sym.to_sym] || sym).to_s,
819
+ )
820
+ new_arr = [sequence, symbol]
821
+ new_arr += expr.flatten.compact unless expr.to_s.strip.empty?
822
+ new_arr
823
+ end
824
+
825
+ rule(binary_class: simple(:function),
826
+ base_value: simple(:base),
827
+ power_value: simple(:power)) do
828
+ Utility.get_class(function).new(
829
+ Utility.unfenced_value(base),
830
+ Utility.unfenced_value(power),
831
+ )
832
+ end
833
+
834
+ rule(binary_class: simple(:function),
835
+ base_value: simple(:base),
836
+ power_value: simple(:power),
837
+ expr: sequence(:expr)) do
838
+ [
839
+ Utility.get_class(function).new(
840
+ Utility.unfenced_value(base),
841
+ Utility.unfenced_value(power),
842
+ ),
843
+ ] + expr
844
+ end
845
+
846
+ rule(binary_class: simple(:function),
847
+ base_value: simple(:base),
848
+ power_value: simple(:power),
849
+ expr: simple(:expr)) do
850
+ [
851
+ Utility.get_class(function).new(
852
+ Utility.unfenced_value(base),
853
+ Utility.unfenced_value(power),
854
+ ),
855
+ expr,
856
+ ]
857
+ end
858
+
859
+ rule(unary_class: simple(:function),
161
860
  intermediate_exp: simple(:int_exp)) do
861
+ first_value = if Utility::UNARY_CLASSES.include?(function)
862
+ int_exp
863
+ else
864
+ Utility.unfenced_value(int_exp)
865
+ end
866
+ Utility.get_class(function).new(first_value)
867
+ end
868
+
869
+ rule(unary_class: simple(:function),
870
+ symbol: simple(:symbol)) do
871
+ symbol_object = Utility.symbol_object(
872
+ (Constants::SYMBOLS[symbol.to_sym] || symbol).to_s,
873
+ )
874
+ Utility.get_class(function).new(symbol_object)
875
+ end
876
+
877
+ rule(unary_class: simple(:function),
878
+ number: simple(:new_number)) do
879
+ number = Math::Number.new(new_number)
880
+ Utility.get_class(function).new(number)
881
+ end
882
+
883
+ rule(unary_class: simple(:function),
884
+ unary: simple(:unary)) do
885
+ first_value = if Utility::UNARY_CLASSES.include?(function)
886
+ unary
887
+ else
888
+ Utility.unfenced_value(unary)
889
+ end
890
+ Utility.get_class(function).new(first_value)
891
+ end
892
+
893
+ rule(unary_class: simple(:function),
894
+ binary_class: simple(:binary_class)) do
895
+ [
896
+ Utility.get_class(function).new,
897
+ Utility.get_class(binary_class).new,
898
+ ]
899
+ end
900
+
901
+ rule(unary_class: simple(:function),
902
+ text: simple(:text)) do
903
+ Utility.get_class(function).new(
904
+ Math::Function::Text.new(text),
905
+ )
906
+ end
907
+
908
+ rule(number: simple(:number),
909
+ comma: simple(:comma)) do
910
+ [
911
+ Math::Number.new(number),
912
+ Utility.symbol_object(comma),
913
+ ]
914
+ end
915
+
916
+ rule(number: simple(:number),
917
+ power: simple(:power)) do
162
918
  Math::Function::Power.new(
163
- binary,
164
- int_exp,
919
+ Math::Number.new(number),
920
+ Utility.unfenced_value(power),
165
921
  )
166
922
  end
167
923
 
168
- rule(binary: simple(:function),
169
- "^": simple(:exponent),
170
- number: simple(:number)) do
924
+ rule(number: simple(:number),
925
+ power: sequence(:power)) do
171
926
  Math::Function::Power.new(
172
- function,
173
927
  Math::Number.new(number),
928
+ Utility.unfenced_value(power),
929
+ )
930
+ end
931
+
932
+ rule(text: simple(:text),
933
+ base: simple(:base)) do
934
+ Math::Function::Base.new(
935
+ Math::Function::Text.new(text),
936
+ Utility.unfenced_value(base),
937
+ )
938
+ end
939
+
940
+ rule(text: sequence(:text),
941
+ base: simple(:base)) do
942
+ Math::Function::Base.new(
943
+ Math::Function::Text.new(text.join),
944
+ Utility.unfenced_value(base),
945
+ )
946
+ end
947
+
948
+ rule(text: simple(:text),
949
+ power: simple(:power)) do
950
+ Math::Function::Power.new(
951
+ Math::Function::Text.new(text),
952
+ Utility.unfenced_value(power),
953
+ )
954
+ end
955
+
956
+ rule(text: sequence(:text),
957
+ power: simple(:power)) do
958
+ Math::Function::Power.new(
959
+ Math::Function::Text.new(text.join),
960
+ Utility.unfenced_value(power),
174
961
  )
175
962
  end
176
963
 
964
+ rule(table: simple(:table),
965
+ expr: sequence(:expr)) do
966
+ Math::Formula.new([table] + expr.flatten.compact)
967
+ end
968
+
969
+ rule(table: simple(:table),
970
+ expr: simple(:expr)) do
971
+ formula_array = [table]
972
+ formula_array << expr unless expr.to_s.strip.empty?
973
+ Math::Formula.new(formula_array)
974
+ end
975
+
177
976
  rule(left: simple(:left),
178
977
  left_right_value: simple(:left_right),
179
978
  right: simple(:right)) do
@@ -186,185 +985,224 @@ module Plurimath
186
985
  )
187
986
  end
188
987
 
189
- Constants::UNARY_CLASSES.each do |unary_class|
190
- rule(unary_class => simple(:function),
191
- intermediate_exp: simple(:int_exp)) do
192
- Utility.get_class(function).new(int_exp)
193
- end
194
-
195
- rule(unary_class => simple(:function),
196
- _: simple(:under_score),
197
- intermediate_exp: simple(:int_exp)) do
198
- Math::Function::Base.new(
199
- Utility.get_class(function).new,
200
- int_exp,
201
- )
202
- end
203
-
204
- rule(unary_class => simple(:function),
205
- symbol: simple(:new_symbol)) do
206
- symbol = Math::Symbol.new(new_symbol)
207
- Utility.get_class(function).new(symbol)
208
- end
988
+ rule(dividend: simple(:dividend),
989
+ mod: simple(:mod),
990
+ divisor: simple(:divisor)) do
991
+ Math::Function::Mod.new(
992
+ dividend,
993
+ divisor,
994
+ )
995
+ end
209
996
 
210
- rule(unary_class => simple(:function),
211
- number: simple(:new_number)) do
212
- number = Math::Number.new(new_number)
213
- Utility.get_class(function).new(number)
214
- end
997
+ rule(text: simple(:text),
998
+ base_value: simple(:base),
999
+ power_value: simple(:power)) do
1000
+ Math::Function::PowerBase.new(
1001
+ Math::Function::Text.new(text),
1002
+ Utility.unfenced_value(base),
1003
+ Utility.unfenced_value(power),
1004
+ )
1005
+ end
215
1006
 
216
- rule(unary_class => simple(:function),
217
- "^": simple(:base),
218
- intermediate_exp: simple(:unary)) do
219
- Math::Function::Power.new(
220
- Utility.get_class(unary_class).new,
221
- unary,
222
- )
223
- end
1007
+ rule(number: simple(:number),
1008
+ base_value: simple(:base),
1009
+ power_value: simple(:power)) do
1010
+ Math::Function::PowerBase.new(
1011
+ Math::Number.new(number),
1012
+ Utility.unfenced_value(base),
1013
+ Utility.unfenced_value(power),
1014
+ )
1015
+ end
224
1016
 
225
- rule(unary_class => simple(:function),
226
- _: simple(:base),
227
- symbol: simple(:symbol)) do
228
- Math::Function::Base.new(
229
- Utility.get_class(unary_class).new,
230
- Math::Symbol.new(symbol),
231
- )
232
- end
1017
+ rule(unary: simple(:unary),
1018
+ base_value: simple(:base),
1019
+ power_value: simple(:power)) do
1020
+ Math::Function::PowerBase.new(
1021
+ unary,
1022
+ Utility.unfenced_value(base),
1023
+ Utility.unfenced_value(power),
1024
+ )
1025
+ end
233
1026
 
234
- rule(unary_class => simple(:function),
235
- _: simple(:under_score),
236
- base: simple(:base),
237
- "^": simple(:power),
238
- exponent: simple(:exponent)) do
239
- Math::Function::PowerBase.new(
240
- Utility.get_class(function).new,
241
- base,
242
- exponent,
243
- )
244
- end
1027
+ rule(unary: sequence(:unary),
1028
+ base_value: simple(:base),
1029
+ power_value: simple(:power)) do
1030
+ unary.first.new(
1031
+ unary.last.new(
1032
+ Utility.unfenced_value(base),
1033
+ Utility.unfenced_value(power),
1034
+ ),
1035
+ )
245
1036
  end
246
1037
 
247
- Constants::BINARY_CLASSES.each do |binary_class|
248
- rule(binary_class => simple(:function)) do
249
- Utility.get_class(function).new
250
- end
1038
+ rule(intermediate_exp: simple(:int_exp),
1039
+ base_value: simple(:base),
1040
+ power_value: simple(:power)) do
1041
+ Math::Function::PowerBase.new(
1042
+ int_exp,
1043
+ Utility.unfenced_value(base),
1044
+ Utility.unfenced_value(power),
1045
+ )
1046
+ end
251
1047
 
252
- rule(binary_class => simple(:function),
253
- _: simple(:under_score),
254
- base: simple(:int_exp),
255
- "^": simple(:power),
256
- exponent: simple(:exponent)) do
257
- Utility.get_class(function).new(
258
- int_exp,
259
- exponent,
260
- )
261
- end
1048
+ rule(intermediate_exp: simple(:int_exp),
1049
+ base_value: simple(:base),
1050
+ power_value: sequence(:power)) do
1051
+ Math::Function::PowerBase.new(
1052
+ int_exp,
1053
+ Utility.unfenced_value(base),
1054
+ Utility.filter_values(power),
1055
+ )
1056
+ end
262
1057
 
263
- rule(binary_class => simple(:function),
264
- _: simple(:under_score),
265
- intermediate_exp: simple(:int_exp)) do
266
- Utility.get_class(function).new(int_exp, nil)
267
- end
1058
+ rule(symbol: simple(:symbol),
1059
+ base_value: simple(:base),
1060
+ power_value: simple(:power)) do
1061
+ Math::Function::PowerBase.new(
1062
+ Utility.symbol_object(
1063
+ (Constants::SYMBOLS[symbol.to_sym] || symbol).to_s,
1064
+ ),
1065
+ Utility.unfenced_value(base),
1066
+ Utility.unfenced_value(power),
1067
+ )
1068
+ end
268
1069
 
269
- rule(binary_class => simple(:function),
270
- "^": simple(:exponent),
271
- intermediate_exp: simple(:int_exp)) do
272
- Utility.get_class(function).new(nil, int_exp)
273
- end
1070
+ rule(symbol: simple(:symbol),
1071
+ base_value: simple(:base),
1072
+ power_value: sequence(:power)) do
1073
+ symbol_object = Utility.symbol_object(
1074
+ (Constants::SYMBOLS[symbol.to_sym] || symbol).to_s,
1075
+ )
1076
+ Math::Function::PowerBase.new(
1077
+ symbol_object,
1078
+ Utility.unfenced_value(base),
1079
+ Utility.filter_values(power),
1080
+ )
1081
+ end
274
1082
 
275
- rule(binary_class => simple(:function),
276
- _: simple(:under_score),
277
- number: simple(:number)) do
278
- Utility.get_class(function).new(
279
- Math::Number.new(number), nil
1083
+ rule(lparen: simple(:lparen),
1084
+ expr: simple(:expr),
1085
+ rparen: simple(:rparen)) do
1086
+ form_value = if expr.is_a?(Slice)
1087
+ expr.to_s.empty? ? nil : [expr]
1088
+ else
1089
+ [expr]
1090
+ end
1091
+ right_paren = rparen.to_s.empty? ? "" : rparen
1092
+ if expr.is_a?(Math::Function::Text)
1093
+ expr
1094
+ else
1095
+ Math::Function::Fenced.new(
1096
+ Utility.symbol_object(lparen),
1097
+ form_value&.flatten&.compact,
1098
+ Utility.symbol_object(right_paren),
280
1099
  )
281
1100
  end
1101
+ end
282
1102
 
283
- rule(binary_class => simple(:function),
284
- _: simple(:under_score),
285
- symbol: simple(:symbol)) do
286
- Utility.get_class(function).new(
287
- Math::Symbol.new(symbol), nil
288
- )
289
- end
1103
+ rule(lparen: simple(:lparen),
1104
+ expr: sequence(:expr),
1105
+ rparen: simple(:rparen)) do
1106
+ right_paren = rparen.to_s.empty? ? "" : rparen
1107
+ Math::Function::Fenced.new(
1108
+ Utility.symbol_object(lparen),
1109
+ expr.flatten.compact,
1110
+ Utility.symbol_object(right_paren),
1111
+ )
1112
+ end
290
1113
 
291
- rule(binary_class => simple(:function),
292
- _: simple(:under_score),
293
- unary: simple(:unary)) do
294
- Utility.get_class(function).new(unary, nil)
295
- end
1114
+ rule(lparen: simple(:lparen),
1115
+ text: simple(:text),
1116
+ rparen: simple(:rparen)) do
1117
+ Math::Function::Text.new(text)
1118
+ end
296
1119
 
297
- rule(binary_class => simple(:function),
298
- "^": simple(:power),
299
- number: simple(:number)) do
300
- Utility.get_class(function).new(
301
- nil,
302
- Math::Number.new(number),
303
- )
304
- end
1120
+ rule(lparen: simple(:lparen),
1121
+ rgb_color: sequence(:color),
1122
+ rparen: simple(:rparen)) do
1123
+ Math::Formula.new(color)
1124
+ end
305
1125
 
306
- rule(binary_class => simple(:function),
307
- "^": simple(:power),
308
- symbol: simple(:symbol)) do
309
- Utility.get_class(function).new(
310
- nil,
311
- Math::Symbol.new(symbol),
312
- )
313
- end
1126
+ rule(lparen: simple(:lparen),
1127
+ rgb_color: simple(:color),
1128
+ rparen: simple(:rparen)) do
1129
+ Math::Formula.new(color)
1130
+ end
314
1131
 
315
- rule(binary_class => simple(:function),
316
- base: simple(:base),
317
- exponent: simple(:exponent)) do
318
- Utility.get_class(function).new(
319
- base,
320
- exponent,
321
- )
322
- end
1132
+ rule(color: sequence(:color),
1133
+ color_value: sequence(:color_value)) do
1134
+ Utility.symbol_object(color)
1135
+ end
323
1136
 
324
- rule(binary: simple(:function),
325
- "^": simple(:exponent),
326
- text: simple(:text)) do
327
- Math::Function::Power.new(
328
- function,
329
- text.is_a?(String) ? Utility.get_class("text").new(text) : text,
330
- )
331
- end
1137
+ rule(color: simple(:color),
1138
+ color_value: simple(:value),
1139
+ expr: simple(:expr)) do
1140
+ color_object = Math::Function::Color.new(
1141
+ color,
1142
+ Utility.unfenced_value(value),
1143
+ )
1144
+ [color_object, expr]
1145
+ end
332
1146
 
333
- rule(binary: simple(:function),
334
- "^": simple(:exponent),
335
- unary: simple(:unary)) do
336
- Math::Function::Power.new(
337
- function,
338
- unary,
339
- )
340
- end
1147
+ rule(color: simple(:color),
1148
+ color_value: simple(:value),
1149
+ expr: sequence(:expr)) do
1150
+ color_object = Math::Function::Color.new(
1151
+ color,
1152
+ Utility.unfenced_value(value),
1153
+ )
1154
+ expr.insert(0, color_object)
1155
+ end
341
1156
 
342
- Constants::UNARY_CLASSES.each do |unary_class|
343
- rule(binary_class => simple(:function),
344
- _: simple(:base),
345
- unary_class => simple(:unary)) do
346
- unary_class = Utility.get_class(unary).new
347
- Utility.get_class(binary_class).new(unary_class, nil)
348
- end
349
-
350
- rule(binary_class => simple(:function),
351
- "^": simple(:base),
352
- unary_class => simple(:unary)) do
353
- unary_class = Utility.get_class(unary).new
354
- Utility.get_class(binary_class).new(nil, unary_class)
355
- end
356
- end
1157
+ rule(color: simple(:color),
1158
+ color_value: simple(:color_value)) do
1159
+ first_value = if color.is_a?(Math::Function::Text)
1160
+ Utility.symbol_object(color.parameter_one)
1161
+ else
1162
+ color
1163
+ end
1164
+ Math::Function::Color.new(
1165
+ first_value,
1166
+ Utility.unfenced_value(color_value),
1167
+ )
1168
+ end
1169
+
1170
+ rule(table_left: simple(:table_left),
1171
+ table_row: simple(:table_row),
1172
+ table_right: simple(:table_right)) do
1173
+ Math::Function::Table.new(
1174
+ [
1175
+ table_row,
1176
+ ],
1177
+ Utility.symbol_object(table_left).value,
1178
+ Utility.symbol_object(table_right).value,
1179
+ )
357
1180
  end
358
1181
 
359
1182
  rule(table_left: simple(:table_left),
360
1183
  table_row: simple(:table_row),
361
1184
  expr: simple(:expr),
362
1185
  table_right: simple(:table_right)) do
1186
+ new_arr = [table_row]
1187
+ new_arr << expr unless expr.to_s.strip.empty?
363
1188
  Math::Function::Table.new(
1189
+ new_arr,
1190
+ Utility.symbol_object(table_left).value,
1191
+ Utility.symbol_object(table_right).value,
1192
+ )
1193
+ end
1194
+
1195
+ rule(norm: simple(:function),
1196
+ table_left: simple(:table_left),
1197
+ table_row: simple(:table_row),
1198
+ expr: simple(:expr),
1199
+ table_right: simple(:table_right)) do
1200
+ table = Math::Function::Table.new(
364
1201
  [table_row, expr],
365
- table_left,
366
- table_right,
1202
+ Utility.symbol_object(table_left).value,
1203
+ Utility.symbol_object(table_right).value,
367
1204
  )
1205
+ Math::Function::Norm.new(table)
368
1206
  end
369
1207
 
370
1208
  rule(table_left: simple(:table_left),
@@ -372,9 +1210,9 @@ module Plurimath
372
1210
  expr: sequence(:expr),
373
1211
  table_right: simple(:table_right)) do
374
1212
  Math::Function::Table.new(
375
- expr.insert(0, table_row),
376
- table_left,
377
- table_right,
1213
+ expr.flatten.compact.insert(0, table_row),
1214
+ Utility.symbol_object(table_left).value,
1215
+ Utility.symbol_object(table_right).value,
378
1216
  )
379
1217
  end
380
1218
 
@@ -382,12 +1220,10 @@ module Plurimath
382
1220
  table_row: simple(:table_row),
383
1221
  expr: sequence(:expr),
384
1222
  right: simple(:right)) do
385
- Math::Formula.new(
386
- [
387
- Math::Function::Left.new(left),
388
- Math::Function::Table.new(expr.insert(0, table_row), "", ""),
389
- Math::Function::Right.new(right),
390
- ],
1223
+ Math::Function::Table.new(
1224
+ expr.flatten.compact.insert(0, table_row),
1225
+ Utility.symbol_object(left).value,
1226
+ Utility.symbol_object(right).value,
391
1227
  )
392
1228
  end
393
1229
  end