chiark / gitweb /
1bcd47318b03a220081b06d8b3406b4f266e6207
[subdirmk.git] / tests / filter / extract-doctest
1 #!/usr/bin/perl -w
2 #
3 # script for extracting doctests from README
4 #
5 # usage:
6 #   expand <README | tests/filter/extract-doctest tests/filter/
7 # writes:
8 #   tests/filter/doctest.mk.part
9 #   tests/filter/sub/dir/doctest.mk.part
10 #
11 # Relies on some properties of the way README is laid out.
12 # See comments below marked `parse:' and `adhoc:'.
13
14 use strict;
15 use Carp;
16 use Data::Dumper;
17
18 our @exp;
19 # $exp[]{In}
20 # $exp[]{Out}
21 # $exp[]{OutTop}
22
23 my $cent;
24 my $in_changequote;
25 my $lastl;
26 my $csection;
27 my $withspcs = qr{\S+(?: \S+)*};
28
29 my $outdir = shift @ARGV // confess;
30
31 while (<>) {
32     # adhoc: rely on structure of indented examples in &:changequote part
33     $in_changequote = (m{^\&\:changequote}...m{^\S}) && m{^\s};
34     if (m{^-----|^- - - - -}) {
35         # parse: rely on underlines for (sub)section headings
36         $csection = $lastl;
37         next;
38     }
39     $lastl = $_;
40     my $e = { L => $. };
41     # parse: rely on looking for => (and .. on subsequent lines)
42     next unless m{\=\>} or ($cent and m{ \.\. });
43     my $mapop = '=>';
44     # adhoc: special case NEWQUOTE here so we recognise things in changequote
45     if (s{^(\s*)(\&\S+|NEWQUOTE\S+|\$)\s+(\=\>|\.\.)\s+(\S+)\s+}{} ||
46         s{^()(\&\:\w+(?: \S+)*)\s{2,}(\=\>)\s{2,}($withspcs)$}{} ||
47         $cent && s{^()($withspcs)\s{2,}(\.\.)\s{2,}($withspcs)$}{}) {
48         # adhoc: expected indented iff in changequote part
49         confess if length($1) xor $in_changequote;
50         $mapop = $3;
51         confess if !$cent && $mapop ne '=>';
52         $e->{In} = $2;
53         $e->{Out} = $4;
54         if (# adhoc: `or ...' introduces the `at toplevel' expansion
55             s{^or (\S+)$}{}) {
56             $e->{OutTop} = $1 eq 'nothing' ? '' : $1;
57         } elsif (# parse: expect other wordish things to be comments
58                  m{^(?!or\b)\w{2,} }) {
59         }
60         $e->{CQ} = $in_changequote;
61     } else {
62         confess "$_ ?";
63     }
64     if ($mapop eq '=>') {
65         if ($e->{In} =~ m/\bNN\b/) {
66             # adhoc: special case NN in examples
67             confess if defined $cent->{OutTop};
68             foreach my $nn (0..11, 999) {
69                 my $f = { %$e };
70                 foreach my $k (qw(In Out)) {
71                     $f->{$k} = $e->{$k};
72                     ($f->{$k} =~ s/\bNN\b/$nn/g) == 1 or confess;
73                 }
74                 push @exp, $f;
75             }
76             $cent=undef;
77         } else {
78             push @exp, $e;
79             $cent=$e;
80         }
81     } elsif ($mapop eq '..') {
82         confess if defined $cent->{OutTop};
83         foreach my $k (qw(In Out)) {
84             $cent->{$k} .= "\n".$e->{$k};
85         }
86     }
87 }
88
89 print Dumper(\@exp);
90
91 sub oi { print I @_ or die $!; }
92 sub oo { print O @_ or die $!; }
93 sub oh { oi @_; oo @_; }
94
95 sub write_permode ($$$$$;$$) {
96     my ($dir_prefix, $start, $end, $senl, $what,
97         $filter, $omap) = @_;
98     $filter //= sub { 1 };
99     $omap //= sub { $_[0] };
100     oi $start;
101     oh "${senl}# ----- $what starts -----\n";
102     foreach my $e (@exp) {
103         next unless $filter->($e);
104         my $rubric = $e->{In};
105         $rubric =~ s/\&/AMP /g;
106         $rubric =~ s/\$/DOLLAR /g;
107         $rubric =~ s/NEWQUOTE/NEW_QUOTE /g;
108         my $f = $e->{In} =~ m/\n/
109                 ? "\n# %s:\n%s\n\n"
110                 : "%-30s: %s.\n";
111         my $o;
112         $o = $e->{OutTop} if $dir_prefix eq '';
113         $o //= $e->{Out};
114         $o =~ s{/sub/dir}{} if $dir_prefix eq '' && !defined $e->{OutTop};
115         $o = $omap->($o, $e);
116         oi sprintf $f, $rubric, $e->{In};
117         oo sprintf $f, $rubric, $o;
118     }
119     oi $end;
120     oh "${senl}# ----- $what ends -----\n";
121 }
122     
123 sub writeout ($) {
124     my ($dir_prefix) = @_;
125     open I, '>', "$outdir/${dir_prefix}doctest.sd.mk" or die $!;
126     open O, '>', "$outdir/${dir_prefix}doctest.mk.part" or die $!;
127     oh "# doctest starts $dir_prefix\n";
128     write_permode($dir_prefix,
129                   '','','', 'normal',
130                  sub { !$_[0]{CQ} } );
131     write_permode($dir_prefix,
132                   "&:changequote NEWQUOTE\n",
133                   "NEWQUOTE:changequote &\n",
134                   "",
135                   'changequote',
136                   sub { $_[0]{CQ} } );
137     oh "# doctest ends\n";
138     close I or die $!;
139 }
140
141 writeout('');
142 writeout('sub/dir/');