%%
%% This is file `boolexpr.sty',
%% generated with the docstrip utility.
%%
%% The original source files were:
%%
%% boolexpr.dtx  (with options: `package')
%% 
%% This is a generated file.
%% 
%% boolexpr : a purely expandable boolean expressions and switch (eTeX)
%% 
%% 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
%% 
%% Copyright (C) 2009 by Florent Chervet <florent.chervet@free.fr>
%% 
%% This work consists of the main source file boolexpr.dtx
%% and the derived files
%%    boolexpr.sty, boolexpr.pdf, boolexpr.ins,
%% 
%% boolexpr : a purely expandable boolean expressions and switch  (eTeX)
\ProvidesPackage{boolexpr}
   [2009/09/03 v3.0 Purely expandable boolean expressions and switch (eTeX)]

% Make a colon (:) of catcode 4 (used as delimiter)
\let\bex@AtEnd\@empty
\def\TMP@EnsureCode#1#2{%
  \edef\bex@AtEnd{%
    \bex@AtEnd
    \catcode#1 \the\catcode#1\relax
  }%
  \catcode#1 #2\relax
}
\TMP@EnsureCode{95}{11}% _
\TMP@EnsureCode{36}{12}% $
\TMP@EnsureCode{60}{12}% <
\TMP@EnsureCode{61}{12}% =
\TMP@EnsureCode{62}{12}% >

% Three helper macros
\let\bex@truepart\@firstoftwo
\let\bex@falsepart\@secondoftwo

\long\def\bex__ifnotblank#1#2$#3#4#5$${#4}

% \bex@rel tests each atomic expression: choose which evaluation macro to use first
\long\def\bex@rel#1{%
   \bex@test_eval#1${\bex@eval{#1}}
      {\bex@test_neq#1<>$${\bex@neq #1$}
         {\bex@test_infeq#1<=$${\bex@infeq #1$}
            {\bex@test_inf#1<$${\bex@inf #1$}
               {\bex@test_supeq#1>=$${\bex@supeq #1$}
                  {\bex@test_sup#1>$${\bex@sup #1$}
                     {\bex@test_eq#1=$${\bex@eq #1$}
                        {\@latex@error{Unknown relation found while scanning
                           \noexpand\boolexpr!}\@ehd}$$}$$}$$}$$}$$}$$}}

% Tests macros to determine the type of each atomic expression
\def\bex@test_neq#1<>#2${\bex__ifnotblank#2$}
\def\bex@test_eq#1=#2${\bex__ifnotblank #2$}
\def\bex@test_infeq#1<=#2${\bex__ifnotblank #2$}
\def\bex@test_inf#1<#2${\bex__ifnotblank #2$}
\def\bex@test_supeq#1>=#2${\bex__ifnotblank #2$}
\def\bex@test_sup#1>#2${\bex__ifnotblank #2$}
\long\def\bex@test_eval#1#2${%
   \ifcat\noexpand#1\relax% #1 is a control sequence
         \bex@test_Eval{#1}
   \else \expandafter\@secondoftwo
   \fi}
\long\def\bex@test_Eval#1#2\fi{\fi\csname @%
   \ifx#1\the second%
   \else\ifx#1\numexpr second%
   \else\ifx #1\number second%
   \else\ifx #1\dimexpr second%
   \else\ifx #1\glueexpr second%
   \else\ifx #1\muexpr second%
   \else\ifx #1\value second%
   \else first%
   \fi\fi\fi\fi\fi\fi\fi oftwo\endcsname}

% Evaluation macro that evaluates an atomic expression depending on its type:
\long\def\bex@true_or_false#1{\csname bex@%
   \ifnum\numexpr#1 true\else false\fi part\endcsname}
\def\bex@false_or_true#1{\csname bex@%
   \ifnum\numexpr#1 false\else true\fi part\endcsname}

