plurimath 0.2.0 → 0.2.2

Sign up to get free protection for your applications and to get access to all the features.
Files changed (121) hide show
  1. checksums.yaml +4 -4
  2. data/.github/workflows/rake.yml +13 -0
  3. data/.github/workflows/release.yml +22 -0
  4. data/.gitignore +1 -0
  5. data/.hound.yml +5 -0
  6. data/.rubocop.yml +8 -0
  7. data/AsciiMath-Supported-Data.adoc +2000 -0
  8. data/Gemfile +3 -0
  9. data/Latex-Supported-Data.adoc +1879 -0
  10. data/MathML-Supported-Data.adoc +287 -0
  11. data/README.adoc +96 -0
  12. data/lib/plurimath/asciimath/constants.rb +267 -229
  13. data/lib/plurimath/asciimath/parse.rb +125 -26
  14. data/lib/plurimath/asciimath/parser.rb +6 -3
  15. data/lib/plurimath/asciimath/transform.rb +1135 -208
  16. data/lib/plurimath/asciimath.rb +1 -1
  17. data/lib/plurimath/html/constants.rb +50 -0
  18. data/lib/plurimath/html/parse.rb +149 -0
  19. data/lib/plurimath/html/parser.rb +26 -0
  20. data/lib/plurimath/html/transform.rb +363 -0
  21. data/lib/plurimath/html.rb +1 -1
  22. data/lib/plurimath/latex/constants.rb +3729 -1906
  23. data/lib/plurimath/latex/parse.rb +167 -51
  24. data/lib/plurimath/latex/parser.rb +12 -4
  25. data/lib/plurimath/latex/transform.rb +598 -183
  26. data/lib/plurimath/math/base.rb +15 -0
  27. data/lib/plurimath/math/formula.rb +96 -11
  28. data/lib/plurimath/math/function/bar.rb +34 -0
  29. data/lib/plurimath/math/function/base.rb +32 -5
  30. data/lib/plurimath/math/function/binary_function.rb +104 -17
  31. data/lib/plurimath/math/function/cancel.rb +8 -0
  32. data/lib/plurimath/math/function/ceil.rb +3 -0
  33. data/lib/plurimath/math/function/color.rb +16 -6
  34. data/lib/plurimath/math/function/f.rb +8 -0
  35. data/lib/plurimath/math/function/fenced.rb +95 -3
  36. data/lib/plurimath/math/function/floor.rb +15 -0
  37. data/lib/plurimath/math/function/font_style/bold.rb +37 -0
  38. data/lib/plurimath/math/function/font_style/double_struck.rb +37 -0
  39. data/lib/plurimath/math/function/font_style/fraktur.rb +37 -0
  40. data/lib/plurimath/math/function/font_style/italic.rb +37 -0
  41. data/lib/plurimath/math/function/font_style/monospace.rb +37 -0
  42. data/lib/plurimath/math/function/font_style/normal.rb +37 -0
  43. data/lib/plurimath/math/function/font_style/sans-serif.rb +37 -0
  44. data/lib/plurimath/math/function/font_style/script.rb +37 -0
  45. data/lib/plurimath/math/function/font_style.rb +18 -25
  46. data/lib/plurimath/math/function/frac.rb +35 -5
  47. data/lib/plurimath/math/function/g.rb +7 -0
  48. data/lib/plurimath/math/function/hat.rb +12 -0
  49. data/lib/plurimath/math/function/inf.rb +21 -1
  50. data/lib/plurimath/math/function/int.rb +23 -2
  51. data/lib/plurimath/math/function/left.rb +25 -4
  52. data/lib/plurimath/math/function/lim.rb +40 -2
  53. data/lib/plurimath/math/function/limits.rb +8 -0
  54. data/lib/plurimath/math/function/log.rb +61 -4
  55. data/lib/plurimath/math/function/longdiv.rb +12 -0
  56. data/lib/plurimath/math/function/mbox.rb +31 -0
  57. data/lib/plurimath/math/function/menclose.rb +46 -0
  58. data/lib/plurimath/math/function/merror.rb +12 -0
  59. data/lib/plurimath/math/function/mod.rb +25 -4
  60. data/lib/plurimath/math/function/msgroup.rb +37 -0
  61. data/lib/plurimath/math/function/msline.rb +12 -0
  62. data/lib/plurimath/math/function/multiscript.rb +30 -0
  63. data/lib/plurimath/math/function/norm.rb +18 -1
  64. data/lib/plurimath/math/function/obrace.rb +17 -0
  65. data/lib/plurimath/math/function/oint.rb +2 -2
  66. data/lib/plurimath/math/function/over.rb +36 -0
  67. data/lib/plurimath/math/function/overset.rb +36 -7
  68. data/lib/plurimath/math/function/phantom.rb +28 -0
  69. data/lib/plurimath/math/function/power.rb +33 -9
  70. data/lib/plurimath/math/function/power_base.rb +110 -5
  71. data/lib/plurimath/math/function/prod.rb +29 -2
  72. data/lib/plurimath/math/function/right.rb +44 -0
  73. data/lib/plurimath/math/function/root.rb +27 -4
  74. data/lib/plurimath/math/function/rule.rb +33 -0
  75. data/lib/plurimath/math/function/scarries.rb +12 -0
  76. data/lib/plurimath/math/function/scarry.rb +12 -0
  77. data/lib/plurimath/math/function/sqrt.rb +24 -2
  78. data/lib/plurimath/math/function/stackrel.rb +27 -0
  79. data/lib/plurimath/math/function/substack.rb +7 -1
  80. data/lib/plurimath/math/function/sum.rb +56 -2
  81. data/lib/plurimath/math/function/sup.rb +3 -0
  82. data/lib/plurimath/math/function/table/align.rb +24 -0
  83. data/lib/plurimath/math/function/table/array.rb +44 -0
  84. data/lib/plurimath/math/function/table/bmatrix.rb +37 -0
  85. data/lib/plurimath/math/function/table/matrix.rb +32 -0
  86. data/lib/plurimath/math/function/table/multline.rb +24 -0
  87. data/lib/plurimath/math/function/table/pmatrix.rb +24 -0
  88. data/lib/plurimath/math/function/table/split.rb +24 -0
  89. data/lib/plurimath/math/function/table/vmatrix.rb +24 -0
  90. data/lib/plurimath/math/function/table.rb +190 -20
  91. data/lib/plurimath/math/function/td.rb +27 -9
  92. data/lib/plurimath/math/function/ternary_function.rb +83 -8
  93. data/lib/plurimath/math/function/text.rb +45 -8
  94. data/lib/plurimath/math/function/tr.rb +28 -4
  95. data/lib/plurimath/math/function/ubrace.rb +17 -0
  96. data/lib/plurimath/math/function/ul.rb +29 -0
  97. data/lib/plurimath/math/function/unary_function.rb +85 -7
  98. data/lib/plurimath/math/function/underline.rb +12 -0
  99. data/lib/plurimath/math/function/underover.rb +107 -0
  100. data/lib/plurimath/math/function/underset.rb +39 -0
  101. data/lib/plurimath/math/function/vec.rb +10 -0
  102. data/lib/plurimath/math/function.rb +13 -2
  103. data/lib/plurimath/math/number.rb +11 -3
  104. data/lib/plurimath/math/symbol.rb +57 -9
  105. data/lib/plurimath/math/unicode.rb +11 -0
  106. data/lib/plurimath/math.rb +15 -6
  107. data/lib/plurimath/mathml/constants.rb +224 -179
  108. data/lib/plurimath/mathml/parser.rb +24 -7
  109. data/lib/plurimath/mathml/transform.rb +249 -148
  110. data/lib/plurimath/mathml.rb +1 -1
  111. data/lib/plurimath/omml/parser.rb +42 -0
  112. data/lib/plurimath/omml/transform.rb +278 -0
  113. data/lib/plurimath/omml.rb +1 -1
  114. data/lib/plurimath/unitsml.rb +4 -0
  115. data/lib/plurimath/utility.rb +395 -0
  116. data/lib/plurimath/version.rb +1 -1
  117. data/plurimath.gemspec +1 -0
  118. metadata +66 -9
  119. data/.github/workflows/test.yml +0 -36
  120. data/README.md +0 -40
  121. data/lib/plurimath/mathml/parse.rb +0 -63
