% Copyright 2012-2020, 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/>.

% nested and boundary parsing macros

\newcount\boundarylower
\newcount\boundaryupper

% for parsers that use short setup

\boundaryupper-\@M
\boundarylower-\@M

\expandafter\def\expandafter\bisonparserdatainit\expandafter{%
    \bisonparserdatainit
    \boundaryupper-\@M
    \boundarylower-\@M
}

\def\inputboundarynext#1{%
    \yybytepure{ }\yycp@\boundarychar
    #1\getstackpointer\yyvsa\relax
    \yybyte\expandafter{\the\yybyte{#1}}%
    \yyreturn % inserting something here will effectively insert it into the input stream
}

\newcount\vspointer

% to use while debugging, the sequence below displays the contents of the value stack

\def\yyactione{%
    \message{value stack at: \getstackpointer\yyvsa}%
    \toksa\expandafter\expandafter\expandafter\expandafter\expandafter\expandafter\expandafter{\gettopofstackcs\yyvsa}%
    \toksb\expandafter\expandafter\expandafter\expandafter\expandafter\expandafter\expandafter{\getmidstackcs\yyvsa{0}}%
    \message{on the stack: \the\toksa, bottom: \the\toksb}%
    \message{the value: \the\yyval}%
    \message{rule: \the\yyn, length: \the\yylen}%
}

% this is an example of boundary processing

\def\yyaction{%
    \vspointer=\getstackpointer\yyvsa\relax
    \advance\vspointer-\yylen
    \ifnum\vspointer<\boundarylower 
        \ifnum\boundaryupper>-\@M % all nested tokens will be reduced
            \boundaryupper=-\@M
            \boundarylower=-\@M
            \message{reduction...(\the\vspointer: \the\yy(1).\the\yy(2).)}%
            \printrule\yyn
        \else % pre boundary tokens are getting reduced
            \boundarylower=\vspointer
            \advance\boundarylower\@ne
        \fi
    \else
        \ifnum\vspointer<\boundaryupper % top nested tokens will be reduced
            \boundaryupper=\vspointer
        \fi
    \fi
    \yysymswitch{\yyn}% setting symbol names
    \yybigswitch{\yyn}%
    \yysymcleanup{\yyn}% removing symbol names from the namespace
}
