plurimath 0.11.16 → 0.11.18

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (189) hide show
  1. checksums.yaml +4 -4
  2. data/.github/workflows/dependent-tests.yml +2 -2
  3. data/.github/workflows/docs.yml +26 -0
  4. data/.github/workflows/gum-reference.yml +48 -0
  5. data/.github/workflows/performance-ci-legs.yml +54 -0
  6. data/.github/workflows/performance.yml +99 -0
  7. data/.github/workflows/roundtrip-cases.yml +33 -0
  8. data/.rubocop_todo.yml +0 -138
  9. data/Gemfile +13 -0
  10. data/Rakefile +2 -0
  11. data/docs/asciimath.adoc +578 -0
  12. data/docs/core-model-contract.adoc +748 -0
  13. data/docs/gum-evaluation.adoc +368 -0
  14. data/docs/latex.adoc +335 -0
  15. data/docs/mathml.adoc +638 -0
  16. data/docs/native-evaluation-request.md +28 -0
  17. data/docs/numeric-request.md +19 -0
  18. data/docs/omml.adoc +589 -0
  19. data/docs/performance-contract.adoc +279 -0
  20. data/docs/pma.adoc +949 -0
  21. data/docs/quantity-request.md +19 -0
  22. data/docs/uncertainty-asciimath-tree.md +2 -3
  23. data/docs/uncertainty.adoc +401 -425
  24. data/docs/unicodemath.adoc +453 -0
  25. data/docs/unitsml.adoc +414 -0
  26. data/lib/plurimath/asciimath/constants.rb +0 -98
  27. data/lib/plurimath/asciimath/mapper/rules.rb +1400 -0
  28. data/lib/plurimath/asciimath/mapper.rb +148 -0
  29. data/lib/plurimath/asciimath/parse.rb +26 -18
  30. data/lib/plurimath/asciimath/parser.rb +25 -11
  31. data/lib/plurimath/asciimath/slice_adapter.rb +1 -1
  32. data/lib/plurimath/asciimath.rb +1 -1
  33. data/lib/plurimath/catalog.rb +4 -5
  34. data/lib/plurimath/cli/convert.rb +3 -2
  35. data/lib/plurimath/cli.rb +2 -1
  36. data/lib/plurimath/configuration.rb +3 -0
  37. data/lib/plurimath/evaluation_request/quantity.rb +254 -0
  38. data/lib/plurimath/evaluation_request.rb +160 -0
  39. data/lib/plurimath/latex/constants.rb +0 -84
  40. data/lib/plurimath/latex/mapper.rb +418 -0
  41. data/lib/plurimath/latex/parse.rb +28 -288
  42. data/lib/plurimath/latex/parser.rb +49 -4
  43. data/lib/plurimath/latex.rb +1 -1
  44. data/lib/plurimath/math/core.rb +266 -1
  45. data/lib/plurimath/math/evaluation/accuracy.rb +62 -0
  46. data/lib/plurimath/math/evaluation/calibration.rb +85 -0
  47. data/lib/plurimath/math/evaluation/compiled_polynomial.rb +99 -0
  48. data/lib/plurimath/math/evaluation/compiled_rational.rb +131 -0
  49. data/lib/plurimath/math/evaluation/conformity.rb +133 -0
  50. data/lib/plurimath/math/evaluation/correlated_normal_sampler.rb +74 -0
  51. data/lib/plurimath/math/evaluation/covariance.rb +173 -0
  52. data/lib/plurimath/math/evaluation/distribution_sampler.rb +126 -0
  53. data/lib/plurimath/math/evaluation/evaluator.rb +236 -22
  54. data/lib/plurimath/math/evaluation/grouped_statistics.rb +56 -0
  55. data/lib/plurimath/math/evaluation/gum.rb +410 -0
  56. data/lib/plurimath/math/evaluation/implicit_model.rb +66 -0
  57. data/lib/plurimath/math/evaluation/monte_carlo.rb +198 -0
  58. data/lib/plurimath/math/evaluation/quantity_evaluator.rb +127 -0
  59. data/lib/plurimath/math/evaluation/result.rb +45 -0
  60. data/lib/plurimath/math/evaluation/statistics.rb +102 -0
  61. data/lib/plurimath/math/evaluation/taylor.rb +162 -0
  62. data/lib/plurimath/math/evaluation/wichmann_hill.rb +40 -0
  63. data/lib/plurimath/math/evaluation.rb +22 -0
  64. data/lib/plurimath/math/formula/mrow.rb +6 -1
  65. data/lib/plurimath/math/formula.rb +22 -4
  66. data/lib/plurimath/math/function/abs.rb +3 -1
  67. data/lib/plurimath/math/function/arccos.rb +1 -1
  68. data/lib/plurimath/math/function/arcsin.rb +1 -1
  69. data/lib/plurimath/math/function/arctan.rb +1 -1
  70. data/lib/plurimath/math/function/bar.rb +2 -1
  71. data/lib/plurimath/math/function/base.rb +2 -1
  72. data/lib/plurimath/math/function/binary_function.rb +1 -1
  73. data/lib/plurimath/math/function/ceil.rb +3 -1
  74. data/lib/plurimath/math/function/color.rb +2 -1
  75. data/lib/plurimath/math/function/cos.rb +1 -1
  76. data/lib/plurimath/math/function/cosh.rb +1 -1
  77. data/lib/plurimath/math/function/cot.rb +1 -1
  78. data/lib/plurimath/math/function/coth.rb +1 -1
  79. data/lib/plurimath/math/function/csc.rb +1 -1
  80. data/lib/plurimath/math/function/csch.rb +1 -1
  81. data/lib/plurimath/math/function/ddot.rb +2 -1
  82. data/lib/plurimath/math/function/dot.rb +2 -1
  83. data/lib/plurimath/math/function/exp.rb +1 -1
  84. data/lib/plurimath/math/function/fenced.rb +5 -3
  85. data/lib/plurimath/math/function/floor.rb +3 -1
  86. data/lib/plurimath/math/function/font_style/bold.rb +1 -1
  87. data/lib/plurimath/math/function/font_style/double_struck.rb +1 -1
  88. data/lib/plurimath/math/function/font_style/fraktur.rb +1 -1
  89. data/lib/plurimath/math/function/font_style/italic.rb +1 -1
  90. data/lib/plurimath/math/function/font_style/monospace.rb +1 -1
  91. data/lib/plurimath/math/function/font_style/normal.rb +22 -1
  92. data/lib/plurimath/math/function/font_style/sans-serif.rb +1 -1
  93. data/lib/plurimath/math/function/font_style/script.rb +1 -1
  94. data/lib/plurimath/math/function/frac.rb +2 -1
  95. data/lib/plurimath/math/function/hat.rb +2 -1
  96. data/lib/plurimath/math/function/int.rb +3 -2
  97. data/lib/plurimath/math/function/lg.rb +1 -1
  98. data/lib/plurimath/math/function/linebreak.rb +2 -1
  99. data/lib/plurimath/math/function/ln.rb +1 -1
  100. data/lib/plurimath/math/function/log.rb +4 -1
  101. data/lib/plurimath/math/function/longdiv.rb +3 -0
  102. data/lib/plurimath/math/function/mpadded.rb +2 -1
  103. data/lib/plurimath/math/function/msgroup.rb +3 -0
  104. data/lib/plurimath/math/function/multiscript.rb +4 -0
  105. data/lib/plurimath/math/function/nary.rb +9 -3
  106. data/lib/plurimath/math/function/norm.rb +3 -1
  107. data/lib/plurimath/math/function/obrace.rb +2 -1
  108. data/lib/plurimath/math/function/oint.rb +3 -2
  109. data/lib/plurimath/math/function/overleftrightarrow.rb +2 -1
  110. data/lib/plurimath/math/function/overset.rb +2 -1
  111. data/lib/plurimath/math/function/power.rb +1 -1
  112. data/lib/plurimath/math/function/power_base.rb +1 -1
  113. data/lib/plurimath/math/function/prod.rb +3 -2
  114. data/lib/plurimath/math/function/sec.rb +1 -1
  115. data/lib/plurimath/math/function/sech.rb +1 -1
  116. data/lib/plurimath/math/function/semantics.rb +35 -0
  117. data/lib/plurimath/math/function/sin.rb +1 -1
  118. data/lib/plurimath/math/function/sinh.rb +1 -1
  119. data/lib/plurimath/math/function/sqrt.rb +3 -2
  120. data/lib/plurimath/math/function/sum.rb +3 -2
  121. data/lib/plurimath/math/function/table/align.rb +1 -1
  122. data/lib/plurimath/math/function/table/bmatrix.rb +1 -1
  123. data/lib/plurimath/math/function/table/cases.rb +4 -0
  124. data/lib/plurimath/math/function/table/multline.rb +1 -1
  125. data/lib/plurimath/math/function/table/pmatrix.rb +1 -1
  126. data/lib/plurimath/math/function/table/split.rb +1 -1
  127. data/lib/plurimath/math/function/table/vmatrix.rb +1 -1
  128. data/lib/plurimath/math/function/table.rb +19 -2
  129. data/lib/plurimath/math/function/tan.rb +1 -1
  130. data/lib/plurimath/math/function/tanh.rb +1 -1
  131. data/lib/plurimath/math/function/td.rb +4 -0
  132. data/lib/plurimath/math/function/ternary_function.rb +2 -2
  133. data/lib/plurimath/math/function/text.rb +3 -0
  134. data/lib/plurimath/math/function/tilde.rb +2 -1
  135. data/lib/plurimath/math/function/tr.rb +3 -0
  136. data/lib/plurimath/math/function/ubrace.rb +2 -1
  137. data/lib/plurimath/math/function/ul.rb +2 -1
  138. data/lib/plurimath/math/function/unary_function.rb +1 -1
  139. data/lib/plurimath/math/function/uncertainty.rb +26 -11
  140. data/lib/plurimath/math/function/underset.rb +2 -1
  141. data/lib/plurimath/math/function/unitsml.rb +18 -6
  142. data/lib/plurimath/math/function/vec.rb +2 -1
  143. data/lib/plurimath/math/number.rb +30 -12
  144. data/lib/plurimath/math/symbol_index.rb +10254 -0
  145. data/lib/plurimath/math/symbol_table.rb +89 -0
  146. data/lib/plurimath/math/symbols/paren.rb +7 -0
  147. data/lib/plurimath/math/symbols/symbol.rb +30 -3
  148. data/lib/plurimath/math/symbols.rb +81 -5
  149. data/lib/plurimath/math.rb +8 -0
  150. data/lib/plurimath/mathml/parser.rb +2 -0
  151. data/lib/plurimath/mathml/translator.rb +17 -3
  152. data/lib/plurimath/native_kernel.rb +64 -0
  153. data/lib/plurimath/omml/parser.rb +1 -0
  154. data/lib/plurimath/omml/translator.rb +2 -1
  155. data/lib/plurimath/pma.rb +19 -0
  156. data/lib/plurimath/unicode_math/constants.rb +0 -258
  157. data/lib/plurimath/unicode_math/mapper/capture_order.rb +142 -0
  158. data/lib/plurimath/unicode_math/mapper/rules.rb +4086 -0
  159. data/lib/plurimath/unicode_math/mapper.rb +146 -0
  160. data/lib/plurimath/unicode_math/parse.rb +54 -294
  161. data/lib/plurimath/unicode_math/parser.rb +71 -2
  162. data/lib/plurimath/unicode_math.rb +1 -2
  163. data/lib/plurimath/unitsml.rb +0 -1
  164. data/lib/plurimath/utility.rb +34 -12
  165. data/lib/plurimath/version.rb +1 -1
  166. data/lib/plurimath.rb +42 -3
  167. data/plurimath.gemspec +3 -1
  168. data/scripts/check_gum_references.rb +69 -0
  169. data/scripts/check_suite_budget.rb +329 -0
  170. data/scripts/docs/asciimath_tables.rb +363 -0
  171. data/scripts/docs/core_inventory.rb +432 -0
  172. data/scripts/docs/format_specs.rb +672 -0
  173. data/scripts/generate_symbol_index.rb +118 -0
  174. data/scripts/measure_require.rb +362 -0
  175. data/scripts/replay_asciimath_corpus.rb +7 -7
  176. data/scripts/replay_stage_roundtrips.rb +242 -0
  177. data/scripts/require_baseline.json +34 -0
  178. data/scripts/roundtrip_cases.rb +462 -0
  179. metadata +97 -16
  180. data/lib/plurimath/asciimath/transform.rb +0 -1395
  181. data/lib/plurimath/latex/transform.rb +0 -1006
  182. data/lib/plurimath/unicode_math/parsing_rules/absence_rules.rb +0 -150
  183. data/lib/plurimath/unicode_math/parsing_rules/common_rules.rb +0 -136
  184. data/lib/plurimath/unicode_math/parsing_rules/constants_rules.rb +0 -136
  185. data/lib/plurimath/unicode_math/parsing_rules/helper.rb +0 -13
  186. data/lib/plurimath/unicode_math/parsing_rules/masked.rb +0 -84
  187. data/lib/plurimath/unicode_math/parsing_rules/sub_sup.rb +0 -281
  188. data/lib/plurimath/unicode_math/parsing_rules.rb +0 -14
  189. data/lib/plurimath/unicode_math/transform.rb +0 -4058
