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) = @_;
|
16408
|
26 |
my $diag = 1;
|
|
27 |
#~ my $diag = 0;
|
16311
|
28 |
$_ = join("", @content);
|
16382
|
29 |
if (m!^theory!cgoms) {
|
16407
|
30 |
my $prelude = $`;
|
16382
|
31 |
my $thyheader = "theory";
|
|
32 |
$thyheader .= skip_wscomment();
|
|
33 |
if (m!\G(\S+)!cgoms) {
|
|
34 |
$thyheader .= $1;
|
|
35 |
$thyheader .= skip_wscomment();
|
16404
|
36 |
$diag and print "--->\n(1)>>>$thyheader<<<\n<---\n";
|
16382
|
37 |
if (m!\G(?:imports|=)!cgoms) {
|
|
38 |
$thyheader .= "imports";
|
|
39 |
$thyheader .= skip_wscomment() || " ";
|
16404
|
40 |
$diag and print "--->\n(2)>>>$thyheader<<<\n<---\n";
|
16411
|
41 |
while (m/\G(?!uses|files|begin|:)/cgoms && m!\G(?:[\w_]+|"[^"]+")!cgoms) {
|
16404
|
42 |
$diag and print "--->\n(3)>>>$&<<<\n<---\n";
|
|
43 |
$thyheader .= $&;
|
|
44 |
$thyheader .= skip_wscomment();
|
16411
|
45 |
if (m!\G\+!cgoms) {
|
|
46 |
m!\G ?!cgoms;
|
|
47 |
}
|
16382
|
48 |
$thyheader .= skip_wscomment();
|
16411
|
49 |
#~ if (m/\G(?!uses|files|begin|:)/cgoms) { print '!!!'; }
|
|
50 |
#~ if (m!\G[\w_]+!cgoms) { print '§§§'; }
|
16382
|
51 |
}
|
16404
|
52 |
$diag and print "--->\n(4)>>>$thyheader<<<\n<---\n";
|
16382
|
53 |
}
|
16411
|
54 |
#~ m!\G.{19}!cgoms;
|
|
55 |
#~ print "***$&\n";
|
|
56 |
#~ die;
|
16382
|
57 |
if (m!\G(?:files|uses)!cgoms) {
|
|
58 |
$thyheader .= "uses";
|
|
59 |
$thyheader .= skip_wscomment();
|
16404
|
60 |
$diag and print "--->\n(5)>>>$thyheader<<<\n<---\n";
|
|
61 |
while (m/\G(?!begin|:)/cgoms && m!\G(\("[^"]+"\)|"[^"]+"|[^"\s]+)!cgoms) {
|
|
62 |
$diag and print "--->\n(6)>>>$&<<<\n<---\n";
|
|
63 |
$thyheader .= $&;
|
16382
|
64 |
$thyheader .= skip_wscomment();
|
|
65 |
}
|
16404
|
66 |
$diag and print "--->\n(7)>>>$thyheader<<<\n<---\n";
|
16382
|
67 |
}
|
16411
|
68 |
|
16404
|
69 |
|
16382
|
70 |
if (m!\G(?:begin|:)!cgoms) {
|
|
71 |
my $postlude = $';
|
16408
|
72 |
if ($& eq ":") {
|
16404
|
73 |
$thyheader .= " ";
|
|
74 |
}
|
16382
|
75 |
$thyheader .= "begin";
|
|
76 |
# do replacement here
|
16406
|
77 |
if ($diag) {
|
|
78 |
print "$file:\n$thyheader\n\n";
|
|
79 |
} else {
|
|
80 |
open(OSTREAM, ">$file") or die("error opening $file");
|
|
81 |
print OSTREAM "$prelude$thyheader$postlude";
|
|
82 |
close(OSTREAM);
|
|
83 |
}
|
16382
|
84 |
}
|
|
85 |
}
|
16311
|
86 |
}
|
16310
|
87 |
}
|
|
88 |
);
|
|
89 |
|
16311
|
90 |
# utility functions
|
|
91 |
sub skip_wscomment {
|
|
92 |
my $commentlevel = 0;
|
|
93 |
my @skipped = ();
|
|
94 |
while () {
|
16411
|
95 |
if (m!\G\(\*!cgoms) {
|
16311
|
96 |
push(@skipped, $&);
|
|
97 |
$commentlevel++;
|
|
98 |
} elsif ($commentlevel > 0) {
|
16411
|
99 |
if (m!\G\*\)!cgoms) {
|
16311
|
100 |
push(@skipped, $&);
|
|
101 |
$commentlevel--;
|
16411
|
102 |
} elsif (m/\G(?:
|
|
103 |
\*(?!\))|\((?!\*)|[^(*]
|
|
104 |
)*/cgomsx) {
|
16311
|
105 |
push(@skipped, $&);
|
|
106 |
} else {
|
|
107 |
die ("probably incorrectly nested comment");
|
|
108 |
}
|
|
109 |
} elsif (m!\G\s+!cgoms) {
|
|
110 |
push(@skipped, $&);
|
|
111 |
} else {
|
|
112 |
return join('', @skipped);
|
|
113 |
}
|
|
114 |
}
|
|
115 |
}
|
|
116 |
|
16310
|
117 |
# process dir tree
|
|
118 |
sub process_tree {
|
|
119 |
my ($root, $migrator, $backupext) = @_;
|
16404
|
120 |
find(sub { process($_, $migrator, $backupext) }, $root);
|
16310
|
121 |
}
|
|
122 |
|
|
123 |
# process single file
|
|
124 |
sub process {
|
|
125 |
my ($file, $migrator, $backupext) = @_;
|
|
126 |
my ($basename, $dirname, $ext) = fileparse($file, @suffices);
|
|
127 |
#~ print "$file\n";
|
|
128 |
if ($ext) {
|
16406
|
129 |
open(ISTREAM, $file) or die("error opening $file");
|
16310
|
130 |
my @content = <ISTREAM>;
|
|
131 |
close ISTREAM;
|
|
132 |
if ($backupext) {
|
|
133 |
copy($file, "$file$backupext");
|
|
134 |
}
|
|
135 |
print "Migrating $file...\n";
|
|
136 |
&$migrator($file, @content);
|
|
137 |
}
|
|
138 |
}
|
|
139 |
|
|
140 |
# first argument: migrator name
|
|
141 |
my $migrator = $migrators{shift @ARGV};
|
|
142 |
$migrator or die ("invalid migrator name");
|
|
143 |
# other arguments: files or trees
|
|
144 |
foreach my $fileloc (@ARGV) {
|
|
145 |
-e $fileloc or die ("no file $fileloc");
|
|
146 |
}
|
|
147 |
foreach my $fileloc (@ARGV) {
|
|
148 |
if (-d $fileloc) {
|
|
149 |
process_tree($fileloc, $migrator, $backupext);
|
|
150 |
} else {
|
|
151 |
process($fileloc, $migrator, $backupext);
|
|
152 |
}
|
|
153 |
}
|
16404
|
154 |
|
|
155 |
#!!! example file: |