chiark / gitweb /
doctests: Fail on extraction if we don't understand the README
[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         } elsif (m/^$/) {
60         } else {
61             confess "unk rhs $_ ?";
62         }
63         $e->{CQ} = $in_changequote;
64     } else {
65         confess "$_ ?";
66     }
67     if ($mapop eq '=>') {
68         if ($e->{In} =~ m/\bNN\b/) {
69             # adhoc: special case NN in examples
70             confess if defined $cent->{OutTop};
71             foreach my $nn (0..11, 999) {
72                 my $f = { %$e };
73                 foreach my $k (qw(In Out)) {
74                     $f->{$k} = $e->{$k};
75                     ($f->{$k} =~ s/\bNN\b/$nn/g) == 1 or confess;
76                 }
77                 push @exp, $f;
78             }
79             $cent=undef;
80         } else {
81             push @exp, $e;
82             $cent=$e;
83         }
84     } elsif ($mapop eq '..') {
85         confess if defined $cent->{OutTop};
86         foreach my $k (qw(In Out)) {
87             $cent->{$k} .= "\n".$e->{$k};
88         }
89     }
90 }
91
92 print Dumper(\@exp);
93
94 sub oi { print I @_ or die $!; }
95 sub oo { print O @_ or die $!; }
96 sub oh { oi @_; oo @_; }
97
98 sub write_permode ($$$$$;$$) {
99     my ($dir_prefix, $start, $end, $senl, $what,
100         $filter, $omap) = @_;
101     $filter //= sub { 1 };
102     $omap //= sub { $_[0] };
103     oi $start;
104     oh "${senl}# ----- $what starts -----\n";
105     foreach my $e (@exp) {
106         next unless $filter->($e);
107         my $rubric = $e->{In};
108         $rubric =~ s/\&/AMP /g;
109         $rubric =~ s/\$/DOLLAR /g;
110         $rubric =~ s/NEWQUOTE/NEW_QUOTE /g;
111         my $f = $e->{In} =~ m/\n/
112                 ? "\n# %s:\n%s\n\n"
113                 : "%-30s: %s.\n";
114         my $o;
115         $o = $e->{OutTop} if $dir_prefix eq '';
116         $o //= $e->{Out};
117         $o =~ s{/sub/dir}{} if $dir_prefix eq '' && !defined $e->{OutTop};
118         $o = $omap->($o, $e);
119         oi sprintf $f, $rubric, $e->{In};
120         oo sprintf $f, $rubric, $o;
121     }
122     oi $end;
123     oh "${senl}# ----- $what ends -----\n";
124 }
125     
126 sub writeout ($) {
127     my ($dir_prefix) = @_;
128     open I, '>', "$outdir/${dir_prefix}doctest.sd.mk" or die $!;
129     open O, '>', "$outdir/${dir_prefix}doctest.mk.part" or die $!;
130     oh "# doctest starts $dir_prefix\n";
131     write_permode($dir_prefix,
132                   '','','', 'normal',
133                  sub { !$_[0]{CQ} } );
134     write_permode($dir_prefix,
135                   "&:changequote NEWQUOTE\n",
136                   "NEWQUOTE:changequote &\n",
137                   "",
138                   'changequote',
139                   sub { $_[0]{CQ} } );
140     oh "# doctest ends\n";
141     close I or die $!;
142 }
143
144 writeout('');
145 writeout('sub/dir/');