plurimath 0.2.0 → 0.2.2

Sign up to get free protection for your applications and to get access to all the features.
Files changed (121) 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/.gitignore +1 -0
  5. data/.hound.yml +5 -0
  6. data/.rubocop.yml +8 -0
  7. data/AsciiMath-Supported-Data.adoc +2000 -0
  8. data/Gemfile +3 -0
  9. data/Latex-Supported-Data.adoc +1879 -0
  10. data/MathML-Supported-Data.adoc +287 -0
  11. data/README.adoc +96 -0
  12. data/lib/plurimath/asciimath/constants.rb +267 -229
  13. data/lib/plurimath/asciimath/parse.rb +125 -26
  14. data/lib/plurimath/asciimath/parser.rb +6 -3
  15. data/lib/plurimath/asciimath/transform.rb +1135 -208
  16. data/lib/plurimath/asciimath.rb +1 -1
  17. data/lib/plurimath/html/constants.rb +50 -0
  18. data/lib/plurimath/html/parse.rb +149 -0
  19. data/lib/plurimath/html/parser.rb +26 -0
  20. data/lib/plurimath/html/transform.rb +363 -0
  21. data/lib/plurimath/html.rb +1 -1
  22. data/lib/plurimath/latex/constants.rb +3729 -1906
  23. data/lib/plurimath/latex/parse.rb +167 -51
  24. data/lib/plurimath/latex/parser.rb +12 -4
  25. data/lib/plurimath/latex/transform.rb +598 -183
  26. data/lib/plurimath/math/base.rb +15 -0
  27. data/lib/plurimath/math/formula.rb +96 -11
  28. data/lib/plurimath/math/function/bar.rb +34 -0
  29. data/lib/plurimath/math/function/base.rb +32 -5
  30. data/lib/plurimath/math/function/binary_function.rb +104 -17
  31. data/lib/plurimath/math/function/cancel.rb +8 -0
  32. data/lib/plurimath/math/function/ceil.rb +3 -0
  33. data/lib/plurimath/math/function/color.rb +16 -6
  34. data/lib/plurimath/math/function/f.rb +8 -0
  35. data/lib/plurimath/math/function/fenced.rb +95 -3
  36. data/lib/plurimath/math/function/floor.rb +15 -0
  37. data/lib/plurimath/math/function/font_style/bold.rb +37 -0
  38. data/lib/plurimath/math/function/font_style/double_struck.rb +37 -0
  39. data/lib/plurimath/math/function/font_style/fraktur.rb +37 -0
  40. data/lib/plurimath/math/function/font_style/italic.rb +37 -0
  41. data/lib/plurimath/math/function/font_style/monospace.rb +37 -0
  42. data/lib/plurimath/math/function/font_style/normal.rb +37 -0
  43. data/lib/plurimath/math/function/font_style/sans-serif.rb +37 -0
  44. data/lib/plurimath/math/function/font_style/script.rb +37 -0
  45. data/lib/plurimath/math/function/font_style.rb +18 -25
  46. data/lib/plurimath/math/function/frac.rb +35 -5
  47. data/lib/plurimath/math/function/g.rb +7 -0
  48. data/lib/plurimath/math/function/hat.rb +12 -0
  49. data/lib/plurimath/math/function/inf.rb +21 -1
  50. data/lib/plurimath/math/function/int.rb +23 -2
  51. data/lib/plurimath/math/function/left.rb +25 -4
  52. data/lib/plurimath/math/function/lim.rb +40 -2
  53. data/lib/plurimath/math/function/limits.rb +8 -0
  54. data/lib/plurimath/math/function/log.rb +61 -4
  55. data/lib/plurimath/math/function/longdiv.rb +12 -0
  56. data/lib/plurimath/math/function/mbox.rb +31 -0
  57. data/lib/plurimath/math/function/menclose.rb +46 -0
  58. data/lib/plurimath/math/function/merror.rb +12 -0
  59. data/lib/plurimath/math/function/mod.rb +25 -4
  60. data/lib/plurimath/math/function/msgroup.rb +37 -0
  61. data/lib/plurimath/math/function/msline.rb +12 -0
  62. data/lib/plurimath/math/function/multiscript.rb +30 -0
  63. data/lib/plurimath/math/function/norm.rb +18 -1
  64. data/lib/plurimath/math/function/obrace.rb +17 -0
  65. data/lib/plurimath/math/function/oint.rb +2 -2
  66. data/lib/plurimath/math/function/over.rb +36 -0
  67. data/lib/plurimath/math/function/overset.rb +36 -7
  68. data/lib/plurimath/math/function/phantom.rb +28 -0
  69. data/lib/plurimath/math/function/power.rb +33 -9
  70. data/lib/plurimath/math/function/power_base.rb +110 -5
  71. data/lib/plurimath/math/function/prod.rb +29 -2
  72. data/lib/plurimath/math/function/right.rb +44 -0
  73. data/lib/plurimath/math/function/root.rb +27 -4
  74. data/lib/plurimath/math/function/rule.rb +33 -0
  75. data/lib/plurimath/math/function/scarries.rb +12 -0
  76. data/lib/plurimath/math/function/scarry.rb +12 -0
  77. data/lib/plurimath/math/function/sqrt.rb +24 -2
  78. data/lib/plurimath/math/function/stackrel.rb +27 -0
  79. data/lib/plurimath/math/function/substack.rb +7 -1
  80. data/lib/plurimath/math/function/sum.rb +56 -2
  81. data/lib/plurimath/math/function/sup.rb +3 -0
  82. data/lib/plurimath/math/function/table/align.rb +24 -0
  83. data/lib/plurimath/math/function/table/array.rb +44 -0
  84. data/lib/plurimath/math/function/table/bmatrix.rb +37 -0
  85. data/lib/plurimath/math/function/table/matrix.rb +32 -0
  86. data/lib/plurimath/math/function/table/multline.rb +24 -0
  87. data/lib/plurimath/math/function/table/pmatrix.rb +24 -0
  88. data/lib/plurimath/math/function/table/split.rb +24 -0
  89. data/lib/plurimath/math/function/table/vmatrix.rb +24 -0
  90. data/lib/plurimath/math/function/table.rb +190 -20
  91. data/lib/plurimath/math/function/td.rb +27 -9
  92. data/lib/plurimath/math/function/ternary_function.rb +83 -8
  93. data/lib/plurimath/math/function/text.rb +45 -8
  94. data/lib/plurimath/math/function/tr.rb +28 -4
  95. data/lib/plurimath/math/function/ubrace.rb +17 -0
  96. data/lib/plurimath/math/function/ul.rb +29 -0
  97. data/lib/plurimath/math/function/unary_function.rb +85 -7
  98. data/lib/plurimath/math/function/underline.rb +12 -0
  99. data/lib/plurimath/math/function/underover.rb +107 -0
  100. data/lib/plurimath/math/function/underset.rb +39 -0
  101. data/lib/plurimath/math/function/vec.rb +10 -0
  102. data/lib/plurimath/math/function.rb +13 -2
  103. data/lib/plurimath/math/number.rb +11 -3
  104. data/lib/plurimath/math/symbol.rb +57 -9
  105. data/lib/plurimath/math/unicode.rb +11 -0
  106. data/lib/plurimath/math.rb +15 -6
  107. data/lib/plurimath/mathml/constants.rb +224 -179
  108. data/lib/plurimath/mathml/parser.rb +24 -7
  109. data/lib/plurimath/mathml/transform.rb +249 -148
  110. data/lib/plurimath/mathml.rb +1 -1
  111. data/lib/plurimath/omml/parser.rb +42 -0
  112. data/lib/plurimath/omml/transform.rb +278 -0
  113. data/lib/plurimath/omml.rb +1 -1
  114. data/lib/plurimath/unitsml.rb +4 -0
  115. data/lib/plurimath/utility.rb +395 -0
  116. data/lib/plurimath/version.rb +1 -1
  117. data/plurimath.gemspec +1 -0
  118. metadata +66 -9
  119. data/.github/workflows/test.yml +0 -36
  120. data/README.md +0 -40
  121. data/lib/plurimath/mathml/parse.rb +0 -63