\def\bex@eq#1=#2${\bex@true_or_false{#1-(#2)=0}}
\def\bex@neq#1<>#2${\bex@false_or_true{#1-(#2)=0}}
\def\bex@infeq#1<=#2${\bex@false_or_true{#1-(#2)>0}}
\def\bex@inf#1<#2${\bex@true_or_false{#1-(#2)<0}}
\def\bex@supeq#1>=#2${\bex@false_or_true{#1-(#2)<0}}
\def\bex@sup#1>#2${\bex@true_or_false{#1-(#2)>0}}
\long\def\bex@eval#1{\bex@true_or_false{#1=0}}

% \bex@OR splits the string to evaluate into two parts: before the first \OR and after
\long\def\bex@OR#1\OR#2${\bex@AND{#2}#1\AND$}

% \bex@AND splits the string to evaluate into two parts: before the first \AND and after:
\long\def\bex@AND#1#2\AND#3${%
   \bex@rel{#2}
      {\bex__ifnotblank #3$${\bex@ANDAND{#1}#3$}{+0}$$}
      {\bex__ifnotblank #1$${\bex@OR#1$}{+1}$$}}

% \bex@ANDAND evaluate successive atomic expressions related by \AND:
\long\def\bex@ANDAND#1#2\AND#3${%
   \bex@rel{#2}
      {\bex__ifnotblank #3$${\bex@ANDAND{#1}#3$}{+0}$$}
      {\bex__ifnotblank #1$${\bex@OR#1$}{+1}$$}}

% \boolexpr: the entry point for evaluating boolean expressions
\newcommand\boolexpr[1]{\bex__ifnotblank #1$${\numexpr\bex@OR#1 \OR$}{+1}$$}

% \ifboolexpr: the LaTeX form of \boolexpr tests
\newcommand\ifboolexpr[1]{\bex@true_or_false{\boolexpr{#1}=0}}

\long\def \switch#1\endswitch {\bex__ifnotblank#1$${\bex@switch_opt#1 \endswitch}{}$$}
\long\def \bex@switch_opt#1#2\endswitch {\bex_ifsinglechar [{#1}%
      {\bex@switch_opti#1#2\endswitch}{\bex@switch_opti[]#1#2\endswitch}}%]
\def \bex@switch_opti[#1]#2\endswitch {\bex@switch_otherwise[{#1}]#2\otherwise\endswitch}

\def\bex@switch_otherwise[#1]#2\otherwise#3\endswitch{%
   \bex@switch_case[{#1}]#2\case\endswitch
      {\bex__ifnotblank#3$${\bex@otherwise#3\endswitch}{}$$}
      \endswitch}

\def\bex@switch_case[#1]#2\case#3\endswitch{\bex__ifnotblank#2$$%
      {\bex@case[{#1}]#2\endcase%
         {\bex__ifnotblank#3$${\bex@switch_case[{#1}]#3\endswitch}\@firstoftwo$$}}%
      {\bex__ifnotblank#3$${\bex@switch_case[{#1}]#3\endswitch}\@firstoftwo$$}$$}

\long\def\bex@case[#1]#2#3\endcase{\ifboolexpr{#1#2}{\bex@after_endswitch{#3}}}

\long\def\bex@after_endswitch#1#2\endswitch{#1}
\long\def\bex@otherwise#1\otherwise#2\endswitch{#1}
\ifdefined\pdfmatch
\long\def\bex_ifsinglechar#1#2{\csname @%
   \ifnum\pdfmatch{\detokenize{^[#1]$}}{\detokenize{#2}}=1 first\else second\fi
   oftwo\endcsname}
\else
\long\gdef\bex_ifsinglechar#1#2{\csname @%
   \bex__ifnotblank#2$${%
   \if\expandafter\@car\detokenize{#2}\relax\@nil\expandafter\@car\detokenize{#1}\string\\\@nil
      \expandafter\expandafter\expandafter\ifx
      \expandafter\expandafter\expandafter\\\expandafter\@cdr\detokenize{#2}\@nil\\%
            first%
      \else second%
      \fi
   \else second%
   \fi}{second}$$oftwo\endcsname}
\fi
\bex@AtEnd\let\bex@AtEnd\@undefined
\endinput
%%
%% End of file `boolexpr.sty'.
