\ProvidesPackage{abraces}% http://ctan.org/pkg/abraces
  [2012/08/24 v1.0 Arbitrary and asymmetric braces]
\newif\if@overload
\DeclareOption{overload}{\@overloadtrue}
\ProcessOptions
\newsavebox{\bracebox}% stores content contained under/over brace

\newcommand{\newbracespec}[2]{% Add a new brace specification based on existing ones
  \@defbracecharcode{#1}{\@genbrace#2\@nnil\@genbrace}
}
% Brace components
\newcommand{\bracescript}[1]{{% Add a script above/below an over-/underbrace
  \let\@@bfil\@@bunfil% Make line leaders into void/empty leaders
  \let\@use@rl@se\phantom% Phantomize some brace components
  \makebox[\wd\bracebox]{$\genbrace{#1}$}% Place the text in a box of width \bracebox
}}
\def\@@bunfil{\leaders\hbox{\ }\hfill}% Empty filler
\newcommand{\bracefil}[1]{%
  \csname brace@char@#1'\endcsname%
}
\def\@@bfil{\leaders \vrule \@height \ht\z@ \@depth \z@ \hfill}% default brace filler
%\def\bfil{\@@bfil}%
%\def\@bLfil{\@@bfil}% left leader filler
%\def\@bRfil{\@@bfil}% right leader filler
\def\aupbracefill#1{$\m@th\setbox\z@\hbox{$\braceld$}\genbrace{#1}$}
\def\adownbracefill#1{$\m@th\setbox\z@\hbox{$\braceld$}\genbrace{#1}$}

% Repetition structure. Reference:
%   Repeat command n times?
%   http://tex.stackexchange.com/q/16189/5764
% Alternatively
%   \RequirePackage{expl3}% http://ctan.org/pkg/expl3
%   \ExplSyntaxOn
%   \cs_new_eq:NN \Repeat \prg_replicate:nn
%   \ExplSyntaxOff

\newcommand{\Repeat}[1]{%
  \expandafter\@Repeat\expandafter{\the\numexpr #1\relax}%
}
\def\@Repeat#1{%
    \ifnum#1>0
        \expandafter\@@Repeat\expandafter{\the\numexpr #1-1\expandafter\relax\expandafter}%
    \else
        \expandafter\@gobble
    \fi
}
\def\@@Repeat#1#2{%
    \@Repeat{#1}{#2}#2%
}
% Taken from mathtools package (http://ctan.org/pkg/mathtools)
\newcommand{\aunderbrace}[2][l1D1r]{%
  \begin{lrbox}{\bracebox}$\displaystyle{#2}$\end{lrbox}%
  \mathop{\vtop{\m@th\ialign{##\crcr
    $\hfil\displaystyle{#2}\hfil$\crcr
    \noalign{\kern.7\fontdimen5\textfont2\nointerlineskip}%
    \aupbracefill{#1}\crcr\noalign{\kern.5\fontdimen5\textfont2}}}}\limits}
\newcommand{\aoverbrace}[2][L1U1R]{%
  \begin{lrbox}{\bracebox}$\displaystyle{#2}$\end{lrbox}%
  \mathop{\vbox{\m@th\ialign{##\crcr
    \noalign{\kern.5\fontdimen5\textfont2}%
    \adownbracefill{#1}\crcr
    \noalign{\kern.7\fontdimen5\textfont2\nointerlineskip}%
  $\hfil\displaystyle{#2}\hfil$\crcr}}}\limits}

% Parsing structure. Reference:
%   Parsing a macro argument character-by-character for conditional execution
%   http://tex.stackexchange.com/q/33197/5764

\let\@use@rl@se\relax
\def\@defbracecharcode#1{% Macro to define brace character "codes"
  \@namedef{brace@char@#1}%
}
\@defbracecharcode{L}{\@use@rl@se\braceld\@genbrace}% Left down
\@defbracecharcode{R}{\@use@rl@se\bracerd\@genbrace}% Right down
\@defbracecharcode{l}{\@use@rl@se\bracelu\@genbrace}% Left up
\@defbracecharcode{r}{\@use@rl@se\braceru\@genbrace}% Right up
\@defbracecharcode{U}{\@use@rl@se\braceru\@use@rl@se\bracelu\@genbrace}% Tip up
\@defbracecharcode{D}{\@use@rl@se\bracerd\@use@rl@se\braceld\@genbrace}% Tip down
\@defbracecharcode{0}{\@@bunfil\@genbrace}% Do nothing
\@defbracecharcode{1}{\@@bfil\@genbrace}% 1 filler
\@defbracecharcode{2}{\@@bfil\@@bfil\@genbrace}% 2 fillers
\@defbracecharcode{3}{\@@bfil\@@bfil\@@bfil\@genbrace}% 3 fillers
\@defbracecharcode{4}{\@@bfil\@@bfil\@@bfil\@@bfil\@genbrace}% 4 fillers
\@defbracecharcode{5}{\@@bfil\@@bfil\@@bfil\@@bfil\@@bfil\@genbrace}% 5 fillers
\@defbracecharcode{6}{\@@bfil\@@bfil\@@bfil\@@bfil\@@bfil\@@bfil\@genbrace}% 6 fillers
\@defbracecharcode{7}{\@@bfil\@@bfil\@@bfil\@@bfil\@@bfil\@@bfil\@@bfil\@genbrace}% 7 fillers
\@defbracecharcode{8}{\@@bfil\@@bfil\@@bfil\@@bfil\@@bfil\@@bfil\@@bfil\@@bfil\@genbrace}% 8 fillers
\@defbracecharcode{9}{\@@bfil\@@bfil\@@bfil\@@bfil\@@bfil\@@bfil\@@bfil\@@bfil\@@bfil\@genbrace}% 9 fillers
\@defbracecharcode{@}{\@firstofone@go}% Insert arbitrary stuff
\@defbracecharcode{!}{\@firstofone@len}% Insert a leader of specific length
\@defbracecharcode{*}{\@repeat@num@times}% Repeat stuff a number of times

\newcommand{\@firstofone@go}[1]{#1\@genbrace}%
\newcommand{\@firstofone@len}[1]{\hb@xt@#1{\@@bfil}\@genbrace}%
\newcommand{\@repeat@num@times}[2]{\Repeat{#1}{\@genbrace#2\@nnil}\@genbrace}%

\newcommand{\@genbrace}[1]{%
  \ifx\@nnil#1\relax\else
    \@nameuse{brace@char@#1\expandafter}%
  \fi%
}
\newcommand{\genbrace}[1]{%
  \@genbrace#1\@nnil%
}

% If package was loaded with \usepackage[overload]{abraces}
\if@overload
  \let\overbrace\aoverbrace
  \let\underbrace\aunderbrace
\fi
\endinput