@@ -3,301 +3,1228 @@
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 }
11
- rule(unary: simple(:unary)) { unary }
12
- rule(power: sequence(:power)) { power }
13
- rule(binary: simple(:binary)) { binary }
14
- rule(sequence: simple(:sequence)) { sequence }
15
- rule(power_base: simple(:power_base)) { power_base }
16
- rule(intermediate_exp: simple(:int_exp)) { int_exp }
17
- rule(mod: simple(:mod), expr: simple(:expr)) { [mod, expr] }
18
- rule(base: sequence(:base), expr: simple(:exp)) { base + [exp] }
6
+ rule(mod: simple(:mod)) { mod }
7
+ rule(frac: simple(:frac)) { frac }
8
+ rule(unary: simple(:unary)) { unary }
9
+ rule(table: simple(:table)) { table }
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) }
19
14
 
20
- rule(expr: sequence(:expr)) do
21
- Plurimath::Math::Formula.new(expr)
15
+ rule(sequence: simple(:sequence)) { sequence }
16
+ rule(table_row: simple(:table_row)) { table_row }
17
+ rule(sequence: sequence(:sequence)) { sequence }
18
+ rule(power_base: simple(:power_base)) { power_base }
19
+ rule(left_right: simple(:left_right)) { left_right }
20
+ rule(table_left: simple(:table_left)) { table_left }
21
+
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
+ )
22
34
  end
