Module: Latexmath::Engine

Defined in:
lib/latexmath/parser/engine.rb

Overview

Runs the grammar on the native or Ruby Parsanol engine with the legacy grammar's settings (Parsanol's default, strict memo policy), maps failures to Latexmath errors and records the engine that produced the result per thread.

Internal.

Constant Summary collapse

LAST_ENGINE_KEY =
:latexmath_last_engine
OPENING =
"([{".chars.freeze
CLOSING =
")]}".chars.freeze

Class Method Summary collapse

Class Method Details

.last_engine ⇒ Object



17
18
19
# File 'lib/latexmath/parser/engine.rb', line 17

def last_engine
  Thread.current[LAST_ENGINE_KEY]
end

.native_available? ⇒ Boolean

Returns:

  • (Boolean)


21
22
23
24
25
# File 'lib/latexmath/parser/engine.rb', line 21

def native_available?
  Parsanol::Native.available?
rescue LoadError, StandardError
  false
end

.resolve(engine) ⇒ Object

The engine (:native or :ruby) that engine (one of ENGINES) runs.



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/latexmath/parser/engine.rb', line 28

def resolve(engine)
  case engine
  when :auto then native_available? ? :native : :ruby
  when :ruby then :ruby
  when :native
    return :native if native_available?

    raise EngineUnavailable,
          "engine: :native requested but the Parsanol native extension " \
          "is not available on this platform (#{RUBY_PLATFORM}, Ruby #{RUBY_VERSION})"
  else
    raise ArgumentError,
          "unknown engine #{engine.inspect} (use #{Configuration::ENGINES.map(&:inspect).join(', ')})"
  end
end

.run(artifact, entry, text, mode, fallback:, source:) ⇒ Object

Parses text from entry of the parse artifact artifact on mode and returns the parse shape. Under fallback (engine: :auto) input too deep for the native engine is rerun on Ruby. source is the normalized caller input text was preprocessed from; a ParseError reports its position in source (and in text).



49
50
51
52
53
54
55
# File 'lib/latexmath/parser/engine.rb', line 49

def run(artifact, entry, text, mode, fallback:, source:)
  shape, ran = run_with_fallback(artifact, entry, text, mode, fallback: fallback)
  record(ran)
  shape
rescue ParseError => e
  raise in_source(e, source)
end