Module: Latexmath::RawParser
- Defined in:
- lib/latexmath/parser/raw_parser.rb
Overview
The internal raw-tree boundary: the legacy preprocessing, then the grammar, then the declared syntax bindings, which carry the legacy tree.
Internal and for tests and parity replay only; not public API. The public Latexmath.parse, which returns the typed LaTeX model, is built on top of it.
Defined Under Namespace
Classes: Result
Class Method Summary collapse
- .entry_for(decimal) ⇒ Object
-
.grammar_input(text) ⇒ Object
The preprocessed text as UTF-8.
-
.normalize_input(input) ⇒ Object
inputas valid UTF-8, or an ArgumentError. - .parse(input, decimal: ".", engine: :auto) ⇒ Object
-
.restore_capture_names(tree) ⇒ Object
Renames the captures the grammar had to spell differently (Configuration::CAPTURE_RENAMES) back to their legacy keys, keeping key order.
Class Method Details
.entry_for(decimal) ⇒ Object
42 43 44 45 46 47 48 |
# File 'lib/latexmath/parser/raw_parser.rb', line 42 def entry_for(decimal) Configuration::DECIMAL_ENTRIES.fetch(decimal) do raise ArgumentError, "unsupported decimal marker #{decimal.inspect} " \ "(supported: #{Configuration::DECIMAL_ENTRIES.keys.map(&:inspect).join(', ')})" end end |
.grammar_input(text) ⇒ Object
The preprocessed text as UTF-8. Preprocessing returns US-ASCII text when nothing outside \text and \mbox groups is non-ASCII (as the legacy preprocessing does); the native engine gives UTF-8 slices either way, and the Ruby engine's slices take the input's encoding.
54 55 56 |
# File 'lib/latexmath/parser/raw_parser.rb', line 54 def grammar_input(text) text.encoding == Encoding::UTF_8 ? text : text.dup.force_encoding(Encoding::UTF_8) end |
.normalize_input(input) ⇒ Object
input as valid UTF-8, or an ArgumentError.
59 60 61 62 63 64 65 66 67 68 |
# File 'lib/latexmath/parser/raw_parser.rb', line 59 def normalize_input(input) raise ArgumentError, "input must be a String, got #{input.class}" unless input.is_a?(String) text = input.encoding == Encoding::UTF_8 ? input : input.encode(Encoding::UTF_8) raise ArgumentError, "input is not valid UTF-8" unless text.valid_encoding? text rescue EncodingError => e raise ArgumentError, "input cannot be converted to UTF-8: #{e.}" end |
.parse(input, decimal: ".", engine: :auto) ⇒ Object
26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 |
# File 'lib/latexmath/parser/raw_parser.rb', line 26 def parse(input, decimal: ".", engine: :auto) entry = entry_for(decimal) mode = Engine.resolve(engine) normalized = normalize_input(input) text = Preprocessor.call(normalized) loaded = Latexmath.artifacts shape = Engine.run(loaded.parse, entry, grammar_input(text), mode, fallback: engine == :auto, source: normalized) bound = loaded.bindings.apply(entry, shape) raw, model = [Configuration::TREE_CAPTURE, Configuration::MODEL_CAPTURE].map do |name| bound.fetch(name) { raise ArtifactError, "the declared bindings did not produce #{name}" } end Result.new(input: normalized, text: text, raw: raw, model: model, engine: Engine.last_engine) end |
.restore_capture_names(tree) ⇒ Object
Renames the captures the grammar had to spell differently (Configuration::CAPTURE_RENAMES) back to their legacy keys, keeping key order.
73 74 75 76 77 78 79 80 81 82 |
# File 'lib/latexmath/parser/raw_parser.rb', line 73 def restore_capture_names(tree) case tree when Hash tree.to_h do |key, value| [Configuration::CAPTURE_RENAMES.fetch(key, key), restore_capture_names(value)] end when Array then tree.map { |value| restore_capture_names(value) } else tree end end |