23
35
 
24
- rule(power: sequence(:power), expr: simple(:expr)) do
25
- power + [expr]
36
+ rule(unary_class: simple(:unary)) do
37
+ Utility.get_class(unary).new
26
38
  end
27
39
 
28
- rule(power: sequence(:power), expr: sequence(:expr)) do
29
- power + expr
40
+ rule(binary_class: simple(:binary)) do
41
+ Utility.get_class(binary).new
30
42
  end
31
43
 
32
- rule(number: simple(:number)) do
33
- Plurimath::Math::Number.new(number.to_s)
44
+ rule(comma_separated: subtree(:comma_separated)) do
45
+ comma_separated.flatten
34
46
  end
35
47
 
36
48
  rule(symbol: simple(:symbol)) do
37
- Plurimath::Math::Symbol.new(symbol.to_s)
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
38
61
  end
39
62
 
40
63
  rule(text: simple(:text)) do
41
- text.is_a?(Slice) ? Transform.get_class("text").new(text.to_s) : text
64
+ text.is_a?(Slice) ? Utility.get_class("text").new(text) : text
42
65
  end
43
66
 
44
- rule(base: simple(:base),
45
- expr: simple(:expr)) do
46
- [base, expr]
67
+ rule(text: sequence(:text)) do
68
+ Utility.get_class("text").new(text.join)
47
69
  end
48
70
 
49
- rule(fonts: simple(:font_style), intermediate_exp: simple(:int_exp)) do
50
- Plurimath::Math::Function::FontStyle.new(int_exp, font_style.to_s)
71
+ rule(power_value: simple(:power_value)) do
72
+ power_value
51
73
  end
52
74
 
53
- rule(fonts: simple(:font_style), text: simple(:text)) do
54
- parameter_one = Plurimath::Math::Function::Text.new(text.to_s)
55
- Plurimath::Math::Function::FontStyle.new(parameter_one, font_style.to_s)
75
+ rule(base_value: simple(:base_value)) do
76
+ base_value
56
77
  end
57
78
 
