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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
|
package Translate;
#
# Contains the code to Translate a pattern to a list of APIs.
#
use strict;
use warnings;
use Getopt::Long;
use feature 'say';
use Function::Parameters;
use Data::Dumper;
use Exporter;
our @ISA = qw(Exporter);
our @EXPORT_OK = qw(pat2apis); # Export on demand
use lib qw(.);
use API;
use PosExpr;
my $debug = 0;
sub setdebug { my($d) = @_; $debug = $d; }
#
# my $name = str2name($str);
# Given a string literal $str, determine a unique pos
# name $name. Usually this is the first letter of $str
# followed by a disambiguating number.
#
my %knownstr2name;
fun str2name( $str )
{
die "str2name: empty str $str!\n" unless $str;
$str =~ /^(.)/;
my $first = $1;
$first = 'p' unless $first =~ /^[0-9a-z]/; # if not alphanumeric..
if( exists $knownstr2name{$first} )
{
my $i;
for( $i=1; exists $knownstr2name{"$first$i"}; $i++ )
{
}
$knownstr2name{"$first$i"}++;
return "$first$i";
}
$knownstr2name{$first}++;
return $first;
}
#
# my $api = mkapi( $str );
# Wrapper: Turns a string into an API element or dies trying.
#
fun mkapi( $str )
{
my @api = API::parse($str);
if( $debug )
{
$str =~ /^(.)/; # grab first char
my $msg = $1 eq 'C' ? 'capture' : '+';
say "$msg $str";
}
return $api[0];
}
#
# my $pe = sp( $str );
# Wrapper: Turns a string into a PosExpr or dies trying.
#
fun sp( $str ) { return PosExpr::simpleparse($str); }
#
# my( $newlpos, @api ) = fixedisland( $island, $lpos, $captures );
# We have a fixed-width island (a pattern with no '*'s) called $island,
# which must start at posexpr $lpos (so it can be symbolic).
# Translate the island into a list of APIs, and return that list along
# with the new lpos after matching it.
# Store any associated text captures (also APIs) in @$captures in left
# to right order (one capture per '?') - they will be added to the final
# list of apis at the end of the whole pattern match (NOT HERE).
#
fun fixedisland( $island, $lpos, $captures )
{
say "island $island @ $lpos" if $debug;
if( $island !~ /\?/ ) # string literal?
{
my $name = str2name($island);
my $l = length($island);
my $api = mkapi("A'$island' $lpos->$name");
$lpos = sp("$name+$l");
return ( $lpos, $api );
}
# ok, the island has at least one '?', maybe more
# build captures as we go along, from left to right
my @api;
while( $island )
{
if( $island =~ s/^([^?]+)// )
{
my $str = $1; # strlit at lpos
my $l = length($str);
my $name = str2name($str);
say "str=$str, name=$name, remaining island=$island";
my $api = mkapi("A'$str' $lpos->$name");
push @api, $api;
$lpos = sp("$name+$l");
} else
{
$island =~ s/^\?//;
# found a '?'
push @$captures, mkapi("C $lpos");
$lpos = $lpos->add(1);
}
say "debug: island now $island, lpos now $lpos" if $debug;
}
return ( $lpos, @api );
}
#
# my( $newlpos, @api ) = floatingisland( $island, $lpos, $rpos, $captures );
# Look for a "floating island" $island at or after position $lpos,
# and allowed to extend up to position $rpos. Return the new lpos "after
# the island" and the list of generated API instructions, and append any
# captures (in left to right order) onto @$captures.
#
fun floatingisland( $island, $lpos, $rpos, $captures )
{
my @api;
if( $island !~ /\?/ ) # if the island is a literal...
{
my $name = str2name($island);
my $l = length($island);
push @api, mkapi("M'$island' $lpos->$name");
my $pos = $rpos->add(1);
push @api, mkapi("T$pos>=$name+$l");
push @$captures, mkapi("C $lpos $name-1");
$lpos = sp("$name+$l");
return( $lpos, @api );
}
# otherwise island contains at least one '?'
# find first strlit in $island, counting how many '?'s
# come before it.
my $nq = 0;
while( $island =~ s/^\?// )
{
$nq++;
}
say "debug: floatisland: found nq=$nq '?'s before first strlit"
if $debug;
$island =~ s/^([^?]+)//; # find and remove first strlit
my $str = $1;
my $l = length($str);
my $name = str2name($str);
my $p = $lpos->add($nq); # form lpos+nq: the first poss
# position that could match
# the first strlit $str
push @api, mkapi("M'$str' $p->$name" );
# generate a capture for the '*' before this island starts
$p = sp("$name-". ($nq+1) );
push @$captures, mkapi("C $lpos $p");
# now generate captures for each of $nq '?'s before the $str
for( my $i=0; $i<$nq; $i++ )
{
$p = $p->add(1);
push @$captures, mkapi("C $p");
}
# now deal with the rest of the floating island, starting at:
$lpos = sp("$name+$l");
# could be empty
return ( $lpos, @api ) unless $island;
# the rest of it is by definition a fixed island starting @ $name+$l
( $lpos, my @newapi ) =
fixedisland( $island, $lpos, $captures );
push @api, @newapi;
return ( $lpos, @api );
}
#
# my $nfix = countfixed( $pat );
# Count the number of fixed-width elements in $pat, ie.
# literal characters and '?' - anything but '*' in fact.
#
fun countfixed( $pat )
{
my $result = grep { $_ ne '*' } split(//,$pat);
return $result;
}
#
# my @api = pat2apis( $pat );
# Translate pattern $pat into a list of API objects.
#
fun pat2apis( $pat )
{
my $lpos = sp('0');
my $rpos = sp('slen-1');
return mkapi( "C $lpos $rpos" ) if $pat eq '*'; # special case
%knownstr2name = (); # empty str2name cache
my @api;
my @captures;
my $nfix = countfixed( $pat );
my $len = length($pat);
if( $nfix == $len ) # if the whole pattern is fixed - no '*'
{
push @api, mkapi("Tslen=$len"); # Test length
($lpos, my @islandapi ) =
fixedisland( $pat, $lpos, \@captures );
push @api, @islandapi, @captures;
return @api;
}
# ok, there's at least 1 '*'.. test the string is long enough, $nfix
# (the number of fixed elements in the pattern) is the minimum length
if( $nfix > 0 )
{
push @api, mkapi("Tslen>=$nfix");
}
$pat =~ s/^([^*]*)//; # find first island (if any)
my $island = $1;
if( $island )
{
($lpos, my @islandapi ) =
fixedisland( $island, $lpos, \@captures );
push @api, @islandapi;
}
$pat =~ s/([^*]*)$//; # find trailing island (if any)
$island = $1;
my @finalcaptures;
if( $island )
{
my $l = length($island);
my $p = sp("slen-$l");
my( $lastislandlpos, @islandapi ) =
fixedisland( $island, $p, \@finalcaptures );
push @api, @islandapi;
$rpos = $lastislandlpos->add(-($l+1));
# NB: need to append @finalcaptures to @captures at the end,
# before appending @captures to @api.
}
# now, pattern comprises * or *island* or *island*island...*
say "middle part ($pat), rpos=$rpos, lpos=$lpos"
if $debug;
if( $pat eq '*' )
{
my $p = $rpos->add(1);
push @api, mkapi("T$p>=$lpos");
push @captures, mkapi("C $lpos $rpos");
} else
{
$pat =~ s/^\*//; # remove leading '*'
while( $pat && $pat =~ s/^([^*]+)\*// )
{
my $float = $1;
say "found * floatingisland '$float' *, lpos=$lpos, ".
"rpos=$rpos" if $debug;
( $lpos, my @fapi ) =
floatingisland( $float, $lpos, $rpos, \@captures );
push @api, @fapi;
}
die "pat2apis: logic error, $pat should be empty\n" if $pat;
# add the capture for the final '*'
push @captures, mkapi("C $lpos $rpos");
}
push @api, @captures, @finalcaptures;
return @api;
}
1;
|