@@ -4,17 +4,38 @@ require "parslet"
4
4
  module Plurimath
5
5
  class Asciimath
6
6
  class Parse < Parslet::Parser
7
- rule(:base) { str("_").as(:_) }
7
+ rule(:td) { expression.as(:td) }
8
+ rule(:base) { str("_") }
9
+ rule(:power) { str("^") }
10
+ rule(:space) { match(/\s+/) }
11
+ rule(:comma) { (str(",") >> space.maybe) }
12
+ rule(:number) do
13
+ (match("[0-9]").repeat(1) >> str(".") >> match("[0-9]").repeat(1)).as(:number) |
14
+ match("[0-9]").repeat(1).as(:number) |
15
+ str(".").as(:symbol)
16
+ end
8
17
 
9
- rule(:power) { str("^").as(:^) }
18
+ rule(:controversial_symbols) { power_base | expression }
19
+ rule(:left_right_open_paren) { str("(") | str("[") }
20
+ rule(:left_right_close_paren) { str(")") | str("]") }
21
+ rule(:color_left_parenthesis) { str("(") | str("[") | str("{") }
22
+ rule(:color_right_parenthesis) { str(")") | str("]") | str("}") }
10
23
 
11
- rule(:symbols) { arr_to_expression(Constants::SYMBOLS, :symbol) }
24
+ rule(:binary_classes) do
25
+ arr_to_expression(Constants::BINARY_CLASSES, :binary_class)
26
+ end
12
27
 