58
- rule(power_base: simple(:power_base), expr: sequence(:expr)) do
59
- expr.insert(0, power_base)
79
+ rule(base_value: sequence(:base_value)) do
80
+ Utility.filter_values(base_value)
60
81
  end
61
82
 
62
- rule(power_base: simple(:power_base), expr: simple(:expr)) do
63
- [power_base, expr]
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
64
101
  end
65
102
 
66
- rule(power_base: sequence(:power_base), expr: simple(:expr)) do
67
- power_base + [expr]
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
68
121
  end
69
122
 
70
- rule(power_base: sequence(:power_base), expr: sequence(:expr)) do
71
- power_base + expr
123
+ rule(sequence: simple(:sequence),
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
72
128
  end
73
129
 
74
- rule(sequence: simple(:sequence), expr: sequence(:expr)) do
75
- expr.insert(0, sequence)
130
+ rule(sequence: simple(:sequence),
131
+ left_right: sequence(:left_right)) do
132
+ left_right.insert(0, sequence)
133
+ left_right
76
134
  end
77
135
 
78
- rule(dividend: simple(:dividend), mod: simple(:mod),
79
- divisor: simple(:divisor)) do
80
- Plurimath::Math::Function::Mod.new(dividend, divisor)
136
+ rule(table_row: simple(:table_row),
137
+ expr: simple(:expr)) do
138
+ new_arr = [table_row]
139
+ new_arr << expr unless expr.to_s.strip.empty?
140
+ new_arr
141
+ end
142
+
143
+ rule(table_row: simple(:table_row),
144
+ expr: sequence(:expr)) do
145
+ expr.flatten.compact.insert(0, table_row)
146
+ end
147
+
148
+ rule(comma: simple(:comma),
149
+ expr: simple(:expr)) do
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))
81
158
  end
82
159
 
83
- rule(unary: simple(:unary), "^": simple(:exponent),
84
- number: simple(:number)) do
160
+ rule(rparen: simple(:rparen),
161
+ expr: simple(:expr)) do
85
162
  [
86
- unary,
87
- Plurimath::Math::Symbol.new(exponent.to_s),
88
- Plurimath::Math::Number.new(number.to_s),
163
+ Utility.symbol_object(rparen),
164
+ expr,
89
165
  ]
90
166
  end
91
167
 
92
- rule(unary: simple(:unary), "^": simple(:exponent),
93
- intermediate_exp: simple(:intermediate_exp)) do
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
94
228
  [
95
- unary,
96
- Plurimath::Math::Symbol.new(exponent.to_s),
97
- intermediate_exp,
229
+ Math::Function::Power.new(
230
+ Utility.symbol_object(rparen),
231
+ Utility.unfenced_value(power),
232
+ ),
233
+ Utility.symbol_object(comma),
98
234
  ]
99
235
  end
100
236
 
101
- rule(unary: simple(:unary), "^": simple(:exponent),
102
- text: simple(:text)) do
103
- [
104
- unary,
105
- Plurimath::Math::Symbol.new(exponent.to_s),
106
- text.is_a?(Slice) ? Transform.get_class("text").new(text.to_s) : text,
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),
107
248
  ]
249
+ new_arr << expr unless expr.to_s.strip.empty?
250
+ new_arr
108
251
  end
109
252
 
110
- rule(symbol: simple(:symbol), "^": simple(:exponent),
111
- number: simple(:number)) do
112
- [
113
- Plurimath::Math::Symbol.new(symbol.to_s),
114
- Plurimath::Math::Symbol.new(exponent.to_s),
115
- Plurimath::Math::Number.new(number.to_s),
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),
116
264
  ]
265
+ new_arr += expr
266
+ new_arr
117
267
  end
118
268
 
119
- rule(binary: simple(:binary),
120
- "^": simple(:under_score),
121
- intermediate_exp: simple(:int_exp)) do
269
+ rule(rparen: simple(:rparen),
270
+ base: simple(:base),
271
+ comma: simple(:comma)) do
122
272
  [
123
- binary,
124
- Plurimath::Math::Symbol.new("^"),
125
- int_exp,
273
+ Math::Function::Base.new(
274
+ Utility.symbol_object(rparen),
275
+ Utility.unfenced_value(base),
276
+ ),
277
+ Utility.symbol_object(comma),
126
278
  ]
