%%
%% This is file `lwarp-simplebnf.sty',
%% generated with the docstrip utility.
%%
%% The original source files were:
%%
%% lwarp.dtx  (with options: `simplebnf')
%% This is a generated file.
%% Copyright 2016-2022 Brian Dunn
%% 
%% 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
%% and version 1.3 or later is part of all distributions of LaTeX
%% version 2005/12/01 or later.
\LWR@ProvidesPackagePass{simplebnf}[2022/05/08]
\ExplSyntaxOn

\RenewDocumentEnvironment { bnfgrammar } { O{llcll} O{[^\|]\|[^\|]} O{\|\|} +b }
  {
    \regex_gset:Nn \g_simplebnf_rhs_newline_r { #2 }
    \regex_gset:Nn \g_simplebnf_rhs_nb_r { #3 }

    %% \l__input_seq is a list of term definitions.
    \regex_split:nnN { ;; } { #4 } \l__input_seq
    \begin{center}
    \begin{lateximage}[#4]%     lwarp
      \tl_set:Nn \l__table_tl
        {
          \begin{tabular}{#1}
        }

    \bool_set_true:N \l_tmp_first_term % Is this the first term in this grammar?
    \seq_map_inline:Nn \l__input_seq
      {
        %% If not-first, add newline
        \bool_if:NTF \l_tmp_first_term
          {
            \bool_set_false:N \l_tmp_first_term
          }
          {
            \tl_put_right:Nn \l__table_tl { \\ }
          }

        \regex_split:nnNTF { ::= } { ##1 } \l__term_seq
          % Parse a ::= definition
          {
            %% \l__term_seq    - (lhs, rhses)...
            %% \l__term_tl     - lhs
            %% \l__keypairs_tl - rhses
            \seq_pop_left:NN \l__term_seq \l__term_tl
            \seq_pop_left:NN \l__term_seq \l__keypairs_tl

            \simplebnf_typeset_lhs:n{\l__term_tl}
            \tl_put_right:Nn \l__table_tl
              {
                & \SimpleBNFDefEq &
              }
            %% \l__keypairs_seq - (rhs:annot | rhs)...
            \regex_split:NVN \g_simplebnf_rhs_newline_r \l__keypairs_tl \l__keypairs_seq

            \bool_set_true:N \l__first_rhs
            \seq_map_function:NN \l__keypairs_seq \simplebnf_typeset_rhs:n
          }
          {
            % Else, parse a \in declaration
            \regex_split:nnNTF { \c{in} } { ##1 } \l__term_seq
              {
                %% \l__term_seq - (lhs, rhs)
                \seq_pop_left:NN \l__term_seq \l_tmpa_tl

                \simplebnf_typeset_lhs:n{\l_tmpa_tl}
                \tl_put_right:Nn \l__table_tl
                  {
                    & $\in$ & $
                  }
                \seq_pop_left:NN \l__term_seq \l_tmpa_tl
                \tl_put_right:NV \l__table_tl \l_tmpa_tl
                \tl_put_right:Nn \l__table_tl
                  {
                    $ &
                  }
              }
              { \msg_error:nn {simplebnf} { Could not parser ##1 } }
          }
      }

    \tl_put_right:Nn \l__table_tl { \end{tabular} }
    \tl_use:N \l__table_tl
    \end{lateximage}%       lwarp
    \end{center}
  }
  { }

\ExplSyntaxOff

\endinput
%%
%% End of file `lwarp-simplebnf.sty'.
