# This file is part of GNU Bison
-# Copyright (C) 1992, 2000-2001, 2005-2006, 2009-2012 Free Software
+# Copyright (C) 1992, 2000-2001, 2005-2006, 2009-2013 Free Software
# Foundation, Inc.
#
# This program is free software: you can redistribute it and/or modify
# Usage: extexi input-file.texi ... -- [FILES to extract]
+# Look for @example environments preceded with lines such as:
+#
+# @comment file calc.y
+# or
+# @comment file calc.y: 3
+#
+# and output their content in that file (calc.y). When numbers are
+# provided, use them to decide the output order (block numbered 1 is
+# output before block 2, even if the latter appears before). The same
+# number may be used several time, in which case the order of
+# appearance is used.
+
use strict;
# normalize($block)
# basename => full file name for files we should extract.
my %file_wanted;
-# Whether we already say that file (in which case, append instead of
-# create).
-my %file_output;
sub process ($)
{
use IO::File;
my $f = new IO::File($in)
or die "$in: cannot open: $?";
- # The latest "@comment file: " argument.
+ # FILE-NAME => { BLOCK-NUM => CODE }
+ my %file;
+
+ # The latest "@comment file: FILE [BLOCK-NUM]" arguments.
my $file;
+ my $block;
# The @example block currently read.
my $input;
local $_;
while (<$f>)
{
- if (/^\@comment file: (.*)/)
+ if (/^\@comment file: ([^:\n]+)(?::\s*(\d+))?$/)
{
my $f = $1;
+ $block = $2 || 1;
if ($file_wanted{$f})
{
$file = $file_wanted{$f};
{
if (/^\@(small)?example$/)
{
- $input = $file_output{$file} ? "\n" : "";
# Bison supports synclines, but not Flex.
$input .= sprintf ("#line %s \"$in\"\n", $. + 1)
if $file =~ /\.[chy]*$/;
die "no contents: $file"
if $input eq "";
- $input = normalize($input);
- # No spurious end of line: use printf.
- my $o =
- ($file_output{$file}
- ? new IO::File(">>$file")
- : new IO::File(">$file"));
- print $o $input;
- $file_output{$file} = 1;
+ $file{$file}{$block} .= normalize($input);
$file = $input = undef;
+ ++$block;
}
else
{
$input .= $_;
}
}
+ }
+ # Output the files.
+ for my $file (keys %file)
+ {
+ # No spurious end of line: use printf.
+ my $o = new IO::File(">$file")
+ or die "$file: cannot create: $?";
+ print $o $file{$file}{$_}
+ for sort keys %{$file{$file}};
}
}