127
279
  end
128
280
 
129
- rule(binary: simple(:function), "^": simple(:exponent),
130
- number: simple(:number)) do
281
+ rule(comma: simple(:comma),
282
+ left_right: simple(:left_right)) do
131
283
  [
132
- function,
133
- Plurimath::Math::Symbol.new("^"),
134
- Plurimath::Math::Number.new(number.to_s),
284
+ Utility.symbol_object(comma),
285
+ left_right,
135
286
  ]
136
287
  end
137
288
 
138
- Constants::UNARY_CLASSES.each do |unary_class|
139
- rule(unary_class => simple(:function),
140
- intermediate_exp: simple(:int_exp)) do
141
- Transform.get_class(function).new(int_exp)
142
- end
143
-
144
- rule(unary_class => simple(:function),
145
- _: simple(:under_score),
146
- intermediate_exp: simple(:int_exp)) do
289
+ rule(td: simple(:td)) do
290
+ Math::Function::Td.new(
147
291
  [
148
- Transform.get_class(function).new,
149
- Plurimath::Math::Symbol.new("_"),
150
- int_exp,
151
- ]
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
+
306
+ rule(open_tr: simple(:tr),
307
+ tds_list: sequence(:tds_list)) do
308
+ Math::Function::Tr.new(tds_list)
309
+ end
310
+
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
+ )
317
+ end
318
+
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),
329
+ )
330
+ end
331
+
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
152
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
153
388
 
154
- rule(unary_class => simple(:function),
155
- symbol: simple(:new_symbol)) do
156
- symbol = Plurimath::Math::Symbol.new(new_symbol.to_s)
157
- Transform.get_class(function).new(symbol)
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
158
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),
445
+ )
446
+ end
447
+
448
+ rule(power_base: simple(:power_base),
449
+ power: simple(:power),
450
+ expr: sequence(:expr)) do
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
159
467
 
160
- rule(unary_class => simple(:function),
161
- number: simple(:new_number)) do
162
- number = Plurimath::Math::Number.new(new_number.to_s)
163
- Transform.get_class(function).new(number)
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
+ )
478
+ end
479
+
480
+ rule(power_base: simple(:power_base),
481
+ base: simple(:base),
482
+ expr: simple(:expr)) do
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)
491
+ end
492
+
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
+ )
164
522
  end
523
+ end
165
524
 
166
- rule(unary_class => simple(:function),
167
- "^": simple(:base),
168
- intermediate_exp: simple(:unary)) do
169
- [
170
- Transform.get_class(unary_class).new,
171
- Plurimath::Math::Symbol.new("^"),
172
- unary,
173
- ]
525
+ rule(power_base: simple(:power_base),
526
+ base_value: simple(:base_value),
527
+ power_value: simple(:power_value),
528
+ expr: sequence(:expr)) do
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
+ )
537
+ end
538
+
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),
546
+ )
547
+ end
548
+
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
635
+ Math::Function::Power.new(
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),
654
+ )
655
+ end
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
174
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
+
700
+ rule(unary: simple(:unary),
701
+ power: simple(:power)) do
702
+ Math::Function::Power.new(
703
+ unary,
704
+ Utility.unfenced_value(power),
705
+ )
706
+ end
707
+
708
+ rule(unary: simple(:unary),
709
+ base: simple(:base)) do
710
+ Math::Function::Base.new(
711
+ unary,
712
+ Utility.unfenced_value(base),
713
+ )
714
+ end
175
715
 
