umath 0.1.0 → 0.2.0

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.
data/lib/umath/engine.rb CHANGED
@@ -105,6 +105,9 @@ module Umath
105
105
  else
106
106
  root.parse(text, compatibility: :parslet)
107
107
  end
108
+ rescue Parsanol::RecursionLimitExceeded => e
109
+ # parsanol 1.3.134+ raises this for native deep nesting.
110
+ raise NativeRecursionLimit, e.message.lines.first.to_s.strip, cause: nil
108
111
  rescue Parsanol::ParseFailed => e
109
112
  record(mode)
110
113
  raise parse_error(e, root, text, mode)
@@ -118,13 +121,16 @@ module Umath
118
121
  raise ArtifactError, "the grammar artifact cannot be used: #{e.class}: #{e.message}"
119
122
  end
120
123
 
121
- # The legacy grammar runs on the native engine wherever it is available
122
- # (Parsanol::Parser picks it for Parslet-compatible parses), so the
123
- # position it reports is the native engine's deepest failure. The Ruby
124
- # engine reports the same position through a Deepest reporter.
124
+ # Both engines report the refusal position from the Ruby engine's
125
+ # Deepest reporter pass. The native engine's own position can stop
126
+ # earlier, at the start of a multi-character terminal or of an
127
+ # unclosed string or fence, so a native refusal reruns that pass and
128
+ # the position does not depend on the engine. Only refusals pay for
129
+ # the pass. When it gives no position (the Ruby stack is exhausted),
130
+ # the native position stands.
125
131
  def parse_error(error, root, text, mode)
126
- cause = mode == :ruby ? ruby_cause(root, text) : error.parse_failure_cause
127
- byte = deepest_position(cause)
132
+ byte = deepest_position(ruby_cause(root, text))
133
+ byte = deepest_position(error.parse_failure_cause) if byte.nil? && mode == :native
128
134
  char = byte && text.byteslice(0, byte).force_encoding(Encoding::UTF_8).scrub.length
129
135
  where = byte ? " at character #{char} (byte #{byte})" : ""
130
136
  ParseError.new("UnicodeMath input rejected#{where}: #{error.message.lines.first.to_s.strip[0, 200]}",
@@ -141,12 +147,17 @@ module Umath
141
147
  end
142
148
 
143
149
  # Iterative: the cause tree of deeply nested input is as deep as the
144
- # input.
150
+ # input. Causes are shared between parents, so each one is visited once;
151
+ # walking the shared graph as a tree took minutes on some refusals.
145
152
  def deepest_position(cause)
146
153
  deepest = nil
154
+ seen = {}.compare_by_identity
147
155
  stack = [cause].compact
148
156
  until stack.empty?
149
157
  node = stack.pop
158
+ next if seen.key?(node)
159
+
160
+ seen[node] = true
150
161
  deepest = node.position if node.position && (deepest.nil? || node.position > deepest)
151
162
  stack.concat(node.children.compact)
152
163
  end
@@ -0,0 +1,33 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "digest"
4
+ require "json"
5
+
6
+ module Umath
7
+ # Verified, data-only declarations shared with foreign language generators.
8
+ module Schema
9
+ class ChecksumMismatch < StandardError; end
10
+
11
+ BUNDLE_PATH = File.expand_path("../../schema/umath.bundle.json", __dir__).freeze
12
+ BUNDLE_SHA256 = "0d14e7e8c5971c9bb304f14362784bbb49baa1a208195c4151090524cd425d8b".freeze
13
+
14
+ def self.bundle_json
15
+ bytes = File.binread(BUNDLE_PATH)
16
+ actual = Digest::SHA256.hexdigest(bytes)
17
+ unless actual == BUNDLE_SHA256
18
+ raise ChecksumMismatch, "model declaration checksum mismatch: #{actual}"
19
+ end
20
+
21
+ bytes.force_encoding(Encoding::UTF_8)
22
+ end
23
+
24
+ def self.bundle_path
25
+ bundle_json
26
+ BUNDLE_PATH
27
+ end
28
+
29
+ def self.bundle
30
+ JSON.parse(bundle_json)
31
+ end
32
+ end
33
+ end
data/lib/umath/version.rb CHANGED
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Umath
4
- VERSION = "0.1.0"
4
+ VERSION = "0.2.0"
5
5
  end
@@ -0,0 +1,14 @@
1
+ {
2
+ "projection": "implicit-json-attribute-mappings-v1",
3
+ "definition_repository": "https://github.com/plurimath/umath",
4
+ "definition_files_sha256": {
5
+ "lib/umath/model.rb": "442e8c34ac021acbc224198f912a59a403256ba69a40bc1ab853148d379c8e5e",
6
+ "lib/umath/model/nodes.rb": "3e7e26bd6ba5595141ad6984ed5ce8e28ac08905c8bc50d26a1c2eac508ac897"
7
+ },
8
+ "producer_script_sha256": "6628ca07f622acb08dfdfa515d45e29c83e18f917b3838989f6e0091d344bf76",
9
+ "exporter_revision": "fd2989fd8478b1ecb2508d98fa2d66792c09c72b",
10
+ "model_count": 75,
11
+ "files": {
12
+ "umath.bundle.json": "0d14e7e8c5971c9bb304f14362784bbb49baa1a208195c4151090524cd425d8b"
13
+ }
14
+ }