Skip to content

Commit 561a43b

Browse files
committed
[std, macros] Use "environ" for grammar extraction.
This replaces the use of the "extract" package with the simpler "environ" package, where the grammar contents are extracted from the "collected body" \BODY macro via the TeX `\meaning` command. This does not preserve whitespace as the "extact" approach did, but if we just spell out whitespace (instead of relying on \obeyspaces), we get the same results.
1 parent 1797461 commit 561a43b

File tree

4 files changed

+30
-20
lines changed

4 files changed

+30
-20
lines changed

.github/workflows/check.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ jobs:
5353
eval "$(/usr/libexec/path_helper)"
5454
echo "PATH=${PATH}" >> ${GITHUB_ENV}
5555
sudo tlmgr update --self
56-
sudo tlmgr install latexmk isodate substr relsize ulem fixme rsfs extract layouts enumitem l3packages l3kernel imakeidx splitindex xstring
56+
sudo tlmgr install latexmk isodate substr relsize ulem fixme rsfs environ layouts enumitem l3packages l3kernel imakeidx splitindex xstring
5757
5858
- name: make (Linux)
5959
if: matrix.cfg.os == 'ubuntu-24.04'

source/grammar.tex

+1-1
Original file line numberDiff line numberDiff line change
@@ -58,4 +58,4 @@
5858
identifier
5959
\end{ncbnf}
6060

61-
\FlushAndPrintGrammar
61+
\input{std-gram.ext}

source/macros.tex

+20-17
Original file line numberDiff line numberDiff line change
@@ -39,20 +39,18 @@
3939
\newenvironment{addedblock}{\color{addclr}}{\color{black}}
4040
\newenvironment{removedblock}{\color{remclr}}{\color{black}}
4141

42-
%%--------------------------------------------------
42+
%%-------------------------------------------------------------
4343
%% Grammar extraction.
44-
%%--------------------------------------------------
45-
\def\gramSec[#1]#2{}
46-
44+
%% Assumes that the output file \gramout is managed externally.
45+
%%-------------------------------------------------------------
4746
\makeatletter
48-
\newcommand{\FlushAndPrintGrammar}{%
49-
\immediate\closeout\XTR@out%
50-
\immediate\openout\XTR@out=std-gram-dummy.tmp%
51-
\def\gramSec[##1]##2{\rSec1[##1]{##2}}%
52-
\input{std-gram.ext}%
53-
}
47+
\newcommand{\gramWrite}[1]{\protected@write\gramout{}{#1}}
48+
\newcommand{\meaningbody}[1]{\expandafter\strip@prefix\meaning#1}
5449
\makeatother
5550

51+
\newcommand{\gramSec}[2][]{\gramWrite{%
52+
\string\rSec1\string[\string#1\string]\string{\string#2\string}}}
53+
5654
%%--------------------------------------------------
5755
% Escaping for index entries. Replaces ! with "! throughout its argument.
5856
%%--------------------------------------------------
@@ -701,7 +699,12 @@
701699
\nonfrenchspacing
702700
}
703701
704-
\newenvironment{simplebnf}
702+
% "ncbnf" is the non-copied "base" versions of the bnf environment;
703+
% instances of the full "bnf" environment is copied to the grammar
704+
% extraction file.
705+
% (Similarly for "ncsimplebnf", though in fact we never extract any
706+
% hypothetical "simplebnf" environments.)
707+
\newenvironment{ncsimplebnf}
705708
{
706709
\begin{bnfbase}
707710
\BnfNontermshape
@@ -712,7 +715,7 @@
712715
\end{bnfbase}
713716
}
714717
715-
\newenvironment{bnf}
718+
\newenvironment{ncbnf}
716719
{
717720
\begin{bnfbase}
718721
\begin{bnflist}
@@ -724,6 +727,7 @@
724727
\end{bnfbase}
725728
}
726729
730+
% The regex grammar is never copied.
727731
\newenvironment{ncrebnf}
728732
{
729733
\begin{bnfbase}
@@ -737,11 +741,10 @@
737741
\end{bnfbase}
738742
}
739743
740-
% non-copied versions of bnf environments
741-
\let\ncsimplebnf\simplebnf
742-
\let\endncsimplebnf\endsimplebnf
743-
\let\ncbnf\bnf
744-
\let\endncbnf\endbnf
744+
\NewEnviron{bnf}{\begin{ncbnf}%
745+
\BODY%
746+
\gramWrite{\string\begin{ncbnf}\meaningbody\BODY\string\end{ncbnf}}%
747+
\end{ncbnf}}{}
745748
746749
%%--------------------------------------------------
747750
%% Environment for imported graphics

source/std.tex

+8-1
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@
4646
urlcolor=blue, % ISO/IEC Directives, Part 2, section 6.5
4747
plainpages=false}
4848
\usepackage{memhfixc} % fix interactions between hyperref and memoir
49-
\usepackage[active,header=false,handles=false,copydocumentclass=false,generate=std-gram.ext,extract-cmdline={gramSec},extract-env={bnf,simplebnf}]{extract} % Grammar extraction
49+
\usepackage{environ}
5050
\usepackage{expl3}
5151
\usepackage{xparse}
5252
\usepackage{xstring}
@@ -57,6 +57,10 @@
5757

5858
\renewcommand\RSsmallest{5.5pt} % smallest font size for relsize
5959

60+
% Begin grammar extraction...
61+
\newwrite\gramout
62+
\immediate\openout\gramout=std-gram.ext
63+
6064
\input{layout}
6165
\input{styles}
6266
\input{macros}
@@ -154,6 +158,9 @@
154158

155159
\numberwithin{table}{chapter}
156160

161+
% ... end grammar extraction.
162+
\immediate\closeout\gramout
163+
157164
\include{grammar}
158165
\include{limits}
159166
\include{compatibility}

0 commit comments

Comments
 (0)