% BEGIN_FOLD Parsing attributes
\let\yquant@langhelper@list@attrs=\empty
% gatecallback is called by every "standard" gate (which does not include the "create" type of gates)
\let\yquant@langhelper@gatecallback=\@gobble

\protected\long\def\yquant@langhelper@check@attrs[#1]{%
   \appto\yquant@langhelper@list@attrs{#1,}%
   \yquant@env@contscan%
}

% shorthands for declaring attributes
\protected\def\yquant@langhelper@declare@attr{%
   \pgfqkeys{/yquant/all attrs}%
}

\protected\def\yquant@langhelper@declare@attr@global{%
   \pgfqkeys{/yquant/global attrs}%
}

\pgfkeys{%
   /yquant/attr filter handler/.code={%
      \let\yquant@langhelper@filter@@name=\pgfkeyscurrentkeyRAW%
      \let\yquant@langhelper@filter@@value=\pgfkeyscurrentvalue%
      % the key was filtered out, i.e., it is neither a local nor a global attribute.
      \pgfkeysinterruptkeyfilter%
         % execute it in /yquant, if it exists there
         \pgfqkeys{/yquant}{\yquant@langhelper@filter@@name/.try/.expand once=\yquant@langhelper@filter@@value}%
         \ifpgfkeyssuccess%
            \eappto#1{%
               ,/yquant/\unexpanded\expandafter{\yquant@langhelper@filter@@name}%
                  \unless\ifx\yquant@langhelper@filter@@value\pgfkeysnovalue@text%
                     ={\unexpanded\expandafter{\yquant@langhelper@filter@@value}}%
                  \fi%
            }%
         \else%
            \pgfkeys{/yquant/operator style/.expanded=%
               {\unexpanded\expandafter{\yquant@langhelper@filter@@name}=%
                  {\unexpanded\expandafter{\yquant@langhelper@filter@@value}}%
               }%
            }%
            \eappto#1{%
               ,/yquant/operator style={\unexpanded\expandafter{\yquant@langhelper@filter@@name}%
                  \unless\ifx\yquant@langhelper@filter@@value\pgfkeysnovalue@text%
                     ={\unexpanded\expandafter{\yquant@langhelper@filter@@value}}%
                  \fi%
               }%
            }%
         \fi%
      \endpgfkeysinterruptkeyfilter%
   },%
   /pgf/key filters/attr filter/.code={%
      % we need to capture every non-attribute (which means filtering them out).
      \expandafter\ifyquant@beginswith\expandafter{\pgfkeyscurrentkey}{/yquant/attrs/}{%
         % the key is in /yquant/attrs. Does it actually exist (but not as a handler, which would define non-existing keys)?
         \ifnum\pgfkeyscasenumber=3 % handler
            % handlers are tricky. The existence does not rely on the question whether the key itself exists, but the handler. This is not what we want for attributes. We strip the handler (the handler is in \pgfkeyscurrentname, while the original key is fully contained in \pgfkeyscurrentpath) and check whether it existed (think of handlers such as .expanded, which for sure should be valid, while we don't want to define new arguments).
            \pgfkeysifassignable{\pgfkeyscurrentkey}{%
               % case 1 and 2
               \pgfkeysfiltercontinuetrue%
            }{%
               % don't have to worry about option 3, so this is 0. But maybe this was actually a global attribute? Again, we have to discard our handler. Note that this change to the current-macros will affect further processing!
               \edef\pgfkeyscurrentkey{/yquant/global attrs/\pgfkeyscurrentkeyRAW}%
               \pgfkeyssplitpath%
               \pgfkeysifassignable{\pgfkeyscurrentpath}{%
                  \pgfkeysfiltercontinuetrue%
               }{%
                  \pgfkeysfiltercontinuefalse%
               }%
            }%
         \else%
            \ifnum\pgfkeyscasenumber=0 % unknown
               % non-existing - now we need to check whether we can find it in the global attributes.
               \pgfkeysifassignable{/yquant/global attrs/\pgfkeyscurrentkeyRAW}%
                  \pgfkeysfiltercontinuetrue%
                  % it is not, so it is not a known attribute for this operator. Filter.
                  \pgfkeysfiltercontinuefalse%
            \else% command key or contains value
               \pgfkeysfiltercontinuetrue%
            \fi%
         \fi%
      }\relax%
   },%
   /yquant/attr filter/.style={%
      /pgf/key filters/attr filter/.install key filter,%
      /yquant/attr filter handler/.install key filter handler=\yquant@attrs@remaining%
   }%
}

