Skip to content

Commit 216634a

Browse files
committed
[std, macros] Use "environ" for grammar extraction.
This replaces the use of the "exract" 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 6b2cfb3 commit 216634a

File tree

3 files changed

+32
-20
lines changed

3 files changed

+32
-20
lines changed

source/grammar.tex

Lines changed: 1 addition & 1 deletion
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

Lines changed: 23 additions & 18 deletions
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,7 @@
701699
\nonfrenchspacing
702700
}
703701
704-
\newenvironment{simplebnf}
702+
\newenvironment{ncsimplebnf}
705703
{
706704
\begin{bnfbase}
707705
\BnfNontermshape
@@ -712,7 +710,7 @@
712710
\end{bnfbase}
713711
}
714712
715-
\newenvironment{bnf}
713+
\newenvironment{ncbnf}
716714
{
717715
\begin{bnfbase}
718716
\begin{bnflist}
@@ -724,6 +722,19 @@
724722
\end{bnfbase}
725723
}
726724
725+
% "nc" are the non-copied versions of bnf environments; the non-"nc"
726+
% versions are copied to the grammar extraction file.
727+
\NewEnviron{bnf}{\begin{ncbnf}%
728+
\BODY%
729+
\gramWrite{\string\begin{ncbnf}\meaningbody\BODY\string\end{ncbnf}}%
730+
\end{ncbnf}}{}
731+
732+
\NewEnviron{simplebnf}{\begin{ncsimplebnf}%
733+
\BODY%
734+
\gramwrite{\string\begin{ncsimplebnf}\meaningbody\BODY\string\end{ncsimplebnf}}%
735+
\end{ncsimplebnf}}{}
736+
737+
% The regex grammar is never copied.
727738
\newenvironment{ncrebnf}
728739
{
729740
\begin{bnfbase}
@@ -737,12 +748,6 @@
737748
\end{bnfbase}
738749
}
739750
740-
% non-copied versions of bnf environments
741-
\let\ncsimplebnf\simplebnf
742-
\let\endncsimplebnf\endsimplebnf
743-
\let\ncbnf\bnf
744-
\let\endncbnf\endbnf
745-
746751
%%--------------------------------------------------
747752
%% Environment for imported graphics
748753
%%--------------------------------------------------

source/std.tex

Lines changed: 8 additions & 1 deletion
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)