-
Notifications
You must be signed in to change notification settings - Fork 29
/
Copy pathprep
executable file
·82 lines (82 loc) · 1.69 KB
/
prep
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
#!/usr/bin/perl
#
# Prepare ABNF for wdiff.
# outside of quotes:
# add space before and after "[][()]"
# and "<" gets space before, and ">" gets space after
# and [[(] only get space after if they have [*0-9] before them.
# strip comments (after ";")
#
# $Id$
#
while(<>) {
chomp;
@line = split(//);
$inq = $inp = 0;
for ($i = 0; $i <= $#line; $i++) {
if ($line[$i] eq '"') {
$inq = !$inq;
next;
}
next if ($inq);
if ($line[$i] eq '<') {
$inp = 1;
next;
}
if ($inp) {
if ($line[$i] eq '>') {
$inp = 0;
}
next;
}
if ($line[$i] eq ';') {
splice(@line, $i);
last;
}
if ($line[$i] eq '/') {
if ($i > 0 && $line[$i-1] ne ' ') {
splice(@line, $i, 0, " ");
$i++;
}
if ($line[$i+1] ne ' ') {
splice(@line, $i + 1, 0, " ");
$i++;
}
}
if ($line[$i] eq '[' || $line[$i] eq '(' || $line[$i] eq '<') {
if ($i > 0) {
if ($line[$i-1] =~ /[^*0-9 ]/) {
splice(@line, $i, 0, " ");
$i++;
}
}
if ($line[$i] ne '<' && $line[$i+1] ne ' ') {
splice(@line, $i + 1, 0, " ");
$i++;
}
next;
}
if ($line[$i] eq ']' || $line[$i] eq ')' || $line[$i] eq '>') {
if ($line[$i] ne '>' && (($i > 0) && ($line[$i - 1] ne ' '))) {
splice(@line, $i, 0, " ");
$i++;
}
if ($line[$i + 1] ne ' ') {
splice(@line, $i + 1, 0, " ");
$i++;
}
next;
}
# if ($quoted[$i] =~ s/;.*//) {
# splice(@quoted, $i + 1); # XXX $i or $i + 1?
# last;
# }
# $quoted[$i] =~ s/([^*0-9])([[(])/$1 $2 /g;
# $quoted[$i] =~ s/([*0-9])([[(])/$1$2 /g;
# $quoted[$i] =~ s/[])]/ $& /g;
# $quoted[$i] =~ s/([^*0-9])(<)/$1 $2/g;
# $quoted[$i] =~ s/>/$& /g;
# $quoted[$i] =~ s,/, $& ,g;
}
print join("", @line), "\n";
}