plurimath 0.7.2 → 0.8.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (105) hide show
  1. checksums.yaml +4 -4
  2. data/.gitmodules +3 -0
  3. data/Latex-Supported-Data.adoc +1 -0
  4. data/lib/plurimath/asciimath/parse.rb +1 -1
  5. data/lib/plurimath/asciimath/transform.rb +2 -6
  6. data/lib/plurimath/latex/constants.rb +2 -0
  7. data/lib/plurimath/math/core.rb +38 -6
  8. data/lib/plurimath/math/formula.rb +60 -6
  9. data/lib/plurimath/math/function/abs.rb +4 -0
  10. data/lib/plurimath/math/function/arg.rb +22 -0
  11. data/lib/plurimath/math/function/bar.rb +4 -0
  12. data/lib/plurimath/math/function/base.rb +49 -0
  13. data/lib/plurimath/math/function/binary_function.rb +6 -0
  14. data/lib/plurimath/math/function/cancel.rb +5 -0
  15. data/lib/plurimath/math/function/ceil.rb +6 -0
  16. data/lib/plurimath/math/function/color.rb +20 -1
  17. data/lib/plurimath/math/function/ddot.rb +4 -0
  18. data/lib/plurimath/math/function/dot.rb +5 -0
  19. data/lib/plurimath/math/function/fenced.rb +98 -7
  20. data/lib/plurimath/math/function/floor.rb +6 -0
  21. data/lib/plurimath/math/function/font_style/monospace.rb +4 -0
  22. data/lib/plurimath/math/function/font_style.rb +31 -6
  23. data/lib/plurimath/math/function/frac.rb +69 -15
  24. data/lib/plurimath/math/function/hat.rb +4 -0
  25. data/lib/plurimath/math/function/inf.rb +30 -0
  26. data/lib/plurimath/math/function/int.rb +47 -1
  27. data/lib/plurimath/math/function/intent.rb +22 -0
  28. data/lib/plurimath/math/function/left.rb +4 -0
  29. data/lib/plurimath/math/function/lim.rb +6 -0
  30. data/lib/plurimath/math/function/limits.rb +28 -0
  31. data/lib/plurimath/math/function/linebreak.rb +5 -0
  32. data/lib/plurimath/math/function/log.rb +27 -20
  33. data/lib/plurimath/math/function/longdiv.rb +4 -0
  34. data/lib/plurimath/math/function/mbox.rb +4 -0
  35. data/lib/plurimath/math/function/menclose.rb +74 -5
  36. data/lib/plurimath/math/function/merror.rb +2 -0
  37. data/lib/plurimath/math/function/mglyph.rb +64 -0
  38. data/lib/plurimath/math/function/mlabeledtr.rb +29 -0
  39. data/lib/plurimath/math/function/mod.rb +4 -0
  40. data/lib/plurimath/math/function/mpadded.rb +84 -0
  41. data/lib/plurimath/math/function/ms.rb +33 -0
  42. data/lib/plurimath/math/function/msgroup.rb +4 -0
  43. data/lib/plurimath/math/function/msline.rb +2 -4
  44. data/lib/plurimath/math/function/multiscript.rb +70 -6
  45. data/lib/plurimath/math/function/nary.rb +69 -10
  46. data/lib/plurimath/math/function/none.rb +25 -0
  47. data/lib/plurimath/math/function/norm.rb +6 -0
  48. data/lib/plurimath/math/function/obrace.rb +4 -0
  49. data/lib/plurimath/math/function/oint.rb +25 -1
  50. data/lib/plurimath/math/function/over.rb +6 -0
  51. data/lib/plurimath/math/function/overset.rb +46 -1
  52. data/lib/plurimath/math/function/phantom.rb +18 -2
  53. data/lib/plurimath/math/function/power.rb +37 -0
  54. data/lib/plurimath/math/function/power_base.rb +45 -18
  55. data/lib/plurimath/math/function/prod.rb +46 -0
  56. data/lib/plurimath/math/function/right.rb +4 -0
  57. data/lib/plurimath/math/function/root.rb +9 -1
  58. data/lib/plurimath/math/function/rule.rb +4 -0
  59. data/lib/plurimath/math/function/sqrt.rb +7 -1
  60. data/lib/plurimath/math/function/stackrel.rb +6 -0
  61. data/lib/plurimath/math/function/substack.rb +4 -0
  62. data/lib/plurimath/math/function/sum.rb +45 -24
  63. data/lib/plurimath/math/function/table/bmatrix.rb +18 -5
  64. data/lib/plurimath/math/function/table/cases.rb +24 -0
  65. data/lib/plurimath/math/function/table/eqarray.rb +24 -0
  66. data/lib/plurimath/math/function/table/matrix.rb +23 -3
  67. data/lib/plurimath/math/function/table/pmatrix.rb +4 -0
  68. data/lib/plurimath/math/function/table/vmatrix.rb +10 -0
  69. data/lib/plurimath/math/function/table.rb +58 -7
  70. data/lib/plurimath/math/function/td.rb +9 -0
  71. data/lib/plurimath/math/function/ternary_function.rb +14 -1
  72. data/lib/plurimath/math/function/text.rb +6 -0
  73. data/lib/plurimath/math/function/tilde.rb +4 -0
  74. data/lib/plurimath/math/function/tr.rb +9 -0
  75. data/lib/plurimath/math/function/ubrace.rb +5 -0
  76. data/lib/plurimath/math/function/ul.rb +4 -0
  77. data/lib/plurimath/math/function/unary_function.rb +4 -0
  78. data/lib/plurimath/math/function/underover.rb +14 -0
  79. data/lib/plurimath/math/function/underset.rb +49 -1
  80. data/lib/plurimath/math/function/vec.rb +4 -0
  81. data/lib/plurimath/math/number.rb +33 -3
  82. data/lib/plurimath/math/symbol.rb +68 -3
  83. data/lib/plurimath/math.rb +3 -2
  84. data/lib/plurimath/mathml/constants.rb +16 -0
  85. data/lib/plurimath/mathml/parser.rb +42 -2
  86. data/lib/plurimath/mathml/transform.rb +80 -29
  87. data/lib/plurimath/omml/parser.rb +8 -0
  88. data/lib/plurimath/omml/transform.rb +29 -26
  89. data/lib/plurimath/unicode_math/constants.rb +1015 -0
  90. data/lib/plurimath/unicode_math/parse.rb +233 -0
  91. data/lib/plurimath/unicode_math/parser.rb +58 -0
  92. data/lib/plurimath/unicode_math/parsing_rules/absence_rules.rb +138 -0
  93. data/lib/plurimath/unicode_math/parsing_rules/common_rules.rb +114 -0
  94. data/lib/plurimath/unicode_math/parsing_rules/constants_rules.rb +102 -0
  95. data/lib/plurimath/unicode_math/parsing_rules/helper.rb +19 -0
  96. data/lib/plurimath/unicode_math/parsing_rules/masked.rb +62 -0
  97. data/lib/plurimath/unicode_math/parsing_rules/sub_sup.rb +254 -0
  98. data/lib/plurimath/unicode_math/transform.rb +3831 -0
  99. data/lib/plurimath/{unicode.rb → unicode_math.rb} +2 -2
  100. data/lib/plurimath/unitsml.rb +14 -1
  101. data/lib/plurimath/utility.rb +346 -11
  102. data/lib/plurimath/version.rb +1 -1
  103. data/lib/plurimath/xml_engine/oga.rb +5 -0
  104. data/lib/plurimath/xml_engine/ox.rb +5 -0
  105. metadata +23 -3