% specifies which attributes can be used for a given command
% #1: command name
% #2: comma-separated list of required attrs
% #3: comma-separated list of optional attrs
\protected\def\yquant@langhelper@setup@attrs#1#2#3{%
   \begingroup%
      \lowercase{\edef\@yquant@cmd{#1}}%
      \edef\@yquant@keyscmd{%
         \noexpand\pgfkeyslet{/yquant/attrs/\@yquant@cmd/.unknown/.@cmd}%
                             \yquant@langhelper@setup@attrs@unknown%
      }%
      \let\@yquant@keysset=\relax%
      \let\@yquant@keyscheck=\relax%
      \def\@yquant@do##1{%
         \eappto\@yquant@keyscmd{%
            \noexpand\pgfkeysdef{/yquant/attrs/\@yquant@cmd/##1}{%
               \unexpanded{%
                  \cslet{yquant@langhelper@attrs@req@##1}\relax%
                  \pgfkeysinterruptkeyfilter%
                     \pgfkeys{/yquant/all attrs/##1={####1}}%
                  \endpgfkeysinterruptkeyfilter%
               }%
            }%
         }%
         \eappto\@yquant@keysset{%
            \def\expandafter\noexpand\csname yquant@langhelper@attrs@req@##1\endcsname{%
               \yquant@langhelper@setup@attrs@missing{##1}%
            }%
         }%
         \expandafter\appto\expandafter\@yquant@keyscheck%
            \csname yquant@langhelper@attrs@req@##1\endcsname%
      }%
      \forcsvlist\@yquant@do{#2}%
      \global\cslet{yquant@langhelper@setup@attrs@set@\@yquant@cmd}\@yquant@keysset%
      \global\cslet{yquant@langhelper@setup@attrs@check@\@yquant@cmd}\@yquant@keyscheck%
      \def\@yquant@do##1{%
         \eappto\@yquant@keyscmd{%
            \noexpand\pgfkeysdef{/yquant/attrs/\@yquant@cmd/##1}{%
               \unexpanded{%
                  \pgfkeysinterruptkeyfilter%
                     \pgfkeys{/yquant/all attrs/##1={####1}}%
                  \endpgfkeysinterruptkeyfilter%
               }%
            }%
         }%
      }%
      \forcsvlist\@yquant@do{#3}%
      \expandafter%
   \endgroup%
   \@yquant@keyscmd%
}

\protected\def\yquant@langhelper@setup@attrs@unknown#1\pgfeov{%
   % This is in its essence /.search also={/yquant/global attrs}, but with filtering interrupted
   \pgfkeysinterruptkeyfilter%
      \ifpgfkeysaddeddefaultpath%
         % only process keys for which no full path has been provided:
         \pgfkeyssuccessfalse%
         \let\pgfkeys@searchalso@name=\pgfkeyscurrentkeyRAW%
         % We interrupt the filtering process because we only want to append the very top-level command to the remaining-macro.
         \unless\ifpgfkeyssuccess%
            \pgfqkeys{/yquant/global attrs}%
                     {\pgfkeys@searchalso@name={#1}}%
         \fi%
      \else%
         % the /.search also implementation defines this in an unexpanded macro, which is then executed. Any reason?
         \pgfkeysvalueof{/handlers/.unknown/.@cmd}#1\pgfeov%
      \fi%
   \endpgfkeysinterruptkeyfilter%
}

\protected\def\yquant@langhelper@setup@attrs@missing#1{%
   \PackageError{yquant.sty}{Required attribute `#1' missing}%
      {You used a command that requires specifying the attribute `#1'.}%
}%
% END_FOLD

% BEGIN_FOLD Parsing command itself
\protected\def\yquant@langhelper@check@name#1 {%
   \lowercase{\edef\@yquant@cmd{#1}}%
   \unless\ifcsname yquant@lang@\@yquant@cmd\endcsname%
      \PackageError{yquant.sty}{Unsupported yquant command: `#1'}%
         {You used a command that is unknown to yquant.}%
   \fi%
   \edef\yquant@langhelper@cmd@original{#1}%
   % Provide the association with the correct attributes
   \csname yquant@langhelper@setup@attrs@set@\@yquant@cmd\endcsname%
   % For "ordinary" uses of the quotes library (without additional options), it would be better not to load the library now, since then the original text (everything in quotes) is just appended to /yquant/operator style. However, then uses with options will break the pgfkeys parser, as they are invalid. For this reason, we must enable the quotes library already at this state, with the consequence that we store the longer version, i.e., the output of the quotes-parsed expression instead.
   \tikz@enable@node@quotes%
   % execute them
   \let\yquant@attrs@remaining=\empty%
   \unless\ifx\yquant@langhelper@list@attrs\empty%
      \edef\yquant@langhelper@list@attrs{%
         \noexpand\pgfqkeysfiltered%
            {/yquant/attrs/\@yquant@cmd}%
            {\unexpanded\expandafter{\yquant@langhelper@list@attrs}}%
      }%
      \pgfkeys{/yquant/attr filter}%
      \yquant@langhelper@list@attrs%
      \let\yquant@langhelper@list@attrs=\empty% required for nested environments
   \fi%
   % If there was an argument, set it
   \@ifnextchar\bgroup%
      \yquant@langhelper@check@name@i%
      \yquant@langhelper@check@name@ii%
}

\protected\long\def\yquant@langhelper@check@name@i#1{%
   \ifdefined\yquant@lang@attr@value%
      \PackageWarning{yquant.sty}{Node content given explicitly and as an attribute. Using the explicit value.}%
   \fi%
   \def\yquant@lang@attr@value{#1}%
   \let\yquant@langhelper@attrs@req@value=\relax%
   \yquant@langhelper@check@name@ii%
}

\protected\def\yquant@langhelper@check@name@ii{%
   % Check all necessary attributes were given.
   \csname yquant@langhelper@setup@attrs@check@\@yquant@cmd\endcsname%
   % Finally execute the command
   \letcs\@yquant@cmd{yquant@lang@\@yquant@cmd}%
   \yquant@langhelper@declare@command@@parse%
}
% END_FOLD

% BEGIN_FOLD Declarations
\newif\ifyquant@langhelper@execclear@lastgate
% Defining commands that accept registers and take an optional value
% #1: name of the argument
% #2: command (sequence) that is to be called before the target macros are filled
% #3: command (sequence) that is to be called after the target macros are filled
\protected\def\yquant@langhelper@declare@command#1#2#3{%
   \begingroup%
      \lowercase{\edef\@yquant@cmd{#1}}%
      \long\protected\csxdef{yquant@lang@\@yquant@cmd}##1##2##3{%
         \noexpand\yquant@langhelper@gatecallback{\@yquant@cmd}%
         \noexpand\yquant@langhelper@execclear@lastgatetrue%
         \unexpanded{#2}%
         \yquant@circuit@operator{##1}{##2}{##3}%
         \noexpand\ifyquant@langhelper@execclear@lastgate%
            \yquant@for \noexpand\@yquant@i := \noexpand\yquant@circuit@operator@minctrl to \noexpand\yquant@circuit@operator@maxctrl {%
               \yquant@register@execclear@lastgate{\noexpand\@yquant@i}{\@yquant@cmd}%
            }%
         \noexpand\fi%
         \unexpanded{#3}%
      }%
   \endgroup%
}

% Defining commands that accept registers and take an optional value, but don't allow for controls
% #1: name of the argument
% #2: command (sequence) that is to be called before the target macros are filled
% #3: command (sequence) that is to be called after the target macros are filled
\protected\def\yquant@langhelper@declare@command@uncontrolled#1#2#3{%
   \begingroup%
      \lowercase{\edef\@yquant@cmd{#1}}%
      \long\protected\csxdef{yquant@lang@\@yquant@cmd}##1##2##3{%
         \noexpand\ifstrempty{##1}\relax{%
            \noexpand\PackageError{yquant.sty}{Positive controls are not allowed for the command `\@yquant@cmd`}{}%
         }%
         \noexpand\ifstrempty{##2}\relax{%
            \noexpand\PackageError{yquant.sty}{Negative controls are not allowed for the command `\@yquant@cmd`}{}%
         }%
         \noexpand\yquant@langhelper@gatecallback{\@yquant@cmd}%
         \noexpand\yquant@langhelper@execclear@lastgatetrue%
         \unexpanded{#2}%
         \yquant@circuit@operator{}{}{##3}%
         \noexpand\ifyquant@langhelper@execclear@lastgate%
            \yquant@for \noexpand\@yquant@i := \noexpand\yquant@circuit@operator@minctrl to \noexpand\yquant@circuit@operator@maxctrl {%
               \yquant@register@execclear@lastgate{\noexpand\@yquant@i}{\@yquant@cmd}%
            }%
         \noexpand\fi%
         \unexpanded{#3}%
      }%
   \endgroup%
}

% Defining commands that accept an undefined register and take an optional value, but don't allow for controls
% #1: name of the argument
% #2: macro that is to be called with parameter #1 being the name of the register
\protected\def\yquant@langhelper@declare@command@create#1#2{%
   \begingroup%
      \lowercase{\edef\@yquant@cmd{#1}}%
      \long\protected\csxdef{yquant@lang@\@yquant@cmd}##1##2{%
         \noexpand\ifstrempty{##1}\relax{%
            \noexpand\PackageError{yquant.sty}{Positive controls are not allowed for the command `\@yquant@cmd`}{}%
         }%
         \noexpand\ifstrempty{##2}\relax{%
            \noexpand\PackageError{yquant.sty}{Negative controls are not allowed for the command `\@yquant@cmd`}{}%
         }%
         \noexpand#2%
      }%
   \endgroup%
}

% make #1 an identical command to #2 (does not copy attributes)
\protected\def\yquant@langhelper@declare@command@alias#1#2{%
   \global\csletcs{yquant@lang@#1}{yquant@lang@#2}%
}

\long\def\yquant@langhelper@declare@command@@parse#1;{%
   \yquant@langhelper@declare@command@@extract@nctrl#1~;%
   \yquant@env@rescan%
}

\def\yquant@langhelper@declare@command@@extract@nctrl#1~#2;{%
   \ifstrempty{#2}{%
      \yquant@langhelper@declare@command@@extract@pctrl#1|~~;%
   }{%
      \yquant@langhelper@declare@command@@extract@pctrl#1|~#2;%
   }%
}

\def\yquant@langhelper@declare@command@@extract@pctrl#1|#2~#3~;{%
   \ifstrempty{#2}{%
      \yquant@langhelper@declare@command@@exec#1||~#3;%
   }{%
      \yquant@langhelper@declare@command@@exec#1|#2~#3;%
   }%
}

\protected\def\yquant@langhelper@declare@command@@exec#1|#2|~#3;{%
   \edef\@yquant@params{%
      {\trim@spaces{#2}}%
      {\trim@spaces{#3}}%
      {\trim@spaces{#1}}%
   }%
   \expandafter\@yquant@cmd\@yquant@params%
}
% END_FOLD

% shorthands for validation of property values
% #1: a macro that will contain the sanitized value
% #2: a TeX register type: most likely \count, \dimen
% #3: a string to be checked
% This macro is not expandable.
\protected\def\yquant@langhelper@validate#1#2#3{%
   \begingroup%
      \afterassignment\yquant@langhelper@validate@i%
      #2 255=#3\relax\yquant@end%
      \unless\@ifyquant@valid%
         \PackageError{yquant.sty}{Invalid property value: `#3'}%
            {The value must be assignable to a \protect#2 register.}%
      \fi%
      \expandafter%
   \endgroup%
   \expandafter\def\expandafter#1\expandafter{\the#2 255}%
}

\def\yquant@langhelper@validate@relax{\relax}
\protected\def\yquant@langhelper@validate@i#1\relax#2\yquant@end{%
   % if the valid value was not user-delimited by \relax, #1 and #2 are empty
   % if the valid value was user-delimited by \relax, #1 is empty and #2 is \relax
   \let\@ifyquant@valid=\iffalse%
   \ifstrempty{#1}{%
      \def\@yquant@tmp{#2}%
      \ifx\empty\@yquant@tmp%
         \csletcs{@ifyquant@valid}{iftrue}%
      \else%
         \ifx\yquant@langhelper@validate@relax\@yquant@tmp%
            \csletcs{@ifyquant@valid}{iftrue}%
         \fi%
      \fi%
   }\relax%
   % eat the rest (which should be empty anyway)
}