summaryrefslogtreecommitdiff
path: root/re
blob: 934a1375d7b8fc5c278a9e94b7c7e7fd7a456d26 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
#!/usr/bin/perl

# re -- reference-based literate programming system

use strict;
use warnings;
use POSIX::Regex qw/:all/;

my $bytes;
my $count;
my %bytes;           # file => current byte
my %file_locations;  # file => [location, ...]
my %files;           # byte => file
my %handles;         # file => handle
my %lines;           # file => current line
my %locations;       # byte => location (i.e. byte in source file)
my @references;

die "usage: $0 file\n" if @ARGV != 1;
open my $fh, '<', $ARGV[0] or die "could not open $ARGV[0]: $!\n";

# collect references
$bytes = 0;
while (<$fh>) {
	$bytes += length($_);
	push @references, [$bytes, $1] if /^\.\s*Re\s+(.*)/;
}

# find referenced locations
for (@references) {
	my ($bytes, $ref) = @$_;
	my $loc = -1;

	goto invalid if not $ref =~ /^([^:]+):(.*)/;
	my ($file, $ident) = ($1, $2);

	if (not exists $handles{$file}) {
		open my $fh, '<', $file or die "could not open $file: $!\n";
		$handles{$file} = $fh;
		$bytes{$file} = 0;
		$lines{$file} = 0;
	}

	if ($ident =~ /^(\d+)$/) {
		my $line = $1;
		if ($ident <= $lines{$file}) {
			seek $handles{$file}, 0, 0;
			$bytes{$file} = 0;
			$lines{$file} = 0;
		}
		local $_;
		while ($_ = readline $handles{$file}) {
			$lines{$file}++;
			if ($lines{$file} == $line) {
				$loc = $bytes{$file};
				last;
			}
			$bytes{$file} += length($_);
		}
	} elsif ($ident =~ m{^/(.*)/$}) {
		my $rx = new POSIX::Regex($1);
		seek $handles{$file}, 0, 0;
		$bytes{$file} = 0;
		$lines{$file} = 0;
		local $_;
		while ($_ = readline $handles{$file}) {
			$lines{$file}++;
			if ($rx->match($_)) {
				$loc = $bytes{$file};
				last;
			}
			$bytes{$file} += length($_);
		}
	} else {
		goto invalid;
	}

	die "could not find location $ident in $file\n" if $loc == -1;
	$locations{$bytes} = $loc;
	$files{$bytes} = $file;
	if (exists $file_locations{$file}) {
		push @{$file_locations{$file}}, $loc;
	} else {
		$file_locations{$file} = [];
	}

	next;
invalid:
	die "invalid syntax: $ref at $bytes\n";
}

# intertwine
seek $fh, 0, 0;
$bytes = 0;
$count = 0;
while (<$fh>) {
	$bytes += length($_);
	goto normal if not @references;
	my $ref_bytes = $references[0][0];
	if ($bytes == $ref_bytes) {
		shift @references;
		my $file = $files{$bytes};
		my $end = shift @{$file_locations{$file}} || '';
		my $loc = $locations{$bytes};

		print ".Sr $file $loc-$end\n";

		seek $handles{$file}, $loc, 0;
		local $_;
		my $bytes = $loc;
		while ($_ = readline $handles{$file}) {
			$bytes += length($_);
			last if $end and $bytes > $end;
			print;
		}

		print ".Se\n";

		next;
	}
normal:
	print;
}

close $_ for values %handles;
close $fh;