16310
|
1 |
#!/usr/bin/env perl
|
|
2 |
#
|
|
3 |
# $Id$
|
|
4 |
# Author: Florian Haftmann, TUM
|
|
5 |
#
|
|
6 |
# A generic framework script for simple theory file migrations
|
|
7 |
# (under developement)
|
|
8 |
#
|
|
9 |
|
|
10 |
use strict;
|
|
11 |
use File::Find;
|
|
12 |
use File::Basename;
|
|
13 |
use File::Copy;
|
|
14 |
|
|
15 |
# configuration
|
|
16 |
my @suffices = ('\.thy');
|
|
17 |
my $backupext = '.bak';
|
|
18 |
|
|
19 |
# migrator lookup hash
|
|
20 |
my %migrators = (
|
|
21 |
id => sub {
|
|
22 |
my ($file, @content) = @_;
|
|
23 |
},
|
16311
|
24 |
thyheader => sub {
|
16310
|
25 |
my ($file, @content) = @_;
|
16311
|
26 |
$_ = join("", @content);
|
16382
|
27 |
if (m!^theory!cgoms) {
|
|
28 |
my $prelude = $';
|
|
29 |
my $thyheader = "theory";
|
|
30 |
$thyheader .= skip_wscomment();
|
|
31 |
if (m!\G(\S+)!cgoms) {
|
|
32 |
$thyheader .= $1;
|
|
33 |
$thyheader .= skip_wscomment();
|
|
34 |
print "--->\n>>>$thyheader<<<\n<---\n";
|
|
35 |
if (m!\G(?:imports|=)!cgoms) {
|
|
36 |
$thyheader .= "imports";
|
|
37 |
$thyheader .= skip_wscomment() || " ";
|
|
38 |
print "--->\n>>>$thyheader<<<\n<---\n";
|
|
39 |
while () {
|
|
40 |
my $str = read_plainstring();
|
|
41 |
print "--->\n>>>$str<<<\n<---\n";
|
|
42 |
if (! $str) {
|
|
43 |
last;
|
|
44 |
}
|
|
45 |
$thyheader .= $str;
|
|
46 |
$thyheader .= skip_wscomment();
|
|
47 |
}
|
|
48 |
#~ print "--->\n>>>$thyheader<<<\n<---\n";
|
|
49 |
}
|
|
50 |
if (m!\G(?:files|uses)!cgoms) {
|
|
51 |
$thyheader .= "uses";
|
|
52 |
$thyheader .= skip_wscomment();
|
|
53 |
while () {
|
|
54 |
my $str = read_plainstring();
|
|
55 |
if (! $str) {
|
|
56 |
last;
|
|
57 |
}
|
|
58 |
$thyheader .= $str;
|
|
59 |
$thyheader .= skip_wscomment();
|
|
60 |
}
|
|
61 |
}
|
|
62 |
if (m!\G(?:begin|:)!cgoms) {
|
|
63 |
my $postlude = $';
|
|
64 |
$thyheader .= "begin";
|
|
65 |
# do replacement here
|
|
66 |
print "$file:\n$thyheader\n\n";
|
|
67 |
}
|
|
68 |
}
|
16311
|
69 |
}
|
16310
|
70 |
}
|
|
71 |
);
|
|
72 |
|
16311
|
73 |
# utility functions
|
|
74 |
sub skip_wscomment {
|
|
75 |
my $commentlevel = 0;
|
|
76 |
my @skipped = ();
|
|
77 |
while () {
|
|
78 |
if (m!\G\{\*!cgoms) {
|
|
79 |
push(@skipped, $&);
|
|
80 |
$commentlevel++;
|
|
81 |
} elsif ($commentlevel > 0) {
|
|
82 |
if (m!\G\*\}!cgoms) {
|
|
83 |
push(@skipped, $&);
|
|
84 |
$commentlevel--;
|
|
85 |
} elsif (m!\G(?:
|
|
86 |
[^{*]|\*[^{}]|\{[^*]
|
|
87 |
)*!cgomsx) {
|
|
88 |
push(@skipped, $&);
|
|
89 |
} else {
|
|
90 |
die ("probably incorrectly nested comment");
|
|
91 |
}
|
|
92 |
} elsif (m!\G\s+!cgoms) {
|
|
93 |
push(@skipped, $&);
|
|
94 |
} else {
|
|
95 |
return join('', @skipped);
|
|
96 |
}
|
|
97 |
}
|
|
98 |
}
|
|
99 |
|
16382
|
100 |
sub read_plainstring {
|
|
101 |
m!\G("[^"]+"|[^"\s]+)!cgoms or return "";
|
|
102 |
return $&;
|
|
103 |
}
|
|
104 |
|
16310
|
105 |
# process dir tree
|
|
106 |
sub process_tree {
|
|
107 |
my ($root, $migrator, $backupext) = @_;
|
|
108 |
find(sub { process($File::Find::name, $migrator, $backupext) }, $root);
|
|
109 |
}
|
|
110 |
|
|
111 |
# process single file
|
|
112 |
sub process {
|
|
113 |
my ($file, $migrator, $backupext) = @_;
|
|
114 |
my ($basename, $dirname, $ext) = fileparse($file, @suffices);
|
|
115 |
#~ print "$file\n";
|
|
116 |
if ($ext) {
|
|
117 |
open ISTREAM, $file or die("error opening $file");
|
|
118 |
my @content = <ISTREAM>;
|
|
119 |
close ISTREAM;
|
|
120 |
if ($backupext) {
|
|
121 |
copy($file, "$file$backupext");
|
|
122 |
}
|
|
123 |
print "Migrating $file...\n";
|
|
124 |
&$migrator($file, @content);
|
|
125 |
}
|
|
126 |
}
|
|
127 |
|
|
128 |
# first argument: migrator name
|
|
129 |
my $migrator = $migrators{shift @ARGV};
|
|
130 |
$migrator or die ("invalid migrator name");
|
|
131 |
# other arguments: files or trees
|
|
132 |
foreach my $fileloc (@ARGV) {
|
|
133 |
-e $fileloc or die ("no file $fileloc");
|
|
134 |
}
|
|
135 |
foreach my $fileloc (@ARGV) {
|
|
136 |
if (-d $fileloc) {
|
|
137 |
process_tree($fileloc, $migrator, $backupext);
|
|
138 |
} else {
|
|
139 |
process($fileloc, $migrator, $backupext);
|
|
140 |
}
|
|
141 |
}
|