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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
|
#!/usr/bin/perl
# a simple script to replace all @G ... @`other' regions with
# @= ... @>@; lines
# comments are allowed at the end of the lines.
# only one style of comments is accepted: /* ... */. note that these are not
# output
open FILE, "$ARGV[0]" or die "Cannot open input file $ARGV[0]\n";
open FILEOUT, ">$ARGV[1]" or die "Cannot open output file $ARGV[1]\n";
$state = 0;
while (<FILE>) {
$inline = $_;
if ( $inline =~ m/^\@G(.*)$/ ) { # @G detected, this line is part of the `other language' region
$inline = $1; $state = 1;
if ( $inline =~ m/^\(([^)]*)\).*/ ) {
printf FILEOUT "\@q Start generic language section\@>\n\@t}\\begingsec{%s}{\@>\n", "$1"; # a parser switcher
} else {
$inline = " Start \@\@G (generic) language section";
printf FILEOUT "\@q%s\@>\n\@t}\\begingsec{b}{\@>\n", "$inline"; # a parser switcher
}
} elsif ( $inline =~ m/^\@[\scp].*$/ ) { # @`other' detected, so `other language' region is over
if ($state == 1) {
printf FILEOUT "\@q%s\@>\n\@t}\\endgsec{\@>\n",
"End of generic language section"; # a parser switcher
}
$state = 0;
printf FILEOUT "%s", "$inline";
} elsif ( $inline =~ m/^\s*\@[=t].*$/ ) { # @= detected, just copy the line
printf FILEOUT "%s", "$inline";
} elsif ( $inline =~ m/^\@g(.*)$/ ) { # @`other' detected, so `other language' region is over
if ($state == 1) {
printf FILEOUT "\@q%s\@>\n\@t}\\endgsec{\@>\n",
"End of generic language section"; # a parser switcher
}
$inline = $1; $state = 0;
if ( not $inline ) { $inline = "End generic language section"; }
printf FILEOUT "\@q%s\@>\n", "$inline";
} elsif ( $inline =~ m/^\@O(.*)$/ ) { # @O detected, so `other language' region is paused
$inline = $1; $state = 0;
if ( not $inline ) { $inline = "End generic language section"; }
printf FILEOUT "\@q%s\@>\n", "$inline";
} elsif ( $inline =~ m/^\@o(.*)$/ ) { # @o detected, so `other language' region is resumed
$inline = $1; $state = 1;
if ( not $inline ) { $inline = "End generic language section"; }
printf FILEOUT "\@q%s\@>\n", "$inline";
} elsif ( $state == 1 ) {
if ( $inline =~ m/\/\*.*\*\/\s*$/ ) {
$inline =~ m/^(.*\S|)\s*(\/\*.*\*\/)\s*$/;
$string = $1; $comment = $2;
} else {
$inline =~ m/^(.*)$/;
$string = $1; $comment = "";
}
if ( $string ) {
printf FILEOUT "\@=%s\@>\@;", "$string";
} else {
printf FILEOUT "\@=%s\@>\@;", " ";
}
if ( $comment ) {
printf FILEOUT "%s", "$comment";
}
printf FILEOUT "%s", "\n";
} else {
printf FILEOUT "%s", "$inline";
}
}
|