@@ -0,0 +1,3831 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Plurimath
4
+ class UnicodeMath
5
+ class Transform < Parslet::Transform
6
+
7
+ rule(td: simple(:td)) { Math::Function::Td.new([td]) }
8
+ rule(tr: simple(:tr)) { Math::Function::Tr.new([tr]) }
9
+
10
+ rule(sub: simple(:sub)) { sub }
11
+ rule(trs: simple(:trs)) { trs }
12
+ rule(exp: simple(:exp)) { exp }
13
+ rule(tr: sequence(:tr)) { Math::Function::Tr.new(tr) }
14
+ rule(td: sequence(:td)) { Math::Function::Td.new(td) }
15
+
16
+ rule(exp: sequence(:exp)) { exp }
17
+ rule(atom: simple(:atom)) { atom }
18
+ rule(rect: simple(:rect)) { rect }
19
+ rule(nary: simple(:nary)) { nary }
20
+ rule(char: simple(:char)) { char }
21
+ rule(expr: simple(:expr)) { expr }
22
+ rule(frac: simple(:frac)) { frac }
23
+ rule(root: simple(:root)) { root }
24
+ rule(text: simple(:text)) { Math::Function::Text.new(text) }
25
+
26
+ rule(sub_exp: simple(:exp)) { exp }
27
+ rule(sup_exp: simple(:exp)) { exp }
28
+ rule(int_exp: simple(:exp)) { exp }
29
+ rule(atom: sequence(:atom)) { atom }
30
+ rule(expr: sequence(:expr)) { expr }
31
+ rule(table: simple(:table)) { table }
32
+ rule(fonts: simple(:fonts)) { fonts }
33
+ rule(digit: simple(:digit)) { digit }
34
+ rule(color: simple(:color)) { color }
35
+ rule(ldots: simple(:ldots)) { ldots }
36
+
37
+ rule(sub_exp: sequence(:exp)) { exp }
38
+ rule(factor: simple(:factor)) { factor }
39
+ rule(hbrack: simple(:hbrack)) { hbrack }
40
+ rule(symbol: simple(:symbol)) { Math::Symbol.new(symbol) }
41
+ rule(number: simple(:number)) { Math::Number.new(number) }
42
+
43
+ rule(backcolor: simple(:color)) { color }
44
+ rule(sub_paren: simple(:paren)) { paren }
45
+ rule(factor: sequence(:factor)) { factor }
46
+ rule(operand: simple(:operand)) { operand }
47
+ rule(accents: subtree(:accent)) { Utility.unicode_accents(accent) }
48
+ rule(sup_alpha: simple(:alpha)) { Math::Symbol.new(Constants::SUP_ALPHABETS.key(alpha).to_s, mini_sup_sized: true) }
49
+
50
+ rule(sub_script: simple(:script)) { script }
51
+ rule(sup_script: simple(:script)) { script }
52
+ rule(pre_script: simple(:script)) { script }
53
+ rule(operand: sequence(:operand)) { operand }
54
+ rule(mini_sup: simple(:mini_sup)) { mini_sup }
55
+ rule(mini_sub: simple(:mini_sub)) { mini_sub }
56
+ rule(close_paren: simple(:paren)) { Math::Symbol.new(paren) }
57
+ rule(operator: simple(:operator)) { Math::Symbol.new(operator) }
58
+
59
+ rule(unary_sub_sup: simple(:unary)) { unary }
60
+ rule(sub_script: sequence(:script)) { script }
61
+ rule(sup_script: sequence(:script)) { script }
62
+ rule(mini_sub: sequence(:mini_sub)) { mini_sub }
63
+ rule(monospace: simple(:monospace)) { monospace }
64
+ rule(slashed_value: simple(:value)) { Utility.slashed_values(value) }
65
+
66
+ rule(intermediate_exp: simple(:expr)) { expr }
67
+ rule(decimal_number: simple(:number)) { number }
68
+ rule(accents_subsup: simple(:subsup)) { subsup }
69
+ rule(subsup_exp: simple(:subsup_exp)) { subsup_exp }
70
+ rule(expression: simple(:expression)) { expression }
71
+ rule(open_paren: simple(:open_paren)) { Math::Symbol.new(open_paren) }
72
+ rule(override_subsup: simple(:subsup)) { subsup }
73
+ rule(slashed_value: sequence(:values)) { Utility.sequence_slashed_values(values) }
74
+
75
+ rule(intermediate_exp: sequence(:expr)) { expr }
76
+ rule(diacritic_belows: simple(:belows)) { belows }
77
+ rule(unary_function: simple(:function)) { function }
78
+ rule(sup_recursion: simple(:recursion)) { recursion }
79
+ rule(nary_sub_sup: simple(:subsup_exp)) { subsup_exp }
80
+ rule(open_paren: sequence(:open_paren)) { open_paren }
81
+
82
+ rule(diacritics_accents: simple(:accent)) { accent }
83
+ rule(mini_sub_sup: simple(:mini_sub_sup)) { mini_sub_sup }
84
+ rule(unary_subsup: simple(:unary_subsup)) { unary_subsup }
85
+ rule(exclamation_symbol: simple(:symbol)) { Math::Symbol.new(symbol) }
86
+ rule(alphanumeric: simple(:alphanumeric)) { Math::Symbol.new(alphanumeric) }
87
+
88
+ rule(diacritic_overlays: simple(:overlays)) { overlays }
89
+ rule(mini_sub_sup: sequence(:mini_sub_sup)) { mini_sub_sup }
90
+ rule(unicode_fractions: simple(:fractions)) { Utility.unicode_fractions(fractions) }
91
+ rule(mini_intermediate_exp: simple(:mini_expr)) { mini_expr }
92
+
93
+ rule(combined_symbols: simple(:combined_symbols)) do
94
+ symbol = Constants::COMBINING_SYMBOLS[combined_symbols.to_sym] || combined_symbols
95
+ Math::Symbol.new(symbol)
96
+ end
97
+
98
+ rule(spaces: simple(:spaces)) do
99
+ space = Constants::SKIP_SYMBOLS[spaces.to_sym] || spaces
100
+ Math::Symbol.new(space, options: { space: true })
101
+ end
102
+
103
+ rule(binary_symbols: simple(:symbols)) do
104
+ symbol = (Constants::BINARY_SYMBOLS[symbols.to_sym] || symbols)
105
+ Math::Symbol.new(symbol)
106
+ end
107
+
108
+ rule(sup_operators: simple(:operator)) do
109
+ op = Constants::SUP_OPERATORS.key(operator).to_s
110
+ Math::Symbol.new(op, mini_sup_sized: true)
111
+ end
112
+
113
+ rule(prefixed_prime: simple(:prime)) do
114
+ Math::Symbol.new(Constants::PREFIXED_PRIMES[prime.to_sym])
115
+ end
116
+
117
+ rule(unary_functions: simple(:unary)) do
118
+ if Constants::UNDEF_UNARY_FUNCTIONS.include?(unary.to_s)
119
+ Math::Symbol.new(unary)
120
+ else
121
+ Utility.get_class(unary).new
122
+ end
123
+ end
124
+
125
+ rule(negated_operator: simple(:operator)) do
126
+ Math::Formula.new([
127
+ Math::Symbol.new(operator),
128
+ Math::Symbol.new("&#x338;"),
129
+ ])
130
+ end
131
+
132
+ rule(ordinary_symbols: simple(:ordinary)) do
133
+ Math::Symbol.new(
134
+ (Constants::ORDINARY_SYMBOLS[ordinary.to_sym] || ordinary),
135
+ )
136
+ end
137
+
138
+ rule(relational_symbols: simple(:symbol)) do
139
+ Math::Symbol.new(
140
+ (Constants::RELATIONAL_SYMBOLS[symbol.to_sym] || symbol),
141
+ )
142
+ end
143
+
144
+ rule(unicode_symbols: simple(:unicode_symbols)) do
145
+ Math::Symbol.new(unicode_symbols)
146
+ end
147
+
148
+ rule(monospace_value: simple(:monospace_value)) do
149
+ Math::Function::FontStyle::Monospace.new(
150
+ Math::Symbol.new(monospace_value),
151
+ )
152
+ end
153
+
154
+ rule(sup_digits: simple(:digits)) do
155
+ digit = Constants::SUP_DIGITS.key(digits).to_s
156
+ Math::Number.new(digit, mini_sup_sized: true)
157
+ end
158
+
159
+ rule(sub_digits: simple(:digits)) do
160
+ digit = Constants::SUB_DIGITS.key(digits).to_s
161
+ Math::Number.new(digit, mini_sub_sized: true)
162
+ end
163
+
164
+ rule(nary_class: simple(:nary_class)) do
165
+ nary_function = if Constants::NARY_CLASSES.key?(nary_class.to_sym)
166
+ nary_class
167
+ else
168
+ (Constants::NARY_CLASSES.invert[nary_class.to_s] || Constants::NARY_SYMBOLS[nary_class.to_sym] || nary_class)
169
+ end
170
+ Utility.get_class(nary_function).new
171
+ end
172
+
173
+ rule(ordinary_negated_operator: simple(:operator)) do
174
+ Math::Formula.new([
175
+ Math::Symbol.new(operator),
176
+ Math::Symbol.new("&#x338;"),
177
+ ])
178
+ end
179
+
180
+ rule(decimal: simple(:decimal),
181
+ whole: simple(:whole)) do
182
+ Math::Number.new("#{decimal}#{whole.value}")
183
+ end
184
+
185
+ rule(unicode_fractions: simple(:fractions),
186
+ expr: sequence(:expr)) do
187
+ [Utility.unicode_fractions(fractions)] + expr
188
+ end
189
+
190
+ rule(unicode_fractions: simple(:fractions),
191
+ expr: simple(:expr)) do
192
+ [Utility.unicode_fractions(fractions), expr]
193
+ end
194
+
195
+ rule(diacritics_accents: simple(:accents),
196
+ expr: sequence(:expr)) do
197
+ [accents] + expr
198
+ end
199
+
200
+ rule(unicoded_font_class: simple(:unicoded),
201
+ symbol: simple(:symbol)) do
202
+ unicode = Constants::UNICODED_FONTS.dig(
203
+ unicoded.to_sym,
204
+ symbol.to_sym,
205
+ )
206
+ Math::Symbol.new(unicode)
207
+ end
208
+
209
+ rule(font_class: simple(:fonts),
210
+ symbol: simple(:symbol)) do
211
+ Utility::FONT_STYLES[fonts.to_sym].new(
212
+ Math::Symbol.new(symbol),
213
+ )
214
+ end
215
+
216
+ rule(symbol: simple(:symbol),
217
+ naryand_recursion: sequence(:naryand_recursion)) do
218
+ [
219
+ Math::Symbol.new(symbol),
220
+ ] + naryand_recursion
221
+ end
222
+
223
+ rule(symbol: simple(:symbol),
224
+ naryand_recursion: simple(:naryand_recursion)) do
225
+ [Math::Symbol.new(symbol), naryand_recursion]
226
+ end
227
+
228
+ rule(symbol: simple(:symbol),
229
+ expr: simple(:expr)) do
230
+ [Math::Symbol.new(symbol), expr]
231
+ end
232
+
233
+ rule(binary_symbols: simple(:symbols),
234
+ expr: simple(:expr)) do
235
+ symbol = (Constants::BINARY_SYMBOLS[symbols.to_sym] || symbols)
236
+ [Math::Symbol.new(symbol), expr]
237
+ end
238
+
239
+ rule(binary_symbols: simple(:symbols),
240
+ exp: sequence(:exp)) do
241
+ symbol = (Constants::BINARY_SYMBOLS[symbols.to_sym] || symbols)
242
+ [Math::Symbol.new(symbol)] + exp
243
+ end
244
+
245
+ rule(binary_symbols: simple(:symbols),
246
+ expr: sequence(:expr)) do
247
+ symbol = (Constants::BINARY_SYMBOLS[symbols.to_sym] || symbols)
248
+ [Math::Symbol.new(symbol)] + expr
249
+ end
250
+
251
+ rule(binary_symbols: simple(:symbols),
252
+ naryand_recursion: simple(:naryand_recursion)) do
253
+ symbol = (Constants::BINARY_SYMBOLS[symbols.to_sym] || symbols)
254
+ [Math::Symbol.new(symbol), naryand_recursion]
255
+ end
256
+
257
+ rule(binary_symbols: simple(:symbols),
258
+ recursive_denominator: simple(:recursive_denominator)) do
259
+ symbol = (Constants::BINARY_SYMBOLS[symbols.to_sym] || symbols)
260
+ [Math::Symbol.new(symbol), recursive_denominator]
261
+ end
262
+
263
+ rule(binary_symbols: simple(:symbols),
264
+ recursive_denominator: sequence(:recursive_denominator)) do
265
+ symbol = (Constants::BINARY_SYMBOLS[symbols.to_sym] || symbols)
266
+ [Math::Symbol.new(symbol)] + recursive_denominator
267
+ end
268
+
269
+ rule(binary_symbols: simple(:symbols),
270
+ recursive_numerator: simple(:recursive_numerator)) do
271
+ symbol = (Constants::BINARY_SYMBOLS[symbols.to_sym] || symbols)
272
+ [Math::Symbol.new(symbol), recursive_numerator]
273
+ end
274
+
275
+ rule(symbol: simple(:symbol),
276
+ expr: sequence(:expr)) do
277
+ [
278
+ Math::Symbol.new(symbol),
279
+ ] + expr
280
+ end
281
+
282
+ rule(negated_operator: simple(:operator),
283
+ expr: simple(:expr)) do
284
+ [
285
+ Math::Formula.new([
286
+ Math::Symbol.new(operator),
287
+ Math::Symbol.new("&#x338;"),
288
+ ]),
289
+ expr,
290
+ ]
291
+ end
292
+
293
+ rule(negated_operator: simple(:operator),
294
+ expr: sequence(:expr)) do
295
+ [
296
+ Math::Formula.new([
297
+ Math::Symbol.new(operator),
298
+ Math::Symbol.new("&#x338;"),
299
+ ]),
300
+ ] + expr
301
+ end
302
+
303
+ rule(ordinary_negated_operator: simple(:operator),
304
+ expr: simple(:expr)) do
305
+ [
306
+ Math::Formula.new([
307
+ Math::Symbol.new(operator),
308
+ Math::Symbol.new("&#x338;"),
309
+ ]),
310
+ expr,
311
+ ]
312
+ end
313
+
314
+ rule(accents: subtree(:accent),
315
+ expr: sequence(:expr)) do
316
+ [Utility.unicode_accents(accent)] + expr
317
+ end
318
+
319
+ rule(accents: subtree(:accent),
320
+ exp: sequence(:exp)) do
321
+ [Utility.unicode_accents(accent)] + exp
322
+ end
323
+
324
+ rule(text: simple(:text),
325
+ expr: sequence(:expr)) do
326
+ [Math::Function::Text.new(text)] + expr
327
+ end
328
+
329
+ rule(text: simple(:text),
330
+ operand: simple(:operand)) do
331
+ [Math::Function::Text.new(text), operand]
332
+ end
333
+
334
+ rule(text: simple(:text),
335
+ operand: sequence(:operand)) do
336
+ [Math::Function::Text.new(text)] + operand
337
+ end
338
+
339
+ rule(text: simple(:text),
340
+ expr: simple(:expr)) do
341
+ [Math::Function::Text.new(text), expr]
342
+ end
343
+
344
+ rule(subsup_exp: simple(:subsup),
345
+ expr: sequence(:expr)) do
346
+ [subsup] + expr
347
+ end
348
+
349
+ rule(subsup_exp: simple(:subsup),
350
+ exp: sequence(:exp)) do
351
+ [subsup] + exp
352
+ end
353
+
354
+ rule(override_subsup: simple(:subsup),
355
+ expr: sequence(:expr)) do
356
+ [subsup] + expr
357
+ end
358
+
359
+ rule(unary_subsup: simple(:subsup),
360
+ expr: sequence(:expr)) do
361
+ [subsup] + expr
362
+ end
363
+
364
+ rule(unary_subsup: simple(:subsup),
365
+ expr: simple(:expr)) do
366
+ [subsup, expr]
367
+ end
368
+
369
+ rule(char: simple(:char),
370
+ alphanumeric: simple(:alphanumeric)) do
371
+ [char, Math::Symbol.new(alphanumeric)]
372
+ end
373
+
374
+ rule(char: simple(:char),
375
+ number: simple(:num)) do
376
+ [char, Math::Number.new(num)]
377
+ end
378
+
379
+ rule(char: simple(:char),
380
+ diacritics: simple(:diacritics)) do
381
+ [char, diacritics]
382
+ end
383
+
384
+ rule(char: simple(:char),
385
+ diacritics: sequence(:diacritics)) do
386
+ [char] + diacritics
387
+ end
388
+
389
+ rule(fonts: simple(:fonts),
390
+ expr: sequence(:expr)) do
391
+ [fonts] + expr
392
+ end
393
+
394
+ rule(phantom: simple(:phantom),
395
+ expr: sequence(:expr)) do
396
+ [phantom] + expr
397
+ end
398
+
399
+ rule(subsup_exp: simple(:subsup),
400
+ expr: simple(:expr)) do
401
+ [subsup, expr]
402
+ end
403
+
404
+ rule(subsup_exp: simple(:subsup),
405
+ exp: simple(:exp)) do
406
+ [subsup, exp]
407
+ end
408
+
409
+ rule(int_exp: simple(:int),
410
+ expr: simple(:expr)) do
411
+ [int, expr]
412
+ end
413
+
414
+ rule(operator: simple(:operator),
415
+ expr: simple(:expr)) do
416
+ [Math::Symbol.new(operator), expr]
417
+ end
418
+
419
+ rule(mid_symbol: simple(:mid_symbol),
420
+ expr: simple(:expr)) do
421
+ [Math::Symbol.new(mid_symbol), expr]
422
+ end
423
+
424
+ rule(mid_symbol: simple(:mid_symbol),
425
+ expr: sequence(:expr)) do
426
+ [Math::Symbol.new(mid_symbol)] + expr
427
+ end
428
+
429
+ rule(operator: simple(:operator),
430
+ exp: simple(:exp)) do
431
+ [Math::Symbol.new(operator), exp]
432
+ end
433
+
434
+ rule(unicode_symbols: simple(:unicode_symbols),
435
+ exp: simple(:exp)) do
436
+ [Math::Symbol.new(unicode_symbols), exp]
437
+ end
438
+
439
+ rule(unicode_symbols: simple(:unicode_symbols),
440
+ expr: simple(:expr)) do
441
+ [Math::Symbol.new(unicode_symbols), expr]
442
+ end
443
+
444
+ rule(unicode_symbols: simple(:unicode_symbols),
445
+ exp: sequence(:exp)) do
446
+ [Math::Symbol.new(unicode_symbols)] + exp
447
+ end
448
+
449
+ rule(unicode_symbols: simple(:unicode_symbols),
450
+ expr: sequence(:expr)) do
451
+ [Math::Symbol.new(unicode_symbols)] + expr
452
+ end
453
+
454
+ rule(atom: simple(:atom),
455
+ atoms: simple(:atoms)) do
456
+ [atom, atoms]
457
+ end
458
+
459
+ rule(atom: sequence(:atom),
460
+ atoms: simple(:atoms)) do
461
+ atom + [atoms]
462
+ end
463
+
464
+ rule(atom: simple(:atom),
465
+ atoms: sequence(:atoms)) do
466
+ [atom] + atoms
467
+ end
468
+
469
+ rule(operator: simple(:operator),
470
+ expr: sequence(:expr)) do
471
+ [Math::Symbol.new(operator)] + expr
472
+ end
473
+
474
+ rule(operator: simple(:operator),
475
+ exp: sequence(:exp)) do
476
+ [Math::Symbol.new(operator)] + exp
477
+ end
478
+
479
+ rule(operator: simple(:operator),
480
+ sup_script: simple(:sup_script)) do
481
+ [Math::Symbol.new(operator), sup_script]
482
+ end
483
+
484
+ rule(operator: simple(:operator),
485
+ sup_script: sequence(:sup_script)) do
486
+ [Math::Symbol.new(operator)] + sup_script
487
+ end
488
+
489
+ rule(combined_symbols: simple(:combined_symbols),
490
+ sup_script: simple(:sup_script)) do
491
+ symbol = Constants::COMBINING_SYMBOLS[combined_symbols.to_sym]
492
+ [Math::Symbol.new(symbol), sup_script]
493
+ end
494
+
495
+ rule(operator: simple(:operator),
496
+ sub_script: simple(:sub_script)) do
497
+ [Math::Symbol.new(operator), sub_script]
498
+ end
499
+
500
+ rule(size_overrides: simple(:size_overrides),
501
+ sub_script: simple(:sub_script)) do
502
+ [size_overrides, sub_script]
503
+ end
504
+
505
+ rule(pre_script: simple(:pre_script),
506
+ expr: sequence(:expr)) do
507
+ [pre_script] + expr
508
+ end
509
+
510
+ rule(pre_script: simple(:pre_script),
511
+ expr: simple(:expr)) do
512
+ [pre_script, expr]
513
+ end
514
+
515
+ rule(sub_digits: simple(:sub_digits),
516
+ sub_recursion_expr: sequence(:sub_recursion_expr)) do
517
+ digit = Constants::SUB_DIGITS.key(sub_digits).to_s
518
+ [
519
+ Math::Number.new(digit, mini_sub_sized: true)
520
+ ] + sub_recursion_expr
521
+ end
522
+
523
+ rule(sub_digits: simple(:sub_digits),
524
+ sub_recursion_expr: simple(:sub_recursion_expr)) do
525
+ digit = Constants::SUB_DIGITS.key(sub_digits).to_s
526
+ [Math::Number.new(digit, mini_sub_sized: true), sub_recursion_expr]
527
+ end
528
+
529
+ rule(sup_alpha: simple(:sup_alpha),
530
+ sup_recursion_expr: sequence(:sup_recursion_expr)) do
531
+ alpha = Constants::SUP_ALPHABETS.key(sup_alpha)
532
+ [
533
+ Math::Symbol.new(alpha.to_s, mini_sup_sized: true)
534
+ ] + sup_recursion_expr
535
+ end
536
+
537
+ rule(sup_alpha: simple(:sup_alpha),
538
+ sup_recursion_expr: simple(:sup_recursion_expr)) do
539
+ alpha = Constants::SUP_ALPHABETS.key(sup_alpha)
540
+ [
541
+ Math::Symbol.new(alpha.to_s, mini_sup_sized: true),
542
+ sup_recursion_expr,
543
+ ]
544
+ end
545
+
546
+ rule(sup_digits: simple(:digits),
547
+ sup_recursion_expr: sequence(:sup_recursion_expr)) do
548
+ digit = Constants::SUP_DIGITS.key(digits).to_s
549
+ [
550
+ Math::Number.new(digit, mini_sup_sized: true)
551
+ ] + sup_recursion_expr
552
+ end
553
+
554
+ rule(sup_digits: simple(:digits),
555
+ sup_recursion_expr: simple(:sup)) do
556
+ digit = Constants::SUP_DIGITS.key(digits).to_s
557
+ [Math::Number.new(digit, mini_sup_sized: true), sup]
558
+ end
559
+
560
+ rule(labeled_tr_value: sequence(:value),
561
+ labeled_tr_id: simple(:id)) do
562
+ Math::Function::Mlabeledtr.new(
563
+ Utility.filter_values(value),
564
+ Math::Function::Text.new(id),
565
+ )
566
+ end
567
+
568
+ rule(labeled_tr_value: simple(:value),
569
+ labeled_tr_id: simple(:id)) do
570
+ Math::Function::Mlabeledtr.new(
571
+ value,
572
+ Math::Function::Text.new(id),
573
+ )
574
+ end
575
+
576
+ rule(operator: simple(:operator),
577
+ sup_recursion: simple(:sup_recursion)) do
578
+ Utility.recursive_sup(
579
+ Math::Symbol.new(operator),
580
+ Utility.unfenced_value(sup_recursion, paren_specific: true),
581
+ )
582
+ end
583
+
584
+ rule(operator: simple(:operator),
585
+ sub_recursion: simple(:sub_recursion)) do
586
+ Utility.recursive_sub(
587
+ Math::Symbol.new(operator),
588
+ sub_recursion,
589
+ )
590
+ end
591
+
592
+ rule(operator: simple(:operator),
593
+ sub_recursion: sequence(:sub_recursion)) do
594
+ [Math::Symbol.new(operator)] + sub_recursion
595
+ end
596
+
597
+ rule(sub_script: simple(:sub_script),
598
+ sub_recursion: simple(:sub_recursion)) do
599
+ Utility.recursive_sub(sub_script, sub_recursion)
600
+ end
601
+
602
+ rule(sub_operators: simple(:operator),
603
+ sub_recursions: simple(:recursions)) do
604
+ op = Constants::SUB_OPERATORS.key(operator).to_s
605
+ [Math::Symbol.new(op, mini_sub_sized: true), recursions]
606
+ end
607
+
608
+ rule(sub_operators: simple(:operator),
609
+ sub_recursions: sequence(:recursions)) do
610
+ op = Constants::SUB_OPERATORS.key(operator).to_s
611
+ [Math::Symbol.new(op, mini_sub_sized: true)] + recursions
612
+ end
613
+
614
+ rule(sup_operators: simple(:operator),
615
+ sup_recursions: simple(:recursions)) do
616
+ op = Constants::SUP_OPERATORS.key(operator).to_s
617
+ [Math::Symbol.new(op, mini_sup_sized: true), recursions]
618
+ end
619
+
620
+ rule(digit: simple(:digit),
621
+ recursive_denominator: simple(:recursive_denominator)) do
622
+ [
623
+ digit,
624
+ recursive_denominator,
625
+ ]
626
+ end
627
+
628
+ rule(relational_symbols: simple(:symbols),
629
+ recursive_denominator: simple(:recursive_denominator)) do
630
+ symbol = Constants::RELATIONAL_SYMBOLS[symbols.to_sym] || symbols
631
+ [
632
+ Math::Symbol.new(symbol.to_s),
633
+ recursive_denominator,
634
+ ]
635
+ end
636
+
637
+ rule(atom: simple(:atom),
638
+ recursive_denominator: simple(:recursive_denominator)) do
639
+ [atom, recursive_denominator]
640
+ end
641
+
642
+ rule(atom: simple(:atom),
643
+ recursive_denominator: sequence(:recursive_denominator)) do
644
+ [atom] + recursive_denominator
645
+ end
646
+
647
+ rule(atom: sequence(:atom),
648
+ recursive_denominator: simple(:recursive_denominator)) do
649
+ atom + [recursive_denominator]
650
+ end
651
+
652
+ rule(atom: sequence(:atom),
653
+ recursive_numerator: simple(:recursive_numerator)) do
654
+ atom + [recursive_numerator]
655
+ end
656
+
657
+ rule(atom: sequence(:atom),
658
+ recursive_denominator: sequence(:recursive_denominator)) do
659
+ atom + recursive_denominator
660
+ end
661
+
662
+ rule(accents_subsup: simple(:accents_subsup),
663
+ expr: simple(:expr)) do
664
+ [accents_subsup, expr]
665
+ end
666
+
667
+ rule(accents_subsup: simple(:accents_subsup),
668
+ expr: sequence(:expr)) do
669
+ [accents_subsup] + expr
670
+ end
671
+
672
+ rule(accents_subsup: simple(:accents_subsup),
673
+ exp: simple(:exp)) do
674
+ [accents_subsup, exp]
675
+ end
676
+
677
+ rule(accents_subsup: simple(:accents_subsup),
678
+ naryand_recursion: simple(:naryand_recursion)) do
679
+ [accents_subsup, naryand_recursion]
680
+ end
681
+
682
+ rule(sup_exp: simple(:sup_exp),
683
+ naryand_recursion: sequence(:naryand_recursion)) do
684
+ [sup_exp] + naryand_recursion
685
+ end
686
+
687
+ rule(nary: simple(:nary),
688
+ naryand_recursion: simple(:naryand_recursion)) do
689
+ [nary, naryand_recursion]
690
+ end
691
+
692
+ rule(factor: simple(:factor),
693
+ operand: simple(:operand)) do
694
+ [factor, operand]
695
+ end
696
+
697
+ rule(factor: simple(:factor),
698
+ unary_subsup: simple(:subsup)) do
699
+ [factor, subsup]
700
+ end
701
+
702
+ rule(factor: simple(:factor),
703
+ operand: sequence(:operand)) do
704
+ [factor] + operand
705
+ end
706
+
707
+ rule(factor: simple(:factor),
708
+ unary_subsup: sequence(:subsup)) do
709
+ [factor] + subsup
710
+ end
711
+
712
+ rule(sup_exp: simple(:sup_exp),
713
+ expr: simple(:expr)) do
714
+ [sup_exp, expr]
715
+ end
716
+
717
+ rule(sup_exp: simple(:sup_exp),
718
+ exp: simple(:exp)) do
719
+ [sup_exp, exp]
720
+ end
721
+
722
+ rule(sub_exp: simple(:sub_exp),
723
+ expr: sequence(:expr)) do
724
+ [sub_exp] + expr
725
+ end
726
+
727
+ rule(sub_exp: sequence(:sub_exp),
728
+ expr: sequence(:expr)) do
729
+ sub_exp + expr
730
+ end
731
+
732
+ rule(sub_exp: simple(:sub_exp),
733
+ exp: sequence(:exp)) do
734
+ [sub_exp] + exp
735
+ end
736
+
737
+ rule(sub_exp: simple(:sub_exp),
738
+ expr: simple(:expr)) do
739
+ [sub_exp, expr]
740
+ end
741
+
742
+ rule(sub_exp: simple(:sub_exp),
743
+ exp: simple(:exp)) do
744
+ [sub_exp, exp]
745
+ end
746
+
747
+ rule(sub_exp: simple(:sub_exp),
748
+ naryand_recursion: sequence(:naryand)) do
749
+ [sub_exp] + naryand
750
+ end
751
+
752
+ rule(exp: simple(:exp),
753
+ expr: simple(:expr)) do
754
+ [exp, expr]
755
+ end
756
+
757
+ rule(exp: simple(:exp),
758
+ expr: sequence(:expr)) do
759
+ [exp] + expr
760
+ end
761
+
762
+ rule(exp: sequence(:exp),
763
+ expr: sequence(:expr)) do
764
+ exp + expr
765
+ end
766
+
767
+ rule(sup_exp: simple(:sup_exp),
768
+ expr: sequence(:expr)) do
769
+ [sup_exp] + expr
770
+ end
771
+
772
+ rule(sup_exp: sequence(:sup_exp),
773
+ expr: sequence(:expr)) do
774
+ sup_exp + expr
775
+ end
776
+
777
+ rule(factor: simple(:factor),
778
+ expr: simple(:expr)) do
779
+ [factor, expr]
780
+ end
781
+
782
+ rule(operand: simple(:operand),
783
+ expr: simple(:expr)) do
784
+ [operand, expr]
785
+ end
786
+
787
+ rule(factor: simple(:factor),
788
+ exp: sequence(:exp)) do
789
+ [factor] + exp
790
+ end
791
+
792
+ rule(operand: simple(:operand),
793
+ expr: sequence(:expr)) do
794
+ [operand] + expr
795
+ end
796
+
797
+ rule(factor: sequence(:factor),
798
+ expr: sequence(:expr)) do
799
+ factor + expr
800
+ end
801
+
802
+ rule(factor: sequence(:factor),
803
+ exp: sequence(:exp)) do
804
+ factor + exp
805
+ end
806
+
807
+ rule(factor: simple(:factor),
808
+ expr: sequence(:expr)) do
809
+ [factor] + expr
810
+ end
811
+
812
+ rule(factor: simple(:factor),
813
+ exp: sequence(:exp)) do
814
+ [factor] + exp
815
+ end
816
+
817
+ rule(factor: simple(:factor),
818
+ exp: simple(:exp)) do
819
+ [factor, exp]
820
+ end
821
+
822
+ rule(monospace: simple(:monospace),
823
+ exp: simple(:exp)) do
824
+ [monospace, exp]
825
+ end
826
+
827
+ rule(monospace: simple(:monospace),
828
+ expr: simple(:expr)) do
829
+ [monospace, expr]
830
+ end
831
+
832
+ rule(monospace: simple(:monospace),
833
+ exp: sequence(:exp)) do
834
+ [monospace] + exp
835
+ end
836
+
837
+ rule(monospace: simple(:monospace),
838
+ expr: sequence(:expr)) do
839
+ [monospace] + expr
840
+ end
841
+
842
+ rule(factor: sequence(:factor),
843
+ expr: simple(:expr)) do
844
+ factor + [expr]
845
+ end
846
+
847
+ rule(factor: sequence(:factor),
848
+ operand: simple(:operand)) do
849
+ factor + [operand]
850
+ end
851
+
852
+ rule(mini_sub: simple(:mini_sub),
853
+ expr: simple(:expr)) do
854
+ [mini_sub, expr]
855
+ end
856
+
857
+ rule(mini_sub: simple(:mini_sub),
858
+ expr: sequence(:expr)) do
859
+ [mini_sub] + expr
860
+ end
861
+
862
+ rule(sub_script: simple(:sub_script),
863
+ mini_sub: simple(:mini_sub)) do
864
+ [sub_script, mini_sub]
865
+ end
866
+
867
+ rule(expression: simple(:expression),
868
+ expr: sequence(:expr)) do
869
+ [expression] + expr
870
+ end
871
+
872
+ rule(expression: simple(:expression),
873
+ expr: simple(:expr)) do
874
+ [expression, expr]
875
+ end
876
+
877
+ rule(unary_function: simple(:unary_function),
878
+ expr: simple(:expr)) do
879
+ [unary_function, expr]
880
+ end
881
+
882
+ rule(unary_function: simple(:unary_function),
883
+ expr: sequence(:expr)) do
884
+ [unary_function] + expr
885
+ end
886
+
887
+ rule(rect: simple(:rect),
888
+ expr: sequence(:expr)) do
889
+ [rect] + expr
890
+ end
891
+
892
+ rule(table: simple(:table),
893
+ expr: sequence(:expr)) do
894
+ [table] + expr
895
+ end
896
+
897
+ rule(table: simple(:table),
898
+ expr: simple(:expr)) do
899
+ [table, expr]
900
+ end
901
+
902
+ rule(intent: simple(:intent),
903
+ intent_expr: simple(:expr)) do
904
+ intent_string = expr.parameter_two.shift
905
+ Math::Function::Intent.new(
906
+ Utility.filter_values(expr.parameter_two),
907
+ intent_string,
908
+ )
909
+ end
910
+
911
+ rule(over: simple(:over),
912
+ sup_script: simple(:sup_script)) do
913
+ Math::Function::Overset.new(
914
+ Utility.unfenced_value(sup_script, paren_specific: true),
915
+ nil,
916
+ )
917
+ end
918
+
919
+ rule(under: simple(:under),
920
+ sub_script: simple(:sub_script)) do
921
+ Math::Function::Underset.new(
922
+ Utility.unfenced_value(sub_script, paren_specific: true),
923
+ nil,
924
+ )
925
+ end
926
+
927
+ rule(sup_script: simple(:sup_script),
928
+ sup_recursion: simple(:sup_recursion)) do
929
+ Utility.recursive_sup(
930
+ sup_script,
931
+ Utility.unfenced_value(sup_recursion, paren_specific: true),
932
+ )
933
+ end
934
+
935
+ rule(sup_script: simple(:sup_script),
936
+ mini_sup: simple(:mini_sup)) do
937
+ [sup_script, mini_sup]
938
+ end
939
+
940
+ rule(sup_script: simple(:sup_script),
941
+ mini_sub: simple(:mini_sub)) do
942
+ [sup_script, mini_sub]
943
+ end
944
+
945
+ rule(sub_script: simple(:sub_script),
946
+ recursion: simple(:recursion)) do
947
+ Utility.recursive_sub(
948
+ sub_script,
949
+ Utility.unfenced_value(recursion, paren_specific: true),
950
+ )
951
+ end
952
+
953
+ rule(sup_script: sequence(:sup_script),
954
+ sup_recursion: simple(:sup_recursion)) do
955
+ Utility.recursive_sup(
956
+ Math::Formula.new(sup_script),
957
+ Utility.unfenced_value(sup_recursion, paren_specific: true),
958
+ )
959
+ end
960
+
961
+ rule(base: simple(:base),
962
+ sub: simple(:sub)) do
963
+ if (Constants::BINARY_FUNCTIONS.include?(base.class_name) && !base.parameter_one)
964
+ if sub.class_name == "underset"
965
+ base.parameter_one = sub.parameter_one
966
+ else
967
+ base.parameter_one = Utility.unfenced_value(sub, paren_specific: true)
968
+ end
969
+ base
970
+ elsif sub.class_name == "underset"
971
+ sub.parameter_two = base
972
+ sub
973
+ elsif base.is_a?(Math::Function::Power) && Utility.base_is_prime?(base)
974
+ Math::Function::PowerBase.new(
975
+ base.parameter_one,
976
+ sub,
977
+ base.parameter_two,
978
+ )
979
+
980
+ else
981
+ if (base.is_a?(Math::Function::Underset) && Constants::HORIZONTAL_BRACKETS.key(base.parameter_one.value.to_s)) ||
982
+ ("ubrace" == base.class_name && !base.parameter_one.is_a?(Math::Formula))
983
+ Math::Function::Underset.new(
984
+ Utility.unfenced_value(sub, paren_specific: true),
985
+ base,
986
+ )
987
+ else
988
+ Math::Function::Base.new(
989
+ base,
990
+ Utility.unfenced_value(sub, paren_specific: true),
991
+ )
992
+ end
993
+ end
994
+ end
995
+
996
+ rule(base: sequence(:base),
997
+ sub: simple(:sub)) do
998
+ new_base = base.pop
999
+ object = if sub.class_name == "underset"
1000
+ sub.parameter_two = new_base
1001
+ sub
1002
+ else
1003
+ Math::Function::Base.new(
1004
+ new_base,
1005
+ Utility.unfenced_value(sub, paren_specific: true),
1006
+ )
1007
+ end
1008
+ base << object
1009
+ end
1010
+
1011
+ rule(base: sequence(:base),
1012
+ sub: sequence(:sub)) do
1013
+ object = Math::Function::Base.new(
1014
+ base.pop,
1015
+ Utility.unfenced_value(sub, paren_specific: true),
1016
+ )
1017
+ base << object
1018
+ end
1019
+
1020
+ rule(base: simple(:base),
1021
+ sub: sequence(:sub)) do
1022
+ if (Constants::BINARY_FUNCTIONS.include?(base.class_name) && !base.parameter_one)
1023
+ base.parameter_one = sub_value
1024
+ base
1025
+ elsif base.is_a?(Math::Function::Power) && Utility.base_is_prime?(base)
1026
+ Math::Function::PowerBase.new(
1027
+ base.parameter_one,
1028
+ sub,
1029
+ base.parameter_two,
1030
+ )
1031
+ else
1032
+ Math::Function::Base.new(
1033
+ base,
1034
+ Utility.unfenced_value(sub, paren_specific: true),
1035
+ )
1036
+ end
1037
+ end
1038
+
1039
+ rule(root_symbol: simple(:root_symbol),
1040
+ first_value: simple(:first_value)) do
1041
+ if ["&#x221b;", "\\cbrt"].include?(root_symbol)
1042
+ Math::Function::Root.new(
1043
+ Math::Number.new("3"),
1044
+ Utility.unfenced_value(first_value, paren_specific: true),
1045
+ )
1046
+ elsif ["&#x221c;", "\\qdrt"].include?(root_symbol)
1047
+ Math::Function::Root.new(
1048
+ Math::Number.new("4"),
1049
+ Utility.unfenced_value(first_value, paren_specific: true),
1050
+ )
1051
+ else
1052
+ Math::Function::Sqrt.new(
1053
+ Utility.unfenced_value(first_value, paren_specific: true),
1054
+ )
1055
+ end
1056
+ end
1057
+
1058
+ rule(base: simple(:base),
1059
+ sup: simple(:sup)) do
1060
+ if "overset" == sup.class_name && !sup.parameter_two
1061
+ sup.parameter_two = Utility.unfenced_value(base, paren_specific: true)
1062
+ sup
1063
+ elsif (Constants::BINARY_FUNCTIONS.include?(base.class_name) && !base.parameter_one)
1064
+ base.parameter_two = Utility.unfenced_value(sup, paren_specific: true)
1065
+ base
1066
+ else
1067
+ if base.is_a?(Math::Function::Overset) && Constants::HORIZONTAL_BRACKETS.key(base.parameter_one.value.to_s) || "obrace" == base.class_name
1068
+ Math::Function::Overset.new(
1069
+ Utility.unfenced_value(sup, paren_specific: true),
1070
+ base,
1071
+ )
1072
+ else
1073
+ Math::Function::Power.new(
1074
+ base,
1075
+ Utility.unfenced_value(sup, paren_specific: true),
1076
+ )
1077
+ end
1078
+ end
1079
+ end
1080
+
1081
+ rule(base: sequence(:base),
1082
+ sup: simple(:sup)) do
1083
+ power = Math::Function::Power.new(
1084
+ base.pop,
1085
+ Utility.unfenced_value(sup, paren_specific: true)
1086
+ )
1087
+ Math::Formula.new(base << power)
1088
+ end
1089
+
1090
+ rule(base: simple(:base),
1091
+ sup: sequence(:sup)) do
1092
+ if (base.class_name == "base" && Utility.base_is_sub_or_sup?(base.parameter_two))
1093
+ Math::Function::PowerBase.new(
1094
+ base.parameter_one,
1095
+ base.parameter_two,
1096
+ Utility.unfenced_value(sup, paren_specific: true)
1097
+ )
1098
+ else
1099
+ Math::Function::Power.new(
1100
+ base,
1101
+ Utility.unfenced_value(sup, paren_specific: true),
1102
+ )
1103
+ end
1104
+ end
1105
+
1106
+ rule(base: sequence(:base),
1107
+ sup: sequence(:sup)) do
1108
+ power = Math::Function::Power.new(
1109
+ base.pop,
1110
+ Utility.unfenced_value(sup, paren_specific: true),
1111
+ )
1112
+ base << power
1113
+ end
1114
+
1115
+ rule(unary_sub_sup: simple(:sub_sup),
1116
+ first_value: simple(:first_value)) do
1117
+ if sub_sup.is_unary?
1118
+ sub_sup.parameter_one.parameter_one = first_value
1119
+ sub_sup
1120
+ else
1121
+ Math::Formula.new([sub_sup, first_value])
1122
+ end
1123
+ end
1124
+
1125
+ rule(color_value: simple(:color),
1126
+ first_value: simple(:first_value)) do
1127
+ Math::Function::Color.new(
1128
+ Math::Symbol.new(color),
1129
+ first_value,
1130
+ )
1131
+ end
1132
+
1133
+ rule(color_value: simple(:color),
1134
+ first_value: sequence(:first_value)) do
1135
+ Math::Function::Color.new(
1136
+ Math::Symbol.new(color),
1137
+ Utility.filter_values(first_value),
1138
+ )
1139
+ end
1140
+
1141
+ rule(unary_arg_functions: simple(:function),
1142
+ first_value: simple(:first_value)) do
1143
+ if ["abs", "&#x249c;"].any?(function)
1144
+ Math::Function::Abs.new(
1145
+ Utility.unfenced_value(first_value, paren_specific: true)
1146
+ )
1147
+ else
1148
+ unary = Constants::UNARY_ARG_FUNCTIONS.key(function) || function.to_sym
1149
+ Math::Function::Menclose.new(
1150
+ Utility::UNICODEMATH_MENCLOSE_FUNCTIONS[unary],
1151
+ Utility.unfenced_value(first_value, paren_specific: true),
1152
+ )
1153
+ end
1154
+ end
1155
+
1156
+ rule(unary_symbols: simple(:unary),
1157
+ first_value: simple(:first_value)) do
1158
+ unary_symbol = (Constants::UNARY_SYMBOLS.key(unary.to_s) || unary.to_sym)
1159
+ if Constants::PHANTOM_SYMBOLS.key?(unary_symbol)
1160
+ new_value = nil
1161
+ Constants::PHANTOM_SYMBOLS[unary_symbol].map do |function_name, attributes|
1162
+ if function_name == :phantom && attributes
1163
+ new_value = Math::Function::Phantom.new(
1164
+ Utility.unfenced_value((new_value.nil? ? first_value : new_value), paren_specific: true)
1165
+ )
1166
+ elsif function_name == :mpadded
1167
+ new_value = Math::Function::Mpadded.new(
1168
+ Utility.unfenced_value(first_value, paren_specific: true),
1169
+ Constants::PHANTOM_SYMBOLS[unary_symbol],
1170
+ )
1171
+ end
1172
+ end
1173
+ new_value
1174
+ else
1175
+ notation = Utility::UNICODEMATH_MENCLOSE_FUNCTIONS[unary.to_sym] ||
1176
+ Utility::UNICODEMATH_MENCLOSE_FUNCTIONS[unary_symbol.to_sym]
1177
+ Math::Function::Menclose.new(
1178
+ notation,
1179
+ Utility.unfenced_value(first_value, paren_specific: true),
1180
+ )
1181
+ end
1182
+ end
1183
+
1184
+ rule(backcolor_value: simple(:color),
1185
+ first_value: simple(:first_value)) do
1186
+ color_obj = Math::Function::Color.new(
1187
+ Math::Symbol.new(color),
1188
+ first_value,
1189
+ { backcolor: true }
1190
+ )
1191
+ end
1192
+
1193
+ rule(backcolor_value: simple(:color),
1194
+ first_value: sequence(:first_value)) do
1195
+ color_obj = Math::Function::Color.new(
1196
+ Math::Symbol.new(color),
1197
+ Utility.filter_values(first_value),
1198
+ { backcolor: true }
1199
+ )
1200
+ end
1201
+
1202
+ rule(rect_value: simple(:mask),
1203
+ first_value: sequence(:first_value)) do
1204
+ Math::Function::Menclose.new(
1205
+ Utility.enclosure_attrs(mask.to_i),
1206
+ Utility.unfenced_value(first_value, paren_specific: true),
1207
+ )
1208
+ end
1209
+
1210
+ rule(rect_value: simple(:mask),
1211
+ first_value: simple(:first_value)) do
1212
+ Math::Function::Menclose.new(
1213
+ Utility.enclosure_attrs(mask.to_i),
1214
+ Utility.unfenced_value(first_value, paren_specific: true),
1215
+ )
1216
+ end
1217
+
1218
+ rule(hbracket_class: simple(:hbrack),
1219
+ first_value: simple(:first_value)) do
1220
+ value = Utility.unfenced_value(first_value, paren_specific: true)
1221
+ if hbrack == "&#x23de;"
1222
+ Math::Function::Obrace.new(value)
1223
+ elsif hbrack == "&#x23df;"
1224
+ if value.is_a?(Math::Function::Base) && !value.parameter_one.is_a?(Math::Formula)
1225
+ Math::Function::Underset.new(
1226
+ value.parameter_two,
1227
+ Math::Function::Ubrace.new(value.parameter_one)
1228
+ )
1229
+ else
1230
+ Math::Function::Ubrace.new(value)
1231
+ end
1232
+ else
1233
+ if Constants::UNDER_HORIZONTAL_BRACKETS[hbrack.to_sym] || Constants::UNDER_HORIZONTAL_BRACKETS.key(hbrack)
1234
+ Math::Function::Underset.new(
1235
+ Math::Symbol.new(Constants::HORIZONTAL_BRACKETS[hbrack.to_sym] || hbrack),
1236
+ value,
1237
+ )
1238
+ else
1239
+ Math::Function::Overset.new(
1240
+ Math::Symbol.new(Constants::HORIZONTAL_BRACKETS[hbrack.to_sym] || hbrack),
1241
+ value,
1242
+ )
1243
+ end
1244
+ end
1245
+ end
1246
+
1247
+ rule(hbracket_class: simple(:hbrack),
1248
+ first_value: sequence(:first_value)) do
1249
+ value = Utility.unfenced_value(first_value, paren_specific: true)
1250
+ if hbrack == "&#x23de;"
1251
+ Math::Function::Obrace.new(value)
1252
+ elsif hbrack == "&#x23df;"
1253
+ if value.is_a?(Math::Function::Base) && !value.parameter_one.is_a?(Math::Formula)
1254
+ Math::Function::Underset.new(
1255
+ value.parameter_two,
1256
+ Math::Function::Ubrace.new(value.parameter_one)
1257
+ )
1258
+ else
1259
+ Math::Function::Ubrace.new(value)
1260
+ end
1261
+ else
1262
+ if Constants::UNDER_HORIZONTAL_BRACKETS[hbrack.to_sym] || Constants::UNDER_HORIZONTAL_BRACKETS.key(hbrack)
1263
+ Math::Function::Underset.new(
1264
+ Math::Symbol.new(Constants::HORIZONTAL_BRACKETS[hbrack.to_sym] || hbrack),
1265
+ value,
1266
+ )
1267
+ else
1268
+ Math::Function::Overset.new(
1269
+ Math::Symbol.new(Constants::HORIZONTAL_BRACKETS[hbrack.to_sym] || hbrack),
1270
+ value,
1271
+ )
1272
+ end
1273
+ end
1274
+ end
1275
+
1276
+ rule(hbracket_class: simple(:hbrack),
1277
+ scripted_first_value: simple(:scripted_first_value)) do
1278
+ value = Utility.unfenced_value(scripted_first_value, paren_specific: true)
1279
+ if hbrack == "&#x23de;"
1280
+ Math::Function::Obrace.new(value)
1281
+ elsif hbrack == "&#x23df;"
1282
+ if value.is_a?(Math::Function::Base) && !value.parameter_one.is_a?(Math::Formula)
1283
+ Math::Function::Underset.new(
1284
+ value.parameter_two,
1285
+ Math::Function::Ubrace.new(
1286
+ Utility.unfenced_value(value.parameter_one, paren_specific: true),
1287
+ )
1288
+ )
1289
+ else
1290
+ Math::Function::Ubrace.new(value)
1291
+ end
1292
+ else
1293
+ if Constants::UNDER_HORIZONTAL_BRACKETS[hbrack.to_sym] || Constants::UNDER_HORIZONTAL_BRACKETS.key(hbrack)
1294
+ Math::Function::Underset.new(
1295
+ Math::Symbol.new(Constants::HORIZONTAL_BRACKETS[hbrack.to_sym] || hbrack),
1296
+ value,
1297
+ )
1298
+ else
1299
+ Math::Function::Overset.new(
1300
+ Math::Symbol.new(Constants::HORIZONTAL_BRACKETS[hbrack.to_sym] || hbrack),
1301
+ value,
1302
+ )
1303
+ end
1304
+ end
1305
+ end
1306
+
1307
+ rule(hbracket_class: simple(:hbrack),
1308
+ scripted_first_value: sequence(:scripted_first_value)) do
1309
+ value = Utility.unfenced_value(scripted_first_value, paren_specific: true)
1310
+ if hbrack == "&#x23de;"
1311
+ Math::Function::Obrace.new(value)
1312
+ elsif hbrack == "&#x23df;"
1313
+ if value.is_a?(Math::Function::Base) && !value.parameter_one.is_a?(Math::Formula)
1314
+ Math::Function::Underset.new(
1315
+ value.parameter_two,
1316
+ Math::Function::Ubrace.new(value.parameter_one)
1317
+ )
1318
+ else
1319
+ Math::Function::Ubrace.new(value)
1320
+ end
1321
+ else
1322
+ if Constants::UNDER_HORIZONTAL_BRACKETS[hbrack.to_sym] || Constants::UNDER_HORIZONTAL_BRACKETS.key(hbrack)
1323
+ Math::Function::Underset.new(
1324
+ Math::Symbol.new(Constants::HORIZONTAL_BRACKETS[hbrack.to_sym] || hbrack),
1325
+ value,
1326
+ )
1327
+ else
1328
+ Math::Function::Overset.new(
1329
+ Math::Symbol.new(Constants::HORIZONTAL_BRACKETS[hbrack.to_sym] || hbrack),
1330
+ value,
1331
+ )
1332
+ end
1333
+ end
1334
+ end
1335
+
1336
+ rule(first_value: simple(:first_value),
1337
+ prime_accent_symbols: sequence(:prime)) do
1338
+ prime.map!.with_index { |pp| (Constants::ACCENT_SYMBOLS[pp.to_sym] || pp) }
1339
+ Math::Function::Power.new(
1340
+ Utility.unfenced_value(first_value, paren_specific: true),
1341
+ Utility.filter_values(prime),
1342
+ )
1343
+ end
1344
+
1345
+ rule(first_value: simple(:first_value),
1346
+ prime_accent_symbols: simple(:prime)) do
1347
+ prime_symbol = (Constants::ACCENT_SYMBOLS[prime.to_sym] || prime)
1348
+ Math::Function::Power.new(
1349
+ Utility.unfenced_value(first_value, paren_specific: true),
1350
+ Math::Symbol.new(prime_symbol),
1351
+ )
1352
+ end
1353
+
1354
+ rule(first_value: sequence(:first_value),
1355
+ overlay_after: simple(:overlay)) do
1356
+ notation = Constants::OVERLAYS_NOTATIONS[overlay.to_sym]
1357
+ overlay_value = Utility.unfenced_value(first_value.pop, paren_specific: true)
1358
+ overlay_object = if notation == "mover"
1359
+ Math::Function::Overset.new(
1360
+ Math::Symbol.new(overlay),
1361
+ overlay_value,
1362
+ { accent: true },
1363
+ )
1364
+ else
1365
+ if overlay == "&#x304;"
1366
+ Math::Function::Overset.new(
1367
+ Math::Symbol.new(overlay),
1368
+ overlay_value,
1369
+ { accent: true },
1370
+ )
1371
+ else
1372
+ Math::Function::Menclose.new(
1373
+ notation,
1374
+ overlay_value
1375
+ )
1376
+ end
1377
+ end
1378
+ Math::Formula.new(first_value << overlay_object)
1379
+ end
1380
+
1381
+ rule(first_value: simple(:first_value),
1382
+ overlay_after: simple(:overlay)) do
1383
+ notation = Constants::OVERLAYS_NOTATIONS[overlay.to_sym]
1384
+ overlay_value = Utility.unfenced_value(first_value, paren_specific: true)
1385
+ if notation == "mover"
1386
+ Math::Function::Overset.new(
1387
+ Math::Symbol.new(overlay),
1388
+ overlay_value,
1389
+ { accent: true },
1390
+ )
1391
+ else
1392
+ if overlay == "&#x304;"
1393
+ Math::Function::Overset.new(
1394
+ Math::Symbol.new(overlay),
1395
+ overlay_value,
1396
+ { accent: true },
1397
+ )
1398
+ else
1399
+ Math::Function::Menclose.new(
1400
+ notation,
1401
+ overlay_value
1402
+ )
1403
+ end
1404
+ end
1405
+ end
1406
+
1407
+ rule(overlay_before: simple(:overlay),
1408
+ first_value: simple(:first_value)) do
1409
+ notation = Constants::OVERLAYS_NOTATIONS[overlay.to_sym]
1410
+ overlay_value = Utility.unfenced_value(first_value, paren_specific: true)
1411
+ if notation == "mover"
1412
+ Math::Function::Overset.new(
1413
+ Math::Symbol.new(overlay),
1414
+ overlay_value,
1415
+ { accent: true },
1416
+ )
1417
+ else
1418
+ Math::Function::Menclose.new(
1419
+ notation,
1420
+ overlay_value
1421
+ )
1422
+ end
1423
+ end
1424
+
1425
+ rule(below_after: simple(:overlay),
1426
+ first_value: simple(:first_value)) do
1427
+ notation = Constants::BELOWS_NOTATIONS[overlay.to_sym]
1428
+ overlay_value = Utility.unfenced_value(first_value, paren_specific: true)
1429
+ if notation == "munder"
1430
+ Math::Function::Underset.new(
1431
+ Math::Symbol.new(overlay),
1432
+ overlay_value,
1433
+ { accent: true },
1434
+ )
1435
+ else
1436
+ Math::Function::Menclose.new(notation, overlay_value)
1437
+ end
1438
+ end
1439
+
1440
+ rule(first_value: sequence(:first_value),
1441
+ prime_accent_symbols: simple(:prime)) do
1442
+ prime_symbol = (Constants::ACCENT_SYMBOLS[prime.to_sym] || prime)
1443
+ Math::Function::Power.new(
1444
+ Utility.unfenced_value(first_value, paren_specific: true),
1445
+ Math::Symbol.new(prime_symbol),
1446
+ )
1447
+ end
1448
+
1449
+ rule(root_first_value: sequence(:first_value),
1450
+ root_second_value: simple(:second_value)) do
1451
+ Math::Function::Root.new(
1452
+ Utility.filter_values(first_value),
1453
+ Utility.unfenced_value(second_value, paren_specific: true),
1454
+ )
1455
+ end
1456
+
1457
+ rule(first_value: simple(:first_value),
1458
+ second_value: sequence(:second_value)) do
1459
+ Math::Function::Root.new(
1460
+ first_value,
1461
+ Utility.unfenced_value(second_value, paren_specific: true),
1462
+ )
1463
+ end
1464
+
1465
+ rule(first_value: simple(:first_value),
1466
+ second_value: simple(:second_value)) do
1467
+ Math::Function::Root.new(
1468
+ first_value,
1469
+ Utility.unfenced_value(second_value, paren_specific: true),
1470
+ )
1471
+ end
1472
+
1473
+ rule(first_value: sequence(:first_value),
1474
+ second_value: sequence(:second_value)) do
1475
+ Math::Function::Root.new(
1476
+ Utility.filter_values(first_value),
1477
+ Utility.unfenced_value(second_value, paren_specific: true),
1478
+ )
1479
+ end
1480
+
1481
+ rule(unary_functions: simple(:unary),
1482
+ first_value: simple(:first_value)) do
1483
+ if Constants::UNDEF_UNARY_FUNCTIONS.include?(unary.to_s)
1484
+ Math::Formula.new([
1485
+ Math::Symbol.new(unary),
1486
+ first_value,
1487
+ ])
1488
+ else
1489
+ Utility.get_class(unary).new(first_value)
1490
+ end
1491
+ end
1492
+
1493
+ rule(unary_functions: simple(:unary),
1494
+ first_value: simple(:first_value)) do
1495
+ if Constants::UNDEF_UNARY_FUNCTIONS.include?(unary.to_s)
1496
+ Math::Formula.new([
1497
+ Math::Symbol.new(unary),
1498
+ first_value,
1499
+ ])
1500
+ else
1501
+ Utility.get_class(unary).new(first_value)
1502
+ end
1503
+ end
1504
+
1505
+ rule(phantom_value: simple(:value),
1506
+ first_value: simple(:first_value)) do
1507
+ Math::Function::Mpadded.new(
1508
+ first_value,
1509
+ { mask: value.to_s },
1510
+ )
1511
+ end
1512
+
1513
+ rule(tr: simple(:tr),
1514
+ trs: simple(:trs)) do
1515
+ [Math::Function::Tr.new([tr]), trs]
1516
+ end
1517
+
1518
+ rule(td: simple(:td),
1519
+ tds: simple(:tds)) do
1520
+ [Math::Function::Td.new([td]), tds]
1521
+ end
1522
+
1523
+ rule(tr: simple(:tr),
1524
+ trs: sequence(:trs)) do
1525
+ [Math::Function::Tr.new([tr])] + trs
1526
+ end
1527
+
1528
+ rule(tr: sequence(:tr),
1529
+ trs: simple(:trs)) do
1530
+ [Math::Function::Tr.new(tr), trs]
1531
+ end
1532
+
1533
+ rule(tr: sequence(:tr),
1534
+ trs: sequence(:trs)) do
1535
+ [Math::Function::Tr.new(tr)] + trs
1536
+ end
1537
+
1538
+ rule(td: simple(:td),
1539
+ tds: sequence(:tds)) do
1540
+ [Math::Function::Td.new([td])] + tds
1541
+ end
1542
+
1543
+ rule(td: sequence(:td),
1544
+ tds: sequence(:tds)) do
1545
+ [Math::Function::Td.new(td)] + tds
1546
+ end
1547
+
1548
+ rule(td: sequence(:td),
1549
+ tds: simple(:tds)) do
1550
+ [Math::Function::Td.new(td), tds]
1551
+ end
1552
+
1553
+ rule(numerator: simple(:numerator),
1554
+ denominator: simple(:denominator)) do
1555
+ Utility.fractions(numerator, denominator)
1556
+ end
1557
+
1558
+ rule(mini_numerator: simple(:numerator),
1559
+ mini_denominator: simple(:denominator)) do
1560
+ Utility.fractions(numerator, denominator, { displaystyle: false })
1561
+ end
1562
+
1563
+ rule(numerator: simple(:numerator),
1564
+ denominator: sequence(:denominator)) do
1565
+ Utility.fractions(numerator, denominator)
1566
+ end
1567
+
1568
+ rule(mini_numerator: simple(:numerator),
1569
+ mini_denominator: sequence(:denominator)) do
1570
+ Utility.fractions(numerator, denominator, { displaystyle: false })
1571
+ end
1572
+
1573
+ rule(numerator: sequence(:numerator),
1574
+ denominator: simple(:denominator)) do
1575
+ Utility.fractions(numerator, denominator)
1576
+ end
1577
+
1578
+ rule(mini_numerator: sequence(:numerator),
1579
+ mini_denominator: simple(:denominator)) do
1580
+ Utility.fractions(numerator, denominator, { displaystyle: false })
1581
+ end
1582
+
1583
+ rule(numerator: sequence(:numerator),
1584
+ denominator: sequence(:denominator)) do
1585
+ Utility.fractions(numerator, denominator)
1586
+ end
1587
+
1588
+ rule(mini_numerator: sequence(:numerator),
1589
+ mini_denominator: sequence(:denominator)) do
1590
+ Utility.fractions(numerator, denominator, { displaystyle: false })
1591
+ end
1592
+
1593
+ rule(matrixs: simple(:matrixs),
1594
+ array: sequence(:array)) do
1595
+ matrix = Constants::MATRIXS.key(matrixs) || matrixs.to_sym
1596
+ if :Vmatrix == matrix
1597
+ Utility.get_table_class(matrix).new(
1598
+ array,
1599
+ "norm[",
1600
+ )
1601
+ elsif :Bmatrix == matrix
1602
+ Utility.get_table_class(matrix).new(
1603
+ array,
1604
+ "{",
1605
+ "}",
1606
+ )
1607
+ elsif :matrix == matrix
1608
+ Math::Function::Table.new(array)
1609
+ else
1610
+ Utility.get_table_class(matrix).new(array)
1611
+ end
1612
+ end
1613
+
1614
+ rule(matrixs: simple(:matrixs),
1615
+ array: simple(:array)) do
1616
+ matrix = Constants::MATRIXS.key(matrixs) || matrixs.to_sym
1617
+ if :Vmatrix == matrix
1618
+ Utility.get_table_class(matrix).new(
1619
+ [array],
1620
+ "norm[",
1621
+ )
1622
+ elsif :Bmatrix == matrix
1623
+ Utility.get_table_class(matrix).new(
1624
+ [array],
1625
+ "{",
1626
+ "}",
1627
+ )
1628
+ elsif :matrix == matrix
1629
+ Math::Function::Table.new([array])
1630
+ else
1631
+ Utility.get_table_class(matrix).new([array])
1632
+ end
1633
+ end
1634
+
1635
+ rule(matrixs: simple(:matrixs),
1636
+ identity_matrix_number: simple(:number)) do
1637
+ matrix = Constants::MATRIXS.key(matrixs) || matrixs.to_sym
1638
+ if :Vmatrix == matrix
1639
+ Utility.get_table_class(matrix).new(
1640
+ Utility.identity_matrix(number.to_i),
1641
+ "norm[",
1642
+ )
1643
+ elsif :Bmatrix == matrix
1644
+ Utility.get_table_class(matrix).new(
1645
+ Utility.identity_matrix(number.to_i),
1646
+ "{",
1647
+ "}",
1648
+ )
1649
+ elsif :matrix == matrix
1650
+ Math::Function::Table.new(
1651
+ Utility.identity_matrix(number.to_i)
1652
+ )
1653
+ else
1654
+ Utility.get_table_class(matrix).new(
1655
+ Utility.identity_matrix(number.to_i)
1656
+ )
1657
+ end
1658
+ end
1659
+
1660
+ rule(factor: simple(:factor),
1661
+ sup_exp: simple(:sup_exp)) do
1662
+ [factor, sup_exp]
1663
+ end
1664
+
1665
+ rule(factor: simple(:factor),
1666
+ sub_exp: simple(:sub_exp)) do
1667
+ [factor, sub_exp]
1668
+ end
1669
+
1670
+ rule(factor: simple(:factor),
1671
+ pre_script: simple(:pre_script)) do
1672
+ [factor, pre_script]
1673
+ end
1674
+
1675
+ rule(factor: simple(:factor),
1676
+ mini_sup: simple(:mini_sup)) do
1677
+ [factor, mini_sup]
1678
+ end
1679
+
1680
+ rule(mini_sup: simple(:mini_sup),
1681
+ expr: simple(:expr)) do
1682
+ [mini_sup, expr]
1683
+ end
1684
+
1685
+ rule(mini_sup: simple(:mini_sup),
1686
+ expr: sequence(:expr)) do
1687
+ [mini_sup] + expr
1688
+ end
1689
+
1690
+ rule(mini_sup: simple(:mini_sup),
1691
+ recursive_numerator: sequence(:recursive_numerator)) do
1692
+ [mini_sup] + recursive_numerator
1693
+ end
1694
+
1695
+ rule(mini_sup: simple(:mini_sup),
1696
+ recursive_numerator: simple(:recursive_numerator)) do
1697
+ [mini_sup, recursive_numerator]
1698
+ end
1699
+
1700
+ rule(atom: simple(:atom),
1701
+ recursive_numerator: simple(:numerator)) do
1702
+ [atom, numerator]
1703
+ end
1704
+
1705
+ rule(digit: simple(:digit),
1706
+ recursive_numerator: simple(:numerator)) do
1707
+ [digit, numerator]
1708
+ end
1709
+
1710
+ rule(digit: simple(:digit),
1711
+ recursive_numerator: sequence(:numerator)) do
1712
+ [digit] + numerator
1713
+ end
1714
+
1715
+ rule(digit: simple(:digit),
1716
+ expr: sequence(:expr)) do
1717
+ [digit] + expr
1718
+ end
1719
+
1720
+ rule(digit: simple(:digit),
1721
+ expr: simple(:expr)) do
1722
+ [digit, expr]
1723
+ end
1724
+
1725
+ rule(sup_exp: simple(:sup),
1726
+ recursive_numerator: simple(:recursive_numerator)) do
1727
+ [sup, recursive_numerator]
1728
+ end
1729
+
1730
+ rule(sup_exp: simple(:sup),
1731
+ recursive_numerator: sequence(:recursive_numerator)) do
1732
+ [sup] + recursive_numerator
1733
+ end
1734
+
1735
+ rule(frac: simple(:frac),
1736
+ expr: simple(:expr)) do
1737
+ [frac, expr]
1738
+ end
1739
+
1740
+ rule(frac: simple(:frac),
1741
+ exp: simple(:exp)) do
1742
+ [frac, exp]
1743
+ end
1744
+
1745
+ rule(expr: simple(:expr),
1746
+ func_expr: simple(:func_expr)) do
1747
+ [expr, func_expr]
1748
+ end
1749
+
1750
+ rule(expr: simple(:expr),
1751
+ func_expr: sequence(:func_expr)) do
1752
+ [expr] + func_expr
1753
+ end
1754
+
1755
+ rule(frac: simple(:frac),
1756
+ expr: sequence(:expr)) do
1757
+ [frac] + expr
1758
+ end
1759
+
1760
+ rule(frac: simple(:frac),
1761
+ exp: sequence(:exp)) do
1762
+ [frac] + exp
1763
+ end
1764
+
1765
+ rule(nary: simple(:nary),
1766
+ expr: sequence(:expr)) do
1767
+ [nary] + expr
1768
+ end
1769
+
1770
+ rule(nary: simple(:nary),
1771
+ expr: simple(:expr)) do
1772
+ [nary, expr]
1773
+ end
1774
+
1775
+ rule(intermediate_exp: simple(:exp),
1776
+ expr: simple(:expr)) do
1777
+ [exp, expr]
1778
+ end
1779
+
1780
+ rule(intermediate_exp: simple(:exp),
1781
+ operator: simple(:operator)) do
1782
+ [exp, Math::Symbol.new(operator)]
1783
+ end
1784
+
1785
+ rule(intermediate_exp: simple(:exp),
1786
+ exclamation_symbol: simple(:exclamation_symbol)) do
1787
+ [exp, Math::Symbol.new(exclamation_symbol)]
1788
+ end
1789
+
1790
+ rule(atom: simple(:atom),
1791
+ exclamation_symbol: simple(:exclamation_symbol)) do
1792
+ [atom, Math::Symbol.new(exclamation_symbol)]
1793
+ end
1794
+
1795
+ rule(intermediate_exp: simple(:exp),
1796
+ expr: sequence(:expr)) do
1797
+ [exp] + expr
1798
+ end
1799
+
1800
+ rule(nary_sub_sup: simple(:subsup_exp),
1801
+ naryand: simple(:naryand)) do
1802
+ if subsup_exp.is_ternary_function?
1803
+ subsup_exp.parameter_three = naryand
1804
+ subsup_exp
1805
+ elsif subsup_exp.is_a?(Math::Function::Nary)
1806
+ subsup_exp.parameter_four = naryand
1807
+ subsup_exp
1808
+ else
1809
+ Math::Formula.new([subsup_exp, naryand])
1810
+ end
1811
+ end
1812
+
1813
+ rule(nary_sub_sup: simple(:subsup_exp),
1814
+ naryand: sequence(:naryand)) do
1815
+ if subsup_exp.is_ternary_function?
1816
+ subsup_exp.parameter_three = Utility.filter_values(naryand)
1817
+ subsup_exp
1818
+ else
1819
+ Math::Function::Nary.new(
1820
+ subsup_exp.parameter_one,
1821
+ subsup_exp.parameter_two,
1822
+ subsup_exp.parameter_three,
1823
+ Utility.filter_values(naryand)
1824
+ )
1825
+ end
1826
+ end
1827
+
1828
+ rule(slashed_value: simple(:value),
1829
+ expr: simple(:expr)) do
1830
+ [Utility.slashed_values(value), expr]
1831
+ end
1832
+
1833
+ rule(slashed_value: simple(:value),
1834
+ exp: simple(:expr)) do
1835
+ [Utility.slashed_values(value), expr]
1836
+ end
1837
+
1838
+ rule(slashed_value: sequence(:values),
1839
+ expr: simple(:expr)) do
1840
+ Utility.sequence_slashed_values(values) + [expr]
1841
+ end
1842
+
1843
+ rule(slashed_value: sequence(:values),
1844
+ expr: sequence(:expr)) do
1845
+ Utility.sequence_slashed_values(values) + expr
1846
+ end
1847
+
1848
+ rule(slashed_value: simple(:value),
1849
+ expr: sequence(:expr)) do
1850
+ [Utility.slashed_values(value)] + expr
1851
+ end
1852
+
1853
+ rule(nary_class: simple(:nary_class),
1854
+ sub: sequence(:sub)) do
1855
+ nary_function = if Constants::NARY_CLASSES.key?(nary_class.to_sym)
1856
+ nary_class
1857
+ else
1858
+ (Constants::NARY_CLASSES.invert[nary_class.to_s] || Constants::NARY_SYMBOLS[nary_class.to_sym] || nary_class)
1859
+ end
1860
+ Utility.get_class(nary_function).new(
1861
+ Utility.filter_values(sub),
1862
+ )
1863
+ end
1864
+
1865
+ rule(nary_class: simple(:nary_class),
1866
+ sub: simple(:sub)) do
1867
+ nary_function = if Constants::NARY_CLASSES.key?(nary_class.to_sym)
1868
+ nary_class
1869
+ else
1870
+ (Constants::NARY_CLASSES.invert[nary_class.to_s] || Constants::NARY_SYMBOLS[nary_class.to_sym] || nary_class)
1871
+ end
1872
+ if Constants::NARY_CLASSES.key?(nary_function.to_sym)
1873
+ nary_value = if sub.class_name == "underset"
1874
+ sub.parameter_one
1875
+ else
1876
+ Utility.unfenced_value(sub, paren_specific: true)
1877
+ end
1878
+ Utility.get_class(nary_function).new(nary_value)
1879
+ else
1880
+ Math::Function::Nary.new(
1881
+ Math::Symbol.new(nary_function),
1882
+ Utility.unfenced_value(sub, paren_specific: true)
1883
+ )
1884
+ end
1885
+ end
1886
+
1887
+ rule(nary_class: simple(:nary_class),
1888
+ sup: simple(:sup)) do
1889
+ nary_function = if Constants::NARY_CLASSES.key?(nary_class.to_sym)
1890
+ nary_class
1891
+ else
1892
+ (Constants::NARY_CLASSES.invert[nary_class.to_s] || Constants::NARY_SYMBOLS[nary_class.to_sym] || nary_class)
1893
+ end
1894
+ nary_value = if sup.class_name == "overset"
1895
+ sup.parameter_one
1896
+ else
1897
+ Utility.unfenced_value(sup, paren_specific: true)
1898
+ end
1899
+ Utility.get_class(nary_function).new(nil, nary_value)
1900
+ end
1901
+
1902
+ rule(nary_class: simple(:nary_class),
1903
+ naryand: simple(:naryand)) do
1904
+ nary_function = if Constants::NARY_CLASSES.key?(nary_class.to_sym)
1905
+ nary_class
1906
+ else
1907
+ (Constants::NARY_CLASSES.invert[nary_class.to_s] || Constants::NARY_SYMBOLS[nary_class.to_sym] || nary_class)
1908
+ end
1909
+ if Constants::NARY_CLASSES.key?(nary_function.to_sym)
1910
+ nary_value = if naryand.class_name == "underset"
1911
+ naryand.parameter_two
1912
+ else
1913
+ Utility.unfenced_value(naryand, paren_specific: true)
1914
+ end
1915
+ Utility.get_class(nary_function).new(nil, nil, nary_value)
1916
+ else
1917
+ Math::Function::Nary.new(
1918
+ Math::Symbol.new(nary_function),
1919
+ nil,
1920
+ nil,
1921
+ naryand,
1922
+ )
1923
+ end
1924
+ end
1925
+
1926
+ rule(pre_supscript: simple(:pre_sup),
1927
+ base: simple(:base)) do
1928
+ Math::Function::Multiscript.new(
1929
+ Math::Function::PowerBase.new(base),
1930
+ [],
1931
+ [pre_sup],
1932
+ )
1933
+ end
1934
+
1935
+ rule(pre_subscript: simple(:pre_sub),
1936
+ base: simple(:base)) do
1937
+ Math::Function::Multiscript.new(
1938
+ Math::Function::PowerBase.new(base),
1939
+ [pre_sub],
1940
+ [],
1941
+ )
1942
+ end
1943
+
1944
+ rule(paren_close_prefix: simple(:prefix),
1945
+ open_paren: simple(:paren)) do
1946
+ [Math::Number.new(""), paren]
1947
+ end
1948
+
1949
+ rule(paren_open_prefix: simple(:prefix),
1950
+ close_paren: simple(:paren)) do
1951
+ [Math::Number.new(""), paren]
1952
+ end
1953
+
1954
+ rule(open_paren: simple(:open_paren),
1955
+ close_paren: simple(:close_paren)) do
1956
+ Math::Function::Fenced.new(
1957
+ open_paren.is_a?(Slice) ? Math::Symbol.new(open_paren) : open_paren,
1958
+ [],
1959
+ close_paren.is_a?(Slice) ? Math::Symbol.new(close_paren) : close_paren
1960
+ )
1961
+ end
1962
+
1963
+ rule(factor: simple(:factor),
1964
+ mini_sup: simple(:mini_sup),
1965
+ expr: sequence(:expr)) do
1966
+ [factor, mini_sup] + expr
1967
+ end
1968
+
1969
+ rule(atom: simple(:atom),
1970
+ atoms: sequence(:atoms),
1971
+ recursive_denominator: simple(:denominator)) do
1972
+ [atom] + atoms + [denominator]
1973
+ end
1974
+
1975
+ rule(atom: simple(:atom),
1976
+ binary_symbols: simple(:symbols),
1977
+ factor: simple(:factor)) do
1978
+ symbol = (Constants::BINARY_SYMBOLS[symbols.to_sym] || symbols)
1979
+ [atom, Math::Symbol.new(symbol), factor]
1980
+ end
1981
+
1982
+ rule(atom: simple(:atom),
1983
+ binary_symbols: simple(:symbols),
1984
+ recursive_numerator: simple(:numerator)) do
1985
+ symbol = (Constants::BINARY_SYMBOLS[symbols.to_sym] || symbols)
1986
+ [atom, Math::Symbol.new(symbol), numerator]
1987
+ end
1988
+
1989
+ rule(paren_open_prefix: simple(:paren_open_prefix),
1990
+ open_paren_mask: simple(:open_paren_mask),
1991
+ open_paren: simple(:open_paren)) do
1992
+ [open_paren_mask, open_paren]
1993
+ end
1994
+
1995
+ rule(paren_close_prefix: simple(:paren_close_prefix),
1996
+ close_paren_mask: simple(:close_paren_mask),
1997
+ open_paren: simple(:open_paren)) do
1998
+ [close_paren_mask, open_paren]
1999
+ end
2000
+
2001
+ rule(paren_close_prefix: simple(:paren_close_prefix),
2002
+ close_paren_mask: simple(:close_paren_mask),
2003
+ close_paren: simple(:close_paren)) do
2004
+ [close_paren_mask, close_paren]
2005
+ end
2006
+
2007
+ rule(paren_open_prefix: simple(:paren_open_prefix),
2008
+ open_paren_mask: simple(:open_paren_mask),
2009
+ close_paren: simple(:close_paren)) do
2010
+ [open_paren_mask, close_paren]
2011
+ end
2012
+
2013
+ rule(char: simple(:char),
2014
+ diacritics: sequence(:diacritics),
2015
+ number: simple(:number)) do
2016
+ [char] + diacritics + [Math::Number.new(number)]
2017
+ end
2018
+
2019
+ rule(char: simple(:char),
2020
+ diacritics: simple(:diacritics),
2021
+ number: simple(:number)) do
2022
+ [char, diacritics, Math::Number.new(number)]
2023
+ end
2024
+
2025
+ rule(char: simple(:char),
2026
+ diacritics: simple(:diacritics),
2027
+ alphanumeric: simple(:alpha)) do
2028
+ [char, diacritics, Math::Symbol.new(alpha)]
2029
+ end
2030
+
2031
+ rule(sub_script: simple(:sub_script),
2032
+ mini_sup: simple(:mini_sup),
2033
+ operand: simple(:operand)) do
2034
+ [sub_script, mini_sup, operand]
2035
+ end
2036
+
2037
+ rule(base: simple(:base),
2038
+ sup: simple(:sup),
2039
+ sub: simple(:sub)) do
2040
+ underover_classes = ["underset", "overset"].freeze
2041
+ if underover_classes.include?(sub.class_name) && underover_classes.include?(sup.class_name)
2042
+ Math::Function::Underover.new(
2043
+ base,
2044
+ sub.parameter_one,
2045
+ sup.parameter_one,
2046
+ )
2047
+ else
2048
+ Math::Function::PowerBase.new(
2049
+ base,
2050
+ Utility.unfenced_value(sub, paren_specific: true),
2051
+ Utility.unfenced_value(sup, paren_specific: true),
2052
+ )
2053
+ end
2054
+ end
2055
+
2056
+ rule(base: simple(:base),
2057
+ size_overrides: simple(:size_overrides),
2058
+ sub_script: sequence(:sub)) do
2059
+ Math::Function::Base.new(
2060
+ base,
2061
+ Utility.filter_values(sub),
2062
+ { size: Constants::SIZE_OVERRIDES_SYMBOLS[size_overrides.to_sym] }
2063
+ )
2064
+ end
2065
+
2066
+ rule(base: simple(:base),
2067
+ size_overrides: simple(:size_overrides),
2068
+ sub_script: simple(:sub)) do
2069
+ Math::Function::Base.new(
2070
+ base,
2071
+ Utility.unfenced_value(sub, paren_specific: true),
2072
+ { size: Constants::SIZE_OVERRIDES_SYMBOLS[size_overrides.to_sym] }
2073
+ )
2074
+ end
2075
+
2076
+ rule(base: sequence(:base),
2077
+ sup: simple(:sup),
2078
+ sub: simple(:sub)) do
2079
+ power_base = Math::Function::PowerBase.new(
2080
+ base.pop,
2081
+ Utility.unfenced_value(sub, paren_specific: true),
2082
+ Utility.unfenced_value(sup, paren_specific: true),
2083
+ )
2084
+ base + [power_base]
2085
+ end
2086
+
2087
+ rule(base: simple(:base),
2088
+ sup: simple(:sup),
2089
+ sub: sequence(:sub)) do
2090
+ Math::Function::PowerBase.new(
2091
+ base,
2092
+ Utility.unfenced_value(sub, paren_specific: true),
2093
+ Utility.unfenced_value(sup, paren_specific: true),
2094
+ )
2095
+ end
2096
+
2097
+ rule(base: simple(:base),
2098
+ sup: sequence(:sup),
2099
+ sub: sequence(:sub)) do
2100
+ Math::Function::PowerBase.new(
2101
+ base,
2102
+ Utility.filter_values(sub),
2103
+ Utility.filter_values(sup),
2104
+ )
2105
+ end
2106
+
2107
+ rule(base: simple(:base),
2108
+ sup: sequence(:sup),
2109
+ sub: simple(:sub)) do
2110
+ Math::Function::PowerBase.new(
2111
+ base,
2112
+ Utility.filter_values(sub),
2113
+ Utility.filter_values(sup),
2114
+ )
2115
+ end
2116
+
2117
+ rule(nary_class: simple(:nary_class),
2118
+ sub: sequence(:sub),
2119
+ sup: simple(:sup)) do
2120
+ nary_function = if Constants::NARY_CLASSES.key?(nary_class.to_sym)
2121
+ nary_class
2122
+ else
2123
+ (Constants::NARY_CLASSES.invert[nary_class.to_s] || Constants::NARY_SYMBOLS[nary_class.to_sym] || nary_class)
2124
+ end
2125
+ Utility.get_class(nary_function).new(
2126
+ Utility.filter_values(sub),
2127
+ Utility.filter_values(sup),
2128
+ )
2129
+ end
2130
+
2131
+ rule(numerator: simple(:numerator),
2132
+ atop: simple(:atop),
2133
+ denominator: simple(:denominator)) do
2134
+ Utility.fractions(numerator, denominator, { linethickness: "0" })
2135
+ end
2136
+
2137
+ rule(numerator: sequence(:numerator),
2138
+ atop: simple(:atop),
2139
+ denominator: simple(:denominator)) do
2140
+ Utility.fractions(numerator, denominator, { linethickness: "0" })
2141
+ end
2142
+
2143
+ rule(numerator: simple(:numerator),
2144
+ choose: simple(:choose),
2145
+ denominator: simple(:denominator)) do
2146
+ Math::Function::Fenced.new(
2147
+ Math::Symbol.new("("),
2148
+ [
2149
+ Utility.fractions(numerator, denominator, { linethickness: "0", choose: true }),
2150
+ ],
2151
+ Math::Symbol.new(")"),
2152
+ )
2153
+ end
2154
+
2155
+ rule(arg: simple(:arg),
2156
+ arg_arguments: simple(:args),
2157
+ first_value: simple(:first_value)) do
2158
+ Math::Function::Arg.new(first_value, args)
2159
+ end
2160
+
2161
+ rule(whole: simple(:whole),
2162
+ decimal: simple(:decimal),
2163
+ fractional: simple(:fractional)) do
2164
+ Math::Number.new("#{whole.value}#{decimal}#{fractional.value}")
2165
+ end
2166
+
2167
+ rule(factor: simple(:factor),
2168
+ operand: sequence(:operand),
2169
+ expr: simple(:expr)) do
2170
+ [factor] + operand + [expr]
2171
+ end
2172
+
2173
+ rule(factor: simple(:factor),
2174
+ operand: sequence(:operand),
2175
+ expr: sequence(:expr)) do
2176
+ [factor] + operand + expr
2177
+ end
2178
+
2179
+ rule(factor: simple(:factor),
2180
+ operand: sequence(:operand),
2181
+ naryand_recursion: sequence(:naryand_recursion)) do
2182
+ [factor] + operand + naryand_recursion
2183
+ end
2184
+
2185
+ rule(factor: sequence(:factor),
2186
+ operand: simple(:operand),
2187
+ expr: sequence(:expr)) do
2188
+ factor + [operand] + expr
2189
+ end
2190
+
2191
+ rule(factor: sequence(:factor),
2192
+ operand: sequence(:operand),
2193
+ expr: simple(:expr)) do
2194
+ factor + operand + [expr]
2195
+ end
2196
+
2197
+ rule(factor: simple(:factor),
2198
+ operand: simple(:operand),
2199
+ expr: sequence(:expr)) do
2200
+ [factor, operand] + expr
2201
+ end
2202
+
2203
+ rule(factor: simple(:factor),
2204
+ operand: simple(:operand),
2205
+ exp: sequence(:exp)) do
2206
+ [factor, operand] + exp
2207
+ end
2208
+
2209
+ rule(factor: simple(:factor),
2210
+ operand: simple(:operand),
2211
+ exp: simple(:exp)) do
2212
+ [factor, operand, exp]
2213
+ end
2214
+
2215
+ rule(factor: simple(:factor),
2216
+ operand: sequence(:operand),
2217
+ exp: sequence(:exp)) do
2218
+ [factor] + operand + exp
2219
+ end
2220
+
2221
+ rule(factor: simple(:factor),
2222
+ sup_exp: simple(:sup_exp),
2223
+ expr: simple(:expr)) do
2224
+ [factor, sup_exp, expr]
2225
+ end
2226
+
2227
+ rule(factor: simple(:factor),
2228
+ sub_exp: simple(:sub_exp),
2229
+ expr: simple(:expr)) do
2230
+ [factor, sub_exp, expr]
2231
+ end
2232
+
2233
+ rule(factor: simple(:factor),
2234
+ sup_exp: simple(:sup_exp),
2235
+ expr: sequence(:expr)) do
2236
+ [factor, sup_exp] + expr
2237
+ end
2238
+
2239
+ rule(factor: simple(:factor),
2240
+ sub_exp: simple(:sub_exp),
2241
+ expr: sequence(:expr)) do
2242
+ [factor, sub_exp] + expr
2243
+ end
2244
+
2245
+ rule(factor: simple(:factor),
2246
+ operand: simple(:operand),
2247
+ expr: simple(:expr)) do
2248
+ [factor, operand, expr]
2249
+ end
2250
+
2251
+ rule(factor: simple(:factor),
2252
+ operand: simple(:operand),
2253
+ recursive_denominator: simple(:denominator)) do
2254
+ [factor, operand, denominator]
2255
+ end
2256
+
2257
+ rule(factor: simple(:factor),
2258
+ expr: simple(:expr),
2259
+ expression: simple(:expression)) do
2260
+ [factor, expr, expression]
2261
+ end
2262
+
2263
+ rule(sub_script: simple(:sub_script),
2264
+ mini_sub: simple(:mini_sub),
2265
+ exp_iteration: simple(:exp_iteration)) do
2266
+ [sub_script, mini_sub, exp_iteration]
2267
+ end
2268
+
2269
+ rule(intermediate_exp: simple(:exp),
2270
+ expr: simple(:expr),
2271
+ expression: simple(:expression)) do
2272
+ [exp, expr, expression]
2273
+ end
2274
+
2275
+ rule(numerator: simple(:numerator),
2276
+ bevelled: simple(:bevelled),
2277
+ denominator: simple(:denominator)) do
2278
+ Utility.fractions(numerator, denominator, { bevelled: true })
2279
+ end
2280
+
2281
+ rule(numerator: simple(:numerator),
2282
+ ldiv: simple(:ldiv),
2283
+ denominator: simple(:denominator)) do
2284
+ Utility.fractions(numerator, denominator, { ldiv: true })
2285
+ end
2286
+
2287
+ rule(numerator: simple(:numerator),
2288
+ bevelled: simple(:bevelled),
2289
+ denominator: sequence(:denominator)) do
2290
+ Utility.fractions(numerator, denominator, { bevelled: true })
2291
+ end
2292
+
2293
+ rule(numerator: simple(:numerator),
2294
+ ldiv: simple(:ldiv),
2295
+ denominator: sequence(:denominator)) do
2296
+ Utility.fractions(numerator, denominator, { ldiv: true })
2297
+ end
2298
+
2299
+ rule(numerator: simple(:numerator),
2300
+ no_display_style: simple(:no_display_style),
2301
+ denominator: sequence(:denominator)) do
2302
+ Utility.fractions(numerator, denominator, { no_display_style: false })
2303
+ end
2304
+
2305
+ rule(numerator: simple(:numerator),
2306
+ no_display_style: simple(:no_display_style),
2307
+ denominator: simple(:denominator)) do
2308
+ Utility.fractions(numerator, denominator, { displaystyle: false })
2309
+ end
2310
+
2311
+ rule(fonts: simple(:fonts),
2312
+ relational_symbols: simple(:symbols),
2313
+ expr: simple(:expr)) do
2314
+ [
2315
+ fonts,
2316
+ Math::Symbol.new((Constants::RELATIONAL_SYMBOLS[symbols.to_sym] || symbols)),
2317
+ expr,
2318
+ ]
2319
+ end
2320
+
2321
+ rule(frac: simple(:frac),
2322
+ relational_symbols: simple(:symbols),
2323
+ expr: simple(:expr)) do
2324
+ [
2325
+ frac,
2326
+ Math::Symbol.new((Constants::RELATIONAL_SYMBOLS[symbols.to_sym] || symbols)),
2327
+ expr,
2328
+ ]
2329
+ end
2330
+
2331
+ rule(base: simple(:base),
2332
+ sub: sequence(:sub),
2333
+ sub_recursion: simple(:sub_recursion)) do
2334
+ if (Constants::BINARY_FUNCTIONS.include?(base.class_name) && !base.parameter_one)
2335
+ base.parameter_one = sub_value
2336
+ base
2337
+ elsif base.is_a?(Math::Function::Power) && Utility.base_is_prime?(base)
2338
+ Math::Function::PowerBase.new(
2339
+ base.parameter_one,
2340
+ sub,
2341
+ base.parameter_two,
2342
+ )
2343
+ else
2344
+ Math::Function::Base.new(
2345
+ base,
2346
+ Utility.recursive_sub(
2347
+ Utility.filter_values(sub),
2348
+ sub_recursion,
2349
+ ),
2350
+ )
2351
+ end
2352
+ end
2353
+
2354
+ rule(mini_intermediate_exp: simple(:mini_exp),
2355
+ sub_operators: simple(:sub_operators),
2356
+ sub_recursions: sequence(:sub_recursions)) do
2357
+ operators = Constants::SUB_OPERATORS.key(sub_operators).to_s
2358
+ [
2359
+ mini_exp,
2360
+ Math::Number.new(operators, mini_sub_sized: true),
2361
+ ] + sub_recursions
2362
+ end
2363
+
2364
+ rule(opener: simple(:opener),
2365
+ operand: simple(:operand),
2366
+ closer: simple(:closer)) do
2367
+ Utility.unfenced_value(operand, paren_specific: true) if [opener, closer].include?("|")
2368
+ Math::Function::Fenced.new(
2369
+ opener.is_a?(Slice) ? Math::Symbol.new(opener) : opener,
2370
+ [operand],
2371
+ closer.is_a?(Slice) ? Math::Symbol.new(closer) : closer,
2372
+ )
2373
+ end
2374
+
2375
+ rule(opener: simple(:opener),
2376
+ operand: sequence(:operand),
2377
+ closer: simple(:closer)) do
2378
+ Math::Function::Fenced.new(
2379
+ opener.is_a?(Slice) ? Math::Symbol.new(opener) : opener,
2380
+ operand,
2381
+ closer.is_a?(Slice) ? Math::Symbol.new(closer) : closer,
2382
+ )
2383
+ end
2384
+
2385
+ rule(opener: sequence(:opener),
2386
+ operand: simple(:operand),
2387
+ closer: simple(:closer)) do
2388
+ options = {}
2389
+ if opener.first.is_a?(Math::Number)
2390
+ mask = "#{1.25**opener[0].value.to_i}em"
2391
+ options[:open_prefixed] = true
2392
+ options[:open_paren] = { minsize: mask, maxsize: mask } unless opener.first.value == ""
2393
+ end
2394
+ Utility.unfenced_value(operand, paren_specific: true) if [opener, closer].include?("|")
2395
+ Math::Function::Fenced.new(
2396
+ Math::Symbol.new(opener.last),
2397
+ [operand],
2398
+ closer.is_a?(Slice) ? Math::Symbol.new(closer) : closer,
2399
+ options,
2400
+ )
2401
+ end
2402
+
2403
+ rule(open_paren: simple(:open_paren),
2404
+ frac: simple(:frac),
2405
+ close_paren: simple(:close_paren)) do
2406
+ Math::Function::Fenced.new(
2407
+ open_paren.is_a?(Slice) ? Math::Symbol.new(open_paren) : open_paren,
2408
+ [frac],
2409
+ close_paren.is_a?(Slice) ? Math::Symbol.new(close_paren) : close_paren,
2410
+ )
2411
+ end
2412
+
2413
+ rule(open_paren: simple(:open_paren),
2414
+ slashed_value: sequence(:values),
2415
+ close_paren: simple(:close_paren)) do
2416
+ Math::Function::Fenced.new(
2417
+ open_paren.is_a?(Slice) ? Math::Symbol.new(open_paren) : open_paren,
2418
+ Utility.sequence_slashed_values(values),
2419
+ close_paren.is_a?(Slice) ? Math::Symbol.new(close_paren) : close_paren,
2420
+ )
2421
+ end
2422
+
2423
+ rule(open_paren: simple(:open_paren),
2424
+ phantom: simple(:phantom),
2425
+ close_paren: simple(:close_paren)) do
2426
+ Math::Function::Fenced.new(
2427
+ open_paren.is_a?(Slice) ? Math::Symbol.new(open_paren) : open_paren,
2428
+ [phantom],
2429
+ close_paren.is_a?(Slice) ? Math::Symbol.new(close_paren) : close_paren,
2430
+ )
2431
+ end
2432
+
2433
+ rule(open_paren: simple(:open_paren),
2434
+ unary_function: simple(:unary_function),
2435
+ close_paren: simple(:close_paren)) do
2436
+ Math::Function::Fenced.new(
2437
+ open_paren.is_a?(Slice) ? Math::Symbol.new(open_paren) : open_paren,
2438
+ [unary_function],
2439
+ close_paren.is_a?(Slice) ? Math::Symbol.new(close_paren) : close_paren,
2440
+ )
2441
+ end
2442
+
2443
+ rule(open_paren: simple(:open_paren),
2444
+ rect: simple(:rect),
2445
+ close_paren: simple(:close_paren)) do
2446
+ Math::Function::Fenced.new(
2447
+ open_paren.is_a?(Slice) ? Math::Symbol.new(open_paren) : open_paren,
2448
+ [rect],
2449
+ close_paren.is_a?(Slice) ? Math::Symbol.new(close_paren) : close_paren,
2450
+ )
2451
+ end
2452
+
2453
+ rule(open_paren: simple(:open_paren),
2454
+ factor: simple(:factor),
2455
+ paren_close_prefix: simple(:close_prefix)) do
2456
+ Math::Function::Fenced.new(
2457
+ open_paren.is_a?(Slice) ? Math::Symbol.new(open_paren) : open_paren,
2458
+ [factor],
2459
+ Math::Symbol.new(close_prefix),
2460
+ { close_prefixed: true }
2461
+ )
2462
+ end
2463
+
2464
+ rule(paren_open_prefix: simple(:open_paren),
2465
+ factor: simple(:factor),
2466
+ close_paren: simple(:close_paren)) do
2467
+ Math::Function::Fenced.new(
2468
+ Math::Symbol.new(open_paren),
2469
+ [factor],
2470
+ close_paren.is_a?(Slice) ? Math::Symbol.new(close_paren) : close_paren,
2471
+ { open_prefixed: true }
2472
+ )
2473
+ end
2474
+
2475
+ rule(open_paren: simple(:open_paren),
2476
+ nary: simple(:nary),
2477
+ close_paren: simple(:close_paren)) do
2478
+ Math::Function::Fenced.new(
2479
+ open_paren.is_a?(Slice) ? Math::Symbol.new(open_paren) : open_paren,
2480
+ [nary],
2481
+ close_paren.is_a?(Slice) ? Math::Symbol.new(close_paren) : close_paren,
2482
+ )
2483
+ end
2484
+
2485
+ rule(open_paren: simple(:open_paren),
2486
+ sub_exp: simple(:sub_exp),
2487
+ close_paren: simple(:close_paren)) do
2488
+ Math::Function::Fenced.new(
2489
+ open_paren.is_a?(Slice) ? Math::Symbol.new(open_paren) : open_paren,
2490
+ [sub_exp],
2491
+ close_paren.is_a?(Slice) ? Math::Symbol.new(close_paren) : close_paren,
2492
+ )
2493
+ end
2494
+
2495
+ rule(open_paren: simple(:open_paren),
2496
+ subsup_exp: simple(:subsup_exp),
2497
+ close_paren: simple(:close_paren)) do
2498
+ Math::Function::Fenced.new(
2499
+ open_paren.is_a?(Slice) ? Math::Symbol.new(open_paren) : open_paren,
2500
+ [subsup_exp],
2501
+ close_paren.is_a?(Slice) ? Math::Symbol.new(close_paren) : close_paren,
2502
+ )
2503
+ end
2504
+
2505
+ rule(open_paren: simple(:open_paren),
2506
+ unary_subsup: simple(:subsup),
2507
+ close_paren: simple(:close_paren)) do
2508
+ Math::Function::Fenced.new(
2509
+ open_paren.is_a?(Slice) ? Math::Symbol.new(open_paren) : open_paren,
2510
+ [subsup],
2511
+ close_paren.is_a?(Slice) ? Math::Symbol.new(close_paren) : close_paren,
2512
+ )
2513
+ end
2514
+
2515
+ rule(open_paren: simple(:open_paren),
2516
+ mini_sup: simple(:mini_sup),
2517
+ close_paren: simple(:close_paren)) do
2518
+ Math::Function::Fenced.new(
2519
+ open_paren.is_a?(Slice) ? Math::Symbol.new(open_paren) : open_paren,
2520
+ [mini_sup],
2521
+ close_paren.is_a?(Slice) ? Math::Symbol.new(close_paren) : close_paren,
2522
+ )
2523
+ end
2524
+
2525
+ rule(sub_open_paren: simple(:open_paren),
2526
+ mini_expr: sequence(:mini_expr),
2527
+ sub_close_paren: simple(:close_paren)) do
2528
+ sub_open = Constants::SUB_PARENTHESIS[:open].key(open_paren)
2529
+ sub_close = Constants::SUB_PARENTHESIS[:close].key(close_paren)
2530
+ Math::Function::Fenced.new(
2531
+ Math::Symbol.new(sub_open.to_s, mini_sub_sized: true),
2532
+ mini_expr,
2533
+ Math::Symbol.new(sub_close.to_s, mini_sub_sized: true),
2534
+ )
2535
+ end
2536
+
2537
+ rule(open_paren: simple(:open_paren),
2538
+ text: simple(:text),
2539
+ close_paren: simple(:close_paren)) do
2540
+ Math::Function::Fenced.new(
2541
+ open_paren.is_a?(Slice) ? Math::Symbol.new(open_paren) : open_paren,
2542
+ [Math::Function::Text.new(text)],
2543
+ close_paren.is_a?(Slice) ? Math::Symbol.new(close_paren) : close_paren,
2544
+ )
2545
+ end
2546
+
2547
+ rule(open_paren: simple(:open_paren),
2548
+ factor: simple(:factor),
2549
+ close_paren: simple(:close_paren)) do
2550
+ new_factor = [open_paren, close_paren].include?("|") ? Utility.unfenced_value(factor, paren_specific: true) : factor
2551
+ Math::Function::Fenced.new(
2552
+ open_paren.is_a?(Slice) ? Math::Symbol.new(open_paren) : open_paren,
2553
+ [new_factor],
2554
+ close_paren.is_a?(Slice) ? Math::Symbol.new(close_paren) : close_paren,
2555
+ )
2556
+ end
2557
+
2558
+ rule(open_paren: simple(:open_paren),
2559
+ sup_exp: simple(:sup_exp),
2560
+ close_paren: simple(:close_paren)) do
2561
+ Math::Function::Fenced.new(
2562
+ open_paren.is_a?(Slice) ? Math::Symbol.new(open_paren) : open_paren,
2563
+ [sup_exp],
2564
+ close_paren.is_a?(Slice) ? Math::Symbol.new(close_paren) : close_paren,
2565
+ )
2566
+ end
2567
+
2568
+ rule(open_paren: simple(:open_paren),
2569
+ accents: subtree(:accents),
2570
+ close_paren: simple(:close_paren)) do
2571
+ Math::Function::Fenced.new(
2572
+ open_paren.is_a?(Slice) ? Math::Symbol.new(open_paren) : open_paren,
2573
+ [Utility.unicode_accents(accents)],
2574
+ close_paren.is_a?(Slice) ? Math::Symbol.new(close_paren) : close_paren,
2575
+ )
2576
+ end
2577
+
2578
+ rule(open_paren: sequence(:open_paren),
2579
+ factor: simple(:factor),
2580
+ close_paren: simple(:close_paren)) do
2581
+ options = {}
2582
+ if open_paren.first.is_a?(Math::Number)
2583
+ mask = "#{1.25**open_paren.first.value.to_i}em"
2584
+ options[:open_prefixed] = true
2585
+ options[:open_paren] = { minsize: mask, maxsize: mask } unless open_paren.first.value == ""
2586
+ end
2587
+ new_factor = [open_paren, close_paren].include?("|") ? Utility.unfenced_value(factor, paren_specific: true) : factor
2588
+ Math::Function::Fenced.new(
2589
+ Math::Symbol.new(open_paren.last),
2590
+ [new_factor],
2591
+ close_paren.is_a?(Slice) ? Math::Symbol.new(close_paren) : close_paren,
2592
+ options
2593
+ )
2594
+ end
2595
+
2596
+ rule(open_paren: sequence(:open_paren),
2597
+ factor: simple(:factor),
2598
+ paren_close_prefix: simple(:close_prefix)) do
2599
+ options = {}
2600
+ if open_paren.first.is_a?(Math::Number)
2601
+ mask = "#{1.25**open_paren.first.value.to_i}em"
2602
+ options[:open_prefixed] = true
2603
+ options[:open_paren] = { minsize: mask, maxsize: mask } unless open_paren.first.value == ""
2604
+ end
2605
+ Math::Function::Fenced.new(
2606
+ Math::Symbol.new(open_paren.last),
2607
+ [factor],
2608
+ Math::Symbol.new(close_prefix),
2609
+ options
2610
+ )
2611
+ end
2612
+
2613
+ rule(open_paren: sequence(:open_paren),
2614
+ frac: simple(:frac),
2615
+ close_paren: sequence(:close_paren)) do
2616
+ options = {}
2617
+ if open_paren.first.is_a?(Math::Number)
2618
+ mask = "#{1.25**open_paren.first.value.to_i}em"
2619
+ options[:open_prefixed] = true
2620
+ options[:open_paren] = { minsize: mask, maxsize: mask } unless open_paren.first.value == ""
2621
+ end
2622
+ if close_paren.first.is_a?(Math::Number)
2623
+ mask = "#{1.25**close_paren.first.value.to_i}em"
2624
+ options[:close_prefixed] = true
2625
+ options[:close_paren] = { minsize: mask, maxsize: mask } unless close_paren.first.value == ""
2626
+ end
2627
+ Math::Function::Fenced.new(
2628
+ Math::Symbol.new(open_paren.last),
2629
+ [frac],
2630
+ Math::Symbol.new(close_paren.last),
2631
+ options
2632
+ )
2633
+ end
2634
+
2635
+ rule(open_paren: simple(:open_paren),
2636
+ frac: simple(:frac),
2637
+ close_paren: sequence(:close_paren)) do
2638
+ options = {}
2639
+ if close_paren.first.is_a?(Math::Number)
2640
+ mask = "#{1.25**close_paren.first.value.to_i}em"
2641
+ options[:close_prefixed] = true
2642
+ options[:close_paren] = { minsize: mask, maxsize: mask } unless close_paren.first.value == ""
2643
+ end
2644
+ Math::Function::Fenced.new(
2645
+ open_paren.is_a?(Slice) ? Math::Symbol.new(open_paren) : open_paren,
2646
+ [frac],
2647
+ Math::Symbol.new(close_paren.last),
2648
+ options
2649
+ )
2650
+ end
2651
+
2652
+ rule(open_paren: sequence(:open_paren),
2653
+ factor: simple(:factor),
2654
+ close_paren: sequence(:close_paren)) do
2655
+ options = {}
2656
+ if open_paren.first.is_a?(Math::Number)
2657
+ mask = "#{1.25**open_paren.first.value.to_i}em"
2658
+ options[:open_prefixed] = true
2659
+ options[:open_paren] = { minsize: mask, maxsize: mask } unless open_paren.first.value == ""
2660
+ end
2661
+ if close_paren.first.is_a?(Math::Number)
2662
+ mask = "#{1.25**close_paren.first.value.to_i}em"
2663
+ options[:close_prefixed] = true
2664
+ options[:close_paren] = { minsize: mask, maxsize: mask } unless close_paren.first.value == ""
2665
+ end
2666
+ Math::Function::Fenced.new(
2667
+ Math::Symbol.new(open_paren.last),
2668
+ [factor],
2669
+ Math::Symbol.new(close_paren.last),
2670
+ options
2671
+ )
2672
+ end
2673
+
2674
+ rule(open_paren: simple(:open_paren),
2675
+ negated_operator: simple(:operator),
2676
+ close_paren: simple(:close_paren)) do
2677
+ Math::Function::Fenced.new(
2678
+ open_paren.is_a?(Slice) ? Math::Symbol.new(open_paren) : open_paren,
2679
+ [
2680
+ Math::Formula.new([
2681
+ Math::Symbol.new(operator),
2682
+ Math::Symbol.new("&#x338;"),
2683
+ ])
2684
+ ],
2685
+ close_paren.is_a?(Slice) ? Math::Symbol.new(close_paren) : close_paren,
2686
+ )
2687
+ end
2688
+
2689
+ rule(open_paren: simple(:open_paren),
2690
+ table: simple(:table),
2691
+ paren_close_prefix: simple(:close_paren)) do
2692
+ table.open_paren = open_paren.to_s
2693
+ table.close_paren = close_paren.to_s
2694
+ table
2695
+ end
2696
+
2697
+ rule(open_paren: simple(:open_paren),
2698
+ table: simple(:table),
2699
+ close_paren: simple(:close_paren)) do
2700
+ table.open_paren = open_paren.to_s
2701
+ table.close_paren = close_paren.to_s
2702
+ table
2703
+ end
2704
+
2705
+ rule(intermediate_exp: simple(:intermediate_exp),
2706
+ operator: simple(:operator),
2707
+ expr: simple(:expr)) do
2708
+ [
2709
+ intermediate_exp,
2710
+ Math::Symbol.new(operator),
2711
+ expr,
2712
+ ]
2713
+ end
2714
+
2715
+ rule(atom: simple(:atom),
2716
+ operator: simple(:operator),
2717
+ frac: simple(:frac)) do
2718
+ [
2719
+ atom,
2720
+ Math::Symbol.new(operator),
2721
+ frac,
2722
+ ]
2723
+ end
2724
+
2725
+ rule(operator: simple(:operator),
2726
+ frac: simple(:frac),
2727
+ expr: sequence(:expr)) do
2728
+ [
2729
+ Math::Symbol.new(operator),
2730
+ frac,
2731
+ ] + expr
2732
+ end
2733
+
2734
+ rule(nary_class: simple(:nary_class),
2735
+ sub: simple(:sub),
2736
+ sup: simple(:sup)) do
2737
+ nary_function = if Constants::NARY_CLASSES.key?(nary_class.to_sym)
2738
+ nary_class
2739
+ else
2740
+ (Constants::NARY_CLASSES.invert[nary_class.to_s] || Constants::NARY_SYMBOLS[nary_class.to_sym] || nary_class)
2741
+ end
2742
+ if Constants::NARY_CLASSES.key?(nary_function.to_sym)
2743
+ new_sub = (sub.class_name == "underset" ? sub.parameter_one : Utility.unfenced_value(sub, paren_specific: true))
2744
+ new_sup = (sup.class_name == "overset" ? sup.parameter_one : Utility.unfenced_value(sup, paren_specific: true))
2745
+ Utility.get_class(nary_function).new(new_sub, new_sup)
2746
+ else
2747
+ Math::Function::Nary.new(
2748
+ Math::Symbol.new(nary_function),
2749
+ Utility.unfenced_value(sub, paren_specific: true),
2750
+ Utility.unfenced_value(sup, paren_specific: true),
2751
+ )
2752
+ end
2753
+ end
2754
+
2755
+ rule(nary_class: simple(:nary_class),
2756
+ sub: sequence(:sub),
2757
+ sup: sequence(:sup)) do
2758
+ nary_function = if Constants::NARY_CLASSES.key?(nary_class.to_sym)
2759
+ nary_class
2760
+ else
2761
+ (Constants::NARY_CLASSES.invert[nary_class.to_s] || Constants::NARY_SYMBOLS[nary_class.to_sym] || nary_class)
2762
+ end
2763
+ Utility.get_class(nary_function).new(
2764
+ Utility.unfenced_value(sub, paren_specific: true),
2765
+ Utility.unfenced_value(sup, paren_specific: true),
2766
+ )
2767
+ end
2768
+
2769
+ rule(nary_class: simple(:nary_class),
2770
+ sub: simple(:sub),
2771
+ sup: sequence(:sup)) do
2772
+ nary_function = if Constants::NARY_CLASSES.key?(nary_class.to_sym)
2773
+ nary_class
2774
+ else
2775
+ (Constants::NARY_CLASSES.invert[nary_class.to_s] || Constants::NARY_SYMBOLS[nary_class.to_sym] || nary_class)
2776
+ end
2777
+ sub_value = sub.is_a?(Math::Function::Underset) ? sub.parameter_one : Utility.unfenced_value(sub, paren_specific: true)
2778
+ Utility.get_class(nary_function).new(
2779
+ sub_value,
2780
+ Utility.unfenced_value(sup, paren_specific: true),
2781
+ )
2782
+ end
2783
+
2784
+ rule(nary_class: simple(:nary_class),
2785
+ mask: simple(:mask),
2786
+ sub: simple(:sub)) do
2787
+ nary_function = if Constants::NARY_CLASSES.key?(nary_class.to_sym)
2788
+ nary_class
2789
+ else
2790
+ (Constants::NARY_CLASSES.invert[nary_class.to_s] || Constants::NARY_SYMBOLS[nary_class.to_sym] || nary_class)
2791
+ end
2792
+ sub_value = sub.is_a?(Math::Function::Underset) ? sub.parameter_one : Utility.unfenced_value(sub, paren_specific: true)
2793
+ options = { mask: mask.value }
2794
+ if Constants::NARY_CLASSES.key?(nary_function.to_sym)
2795
+ Utility.get_class(nary_function).new(
2796
+ sub_value,
2797
+ nil,
2798
+ nil,
2799
+ options
2800
+ )
2801
+ else
2802
+ Math::Function::Nary.new(
2803
+ Math::Symbol.new(nary_function),
2804
+ sub_value,
2805
+ nil,
2806
+ nil,
2807
+ options
2808
+ )
2809
+ end
2810
+ end
2811
+
2812
+ rule(nary_class: simple(:nary_class),
2813
+ mask: simple(:mask),
2814
+ sub: sequence(:sub)) do
2815
+ nary_function = if Constants::NARY_CLASSES.key?(nary_class.to_sym)
2816
+ nary_class
2817
+ else
2818
+ (Constants::NARY_CLASSES.invert[nary_class.to_s] || Constants::NARY_SYMBOLS[nary_class.to_sym] || nary_class)
2819
+ end
2820
+ options = { mask: mask.value }
2821
+ if Constants::NARY_CLASSES.key?(nary_function.to_sym)
2822
+ Utility.get_class(nary_function).new(
2823
+ Utility.unfenced_value(sub, paren_specific: true),
2824
+ nil,
2825
+ nil,
2826
+ options
2827
+ )
2828
+ else
2829
+ Math::Function::Nary.new(
2830
+ Math::Symbol.new(nary_function),
2831
+ Utility.unfenced_value(sub, paren_specific: true),
2832
+ nil,
2833
+ nil,
2834
+ options
2835
+ )
2836
+ end
2837
+ end
2838
+
2839
+ rule(nary_class: simple(:nary_class),
2840
+ mask: simple(:mask),
2841
+ sup: simple(:sup)) do
2842
+ nary_function = if Constants::NARY_CLASSES.key?(nary_class.to_sym)
2843
+ nary_class
2844
+ else
2845
+ (Constants::NARY_CLASSES.invert[nary_class.to_s] || Constants::NARY_SYMBOLS[nary_class.to_sym] || nary_class)
2846
+ end
2847
+ sup_value = sup.is_a?(Math::Function::Overset) ? sup.parameter_one : Utility.unfenced_value(sup, paren_specific: true)
2848
+ options = { mask: mask.value }
2849
+ if Constants::NARY_CLASSES.key?(nary_function.to_sym)
2850
+ Utility.get_class(nary_function).new(
2851
+ nil,
2852
+ sup_value,
2853
+ nil,
2854
+ options
2855
+ )
2856
+ else
2857
+ Math::Function::Nary.new(
2858
+ Math::Symbol.new(nary_function),
2859
+ nil,
2860
+ sup_value,
2861
+ nil,
2862
+ options
2863
+ )
2864
+ end
2865
+ end
2866
+
2867
+ rule(nary_class: simple(:nary_class),
2868
+ mask: simple(:mask),
2869
+ sup: simple(:sup)) do
2870
+ nary_function = if Constants::NARY_CLASSES.key?(nary_class.to_sym)
2871
+ nary_class
2872
+ else
2873
+ (Constants::NARY_CLASSES.invert[nary_class.to_s] || Constants::NARY_SYMBOLS[nary_class.to_sym] || nary_class)
2874
+ end
2875
+ options = { mask: mask.value }
2876
+ if Constants::NARY_CLASSES.key?(nary_function.to_sym)
2877
+ Utility.get_class(nary_function).new(
2878
+ nil,
2879
+ Utility.unfenced_value(sup, paren_specific: true),
2880
+ nil,
2881
+ options
2882
+ )
2883
+ else
2884
+ Math::Function::Nary.new(
2885
+ Math::Symbol.new(nary_function),
2886
+ nil,
2887
+ Utility.unfenced_value(sup, paren_specific: true),
2888
+ nil,
2889
+ options
2890
+ )
2891
+ end
2892
+ end
2893
+
2894
+ rule(pre_supscript: simple(:pre_sup),
2895
+ base: simple(:base),
2896
+ sub: simple(:sub)) do
2897
+ Math::Function::Multiscript.new(
2898
+ Math::Function::PowerBase.new(base, sub),
2899
+ [],
2900
+ [pre_sup],
2901
+ )
2902
+ end
2903
+
2904
+ rule(pre_supscript: simple(:pre_sup),
2905
+ pre_subscript: simple(:pre_sub),
2906
+ base: simple(:base)) do
2907
+ Math::Function::Multiscript.new(
2908
+ Math::Function::PowerBase.new(base),
2909
+ [pre_sub],
2910
+ [pre_sup],
2911
+ )
2912
+ end
2913
+
2914
+ rule(pre_subscript: simple(:pre_sub),
2915
+ base: simple(:base),
2916
+ sub: simple(:sub)) do
2917
+ Math::Function::Multiscript.new(
2918
+ Math::Function::PowerBase.new(
2919
+ base,
2920
+ Utility.unfenced_value(sub, paren_specific: true),
2921
+ ),
2922
+ [pre_sub],
2923
+ [],
2924
+ )
2925
+ end
2926
+
2927
+ rule(pre_subscript: simple(:pre_sub),
2928
+ base: simple(:base),
2929
+ sub_digits: simple(:digits)) do
2930
+ digit = Constants::SUB_DIGITS.key(digits).to_s
2931
+ number = Math::Number.new(digit, mini_sub_sized: true)
2932
+ Math::Function::Multiscript.new(
2933
+ Math::Function::PowerBase.new(base, number),
2934
+ [pre_sub],
2935
+ [],
2936
+ )
2937
+ end
2938
+
2939
+ rule(open_paren: simple(:open_paren),
2940
+ pre_script: simple(:pre_script),
2941
+ close_paren: simple(:close_paren)) do
2942
+ Math::Function::Fenced.new(
2943
+ open_paren.is_a?(Slice) ? Math::Symbol.new(open_paren) : open_paren,
2944
+ [pre_script],
2945
+ close_paren.is_a?(Slice) ? Math::Symbol.new(close_paren) : close_paren,
2946
+ )
2947
+ end
2948
+
2949
+ rule(nary_class: simple(:nary_class),
2950
+ mask: simple(:mask),
2951
+ sub: sequence(:sub),
2952
+ sup: sequence(:sup)) do
2953
+ nary_function = if Constants::NARY_CLASSES.key?(nary_class.to_sym)
2954
+ nary_class
2955
+ else
2956
+ (Constants::NARY_CLASSES.invert[nary_class.to_s] || Constants::NARY_SYMBOLS[nary_class.to_sym] || nary_class)
2957
+ end
2958
+ options = { mask: mask.value }
2959
+ if Constants::NARY_CLASSES.key?(nary_function&.to_sym)
2960
+ Utility.get_class(nary_function).new(
2961
+ Utility.filter_values(sub),
2962
+ Utility.filter_values(sup),
2963
+ options
2964
+ )
2965
+ else
2966
+ Math::Function::Nary.new(
2967
+ Math::Symbol.new(nary_function),
2968
+ Utility.filter_values(sub),
2969
+ Utility.filter_values(sup),
2970
+ nil,
2971
+ options
2972
+ )
2973
+ end
2974
+ end
2975
+
2976
+ rule(nary_class: simple(:nary_class),
2977
+ mask: simple(:mask),
2978
+ sub: simple(:sub),
2979
+ sup: sequence(:sup)) do
2980
+ nary_function = if Constants::NARY_CLASSES.key?(nary_class.to_sym)
2981
+ nary_class
2982
+ else
2983
+ (Constants::NARY_CLASSES.invert[nary_class.to_s] || Constants::NARY_SYMBOLS[nary_class.to_sym] || nary_class)
2984
+ end
2985
+ options = { mask: mask.value }
2986
+ if Constants::NARY_CLASSES.key?(nary_function&.to_sym)
2987
+ Utility.get_class(nary_function).new(
2988
+ Utility.unfenced_value(sub, paren_specific: true),
2989
+ Utility.filter_values(sup),
2990
+ options
2991
+ )
2992
+ else
2993
+ Math::Function::Nary.new(
2994
+ Math::Symbol.new(nary_function),
2995
+ Utility.unfenced_value(sub, paren_specific: true),
2996
+ Utility.filter_values(sup),
2997
+ nil,
2998
+ options
2999
+ )
3000
+ end
3001
+ end
3002
+
3003
+ rule(nary_class: simple(:nary_class),
3004
+ mask: simple(:mask),
3005
+ sub: sequence(:sub),
3006
+ sup: simple(:sup)) do
3007
+ nary_function = if Constants::NARY_CLASSES.key?(nary_class.to_sym)
3008
+ nary_class
3009
+ else
3010
+ (Constants::NARY_CLASSES.invert[nary_class.to_s] || Constants::NARY_SYMBOLS[nary_class.to_sym] || nary_class)
3011
+ end
3012
+ options = { mask: mask.value }
3013
+ if Constants::NARY_CLASSES.key?(nary_function&.to_sym)
3014
+ Utility.get_class(nary_function).new(
3015
+ Utility.filter_values(sub),
3016
+ Utility.unfenced_value(sup, paren_specific: true),
3017
+ options
3018
+ )
3019
+ else
3020
+ Math::Function::Nary.new(
3021
+ Math::Symbol.new(nary_function),
3022
+ Utility.filter_values(sub),
3023
+ Utility.unfenced_value(sup, paren_specific: true),
3024
+ nil,
3025
+ options
3026
+ )
3027
+ end
3028
+ end
3029
+
3030
+ rule(atom: simple(:atom),
3031
+ operator: simple(:operator),
3032
+ frac: simple(:frac),
3033
+ expr: sequence(:expr)) do
3034
+ [
3035
+ atom,
3036
+ Math::Symbol.new(operator),
3037
+ frac,
3038
+ ] + expr
3039
+ end
3040
+
3041
+ rule(open_paren: sequence(:open_paren),
3042
+ factor: simple(:factor),
3043
+ exp: sequence(:exp),
3044
+ close_paren: sequence(:close_paren)) do
3045
+ options = {}
3046
+ if open_paren.first.is_a?(Math::Number)
3047
+ mask = "#{1.25**open_paren.first.value.to_i}em"
3048
+ options[:open_prefixed] = true
3049
+ options[:open_paren] = { minsize: mask, maxsize: mask } unless open_paren.first.value == ""
3050
+ end
3051
+ if close_paren.first.is_a?(Math::Number)
3052
+ mask = "#{1.25**close_paren.first.value.to_i}em"
3053
+ options[:close_prefixed] = true
3054
+ options[:close_paren] = { minsize: mask, maxsize: mask } unless close_paren.first.value == ""
3055
+ end
3056
+ Math::Function::Fenced.new(
3057
+ Math::Symbol.new(open_paren.last),
3058
+ ([factor] + exp),
3059
+ Math::Symbol.new(close_paren.last),
3060
+ options
3061
+ )
3062
+ end
3063
+
3064
+ rule(open_paren: simple(:open_paren),
3065
+ unary_function: simple(:unary),
3066
+ exp: sequence(:exp),
3067
+ close_paren: simple(:close_paren)) do
3068
+ Math::Function::Fenced.new(
3069
+ open_paren.is_a?(Slice) ? Math::Symbol.new(open_paren) : open_paren,
3070
+ ([unary] + exp),
3071
+ close_paren.is_a?(Slice) ? Math::Symbol.new(close_paren) : close_paren,
3072
+ )
3073
+ end
3074
+
3075
+ rule(open_paren: simple(:open_paren),
3076
+ factor: simple(:factor),
3077
+ close_paren: simple(:close_paren),
3078
+ naryand_recursion: sequence(:naryand_recursion)) do
3079
+ new_factor = [open_paren, close_paren].include?("|") ? Utility.unfenced_value(factor, paren_specific: true) : factor
3080
+ fenced = Math::Function::Fenced.new(
3081
+ open_paren.is_a?(Slice) ? Math::Symbol.new(open_paren) : open_paren,
3082
+ [new_factor],
3083
+ close_paren.is_a?(Slice) ? Math::Symbol.new(close_paren) : close_paren,
3084
+ )
3085
+ [fenced] + naryand_recursion
3086
+ end
3087
+
3088
+ rule(open_paren: simple(:open_paren),
3089
+ factor: simple(:factor),
3090
+ close_paren: simple(:close_paren),
3091
+ naryand_recursion: simple(:naryand_recursion)) do
3092
+ new_factor = [open_paren, close_paren].include?("|") ? Utility.unfenced_value(factor, paren_specific: true) : factor
3093
+ fenced = Math::Function::Fenced.new(
3094
+ open_paren.is_a?(Slice) ? Math::Symbol.new(open_paren) : open_paren,
3095
+ [new_factor],
3096
+ close_paren.is_a?(Slice) ? Math::Symbol.new(close_paren) : close_paren,
3097
+ )
3098
+ [fenced, naryand_recursion]
3099
+ end
3100
+
3101
+ rule(open_paren: simple(:open_paren),
3102
+ mini_sub: simple(:mini_sub),
3103
+ expr: sequence(:expr),
3104
+ close_paren: simple(:close_paren)) do
3105
+ Math::Function::Fenced.new(
3106
+ open_paren.is_a?(Slice) ? Math::Symbol.new(open_paren) : open_paren,
3107
+ ([mini_sub] + expr),
3108
+ close_paren.is_a?(Slice) ? Math::Symbol.new(close_paren) : close_paren,
3109
+ )
3110
+ end
3111
+
3112
+ rule(open_paren: simple(:open_paren),
3113
+ mini_sub: simple(:mini_sub),
3114
+ exp: sequence(:exp),
3115
+ close_paren: simple(:close_paren)) do
3116
+ Math::Function::Fenced.new(
3117
+ open_paren.is_a?(Slice) ? Math::Symbol.new(open_paren) : open_paren,
3118
+ ([mini_sub] + exp),
3119
+ close_paren.is_a?(Slice) ? Math::Symbol.new(close_paren) : close_paren,
3120
+ )
3121
+ end
3122
+
3123
+ rule(open_paren: simple(:open_paren),
3124
+ mini_sub_sup: simple(:mini_sub_sup),
3125
+ exp: sequence(:exp),
3126
+ close_paren: simple(:close_paren)) do
3127
+ Math::Function::Fenced.new(
3128
+ open_paren.is_a?(Slice) ? Math::Symbol.new(open_paren) : open_paren,
3129
+ ([mini_sub_sup] + exp),
3130
+ close_paren.is_a?(Slice) ? Math::Symbol.new(close_paren) : close_paren,
3131
+ )
3132
+ end
3133
+
3134
+ rule(open_paren: simple(:open_paren),
3135
+ mini_sup: simple(:mini_sup),
3136
+ expr: sequence(:expr),
3137
+ close_paren: simple(:close_paren)) do
3138
+ Math::Function::Fenced.new(
3139
+ open_paren.is_a?(Slice) ? Math::Symbol.new(open_paren) : open_paren,
3140
+ ([mini_sup] + expr),
3141
+ close_paren.is_a?(Slice) ? Math::Symbol.new(close_paren) : close_paren,
3142
+ )
3143
+ end
3144
+
3145
+ rule(open_paren: simple(:open_paren),
3146
+ mini_sup: simple(:mini_sup),
3147
+ exp: sequence(:exp),
3148
+ close_paren: simple(:close_paren)) do
3149
+ Math::Function::Fenced.new(
3150
+ open_paren.is_a?(Slice) ? Math::Symbol.new(open_paren) : open_paren,
3151
+ ([mini_sup] + exp),
3152
+ close_paren.is_a?(Slice) ? Math::Symbol.new(close_paren) : close_paren,
3153
+ )
3154
+ end
3155
+
3156
+ rule(open_paren: simple(:open_paren),
3157
+ intermediate_exp: simple(:intermediate_exp),
3158
+ expr: sequence(:expr),
3159
+ close_paren: simple(:close_paren)) do
3160
+ Math::Function::Fenced.new(
3161
+ open_paren.is_a?(Slice) ? Math::Symbol.new(open_paren) : open_paren,
3162
+ ([intermediate_exp] + expr),
3163
+ close_paren.is_a?(Slice) ? Math::Symbol.new(close_paren) : close_paren,
3164
+ )
3165
+ end
3166
+
3167
+ rule(open_paren: simple(:open_paren),
3168
+ factor: simple(:factor),
3169
+ expr: simple(:expr),
3170
+ close_paren: simple(:close_paren)) do
3171
+ Math::Function::Fenced.new(
3172
+ open_paren.is_a?(Slice) ? Math::Symbol.new(open_paren) : open_paren,
3173
+ [factor, expr],
3174
+ close_paren.is_a?(Slice) ? Math::Symbol.new(close_paren) : close_paren,
3175
+ )
3176
+ end
3177
+
3178
+ rule(open_paren: simple(:open_paren),
3179
+ factor: simple(:factor),
3180
+ exp: simple(:exp),
3181
+ close_paren: simple(:close_paren)) do
3182
+ Math::Function::Fenced.new(
3183
+ open_paren.is_a?(Slice) ? Math::Symbol.new(open_paren) : open_paren,
3184
+ [factor, exp],
3185
+ close_paren.is_a?(Slice) ? Math::Symbol.new(close_paren) : close_paren,
3186
+ )
3187
+ end
3188
+
3189
+ rule(open_paren: simple(:open_paren),
3190
+ accents: subtree(:accent),
3191
+ expr: sequence(:expr),
3192
+ close_paren: simple(:close_paren)) do
3193
+ Math::Function::Fenced.new(
3194
+ open_paren.is_a?(Slice) ? Math::Symbol.new(open_paren) : open_paren,
3195
+ ([Utility.unicode_accents(accent)] + expr),
3196
+ close_paren.is_a?(Slice) ? Math::Symbol.new(close_paren) : close_paren,
3197
+ )
3198
+ end
3199
+
3200
+ rule(open_paren: simple(:open_paren),
3201
+ accents: subtree(:accent),
3202
+ exp: sequence(:exp),
3203
+ close_paren: simple(:close_paren)) do
3204
+ Math::Function::Fenced.new(
3205
+ open_paren.is_a?(Slice) ? Math::Symbol.new(open_paren) : open_paren,
3206
+ ([Utility.unicode_accents(accent)] + exp),
3207
+ close_paren.is_a?(Slice) ? Math::Symbol.new(close_paren) : close_paren,
3208
+ )
3209
+ end
3210
+
3211
+ rule(open_paren: simple(:open_paren),
3212
+ unicode_fractions: simple(:fraction),
3213
+ expr: sequence(:expr),
3214
+ close_paren: simple(:close_paren)) do
3215
+ Math::Function::Fenced.new(
3216
+ open_paren.is_a?(Slice) ? Math::Symbol.new(open_paren) : open_paren,
3217
+ ([Utility.unicode_fractions(fraction)] + expr),
3218
+ close_paren.is_a?(Slice) ? Math::Symbol.new(close_paren) : close_paren,
3219
+ )
3220
+ end
3221
+
3222
+ rule(open_paren: simple(:open_paren),
3223
+ unicode_fractions: simple(:fraction),
3224
+ exp: sequence(:exp),
3225
+ close_paren: simple(:close_paren)) do
3226
+ Math::Function::Fenced.new(
3227
+ open_paren.is_a?(Slice) ? Math::Symbol.new(open_paren) : open_paren,
3228
+ ([Utility.unicode_fractions(fraction)] + exp),
3229
+ close_paren.is_a?(Slice) ? Math::Symbol.new(close_paren) : close_paren,
3230
+ )
3231
+ end
3232
+
3233
+ rule(open_paren: simple(:open_paren),
3234
+ unicode_symbols: simple(:symbol),
3235
+ exp: sequence(:exp),
3236
+ close_paren: simple(:close_paren)) do
3237
+ Math::Function::Fenced.new(
3238
+ open_paren.is_a?(Slice) ? Math::Symbol.new(open_paren) : open_paren,
3239
+ ([Math::Symbol.new(symbol)] + exp),
3240
+ close_paren.is_a?(Slice) ? Math::Symbol.new(close_paren) : close_paren,
3241
+ )
3242
+ end
3243
+
3244
+ rule(open_paren: simple(:open_paren),
3245
+ unicode_symbols: simple(:symbol),
3246
+ exp: simple(:exp),
3247
+ close_paren: simple(:close_paren)) do
3248
+ Math::Function::Fenced.new(
3249
+ open_paren.is_a?(Slice) ? Math::Symbol.new(open_paren) : open_paren,
3250
+ [Math::Symbol.new(symbol), exp],
3251
+ close_paren.is_a?(Slice) ? Math::Symbol.new(close_paren) : close_paren,
3252
+ )
3253
+ end
3254
+
3255
+ rule(open_paren: simple(:open_paren),
3256
+ operator: simple(:operator),
3257
+ expr: simple(:expr),
3258
+ close_paren: simple(:close_paren)) do
3259
+ Math::Function::Fenced.new(
3260
+ open_paren.is_a?(Slice) ? Math::Symbol.new(open_paren) : open_paren,
3261
+ [Math::Symbol.new(operator), expr],
3262
+ close_paren.is_a?(Slice) ? Math::Symbol.new(close_paren) : close_paren,
3263
+ )
3264
+ end
3265
+
3266
+ rule(open_paren: simple(:open_paren),
3267
+ sub_exp: simple(:sub_exp),
3268
+ expr: sequence(:expr),
3269
+ close_paren: simple(:close_paren)) do
3270
+ Math::Function::Fenced.new(
3271
+ open_paren.is_a?(Slice) ? Math::Symbol.new(open_paren) : open_paren,
3272
+ ([sub_exp] + expr),
3273
+ close_paren.is_a?(Slice) ? Math::Symbol.new(close_paren) : close_paren,
3274
+ )
3275
+ end
3276
+
3277
+ rule(open_paren: simple(:open_paren),
3278
+ sub_exp: simple(:sub_exp),
3279
+ exp: sequence(:exp),
3280
+ close_paren: simple(:close_paren)) do
3281
+ Math::Function::Fenced.new(
3282
+ open_paren.is_a?(Slice) ? Math::Symbol.new(open_paren) : open_paren,
3283
+ ([sub_exp] + exp),
3284
+ close_paren.is_a?(Slice) ? Math::Symbol.new(close_paren) : close_paren,
3285
+ )
3286
+ end
3287
+
3288
+ rule(open_paren: simple(:open_paren),
3289
+ sub_exp: simple(:sub_exp),
3290
+ expr: simple(:expr),
3291
+ close_paren: simple(:close_paren)) do
3292
+ Math::Function::Fenced.new(
3293
+ open_paren.is_a?(Slice) ? Math::Symbol.new(open_paren) : open_paren,
3294
+ ([sub_exp, expr]),
3295
+ close_paren.is_a?(Slice) ? Math::Symbol.new(close_paren) : close_paren,
3296
+ )
3297
+ end
3298
+
3299
+ rule(open_paren: simple(:open_paren),
3300
+ sub_exp: simple(:sub_exp),
3301
+ exp: simple(:exp),
3302
+ close_paren: simple(:close_paren)) do
3303
+ Math::Function::Fenced.new(
3304
+ open_paren.is_a?(Slice) ? Math::Symbol.new(open_paren) : open_paren,
3305
+ ([sub_exp, exp]),
3306
+ close_paren.is_a?(Slice) ? Math::Symbol.new(close_paren) : close_paren,
3307
+ )
3308
+ end
3309
+
3310
+ rule(open_paren: simple(:open_paren),
3311
+ sup_exp: simple(:sup_exp),
3312
+ expr: simple(:expr),
3313
+ close_paren: simple(:close_paren)) do
3314
+ Math::Function::Fenced.new(
3315
+ open_paren.is_a?(Slice) ? Math::Symbol.new(open_paren) : open_paren,
3316
+ ([sup_exp, expr]),
3317
+ close_paren.is_a?(Slice) ? Math::Symbol.new(close_paren) : close_paren,
3318
+ )
3319
+ end
3320
+
3321
+ rule(open_paren: simple(:open_paren),
3322
+ sup_exp: simple(:sup_exp),
3323
+ exp: simple(:exp),
3324
+ close_paren: simple(:close_paren)) do
3325
+ Math::Function::Fenced.new(
3326
+ open_paren.is_a?(Slice) ? Math::Symbol.new(open_paren) : open_paren,
3327
+ ([sup_exp, exp]),
3328
+ close_paren.is_a?(Slice) ? Math::Symbol.new(close_paren) : close_paren,
3329
+ )
3330
+ end
3331
+
3332
+ rule(open_paren: simple(:open_paren),
3333
+ monospace: simple(:monospace),
3334
+ exp: sequence(:exp),
3335
+ close_paren: simple(:close_paren)) do
3336
+ Math::Function::Fenced.new(
3337
+ open_paren.is_a?(Slice) ? Math::Symbol.new(open_paren) : open_paren,
3338
+ ([monospace] + exp),
3339
+ close_paren.is_a?(Slice) ? Math::Symbol.new(close_paren) : close_paren,
3340
+ )
3341
+ end
3342
+
3343
+ rule(open_paren: simple(:open_paren),
3344
+ frac: simple(:frac),
3345
+ exp: simple(:exp),
3346
+ close_paren: simple(:close_paren)) do
3347
+ Math::Function::Fenced.new(
3348
+ open_paren.is_a?(Slice) ? Math::Symbol.new(open_paren) : open_paren,
3349
+ ([frac, exp]),
3350
+ close_paren.is_a?(Slice) ? Math::Symbol.new(close_paren) : close_paren,
3351
+ )
3352
+ end
3353
+
3354
+ rule(open_paren: simple(:open_paren),
3355
+ frac: simple(:frac),
3356
+ exp: sequence(:exp),
3357
+ close_paren: simple(:close_paren)) do
3358
+ Math::Function::Fenced.new(
3359
+ open_paren.is_a?(Slice) ? Math::Symbol.new(open_paren) : open_paren,
3360
+ ([frac] + exp),
3361
+ close_paren.is_a?(Slice) ? Math::Symbol.new(close_paren) : close_paren,
3362
+ )
3363
+ end
3364
+
3365
+ rule(open_paren: simple(:open_paren),
3366
+ sup_exp: simple(:sup_exp),
3367
+ exp: sequence(:exp),
3368
+ close_paren: simple(:close_paren)) do
3369
+ Math::Function::Fenced.new(
3370
+ open_paren.is_a?(Slice) ? Math::Symbol.new(open_paren) : open_paren,
3371
+ ([sup_exp] + exp),
3372
+ close_paren.is_a?(Slice) ? Math::Symbol.new(close_paren) : close_paren,
3373
+ )
3374
+ end
3375
+
3376
+ rule(open_paren: simple(:open_paren),
3377
+ subsup_exp: simple(:subsup_exp),
3378
+ expr: sequence(:expr),
3379
+ close_paren: simple(:close_paren)) do
3380
+ Math::Function::Fenced.new(
3381
+ open_paren.is_a?(Slice) ? Math::Symbol.new(open_paren) : open_paren,
3382
+ ([subsup_exp] + expr),
3383
+ close_paren.is_a?(Slice) ? Math::Symbol.new(close_paren) : close_paren,
3384
+ )
3385
+ end
3386
+
3387
+ rule(open_paren: simple(:open_paren),
3388
+ subsup_exp: simple(:subsup_exp),
3389
+ exp: sequence(:exp),
3390
+ close_paren: simple(:close_paren)) do
3391
+ Math::Function::Fenced.new(
3392
+ open_paren.is_a?(Slice) ? Math::Symbol.new(open_paren) : open_paren,
3393
+ ([subsup_exp] + exp),
3394
+ close_paren.is_a?(Slice) ? Math::Symbol.new(close_paren) : close_paren,
3395
+ )
3396
+ end
3397
+
3398
+ rule(open_paren: simple(:open_paren),
3399
+ factor: simple(:factor),
3400
+ expr: sequence(:expr),
3401
+ close_paren: simple(:close_paren)) do
3402
+ Math::Function::Fenced.new(
3403
+ open_paren.is_a?(Slice) ? Math::Symbol.new(open_paren) : open_paren,
3404
+ ([factor] + expr),
3405
+ close_paren.is_a?(Slice) ? Math::Symbol.new(close_paren) : close_paren,
3406
+ )
3407
+ end
3408
+
3409
+ rule(open_paren: simple(:open_paren),
3410
+ factor: simple(:factor),
3411
+ exp: sequence(:exp),
3412
+ close_paren: simple(:close_paren)) do
3413
+ Math::Function::Fenced.new(
3414
+ open_paren.is_a?(Slice) ? Math::Symbol.new(open_paren) : open_paren,
3415
+ ([factor] + exp),
3416
+ close_paren.is_a?(Slice) ? Math::Symbol.new(close_paren) : close_paren,
3417
+ )
3418
+ end
3419
+
3420
+ rule(open_paren: simple(:open_paren),
3421
+ factor: sequence(:factor),
3422
+ expr: sequence(:expr),
3423
+ close_paren: simple(:close_paren)) do
3424
+ Math::Function::Fenced.new(
3425
+ open_paren.is_a?(Slice) ? Math::Symbol.new(open_paren) : open_paren,
3426
+ (factor + expr),
3427
+ close_paren.is_a?(Slice) ? Math::Symbol.new(close_paren) : close_paren,
3428
+ )
3429
+ end
3430
+
3431
+ rule(open_paren: simple(:open_paren),
3432
+ symbol: simple(:symbol),
3433
+ expr: sequence(:expr),
3434
+ close_paren: simple(:close_paren)) do
3435
+ Math::Function::Fenced.new(
3436
+ open_paren.is_a?(Slice) ? Math::Symbol.new(open_paren) : open_paren,
3437
+ ([Math::Symbol.new(symbol)] + expr),
3438
+ close_paren.is_a?(Slice) ? Math::Symbol.new(close_paren) : close_paren,
3439
+ )
3440
+ end
3441
+
3442
+ rule(open_paren: simple(:open_paren),
3443
+ factor: sequence(:factor),
3444
+ exp: sequence(:exp),
3445
+ close_paren: simple(:close_paren)) do
3446
+ Math::Function::Fenced.new(
3447
+ open_paren.is_a?(Slice) ? Math::Symbol.new(open_paren) : open_paren,
3448
+ (factor + exp),
3449
+ close_paren.is_a?(Slice) ? Math::Symbol.new(close_paren) : close_paren,
3450
+ )
3451
+ end
3452
+
3453
+ rule(open_paren: simple(:open_paren),
3454
+ factor: sequence(:factor),
3455
+ close_paren: simple(:close_paren)) do
3456
+ Math::Function::Fenced.new(
3457
+ open_paren.is_a?(Slice) ? Math::Symbol.new(open_paren) : open_paren,
3458
+ factor,
3459
+ close_paren.is_a?(Slice) ? Math::Symbol.new(close_paren) : close_paren,
3460
+ )
3461
+ end
3462
+
3463
+ rule(open_paren: simple(:open_paren),
3464
+ factor: simple(:factor),
3465
+ operand: simple(:operand),
3466
+ close_paren: simple(:close_paren)) do
3467
+ Math::Function::Fenced.new(
3468
+ open_paren.is_a?(Slice) ? Math::Symbol.new(open_paren) : open_paren,
3469
+ ([factor, operand]),
3470
+ close_paren.is_a?(Slice) ? Math::Symbol.new(close_paren) : close_paren,
3471
+ )
3472
+ end
3473
+
3474
+ rule(open_paren: simple(:open_paren),
3475
+ factor: simple(:factor),
3476
+ operand: sequence(:operand),
3477
+ close_paren: simple(:close_paren)) do
3478
+ Math::Function::Fenced.new(
3479
+ open_paren.is_a?(Slice) ? Math::Symbol.new(open_paren) : open_paren,
3480
+ ([factor] + operand),
3481
+ close_paren.is_a?(Slice) ? Math::Symbol.new(close_paren) : close_paren,
3482
+ )
3483
+ end
3484
+
3485
+ rule(open_paren: simple(:open_paren),
3486
+ factor: sequence(:factor),
3487
+ operand: sequence(:operand),
3488
+ close_paren: simple(:close_paren)) do
3489
+ Math::Function::Fenced.new(
3490
+ open_paren.is_a?(Slice) ? Math::Symbol.new(open_paren) : open_paren,
3491
+ (factor + operand),
3492
+ close_paren.is_a?(Slice) ? Math::Symbol.new(close_paren) : close_paren,
3493
+ )
3494
+ end
3495
+
3496
+ rule(open_paren: simple(:open_paren),
3497
+ text: simple(:text),
3498
+ operand: simple(:operand),
3499
+ exp: simple(:exp),
3500
+ close_paren: simple(:close_paren)) do
3501
+ Math::Function::Fenced.new(
3502
+ open_paren.is_a?(Slice) ? Math::Symbol.new(open_paren) : open_paren,
3503
+ [Math::Function::Text.new(text), operand, exp],
3504
+ close_paren.is_a?(Slice) ? Math::Symbol.new(close_paren) : close_paren,
3505
+ )
3506
+ end
3507
+
3508
+ rule(open_paren: simple(:open_paren),
3509
+ text: simple(:text),
3510
+ operand: simple(:operand),
3511
+ exp: sequence(:exp),
3512
+ close_paren: simple(:close_paren)) do
3513
+ Math::Function::Fenced.new(
3514
+ open_paren.is_a?(Slice) ? Math::Symbol.new(open_paren) : open_paren,
3515
+ [Math::Function::Text.new(text), operand] + exp,
3516
+ close_paren.is_a?(Slice) ? Math::Symbol.new(close_paren) : close_paren,
3517
+ )
3518
+ end
3519
+
3520
+ rule(nary_class: simple(:nary_class),
3521
+ mask: simple(:mask),
3522
+ sub: simple(:sub),
3523
+ sup: simple(:sup)) do
3524
+ nary_function = if Constants::NARY_CLASSES.key?(nary_class.to_sym)
3525
+ nary_class
3526
+ else
3527
+ (Constants::NARY_CLASSES.invert[nary_class.to_s] || Constants::NARY_SYMBOLS[nary_class.to_sym] || nary_class)
3528
+ end
3529
+ sub_value = sub.is_a?(Math::Function::Underset) ? sub.parameter_one : Utility.unfenced_value(sub, paren_specific: true)
3530
+ sup_value = sup.is_a?(Math::Function::Overset) ? sup.parameter_one : Utility.unfenced_value(sup, paren_specific: true)
3531
+ options = { mask: mask.value }
3532
+ if Constants::NARY_CLASSES.key?(nary_function.to_sym)
3533
+ Utility.get_class(nary_function).new(
3534
+ sub_value,
3535
+ sup_value,
3536
+ nil,
3537
+ options
3538
+ )
3539
+ else
3540
+ Math::Function::Nary.new(
3541
+ Math::Symbol.new(nary_function),
3542
+ sub_value,
3543
+ sup_value,
3544
+ nil,
3545
+ options
3546
+ )
3547
+ end
3548
+ end
3549
+
3550
+ rule(open_paren: simple(:open_paren),
3551
+ operator: simple(:operator),
3552
+ expr: sequence(:expr),
3553
+ close_paren: simple(:close_paren)) do
3554
+ Math::Function::Fenced.new(
3555
+ open_paren.is_a?(Slice) ? Math::Symbol.new(open_paren) : open_paren,
3556
+ ([Math::Symbol.new(operator)] + expr),
3557
+ close_paren.is_a?(Slice) ? Math::Symbol.new(close_paren) : close_paren,
3558
+ )
3559
+ end
3560
+
3561
+ rule(open_paren: simple(:open_paren),
3562
+ operator: simple(:operator),
3563
+ exp: sequence(:exp),
3564
+ close_paren: simple(:close_paren)) do
3565
+ Math::Function::Fenced.new(
3566
+ open_paren.is_a?(Slice) ? Math::Symbol.new(open_paren) : open_paren,
3567
+ ([Math::Symbol.new(operator)] + exp),
3568
+ close_paren.is_a?(Slice) ? Math::Symbol.new(close_paren) : close_paren,
3569
+ )
3570
+ end
3571
+
3572
+ rule(pre_subscript: simple(:pre_sub),
3573
+ pre_supscript: simple(:pre_sup),
3574
+ base: simple(:base),
3575
+ sub: simple(:sub)) do
3576
+ Math::Function::Multiscript.new(
3577
+ Math::Function::PowerBase.new(
3578
+ base,
3579
+ Utility.unfenced_value(sub, paren_specific: true),
3580
+ ),
3581
+ [Utility.unfenced_value(pre_sub, paren_specific: true)],
3582
+ [Utility.unfenced_value(pre_sup, paren_specific: true)],
3583
+ )
3584
+ end
3585
+
3586
+ rule(open_paren: simple(:open_paren),
3587
+ operator: simple(:operator),
3588
+ exp: simple(:exp),
3589
+ close_paren: simple(:close_paren)) do
3590
+ Math::Function::Fenced.new(
3591
+ open_paren.is_a?(Slice) ? Math::Symbol.new(open_paren) : open_paren,
3592
+ ([Math::Symbol.new(operator), exp]),
3593
+ close_paren.is_a?(Slice) ? Math::Symbol.new(close_paren) : close_paren,
3594
+ )
3595
+ end
3596
+
3597
+ rule(open_paren: simple(:open_paren),
3598
+ pre_subscript: simple(:pre_sub),
3599
+ close_paren: simple(:close_paren),
3600
+ base: simple(:base)) do
3601
+ Math::Function::Multiscript.new(
3602
+ Math::Function::PowerBase.new(base),
3603
+ [pre_sub],
3604
+ [],
3605
+ )
3606
+ end
3607
+
3608
+ rule(open_paren: simple(:open_paren),
3609
+ operator: simple(:operator),
3610
+ expr: simple(:expr),
3611
+ close_paren: simple(:close_paren),
3612
+ sup: simple(:sup)) do
3613
+ fenced = Math::Function::Fenced.new(
3614
+ open_paren.is_a?(Slice) ? Math::Symbol.new(open_paren) : open_paren,
3615
+ [Math::Symbol.new(operator), expr],
3616
+ close_paren.is_a?(Slice) ? Math::Symbol.new(close_paren) : close_paren,
3617
+ )
3618
+ Math::Function::Power.new(fenced, sup)
3619
+ end
3620
+
3621
+ rule(open_paren: simple(:open_paren),
3622
+ factor: simple(:factor),
3623
+ sup_exp: simple(:sup_exp),
3624
+ exp: sequence(:exp),
3625
+ close_paren: simple(:close_paren)) do
3626
+ Math::Function::Fenced.new(
3627
+ open_paren.is_a?(Slice) ? Math::Symbol.new(open_paren) : open_paren,
3628
+ ([factor, sup_exp] + exp),
3629
+ close_paren.is_a?(Slice) ? Math::Symbol.new(close_paren) : close_paren,
3630
+ )
3631
+ end
3632
+
3633
+ rule(open_paren: simple(:open_paren),
3634
+ sub_exp: simple(:sub_exp),
3635
+ exp: simple(:exp),
3636
+ close_paren: simple(:close_paren),
3637
+ sup: simple(:sup)) do
3638
+ fenced = Math::Function::Fenced.new(
3639
+ open_paren.is_a?(Slice) ? Math::Symbol.new(open_paren) : open_paren,
3640
+ ([sub_exp, exp]),
3641
+ close_paren.is_a?(Slice) ? Math::Symbol.new(close_paren) : close_paren,
3642
+ )
3643
+ Math::Function::Power.new(
3644
+ fenced,
3645
+ Utility.unfenced_value(sup, paren_specific: true)
3646
+ )
3647
+ end
3648
+
3649
+ rule(open_paren: simple(:open_paren),
3650
+ sub_exp: simple(:sub_exp),
3651
+ exp: sequence(:exp),
3652
+ close_paren: simple(:close_paren),
3653
+ sup: simple(:sup)) do
3654
+ fenced = Math::Function::Fenced.new(
3655
+ open_paren.is_a?(Slice) ? Math::Symbol.new(open_paren) : open_paren,
3656
+ ([sub_exp] + exp),
3657
+ close_paren.is_a?(Slice) ? Math::Symbol.new(close_paren) : close_paren,
3658
+ )
3659
+ Math::Function::Power.new(
3660
+ fenced,
3661
+ Utility.unfenced_value(sup, paren_specific: true)
3662
+ )
3663
+ end
3664
+
3665
+ rule(open_paren: simple(:open_paren),
3666
+ operator: simple(:operator),
3667
+ exp: simple(:exp),
3668
+ close_paren: simple(:close_paren),
3669
+ sup: simple(:sup)) do
3670
+ fenced = Math::Function::Fenced.new(
3671
+ open_paren.is_a?(Slice) ? Math::Symbol.new(open_paren) : open_paren,
3672
+ [Math::Symbol.new(operator), exp],
3673
+ close_paren.is_a?(Slice) ? Math::Symbol.new(close_paren) : close_paren,
3674
+ )
3675
+ Math::Function::Power.new(fenced, sup)
3676
+ end
3677
+
3678
+ rule(open_paren: simple(:open_paren),
3679
+ pre_subscript: simple(:pre_sub),
3680
+ close_paren: simple(:close_paren),
3681
+ base: simple(:base),
3682
+ sub: simple(:sub)) do
3683
+ Math::Function::Multiscript.new(
3684
+ Math::Function::PowerBase.new(base, sub),
3685
+ [pre_sub],
3686
+ [],
3687
+ )
3688
+ end
3689
+
3690
+ rule(open_paren: simple(:open_paren),
3691
+ factor: simple(:factor),
3692
+ operand: simple(:operand),
3693
+ expr: sequence(:expr),
3694
+ close_paren: simple(:close_paren)) do
3695
+ Math::Function::Fenced.new(
3696
+ open_paren.is_a?(Slice) ? Math::Symbol.new(open_paren) : open_paren,
3697
+ ([factor, operand] + expr),
3698
+ close_paren.is_a?(Slice) ? Math::Symbol.new(close_paren) : close_paren,
3699
+ )
3700
+ end
3701
+
3702
+ rule(open_paren: simple(:open_paren),
3703
+ factor: simple(:factor),
3704
+ operand: simple(:operand),
3705
+ exp: sequence(:exp),
3706
+ close_paren: simple(:close_paren)) do
3707
+ Math::Function::Fenced.new(
3708
+ open_paren.is_a?(Slice) ? Math::Symbol.new(open_paren) : open_paren,
3709
+ ([factor, operand] + exp),
3710
+ close_paren.is_a?(Slice) ? Math::Symbol.new(close_paren) : close_paren,
3711
+ )
3712
+ end
3713
+
3714
+ rule(open_paren: simple(:open_paren),
3715
+ factor: simple(:factor),
3716
+ operand: simple(:operand),
3717
+ expr: simple(:expr),
3718
+ close_paren: simple(:close_paren)) do
3719
+ Math::Function::Fenced.new(
3720
+ open_paren.is_a?(Slice) ? Math::Symbol.new(open_paren) : open_paren,
3721
+ ([factor, operand, expr]),
3722
+ close_paren.is_a?(Slice) ? Math::Symbol.new(close_paren) : close_paren,
3723
+ )
3724
+ end
3725
+
3726
+ rule(open_paren: simple(:open_paren),
3727
+ factor: simple(:factor),
3728
+ sub_exp: sequence(:sub_exp),
3729
+ exp: simple(:exp),
3730
+ close_paren: simple(:close_paren)) do
3731
+ Math::Function::Fenced.new(
3732
+ open_paren.is_a?(Slice) ? Math::Symbol.new(open_paren) : open_paren,
3733
+ ([factor] + sub_exp + [exp]),
3734
+ close_paren.is_a?(Slice) ? Math::Symbol.new(close_paren) : close_paren,
3735
+ )
3736
+ end
3737
+
3738
+ rule(open_paren: simple(:open_paren),
3739
+ monospace: simple(:monospace),
3740
+ relational_symbols: simple(:symbol),
3741
+ expr: simple(:expr),
3742
+ exp: sequence(:exp),
3743
+ close_paren: simple(:close_paren)) do
3744
+ relational = Math::Symbol.new(
3745
+ (Constants::RELATIONAL_SYMBOLS[symbol.to_sym] || symbol),
3746
+ )
3747
+ Math::Function::Fenced.new(
3748
+ open_paren.is_a?(Slice) ? Math::Symbol.new(open_paren) : open_paren,
3749
+ ([monospace, relational, expr] + exp),
3750
+ close_paren.is_a?(Slice) ? Math::Symbol.new(close_paren) : close_paren,
3751
+ )
3752
+ end
3753
+
3754
+ rule(pre_subscript: simple(:pre_sub),
3755
+ pre_supscript: simple(:pre_sup),
3756
+ base: simple(:base),
3757
+ sub: simple(:sub),
3758
+ sup: simple(:sup)) do
3759
+ Math::Function::Multiscript.new(
3760
+ Math::Function::PowerBase.new(
3761
+ base,
3762
+ Utility.unfenced_value(sub, paren_specific: true),
3763
+ Utility.unfenced_value(sup, paren_specific: true),
3764
+ ),
3765
+ [Utility.unfenced_value(pre_sub, paren_specific: true)],
3766
+ [Utility.unfenced_value(pre_sup, paren_specific: true)],
3767
+ )
3768
+ end
3769
+
3770
+ rule(intent: simple(:intent),
3771
+ open_paren: simple(:open_paren),
3772
+ intent_arguments: simple(:args),
3773
+ first_value: simple(:value),
3774
+ close_paren: simple(:close_paren)) do
3775
+ Math::Function::Intent.new(value, args)
3776
+ end
3777
+
3778
+ rule(intent: simple(:intent),
3779
+ open_paren: simple(:open_paren),
3780
+ intent_arguments: simple(:args),
3781
+ first_value: sequence(:value),
3782
+ close_paren: simple(:close_paren)) do
3783
+ Math::Function::Intent.new(Utility.filter_values(value), args)
3784
+ end
3785
+
3786
+ rule(paren_open_prefix: simple(:open_paren),
3787
+ factor: simple(:factor),
3788
+ accents_subsup: simple(:subsup),
3789
+ exp: simple(:exp),
3790
+ paren_close_prefix: simple(:close_paren)) do
3791
+ Math::Function::Fenced.new(
3792
+ Math::Symbol.new(open_paren),
3793
+ [factor, subsup, exp],
3794
+ Math::Symbol.new(close_paren),
3795
+ )
3796
+ end
3797
+
3798
+ rule(open_paren: simple(:open_paren),
3799
+ pre_subscript: simple(:pre_sub),
3800
+ pre_supscript: simple(:pre_sup),
3801
+ close_paren: simple(:close_paren),
3802
+ base: simple(:base)) do
3803
+ Math::Function::Multiscript.new(
3804
+ Math::Function::PowerBase.new(
3805
+ base,
3806
+ ),
3807
+ [pre_sub],
3808
+ [pre_sup],
3809
+ )
3810
+ end
3811
+
3812
+ rule(open_paren: simple(:open_paren),
3813
+ pre_subscript: simple(:pre_sub),
3814
+ pre_supscript: simple(:pre_sup),
3815
+ close_paren: simple(:close_paren),
3816
+ base: simple(:base),
3817
+ sub: simple(:sub),
3818
+ sup: simple(:sup)) do
3819
+ Math::Function::Multiscript.new(
3820
+ Math::Function::PowerBase.new(
3821
+ base,
3822
+ Utility.unfenced_value(sub, paren_specific: true),
3823
+ Utility.unfenced_value(sup, paren_specific: true),
3824
+ ),
3825
+ [pre_sub],
3826
+ [pre_sup],
3827
+ )
3828
+ end
3829
+ end
3830
+ end
3831
+ end