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

% handy debugging shortcuts

\def\yydebugsetup{% initialize debugging (note that arbitrary commands 
                  % can be also inserted by \insertraw{...}
    \expandafter\def\expandafter\multicharswitch % allow execution of any code in the middle of input
        \expandafter{\multicharswitch\yydebugbegin\yydebugend{\the\yybyte}}%
}

\def\yydebuggenericstart{% generic command executed in the middle of input
    \yydebugmost % turn on tracing
    \yyinput % ... and continue
}

\def\yydebuggenericfinish{% stop debugging
    \yydebugnone % turn off tracing
    \yyinput % ... and continue
}

\let\yydebugbegin\yydebuggeneric
\let\yydebugend\yydebuggenericfinish

\newif\ifcdebug % a temporary conditional

\def\yydebugall{% turn all debugging options on
    \yydebugmost
    \checktabletrue
    \saveparseoutputtrue
    \indexverbosetrue
    \displaytokenrawtrue % so that the `nonstringified' version of the token is displayed as well

}

\def\yydebugparse{%
    \tracelookaheadtrue
    \traceparseresultstrue
    \traceparserstatestrue
    \tracerulestrue
    \tracestackstrue
    \tracediscardedinputtrue
}

\def\reporttokendisplay{% to make this skippable in bootstrap mode
    \ifdisplaytokenraw\ferrmessage{tokens are typeset as is^^J}\fi
}

\def\yydebugmost{% do not save or display the tables
    \ifbootstrapmode
        \ferrmessage{*** bootstrap mode is on ***^^J^^J}%
    \else
        \reporttokendisplay
        \checktrimtrue
        \parseverbosetrue
        \checktrailingstashtrue
    \fi
    \ifbracketedvisible\ferrmessage{Symbolic term names are visible^^J}\fi
    \iftermindex\else\ferrmessage{Indexing is turned off^^J}\fi
    \ifshowlastaction\else\ferrmessage{Last action will not be shown}\fi
    \ifxreflocal\else\ferrmessage{Cross references are generated in noweb style}\fi
    \ifx\optimization\UNDEFINED
    \else
        \ferrmessage{Optimization level set at \number\optimization^^J}%
    \fi

    \tracingonline=3
    \showboxbreadth=10000
    \showboxdepth=10000
%
    \yydebugparse
    \traceactioncodetrue
    \traceactionstrue
    \tracebadcharstrue
    \tracebadnamestrue
    \tracedfatrue
    \traceflexbufferstrue
    \tracenamestrue
    \tracestatestrue
    \traceswitchlabelstrue
    \tracetexpptrue
    \tracetokennamestrue
    \yyflexdebugtrue
    \yyinputdebugtrue
    \yytracereplacementstrue
    \traceprettytokenstrue
    \tracingliststrue
}

\def\yydebugnone{% turn all debugging options off
    \checktablefalse
    \ifbootstrapmode
    \else
        \checktrimfalse
        \parseverbosefalse
    \fi
    \saveparseoutputfalse
    \traceactioncodefalse
    \traceactionsfalse
    \tracebadcharsfalse
    \tracebadnamesfalse
    \tracedfafalse
    \tracediscardedinputfalse
    \traceflexbuffersfalse
    \tracelookaheadfalse
    \tracenamesfalse
    \traceparseresultsfalse
    \traceparserstatesfalse
    \tracerulesfalse
    \tracestacksfalse
    \tracestatesfalse
    \traceswitchlabelsfalse
    \tracetexppfalse
    \tracetokennamesfalse
    \yyflexdebugfalse
    \yyinputdebugfalse
    \yytracereplacementsfalse
    \cdebugfalse
    \indexverbosefalse
    \displaytokenrawfalse
    \tracinglistsfalse
}

% miscellaneous shortcuts

\def\shownethe#1{% show the token register only if it is not empty
    {%
        \edef\next{\the#1}%
        \ifx\next\empty
        \else
            \showthe#1%
        \fi
    }%
}

\def\showem#1#2#3{\toksa{#1}\toksb{#2}\toksc{#3}{\newlinechar=`^^J% show the first three arguments
    \errmessage{%
        arg. 1: \the\toksa^^J%
        arg. 2: \the\toksb^^J%
        arg. 3: \the\toksc%
}}}

\def\debugthisbox#1{% show the box with good depth and width
    \showboxdepth=1000
    \showboxbreadth=1000
    \ifcdebug\showbox#1 \fi
}
