| 1 |
#!/usr/bin/perl |
| 2 |
# -*- mode: cperl; indent-tabs-mode: nil; tab-width: 3; cperl-indent-level: 3; -*- |
| 3 |
use warnings; |
| 4 |
use strict; |
| 5 |
use utf8; |
| 6 |
BEGIN { |
| 7 |
$| = 1; |
| 8 |
binmode(STDIN, ':encoding(UTF-8)'); |
| 9 |
binmode(STDOUT, ':encoding(UTF-8)'); |
| 10 |
} |
| 11 |
use open qw( :encoding(UTF-8) :std ); |
| 12 |
|
| 13 |
use Getopt::Long; |
| 14 |
Getopt::Long::Configure('no_ignore_case'); |
| 15 |
|
| 16 |
my %opts = (); |
| 17 |
GetOptions (\%opts, |
| 18 |
'help|?', |
| 19 |
'grammar|g=s', |
| 20 |
'output|o', |
| 21 |
'strip', |
| 22 |
'secondary', |
| 23 |
'regex', |
| 24 |
'icase', |
| 25 |
'baseforms', |
| 26 |
'wordforms', |
| 27 |
'all', |
| 28 |
); |
| 29 |
|
| 30 |
sub print_help { |
| 31 |
print <<'XOUT'; |
| 32 |
Usage: cg-strictify [OPTIONS] <grammar> |
| 33 |
|
| 34 |
Options: |
| 35 |
-?, --help outputs this help |
| 36 |
-g, --grammar the grammar to parse; defaults to first non-option argument |
| 37 |
-o, --output outputs the whole grammar with STRICT-TAGS |
| 38 |
--strip removes superfluous LISTs from the output grammar; implies -o |
| 39 |
--secondary adds secondary tags (<...>) to strict list |
| 40 |
--regex adds regular expression tags (/../r, <..>r, etc) to strict list |
| 41 |
--icase adds case-insensitive tags to strict list |
| 42 |
--baseforms adds baseform tags ("...") to strict list |
| 43 |
--wordforms adds wordform tags ("<...>") to strict list |
| 44 |
--all same as --strip --secondary --regex --icase --baseforms --wordforms |
| 45 |
|
| 46 |
XOUT |
| 47 |
} |
| 48 |
|
| 49 |
if (defined $opts{'help'}) { |
| 50 |
print_help(); |
| 51 |
exit(0); |
| 52 |
} |
| 53 |
if (!defined $opts{'grammar'}) { |
| 54 |
if (!$ARGV[0]) { |
| 55 |
print "Missing input grammar argument!\n\n"; |
| 56 |
print_help(); |
| 57 |
exit(-1); |
| 58 |
} |
| 59 |
$opts{'grammar'} = $ARGV[0]; |
| 60 |
} |
| 61 |
if (! -s $opts{'grammar'}) { |
| 62 |
print "Grammar is either missing or empty!\n"; |
| 63 |
exit(-1); |
| 64 |
} |
| 65 |
|
| 66 |
if (defined $opts{'strip'}) { |
| 67 |
$opts{'output'} = 1; |
| 68 |
} |
| 69 |
|
| 70 |
if (defined $opts{'all'}) { |
| 71 |
$opts{'secondary'} = 1; |
| 72 |
$opts{'regex'} = 1; |
| 73 |
$opts{'icase'} = 1; |
| 74 |
$opts{'baseforms'} = 1; |
| 75 |
$opts{'wordforms'} = 1; |
| 76 |
} |
| 77 |
|
| 78 |
my $tags = `vislcg3 --show-tags -g '$opts{grammar}' | LC_ALL=C sort`; |
| 79 |
$tags =~ s@[\r\n]+@\n@g; # Normalize newlines |
| 80 |
chomp($tags); |
| 81 |
my @tags = split /\n/, $tags; |
| 82 |
|
| 83 |
my $options = ''; |
| 84 |
if (defined $opts{'secondary'} || defined $opts{'regex'} || defined $opts{'icase'} || defined $opts{'baseforms'} || defined $opts{'wordforms'}) { |
| 85 |
$options .= 'OPTIONS +='; |
| 86 |
if (defined $opts{'secondary'}) { |
| 87 |
$options .= ' strict-secondary'; |
| 88 |
} |
| 89 |
if (defined $opts{'regex'}) { |
| 90 |
$options .= ' strict-regex'; |
| 91 |
} |
| 92 |
if (defined $opts{'icase'}) { |
| 93 |
$options .= ' strict-icase'; |
| 94 |
} |
| 95 |
if (defined $opts{'baseforms'}) { |
| 96 |
$options .= ' strict-baseforms'; |
| 97 |
} |
| 98 |
if (defined $opts{'wordforms'}) { |
| 99 |
$options .= ' strict-wordforms'; |
| 100 |
} |
| 101 |
$options .= " ;\n"; |
| 102 |
} |
| 103 |
|
| 104 |
my $strict_tags = 'STRICT-TAGS +='; |
| 105 |
my @strict = (); |
| 106 |
my $lf = ''; |
| 107 |
foreach my $tag (@tags) { |
| 108 |
if ($tag eq '*') { |
| 109 |
next; # Marker for erasure, any, etc. |
| 110 |
} |
| 111 |
if ($tag eq '>>>') { |
| 112 |
next; # Start of window marker |
| 113 |
} |
| 114 |
if ($tag eq '<<<') { |
| 115 |
next; # End of window marker |
| 116 |
} |
| 117 |
if ($tag =~ m@^\^@) { |
| 118 |
next; # Fail-fast tags |
| 119 |
} |
| 120 |
if ($tag =~ m@^VSTR:@) { |
| 121 |
next; # Varstrings |
| 122 |
} |
| 123 |
if ($tag =~ m@^VAR:@) { |
| 124 |
next; # Global variables |
| 125 |
} |
| 126 |
if ($tag =~ m@^_.+_$@) { |
| 127 |
next; # Magic placeholders such as _TARGET_ |
| 128 |
} |
| 129 |
if ($tag =~ m@dummy string@) { |
| 130 |
next; # Internal placeholder for tag #0 |
| 131 |
} |
| 132 |
|
| 133 |
if ($tag =~ m@[>"/]v$@) { |
| 134 |
next; |
| 135 |
} |
| 136 |
if (! defined $opts{'secondary'} && $tag =~ m@^<.+>[riv]*$@) { |
| 137 |
next; |
| 138 |
} |
| 139 |
if (! defined $opts{'regex'} && $tag =~ m@^/.+/[riv]*$@) { |
| 140 |
next; |
| 141 |
} |
| 142 |
if (! defined $opts{'icase'} && $tag =~ m@[>"/]i$@) { |
| 143 |
next; |
| 144 |
} |
| 145 |
if (! defined $opts{'baseforms'} && $tag =~ m@^"[^<].*"[riv]*$@) { |
| 146 |
next; |
| 147 |
} |
| 148 |
if (! defined $opts{'wordforms'} && $tag =~ m@^"<.*>"[riv]*$@) { |
| 149 |
next; |
| 150 |
} |
| 151 |
|
| 152 |
my $nlf = substr($tag, 0, 1); |
| 153 |
if ($nlf =~ m@^\p{Lu}@) { |
| 154 |
$nlf = 'A'; |
| 155 |
} |
| 156 |
elsif ($nlf =~ m@^\p{Ll}@) { |
| 157 |
$nlf = 'a'; |
| 158 |
} |
| 159 |
elsif ($tag =~ m@^"<.+>"[riv]*$@) { |
| 160 |
$nlf = 'W'; |
| 161 |
} |
| 162 |
elsif ($tag =~ m@^".+"[riv]*$@) { |
| 163 |
$nlf = 'B'; |
| 164 |
} |
| 165 |
if ($lf && $lf ne $nlf) { |
| 166 |
$strict_tags .= "\n"; |
| 167 |
} |
| 168 |
$lf = $nlf; |
| 169 |
|
| 170 |
$strict_tags .= " $tag"; |
| 171 |
push(@strict, $tag); |
| 172 |
} |
| 173 |
$strict_tags .= " ;\n"; |
| 174 |
|
| 175 |
if (! defined $opts{'output'}) { |
| 176 |
print "Put these lines at the top of your grammar, and edit the STRICT-TAGS list by removing invalid tags:\n\n"; |
| 177 |
print $options; |
| 178 |
print $strict_tags; |
| 179 |
exit(0); |
| 180 |
} |
| 181 |
|
| 182 |
my $g = ''; |
| 183 |
{ |
| 184 |
local $/ = undef; |
| 185 |
open(FH, '<'.$opts{'grammar'}); |
| 186 |
$g = <FH>; |
| 187 |
close(FH); |
| 188 |
} |
| 189 |
|
| 190 |
if (defined $opts{'strip'}) { |
| 191 |
print STDERR "Deleting unnecessary LISTs...\n"; |
| 192 |
foreach my $tag (@strict) { |
| 193 |
if ($g =~ m@[\b\n][Ll][Ii][Ss][Tt]\s+\Q$tag\E\s*=\s*\Q$tag\E\s*;@) { |
| 194 |
print STDERR "Deleting LIST $tag\n"; |
| 195 |
$g =~ s@[\b\n][Ll][Ii][Ss][Tt]\s+\Q$tag\E\s*=\s*\Q$tag\E\s*;@@; |
| 196 |
} |
| 197 |
} |
| 198 |
} |
| 199 |
|
| 200 |
print $options; |
| 201 |
print $strict_tags; |
| 202 |
print "\n"; |
| 203 |
print $g; |