% Copyright 2012-2014, Alexander Shibakov
% This file is part of SPLinT
%
% SPLinT is free software: you can redistribute it and/or modify
% it under the terms of the GNU General Public License as published by
% the Free Software Foundation, either version 3 of the License, or
% (at your option) any later version.
%
% SPLinT is distributed in the hope that it will be useful,
% but WITHOUT ANY WARRANTY; without even the implied warranty of
% MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
% GNU General Public License for more details.
%
% You should have received a copy of the GNU General Public License
% along with SPLinT.  If not, see <http://www.gnu.org/licenses/>.

% token equivalence table does not exist, switch to a bootstrap parser

\immediate\openout\tokendefs=\tokendeffile
\expandafter\input \bstrapparser
\bootstraplexersetup

\input cweb/ltab.tex % load lexer tables

% at this point the macros inside the table files (\newtable, \constset, \yybigswitch, \stashswitch, \addname, 
% \yydoactionswitch, \setflexstates, \stateset, \tokeneq) have set up the corresponding stuctures in the `parser namespace'
% (e.g. \newtable{yyaccept} created a token register \yyaccept[main]), assigned the `generic' names to them
% (to continue the example above, \newtable does \let\yyaccept\yyaccept[main]) and recorded the corresponding commands
% in \pinittoks for future use.

% lexer state macros are namespace specific (just like token names) so they have to be set in each namespace.
\setflexstates

% finally, we add the definitions for the variables used in running the lexer and the parser.
\newparserstate
\newlexerstate
\newlexerstateextra

% we record all the commands necessary to switch to the desired namespace in a convenient macro
\edef\tomainparser{%
    \noexpand\savefullstate % unnecessary
    \noexpand\savefullstateextra % unnecessary
    \let\noexpand\parsernamespace\noexpand\mainnamespace % switch to the new namespace
    \the\pinittoks % restore all the tables, tokens and constants, and stacks
    \let\noexpand\getcurrentparser\noexpand\tomainparser
}%

\optimizeall

\def\parserinit{%
    \basicparserinit
    \yyinitstack\obstackforstring
    \yyinitstack\obstackforstringraw
    \percentpercentcount=\z@
    \lonesting=\z@
    \laststring{}\laststringraw{}%
    \currentlaststring{}\currentlaststringraw{}%
    \parserdatainit
}

\def\parserdatainit{%
    % make control sequences inserted by the parser non expandable to facilitate
    % token list manipulation; currently nothing is done
}

\tomainparser % this saves the current state

\input yytexlex.sty

\savecs{global}{\vvb\6}

\long\def\B@@#1\E#2\par{%
%  \rightskip=0pt plus 100pt minus 10pt % go into C mode
%  \sfcode`;=3000
%  \pretolerance 10000
%  \hyphenpenalty 1000 % so strings can be broken (discretionary \ is inserted)
%  \exhyphenpenalty 10000
%  \global\ind=2 \1\ \unskip 
%% the old \B up to here
  \oldB#1\E{}$% finish the $-mode and the corresponding group
  {\ninepoint
  \vbsectionfalse
  \let\vvb=\prodvvb
  \gettovb{}${}#2\vb\end % $tage one, collect the tokens and intergroup material
  \ifvbsection
      \parsevb % Stage two, start the parsing
      \ifyyparsefail % do nothing if parsing failed
      \else % Stage three, process the parsed table
          \initbootstrap
          \the\table
      \fi
  \else
      \the\bsectiontoks
  \fi
  \par}%
}

\let\B\Bprod

\long\def\processintervb#1{}% minor speedup

% stage two macros: parsing

\def\parsevb{%
    \ifchecktable \errmessage{table before parsing: \the\table}\fi
    \tomainparser % this is not currently required since there are no namespace switching macros
                  % in the \TeX\ portion of the text; it is left here in case such macros are used in the future
    \doparse{\the\table}%
}

\newif\ifchecktable
\newif\ifchecktrim
\newif\ifsaveparseoutput

\newwrite\exampletable

\long\def\beginprod#1\endprod{%
  \endgroup
}

\let\endprod\endgroup
\let\begincprod\beginprod
\let\endcprod\endgroup
