plurimath 0.1.0 → 0.2.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (142) hide show
  1. checksums.yaml +4 -4
  2. data/.github/workflows/test.yml +33 -0
  3. data/.gitignore +1 -0
  4. data/.rubocop.yml +2 -0
  5. data/AsciiMath-Supported-Data.adoc +280 -0
  6. data/Gemfile +1 -0
  7. data/Latex-Supported-Data.adoc +1872 -0
  8. data/MathML-Supported-Data.adoc +270 -0
  9. data/README.adoc +94 -0
  10. data/lib/plurimath/asciimath/constants.rb +301 -0
  11. data/lib/plurimath/asciimath/parse.rb +121 -0
  12. data/lib/plurimath/asciimath/parser.rb +25 -0
  13. data/lib/plurimath/asciimath/transform.rb +395 -0
  14. data/lib/plurimath/asciimath.rb +16 -0
  15. data/lib/plurimath/html/constants.rb +50 -0
  16. data/lib/plurimath/html/parse.rb +149 -0
  17. data/lib/plurimath/html/parser.rb +26 -0
  18. data/lib/plurimath/html/transform.rb +363 -0
  19. data/lib/plurimath/html.rb +15 -0
  20. data/lib/plurimath/latex/constants.rb +1990 -0
  21. data/lib/plurimath/latex/parse.rb +198 -0
  22. data/lib/plurimath/latex/parser.rb +25 -0
  23. data/lib/plurimath/latex/transform.rb +458 -0
  24. data/lib/plurimath/latex.rb +15 -0
  25. data/lib/plurimath/math/formula.rb +51 -0
  26. data/lib/plurimath/math/function/abs.rb +12 -0
  27. data/lib/plurimath/math/function/arccos.rb +12 -0
  28. data/lib/plurimath/math/function/arcsin.rb +12 -0
  29. data/lib/plurimath/math/function/arctan.rb +12 -0
  30. data/lib/plurimath/math/function/bar.rb +16 -0
  31. data/lib/plurimath/math/function/base.rb +36 -0
  32. data/lib/plurimath/math/function/binary_function.rb +58 -0
  33. data/lib/plurimath/math/function/cancel.rb +12 -0
  34. data/lib/plurimath/math/function/ceil.rb +12 -0
  35. data/lib/plurimath/math/function/color.rb +23 -0
  36. data/lib/plurimath/math/function/cos.rb +12 -0
  37. data/lib/plurimath/math/function/cosh.rb +12 -0
  38. data/lib/plurimath/math/function/cot.rb +12 -0
  39. data/lib/plurimath/math/function/coth.rb +12 -0
  40. data/lib/plurimath/math/function/csc.rb +12 -0
  41. data/lib/plurimath/math/function/csch.rb +12 -0
  42. data/lib/plurimath/math/function/ddot.rb +12 -0
  43. data/lib/plurimath/math/function/deg.rb +12 -0
  44. data/lib/plurimath/math/function/det.rb +12 -0
  45. data/lib/plurimath/math/function/dim.rb +12 -0
  46. data/lib/plurimath/math/function/dot.rb +12 -0
  47. data/lib/plurimath/math/function/exp.rb +12 -0
  48. data/lib/plurimath/math/function/f.rb +12 -0
  49. data/lib/plurimath/math/function/fenced.rb +27 -0
  50. data/lib/plurimath/math/function/floor.rb +12 -0
  51. data/lib/plurimath/math/function/font_style/bold.rb +18 -0
  52. data/lib/plurimath/math/function/font_style/double_struck.rb +18 -0
  53. data/lib/plurimath/math/function/font_style/fraktur.rb +18 -0
  54. data/lib/plurimath/math/function/font_style/monospace.rb +18 -0
  55. data/lib/plurimath/math/function/font_style/sans-serif.rb +18 -0
  56. data/lib/plurimath/math/function/font_style/script.rb +18 -0
  57. data/lib/plurimath/math/function/font_style.rb +25 -0
  58. data/lib/plurimath/math/function/frac.rb +23 -0
  59. data/lib/plurimath/math/function/g.rb +12 -0
  60. data/lib/plurimath/math/function/gcd.rb +12 -0
  61. data/lib/plurimath/math/function/glb.rb +12 -0
  62. data/lib/plurimath/math/function/hat.rb +12 -0
  63. data/lib/plurimath/math/function/hom.rb +12 -0
  64. data/lib/plurimath/math/function/inf.rb +17 -0
  65. data/lib/plurimath/math/function/int.rb +17 -0
  66. data/lib/plurimath/math/function/ker.rb +12 -0
  67. data/lib/plurimath/math/function/lcm.rb +12 -0
  68. data/lib/plurimath/math/function/left.rb +20 -0
  69. data/lib/plurimath/math/function/lg.rb +12 -0
  70. data/lib/plurimath/math/function/lim.rb +23 -0
  71. data/lib/plurimath/math/function/liminf.rb +12 -0
  72. data/lib/plurimath/math/function/limits.rb +18 -0
  73. data/lib/plurimath/math/function/limsup.rb +12 -0
  74. data/lib/plurimath/math/function/ln.rb +12 -0
  75. data/lib/plurimath/math/function/log.rb +29 -0
  76. data/lib/plurimath/math/function/lub.rb +12 -0
  77. data/lib/plurimath/math/function/max.rb +12 -0
  78. data/lib/plurimath/math/function/min.rb +12 -0
  79. data/lib/plurimath/math/function/mod.rb +29 -0
  80. data/lib/plurimath/math/function/multiscript.rb +11 -0
  81. data/lib/plurimath/math/function/norm.rb +16 -0
  82. data/lib/plurimath/math/function/obrace.rb +12 -0
  83. data/lib/plurimath/math/function/oint.rb +17 -0
  84. data/lib/plurimath/math/function/over.rb +29 -0
  85. data/lib/plurimath/math/function/overset.rb +23 -0
  86. data/lib/plurimath/math/function/power.rb +36 -0
  87. data/lib/plurimath/math/function/power_base.rb +33 -0
  88. data/lib/plurimath/math/function/prod.rb +29 -0
  89. data/lib/plurimath/math/function/right.rb +24 -0
  90. data/lib/plurimath/math/function/root.rb +23 -0
  91. data/lib/plurimath/math/function/sec.rb +12 -0
  92. data/lib/plurimath/math/function/sech.rb +12 -0
  93. data/lib/plurimath/math/function/sin.rb +12 -0
  94. data/lib/plurimath/math/function/sinh.rb +12 -0
  95. data/lib/plurimath/math/function/sqrt.rb +16 -0
  96. data/lib/plurimath/math/function/stackrel.rb +12 -0
  97. data/lib/plurimath/math/function/substack.rb +17 -0
  98. data/lib/plurimath/math/function/sum.rb +29 -0
  99. data/lib/plurimath/math/function/sup.rb +12 -0
  100. data/lib/plurimath/math/function/table/align.rb +24 -0
  101. data/lib/plurimath/math/function/table/array.rb +25 -0
  102. data/lib/plurimath/math/function/table/bmatrix.rb +26 -0
  103. data/lib/plurimath/math/function/table/matrix.rb +24 -0
  104. data/lib/plurimath/math/function/table/multline.rb +24 -0
  105. data/lib/plurimath/math/function/table/pmatrix.rb +24 -0
  106. data/lib/plurimath/math/function/table/split.rb +24 -0
  107. data/lib/plurimath/math/function/table/vmatrix.rb +25 -0
  108. data/lib/plurimath/math/function/table.rb +54 -0
  109. data/lib/plurimath/math/function/tan.rb +12 -0
  110. data/lib/plurimath/math/function/tanh.rb +12 -0
  111. data/lib/plurimath/math/function/td.rb +32 -0
  112. data/lib/plurimath/math/function/ternary_function.rb +52 -0
  113. data/lib/plurimath/math/function/text.rb +44 -0
  114. data/lib/plurimath/math/function/tilde.rb +12 -0
  115. data/lib/plurimath/math/function/tr.rb +30 -0
  116. data/lib/plurimath/math/function/ubrace.rb +12 -0
  117. data/lib/plurimath/math/function/ul.rb +12 -0
  118. data/lib/plurimath/math/function/unary_function.rb +46 -0
  119. data/lib/plurimath/math/function/underover.rb +12 -0
  120. data/lib/plurimath/math/function/underset.rb +12 -0
  121. data/lib/plurimath/math/function/vec.rb +16 -0
  122. data/lib/plurimath/math/function.rb +14 -0
  123. data/lib/plurimath/math/number.rb +37 -0
  124. data/lib/plurimath/math/symbol.rb +43 -0
  125. data/lib/plurimath/math.rb +56 -0
  126. data/lib/plurimath/mathml/constants.rb +295 -0
  127. data/lib/plurimath/mathml/parse.rb +68 -0
  128. data/lib/plurimath/mathml/parser.rb +26 -0
  129. data/lib/plurimath/mathml/transform.rb +200 -0
  130. data/lib/plurimath/mathml.rb +16 -0
  131. data/lib/plurimath/omml/constants.rb +154 -0
  132. data/lib/plurimath/omml/parser.rb +22 -0
  133. data/lib/plurimath/omml/transform.rb +216 -0
  134. data/lib/plurimath/omml.rb +15 -0
  135. data/lib/plurimath/unicode.rb +15 -0
  136. data/lib/plurimath/unitsml.rb +15 -0
  137. data/lib/plurimath/utility.rb +73 -0
  138. data/lib/plurimath/version.rb +3 -1
  139. data/lib/plurimath.rb +3 -5
  140. data/plurimath.gemspec +4 -3
  141. metadata +169 -9
  142. data/README.md +0 -40
