texlive[56761] Master/texmf-dist: simplebnf (25oct20)

commits+karl at tug.org commits+karl at tug.org
Sun Oct 25 22:34:44 CET 2020


Revision: 56761
          http://tug.org/svn/texlive?view=revision&revision=56761
Author:   karl
Date:     2020-10-25 22:34:44 +0100 (Sun, 25 Oct 2020)
Log Message:
-----------
simplebnf (25oct20)

Modified Paths:
--------------
    trunk/Master/texmf-dist/doc/latex/simplebnf/simplebnf-doc.pdf
    trunk/Master/texmf-dist/doc/latex/simplebnf/simplebnf-doc.tex
    trunk/Master/texmf-dist/tex/latex/simplebnf/simplebnf.sty

Modified: trunk/Master/texmf-dist/doc/latex/simplebnf/simplebnf-doc.pdf
===================================================================
(Binary files differ)

Modified: trunk/Master/texmf-dist/doc/latex/simplebnf/simplebnf-doc.tex
===================================================================
--- trunk/Master/texmf-dist/doc/latex/simplebnf/simplebnf-doc.tex	2020-10-25 21:34:30 UTC (rev 56760)
+++ trunk/Master/texmf-dist/doc/latex/simplebnf/simplebnf-doc.tex	2020-10-25 21:34:44 UTC (rev 56761)
@@ -1,82 +1,76 @@
 \documentclass[a4paper]{article}
 
-\usepackage{fontspec}
-\setmainfont{Linux Libertine}
-\setsansfont{Libertinus Sans}
-\setmonofont{Inconsolata}
-
+\usepackage{simplebnf}
 \usepackage{hyperref}
 
 \usepackage{tcolorbox}
 \tcbuselibrary{listings,breakable}
 \tcbset{listing engine=listings,colframe=black,colback=white,size=small}
+
 \NewDocumentEnvironment {exampleside} {}
-  { \tcblisting{listing side text,righthand width=.4\textwidth} }
+  { \tcblisting{listing side text,righthand width=.5\textwidth} }
   { \endtcblisting }
 
