Debian Codegraph @

texmf-dist/doc/pdftex/manual/syntaxform.pl

raw
1 #!/usr/bin/env perl
2 # $Id: syntaxform.pl 914 2024-01-07 21:48:39Z karl $
3 # Public domain. Originally written by Karl Berry, 2016.
4 # Read pdftex.tex and generate pdftex-syntax.tex
5 # by looking for the primitive descriptions.
6
7 use strict; use warnings;
8
9 # classes need to be shown in a specific order,
10 # and have specific text for the headings.
11 # sorry for using an array. global for simpler sorting.
12 my %class_info = (
13 "integer" => [1, "Integer registers"],
14 "read-only integer" => [2, "Read-only integers"],
15 "dimen" => [3, "Dimen registers"],
16 "tokens" => [4, "Token registers"],
17 "expandable" => [5, "Expandable commands"],
18 "general" => [6, "General commands"],
19 );
20
21 exit (&main ());
22
23 sub main {
24 my %primitive = &read_manual_for_primitives ();
25
26 # group primitives into classes based on their \Whatever construct.
27 my %class;
28 #warn "\n\f\n sorting\n";
29 for my $p (sort keys %primitive) {
30 #print STDERR "sorting primitive $p...";
31 my ($class) = $primitive{$p} =~ /\\Whatever *\{(.*?)\}/;
32 # general commands don't have a \Whatever.
33 $class = "general" if ! $class;
34 #warn "class $class\n";
35
36 # append this primitive, comma-separated.
37 $class{$class} .= "$p,"
38 }
39
40 print <<END_HEADER;
41 % This is the list of new or extended primitives provided by pdftex.
42 % Don't edit this file, as it is auto-generated from the
43 % pdfTeX manual source pdftex.tex by the script syntaxform.pl.
44 % Syntax rule conventions borrowed from `TeXbook naruby' by Petr Olsak.
45 END_HEADER
46 &print_by_class (\%class, \%primitive);
47 return 0;
48 }
49
50 {
51
52 sub print_by_class {
53 my ($class_ref,$primitive_ref) = @_;
54 my %class = %$class_ref;
55
56 #warn "\n\f\n printing by class\n";
57 for my $c (sort by_class keys %class) {
58 #warn "\f printing class $c\n";
59 my $aref = $class_info{$c};
60 my $heading_name = $class_info{$c}->[1];
61 #warn " heading_name $heading_name\n";
62 print <<END_START_CLASS;
63 \\section{$heading_name}
64
65 END_START_CLASS
66
67 # extract list of primitives for this class.
68 my @prims = split (/,/, $class{$c});
69 for my $p (@prims) {
70 my $val = $primitive_ref->{$p};
71 #warn " primitive $p, val $val\n";
72
73 # get rid of the \pdftexprimitive{ and trailing }
74 $val =~ s,^\\pdftexprimitive *\{,,;
75 $val =~ s, *\} *$,,;
76
77 # leading \Syntax and trailing } on their own lines for syntaxform.awk.
78 $val =~ s,^ *\\Syntax *\{,\\Syntax\{\n,;
79 $val =~ s, *\} *$,\n\},;
80
81 # collapse multiple spaces
82 $val =~ s, +, ,g;
83
84 print "\n$val\n";
85 }
86 }
87 }
88
89 sub by_class {
90 if (! defined ($class_info{$a}->[0])) {
91 warn "undef $a $class_info{$a},", $class_info{$a}->[0], ".\n"; }
92 # if (! $class_info{$b}->[0]) { warn "no class info for $b\n"; }
93 $class_info{$a}->[0] <=> $class_info{$b}->[0]; }
94
95 } # end block for %class_info
96
97
98 # read <> for \pdftexprimitive blocks. Return hash with keys being the
99 # primitive name (including the leading \) and values the entire block,
100 # without newlines.
101 #
102 sub read_manual_for_primitives {
103 my $printing = 0;
104 my $primitive = "";
105 my %primitive;
106
107 while (<>) {
108 # \pdftexprimitive block ends at next unindented blank or \... line.
109 $printing = 0 if /^($|\\)/;
110 if (/^\\pdftexprimitive/) {
111 $printing = 1;
112
113 my $type;
114 ($primitive) = m/\\cs *\{(.*?)\}/;
115 #warn "$.: found primitive $primitive\n";
116 warn "$ARGV:$.: no primitive found in: $_" if (! $primitive);
117 $primitive = "\\$primitive";
118
119 # Just one case, \special, has multiple instances of \pdftexprimitive.
120 # kludge by appending spaces to the name to make it unique;
121 # we later reduce multiple spaces to one, so it's not visible.
122 $primitive .= " " until ! exists $primitive{$primitive};
123 }
124
125 if ($printing) {
126 # concatenate lines of definition; assume spacing is reasonable.
127 chomp;
128 $primitive{$primitive} .= $_;
129 #warn " concatenated $primitive to $primitive{$primitive}\n";
130 }
131 }
132
133 return %primitive;
134 }