13
- rule(:unary_functions) { arr_to_expression(Constants::UNARY_CLASSES) }
28
+ rule(:sub_sup_classes) do
29
+ arr_to_expression(Constants::SUB_SUP_CLASSES, :binary_class)
30
+ end
14
31
 
15
- rule(:font_style) { arr_to_expression(Constants::FONT_STYLES, :fonts) }
32
+ rule(:open_table) do
33
+ arr_to_expression(Constants::TABLE_PARENTHESIS.keys, :table_left)
34
+ end
16
35
 
17
- rule(:binary_functions) { arr_to_expression(Constants::BINARY_CLASSES) }
36
+ rule(:close_table) do
37
+ arr_to_expression(Constants::TABLE_PARENTHESIS.values, :table_right)
38
+ end
18
39
 
19
40
  rule(:lparen) do
20
41
  Constants::PARENTHESIS.keys.reduce do |expression, parenthesis|
@@ -30,56 +51,134 @@ module Plurimath
30
51
  end
31
52
  end
32
53
 
54
+ rule(:left_right) do
55
+ (str("left") >> left_right_open_paren.as(:left) >> iteration.as(:left_right_value) >> str("right") >> left_right_close_paren.as(:right)) |
56
+ ((table.as(:numerator) >> space.maybe >> match(/(?<!\/)\/(?!\/)/) >> space.maybe >> iteration.as(:denominator)).as(:frac) >> expression) |
57
+ (table.as(:table) >> expression.maybe)
58
+ end
59
+
33
60
  rule(:quoted_text) do
34
61
  str('"') >> match("[^\"]").repeat.as(:text) >> str('"')
35
62
  end
36
63
 
37
64
  rule(:symbol_text_or_integer) do
38
- binary_functions |
39
- unary_functions |
40
- symbols |
65
+ sub_sup_classes |
66
+ binary_classes |
67
+ hash_to_expression(Constants.precompile_constants) |
68
+ (match(/[0-9]/).as(:number) >> comma.as(:comma)).repeat(1).as(:comma_separated) |
41
69
  quoted_text |
70
+ (str("d").as(:d) >> str("x").as(:x)).as(:intermediate_exp) |
42
71
  match["a-zA-Z"].as(:symbol) |
