3 # script for extracting doctests from README
6 # expand <README | tests/filter/extract-doctest tests/filter/
8 # tests/filter/doctest.mk.part
9 # tests/filter/sub/dir/doctest.mk.part
11 # Relies on some properties of the way README is laid out.
12 # See comments below marked `parse:' and `adhoc:'.
27 my $withspcs = qr{\S+(?: \S+)*};
29 my $outdir = shift @ARGV // confess;
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
41 # parse: rely on looking for => (and .. on subsequent lines)
42 next unless m{\=\>} or ($cent and m{ \.\. });
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;
51 confess if !$cent && $mapop ne '=>';
54 if (# adhoc: `or ...' introduces the `at toplevel' expansion
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 (# adhoc: slightly special case for $(eval $(call
64 confess "unk rhs $_ ?";
66 $e->{CQ} = $in_changequote;
67 # adhoc: rely on this specific section title
68 $e->{DD} = $csection =~ m{^while dollar[- ]doubling}i;
73 if ($e->{In} =~ m/\bNN\b/) {
74 # adhoc: special case NN in examples
75 confess if defined $cent->{OutTop};
76 foreach my $nn (0..11, 999) {
78 foreach my $k (qw(In Out)) {
80 ($f->{$k} =~ s/\bNN\b/$nn/g) == 1 or confess;
89 } elsif ($mapop eq '..') {
90 confess if defined $cent->{OutTop};
91 foreach my $k (qw(In Out)) {
92 $cent->{$k} .= "\n".$e->{$k};
99 sub oi { print I @_ or die $!; }
100 sub oo { print O @_ or die $!; }
101 sub oh { oi @_; oo @_; }
103 sub write_permode ($$$$$;$$) {
104 my ($dir_prefix, $start, $end, $senl, $what,
105 $filter, $omap) = @_;
106 $filter //= sub { 1 };
107 $omap //= sub { $_[0] };
109 oh "${senl}# ----- $what starts -----\n";
110 foreach my $e (@exp) {
111 next unless $filter->($e);
113 $desc =~ s/\&/AMP /g;
114 $desc =~ s/\$/DOLLAR /g;
115 $desc =~ s/NEWQUOTE/NEW_QUOTE /g;
116 my ($f,$pdesc) = $desc =~ m/^(.*)\n/
117 ? ("\n# %s:\n%s\n\n", $1)
118 : ("%-30s: %s .\n", $desc);
120 $o = $e->{OutTop} if $dir_prefix eq '';
122 $o =~ s{/sub/dir}{} if $dir_prefix eq '' && !defined $e->{OutTop};
123 $o = $omap->($o, $e);
124 oi sprintf $f, $pdesc, $e->{In};
125 oo sprintf $f, $pdesc, $o;
128 oh "${senl}# ----- $what ends -----\n";
132 my ($dir_prefix) = @_;
133 open I, '>', "$outdir/${dir_prefix}doctest.sd.mk" or die $!;
134 open O, '>', "$outdir/${dir_prefix}doctest.mk.part" or die $!;
135 oh "# doctest starts $dir_prefix\n";
136 write_permode($dir_prefix,
138 sub { !$_[0]{DD} && !$_[0]{CQ} } );
139 write_permode($dir_prefix,
144 # adhoc: skip &:macro in already-doubling part
145 return 0 if $e->{In} =~ m{^\&\:macro};
146 # adhoc: skip &${ ie eval in already-doubling part
147 return 0 if $e->{In} =~ m{^\&\$\{};
148 return 0 if $e->{CQ};
149 return $e->{DD} || !grep {
150 # If there are two entries with the same In,
151 # use only the one from the `while dollar
152 # doubling' section. So entries there override
153 # entries in the rest o the file.
154 $_ ne $e && $_->{In} eq $e->{In}
159 s/\$/\$\$/g unless $_[1]{DD};
162 write_permode($dir_prefix,
163 "&:changequote NEWQUOTE\n",
164 "NEWQUOTE:changequote &\n",
168 oh "# doctest ends\n";
173 writeout('sub/dir/');