+\NewDocumentCommand \cmd { m } {\texttt{\textbackslash#1}}
+
 \NewDocumentEnvironment { presentcommand } { b }
   {%
     \vspace*{0.5\baselineskip}\noindent\fbox{%
-      \begin{minipage}{\dimexpr\textwidth-2\fboxsep-2\fboxrule}
-        #1
-      \end{minipage}}\vspace*{0.5\baselineskip}
+    \begin{minipage}{\dimexpr\textwidth-2\fboxsep-2\fboxrule}
+      #1
+    \end{minipage}}\vspace*{0.5\baselineskip}
   }
   { }
 
-\NewDocumentCommand \cmd { m } { \texttt{\textbackslash#1} }
 \NewDocumentCommand \env { m m }
   {
     \texttt{%
-      \textbackslash begin\{#1\} \textrm{#2} %
+      \textbackslash begin\{#1\} \textrm{#2}%
       \textbackslash end\{#1\}%
     }%
   }
 
-\usepackage{simplebnf}
-
 \title{%
   \textsf{simplebnf} --- A simple package to format Backus-Naur form%
-  \footnote{This file describes v0.1.0.}}
+  \footnote{This file describes v0.2.0.}}
 \author{Jay Lee\footnote{E-mail: %
   \href{mailto:jaeho.lee at snu.ac.kr}{\texttt{jaeho.lee at snu.ac.kr}}}}
-\date{2019/12/23}
+\date{2020/09/01}
 
 \begin{document}
 \maketitle
 
+This package provides a simple way to typeset grammars written in Backus-Naur form (BNF).
+
 \begin{presentcommand}
   \cmd{bnfexpr} \cmd{bnfannot}
 \end{presentcommand}
-The \cmd{bnfexpr} and the \cmd{bnfannot} commands are simply wrappers around
-\cmd{texttt} and \cmd{textit}, respectively.
+These commands are wrappers around \cmd{texttt} and \cmd{textit} respectively.
 
 \begin{presentcommand}
   \env{bnfgrammar}{text}
 \end{presentcommand}
-The \textit{term} argument of the \texttt{bnfgrammar} environment is the term
-to define a grammar.
-The text inside the environment should be formatted as:
-\begin{equation*}
-  \bnfexpr{term} \Coloneqq \langle\textit{keypairs}\rangle
-\end{equation*}
-where each pair of \textit{keypairs} represents an alternative syntactic form
-of the \textit{term} and its annotation, delimited with a colon(\verb/:/).
-If you don't need annotations, simply omit the colons and annotations
-altogether.
+can be used to typeset BNF grammars. The \textit{text} inside the environment should be formatted as:
+\begin{verbatim}
+  term1 ::= rhs1
+  ;;
+  term2 ::= rhs2
+  ;;
+  ...
+  termk ::= rhsk
+\end{verbatim}
+where each of the \textit{rhs} represents alternative syntactic forms of the \textit{term}. An annotation may accompany each alternative in which case the alternative must be separated from its annotation with a colon (\verb/:/). If you don't need annotations, simply omit the colons and annotations altogether. The alternatives themselves are separated using the pipe symbol (\verb/|/).
 
 A sample code and the result is shown below:
 \begin{exampleside}
-\begin{bnfgrammar}
-  v ::= n | $\lambda$x.e
-\end{bnfgrammar}
-
-\begin{bnfgrammar}
-  C ::=
-    []:      hole
-  | C\,e:    application 1
-  | v\,C:    application 2
-  | C\,+\,e: addition 1
-  | v\,+\,C: addition 2
-\end{bnfgrammar}
+  \begin{bnfgrammar}
+    expr ::=
+      expr + term : sum
+    | term        : term
+    ;;
+    term ::=
+      term * a : product
+    | a        : variable
+  \end{bnfgrammar}
 \end{exampleside}
-\end{document}
+\end{document}
\ No newline at end of file

Modified: trunk/Master/texmf-dist/tex/latex/simplebnf/simplebnf.sty
===================================================================
--- trunk/Master/texmf-dist/tex/latex/simplebnf/simplebnf.sty	2020-10-25 21:34:30 UTC (rev 56760)
+++ trunk/Master/texmf-dist/tex/latex/simplebnf/simplebnf.sty	2020-10-25 21:34:44 UTC (rev 56761)
@@ -13,13 +13,11 @@
 \RequirePackage{mathtools}
 \ProvidesExplPackage
   {simplebnf}
-  {2019/12/23}
-  {0.1.0}
+  {2020/09/01}
+  {0.2.0}
   {A simple package to format Backus–Naur form}
 
-
 \cs_generate_variant:Nn \seq_set_split:Nnn {NVn}
-
 \tl_new:N \g__simplebnf_defeq_tl
 \tl_gset:Nn \g__simplebnf_defeq_tl { \ensuremath{\Coloneqq} }
 
@@ -27,63 +25,92 @@
 \tl_gset:Nn \g__simplebnf_defor_tl { \ensuremath{|} }
 
 \seq_new:N \l__input_seq
+\seq_new:N \l__term_seq
 \tl_new:N \l__term_tl
-\tl_new:N \l__body_tl
 \tl_new:N \l__keypairs_tl
+\tl_new:N \l__table_tl
 \seq_new:N \l__keypairs_seq
+\bool_new:N \l__first_rhs
 
+%% Typeset a single rhs of a production.
+%% \l__first_rhs = true  => `::=' already typeset
+%% \l__first_rhs = false => move to a newline and typeset `|'
+%% #1 - rhs : annot or rhs
+\cs_new:Nn \simplebnf_typeset_rhs:n
+{
+  \bool_if:NTF \l__first_rhs
+    {
+      \bool_set_false:N \l__first_rhs
+    }
+    {
+      \tl_put_right:Nn \l__table_tl { \\ & \g__simplebnf_defor_tl & }
+    }
+
+  \regex_split:nnNTF { : } { #1 } \l_tmpa_seq
+    {
+      \seq_pop_left:NNT \l_tmpa_seq \l_tmpa_tl
+        {
+          \tl_set:Nx \l_tmpa_tl { \bnfexpr { \l_tmpa_tl } }
+          \tl_put_right:NV \l__table_tl \l_tmpa_tl
+        }
+
+      \tl_put_right:Nn \l__table_tl { & }
+
+      \seq_pop_left:NNT \l_tmpa_seq \l_tmpb_tl
+        {
+          \tl_set:Nx \l_tmpb_tl { \bnfannot { \l_tmpb_tl } }
+          \tl_put_right:NV \l__table_tl \l_tmpb_tl
+        }
+    }
+    {
+      \tl_put_right:Nn \l__table_tl { \bnfexpr { #1 } &}
+    }
+}
+
 \NewDocumentCommand \bnfexpr { m } { \texttt { #1 } }
 \NewDocumentCommand \bnfannot { m } { \textit{ #1 } }
 
 \NewDocumentEnvironment { bnfgrammar } { +b }
   {
-    % Store the term to define grammar in \l__term_tl and the
-    % (alternative form: annotion) keypairs in \l__keypairs_tl
-    \regex_split:nnN { ::= } { #1 } \l__input_seq
-    \seq_pop_left:NN \l__input_seq \l__term_tl
-    \seq_pop_left:NN \l__input_seq \l__keypairs_tl
-
-    % Store keypairs
-    \seq_set_split:NnV \l__keypairs_seq { | } \l__keypairs_tl
-
+    %% \l__input_seq is a list of term definitions.
+    \regex_split:nnN { ;; } { #1 } \l__input_seq
     \begin{center}
       \tl_set:Nn \l__table_tl
         {
           \begin{tabular}{lcll}
-            \bnfexpr { \l__term_tl } & \g__simplebnf_defeq_tl &
         }
+    \bool_set_true:N \l_tmp_first_term % Is this the first term in this grammar?
+    \seq_map_inline:Nn \l__input_seq
+      {
+        %% \l__term_seq    - (term, rhses)...
+        %% \l__term_tl     - term
+        %% \l__keypairs_tl - rhses
+        \regex_split:nnN { ::= } { ##1 } \l__term_seq
+        \seq_pop_left:NN \l__term_seq \l__term_tl
+        \seq_pop_left:NN \l__term_seq \l__keypairs_tl
 
-      \bool_set_false:N \l_tmpa_bool
+        \regex_replace_once:nnN { ^\s+ } {} \l__term_tl
 
-      \seq_map_inline:Nn \l__keypairs_seq
-        {
-          \bool_if:NTF \l_tmpa_bool
-            { \tl_put_right:Nn \l__table_tl { \\ & \g__simplebnf_defor_tl & } }
-            { \bool_set_true:N \l_tmpa_bool }
+        \bool_if:NTF \l_tmp_first_term
+          {
+            \bool_set_false:N \l_tmp_first_term
+          }
+          {
+            \tl_put_right:Nn \l__table_tl { \\ }
+          }
+        \tl_put_right:Nx \l__table_tl
+          {
+            \bnfexpr { \l__term_tl } & \g__simplebnf_defeq_tl &
+          }
+        %% \l__keypairs_seq - (rhs:annot | rhs)...
+        \seq_set_split:NnV \l__keypairs_seq { | } \l__keypairs_tl
 
-          \regex_split:nnNTF { : } { ##1 } \l_tmpa_seq
-            {
-              \seq_pop_left:NNT \l_tmpa_seq \l_tmpa_tl
-                {
-                  \tl_set:Nx \l_tmpa_tl { \bnfexpr { \l_tmpa_tl } }
-                  \tl_put_right:NV \l__table_tl \l_tmpa_tl
-                }
+        \bool_set_true:N \l__first_rhs
+        \seq_map_function:NN \l__keypairs_seq \simplebnf_typeset_rhs:n
+      }
 
-              \tl_put_right:Nn \l__table_tl { & }
-
-              \seq_pop_left:NNT \l_tmpa_seq \l_tmpb_tl
-                {
-                  \tl_set:Nx \l_tmpb_tl { \bnfannot { \l_tmpb_tl } }
-                  \tl_put_right:NV \l__table_tl \l_tmpb_tl
-                }
-            }
-            {
-              \tl_put_right:Nn \l__table_tl { \bnfexpr { ##1 } & }
-            }
-        }
-
-      \tl_put_right:Nn \l__table_tl { \end{tabular} }
-      \tl_use:N \l__table_tl
+    \tl_put_right:Nn \l__table_tl { \end{tabular} }
+    \tl_use:N \l__table_tl
     \end{center}
   }
   { }



More information about the tex-live-commits mailing list.