summaryrefslogtreecommitdiff
path: root/macros/latex/contrib/simplebnf/simplebnf.sty
blob: 511a01c7d3968f0d658c045282cc64727c4ae076 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
%%
%% This is file `simplebnf.sty'.
%%
%% ---------------------------------------------------------------------------
%% The simplebnf package --- A simple package to format Backus-Naur form
%% Maintained by Jay Lee
%% E-mail: jaeho.lee@snu.ac.kr
%% Released under the MIT License.
%% ---------------------------------------------------------------------------
%%
\RequirePackage{expl3,xparse}
% mathtools is needed for the \Coloneqq simbol
\RequirePackage{mathtools}
\ProvidesExplPackage
  {simplebnf}
  {2019/12/23}
  {0.1.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} }

\tl_new:N \g__simplebnf_defor_tl
\tl_gset:Nn \g__simplebnf_defor_tl { \ensuremath{|} }

\seq_new:N \l__input_seq
\tl_new:N \l__term_tl
\tl_new:N \l__body_tl
\tl_new:N \l__keypairs_tl
\seq_new:N \l__keypairs_seq

\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

    \begin{center}
      \tl_set:Nn \l__table_tl
        {
          \begin{tabular}{lcll}
            \bnfexpr { \l__term_tl } & \g__simplebnf_defeq_tl &
        }

      \bool_set_false:N \l_tmpa_bool

      \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 }

          \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 } & }
            }
        }

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

%% The MIT License (MIT)
%%
%% Copyright © 2019-2020 Jay Lee <jaeho.lee@snu.ac.kr>
%%
%% 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.
%%
%% End of file `simplebnf.sty'.