@@ -0,0 +1,121 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "parslet"
4
+ module Plurimath
5
+ class Asciimath
6
+ class Parse < Parslet::Parser
7
+ rule(:space) { str(" ") }
8
+ rule(:base) { str("_").as(:_) }
9
+ rule(:power) { str("^").as(:^) }
10
+ rule(:comma) { (str(",") >> space) | str(",") }
11
+
12
+ rule(:symbols) { arr_to_expression(Constants::SYMBOLS.keys, :symbol) }
13
+ rule(:font_style) { arr_to_expression(Constants::FONT_STYLES, :fonts) }
14
+
15
+ rule(:unary_functions) { arr_to_expression(Constants::UNARY_CLASSES) }
16
+ rule(:binary_functions) { arr_to_expression(Constants::BINARY_CLASSES) }
17
+
18
+ rule(:left_right_open_paren) { str("(") | str("[") }
19
+ rule(:left_right_close_paren) { str(")") | str("]") }
20
+
21
+ rule(:left_right) do
22
+ (str("left") >> left_right_open_paren.as(:left) >> iteration.as(:left_right_value) >> str("right") >> left_right_close_paren.as(:right)) |
23
+ table
24
+ end
25
+
26
+ rule(:open_table) do
27
+ arr_to_expression(Constants::TABLE_PARENTHESIS.keys, :table_left)
28
+ end
29
+
30
+ rule(:close_table) do
31
+ arr_to_expression(Constants::TABLE_PARENTHESIS.values, :table_right)
32
+ end
33
+
34
+ rule(:lparen) do
35
+ Constants::PARENTHESIS.keys.reduce do |expression, parenthesis|
36
+ expression = str(expression) if expression.is_a?(Symbol)
37
+ expression | str(parenthesis)
38
+ end
39
+ end
40
+
41
+ rule(:rparen) do
42
+ Constants::PARENTHESIS.values.reduce do |expression, parenthesis|
43
+ expression = str(expression) if expression.is_a?(String)
44
+ expression | str(parenthesis)
45
+ end
46
+ end
47
+
48
+ rule(:quoted_text) do
49
+ str('"') >> match("[^\"]").repeat.as(:text) >> str('"')
50
+ end
51
+
52
+ rule(:symbol_text_or_integer) do
53
+ binary_functions |
54
+ unary_functions |
55
+ symbols |
56
+ quoted_text |
57
+ match["a-zA-Z"].as(:symbol) |
58
+ match("[0-9]").repeat(1).as(:number)
59
+ end
60
+
61
+ rule(:table) do
62
+ (open_table.as(:table_left) >> tr >> close_table.as(:table_right)) |
63
+ (str("left") >> left_right_open_paren.as(:left) >> tr >> str("right") >> left_right_close_paren.as(:right))
64
+ end
65
+
66
+ rule(:tr) do
67
+ ((str("[").as(:open_tr) >> td.as(:tds_list) >> str("]")).as(:table_row) >> comma >> tr.as(:expr)) |
68
+ (str("[").as(:open_tr) >> td.as(:tds_list) >> str("]")).as(:table_row)
69
+ end
70
+
71
+ rule(:td) do
72
+ (sequence.as(:td) >> str(",") >> sequence.as(:tds)) |
73
+ sequence.as(:td)
74
+ end
75
+
76
+ rule(:sequence) do
77
+ (lparen >> expression >> rparen).as(:intermediate_exp) |
78
+ (binary_functions >> sequence.as(:base) >> sequence.maybe.as(:exponent)).as(:binary) |
79
+ (str("text") >> lparen.capture(:paren) >> read_text >> rparen) |
80
+ (unary_functions >> sequence).as(:unary) |
81
+ (font_style >> sequence).as(:fonts) |
82
+ symbol_text_or_integer
83
+ end
84
+
85
+ rule(:iteration) do
86
+ table.as(:table) |
87
+ (sequence.as(:dividend) >> str("mod").as(:mod) >> sequence.as(:divisor)).as(:mod) |
88
+ (sequence >> base >> sequence.as(:base) >> power >> sequence.as(:exponent)).as(:power_base) |
89
+ (sequence >> base >> sequence).as(:base) |
90
+ (sequence >> power >> sequence).as(:power) |
91
+ sequence.as(:sequence) |
92
+ space
93
+ end
94
+
95
+ rule(:expression) do
96
+ left_right.as(:left_right) |
97
+ (iteration >> expression).as(:expr) |
98
+ (iteration >> str("/").as(:/) >> iteration).as(:expr) |
99
+ str("")
100
+ end
101
+
102
+ root :expression
103
+
104
+ def arr_to_expression(arr, name = nil)
105
+ type = arr.first.class
106
+ arr.reduce do |expression, expr_string|
107
+ as_value = name.nil? ? expr_string || expression : name
108
+ expression = str(expression).as(as_value) if expression.is_a?(type)
109
+ expression | str(expr_string).as(as_value)
110
+ end
111
+ end
112
+
113
+ def read_text
114
+ dynamic do |_sour, context|
115
+ rparen = Constants::PARENTHESIS[context.captures[:paren].to_sym]
116
+ match("[^#{rparen}]").repeat.as(:text)
117
+ end
118
+ end
119
+ end
120
+ end
121
+ end
@@ -0,0 +1,25 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "parse"
4
+ require_relative "constants"
5
+ require_relative "transform"
6
+ module Plurimath
7
+ class Asciimath
8
+ class Parser
9
+ attr_accessor :text
10
+
11
+ def initialize(text)
12
+ @text = text
13
+ end
14
+
15
+ def parse
16
+ nodes = Parse.new.parse(text)
17
+ nodes = JSON.parse(nodes.to_json, symbolize_names: true)
18
+ transformed_tree = Transform.new.apply(nodes)
19
+ return transformed_tree if transformed_tree.is_a?(Math::Formula)
20
+
21
+ Math::Formula.new(transformed_tree)
22
+ end
23
+ end
24
+ end
25
+ end
@@ -0,0 +1,395 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Plurimath
4
+ class Asciimath
5
+ class Transform < Parslet::Transform
6
+ rule(expr: simple(:expr)) { expr }
7
+ rule(base: simple(:base)) { base }
8
+ rule(base: sequence(:base)) { base }
9
+ rule(fonts: simple(:fonts)) { fonts }
10
+ rule(power: simple(:power)) { power }
11
+ rule(unary: simple(:unary)) { unary }
12
+ rule(table: simple(:table)) { table }
13
+ rule(power: sequence(:power)) { power }
14
+ rule(binary: simple(:binary)) { binary }
15
+
16
+ rule(sequence: simple(:sequence)) { sequence }
17
+ rule(table_row: simple(:table_row)) { table_row }
18
+
19
+ rule(power_base: simple(:power_base)) { power_base }
20
+ rule(left_right: simple(:left_right)) { left_right }
21
+ rule(table_left: simple(:table_left)) { table_left }
22
+
23
+ rule(table_right: simple(:table_right)) { table_right }
24
+ rule(intermediate_exp: simple(:int_exp)) { int_exp }
25
+
26
+ rule(mod: simple(:mod), expr: simple(:expr)) { [mod, expr] }
27
+ rule(base: sequence(:base), expr: simple(:exp)) { base + [exp] }
28
+
29
+ rule(number: simple(:number)) do
30
+ Math::Number.new(number)
31
+ end
32
+
33
+ rule(symbol: simple(:symbol)) do
34
+ Math::Symbol.new(symbol)
35
+ end
36
+
37
+ rule(text: simple(:text)) do
38
+ text.is_a?(String) ? Utility.get_class("text").new(text) : text
39
+ end
40
+
41
+ rule(expr: sequence(:expr)) do
42
+ Math::Formula.new(expr)
43
+ end
44
+
45
+ rule(sequence: simple(:sequence),
46
+ expr: simple(:exp)) do
47
+ [sequence, exp]
48
+ end
49
+
50
+ rule(power: simple(:power),
51
+ expr: simple(:expr)) do
52
+ [power, expr]
53
+ end
54
+
55
+ rule(power: simple(:power),
56
+ expr: sequence(:expr)) do
57
+ expr.insert(0, power)
58
+ end
59
+
60
+ rule(table_row: simple(:table_row),
61
+ expr: simple(:expr)) do
62
+ [table_row, expr]
63
+ end
64
+
65
+ rule(td: simple(:td),
66
+ tds: simple(:tds)) do
67
+ [
68
+ Math::Function::Td.new([td]),
69
+ Math::Function::Td.new([tds]),
70
+ ]
71
+ end
72
+
73
+ rule(open_tr: simple(:tr),
74
+ tds_list: sequence(:tds_list)) do
75
+ Math::Function::Tr.new(tds_list)
76
+ end
77
+
78
+ rule(base: simple(:base),
79
+ expr: simple(:expr)) do
80
+ [base, expr]
81
+ end
82
+
83
+ rule(fonts: simple(:font_style),
84
+ intermediate_exp: simple(:int_exp)) do
85
+ Utility::FONT_STYLES[font_style.to_sym].new(
86
+ int_exp,
87
+ font_style,
88
+ )
89
+ end
90
+
91
+ rule(fonts: simple(:font_style),
92
+ text: simple(:text)) do
93
+ Utility::FONT_STYLES[font_style.to_sym].new(
94
+ Math::Function::Text.new(text),
95
+ font_style,
96
+ )
97
+ end
98
+
99
+ rule(power_base: simple(:power_base),
100
+ expr: sequence(:expr)) do
101
+ expr.insert(0, power_base)
102
+ end
103
+
104
+ rule(power_base: simple(:power_base),
105
+ expr: simple(:expr)) do
106
+ [power_base, expr]
107
+ end
108
+
109
+ rule(sequence: simple(:sequence),
110
+ expr: sequence(:expr)) do
111
+ expr.insert(0, sequence)
112
+ end
113
+
114
+ rule(dividend: simple(:dividend),
115
+ mod: simple(:mod),
116
+ divisor: simple(:divisor)) do
117
+ Math::Function::Mod.new(
118
+ dividend,
119
+ divisor,
120
+ )
121
+ end
122
+
123
+ rule(unary: simple(:unary),
124
+ "^": simple(:exponent),
125
+ number: simple(:number)) do
126
+ Math::Function::Power.new(
127
+ unary,
128
+ Math::Number.new(number),
129
+ )
130
+ end
131
+
132
+ rule(unary: simple(:unary),
133
+ "^": simple(:exponent),
134
+ intermediate_exp: simple(:intermediate_exp)) do
135
+ Math::Function::Power.new(
136
+ unary,
137
+ intermediate_exp,
138
+ )
139
+ end
140
+
141
+ rule(unary: simple(:unary),
142
+ "^": simple(:exponent),
143
+ text: simple(:text)) do
144
+ Math::Function::Power.new(
145
+ unary,
146
+ text.is_a?(String) ? Utility.get_class("text").new(text) : text,
147
+ )
148
+ end
149
+
150
+ rule(symbol: simple(:symbol),
151
+ "^": simple(:exponent),
152
+ number: simple(:number)) do
153
+ Math::Function::Power.new(
154
+ Math::Symbol.new(symbol),
155
+ Math::Number.new(number),
156
+ )
157
+ end
158
+
159
+ rule(binary: simple(:binary),
160
+ "^": simple(:under_score),
161
+ intermediate_exp: simple(:int_exp)) do
162
+ Math::Function::Power.new(
163
+ binary,
164
+ int_exp,
165
+ )
166
+ end
167
+
168
+ rule(binary: simple(:function),
169
+ "^": simple(:exponent),
170
+ number: simple(:number)) do
171
+ Math::Function::Power.new(
172
+ function,
173
+ Math::Number.new(number),
174
+ )
175
+ end
176
+
177
+ rule(left: simple(:left),
178
+ left_right_value: simple(:left_right),
179
+ right: simple(:right)) do
180
+ Math::Formula.new(
181
+ [
182
+ Math::Function::Left.new(left),
183
+ left_right,
184
+ Math::Function::Right.new(right),
185
+ ],
186
+ )
187
+ end
188
+
189
+ Constants::UNARY_CLASSES.each do |unary_class|
190
+ rule(unary_class => simple(:function),
191
+ intermediate_exp: simple(:int_exp)) do
192
+ Utility.get_class(function).new(int_exp)
193
+ end
194
+
195
+ rule(unary_class => simple(:function),
196
+ _: simple(:under_score),
197
+ intermediate_exp: simple(:int_exp)) do
198
+ Math::Function::Base.new(
199
+ Utility.get_class(function).new,
200
+ int_exp,
201
+ )
202
+ end
203
+
204
+ rule(unary_class => simple(:function),
205
+ symbol: simple(:new_symbol)) do
206
+ symbol = Math::Symbol.new(new_symbol)
207
+ Utility.get_class(function).new(symbol)
208
+ end
209
+
210
+ rule(unary_class => simple(:function),
211
+ number: simple(:new_number)) do
212
+ number = Math::Number.new(new_number)
213
+ Utility.get_class(function).new(number)
214
+ end
215
+
216
+ rule(unary_class => simple(:function),
217
+ "^": simple(:base),
218
+ intermediate_exp: simple(:unary)) do
219
+ Math::Function::Power.new(
220
+ Utility.get_class(unary_class).new,
221
+ unary,
222
+ )
223
+ end
224
+
225
+ rule(unary_class => simple(:function),
226
+ _: simple(:base),
227
+ symbol: simple(:symbol)) do
228
+ Math::Function::Base.new(
229
+ Utility.get_class(unary_class).new,
230
+ Math::Symbol.new(symbol),
231
+ )
232
+ end
233
+
234
+ rule(unary_class => simple(:function),
235
+ _: simple(:under_score),
236
+ base: simple(:base),
237
+ "^": simple(:power),
238
+ exponent: simple(:exponent)) do
239
+ Math::Function::PowerBase.new(
240
+ Utility.get_class(function).new,
241
+ base,
242
+ exponent,
243
+ )
244
+ end
245
+ end
246
+
247
+ Constants::BINARY_CLASSES.each do |binary_class|
248
+ rule(binary_class => simple(:function)) do
249
+ Utility.get_class(function).new
250
+ end
251
+
252
+ rule(binary_class => simple(:function),
253
+ _: simple(:under_score),
254
+ base: simple(:int_exp),
255
+ "^": simple(:power),
256
+ exponent: simple(:exponent)) do
257
+ Utility.get_class(function).new(
258
+ int_exp,
259
+ exponent,
260
+ )
261
+ end
262
+
263
+ rule(binary_class => simple(:function),
264
+ _: simple(:under_score),
265
+ intermediate_exp: simple(:int_exp)) do
266
+ Utility.get_class(function).new(int_exp, nil)
267
+ end
268
+
269
+ rule(binary_class => simple(:function),
270
+ "^": simple(:exponent),
271
+ intermediate_exp: simple(:int_exp)) do
272
+ Utility.get_class(function).new(nil, int_exp)
273
+ end
274
+
275
+ rule(binary_class => simple(:function),
276
+ _: simple(:under_score),
277
+ number: simple(:number)) do
278
+ Utility.get_class(function).new(
279
+ Math::Number.new(number), nil
280
+ )
281
+ end
282
+
283
+ rule(binary_class => simple(:function),
284
+ _: simple(:under_score),
285
+ symbol: simple(:symbol)) do
286
+ Utility.get_class(function).new(
287
+ Math::Symbol.new(symbol), nil
288
+ )
289
+ end
290
+
291
+ rule(binary_class => simple(:function),
292
+ _: simple(:under_score),
293
+ unary: simple(:unary)) do
294
+ Utility.get_class(function).new(unary, nil)
295
+ end
296
+
297
+ rule(binary_class => simple(:function),
298
+ "^": simple(:power),
299
+ number: simple(:number)) do
300
+ Utility.get_class(function).new(
301
+ nil,
302
+ Math::Number.new(number),
303
+ )
304
+ end
305
+
306
+ rule(binary_class => simple(:function),
307
+ "^": simple(:power),
308
+ symbol: simple(:symbol)) do
309
+ Utility.get_class(function).new(
310
+ nil,
311
+ Math::Symbol.new(symbol),
312
+ )
313
+ end
314
+
315
+ rule(binary_class => simple(:function),
316
+ base: simple(:base),
317
+ exponent: simple(:exponent)) do
318
+ Utility.get_class(function).new(
319
+ base,
320
+ exponent,
321
+ )
322
+ end
323
+
324
+ rule(binary: simple(:function),
325
+ "^": simple(:exponent),
326
+ text: simple(:text)) do
327
+ Math::Function::Power.new(
328
+ function,
329
+ text.is_a?(String) ? Utility.get_class("text").new(text) : text,
330
+ )
331
+ end
332
+
333
+ rule(binary: simple(:function),
334
+ "^": simple(:exponent),
335
+ unary: simple(:unary)) do
336
+ Math::Function::Power.new(
337
+ function,
338
+ unary,
339
+ )
340
+ end
341
+
342
+ Constants::UNARY_CLASSES.each do |unary_class|
343
+ rule(binary_class => simple(:function),
344
+ _: simple(:base),
345
+ unary_class => simple(:unary)) do
346
+ unary_class = Utility.get_class(unary).new
347
+ Utility.get_class(binary_class).new(unary_class, nil)
348
+ end
349
+
350
+ rule(binary_class => simple(:function),
351
+ "^": simple(:base),
352
+ unary_class => simple(:unary)) do
353
+ unary_class = Utility.get_class(unary).new
354
+ Utility.get_class(binary_class).new(nil, unary_class)
355
+ end
356
+ end
357
+ end
358
+
359
+ rule(table_left: simple(:table_left),
360
+ table_row: simple(:table_row),
361
+ expr: simple(:expr),
362
+ table_right: simple(:table_right)) do
363
+ Math::Function::Table.new(
364
+ [table_row, expr],
365
+ table_left,
366
+ table_right,
367
+ )
368
+ end
369
+
370
+ rule(table_left: simple(:table_left),
371
+ table_row: simple(:table_row),
372
+ expr: sequence(:expr),
373
+ table_right: simple(:table_right)) do
374
+ Math::Function::Table.new(
375
+ expr.insert(0, table_row),
376
+ table_left,
377
+ table_right,
378
+ )
379
+ end
380
+
381
+ rule(left: simple(:left),
382
+ table_row: simple(:table_row),
383
+ expr: sequence(:expr),
384
+ right: simple(:right)) do
385
+ Math::Formula.new(
386
+ [
387
+ Math::Function::Left.new(left),
388
+ Math::Function::Table.new(expr.insert(0, table_row), "", ""),
389
+ Math::Function::Right.new(right),
390
+ ],
391
+ )
392
+ end
393
+ end
394
+ end
395
+ end
@@ -0,0 +1,16 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "math"
4
+ module Plurimath
5
+ class Asciimath
6
+ attr_accessor :text
7
+
8
+ def initialize(text)
9
+ @text = text
10
+ end
11
+
12
+ def to_formula
13
+ Parser.new(text).parse
14
+ end
15
+ end
16
+ end
@@ -0,0 +1,50 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Plurimath
4
+ class Html
5
+ class Constants
6
+ PARENTHESIS = {
7
+ "(": :")",
8
+ "{": :"}",
9
+ "[": :"]",
10
+ }.freeze
11
+ UNARY_CLASSES = %w[
12
+ arcsin
13
+ arccos
14
+ arctan
15
+ coth
16
+ tanh
17
+ sech
18
+ csch
19
+ sqrt
20
+ ceil
21
+ sinh
22
+ cosh
23
+ sin
24
+ cos
25
+ gcd
26
+ csc
27
+ abs
28
+ vec
29
+ exp
30
+ sec
31
+ tan
32
+ cot
33
+ lcm
34
+ det
35
+ ln
36
+ lg
37
+ g
38
+ f
39
+ ].freeze
40
+ SUB_SUP_CLASSES = {
41
+ "&prod;": :prod,
42
+ "&sum;": :sum,
43
+ log: :log,
44
+ lim: :lim,
45
+ "∏": :prod,
46
+ "∑": :sum,
47
+ }.freeze
48
+ end
49
+ end
50
+ end