%%
%% This is file `naive-ebnf.sty',
%% generated with the docstrip utility.
%%
%% The original source files were:
%%
%% naive-ebnf.dtx  (with options: `package')
%% (The MIT License)
%% 
%% Copyright (c) 2023-2025 Yegor Bugayenko
%% 
%% Permission is hereby granted, free of charge, to any person obtaining a copy
%% of this software and associated documentation files (the 'Software'), to deal
%% in the Software without restriction, including without limitation the rights
%% to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
%% copies of the Software, and to permit persons to whom the Software is
%% furnished to do so, subject to the following conditions:
%% 
%% The above copyright notice and this permission notice shall be included in all
%% copies or substantial portions of the Software.
%% 
%% THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
%% IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
%% FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
%% AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
%% LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
%% OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
%% SOFTWARE.



\NeedsTeXFormat{LaTeX2e}
\ProvidesPackage{naive-ebnf}
[0000/00/00 0.0.0 EBNF in Plain Text]

















\RequirePackage{pgfopts}
\pgfkeys{
  /ebnf/.cd,
  bw/.store in=\ebnf@bw,
  trail/.store in=\ebnf@trail,
  trail/.default=naive-ebnf.tmp.tex,
}
\ProcessPgfPackageOptions{/ebnf}

\RequirePackage{expl3}

\makeatletter\ifdefined\ebnf@bw\else
  \RequirePackage{xcolor}
