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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
|
%%
%% 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}
{2023/01/07}
{0.3.2}
{A simple package to format Backus–Naur form}
\cs_generate_variant:Nn \regex_split:nnNTF {nVNTF}
\cs_generate_variant:Nn \regex_split:NnN {NVN}
\NewDocumentCommand\SimpleBNFDefEq{}{\ensuremath{\Coloneqq}}
\NewDocumentCommand\SimpleBNFDefOr{}{\ensuremath{|}}
\NewDocumentCommand\SimpleBNFStretch{}{0em}
\seq_new:N \l__input_seq
\seq_new:N \l__term_seq
\tl_new:N \l__term_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
\regex_new:N \g_simplebnf_rhs_newline_r
\regex_new:N \g_simplebnf_rhs_nb_r
%% 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 { \\ && \SimpleBNFDefOr & }
}
\tl_set:Nn \l_tmpa_tl { #1 }
\regex_replace_once:nnN { ^\s+ } {} \l_tmpa_tl
\regex_replace_once:nnN { \s+$ } {} \l_tmpa_tl
\regex_split:nVNTF { : } \l_tmpa_tl \l_tmpa_seq
{
\seq_pop_left:NNT \l_tmpa_seq \l_tmpa_tl
{
\regex_replace_all:NnN \g_simplebnf_rhs_nb_r { \c{SimpleBNFDefOr} } \l_tmpa_tl
% Expand only the local temporary variable.
\tl_put_right:No \l__table_tl
{
\exp_after:wN\bnfexpr\exp_after:wN{\l_tmpa_tl} &
}
}
\seq_pop_left:NNT \l_tmpa_seq \l_tmpb_tl
{
\regex_replace_once:nnN { ^\s+ } {} \l_tmpb_tl
\tl_put_right:No \l__table_tl
{
\exp_after:wN\bnfannot\exp_after:wN{\l_tmpb_tl}
}
}
}
{
\regex_replace_all:NnN \g_simplebnf_rhs_nb_r { \c{SimpleBNFDefOr} } \l_tmpa_tl
\tl_put_right:No \l__table_tl
{
\exp_after:wN\bnfexpr\exp_after:wN{\l_tmpa_tl}
}
}
}
%% Typeset a single lhs of a production.
%% #1 - lhs : either term or (term : annotation)
\cs_new:Nn \simplebnf_typeset_lhs:n
{
\tl_set:Nx \l_tmpa_tl { #1 }
\regex_replace_once:nnN { ^\s+ } {} \l_tmpa_tl
\regex_replace_once:nnN { \s+$ } {} \l_tmpa_tl
\regex_split:nVNTF { : } \l_tmpa_tl \l_tmpa_seq
{
\seq_pop_right:NN \l_tmpa_seq \l_tmpa_tl
\regex_replace_once:nnN { ^\s+ } {} \l_tmpa_tl
\tl_put_right:No \l__table_tl
{
\exp_after:wN\bnfannot\exp_after:wN{\l_tmpa_tl} &
}
\seq_pop_left:NN \l_tmpa_seq \l_tmpa_tl
\tl_put_right:No \l__table_tl
{
\exp_after:wN\bnfexpr\exp_after:wN{\l_tmpa_tl}
}
}
{
\tl_put_right:No \l__table_tl
{
\exp_after:wN&\exp_after:wN\bnfexpr\exp_after:wN{\l_tmpa_tl}
}
}
}
\NewDocumentCommand \bnfexpr { m } { \texttt { #1 } }
\NewDocumentCommand \bnfannot { m } { \textit{ #1 } }
%% Typeset a BNF grammar.
%% #1 - tabular specification (llcll)
%% #2 - regexp for newline separator for rhses
%% #2 - regexp for non-breaking separator for rhses
%% #3 - grammar
\NewDocumentEnvironment { 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}
\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 { \\[\SimpleBNFStretch] }
}
\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{center}
}
{ }
%% The MIT License (MIT)
%%
%% Copyright © 2019-2023 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'.
|