%% The LaTeX package genealogytree - version 2.3.0 (2023/03/09)
%% gtrcore.parser.code.tex: Parser
%%
%% -------------------------------------------------------------------------------------------
%% Copyright (c) 2013-2023 by Prof. Dr. Dr. Thomas F. Sturm <thomas dot sturm at unibw dot de>
%% -------------------------------------------------------------------------------------------
%%
%% This work may be distributed and/or modified under the
%% conditions of the LaTeX Project Public License, either version 1.3
%% of this license or (at your option) any later version.
%% The latest version of this license is in
%%   http://www.latex-project.org/lppl.txt
%% and version 1.3 or later is part of all distributions of LaTeX
%% version 2005/12/01 or later.
%%
%% This work has the LPPL maintenance status `author-maintained'.
%%
%% This work consists of all files listed in README
%%
\gtr@set@library@version{2.3.0}

%% (local) stack implementation
\def\gtrnewstack#1{%
  \csedef{gtr@stack@#1@size}{0}%
}

\def\gtrstacksize#1{%
  \csname gtr@stack@#1@size\endcsname%
}

\def\gtrstackpush#1#2{%
  \count@\gtrstacksize{#1}%
  \advance\count@ 1\relax%
  \csedef{gtr@stack@#1@size}{\number\count@}%
  \csedef{gtr@stack@#1@\number\count@}{#2}%
}

\def\gtrstackpopto#1#2{%
  \count@\gtrstacksize{#1}%
  \edef#2{\csname gtr@stack@#1@\number\count@\endcsname}%
  \advance\count@ -1\relax%
  \csedef{gtr@stack@#1@size}{\number\count@}%
}

\def\gtrstackpop#1{%
  \count@\gtrstacksize{#1}%
  \csname gtr@stack@#1@\number\count@\endcsname%
  \advance\count@ -1\relax%
  \csedef{gtr@stack@#1@size}{\number\count@}%
}

\def\gtrstackpeekto#1#2{%
  \count@\gtrstacksize{#1}%
  \edef#2{\csname gtr@stack@#1@\number\count@\endcsname}%
}

\def\gtrstackpeek#1{%
  \count@\gtrstacksize{#1}%
  \csname gtr@stack@#1@\number\count@\endcsname%
}

\def\gtrmakestack#1{%
  \gtrnewstack{#1}%
  \csedef{gtr@stack@#1@size}{0}%
  \csdef{#1size}{\gtrstacksize{#1}}%
  \csdef{#1push}{\gtrstackpush{#1}}%
  \csdef{#1popto}{\gtrstackpopto{#1}}%
  \csdef{#1pop}{\gtrstackpop{#1}}%
  \csdef{#1peekto}{\gtrstackpeekto{#1}}%
  \csdef{#1peek}{\gtrstackpeek{#1}}%
}


%%%%%%%%%%%%%%%%%%%%%%%
%% Main Parser
%%%%%%%%%%%%%%%%%%%%%%%

\newcount\gtr@c@level%
\newcount\gtr@c@family%
\newcount\gtr@c@pid%

\gtrmakestack{gtr@famstack@}%
\gtrmakestack{gtr@levelstack@}%

\gtr@c@family\z@\relax
\gtr@c@pid\z@\relax

\newcommand{\gtr@parsegraph}[2][]{%
  \gtrset{#1,ignore family database}%
  \gtrkv@tikzset%
  \gtrkv@tcbset%
  %
  \let\gtr@timeflow=\gtrkv@timeflow%
  \letcs\gtr@init@leveldimens{gtr@init@timeflow@\gtr@timeflow}%
  \letcs\gtr@set@level@position{gtr@set@level@position@\gtr@timeflow}%
  \letcs\gtr@set@position@family{gtr@set@position@family@\gtr@timeflow}%
  %
  \edef\gtr@level@reference{0pt}%
  \gtr@proc@level@save@settings%
  \gtr@set@level{\gtrkv@proband@level}%
  \let\gtr@currentfamily=\@undefined%
  \numdef\gtr@num@fam@first{\gtr@c@family+1}%
  \numdef\gtr@num@per@first{\gtr@c@pid+1}%
  \gtr@parse@graphwithhandle{\gtr@handle@continue@graph}#2x%
  \numdef\gtr@num@fam@last{\gtr@c@family}%
  \numdef\gtr@num@per@last{\gtr@c@pid}%
  \gtr@set@currentfamily{\gtr@num@fam@first}%
  \csdimdef{gtr@fam@\gtr@currentfamily @offset}{\gtrkv@tree@offset}%
  \gtr@positioning%
}

\def\gtr@parse@graphwithhandle#1{%
  \let\gtr@handle@continue=#1%
  \gtr@parse@graph%
}

\def\gtr@parse@graph{%
  \afterassignment\gtr@handle@graph\let\@gtr@token=%
}

\def\gtr@handle@graph{%
  \let\@next=\gtr@parse@error@token%
  \ifx\@gtr@token x%
    \let\@next=\gtr@parse@x%
  \else%
    \expandafter\ifx\space\@gtr@token%
      \let\@next\gtr@parse@graph%
    \else%
      \ifx\@gtr@token i%
        \let\@next\gtr@parse@i%
      \else%
        \ifx\@gtr@token\par%
          \let\@next\gtr@parse@graph%
        \else%
          \gtr@handle@continue%
        \fi%
      \fi%
    \fi%
  \fi%
  \@next%
}

\def\gtr@handle@continue@graph{%
  \ifx\@gtr@token p%
    \let\gtr@handle@continue\gtr@handle@continue@parent%
    \let\@next=\gtr@parse@graph@parent@%
  \else%
    \ifx\@gtr@token c%
      \let\gtr@handle@continue\gtr@handle@continue@child%
      \let\@next=\gtr@parse@graph@child@%
    \else%
      \ifx\@gtr@token s%
        \let\gtr@handle@continue\gtr@handle@continue@sandclock%
        \let\@next=\gtr@parse@graph@sandclock@%
      \fi%
    \fi%
  \fi%
}


\def\gtr@parse@graph@parent@ arent{\@ifnextchar[{\gtr@parent@@parent}{\gtr@parent@@parent[]}}

\def\gtr@parse@graph@child@ hild{%
  \gtr@up@level%
  \dimdef\gtr@level@reference{\gtr@level@reference+\gtr@level@size+\gtr@level@distance}%
  \@ifnextchar[{\gtr@child@@child}{\gtr@child@@child[]}}

\def\gtr@parse@graph@sandclock@ andclock{\@ifnextchar[{\gtr@sandclock@@}{\gtr@sandclock@@[]}}

\def\gtr@parse@i n{%
  \@ifnextchar p{\gtr@parse@input@}{\gtr@parse@insert@}}

\def\gtr@parse@input@ put#1{\expandafter\expandafter\expandafter \gtr@parse@graph \csname @@input\endcsname #1 }

\def\gtr@parse@insert@ sert#1{\expandafter\expandafter\expandafter\gtr@parse@graph\csname#1\endcsname}

\def\gtr@parse@x{}

\def\gtr@parse@err{%
  \gtr@error{Parser error}{Some went wrong during parsing your genealogy tree data}%
}

\def\gtr@parse@error@token{%
  \gtr@error{Parser: unfeasible token}{You probably used some unfeasible token in your genealogy tree data.
    Press return to display this token.}%
  \show\@gtr@token
}

\long\def\gtr@parse@error#1{%
  \gtr@error{Parser: undefined #1}{Some went wrong during parsing your genealogy tree data}%%
}


%%%%%%%%%%%%%%%%%%%%%%%
%% Parent Parser
%%%%%%%%%%%%%%%%%%%%%%%

\def\gtr@handle@continue@parent{%
  \ifx\@gtr@token g%
    \let\@next=\gtr@pparse@g%
  \else%
    \ifx\@gtr@token p%
      \let\@next=\gtr@pparse@p%
    \else
      \ifx\@gtr@token c%
        \let\@next=\gtr@pparse@c%
      \fi%
    \fi%
  \fi%
}


\def\gtr@pparse@c{\@ifnextchar[{\gtr@parent@@c}{\gtr@parent@@c[]}}
\long\def\gtr@parent@@c[#1]#2{%
  \gtr@trav@@c[#1]{#2}%
  \gtr@parse@graphwithhandle{\gtr@handle@continue@parent}%
}
\long\def\gtr@parent@@c@ignore[#1]#2{%
  \gtr@parse@graphwithhandle{\gtr@handle@continue@parent}%
}


\def\gtr@pparse@p{%
  \@ifnextchar a{\gtr@pparse@parent@}{\gtr@pparse@p@}}
\def\gtr@pparse@parent@ arent{\@ifnextchar[{\gtr@parent@@parent}{\gtr@parent@@parent[]}}
\long\def\gtr@parent@@parent[#1]#2{%
  \gtr@trav@@parent[#1]{#2}%
  \gtr@parse@graphwithhandle{\gtr@handle@continue@parent}%
}
\def\gtr@pparse@p@{\@ifnextchar[{\gtr@parent@@p}{\gtr@parent@@p[]}}


\long\def\gtr@parent@@p@normal[#1]#2{%
  \gtr@trav@@p[#1]{#2}%
  \gtr@parse@graphwithhandle{\gtr@handle@continue@parent}%
}
\long\def\gtr@parent@@p@malefemale[#1]#2{%
  \ifnum\gtr@c@level<\gtr@complementparents@levellimit\relax%
    \gtr@trav@@complementedparent{g[#1]{#2}p[c@mpl@m@nt@d,complemented]{male}p[c@mpl@m@nt@d,complemented]{female}}%
  \else%
    \gtr@trav@@p[#1]{#2}%
  \fi%
  \gtr@parse@graphwithhandle{\gtr@handle@continue@parent}%
}
\long\def\gtr@parent@@p@femalemale[#1]#2{%
  \ifnum\gtr@c@level<\gtr@complementparents@levellimit\relax%
    \gtr@trav@@complementedparent{g[#1]{#2}p[c@mpl@m@nt@d,complemented]{female}p[c@mpl@m@nt@d,complemented]{male}}%
  \else%
    \gtr@trav@@p[#1]{#2}%
  \fi%
  \gtr@parse@graphwithhandle{\gtr@handle@continue@parent}%
}
\long\def\gtr@parent@@p@unspecific[#1]#2{%
  \ifnum\gtr@c@level<\gtr@complementparents@levellimit\relax%
    \gtr@trav@@complementedparent{g[#1]{#2}p[c@mpl@m@nt@d,complemented]{}p[c@mpl@m@nt@d,complemented]{}}%
  \else%
    \gtr@trav@@p[#1]{#2}%
  \fi%
  \gtr@parse@graphwithhandle{\gtr@handle@continue@parent}%
}


\def\gtr@pparse@g{\@ifnextchar[{\gtr@parent@@g}{\gtr@parent@@g[]}}
\long\def\gtr@parent@@g[#1]#2{%
  \gtr@trav@@parent@g[#1]{#2}%
  \gtr@parse@graphwithhandle{\gtr@handle@continue@parent}%
}


%%%%%%%%%%%%%%%%%%%%%%%
%% Child Parser
%%%%%%%%%%%%%%%%%%%%%%%

\def\gtr@handle@continue@child{%
  \ifx\@gtr@token g%
    \let\@next=\gtr@cparse@g%
  \else%
    \ifx\@gtr@token p%
      \let\@next=\gtr@cparse@p%
    \else
      \ifx\@gtr@token c%
        \let\@next=\gtr@cparse@c%
      \else
        \ifx\@gtr@token u%
          \let\@next=\gtr@cparse@u%
        \fi
      \fi%
    \fi%
  \fi%
}


\def\gtr@cparse@p{\@ifnextchar[{\gtr@child@@p}{\gtr@child@@p[]}}
\long\def\gtr@child@@p[#1]#2{%
  \gtr@trav@@p[#1]{#2}%
  \gtr@parse@graphwithhandle{\gtr@handle@continue@child}%
}

\def\gtr@cparse@c{%
  \@ifnextchar h{\gtr@cparse@child@}{\gtr@cparse@c@}}
\def\gtr@cparse@child@ hild{\@ifnextchar[{\gtr@child@@child}{\gtr@child@@child[]}}
\long\def\gtr@child@@child[#1]#2{%
  \gtr@trav@@child[#1]{#2}%
  \gtr@parse@graphwithhandle{\gtr@handle@continue@child}%
}
\def\gtr@cparse@c@{\@ifnextchar[{\gtr@child@@c}{\gtr@child@@c[]}}
\long\def\gtr@child@@c[#1]#2{%
  \gtr@trav@@c[#1]{#2}%
  \gtr@parse@graphwithhandle{\gtr@handle@continue@child}%
}

\def\gtr@cparse@u nion{\@ifnextchar[{\gtr@child@@union}{\gtr@child@@union[]}}
\long\def\gtr@child@@union[#1]#2{%
  \gtr@trav@@union[#1]{#2}%
  \gtr@parse@graphwithhandle{\gtr@handle@continue@child}%
}

\def\gtr@cparse@g{\@ifnextchar[{\gtr@child@@g}{\gtr@child@@g[]}}
\long\def\gtr@child@@g[#1]#2{%
  \gtr@trav@@child@g[#1]{#2}%
  \gtr@parse@graphwithhandle{\gtr@handle@continue@child}%
}


%%%%%%%%%%%%%%%%%%%%%%%
%% Union Parser
%%%%%%%%%%%%%%%%%%%%%%%

\def\gtr@handle@continue@union{%
  \ifx\@gtr@token c%
    \let\@next=\gtr@uparse@c%
  \else%
    \ifx\@gtr@token p%
      \let\@next=\gtr@uparse@p%
    \fi%
  \fi%
}

\def\gtr@uparse@c{%
  \@ifnextchar h{\gtr@uparse@child@}{\gtr@uparse@c@}}
\def\gtr@uparse@child@ hild{\@ifnextchar[{\gtr@union@@child}{\gtr@union@@child[]}}
\long\def\gtr@union@@child[#1]#2{%
  \let\gtr@handle@continue\gtr@handle@continue@child%
  \gtr@trav@@child[#1]{#2}%
  \gtr@parse@graphwithhandle{\gtr@handle@continue@union}%
}
\def\gtr@uparse@c@{\@ifnextchar[{\gtr@union@@c}{\gtr@union@@c[]}}
\long\def\gtr@union@@c[#1]#2{%
  \gtr@trav@@c[#1]{#2}%
  \gtr@parse@graphwithhandle{\gtr@handle@continue@union}%
}

\def\gtr@uparse@p{\@ifnextchar[{\gtr@union@@p}{\gtr@union@@p[]}}
\long\def\gtr@union@@p[#1]#2{%
  \gtr@trav@@p[#1]{#2}%
  \gtr@parse@graphwithhandle{\gtr@handle@continue@union}%
}


%%%%%%%%%%%%%%%%%%%%%%%
%% Sandclock Parser
%%%%%%%%%%%%%%%%%%%%%%%

\def\gtr@handle@continue@sandclock{%
  \ifx\@gtr@token c%
    \let\@next=\gtr@sparse@c%
  \else%
    \ifx\@gtr@token p%
      \let\@next=\gtr@sparse@p%
    \fi%
  \fi%
}


\long\def\gtr@sandclock@@[#1]#2{%
  \gtr@trav@@sandclock[#1]{#2}%
  \gtr@parse@graphwithhandle{\gtr@handle@continue@graph}%
}

\def\gtr@sparse@c{%
  \@ifnextchar h{\gtr@sparse@child@}{\gtr@sparse@c@}}
\def\gtr@sparse@child@ hild{\@ifnextchar[{\gtr@sandclock@@child}{\gtr@sandclock@@child[]}}
\long\def\gtr@sandclock@@child[#1]#2{%
  \gtr@trav@@child[#1]{#2}%
  \gtr@parse@graphwithhandle{\gtr@handle@continue@sandclock}%
}
\def\gtr@sparse@c@{\@ifnextchar[{\gtr@sandclock@@c}{\gtr@sandclock@@c[]}}
\long\def\gtr@sandclock@@c[#1]#2{%
  \gtr@trav@@c[#1]{#2}%
  \gtr@parse@graphwithhandle{\gtr@handle@continue@sandclock}%
}

\def\gtr@sparse@p{%
  \@ifnextchar a{\gtr@sparse@parent@}{\gtr@sparse@p@}}
\def\gtr@sparse@parent@ arent{\@ifnextchar[{\gtr@sandclock@@parent}{\gtr@sandclock@@parent[]}}
\long\def\gtr@sandclock@@parent[#1]#2{%
  \gtr@trav@@parent[#1]{#2}%
  \gtr@parse@graphwithhandle{\gtr@handle@continue@sandclock}%
}
\def\gtr@sparse@p@{\@ifnextchar[{\gtr@sandclock@@p}{\gtr@sandclock@@p[]}}
\long\def\gtr@sandclock@@p[#1]#2{%
  \gtr@trav@@p[#1]{#2}%
  \gtr@parse@graphwithhandle{\gtr@handle@continue@sandclock}%
}


%%%%%%%%%%%%%%%%%%%%%%%
%% Traversing
%%%%%%%%%%%%%%%%%%%%%%%

\def\gtr@set@currentfamily#1{\edef\gtr@currentfamily{\number#1}}

\def\gtr@next@family{%
  \let\gtr@lastfamily=\gtr@currentfamily%
  \ifdef{\gtr@currentfamily}{\gtr@famstack@push{\gtr@currentfamily}}{}%
  \advance\gtr@c@family 1\relax%
  \gtr@set@currentfamily{\gtr@c@family}%
}

\def\gtr@previous@family{%
  \let\gtr@lastfamily=\gtr@currentfamily%
  \ifnum\gtr@famstack@size>0\relax%
     \gtr@famstack@popto{\gtr@currentfamily}%
  \else%
     \let\gtr@currentfamily=\@undefined%
  \fi%
}

\def\gtr@set@currentperson#1{\edef\gtr@currentperson{\number#1}}

\def\gtr@next@person{%
  \advance\gtr@c@pid 1\relax%
  \gtr@set@currentperson{\gtr@c@pid}%
}

\def\gtr@set@level#1{%
  \gtr@c@level#1\relax%
  \gtr@proc@level@opt%
}

\def\gtr@up@level{%
  \advance\gtr@c@level 1\relax%
  \gtr@proc@level@opt%
}

\def\gtr@up@level@push{%
  \gtr@levelstack@push{\gtr@level@reference}%
  \gtr@up@level%
  \dimdef\gtr@level@reference{\gtr@level@reference+\gtr@level@size+\gtr@level@distance}%
}

\def\gtr@up@level@pop{%
  \gtr@up@level%
  \gtr@levelstack@popto{\gtr@level@reference}%
}

\def\gtr@down@level{%
  \advance\gtr@c@level -1\relax%
  \gtr@proc@level@opt%
}

\def\gtr@down@level@push{%
  \gtr@levelstack@push{\gtr@level@reference}%
  \dimdef\gtr@level@reference{\gtr@level@reference-\gtr@level@size-\gtr@level@distance}%
  \gtr@down@level%
}

\def\gtr@down@level@pop{%
  \gtr@down@level%
  \gtr@levelstack@popto{\gtr@level@reference}%
}

\long\def\gtr@trav@@parent[#1]#2{%
  \gtr@up@level@push%
  \gtr@next@family%
  \gtr@proc@@parent@begin{#1}%
  \ifgtr@ignore%
    \advance\gtr@c@family -1\relax%
  \else%
    \gtr@parse@beginfamily@hook%
    \gtr@parse@graphwithhandle{\gtr@handle@continue@parent}#2x%
    \gtr@parse@endfamily@hook%
    \gtr@parse@complementparents@hook%
    \gtr@proc@@parent@end%
  \fi%
  \gtr@previous@family%
  \gtr@down@level@pop%
  \gtr@parse@afterfamily@hook%
}

\long\def\gtr@trav@@complementedparent#1{%
  \gtr@up@level@push%
  \gtr@next@family%
  \gtr@proc@@parent@begin{family=complemented family}%
  \ifgtr@ignore%
    \advance\gtr@c@family -1\relax%
  \else%
    \gtr@parse@beginfamily@hook%
    \gtr@parse@graph #1x%
    \gtr@parse@endfamily@hook%
    \gtr@proc@@parent@end%
  \fi%
  \gtr@previous@family%
  \gtr@down@level@pop%
  \gtr@parse@afterfamily@hook%
}

\long\def\gtr@trav@@child[#1]#2{%
  \gtr@down@level@push%
  \gtr@next@family%
  \gtr@proc@@child@begin{#1}%
  \ifgtr@ignore%
    \advance\gtr@c@family -1\relax%
  \else%
    \gtr@parse@beginfamily@hook%
    \gtr@parse@graphwithhandle{\gtr@handle@continue@child}#2x%
    \gtr@parse@endfamily@hook%
    \gtr@parse@childlesschild@hook%
    \gtr@proc@@child@end%
  \fi%
  \gtr@previous@family%
  \gtr@up@level@pop%
  \gtr@parse@afterfamily@hook%
}

\long\def\gtr@trav@@sandclock[#1]#2{%
  \gtr@up@level@push%
  \gtr@next@family%
  \gtr@proc@@sandclock@begin{#1}%
  \ifgtr@ignore%
    \advance\gtr@c@family -1\relax%
  \else%
    \gtr@parse@beginfamily@hook%
    \gtr@parse@graphwithhandle{\gtr@handle@continue@sandclock}#2x%
    \gtr@parse@endfamily@hook%
    \gtr@proc@@sandclock@end%
  \fi%
  \gtr@previous@family%
  \gtr@down@level@pop%
}

\long\def\gtr@trav@@union[#1]#2{%
  \gtr@next@family%
  \gtr@proc@@union@begin{#1}%
  \ifgtr@ignore%
    \advance\gtr@c@family -1\relax%
  \else%
    \gtr@parse@beginfamily@hook%
    \gtr@parse@graphwithhandle{\gtr@handle@continue@union}#2x%
    \gtr@parse@endfamily@hook%
    \gtr@proc@@union@end%
  \fi%
  \gtr@previous@family%
  \gtr@parse@afterfamily@hook%
}


\long\def\gtr@trav@@add@node#1#2#3{%
  \gtr@proc@add@node{#1}{#2}%
  \ifgtr@ignore%
    \advance\gtr@c@pid -1\relax%
  \else%
    #3%
  \fi%
}
% \long\def\gtr@trav@@add@node@malefemale#1#2#3{%
  % \gdef\gtr@gtrav@mark{m}%
  % \gtr@proc@add@node{#1}{#2}%
  % \ifgtr@ignore%
    % \advance\gtr@c@pid -1\relax%
  % \else%
    % \csedef{gtr@per@\gtr@currentperson @mark}{\gtr@gtrav@mark}%
    % #3%
  % \fi%
% }
% \long\def\gtr@trav@@add@node@femalemale#1#2#3{%
  % \gdef\gtr@gtrav@mark{f}%
  % \gtr@proc@add@node{#1}{#2}%
  % \ifgtr@ignore%
    % \advance\gtr@c@pid -1\relax%
  % \else%
    % \csedef{gtr@per@\gtr@currentperson @mark}{\gtr@gtrav@mark}%
    % #3%
  % \fi%
% }


\long\def\gtr@trav@@parent@g[#1]#2{%
  \gtr@down@level@push%
  \gtr@next@person%
  \def\gtrnodetype{g}%
  \let\gtrifcnode\@secondoftwo%
  \let\gtrifpnode\@secondoftwo%
  \let\gtrifgnode\@firstoftwo%
  \ifcsdef{gtr@fam@\gtr@currentfamily @fam}{%
    \let\gtrifroot\@secondoftwo%
    \let\gtrifparent\@firstoftwo%
    \let\gtrifleaf\@secondoftwo%
    \let\gtrifleafchild\@secondoftwo%
  }{%
    \let\gtrifroot\@firstoftwo%
    \let\gtrifparent\@secondoftwo%
    \let\gtrifleaf\@firstoftwo%
    \let\gtrifleafchild\@firstoftwo%
  }%
  \let\gtrifchild\@firstoftwo%
  \let\gtrifleafparent\@secondoftwo%
  \gtr@trav@@add@node{#1}{#2}{\gtr@proc@@parent@g@content}%
  \gtr@up@level@pop%
  \gtr@parse@afternode@hook%
}

\long\def\gtr@trav@@child@g[#1]#2{%
  \gtr@next@person%
  \def\gtrnodetype{g}%
  \let\gtrifcnode\@secondoftwo%
  \let\gtrifpnode\@secondoftwo%
  \let\gtrifgnode\@firstoftwo%
  \ifcsdef{gtr@fam@\gtr@currentfamily @fam}{%
    \let\gtrifroot\@secondoftwo%
    \let\gtrifchild\@firstoftwo%
    \let\gtrifleaf\@secondoftwo%
    \let\gtrifleafparent\@secondoftwo%
  }{%
    \let\gtrifroot\@firstoftwo%
    \let\gtrifchild\@secondoftwo%
    \let\gtrifleaf\@firstoftwo%
    \let\gtrifleafparent\@firstoftwo%
  }%
  \let\gtrifparent\@firstoftwo%
  \let\gtrifleafchild\@secondoftwo%
  \gtr@trav@@add@node{#1}{#2}{\gtr@proc@@child@g@content}%
  \gtr@parse@afternode@hook%
}

\long\def\gtr@trav@@p[#1]#2{%
  \gtr@next@person%
  \def\gtrnodetype{p}%
  \let\gtrifcnode\@secondoftwo%
  \let\gtrifpnode\@firstoftwo%
  \let\gtrifgnode\@secondoftwo%
  \let\gtrifroot\@secondoftwo%
  \let\gtrifchild\@secondoftwo%
  \let\gtrifparent\@firstoftwo%
  \let\gtrifleaf\@firstoftwo%
  \let\gtrifleafchild\@secondoftwo%
  \let\gtrifleafparent\@firstoftwo%
  \gtr@trav@@add@node{#1}{#2}{\gtr@proc@@p@content}%
  \gtr@parse@afternode@hook%
}

\long\def\gtr@trav@@c[#1]#2{%
  \gtr@down@level@push%
  \gtr@next@person%
  \def\gtrnodetype{c}%
  \let\gtrifcnode\@firstoftwo%
  \let\gtrifpnode\@secondoftwo%
  \let\gtrifgnode\@secondoftwo%
  \let\gtrifroot\@secondoftwo%
  \let\gtrifchild\@firstoftwo%
  \let\gtrifparent\@secondoftwo%
  \let\gtrifleaf\@firstoftwo%
  \let\gtrifleafchild\@firstoftwo%
  \let\gtrifleafparent\@secondoftwo%
  \gtr@trav@@add@node{#1}{#2}{\gtr@proc@@c@content}%
  \gtr@up@level@pop%
  \gtr@parse@afternode@hook%
}


%%%%%%%%%%%%%%%%%%%%%%%
%% Hooks
%%%%%%%%%%%%%%%%%%%%%%%

\def\gtr@parse@init@hook#1#2{%
  \ifcsvoid{#1@content}{%
    \csdef{#1}{%
      \letcs\gtr@currentid{#2}%
      \ifdefvoid{\gtr@currentid}{}{\csuse{#1@content}}%
    }%
  }{}%
}

\def\gtr@parse@appto@hook#1#2#3{%
  \begingroup\edef\x{\endgroup\noexpand\appto\expandonce{\csname #1@content\endcsname}{%
    \noexpand\ifdefstring{\noexpand\gtr@currentid}{\expandonce{#2}}{%
      \noexpand\gtr@parse@graph\expandonce{#3}x}{}%
  }}\x%
}


%%%%%%%%%%%%%%%%%%%%%%%
%% Parent Autofill
%%%%%%%%%%%%%%%%%%%%%%%

% precondition: list with exact 2 parents
\def\gtr@parse@toggle@parents{%
  \letcs\gtr@currentparentlist{gtr@fam@\gtr@currentfamily @par}%
  \def\dodo##1{%
    \def\gtr@tempB{##1}%
  }%
  \def\do##1{%
    \def\gtr@tempA{##1}%
    \let\do\dodo%
  }%
  \dolistloop{\gtr@currentparentlist}%
  \csundef{gtr@fam@\gtr@currentfamily @par}%
  \listcseadd{gtr@fam@\gtr@currentfamily @par}{\gtr@tempB}%
  \listcseadd{gtr@fam@\gtr@currentfamily @par}{\gtr@tempA}%
  \letcs\gtr@currentparentlist{gtr@fam@\gtr@currentfamily @par}%
}

\def\gtr@parse@complementparents@malefemale{%
  \ifnum\gtr@c@level>\gtr@complementparents@levellimit\relax%
  \else%
    \ifcsdef{gtr@fam@\gtr@currentfamily @par}%
    {%
      \def\dodo##1{%
        \let\gtr@parse@do@complementparents\@secondoftwo%
      }%
      \def\do##1{%
        \let\gtr@parse@do@complementparents\@firstoftwo%
        \letcs\gtr@temp{gtr@per@##1@flag@s}%
        \let\do\dodo%
      }%
      \dolistcsloop{gtr@fam@\gtr@currentfamily @par}%
      \gtr@parse@do@complementparents%
      {%
        \ifdefstring{\gtr@temp}{female}%
        {
          \gtr@parse@graph p[c@mpl@m@nt@d,complemented]{male}x
          \gtr@parse@toggle@parents%
        }%
        {
          \gtr@parse@graph p[c@mpl@m@nt@d,complemented]{female}x
        }%
      }{%
        \ifdefstring{\gtr@temp}{female}{\gtr@parse@toggle@parents}{}%
      }%
    }%
    {%
      \gtr@parse@graph p[c@mpl@m@nt@d,complemented]{male}p[c@mpl@m@nt@d,complemented]{female}x
    }%
  \fi%
}

\def\gtr@parse@complementparents@femalemale{%
  \ifnum\gtr@c@level>\gtr@complementparents@levellimit\relax%
  \else%
    \ifcsdef{gtr@fam@\gtr@currentfamily @par}%
    {%
      \def\dodo##1{%
        \let\gtr@parse@do@complementparents\@secondoftwo%
      }%
      \def\do##1{%
        \let\gtr@parse@do@complementparents\@firstoftwo%
        \letcs\gtr@temp{gtr@per@##1@flag@s}%
        \let\do\dodo%
      }%
      \dolistcsloop{gtr@fam@\gtr@currentfamily @par}%
      \gtr@parse@do@complementparents%
      {%
        \ifdefstring{\gtr@temp}{male}%
        {
          \gtr@parse@graph p[c@mpl@m@nt@d,complemented]{female}x
          \gtr@parse@toggle@parents%
        }%
        {
          \gtr@parse@graph p[c@mpl@m@nt@d,complemented]{male}x
        }%
      }{%
        \ifdefstring{\gtr@temp}{male}{\gtr@parse@toggle@parents}{}%
      }%
    }%
    {%
      \gtr@parse@graph p[c@mpl@m@nt@d,complemented]{female}p[c@mpl@m@nt@d,complemented]{male}x
    }%
  \fi%
}

\def\gtr@parse@complementparents@unspecific{%
  \ifnum\gtr@c@level>\gtr@complementparents@levellimit\relax%
  \else%
    \ifcsdef{gtr@fam@\gtr@currentfamily @par}%
    {%
      \def\dodo##1{%
        \let\gtr@parse@do@complementparents\@secondoftwo%
      }%
      \def\do##1{%
        \let\gtr@parse@do@complementparents\@firstoftwo%
        \let\do\dodo%
      }%
      \dolistcsloop{gtr@fam@\gtr@currentfamily @par}%
      \gtr@parse@do@complementparents%
      {%
        \gtr@parse@graph p[c@mpl@m@nt@d,complemented]{}x
      }{}%
    }%
    {%
      \gtr@parse@graph p[c@mpl@m@nt@d,complemented]{}p[c@mpl@m@nt@d,complemented]{}x
    }%
  \fi%
}


%%%%%%%%%%%%%%%%%%%%%%%
%% Information
%%%%%%%%%%%%%%%%%%%%%%%

\def\gtrnodenumber{\gtr@currentperson}
\def\gtrnodefamily{\gtr@currentfamily}
\def\gtrnodelevel{\number\gtr@c@level}

\newrobustcmd\gtrifnodeid[2]{\ifdefvoid{\gtr@gkv@id}{#2}{#1}}
\def\gtrnodeid{\gtrifnodeid{\expandafter\detokenize\expandafter{\gtr@gkv@id}}{???}}