@@ -0,0 +1,432 @@
1
+ # frozen_string_literal: true
2
+
3
+ # Regenerates the generated regions of docs/core-model-contract.adoc: the
4
+ # inventory of every Plurimath::Math class, read from the loaded code on the
5
+ # checked-out revision.
6
+ #
7
+ # Usage, from the Plurimath checkout:
8
+ #
9
+ # bundle exec ruby scripts/docs/core_inventory.rb [--check]
10
+ #
11
+ # Without --check it rewrites the regions between
12
+ # "// BEGIN GENERATED <name>" and "// END GENERATED <name>" in
13
+ # docs/core-model-contract.adoc. With --check it writes nothing and exits 1
14
+ # when the file differs from what it would write. The output depends only on
15
+ # the source tree, never on time or local paths.
16
+ #
17
+ # What is measured, and how:
18
+ #
19
+ # * Classes: every Class whose name starts with "Plurimath::Math::", after
20
+ # every file under lib/plurimath/math is required.
21
+ # * Fields: methods created by attr_accessor, attr_reader or attr_writer. A
22
+ # method counts as a field when its source line is an attr_* call.
23
+ # * Constructor: the owner of #initialize and its parameters, with default
24
+ # expressions copied from the source by Prism.
25
+ # * Public methods: public_instance_methods(false), minus the field methods.
26
+ # * Writers: the six per-format writer methods listed in WRITERS.
27
+ # * Parser-only methods: public methods defined under lib/plurimath/math whose
28
+ # only call sites in lib/ are in the in-tree LaTeX and UnicodeMath layers.
29
+
30
+ require "prism"
31
+
32
+ module CoreInventory
33
+ ROOT = File.expand_path("../..", __dir__)
34
+ DOC = File.join(ROOT, "docs/core-model-contract.adoc")
35
+ MATH_DIR = File.join(ROOT, "lib/plurimath/math")
36
+
37
+ WRITERS = %i[
38
+ to_asciimath to_latex to_mathml_without_math_tag
39
+ to_omml_without_math_tag to_unicodemath to_html
40
+ ].freeze
41
+
42
+ # Directories (under lib/plurimath) that hold the LaTeX and UnicodeMath
43
+ # layers #531 reduced to mappers from the stage-gem models.
44
+ PARSER_LAYERS = %w[latex unicode_math].freeze
45
+
46
+ FAMILY_ORDER = %w[
47
+ core formula function-unary function-binary function-ternary
48
+ function-nary function-table uncertainty-unitsml symbols-symbol
49
+ symbols-paren support
50
+ ].freeze
51
+
52
+ MARKS = %w[stays-core removed-by-531 support].freeze
53
+
54
+ # Subclasses of Core that are not tree nodes. Semantics::Annotation is the
55
+ # entry type of the Semantics annotation list; no writer reaches it.
56
+ NON_NODES = %w[Plurimath::Math::Function::Semantics::Annotation].freeze
57
+ SYMBOL_FAMILIES = %w[symbols-symbol symbols-paren].freeze
58
+ BASE_SYMBOLS = %w[Plurimath::Math::Symbols::Symbol Plurimath::Math::Symbols::Paren].freeze
59
+
60
+ FAMILY_TITLES = {
61
+ "core" => "Core and Number",
62
+ "formula" => "Formula",
63
+ "function-unary" => "Function, unary",
64
+ "function-binary" => "Function, binary",
65
+ "function-ternary" => "Function, ternary",
66
+ "function-nary" => "Function, n-ary",
67
+ "function-table" => "Function, table",
68
+ "uncertainty-unitsml" => "Uncertainty and UnitsML nodes",
69
+ "symbols-symbol" => "Symbols",
70
+ "symbols-paren" => "Symbols, parentheses",
71
+ "support" => "Support (not tree nodes)",
72
+ }.freeze
73
+
74
+ class Error < StandardError; end
75
+
76
+ module_function
77
+
78
+ def load_tree
79
+ $LOAD_PATH.unshift(File.join(ROOT, "lib"))
80
+ require "plurimath"
81
+ Dir.glob(File.join(MATH_DIR, "**/*.rb")).each { |file| require file }
82
+ end
83
+
84
+ def m
85
+ Plurimath::Math
86
+ end
87
+
88
+ def classes
89
+ @classes ||= ObjectSpace.each_object(Class)
90
+ .select { |k| k.name&.start_with?("Plurimath::Math::") }
91
+ .sort_by(&:name)
92
+ end
93
+
94
+ def short(klass)
95
+ klass.name.delete_prefix("Plurimath::Math::")
96
+ end
97
+
98
+ def node?(klass)
99
+ klass <= m::Core && !NON_NODES.include?(klass.name)
100
+ end
101
+
102
+ def family(klass)
103
+ f = m::Function
104
+ s = m::Symbols
105
+ return "support" unless node?(klass)
106
+ return "formula" if klass <= m::Formula
107
+ return "function-unary" if klass <= f::UnaryFunction
108
+ return "function-binary" if klass <= f::BinaryFunction
109
+ return "function-ternary" if klass <= f::TernaryFunction
110
+ return "function-nary" if klass <= f::Nary
111
+ return "function-table" if klass <= f::Table
112
+ return "uncertainty-unitsml" if [f::Uncertainty, f::Unitsml].include?(klass)
113
+ return "symbols-paren" if klass <= s::Paren
114
+ return "symbols-symbol" if klass <= s::Symbol
115
+
116
+ "core"
117
+ end
118
+
119
+ def source_lines(file)
120
+ @source_lines ||= {}
121
+ @source_lines[file] ||= File.readlines(file)
122
+ end
123
+
124
+ def relative(file)
125
+ file.delete_prefix("#{ROOT}/")
126
+ end
127
+
128
+ def field_method?(klass, name)
129
+ file, line = klass.instance_method(name).source_location
130
+ return false unless file && line
131
+
132
+ source_lines(file)[line - 1].to_s.match?(/\battr_(accessor|reader|writer)\b/)
133
+ end
134
+
135
+ # Fields declared by this class itself (not inherited).
136
+ def own_fields(klass)
137
+ @own_fields ||= {}
138
+ @own_fields[klass] ||= begin
139
+ names = klass.public_instance_methods(false) +
140
+ klass.private_instance_methods(false)
141
+ names.select { |n| field_method?(klass, n) }
142
+ .map { |n| n.to_s.delete_suffix("=") }.uniq.sort
143
+ end
144
+ end
145
+
146
+ def fields(klass)
147
+ klass.ancestors.select { |a| a.is_a?(Class) && a.name&.start_with?("Plurimath::Math::") }
148
+ .flat_map { |a| own_fields(a) }.uniq.sort
149
+ end
150
+
151
+ def field_owner(klass)
152
+ klass.ancestors.find do |a|
153
+ a.is_a?(Class) && a.name&.start_with?("Plurimath::Math::") &&
154
+ !own_fields(a).empty?
155
+ end
156
+ end
157
+
158
+ def own_methods(klass)
159
+ @own_methods ||= {}
160
+ @own_methods[klass] ||= begin
161
+ fields = own_fields(klass)
162
+ klass.public_instance_methods(false)
163
+ .reject { |n| fields.include?(n.to_s.delete_suffix("=")) }
164
+ .map(&:to_s).sort
165
+ end
166
+ end
167
+
168
+ def constructor_owner(klass)
169
+ owner = klass.instance_method(:initialize).owner
170
+ owner.name&.start_with?("Plurimath::Math::") ? owner : nil
171
+ end
172
+
173
+ def def_node(file, line)
174
+ @parsed ||= {}
175
+ @parsed[file] ||= Prism.parse_file(file).value
176
+ found = nil
177
+ queue = [@parsed[file]]
178
+ until queue.empty?
179
+ node = queue.shift
180
+ if node.is_a?(Prism::DefNode) && node.name == :initialize &&
181
+ node.location.start_line == line
182
+ found = node
183
+ break
184
+ end
185
+ queue.concat(node.compact_child_nodes)
186
+ end
187
+ found
188
+ end
189
+
190
+ def signature(klass)
191
+ owner = constructor_owner(klass)
192
+ return "(Object default)" unless owner
193
+
194
+ file, line = owner.instance_method(:initialize).source_location
195
+ node = def_node(file, line)
196
+ raise Error, "no def initialize at #{relative(file)}:#{line}" unless node
197
+
198
+ params = node.parameters
199
+ return "()" unless params
200
+
201
+ parts = params.requireds.map(&:slice)
202
+ parts.concat(params.optionals.map { |p| "#{p.name} = #{p.value.slice}" })
203
+ parts << params.rest.slice if params.rest
204
+ parts.concat(params.posts.map(&:slice))
205
+ parts.concat(params.keywords.map { |p| keyword(p) })
206
+ parts << params.keyword_rest.slice if params.keyword_rest
207
+ parts << params.block.slice if params.block
208
+ "(#{parts.join(', ').gsub(/\s+/, ' ')})"
209
+ end
210
+
211
+ def keyword(param)
212
+ return "#{param.name}:" unless param.is_a?(Prism::OptionalKeywordParameterNode)
213
+
214
+ "#{param.name}: #{param.value.slice}"
215
+ end
216
+
217
+ def writers(klass)
218
+ WRITERS.select { |w| klass.method_defined?(w) }
219
+ end
220
+
221
+ # stays-core: a tree node that carries at least one writer, plus the
222
+ # abstract root Core. removed-by-531: a tree node with no writer at all,
223
+ # so it cannot reach any output and only serves the parser layers.
224
+ # support: not a tree node.
225
+ def mark(klass)
226
+ return "support" unless node?(klass)
227
+ return "stays-core" if klass == m::Core || !writers(klass).empty?
228
+
229
+ "removed-by-531"
230
+ end
231
+
232
+ def missing_writers(klass)
233
+ return [] unless node?(klass) && klass != m::Core
234
+
235
+ WRITERS - writers(klass)
236
+ end
237
+
238
+ def lib_files
239
+ @lib_files ||= Dir.glob(File.join(ROOT, "lib/**/*.rb"))
240
+ .to_h { |f| [relative(f), File.read(f)] }
241
+ end
242
+
243
+ def layer(path)
244
+ path.delete_prefix("lib/plurimath/").split("/").first.delete_suffix(".rb")
245
+ end
246
+
247
+ # Public methods defined on tree nodes whose only call sites are in the
248
+ # parser layers #531 removes.
249
+ def parser_only_methods
250
+ node_classes = classes.select { |k| node?(k) }
251
+ names = node_classes.flat_map { |k| own_methods(k) }.uniq.sort
252
+ names -= Object.public_instance_methods.map(&:to_s)
253
+ names.filter_map do |name|
254
+ pattern = /(?:\.|&\.|send\(:)#{Regexp.escape(name)}(?![\w?!=])/
255
+ layers = lib_files.select { |_, s| s.match?(pattern) }.keys.map { |f| layer(f) }.uniq
256
+ next if layers.empty? || !(layers - PARSER_LAYERS).empty?
257
+
258
+ owners = node_classes.select { |k| own_methods(k).include?(name) }.map { |k| short(k) }
259
+ [name, owners, layers.sort]
260
+ end
261
+ end
262
+
263
+ def input_table_counts
264
+ symbols = classes.select { |k| k <= m::Symbols::Symbol && k != m::Symbols::Symbol }
265
+ langs = symbols.flat_map { |k| k::INPUT.keys }.uniq.sort
266
+ langs.to_h { |lang| [lang, symbols.count { |k| k::INPUT.key?(lang) }] }
267
+ .merge(total: symbols.size,
268
+ only_parser: symbols.count { |k| !k::INPUT.empty? && (k::INPUT.keys - %i[latex unicodemath]).empty? })
269
+ end
270
+
271
+ # Tables use "¦" as the cell separator, so cell text (which may hold "|",
272
+ # as in Vmatrix's defaults) needs no escaping. A cell that holds the
273
+ # separator itself is refused.
274
+ SEPARATOR = "\u00a6"
275
+
276
+ def table_start(cols)
277
+ "[cols=\"#{cols}\",options=\"header\",separator=#{SEPARATOR}]\n|===\n"
278
+ end
279
+
280
+ def row(cells)
281
+ cells = cells.map(&:to_s)
282
+ bad = cells.find { |c| c.include?(SEPARATOR) }
283
+ raise Error, "cell contains the table separator: #{bad}" if bad
284
+
285
+ "#{SEPARATOR} #{cells.join(" #{SEPARATOR} ")}\n"
286
+ end
287
+
288
+ def code(text)
289
+ text.to_s.empty? ? "-" : "`+#{text}+`"
290
+ end
291
+
292
+ def list(items)
293
+ items.empty? ? "-" : items.map { |i| code(i) }.join(", ")
294
+ end
295
+
296
+ # --- generated regions -----------------------------------------------
297
+
298
+ def counts_region
299
+ marks = MARKS
300
+ rows = FAMILY_ORDER.map do |fam|
301
+ in_fam = classes.select { |k| family(k) == fam }
302
+ [FAMILY_TITLES.fetch(fam), in_fam.size, *marks.map { |mk| in_fam.count { |k| mark(k) == mk } }]
303
+ end
304
+ totals = ["*Total*", classes.size, *marks.map { |mk| classes.count { |k| mark(k) == mk } }]
305
+ out = +table_start("3,1,1,1,1")
306
+ out << row(["Family", "Classes", *MARKS]) << "\n"
307
+ (rows + [totals]).each { |r| out << row(r) }
308
+ out << "|===\n"
309
+ end
310
+
311
+ def bases_region
312
+ owners = classes.select { |k| node?(k) }.filter_map { |k| field_owner(k) }.uniq
313
+ owners += classes.select { |k| node?(k) }.filter_map { |k| constructor_owner(k) }
314
+ owners = owners.uniq.sort_by(&:name)
315
+ out = +table_start("2,3,4,1")
316
+ out << row(["Class", "Own fields", "Constructor", "Classes served"]) << "\n"
317
+ owners.each do |o|
318
+ users = classes.count { |k| node?(k) && (field_owner(k) == o || constructor_owner(k) == o) }
319
+ ctor = constructor_owner(o) == o ? code(signature(o)) : "inherited"
320
+ out << row([code(short(o)), list(own_fields(o)), ctor, users])
321
+ end
322
+ out << "|===\n"
323
+ end
324
+
325
+ def constructor_text(klass)
326
+ ctor = constructor_owner(klass)
327
+ return "-" unless ctor
328
+ return code(signature(klass)) if ctor == klass
329
+
330
+ "from #{code(short(ctor))}"
331
+ end
332
+
333
+ def class_row(klass)
334
+ owner = field_owner(klass)
335
+ [code(short(klass)), FAMILY_TITLES.fetch(family(klass)),
336
+ code(klass.superclass.name.delete_prefix("Plurimath::Math::")),
337
+ owner ? code(short(owner)) : "-", list(own_fields(klass)),
338
+ constructor_text(klass), list(own_methods(klass)),
339
+ list(missing_writers(klass)), mark(klass)]
340
+ end
341
+
342
+ def classes_region
343
+ rows = classes.reject { |k| symbol_leaf?(k) }
344
+ out = +table_start("3,2,2,2,2,3,5,2,1")
345
+ out << row(["Class", "Family", "Superclass", "Field owner", "Own fields", "Constructor",
346
+ "Own public methods", "Writers not defined", "Mark"]) << "\n"
347
+ rows.each { |k| out << row(class_row(k)) }
348
+ out << "|===\n"
349
+ end
350
+
351
+ def symbol_leaf?(klass)
352
+ SYMBOL_FAMILIES.include?(family(klass)) && !BASE_SYMBOLS.include?(klass.name)
353
+ end
354
+
355
+ # Symbols are listed in groups of identical shape: same superclass, own
356
+ # fields, constructor owner, own public methods and mark.
357
+ def symbols_region
358
+ leaves = classes.select { |k| symbol_leaf?(k) }
359
+ groups = leaves.group_by do |k|
360
+ ctor = constructor_owner(k)
361
+ [k.superclass.name, own_fields(k), ctor && short(ctor), own_methods(k), mark(k)]
362
+ end
363
+ out = +""
364
+ groups.sort_by { |key, members| [-members.size, key.inspect] }.each_with_index do |(key, members), i|
365
+ sup, fields, ctor, methods, mk = key
366
+ out << "[%collapsible]\n.Shape #{i + 1}: #{members.size} classes < #{code(sup.delete_prefix('Plurimath::Math::'))}, #{mk}\n====\n"
367
+ out << "Own fields: #{list(fields)}. Constructor: #{ctor ? code(ctor) : '-'}.\n"
368
+ out << "Own public methods: #{list(methods)}.\n\n"
369
+ out << "Classes: #{members.map { |k| short(k).delete_prefix('Symbols::') }.join(', ')}.\n====\n\n"
370
+ end
371
+ out
372
+ end
373
+
374
+ def parser_only_region
375
+ out = +table_start("2,3,2")
376
+ out << row(["Method", "Defined on", "Called only from"]) << "\n"
377
+ rows = parser_only_methods
378
+ rows.each do |name, owners, layers|
379
+ out << row([code(name), list(owners), layers.map { |l| "`lib/plurimath/#{l}/`" }.join(", ")])
380
+ end
381
+ out << "|===\n"
382
+ c = input_table_counts
383
+ out << "\nSymbol `INPUT` tables (#{c[:total]} symbol classes):\n\n"
384
+ c.except(:total, :only_parser).each { |lang, n| out << "* `#{lang}`: #{n} classes\n" }
385
+ out << "* only `latex` and/or `unicodemath` keys: #{c[:only_parser]} classes\n"
386
+ out
387
+ end
388
+
389
+ REGIONS = {
390
+ "counts" => :counts_region,
391
+ "bases" => :bases_region,
392
+ "classes" => :classes_region,
393
+ "symbols" => :symbols_region,
394
+ "parser-only" => :parser_only_region,
395
+ }.freeze
396
+
397
+ def render(text)
398
+ REGIONS.reduce(text) do |doc, (name, meth)|
399
+ pattern = %r{(^// BEGIN GENERATED #{name}\n).*?(^// END GENERATED #{name}\n)}m
400
+ raise Error, "region #{name} not found in #{relative(DOC)}" unless doc.match?(pattern)
401
+
402
+ body = public_send(meth)
403
+ doc.sub(pattern) { "#{Regexp.last_match(1)}#{body}#{Regexp.last_match(2)}" }
404
+ end
405
+ end
406
+
407
+ def run(argv)
408
+ check = argv.delete("--check")
409
+ raise Error, "unknown arguments: #{argv.join(' ')}" unless argv.empty?
410
+
411
+ load_tree
412
+ current = File.read(DOC)
413
+ updated = render(current)
414
+ if check
415
+ return 0 if current == updated
416
+
417
+ warn "#{relative(DOC)} is out of date; run scripts/docs/core_inventory.rb"
418
+ return 1
419
+ end
420
+ File.write(DOC, updated) unless current == updated
421
+ 0
422
+ end
423
+ end
424
+
425
+ if $PROGRAM_NAME == __FILE__
426
+ begin
427
+ exit CoreInventory.run(ARGV.dup)
428
+ rescue CoreInventory::Error => e
429
+ warn e.message
430
+ exit 2
431
+ end
432
+ end