% File load-unicode-math-classes.tex
%
% Copyright 2015-2017 The LaTeX3 Project
%
% It may be distributed and/or modified under the conditions of
% the LaTeX Project Public License (LPPL), either version 1.3c of
% this license or (at your option) any later version. The latest
% version of this license is in the file
% http://www.latex-project.org/lppl.txt.
%
% Issues with this file should be reported at
% https://github.com/latex3/unicode-data
%
% This file parses MathClass.txt, provided by the Unicode Consortium, and sets
% up the following mapping between Unicode classes and TeX math types
% - "L" (large)       \mathop
% - "B" (binary)      \mathbin
% - "V" (vary)        \mathbin
% - "R" (relation)    \mathrel
% - "O" (opening)     \mathopen
% - "C" (closing)     \mathclose
% - "P" (punctuation) \mathpunct
% - "A" (alphabetic)  \mathalpha
% 
% For each code point processed, the result is of the form
%
%     \Umathcode <codepoint> = <type> 1 <codepoint>
%
% =============================================================================
%
% The data can only be loaded by Unicode engines. Currently this is limited to
% XeTeX and LuaTeX, both of which define \Umathcode.
\ifx\Umathcode\undefined
  \expandafter\endinput
\fi
% Just in case, check for the e-TeX extensions.
\ifx\eTeXversion\undefined
  \expandafter\endinput
\fi
% This file can be loaded in IniTeX mode so the category codes of |{|, |}| and
% |#| may not be correct. Everything is done in a group so that only the
% settings we want to propagate are made available generally.
\begingroup
  \catcode`\{=1 %
  \catcode`\}=2 %
  \catcode`\#=6 %
% Write some basic information to the log.
  \catcode`\^=7 %
  \newlinechar=`\^^J %
  \message{^^J}%
  \message{load-unicode-math-classes.tex v1.6 (2017-06-27)^^J}%
  \message{Reading math class data^^J}%
% The parser for data lines starts by skipping any comments (which start with
% a |#| and which will be category code~$12$).
  \edef\hash{\string#}%
  \def\firsttoken#1#2\relax{#1}%
  \def\parseunicodedataI#1\relax{%
    \unless\if\hash\firsttoken#1?\relax
      \parseunicodedataII#1\relax
    \fi
  }%
% The first entry in each data line is a code point or range of code points:
% set up to find a range if present.
  \def\parseunicodedataII#1;#2\relax{%
    \parseunicodedataIII#1....\relax{#2}%
  }%
  \def\parseunicodedataIII#1..#2..#3\relax#4{%
    \ifx\relax#2\relax
      \parseunicodedataIV{#1}{#1}#4\relax
    \else
      \parseunicodedataIV{#1}{#2}#4\relax
    \fi
  }%
% Examine the Unicode class and if known set up the math code appropriately.
  \chardef\mathclassL=1 %
  \chardef\mathclassB=2 %
  \chardef\mathclassV=2 %
  \chardef\mathclassR=3 %
  \chardef\mathclassO=4 %
  \chardef\mathclassC=5 %
  \chardef\mathclassP=6 %
  \chardef\mathclassA=7 %
  \def\parseunicodedataIV#1#2#3#4\relax{%
    \begingroup
      \count0="#1 %
      \loop
        \unless\ifnum\count0>"#2 %
          \ifcsname mathclass#3\endcsname
            \global\Umathcode\count0=\csname mathclass#3\endcsname 1 \count0 %
          \fi
        \advance\count0 by 1 %
      \repeat
    \endgroup
  }%
% From plain: may not be defined (yet).
  \def\loop#1\repeat{\def\body{#1}\iterate}%
  \def\iterate{%
    \body
      \let\next\iterate
    \else
      \let\next\relax
    \fi
    \next
  }%
  \let\repeat\fi
% Now we can load the data.
  \catcode`\#=12 %
  \def\storedpar{\par}%
  \openin0=MathClass-15.txt %
  \read0 to \unicodedataline
  \message{\unicodedataline ^^J}%
  \read0 to \unicodedataline
  \message{\unicodedataline ^^J}%
  \loop\unless\ifeof0 %
    \read0 to \unicodedataline
    \unless\ifx\unicodedataline\storedpar
      \expandafter\parseunicodedataI\unicodedataline\relax
    \fi
  \repeat
  \closein0 %
\endgroup
