%    pst-qtree.tex
%    Copyright (C) 2002 by David Chiang

%    This program 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 2 of the License, or
%    (at your option) any later version.

%    This program 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 this program; if not, write to the Free Software Foundation, Inc.,
%    51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.


\psset{nodesep=2pt,levelsep=36pt,treesep=24pt}
%\def\psedge{\ncdiag[arm=0,angleA=-90,angleB=90]}
\def\psnode#1{\TR{#1}}

\edef\TheAtCode{\the\catcode`\@}
\catcode`\@=11

\newtoks\result

%% These macros facilitate building up an object recursively before
%% putting it into the input stream.

\def\call#1#2{\let\@cont=#2\bgroup\result={}#1}
\def\return{%
\global\result=\result
\egroup\@cont
}

\def\ifeq#1#2{\edef\testa{#1}\edef\testb{#2}%
\ifx\testa\testb}

%% scan a tree: this just scans a subtree and then puts it onto the
%% input stream

\def\Tree{%
\ifeq{\psk@treemode}{0}%
 \def\psedge{\ncdiag[arm=0,angleA=-90,angleB=90]}
\fi
\ifeq{\psk@treemode}{2}%  if \psset{treemode=U}
 \def\psedge{\ncdiag[arm=0,angleA=90,angleB=-90]}
\fi
\ifeq{\psk@treemode}{1}%  if \psset{treemode=R}
 \def\psedge{\ncdiag[arm=0,angleA=0,angleB=180]}
\fi
\ifeq{\psk@treemode}{3}%  if \psset{treemode=L}
 \def\psedge{\ncdiag[arm=0,angleA=180,angleB=0]}
\fi
\call\@subtree\@Tree}
\def\@Tree{\the\result}

%% scan a subtree

\def\@subtree[{%
\toks0={\@nil}%
\@ifnextchar.{\call\@dotlabel\@@subtree}{\@@@subtree}}

\def\@@subtree{%
\toks0=\result
\@@@subtree
}

\def\@@@subtree{%
\call\@children\@@@@subtree
}

\def\@@@@subtree]{%
\toks2=\result
\@ifnextchar.{\call\@dotlabel\@@@@@subtree}{\@@@@@@subtree}}

\def\@@@@@subtree{%
%%% Check for mismatch.
\ifeq{\the\toks0}{\noexpand\@nil}%
  \toks0=\result
\fi
\ifeq{\the\toks0}{\the\result}\else
  \message{Warning: mismatched labels, \the\toks0{} and \the\result.}%
\fi
\@@@@@@subtree
}

\def\@@@@@@subtree{%
\ifeq{\the\toks0}{\noexpand\@nil}%
  \edef\act{\noexpand\result={\noexpand\pstree{\noexpand\psnode{}}{\the\toks2}}}%
\else
  \edef\act{\noexpand\result={\noexpand\pstree{\the\toks0}{\the\toks2}}}%
\fi
\act
\return}

%% scan a sequence of subtrees or leaves

\def\@children{%
\@ifnextchar]{\return}{%
\@ifnextchar[{\call\@subtree\@@children}{\call\@label\@@children}}}

\def\@@children{%
\toks0=\result
\call\@children\@@@children
}

\def\@@@children{%
\ifeq{\the\toks0}{}%
\else
  \toks2=\result
  \edef\act{\noexpand\result={\the\toks0 \the\toks2}}%
  \act
\fi
\return
}

%% scan an interior label

\def\@dotlabel.{%
\@label
}

%% scan a leaf node

\def\@label{%
\@ifnextchar!{\@litlabel}{\@@label}}

\def\@@label#1 {%
\result={\psnode{\hbox{\begin{tabular}{c} #1 \end{tabular}}}}%
\return
}

\def\@litlabel!#1 {%
\result={#1}%
\return
}

\def\roofedge#1#2{%
\ncdiag[arm=0pt,angleA=-90,angleB=90]{#1}{#2l}
\ncdiag[arm=0pt,angleA=-90,angleB=90]{#1}{#2r}
\ncdiag[arm=0pt,angleA=90,angleB=90]{#2l}{#2r}}

\def\Troof#1{%
\TR[edge=\roofedge]{%
\Rnode[href=-1]{\pspred-\the\psnodecnt l}
{\Rnode[href=1]{\pspred-\the\psnodecnt r}{#1}}}%
}

\catcode`\@=\TheAtCode\relax
\endinput

