lib/scripts/fixdots.pl
author wenzelm
Tue, 13 Feb 2001 16:31:18 +0100
changeset 11109 ce1cefc6c14c
parent 9789 7e5e6c47c0b5
child 14981 e73f8140af78
permissions -rw-r--r--
tuned;
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
3824
9fdde15e3215 ensure that dots in formulas are followed by non-idents;
wenzelm
parents:
diff changeset
     1
#
9fdde15e3215 ensure that dots in formulas are followed by non-idents;
wenzelm
parents:
diff changeset
     2
# $Id$
9789
wenzelm
parents: 4076
diff changeset
     3
# Author: Markus Wenzel, TU Muenchen
wenzelm
parents: 4076
diff changeset
     4
# License: GPL (GNU GENERAL PUBLIC LICENSE)
3824
9fdde15e3215 ensure that dots in formulas are followed by non-idents;
wenzelm
parents:
diff changeset
     5
#
9fdde15e3215 ensure that dots in formulas are followed by non-idents;
wenzelm
parents:
diff changeset
     6
# fixdots.pl - ensure that dots in formulas are followed by non-idents
9fdde15e3215 ensure that dots in formulas are followed by non-idents;
wenzelm
parents:
diff changeset
     7
#
3826
wenzelm
parents: 3824
diff changeset
     8
# tries to be smart; may produce garbage if nested comments and
wenzelm
parents: 3824
diff changeset
     9
# solitary quotes (") are intermixed badly;
wenzelm
parents: 3824
diff changeset
    10
#
wenzelm
parents: 3824
diff changeset
    11
wenzelm
parents: 3824
diff changeset
    12
sub fixdots {
wenzelm
parents: 3824
diff changeset
    13
    my ($file) = @_;
wenzelm
parents: 3824
diff changeset
    14
wenzelm
parents: 3824
diff changeset
    15
    open (FILE, $file) || die $!;
wenzelm
parents: 3824
diff changeset
    16
    undef $/; $text = <FILE>; $/ = "\n";         # slurp whole file
wenzelm
parents: 3824
diff changeset
    17
    close FILE || die $!;
wenzelm
parents: 3824
diff changeset
    18
wenzelm
parents: 3824
diff changeset
    19
wenzelm
parents: 3824
diff changeset
    20
    $result = "";
wenzelm
parents: 3824
diff changeset
    21
    $rest = $text;
wenzelm
parents: 3824
diff changeset
    22
wenzelm
parents: 3824
diff changeset
    23
    while (1) {
wenzelm
parents: 3824
diff changeset
    24
	$_ = $rest;
wenzelm
parents: 3824
diff changeset
    25
	($pre, $str, $rest) =
3838
wenzelm
parents: 3826
diff changeset
    26
	    m/^((?: \(\*.*?\*\) | [^"] )*)      # pre: text or (non-nested!) comments
3826
wenzelm
parents: 3824
diff changeset
    27
            "((?: [^"\\] | \\\S | \\\s+\\ )*)"  # quoted string
3838
wenzelm
parents: 3826
diff changeset
    28
            (.*)$/sx;                           # rest of file
3826
wenzelm
parents: 3824
diff changeset
    29
	if ($str) {
wenzelm
parents: 3824
diff changeset
    30
	    $_ = $str;
wenzelm
parents: 3824
diff changeset
    31
	    if (!m/^[a-zA-Z0-9_\/\.]*$/s && !m/\.ML/s && !m/\.thy/s) {   # exclude filenames!
wenzelm
parents: 3824
diff changeset
    32
		s/([a-zA-Z][a-zA-Z0-9_']*)\.([a-zA-Z])/$1\. $2/sg;
wenzelm
parents: 3824
diff changeset
    33
	    }
wenzelm
parents: 3824
diff changeset
    34
            $result = $result . $pre . '"' . $_ . '"';
wenzelm
parents: 3824
diff changeset
    35
        } else {
wenzelm
parents: 3824
diff changeset
    36
 	    $result = $result . $_;
wenzelm
parents: 3824
diff changeset
    37
    	    last;
wenzelm
parents: 3824
diff changeset
    38
	}
wenzelm
parents: 3824
diff changeset
    39
    }
wenzelm
parents: 3824
diff changeset
    40
wenzelm
parents: 3824
diff changeset
    41
    if ($text ne $result) {
wenzelm
parents: 3824
diff changeset
    42
	print STDERR "fixing $file\n";
4076
8315021bf7d6 do not overwrite backup files;
wenzelm
parents: 3838
diff changeset
    43
        if (! -f "$file~~") {
8315021bf7d6 do not overwrite backup files;
wenzelm
parents: 3838
diff changeset
    44
	    rename $file, "$file~~" || die $!;
8315021bf7d6 do not overwrite backup files;
wenzelm
parents: 3838
diff changeset
    45
        }
3826
wenzelm
parents: 3824
diff changeset
    46
	open (FILE, "> $file") || die $!;
wenzelm
parents: 3824
diff changeset
    47
	print FILE $result;
wenzelm
parents: 3824
diff changeset
    48
	close FILE || die $!;
wenzelm
parents: 3824
diff changeset
    49
    }
wenzelm
parents: 3824
diff changeset
    50
}
wenzelm
parents: 3824
diff changeset
    51
wenzelm
parents: 3824
diff changeset
    52
wenzelm
parents: 3824
diff changeset
    53
## main
3824
9fdde15e3215 ensure that dots in formulas are followed by non-idents;
wenzelm
parents:
diff changeset
    54
9fdde15e3215 ensure that dots in formulas are followed by non-idents;
wenzelm
parents:
diff changeset
    55
foreach $file (@ARGV) {
3826
wenzelm
parents: 3824
diff changeset
    56
  eval { &fixdots($file); };
wenzelm
parents: 3824
diff changeset
    57
  if ($@) { print STDERR "*** fixdots $file: ", $@, "\n"; }
3824
9fdde15e3215 ensure that dots in formulas are followed by non-idents;
wenzelm
parents:
diff changeset
    58
}