-- lx-bundle:true

-- This file is part of Lx, created by Liqube <www.liqube.com>
-- Copyright Liqube Audio. All rights reserved.
-- Lx Interim License. See LICENSE for details.

--- TeX spacing classes for visible mathematical symbols
-- @module plugins.mdpreview.math.classes

--[[
Anything absent should normally default to:

local class = CLASSES[glyph] or "ordinary"

Important TeX rule: binary demotion

A binary operator does not always remain binary. TeX reclassifies it as ordinary in contexts like:

-x
a + -b
(x +)

A simple rule:

local CANNOT_PRECEDE_BINARY = {
	binary = true,
	relation = true,
	open = true,
	punctuation = true,
	operator = true,
}

local CANNOT_FOLLOW_BINARY = {
	relation = true,
	close = true,
	punctuation = true,
}

local function normalize_binary_classes(atoms)
	for i, atom in ipairs(atoms) do
		if atom.class == "binary" then
			local previous = atoms[i - 1]
			local following = atoms[i + 1]

			if not previous
				or CANNOT_PRECEDE_BINARY[previous.class]
				or not following
				or CANNOT_FOLLOW_BINARY[following.class]
			then
				atom.class = "ordinary"
			end
		end
	end
end
]]

local CLASSES = {
	-- Binary operators
	["+"] = "binary",
	["-"] = "binary",
	["±"] = "binary",
	["∓"] = "binary",
	["×"] = "binary",
	["÷"] = "binary",
	["·"] = "binary",
	["∗"] = "binary",
	["⋆"] = "binary",
	["∘"] = "binary",
	["∙"] = "binary",
	["⋄"] = "binary",
	["∔"] = "binary",
	["○"] = "binary",
	["≀"] = "binary",
	["⨿"] = "binary",
	["⊛"] = "binary",
	["⊚"] = "binary",
	["⊝"] = "binary",
	["⋇"] = "binary",
	["⋉"] = "binary",
	["⋊"] = "binary",
	["⋋"] = "binary",
	["⋌"] = "binary",
	["⊻"] = "binary",
	["⊺"] = "binary",
	["∖"] = "binary",

	["∩"] = "binary",
	["∪"] = "binary",
	["⊎"] = "binary",
	["⊓"] = "binary",
	["⊔"] = "binary",

	["∧"] = "binary",
	["∨"] = "binary",
	["⊕"] = "binary",
	["⊖"] = "binary",
	["⊗"] = "binary",
	["⊘"] = "binary",
	["⊙"] = "binary",

	["⊞"] = "binary",
	["⊟"] = "binary",
	["⊠"] = "binary",
	["⊡"] = "binary",
	["⊼"] = "binary",
	["⋎"] = "binary",
	["⋏"] = "binary",
	["⩞"] = "binary",
	["⋒"] = "binary",
	["⋓"] = "binary",

	["▷"] = "binary",
	["◁"] = "binary",
	["⊲"] = "binary",
	["⊳"] = "binary",
	["⊴"] = "binary",
	["⊵"] = "binary",

	-- Relations
	["="] = "relation",
	["≠"] = "relation",
	["≡"] = "relation",
	["∼"] = "relation",
	["≃"] = "relation",
	["≈"] = "relation",
	["≅"] = "relation",
	["≍"] = "relation",
	["∝"] = "relation",
	[":−"] = "relation",
	["∷−"] = "relation",
	[":≈"] = "relation",
	["∷≈"] = "relation",
	[":∼"] = "relation",
	["∷∼"] = "relation",
	["∼:"] = "relation",
	["∼∷"] = "relation",
	["∷"] = "relation",
	["∷="] = "relation",
	["≔"] = "relation",
	["≕"] = "relation",
	["∹"] = "relation",
	["−∷"] = "relation",
	["=∷"] = "relation",
	["≈:"] = "relation",
	["≈∷"] = "relation",
	["≂"] = "relation",
	["≟"] = "relation",
	["⋖"] = "relation",
	["⋗"] = "relation",

	["<"] = "relation",
	[">"] = "relation",
	["≤"] = "relation",
	["≥"] = "relation",
	["≪"] = "relation",
	["≫"] = "relation",
	["≺"] = "relation",
	["≻"] = "relation",
	["≼"] = "relation",
	["≽"] = "relation",
	["≰"] = "relation",
	["≱"] = "relation",
	["≨"] = "relation",
	["≩"] = "relation",
	["⋠"] = "relation",
	["⋡"] = "relation",
	["⋑"] = "relation",
	["⋐"] = "relation",
	["⋙"] = "relation",
	["⊶"] = "relation",
	["⊷"] = "relation",
	["≑"] = "relation",
	["⪅"] = "relation",
	["⌣"] = "relation",
	["≖"] = "relation",
	["⋚"] = "relation",
	["⪋"] = "relation",
	["≶"] = "relation",
	["≲"] = "relation",
	["⋘"] = "relation",
	["≊"] = "relation",
	["⪖"] = "relation",
	["⪕"] = "relation",
	["⫅"] = "relation",
	["∍"] = "relation",
	["∽"] = "relation",
	["≒"] = "relation",
	["⪸"] = "relation",
	["⋍"] = "relation",
	["⌢"] = "relation",
	["≬"] = "relation",
	["⪰"] = "relation",
	["≿"] = "relation",
	["≏"] = "relation",
	["≧"] = "relation",
	["≎"] = "relation",
	["⩾"] = "relation",
	["⟂"] = "relation",
	["≗"] = "relation",
	["⋔"] = "relation",
	["⫆"] = "relation",
	["⪷"] = "relation",
	["⪆"] = "relation",
	["⪯"] = "relation",
	["⋛"] = "relation",
	["≾"] = "relation",
	["≜"] = "relation",
	["⪌"] = "relation",
	["≷"] = "relation",
	["≓"] = "relation",
	["≳"] = "relation",
	["△"] = "relation",
	["⋞"] = "relation",
	["⋟"] = "relation",
	["≐"] = "relation",
	["≦"] = "relation",
	["⩽"] = "relation",
	["⪊"] = "relation",
	["⪵"] = "relation",
	["⪈"] = "relation",
	["≯"] = "relation",
	["⋨"] = "relation",
	["⊁"] = "relation",
	["⊊"] = "relation",
	["⋧"] = "relation",
	["⫋"] = "relation",
	["⪺"] = "relation",
	["⪉"] = "relation",
	["≮"] = "relation",
	["⪶"] = "relation",
	["⪇"] = "relation",
	["⋪"] = "relation",
	["⋩"] = "relation",
	["⋬"] = "relation",
	["⊋"] = "relation",
	["⋦"] = "relation",
	["⋫"] = "relation",
	["⫌"] = "relation",
	["⋭"] = "relation",
	["≇"] = "relation",
	["⊀"] = "relation",
	["⊬"] = "relation",
	["⊭"] = "relation",
	["⊯"] = "relation",
	["⊮"] = "relation",
	["≁"] = "relation",
	["⪹"] = "relation",

	["∈"] = "relation",
	["∊"] = "relation",
	["∉"] = "relation",
	["∋"] = "relation",
	["∌"] = "relation",

	["⊂"] = "relation",
	["⊃"] = "relation",
	["⊆"] = "relation",
	["⊇"] = "relation",
	["⊄"] = "relation",
	["⊅"] = "relation",
	["⊈"] = "relation",
	["⊉"] = "relation",
	["⊏"] = "relation",
	["⊐"] = "relation",
	["⊑"] = "relation",
	["⊒"] = "relation",

	["∣"] = "relation",
	["∤"] = "relation",
	["∥"] = "relation",
	["∦"] = "relation",
	["⊥"] = "relation",
	["⋈"] = "relation",

	["⊢"] = "relation",
	["⊣"] = "relation",
	["⊨"] = "relation",
	["⊩"] = "relation",
	["⊪"] = "relation",
	["⊫"] = "relation",

	["→"] = "relation",
	["←"] = "relation",
	["↔"] = "relation",
	["⇒"] = "relation",
	["⇐"] = "relation",
	["⇔"] = "relation",
	["↦"] = "relation",
	["↤"] = "relation",
	["⟻"] = "relation",
	["⊸"] = "relation",
	["↺"] = "relation",
	["↻"] = "relation",
	["↶"] = "relation",
	["↷"] = "relation",
	["↫"] = "relation",
	["↬"] = "relation",
	["↾"] = "relation",
	["⇠"] = "relation",
	["⇢"] = "relation",
	["↝"] = "relation",
	["↼"] = "relation",
	["⇋"] = "relation",
	["⇁"] = "relation",
	["⇀"] = "relation",
	["⇌"] = "relation",
	["⇃"] = "relation",
	["⇂"] = "relation",
	["↿"] = "relation",
	["↽"] = "relation",
	["⇇"] = "relation",
	["⇆"] = "relation",
	["↭"] = "relation",
	["⇚"] = "relation",
	["⇄"] = "relation",
	["⇉"] = "relation",
	["⇊"] = "relation",
	["⇝"] = "relation",
	["⇛"] = "relation",
	["↱"] = "relation",
	["↰"] = "relation",
	["⇈"] = "relation",
	["⟵"] = "relation",
	["⟸"] = "relation",
	["⟷"] = "relation",
	["⟺"] = "relation",
	["⟼"] = "relation",
	["⟶"] = "relation",
	["⟹"] = "relation",
	["↣"] = "relation",
	["↞"] = "relation",
	["↩"] = "relation",
	["↠"] = "relation",
	["↪"] = "relation",
	["↢"] = "relation",
	["↚"] = "relation",
	["⇍"] = "relation",
	["↮"] = "relation",
	["⇎"] = "relation",
	["↛"] = "relation",
	["⇏"] = "relation",

	["↑"] = "relation",
	["↓"] = "relation",
	["↕"] = "relation",
	["⇑"] = "relation",
	["⇓"] = "relation",
	["⇕"] = "relation",

	["↗"] = "relation",
	["↘"] = "relation",
	["↙"] = "relation",
	["↖"] = "relation",

	-- Opening delimiters
	["("] = "open",
	["["] = "open",
	["{"] = "open",
	["⟨"] = "open",
	["⌈"] = "open",
	["⌊"] = "open",
	["⟦"] = "open",

	-- Closing delimiters
	[")"] = "close",
	["]"] = "close",
	["}"] = "close",
	["⟩"] = "close",
	["⌉"] = "close",
	["⌋"] = "close",
	["⟧"] = "close",

	-- Fence-like delimiters need contextual open/close handling
	["|"] = "fence",
	["‖"] = "fence",

	-- Punctuation
	[","] = "punctuation",
	[";"] = "punctuation",
	[":"] = "punctuation",
	["."] = "punctuation",

	-- Large operators
	["∑"] = "operator",
	["∏"] = "operator",
	["∐"] = "operator",
	["∫"] = "operator",
	["∬"] = "operator",
	["∭"] = "operator",
	["∮"] = "operator",
	["⋂"] = "operator",
	["⋃"] = "operator",
	["⋀"] = "operator",
	["⋁"] = "operator",
	["⨁"] = "operator",
	["⨂"] = "operator",
}

return CLASSES
