summaryrefslogtreecommitdiff
path: root/macros/latex/contrib/boolexpr/boolexpr.sty
blob: 7836e6d25e75c6793116ab936d320d83511f366d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
%%
%% 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}
   [2010/04/15 v3.14 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{47}{8}% / etextool delimiter
\TMP@EnsureCode{60}{12}% <
\TMP@EnsureCode{61}{12}% =
\TMP@EnsureCode{62}{12}% >
\TMP@EnsureCode{43}{12}% -
\TMP@EnsureCode{45}{12}% +
\TMP@EnsureCode{58}{8}% : delimitor

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

\long\def\bex@nbk#1#2/#3#4#5//{#4}
\long\def\bex@ifoptchar#1[#2/#3#{\csname @\if @\detokenize{#1#2}@%
   first\else second\fi oftwo\endcsname}

% \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@nbk#2/}
\def\bex@test_eq#1=#2/{\bex@nbk #2/}
\def\bex@test_infeq#1<=#2/{\bex@nbk #2/}
\def\bex@test_inf#1<#2/{\bex@nbk #2/}
\def\bex@test_supeq#1>=#2/{\bex@nbk #2/}
\def\bex@test_sup#1>#2/{\bex@nbk #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}
\long\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@nbk #3//{\bex@ANDAND{#1}#3:}{+0}//}
      {\bex@nbk #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@nbk #3//{\bex@ANDAND{#1}#3:}{+0}//}
      {\bex@nbk #1//{\bex@OR#1:}{+1}//}}

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

% \ifboolexpr: the LaTeX form of \boolexpr tests
\ifdefined\ifboolexpr% etoolbox defines ifboolexpr...
\PackageWarning{boolexpr}{\string\ifboolexpr\space has been defined before\MessageBreak
    by etoolbox (I suppose) - Overwritting}
\renewcommand\ifboolexpr[1]{\bex@true_or_false{\boolexpr{#1}=0}}
\else
\newcommand\ifboolexpr[1]{\bex@true_or_false{\boolexpr{#1}=0}}
\fi

\long\def \switch#1\endswitch {\bex@nbk#1//{\bex@switch_opt#1\endswitch}{}//}
\long\def \bex@switch_opt#1#2\endswitch{\bex@ifoptchar#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@nbk#3//{\bex@otherwise#3\endswitch}{}//}
      \endswitch}

\def\bex@switch_case[#1]#2\case#3\endswitch{\bex@nbk#2//%
      {\bex@case[{#1}]#2\endcase%
         {\bex@nbk#3//{\bex@switch_case[{#1}]#3\endswitch}\@firstoftwo//}}%
      {\bex@nbk#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}
\long\def\bex@pdfmatch#1#2{\ifnum\pdfmatch{#2}{#1}=0 1\else0\fi}
\long\def\bex@ifempty#1{\if\relax\detokenize{#1}\relax0\else1\fi}
\long\def\bex_ifempty#1{\csname @\if\relax\detokenize{#1}\relax first\else second\fi oftwo\endcsname}
\long\def\bex@ifblank#1{\bex@nbk#1//10//}
\long\def\bex@ifx#1#2{\bex__ifx#1#2//}
\long\def\bex_ifx#1#2#3/#4#5#6//{\bex@nbk#6//{\ifx#1#2\bex_else#5\else\bex_fi#6\fi}{#5}//}
\long\def\bex_else#1\else#2\fi{\fi#1}
\long\def\bex_fi#1\fi{\fi#1}
\long\def\bex@comp#1{\bex@ifoptchar#1/[/\bex@c@mp{\bex@c@mp@[\numexpr]}}
\long\def\bex@c@mp[#1#2]#3#4#5{%
   \bex_ifempty{#2}{%
      \ifx #1\dimexpr         \bex@c@mp@\ifdim\dimexpr{#3}{#4}{#5}%
      \else\ifx #1\numexpr    \bex@c@mp@\ifnum\numexpr{#3}{#4}{#5}%
      \else\ifx #1\glueexpr   \bex@c@mp@\ifdim\glueexpr{#3}{#4}{#5}%
      \else\ifx #1\muexpr     \bex@c@mp@\ifdim\muexpr{#3}{#4}{#5}%
      \else\ifx #1\number     \bex@c@mp@\ifnum\numexpr{#3}{#4}{#5}%
      \else\PackageError{boolexpr}{%
         Invalid comparison test while scanning \string\bex@comp\MessageBreak
         found: \detokenize{#1}}%
      \fi\fi\fi\fi\fi}%
      {\PackageError{boolexpr}{Invalid comparison test while scanning \string\bex@comp\MessageBreak
      found: \detokenize{#1}}}}
\long\def\bex@c@mp@#1#2#3#4#5{#1#2#3#4#5 0\else 1\fi}
\bex@AtEnd\let\bex@AtEnd\@undefined
\endinput
%%
%% End of file `boolexpr.sty'.