176
- rule(unary_class => simple(:function),
177
- _: simple(:base),
178
- symbol: simple(:symbol)) do
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(
179
737
  [
180
- Transform.get_class(unary_class).new,
181
- Plurimath::Math::Symbol.new("_"),
182
- Plurimath::Math::Symbol.new(symbol.to_s),
183
- ]
184
- end
738
+ Utility.symbol_object(d),
739
+ Utility.symbol_object(x),
740
+ ],
741
+ )
742
+ end
743
+
744
+ rule(symbol: simple(:symbol),
745
+ power: simple(:power)) do
746
+ Math::Function::Power.new(
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,
776
+ )
777
+ [symbol, expr]
778
+ end
779
+
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),
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
918
+ Math::Function::Power.new(
919
+ Math::Number.new(number),
920
+ Utility.unfenced_value(power),
921
+ )
922
+ end
185
923
 
186
- rule(unary_class => simple(:function),
187
- _: simple(:under_score),
188
- base: simple(:base),
189
- "^": simple(:power),
190
- exponent: simple(:exponent)) do
924
+ rule(number: simple(:number),
925
+ power: sequence(:power)) do
926
+ Math::Function::Power.new(
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),
961
+ )
962
+ end
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
+
976
+ rule(left: simple(:left),
977
+ left_right_value: simple(:left_right),
978
+ right: simple(:right)) do
979
+ Math::Formula.new(
191
980
  [
192
- Transform.get_class(function).new,
193
- Plurimath::Math::Symbol.new("_"),
194
- base,
195
- Plurimath::Math::Symbol.new("^"),
196
- exponent,
197
- ]
198
- end
981
+ Math::Function::Left.new(left),
982
+ left_right,
983
+ Math::Function::Right.new(right),
984
+ ],
985
+ )
199
986
  end
200
987
 
201
- Constants::BINARY_CLASSES.each do |binary_class|
202
- rule(binary_class => simple(:function)) do
203
- Transform.get_class(function).new
204
- 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
205
996
 
206
- rule(binary_class => simple(:function), _: simple(:under_score),
207
- base: simple(:int_exp), "^": simple(:power),
208
- exponent: simple(:exponent)) do
209
- Transform.get_class(function).new(int_exp, exponent)
210
- 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
211
1006
 
212
- rule(binary_class => simple(:function), _: simple(:under_score),
213
- intermediate_exp: simple(:int_exp)) do
214
- Transform.get_class(function).new(int_exp, nil)
215
- 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
216
1016
 
217
- rule(binary_class => simple(:function), "^": simple(:exponent),
218
- intermediate_exp: simple(:int_exp)) do
219
- Transform.get_class(function).new(nil, int_exp)
220
- 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
221
1026
 
222
- rule(binary_class => simple(:function), _: simple(:under_score),
223
- number: simple(:number)) do
224
- Transform.get_class(function).new(
225
- Plurimath::Math::Number.new(number.to_s), nil
226
- )
227
- 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
+ )
1036
+ end
228
1037
 
229
- rule(binary_class => simple(:function), _: simple(:under_score),
230
- symbol: simple(:symbol)) do
231
- Transform.get_class(function).new(
232
- Plurimath::Math::Symbol.new(symbol.to_s), nil
233
- )
234
- 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
235
1047
 
236
- rule(binary_class => simple(:function), _: simple(:under_score),
237
- unary: simple(:unary)) do
238
- Transform.get_class(function).new(unary, nil)
239
- 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
240
1057
 
241
- rule(binary_class => simple(:function), "^": simple(:power),
242
- number: simple(:number)) do
243
- Transform.get_class(function).new(
244
- nil,
245
- Plurimath::Math::Number.new(number.to_s),
246
- )
247
- 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
1069
+
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
248
1082
 
249
- rule(binary_class => simple(:function), "^": simple(:power),
250
- symbol: simple(:symbol)) do
251
- Transform.get_class(function).new(
252
- nil,
253
- Plurimath::Math::Symbol.new(symbol.to_s),
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),
254
1099
  )
255
1100
  end
1101
+ end
256
1102
 
