-
Notifications
You must be signed in to change notification settings - Fork 26
Expand file tree
/
Copy pathperl_throwme_extractor.pl
More file actions
79 lines (72 loc) · 1.92 KB
/
Copy pathperl_throwme_extractor.pl
File metadata and controls
79 lines (72 loc) · 1.92 KB
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
#!/usr/bin/perl
use strict;
use warnings;
main();
# ---
# Read stdin line by line and extract "throwme()" calls.
# Don't bother to detect comments.
# ---
sub main {
# First argument is an integer (or missing)
# Output will be indented by that many spaces on output
my $indent=$ARGV[1];
if (defined $indent) { $indent=int($indent*1) } else { $indent = 0 }
my $test_case_counter = 1;
while (my $line = <>) {
chomp $line;
if ($line =~ /\bthrowme\((.*)$/) {
my $args = extract_args($1);
my $call = "throwme(" . $args;
output($test_case_counter,$indent,$call);
$test_case_counter++;
}
}
}
sub output {
my($test_case_counter,$indent,$call) = @_;
for (my $i=0;$i<$indent;$i++) { print " " }
print
"test(",
sprintf("%2d",$test_case_counter),
") :- exc_test(",
$call,
").\n";
}
sub extract_args {
my($argstr) = @_;
my $depth = 1; # the first opening parenthesis has already been grabbed
my $i = 0;
my $extracted = "";
my $sof = 1; # boolean; if true, expect a "start of term"
while ($i < length($argstr) && $depth > 0) {
my $ch = substr($argstr,$i,1);
if ($ch eq '(') {
$depth++;
$sof = 1;
$extracted .= $ch
}
elsif ($ch eq ')') {
$depth--;
$sof = 1;
$extracted .= $ch
}
elsif ($ch =~ /[A-Za-z0-9_]/) {
if ($sof) {
if ($ch =~ /[A-Z]/ && $ch ne '_') {
# looks like a variable, anonymize it!
$extracted .= "_";
}
$sof = 0
}
$extracted .= $ch
}
else {
# probabyl punctuation characters etc; don't analyze too closely for now
$extracted .= $ch;
$sof = 1
}
$i++
}
die "Non zero depth $depth at end of string $argstr" unless $depth == 0; # probably an error in the Prolog code
return $extracted
}