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,233 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "parsing_rules/masked"
4
+ require_relative "parsing_rules/sub_sup"
5
+ require_relative "parsing_rules/common_rules"
6
+ require_relative "parsing_rules/absence_rules"
7
+ require_relative "parsing_rules/constants_rules"
8
+ module Plurimath
9
+ class UnicodeMath
10
+ class Parse < Parslet::Parser
11
+ include ParsingRules::Masked
12
+ include ParsingRules::SubSup
13
+ include ParsingRules::CommonRules
14
+ include ParsingRules::ConstantsRules
15
+ include ParsingRules::AbsenceRules
16
+
17
+ rule(:an) { an_math | an_other }
18
+ rule(:box) { str("□") >> operand }
19
+
20
+ rule(:char) { (absent_chars >> unicode.as(:unicode_symbols)) }
21
+ rule(:rows) { (str("@").as(:tr) >> space? >> rows.as(:trs).maybe) | (row.as(:tr) >> space? >> str("@") >> space? >> rows.as(:trs).maybe) | row.as(:tr) }
22
+
23
+ rule(:space) { match(/\s/).repeat(1) | invisible_times }
24
+ rule(:other) { other_absent >> alphanumeric.as(:symbol) }
25
+
26
+ rule(:space?) { space.maybe }
27
+ rule(:digits) { (n_ascii >> digits) | n_ascii }
28
+
29
+ rule(:op_nary) { op_nary_symbols | op_nary_text }
30
+ rule(:n_ascii) { match["0-9"].repeat(1).as(:number) }
31
+ rule(:a_ascii) { match["A-Za-z"].repeat(1).as(:symbol) }
32
+ rule(:unicode) { str("&#x") >> match["0-9a-fA-F"].repeat >> str(";") }
33
+ rule(:an_math) { space.absent? >> match("[\u{1D400}-\u{1D7FF}\u{2102}-\u{2134}]") }
34
+
35
+ rule(:td_value) { expression.as(:exp) >> space? >> td_value.as(:expr).maybe }
36
+ rule(:an_other) { (an_math | n_ascii).absent? >> alphanumeric.as(:alphanumeric) }
37
+ rule(:function) { root_functions | box | hbrack | arg_function | intent_function }
38
+ rule(:op_array) { op_matrixs | op_prefixed_matrixs | str("&") | str("&#xb;") | str("\\array") }
39
+
40
+ rule(:op_opener) { open_paren | op_open_unicode | op_open_paren | op_open }
41
+ rule(:op_closer) { op_close_unicode | close_paren | op_close_paren | op_close }
42
+
43
+ rule(:op_decimal) { str(",") | str(".") }
44
+ rule(:diacritics) { (char.as(:char) >> diacritics.as(:diacritics)) | char.as(:char) }
45
+ rule(:open_paren) { (op_masked_open >> (op_closer | op_opener)).as(:open_paren) | op_masked_open }
46
+
47
+ rule(:matrix_only) { non_matrixs_absence? >> (op_matrixs | op_prefixed_matrixs) }
48
+ rule(:close_paren) { (op_masked_close >> (op_opener | op_closer)).as(:close_paren) | op_masked_close }
49
+
50
+ rule(:diacriticbase) { an | n_ascii }
51
+ rule(:forward_slash) { str("/") | str("&#x2f;") | str("&#x2044;") }
52
+
53
+ rule(:root_functions) { qdrt | cbrt | sqrt | binary_root | nthrt }
54
+ rule(:op_masked_open) { (str("\\left") | str("\\open") | str("&#x251c;")).as(:paren_open_prefix) >> digits.as(:open_paren_mask).maybe }
55
+
56
+ rule(:invisible_times) { (str("&#x2062;") | str("&#x2061;") | str("&#x20;") | str("\\itimes") >> space?) }
57
+
58
+ rule(:ordinary_symbols) { op_ordinary_symbols | op_prefixed_ordinary_symbols }
59
+ rule(:negatable_symbols) { forward_slash >> negated | negated >> str("&#x338;") }
60
+ rule(:invisible_unicode) { str("&#x2592;") | (str("\\naryand") | str("\\of") >> space?) }
61
+
62
+ rule(:invisible_unicode?) { invisible_unicode.maybe }
63
+ rule(:relational_symbols) { op_relational_unicode | op_relational_symbols }
64
+
65
+ rule(:hbrack_power_base_check) { (sub_sup_operand >> (power_symbol | base_symbol)).present? }
66
+ rule(:spaced_bracketed_operand) { operand >> space? >> spaced_bracketed_operand.as(:expr).maybe }
67
+
68
+ rule(:hbrack) do
69
+ (op_h_brackets >> str("(").present? >> exp_bracket.as(:first_value)) |
70
+ (op_h_brackets >> (sub_sup_operand >> (power_symbol | base_symbol)).present? >> exp_script.as(:scripted_first_value)).as(:hbrack) |
71
+ (op_h_brackets >> operand.as(:first_value)).as(:hbrack)
72
+ end
73
+
74
+ rule(:negated) do
75
+ root_functions.absent? >> (
76
+ op_negated |
77
+ op_prefixed_negated |
78
+ op_prefixed_ordinary_negated |
79
+ op_ordinary_negated |
80
+ binary_negated |
81
+ absent_negated_unicodes >> unicode.as(:negated_operator)
82
+ )
83
+ end
84
+
85
+ rule(:op_over) do
86
+ str("&#x2044;").as(:bevelled) |
87
+ slash >> (str("sdiv") | str("sdivide") | str("sfrac")).as(:bevelled) |
88
+ str("&#x2298;").as(:no_display_style) |
89
+ slash >> (str("ndiv") | str("oslash")).as(:no_display_style) |
90
+ (str("&#x2215;") | str("\\ldiv")).as(:ldiv) |
91
+ forward_slash |
92
+ str("\\over") |
93
+ str("&#x2f;") |
94
+ str("\\not") |
95
+ (str("&#xa6;") | str("\\atop")).as(:atop) |
96
+ (str("\\choose") | str("&#x249e;")).as(:choose)
97
+ end
98
+
99
+ rule(:element_exp_script_validation?) do
100
+ (((op_unary_functions | unary_arg_functions).absent? >> atom.as(:factor).maybe) >> (mini_sub_sup_present? >> operator >> mini_fraction.present?).absent?)
101
+ end
102
+
103
+ rule(:spaced_exp_bracket) do
104
+ expression >> space? >> spaced_exp_bracket.as(:exp) |
105
+ mid_symbols >> space? >> spaced_exp_bracket.as(:expr) |
106
+ str("&#x2212;").as(:symbol) >> space? >> spaced_exp_bracket.as(:expr) |
107
+ expression >> space?
108
+ end
109
+
110
+ rule(:row) do
111
+ (td_value.as(:td) >> space? >> str("&") >> space? >> row.as(:tds).maybe) |
112
+ td_value.as(:td) |
113
+ (char.absent? >> space? >> str("&").as(:td) >> space? >> row.as(:tds).maybe)
114
+ end
115
+
116
+ rule(:array) do
117
+ (op_array >> str("(") >> rows.as(:array) >> str(")")).as(:table) |
118
+ (matrix_only >> match["0-9"].as(:identity_matrix_number)).as(:table)
119
+ end
120
+
121
+ rule(:slashed_operator) do
122
+ absent_slashed_values >> str("\\") >> (str("\\") | unicode | (n_ascii | a_ascii).repeat(1) | any).as(:slashed_value) |
123
+ absent_slashed_values >> str("\\").as(:slashed_value)
124
+ end
125
+
126
+ rule(:number) do
127
+ (digits.as(:whole) >> op_decimal.as(:decimal) >> digits.as(:fractional)).as(:decimal_number) |
128
+ (op_decimal.as(:decimal) >> digits.as(:whole) >> space?).as(:decimal_number) |
129
+ digits.as(:digit)
130
+ end
131
+
132
+ rule(:numerator) do
133
+ (relational_symbols.absent? | exp_script.present?) >> (
134
+ (unary_arg_functions >> numerator.as(:recursive_numerator).maybe) |
135
+ ((absent_numerator_exp_script? >> op_nary.absent?) >> mini_fraction_exp_script_absent? >> exp_script >> space) >> numerator.as(:recursive_numerator).maybe |
136
+ (absent_numerator_exp_script? >> mini_fraction_exp_script_absent? >> exp_script) >> numerator.as(:recursive_numerator).maybe |
137
+ (accents.as(:base) >> accents_subsup).as(:accents_subsup) |
138
+ sub_paren |
139
+ sup_paren |
140
+ accents |
141
+ unary_arg_functions |
142
+ (frac_binary_absent >> numerator.as(:recursive_numerator)) |
143
+ (factor >> frac_binary_absent.maybe >> numerator.as(:recursive_numerator).maybe) |
144
+ operator.absent? >> operand >> frac_binary_absent.maybe |
145
+ frac_binary_absent
146
+ )
147
+ end
148
+
149
+ rule(:op_build_up) do
150
+ str("_") |
151
+ str("^") |
152
+ str("∛") |
153
+ str("∜") |
154
+ str("□") |
155
+ str("|") |
156
+ op_array |
157
+ op_open |
158
+ op_close |
159
+ op_nary |
160
+ op_over |
161
+ forward_slash |
162
+ op_h_brackets |
163
+ root_symbols |
164
+ op_decimal
165
+ end
166
+
167
+ rule(:exp_bracket) do
168
+ (str("||").as(:open_paren) >> space? >> spaced_exp_bracket >> space? >> str("||").as(:close_paren)) |
169
+ (str("|").as(:open_paren) >> space? >> spaced_exp_bracket >> space? >> str("|").as(:close_paren)) |
170
+ (op_opener >> space? >> spaced_exp_bracket.maybe >> space? >> op_closer) |
171
+ (mix_bracketed.as(:intermediate_exp) >> space? >> expression.as(:expr)) |
172
+ (mix_bracketed.as(:intermediate_exp) >> space)
173
+ end
174
+
175
+ rule(:mix_bracketed) do
176
+ (op_opener >> space? >> spaced_exp_bracket >> space? >> str("|").as(:close_paren)) |
177
+ (str("|").as(:open_paren) >> space? >> spaced_exp_bracket >> space? >> op_closer)
178
+ end
179
+
180
+ rule(:denominator) do
181
+ operator.absent? >> fraction.as(:frac) |
182
+ exp_script |
183
+ sub_paren |
184
+ sup_paren |
185
+ frac_binary_absent >> invisible_times.maybe >> relational_symbols.absent? >> denominator.as(:recursive_denominator) |
186
+ operator.absent? >> factor >> invisible_times.maybe >> relational_symbols.absent? >> denominator.as(:recursive_denominator) |
187
+ operator.absent? >> operand >> invisible_times.maybe >> relational_symbols.absent? >> denominator.as(:recursive_denominator) |
188
+ frac_binary_absent |
189
+ operator.absent? >> operand
190
+ end
191
+
192
+ rule(:op_masked_close) do
193
+ (str("\\right") | str("\\close") | str("&#x2524;")).as(:paren_close_prefix) >> digits.as(:close_paren_mask).maybe
194
+ end
195
+
196
+ rule(:element) do
197
+ accents.present? >> (accents.as(:base) >> accents_subsup).as(:accents_subsup) |
198
+ accents.present? >> fraction.as(:frac) |
199
+ ((op_unary_functions | unary_arg_functions).present? >> fraction.as(:frac)) |
200
+ mini_sub_sup_present? >> operator >> mini_fraction.as(:frac) |
201
+ accents |
202
+ diacritics_accents |
203
+ op_unicode_fractions |
204
+ fraction.as(:frac) |
205
+ monospace_fonts |
206
+ array |
207
+ exp_script |
208
+ element_exp_script_validation? >> space? >> exp_script |
209
+ unary_arg_functions |
210
+ combined_symbols |
211
+ operand |
212
+ char |
213
+ alphanumeric |
214
+ negatable_symbols |
215
+ operator |
216
+ binary_symbols |
217
+ slashed_operator
218
+ end
219
+
220
+ rule(:expression) do
221
+ element >> other.as(:other) >> expression.as(:expr) |
222
+ element >> relational_symbols >> expression.as(:expr).maybe |
223
+ element |
224
+ element >> space? >> expression.as(:expr) |
225
+ slashed_operator >> space? >> expression.as(:expr).maybe |
226
+ element >> space? >> expression.as(:expr) >> space? >> expression.as(:expression).maybe |
227
+ mini_values >> space? >> expression.as(:expr).maybe
228
+ end
229
+
230
+ root :expression
231
+ end
232
+ end
233
+ end
@@ -0,0 +1,58 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "parslet"
4
+ require "parslet/convenience"
5
+ require_relative "parse"
6
+ require_relative "transform"
7
+ require_relative "constants"
8
+ module Plurimath
9
+ class UnicodeMath
10
+ class Parser
11
+ attr_accessor :text
12
+
13
+ LABELED_TR_REGEX = /"([^"]*(#|&#x23;|\\\\eqno)[^"]*[^"]*|[^"]*(#|&#x23;|\\\\eqno)[^"]*[^"]*)"/
14
+
15
+ def initialize(text)
16
+ text = pre_processing(text)
17
+ @text = HTMLEntities.new.encode(text, :hexadecimal)
18
+ @text.gsub!("&#x26;", "&")
19
+ @text.gsub!("&#x22;", "\"")
20
+ @text.gsub!(/&#x2af7;.*&#x2af8;/, "")
21
+ @text.gsub!(/\\\\/, "\\")
22
+ @text.gsub!(/\\u([\da-fA-F]{1,5})\w{0,5}/) { "&#x#{$1};" } # Converting \u#{xxxx} encoding to &#x#{xxxx};
23
+ @text.strip!
24
+ end
25
+
26
+ def parse
27
+ tree = Parse.new.parse(text)
28
+ tree = post_processing(tree) if @splitted
29
+ Math::Formula.new(
30
+ Array(
31
+ Transform.new.apply(tree),
32
+ ),
33
+ )
34
+ end
35
+
36
+ private
37
+
38
+ def post_processing(tree)
39
+ {
40
+ labeled_tr_value: tree,
41
+ labeled_tr_id: @splitted
42
+ }
43
+ end
44
+
45
+ def pre_processing(text)
46
+ text unless text.include?("#") && !text.match?(LABELED_TR_REGEX)
47
+
48
+ text.gsub!(/✎\(.*(\#).*\)/) do |str|
49
+ str.gsub!("#", "\"replacement\"")
50
+ end
51
+ splitted = text.split("#")
52
+ splitted.first.gsub!("\"replacement\"", "#")
53
+ @splitted = splitted.last if splitted.count > 1
54
+ splitted.first
55
+ end
56
+ end
57
+ end
58
+ end
@@ -0,0 +1,138 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "helper"
4
+ module Plurimath
5
+ class UnicodeMath
6
+ module ParsingRules
7
+ module AbsenceRules
8
+ include Helper
9
+
10
+ rule(:frac_binary_absent) { frac_binary_absent_symbols? >> binary_symbols }
11
+
12
+ rule(:non_matrixs_absence?) { (str("eqarray") | str("&#x2588;") | str("cases") | str("&#x24b8;")).absent? }
13
+
14
+ rule(:sub_sup_binary_absent) { ((slash >> str("times")) | str("&#xd7;")).absent? >> binary_symbols }
15
+
16
+ rule(:absent_negated_unicodes) { sqrt_symbols | root_symbols }
17
+
18
+ rule(:frac_binary_absent_symbols?) { ((slash >> (str("times") | str("neq") | str("ne"))) | (str("&#xd7;") | str("&#x2260"))).absent? }
19
+
20
+ rule(:binary_negated_absent_symbols?) { ((slash >> str("dd")) | str("&#x2146;")).absent? }
21
+
22
+ rule(:mini_fraction_exp_script_absent?) { (operator >> mini_fraction).absent? }
23
+
24
+ rule(:absent_chars) do
25
+ (
26
+ op_unary_arg_functions |
27
+ op_diacritic_overlays |
28
+ op_unicode_fractions |
29
+ op_diacritic_belows |
30
+ op_ordinary_symbols |
31
+ relational_symbols |
32
+ op_sub_close_paren |
33
+ op_sup_close_paren |
34
+ op_sub_open_paren |
35
+ op_sup_open_paren |
36
+ op_binary_symbols |
37
+ invisible_unicode |
38
+ op_unary_symbols |
39
+ op_sub_operators |
40
+ op_sup_operators |
41
+ op_close_unicode |
42
+ str("&#x2534;") |
43
+ str("&#x252c;") |
44
+ str("&#x2524;") |
45
+ str("&#x251c;") |
46
+ str("&#x270e;") |
47
+ str("&#x2062;") |
48
+ str("&#x2044;") |
49
+ str("&#x2061;") |
50
+ str("&#x2601;") |
51
+ str("&#x2592;") |
52
+ str("&#x249e;") |
53
+ str("&#x2298;") |
54
+ op_open_unicode |
55
+ op_nary_symbols |
56
+ str("&#x221a;") |
57
+ str("&#x221b;") |
58
+ str("&#x221c;") |
59
+ str("&#x24ad;") |
60
+ str("&#x25ad;") |
61
+ str("&#xffd7;") |
62
+ str("&#x24d0;") |
63
+ str("&#x24d8;") |
64
+ str("&#x2223;") |
65
+ str("&#x2215;") |
66
+ str("&#x20;") |
67
+ str("&#x27;") |
68
+ str("&#x2f;") |
69
+ str("&#xac;") |
70
+ str("&#xa6;") |
71
+ op_sup_digits |
72
+ op_sub_digits |
73
+ op_sup_alpha |
74
+ op_sub_alpha |
75
+ op_h_bracket |
76
+ skip_symbols |
77
+ op_matrixs |
78
+ op_accent |
79
+ primes
80
+ ).absent?
81
+ end
82
+
83
+ rule(:absent_slashed_values) do
84
+ (
85
+ op_prefixed_unary_arg_functions |
86
+ op_prefixed_ordinary_symbols |
87
+ op_binary_symbols_prefixed |
88
+ op_prefixed_unary_symbols |
89
+ slash >> str("backcolor") |
90
+ slash >> str("naryand") |
91
+ slash >> str("sdivide") |
92
+ slash >> str("oslash") |
93
+ slash >> str("color") |
94
+ op_relational_symbols |
95
+ op_h_bracket_prefixed |
96
+ op_alphanumeric_fonts |
97
+ skip_symbols_prefixed |
98
+ slash >> str("sfrac") |
99
+ slash >> str("rect") |
100
+ slash >> str("sqrt") |
101
+ slash >> str("qdrt") |
102
+ slash >> str("cbrt") |
103
+ slash >> str("root") |
104
+ slash >> str("sdiv") |
105
+ slash >> str("ndiv") |
106
+ slash >> str("ldiv") |
107
+ op_prefixed_matrixs |
108
+ op_binary_symbols_prefixed |
109
+ op_prefixed_negated |
110
+ slash >> str("mid") |
111
+ op_accent_prefixed |
112
+ prefixed_primes |
113
+ op_nary_text |
114
+ str("\\of") |
115
+ op_close |
116
+ op_fonts |
117
+ op_open
118
+ ).absent?
119
+ end
120
+
121
+ rule(:other_absent) do
122
+ (
123
+ an.as(:other_exp) |
124
+ op_build_up |
125
+ match('\r') |
126
+ n_ascii |
127
+ char
128
+ ).absent?
129
+ end
130
+
131
+ rule(:absent_numerator_exp_script?) do
132
+ (power_base_script.as(:nary_sub_sup) >> invisible_space? >> naryand_recursion.as(:naryand)).absent? |
133
+ (op_nary >> invisible_space? >> naryand_recursion.as(:naryand).maybe).absent?
134
+ end
135
+ end
136
+ end
137
+ end
138
+ end
@@ -0,0 +1,114 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "helper"
4
+ module Plurimath
5
+ class UnicodeMath
6
+ module ParsingRules
7
+ module CommonRules
8
+ include Helper
9
+
10
+ rule(:atom) { (diacritics >> diacriticbase.maybe) | an }
11
+ rule(:atoms) { (atom.as(:atom) >> atoms.as(:atoms).maybe) }
12
+ rule(:entity) { atoms | number }
13
+
14
+ rule(:operator) { match["-+*=.?:,`"].as(:operator) }
15
+ rule(:op_unary) { op_prefixed_unary_arg_functions | op_unary_arg_functions | op_prefixed_unary_symbols | op_unary_symbols }
16
+
17
+ rule(:mid_symbols) { (slash >> str("mid").as(:mid_symbol)) | str("&#x2223;").as(:mid_symbol) }
18
+
19
+ rule(:unary_spaces) { space | invisible_unicode }
20
+ rule(:custom_fonts) { str("double") | str("fraktur") | str("script") }
21
+ rule(:parsing_text) { str("\"") >> match("[^\"]").repeat(1).as(:text) >> str("\"") }
22
+ rule(:alphanumeric) { match("[\u{0041}-\u{005A}\u{0061}-\u{007A}\u{0391}-\u{2207}\u{3B1}-\u{3DD}\u{30}-\u{39}]") }
23
+
24
+ rule(:op_h_brackets) { op_h_bracket | op_h_bracket_prefixed }
25
+ rule(:nary_functions) { (op_unary >> unary_spaces.maybe) | (op_unary_functions >> unary_spaces) }
26
+ rule(:exclamation_symbols) { (str("!") | str("!!")).as(:exclamation_symbol) }
27
+ rule(:exclamation_symbols?) { exclamation_symbols.maybe }
28
+
29
+ rule(:mini_fraction) do
30
+ sup_paren.as(:mini_numerator) >> (negatable_symbols.absent? >> op_over) >> sub_paren.as(:mini_denominator)
31
+ end
32
+
33
+ rule(:fraction) do
34
+ mini_fraction |
35
+ numerator.as(:numerator) >> space? >> (negatable_symbols.absent? >> op_over) >> space? >> denominator.as(:denominator)
36
+ end
37
+
38
+ rule(:fonts) do
39
+ str("\\") >> custom_fonts.as(:unicoded_font_class) >> str("H").as(:symbol) |
40
+ str("\\") >> str("mitBbb").as(:unicoded_font_class) >> match(/D|d|e|i|j/).as(:symbol)|
41
+ op_fonts >> match["A-Za-z"].as(:symbol) |
42
+ op_alphanumeric_fonts >> (match["A-Za-z"].as(:symbol) | match("[0-9]").as(:number))
43
+ end
44
+
45
+ rule(:unary_arg_functions) do
46
+ (op_unary_functions >> space? >> (soperand | exp_bracket).as(:first_value).maybe) |
47
+ (nary_functions >> space? >> (exp_bracket | soperand).as(:first_value)).as(:unary_function)
48
+ end
49
+
50
+ rule(:accents) do
51
+ (exp_bracket.as(:intermediate_exp).as(:first_value) >> str("&#xa0;").maybe >> repeated_accent_symbols).as(:accents) |
52
+ (str("&#xa0;").absent? >> factor.as(:first_value) >> str("&#xa0;").maybe >> repeated_accent_symbols).as(:accents)
53
+ end
54
+
55
+ rule(:diacritics_accents) do
56
+ (operand.as(:first_value) >> op_diacritic_overlays.as(:overlay_after)).as(:diacritics_accents) |
57
+ (operand.as(:first_value) >> op_diacritic_belows.as(:below_after)).as(:diacritics_accents) |
58
+ (op_diacritic_belows.as(:below_before) >> operand.as(:first_value)).as(:diacritics_accents) |
59
+ (op_diacritic_overlays.as(:overlay_before) >> operand.as(:first_value)).as(:diacritics_accents)
60
+ end
61
+
62
+ rule(:repeated_accent_symbols) do
63
+ (op_accent | op_accent_prefixed).repeat(1) >> prime_symbols.maybe |
64
+ prime_symbols
65
+ end
66
+
67
+ rule(:prime_symbols) do
68
+ ((slash >> prefixed_primes.as(:prefixed_prime) | primes).repeat(1).as(:prime_accent_symbols))
69
+ end
70
+
71
+ rule(:operand) do
72
+ rect |
73
+ phant |
74
+ accents |
75
+ negatable_symbols |
76
+ fonts.as(:fonts) |
77
+ ((parsing_text | factor.as(:factor)) >> operand.as(:operand).maybe)
78
+ end
79
+
80
+ rule(:factor) do
81
+ combined_symbols |
82
+ ((str("&#x2212;").absent? >> op_unary_functions.absent?) >> entity >> exclamation_symbols?) |
83
+ color |
84
+ (exp_bracket.as(:intermediate_exp) >> exclamation_symbols?) |
85
+ function |
86
+ backcolor |
87
+ op_spaces |
88
+ monospace_fonts |
89
+ relational_symbols |
90
+ unary_arg_functions |
91
+ op_unary_functions >> unary_spaces >> (operand | exp_bracket).absent? |
92
+ ordinary_symbols |
93
+ negatable_symbols |
94
+ str("...").as(:ldots).as(:symbol) |
95
+ exclamation_symbols
96
+ end
97
+
98
+ rule(:soperand) do
99
+ operand |
100
+ str("&#x221e;").as(:infty) |
101
+ str("-&#x221e;").as(:symbol) |
102
+ str("&#x2212;").as(:symbol) |
103
+ str("-").as(:symbol) |
104
+ operator
105
+ end
106
+
107
+ rule(:bracketed_soperand) do
108
+ (op_opener.as(:opener) >> space? >> soperand.as(:operand) >> space? >> op_closer.as(:closer)).as(:int_exp) |
109
+ soperand.as(:operand)
110
+ end
111
+ end
112
+ end
113
+ end
114
+ end
@@ -0,0 +1,102 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "helper"
4
+ module Plurimath
5
+ class UnicodeMath
6
+ module ParsingRules
7
+ module ConstantsRules
8
+ include Helper
9
+
10
+ rule(:slash) { str("\\") }
11
+ rule(:slash?) { slash.maybe }
12
+ rule(:primes) { str("&#x2057;") | str("&#x2034;") | str("&#x2033;") | str("&#x2032;") | str("&#x27;") | str("'") }
13
+
14
+ rule(:op_open) { slash >> arr_to_expression(Constants::OPEN_SYMBOLS.keys, :open_paren) }
15
+
16
+ rule(:op_fonts) { slash >> arr_to_expression(Constants::FONTS_CLASSES, :font_class) }
17
+ rule(:op_close) { slash >> arr_to_expression(Constants::CLOSE_SYMBOLS.keys, :close_paren) }
18
+
19
+ rule(:op_accent) { arr_to_expression(Constants::ACCENT_SYMBOLS.values, :accent_symbols) }
20
+ rule(:op_spaces) { skip_symbols | skip_symbols_prefixed }
21
+
22
+ rule(:op_matrixs) { arr_to_expression(Constants::MATRIXS.values, :matrixs) }
23
+ rule(:op_negated) { arr_to_expression(Constants::NEGATABLE_SYMBOLS, :negated_operator) }
24
+
25
+ rule(:op_nary_text) { slash >> arr_to_expression(Constants::NARY_SYMBOLS.keys, :nary_class) }
26
+ rule(:op_sub_alpha) { arr_to_expression(Constants::SUB_ALPHABETS.values, :sub_alpha) }
27
+ rule(:op_sup_alpha) { arr_to_expression(Constants::SUP_ALPHABETS.values, :sup_alpha) }
28
+ rule(:op_h_bracket) { arr_to_expression(Constants::HORIZONTAL_BRACKETS.values, :hbracket_class) }
29
+ rule(:skip_symbols) { arr_to_expression(Constants::SKIP_SYMBOLS.values, :spaces) }
30
+
31
+ rule(:op_open_paren) { arr_to_expression(Constants::OPEN_PARENTHESIS, :open_paren) }
32
+ rule(:op_sup_digits) { arr_to_expression(Constants::SUP_DIGITS.values, :sup_digits) }
33
+ rule(:op_sub_digits) { arr_to_expression(Constants::SUB_DIGITS.values, :sub_digits) }
34
+
35
+ rule(:binary_symbols) { op_binary_symbols | op_binary_symbols_prefixed }
36
+ rule(:binary_negated) { binary_negated_absent_symbols? >> (op_binary_negated | op_prefixed_binary_negated) }
37
+ rule(:op_close_paren) { arr_to_expression(Constants::CLOSE_PARENTHESIS, :close_paren) }
38
+
39
+ rule(:op_nary_symbols) { arr_to_expression(Constants::NARY_SYMBOLS.values, :nary_class) }
40
+ rule(:op_open_unicode) { arr_to_expression(Constants::OPEN_SYMBOLS.values, :open_paren) }
41
+ rule(:prefixed_primes) { str("pppprime") | str("ppprime") | str("pprime") | str("prime") }
42
+
43
+ rule(:combined_symbols) { op_combined_symbols | op_combined_unicode }
44
+ rule(:op_close_unicode) { arr_to_expression(Constants::CLOSE_SYMBOLS.values, :close_paren) }
45
+ rule(:op_unary_symbols) { arr_to_expression(Constants::UNARY_SYMBOLS.values, :unary_symbols) }
46
+ rule(:op_sub_operators) { arr_to_expression(Constants::SUB_OPERATORS.values, :sub_operators) }
47
+ rule(:op_sup_operators) { arr_to_expression(Constants::SUP_OPERATORS.values, :sup_operators) }
48
+
49
+ rule(:op_binary_symbols) { arr_to_expression(Constants::BINARY_SYMBOLS.values, :binary_symbols) }
50
+ rule(:op_binary_negated) { arr_to_expression(Constants::BINARY_SYMBOLS.values, :binary_negated_operator) }
51
+ rule(:op_sub_open_paren) { arr_to_expression(Constants::SUB_PARENTHESIS[:open].values, :sub_open_paren) }
52
+ rule(:op_sup_open_paren) { arr_to_expression(Constants::SUP_PARENTHESIS[:open].values, :sup_open_paren) }
53
+
54
+ rule(:op_accent_prefixed) { slash >> arr_to_expression(Constants::ACCENT_SYMBOLS.keys, :accent_symbols) }
55
+ rule(:op_unary_functions) { arr_to_expression(Constants::UNARY_FUNCTIONS, :unary_functions) }
56
+ rule(:op_sub_close_paren) { arr_to_expression(Constants::SUB_PARENTHESIS[:close].values, :sub_close_paren) }
57
+ rule(:op_sup_close_paren) { arr_to_expression(Constants::SUP_PARENTHESIS[:close].values, :sup_close_paren) }
58
+
59
+ rule(:op_prefixed_matrixs) { slash >> arr_to_expression(Constants::MATRIXS.keys, :matrixs) }
60
+ rule(:op_diacritic_belows) { arr_to_expression(Constants::DIACRITIC_BELOWS, :diacritic_belows) }
61
+ rule(:op_combined_symbols) { arr_to_expression(Constants::COMBINING_SYMBOLS.keys, :combined_symbols) }
62
+ rule(:op_ordinary_symbols) { arr_to_expression(Constants::ORDINARY_SYMBOLS.values, :ordinary_symbols) }
63
+ rule(:op_combined_unicode) { arr_to_expression(Constants::COMBINING_SYMBOLS.values, :combined_symbols) }
64
+ rule(:op_ordinary_negated) { arr_to_expression(Constants::ORDINARY_SYMBOLS.values, :ordinary_negated_operator) }
65
+ rule(:op_prefixed_negated) { slash >> arr_to_expression(Constants::PREFIXED_NEGATABLE_SYMBOLS, :negated_operator) }
66
+
67
+ rule(:op_unicode_fractions) { arr_to_expression(Constants::UNICODE_FRACTIONS.keys, :unicode_fractions) }
68
+ rule(:skip_symbols_prefixed) { slash >> arr_to_expression(Constants::SKIP_SYMBOLS.keys, :spaces) }
69
+ rule(:op_alphanumeric_fonts) { slash >> arr_to_expression(Constants::ALPHANUMERIC_FONTS_CLASSES, :font_class) }
70
+ rule(:op_h_bracket_prefixed) { slash >> arr_to_expression(Constants::HORIZONTAL_BRACKETS.keys, :hbracket_class) }
71
+ rule(:op_relational_symbols) { slash >> arr_to_expression(Constants::RELATIONAL_SYMBOLS.keys, :relational_symbols) }
72
+ rule(:op_diacritic_overlays) { arr_to_expression(Constants::DIACRITIC_OVERLAYS, :diacritic_overlays) }
73
+ rule(:op_relational_unicode) { arr_to_expression(Constants::RELATIONAL_SYMBOLS.values, :relational_symbols) }
74
+
75
+ rule(:op_unary_arg_functions) { arr_to_expression(Constants::UNARY_ARG_FUNCTIONS.values, :unary_arg_functions) }
76
+
77
+ rule(:op_prefixed_unary_symbols) { slash >> arr_to_expression(Constants::UNARY_SYMBOLS.keys, :unary_symbols) }
78
+ rule(:op_size_overrides_symbols) { str("&#x2132;") >> arr_to_expression(Constants::SIZE_OVERRIDES_SYMBOLS.keys, :size_overrides) }
79
+
80
+ rule(:op_binary_symbols_prefixed) { slash >> arr_to_expression(Constants::BINARY_SYMBOLS.keys, :binary_symbols) }
81
+ rule(:op_prefixed_binary_negated) { slash >> arr_to_expression(Constants::BINARY_SYMBOLS.keys, :binary_negated_operator) }
82
+
83
+ rule(:op_prefixed_ordinary_symbols) { slash >> arr_to_expression(Constants::ORDINARY_SYMBOLS.keys, :ordinary_symbols) }
84
+ rule(:op_prefixed_ordinary_negated) { slash >> arr_to_expression(Constants::ORDINARY_SYMBOLS.keys, :ordinary_negated_operator) }
85
+
86
+ rule(:op_prefixed_unary_arg_functions) { slash >> arr_to_expression(Constants::UNARY_ARG_FUNCTIONS.keys, :unary_arg_functions) }
87
+
88
+ def arr_to_expression(arr, name = nil)
89
+ type = arr.first.class
90
+ if arr.length > 1
91
+ arr.reduce do |expression, expr_string|
92
+ expression = str(expression).as(name) if expression.is_a?(type)
93
+ expression | str(expr_string).as(name)
94
+ end
95
+ else
96
+ str(arr.first).as(name)
97
+ end
98
+ end
99
+ end
100
+ end
101
+ end
102
+ end
@@ -0,0 +1,19 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Plurimath
4
+ class UnicodeMath
5
+ module ParsingRules
6
+ module Helper
7
+ def self.included(base)
8
+ base.extend(ClassMethods)
9
+ end
10
+ end
11
+
12
+ module ClassMethods
13
+ def rule(name, &block)
14
+ Parslet::Parser.rule(name, &block)
15
+ end
16
+ end
17
+ end
18
+ end
19
+ end