257
- rule(binary_class => simple(:function), base: simple(:base),
258
- exponent: simple(:exponent)) do
259
- Transform.get_class(function).new(base, exponent)
260
- 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
261
1113
 
262
- rule(binary: simple(:function), "^": simple(:exponent),
263
- text: simple(:text)) do
264
- [
265
- function,
266
- Plurimath::Math::Symbol.new("^"),
267
- text.is_a?(Slice) ? Transform.get_class("text").new(text.to_s) : text,
268
- ]
269
- end
1114
+ rule(lparen: simple(:lparen),
1115
+ text: simple(:text),
1116
+ rparen: simple(:rparen)) do
1117
+ Math::Function::Text.new(text)
1118
+ end
1119
+
1120
+ rule(lparen: simple(:lparen),
1121
+ rgb_color: sequence(:color),
1122
+ rparen: simple(:rparen)) do
1123
+ Math::Formula.new(color)
1124
+ end
270
1125
 
271
- rule(binary: simple(:function), "^": simple(:exponent),
272
- unary: simple(:unary)) do
1126
+ rule(lparen: simple(:lparen),
1127
+ rgb_color: simple(:color),
1128
+ rparen: simple(:rparen)) do
1129
+ Math::Formula.new(color)
1130
+ end
1131
+
1132
+ rule(color: sequence(:color),
1133
+ color_value: sequence(:color_value)) do
1134
+ Utility.symbol_object(color)
1135
+ end
1136
+
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
1146
+
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
1156
+
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(
273
1174
  [
274
- function,
275
- Plurimath::Math::Symbol.new("^"),
276
- unary,
277
- ]
278
- end
1175
+ table_row,
1176
+ ],
1177
+ Utility.symbol_object(table_left).value,
1178
+ Utility.symbol_object(table_right).value,
1179
+ )
1180
+ end
279
1181
 
280
- Constants::UNARY_CLASSES.each do |unary_class|
281
- rule(binary_class => simple(:function),
282
- _: simple(:base),
283
- unary_class => simple(:unary)) do
284
- unary_class = Transform.get_class(unary).new
285
- Transform.get_class(binary_class).new(unary_class, nil)
286
- end
287
-
288
- rule(binary_class => simple(:function),
289
- "^": simple(:base),
290
- unary_class => simple(:unary)) do
291
- unary_class = Transform.get_class(unary).new
292
- Transform.get_class(binary_class).new(nil, unary_class)
293
- end
294
- end
1182
+ rule(table_left: simple(:table_left),
1183
+ table_row: simple(:table_row),
1184
+ expr: simple(:expr),
1185
+ table_right: simple(:table_right)) do
1186
+ new_arr = [table_row]
1187
+ new_arr << expr unless expr.to_s.strip.empty?
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(
1201
+ [table_row, expr],
1202
+ Utility.symbol_object(table_left).value,
1203
+ Utility.symbol_object(table_right).value,
1204
+ )
1205
+ Math::Function::Norm.new(table)
295
1206
  end
296
1207
 
297
- rule(sequence: simple(:sequence), expr: simple(:exp)) { [sequence, exp] }
1208
+ rule(table_left: simple(:table_left),
1209
+ table_row: simple(:table_row),
1210
+ expr: sequence(:expr),
1211
+ table_right: simple(:table_right)) do
1212
+ Math::Function::Table.new(
1213
+ expr.flatten.compact.insert(0, table_row),
1214
+ Utility.symbol_object(table_left).value,
1215
+ Utility.symbol_object(table_right).value,
1216
+ )
1217
+ end
298
1218
 
299
- def self.get_class(text)
300
- Object.const_get("Plurimath::Math::Function::#{text.to_s.capitalize}")
1219
+ rule(left: simple(:left),
1220
+ table_row: simple(:table_row),
1221
+ expr: sequence(:expr),
1222
+ right: simple(:right)) do
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,
1227
+ )
301
1228
  end
302
1229
  end
303
1230
  end