43
- match("[0-9]").repeat(1).as(:number)
72
+ match(/[^\[{(\\\/@;:.,'"|\]})0-9a-zA-Z\-><$%^&*_=+!`~\s?]/).as(:symbol) |
73
+ number
74
+ end
75
+
76
+ rule(:power_base) do
77
+ (base >> space.maybe >> sequence.as(:base_value) >> power >> space.maybe >> sequence.as(:power_value)) |
78
+ (space.maybe >> base >> space.maybe >> sequence.as(:base_value)).as(:base) |
79
+ (space.maybe >> power >> space.maybe >> sequence.as(:power_value)).as(:power) |
80
+ (space.maybe >> base >> space.maybe >> power.as(:symbol).as(:base_value)).as(:base) |
81
+ (space.maybe >> power >> space.maybe >> base.as(:symbol).as(:power_value)).as(:power)
82
+ end
83
+
84
+ rule(:power_base_rules) do
85
+ (sub_sup_classes >> power_base).as(:power_base) |
86
+ (binary_classes >> space.maybe >> sequence.as(:base_value).maybe >> space.maybe >> sequence.as(:power_value).maybe).as(:power_base) |
87
+ (sequence.as(:power_base) >> power_base).as(:power_base)
88
+ end
89
+
90
+ rule(:table) do
91
+ (open_table.as(:table_left) >> tr >> close_table.as(:table_right)) |
92
+ (open_table.as(:table_left) >> tr >> str("}").as(:table_right)) |
93
+ (str("norm").as(:norm) >> open_table.as(:table_left) >> tr >> close_table.as(:table_right)) |
94
+ (str("{").as(:table_left) >> tr >> close_table.as(:table_right)) |
95
+ (str("|").as(:table_left) >> tr >> str("|").as(:table_right)) |
96
+ (str("left") >> left_right_open_paren.as(:left) >> tr >> str("right") >> left_right_close_paren.as(:right))
97
+ end
98
+
99
+ rule(:tr) do
100
+ ((left_right_open_paren.as(:open_tr) >> td.as(:tds_list) >> left_right_close_paren).as(:table_row) >> comma >> tr.as(:expr)) |
101
+ (left_right_open_paren.as(:open_tr) >> td.as(:tds_list) >> left_right_close_paren).as(:table_row)
102
+ end
103
+
104
+ rule(:color_value) do
105
+ (color_left_parenthesis.capture(:paren).as(:lparen) >> expression.as(:rgb_color) >> color_right_parenthesis.maybe.as(:rparen)).as(:intermediate_exp) |
106
+ iteration
44
107
  end
45
108
 
46
109
  rule(:sequence) do
47
- (lparen >> expression >> rparen).as(:intermediate_exp) |
48
- (binary_functions >> sequence.as(:base) >> sequence.maybe.as(:exponent)).as(:binary) |
49
- (str("text") >> lparen.capture(:paren) >> read_text >> rparen) |
50
- (unary_functions >> sequence).as(:unary) |
51
- (font_style >> sequence).as(:fonts) |
110
+ (lparen.as(:lparen) >> space.maybe >> expression.maybe.as(:expr) >> space.maybe >> rparen.maybe.as(:rparen)).as(:intermediate_exp) |
111
+ (str("text") >> lparen.capture(:paren).as(:lparen) >> read_text.as(:text) >> rparen.maybe.as(:rparen)).as(:intermediate_exp) |
52
112
  symbol_text_or_integer
53
113
  end
54
114
 
115
+ rule(:frac) do
116
+ (sequence.as(:numerator) >> space.maybe >> match(/(?<!\/)\/(?!\/)/) >> space.maybe >> iteration.as(:denominator)).as(:frac) |
117
+ ((power_base_rules | power_base).as(:numerator) >> match(/(?<!\/)\/(?!\/)/) >> iteration.as(:denominator)).as(:frac)
118
+ end
119
+
120
+ rule(:mod) do
121
+ (sequence.as(:dividend) >> space.maybe >> str("mod").as(:mod) >> space.maybe >> iteration.as(:divisor)).as(:mod) |
122
+ ((power_base_rules >> power_base).as(:dividend) >> space.maybe >> str("mod").as(:mod) >> space.maybe >> iteration.as(:divisor)).as(:mod) |
123
+ (power_base_rules.as(:dividend) >> space.maybe >> str("mod").as(:mod) >> space.maybe >> iteration.as(:divisor)).as(:mod)
124
+ end
125
+
55
126
  rule(:iteration) do
56
- (sequence.as(:dividend) >> str("mod").as(:mod) >> sequence.as(:divisor)).as(:mod) |
57
- (sequence >> base >> sequence.as(:base) >> power >> sequence.as(:exponent)).as(:power_base) |
58
- (sequence >> base >> sequence).as(:base) |
59
- (sequence >> power >> sequence).as(:power) |
127
+ table.as(:table) |
128
+ comma.as(:comma) |
129
+ mod |
130
+ (sequence.as(:sequence) >> space.maybe >> str("//").as(:symbol)) |
131
+ (str("color") >> color_value.as(:color) >> sequence.as(:color_value)) |
132
+ frac |
133
+ (power_base_rules >> power_base) |
134
+ power_base_rules |
60
135
  sequence.as(:sequence) |
61
- str(" ")
136
+ space
62
137
  end
63
138
 
64
139
  rule(:expression) do
65
- (iteration >> expression).as(:expr) |
66
- (iteration >> str("/").as(:/) >> iteration).as(:expr) |
67
- str("")
140
+ left_right.as(:left_right) |
141
+ (iteration >> space.maybe >> expression).as(:expr) |
142
+ (base.as(:symbol) >> expression.maybe).as(:expr) |
143
+ (power.as(:symbol) >> expression.maybe).as(:expr) |
144
+ str("") |
145
+ (rparen.as(:rparen) >> space.maybe >> controversial_symbols >> comma.as(:comma).maybe >> expression).repeat(1).as(:expr) |
146
+ (power.as(:symbol) >> space.maybe >> expression).as(:expr) |
147
+ comma.as(:comma).maybe
68
148
  end
69
149
 
70
150
  root :expression
71
151
 
72
152
  def arr_to_expression(arr, name = nil)
153
+ type = arr.first.class
73
154
  arr.reduce do |expression, expr_string|
74
- expression = str(expression).as(name || expression) if expression.is_a?(Symbol)
75
- expression | str(expr_string).as(name || expr_string)
155
+ expression = str(expression).as(name) if expression.is_a?(type)
156
+ expression | str(expr_string).as(name)
76
157
  end
77
158
  end
78
159
 
79
160
  def read_text
80
161
  dynamic do |_sour, context|
81
162
  rparen = Constants::PARENTHESIS[context.captures[:paren].to_sym]
82
- match("[^#{rparen}]").repeat.as(:text)
163
+ match("[^#{rparen}]").repeat
164
+ end
165
+ end
166
+
167
+ def hash_to_expression(arr)
168
+ type = arr.first.class
169
+ @@expression ||= arr.reduce do |expression, expr_string|
170
+ expression = dynamic_parser_rules(expression) if expression.is_a?(type)
171
+ expression | dynamic_parser_rules(expr_string)
172
+ end
173
+ end
174
+
175
+ def dynamic_parser_rules(expr)
176
+ first_value = str(expr.first.to_s)
177
+ case expr.last
178
+ when :symbol then first_value.as(:symbol)
179
+ when :unary_class then (first_value.as(:unary_class) >> space.maybe >> sequence.maybe).as(:unary)
180
+ when :fonts then first_value.as(:fonts_class) >> space.maybe >> sequence.as(:fonts_value)
181
+ when :special_fonts then first_value.as(:bold_fonts)
83
182
  end
84
183
  end
85
184
  end
@@ -10,14 +10,17 @@ module Plurimath
10
10
 
11
11
  def initialize(text)
12
12
  @text = text
13
+ &.gsub(/(\|:|:\|)/, "|")
14
+ &.gsub(/(\{:)/, "ℒ")
15
+ &.gsub(/(:\})/, "ℛ")
13
16
  end
14
17
 
15
18
  def parse
16
19
  nodes = Parse.new.parse(text)
17
- transformed_tree = Plurimath::Asciimath::Transform.new.apply(nodes)
18
- return transformed_tree if transformed_tree.is_a?(Plurimath::Math::Formula)
20
+ transformed_tree = Transform.new.apply(nodes)
21
+ return transformed_tree if transformed_tree.is_a?(Math::Formula)
19
22
 
20
- Plurimath::Math::Formula.new(transformed_tree)
23
+ Math::Formula.new(transformed_tree)
21
24
  end
22
25
  end
23
26
  end