\fi
\newcommand\ebnf@color[2]
  {\ifdefined\ebnf@bw#2\else\textcolor{#1}{#2}\fi}
\makeatother

\makeatletter
\newcommand\EbnfTerminal[1]{{%
  \relax\ifmmode\else\ttfamily\fi%
  \ebnf@color{gray}{\relax\ifmmode\textsf{``}\else{\sffamily``}\fi}%
  #1%
  \ebnf@color{gray}{\relax\ifmmode\textsf{''}\else{\sffamily''}\fi}}}
\makeatother

\makeatletter
\newcommand\EbnfNonTerminal[1]{{%
  \ebnf@color{gray}{\relax\ifmmode\langle\else\(\langle\)\fi}%
  \relax\ifmmode\textsf{#1}\else{\sffamily#1}\fi%
  \ebnf@color{gray}{\relax\ifmmode\rangle\else\(\rangle\)\fi}}}
\makeatother

\makeatletter
\newcommand\EbnfSpecial[1]{{\relax\ifmmode\else\ttfamily\fi#1}}%
\makeatother

\makeatletter
\newcommand\EbnfRegex[1]{{\relax\ifmmode\else\ttfamily\fi/#1/}}%
\makeatother

\makeatletter
\newcommand\ebnf@optional[1]
  {\ebnf@color{gray}{[}#1\ebnf@color{gray}{]}}
\newcommand\ebnf@repetition[2][]
  {\ebnf@color{gray}{\{}#2\ebnf@color{gray}{\}\(^{\scriptscriptstyle #1}\)}}
\newcommand\ebnf@grouping[1]
  {\ebnf@color{gray}{(}#1\ebnf@color{gray}{)}}
\ExplSyntaxOn
\newcommand\ebnf@terminal[1]{
  \tl_set:Nn \l_ebnf_tl {}
  \tl_set_rescan:Nnn \l_ebnf_tl {} { #1 }
  \EbnfTerminal{\l_ebnf_tl}
}
\newcommand\ebnf@special[1]{
  \tl_set:Nn \l_ebnf_tl {}
  \tl_set_rescan:Nnn \l_ebnf_tl {} { #1 }
  \EbnfSpecial{\l_ebnf_tl}
}
\newcommand\ebnf@nonterminal[1]{
  \tl_set:Nn \l_ebnf_tl {}
  \tl_set_rescan:Nnn \l_ebnf_tl {} { #1 }
  \EbnfNonTerminal{\l_ebnf_tl}
}
\newcommand\ebnf@regexp[1]{
  \tl_set:Nn \l_ebnf_tl {}
  \tl_set_rescan:Nnn \l_ebnf_tl {} { #1 }
  \EbnfRegex{\l_ebnf_tl}
}
\ExplSyntaxOff
\newcommand\ebnf@to
  {\ebnf@color{gray}{\(\to\)}}
\newcommand\ebnf@alternation
  {\ebnf@color{gray}{\(\vert\)}}
\makeatother

\ExplSyntaxOn
\cs_generate_variant:Nn \tl_replace_all:Nnn {Nx}
\makeatletter
\NewDocumentEnvironment{ebnf}{O{4em}+b}
  {\tl_set:Nn\ebnf_tmp{#2}}
  {%
  \regex_replace_all:nnN
    { ([^\s])/([^\s]) } {\1\\slash{}\2} \ebnf_tmp%
  \regex_replace_all:nnN
    { ([^\s])< } {\1\\textless{}} \ebnf_tmp%
  \regex_replace_all:nnN
    { >([^\s]) } {\\textgreater{}\1} \ebnf_tmp%
  \regex_replace_all:nnN
    { ([^\s])'([^\s]) } {\1\\textquotesingle{}\2} \ebnf_tmp%
  \regex_replace_all:nnN { \|\|\| }%
    {\c{makebox}[#1][r]{ }} \ebnf_tmp%
  \regex_replace_all:nnN
    { ([^\s])\|([^\s]) } {\1\\textbar{}\2} \ebnf_tmp%
  %
  \regex_replace_all:nnN
    { /(.+?)/ }%
    {\c{ebnf@regexp}{\1}} \ebnf_tmp%
  \cs_undefine:N\ebnf_curled%
  \cs_new:Npn\ebnf_curled{%
    \regex_replace_all:nnNT
    { \{\s(([^\s]*(\s[^\}\{]|\s(\}|\{)[^\s])?)*)\s\}([\+\*])? }%
    {\c{ebnf@repetition}[\5]{\1}} \ebnf_tmp \ebnf_curled}%
  \ebnf_curled%
  \cs_undefine:N\ebnf_brackets%
  \cs_new:Npn\ebnf_brackets{%
    \regex_replace_all:nnNT
    { \(\s(([^\s]*(\s[^\)\(]|\s(\)|\()[^\s])?)*)\s\) }%
    {\c{ebnf@grouping}{\1}} \ebnf_tmp \ebnf_brackets}%
  \ebnf_brackets%
  \cs_undefine:N\ebnf_squares%
  \cs_new:Npn\ebnf_squares{%
    \regex_replace_all:nnNT
    { \[\s(([^\s]*(\s[^\]\[]|\s(\]|\[)[^\s])?)*)\s\] }%
    {\c{ebnf@optional}{\1}} \ebnf_tmp \ebnf_squares}%
  \ebnf_squares%
  \regex_replace_all:nnN { (<[^>]+?>\s:=) }%
    {\c{makebox}[#1][r]{\1}} \ebnf_tmp%
  \regex_replace_all:nnN { <(.+?)> }%
    {\c{ebnf@nonterminal}{\1}} \ebnf_tmp%
  \regex_replace_all:nnN { "(.+?)" }%
    {\c{ebnf@terminal}{\1}} \ebnf_tmp%
  \regex_replace_all:nnN { '(.+?)' }%
    {\c{ebnf@special}{\1}} \ebnf_tmp%
  \regex_replace_all:nnN { \|(\|) }%
    {\c{makebox}[#1][r]{ \1 }} \ebnf_tmp%
  \regex_replace_all:nnN { \| }%
    {\c{ebnf@alternation}{}} \ebnf_tmp%
  \regex_replace_all:nnN { := }%
    {\c{ebnf@to}{}} \ebnf_tmp%
  \tl_put_left:Nn \ebnf_tmp {\noindent}
  \tl_put_right:Nn \ebnf_tmp {}
  \ifdefined\ebnf@trail%
    \newwrite\ebnf@write%
    \immediate\openout\ebnf@write\ebnf@trail\relax%
    \immediate\write\ebnf@write{\unexpanded\expandafter{\ebnf_tmp}}%
    \immediate\closeout\ebnf@write%
    \message{naive-ebnf:\space pre-processed\space TeX
      \space saved\space to\space "\ebnf@trail"^^J}%
  \fi%
  \ebnf_tmp}
\makeatother
\ExplSyntaxOff

\endinput
%%
%% End of file `naive-ebnf.sty'.
