% 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/>.

\def\optimization{5}

\input yycommon.sty % general routines for stack and array access
\input yymisc.sty   % helper macros (stack manipulation, table processing, value stack pointers)
\input yyinput.sty  % input functions
\input yyparse.sty  % parser machinery
\input flex.sty     % lexer functions
\input yyfaststack.sty % sped up stack access functions
\input yyboth.sty   % parser initialization, optimization
\input yyunion.sty

\def\mainnamespace{[stategrab]} % the state parser namespace 
\let\parsernamespace\mainnamespace
\let\currentparsernamespace\parsernamespace % error reporting

\input cweb/byytab.tex % load parser tables
\settokens
\input cweb/lstab.tex % load lexer tables
\let\yylexreturn\yylexreturnregular

\newparserstate % initialize the new parser
\newlexerstate  % initialize the new lexer

\optimizeall % put optimization options into effect

% main section macro

\savecs{global}{\vvb}

\let\nx\noexpand

\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
  \vbsectionfalse
  \let\vvb=\prodvvb
  \gettovb{}${}#2\vb\end % $tage one, collect the tokens and intergroup material
  \ifvbsection
      \basicparserinit\expandafter\yyparse\the\table\yyeof\yyeof\endparseinput\endparse
      \ifyyparsefail % revert to generic macros if parsing failed
          \restorecs{global}{\vvb}%
          {}${}#2%$
      \else % Stage three, process the parsed table
          \begingroup
              \restorecslist{bootstrap}\yyunion
              \the\table\relax
          \endgroup
      \fi
  \else
      \the\bsectiontoks
  \fi
  \par
}

\let\B\Bprod
\newwrite\stlist
