-
Notifications
You must be signed in to change notification settings - Fork 15
/
TryCatch.pm
253 lines (190 loc) · 6 KB
/
TryCatch.pm
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
package TryCatch;
use strict;
use warnings;
use base 'DynaLoader';
our $VERSION = '1.000000';
our $PARSE_CATCH_NEXT = 0;
our ($CHECK_OP_HOOK, $CHECK_OP_DEPTH) = (undef, 0);
sub dl_load_flags { 0x01 }
__PACKAGE__->bootstrap($VERSION);
use Sub::Exporter -setup => {
exports => [qw/try/],
groups => { default => [qw/try/] },
installer => sub {
my ($args, $to_export) = @_;
my $pack = $args->{into};
foreach my $name (@$to_export) {
if (my $parser = __PACKAGE__->can("_parse_${name}")) {
Devel::Declare->setup_for(
$pack,
{ $name => { const => sub { $parser->($pack, @_) } } },
);
}
if (my $code = __PACKAGE__->can("_extras_for_${name}")) {
$code->($pack);
}
}
Sub::Exporter::default_installer(@_);
}
};
use Devel::Declare ();
use B::Hooks::EndOfScope;
use B::Hooks::OP::PPAddr;
use Devel::Declare::Context::Simple;
use Parse::Method::Signatures;
use Moose::Util::TypeConstraints;
use Scope::Upper qw/unwind want_at :words/;
use TryCatch::Exception;
use TryCatch::TypeParser;
use Carp qw/croak/;
# The actual try call itself. Nothing to do with parsing.
sub try {
my ($sub) = @_;
return new TryCatch::Exception( try => $sub, ctx => SUB(CALLER(1)) );
}
# Where we store all the TCs for catch blocks created at compile time
# Not sure we really want to do this, but we will for now.
our $TC_LIBRARY = {};
sub get_tc {
my ($class, $tc) = @_;
$TC_LIBRARY->{$tc} or die "Unable to find parse TC for '$tc'";
}
# From here on out its parsing methods.
sub _extras_for_try {
my ($pack) = @_;
Devel::Declare->setup_for(
$pack,
{ catch => { const => sub { _parse_catch($pack, @_) } } }
);
}
# Replace 'try {' with an 'try (sub {'
sub _parse_try {
my $pack = shift;
# Hide Devel::Declare from carp;
local $Carp::Internal{'Devel::Declare'} = 1;
my $ctx = Devel::Declare::Context::Simple->new->init(@_);
if (my $len = Devel::Declare::toke_scan_ident( $ctx->offset )) {
$ctx->inc_offset($len);
$ctx->skipspace;
my $linestr = $ctx->get_linestr;
croak "block required after try"
unless substr($linestr, $ctx->offset, 1) eq '{';
substr($linestr, $ctx->offset+1,0) = q# BEGIN { TryCatch::try_postlude() }#;
substr($linestr, $ctx->offset,0) = q#(sub #;
$ctx->set_linestr($linestr);
if (! $CHECK_OP_DEPTH++) {
$CHECK_OP_HOOK = TryCatch::XS::install_return_op_check();
}
}
}
sub try_postlude {
on_scope_end { block_postlude() }
}
sub catch_postlude {
on_scope_end { block_postlude() }
}
sub close_block {
on_scope_end { block_closer() }
}
# Called after the block from try {} or catch {}
sub block_postlude {
my $ctx = Devel::Declare::Context::Simple->new->init(
'',
Devel::Declare::get_linestr_offset()
);
my $offset = $ctx->skipspace;
my $linestr = $ctx->get_linestr;
my $toke = '';
my $len = 0;
if ($len = Devel::Declare::toke_scan_word($offset, 1 )) {
$toke = substr( $linestr, $offset, $len );
}
if (--$CHECK_OP_DEPTH == 0) {
TryCatch::XS::uninstall_return_op_check($CHECK_OP_HOOK);
}
if ($toke eq 'catch') {
$ctx->skipspace;
substr($linestr, $ctx->offset, 0) = ')->';
$ctx->set_linestr($linestr);
$TryCatch::PARSE_CATCH_NEXT = 1;
} else {
substr($linestr, $offset, 0) = ")->run;";
$ctx->set_linestr($linestr);
}
}
sub block_closer {
my $offset = Devel::Declare::get_linestr_offset();
my $linestr = Devel::Declare::get_linestr();
substr($linestr,$offset, 0, "}");
Devel::Declare::set_linestr($linestr);
}
# turn 'catch() {' into '->catch({ TC_check_code;'
# the '->' is added by one of the postlude hooks
sub _parse_catch {
my $pack = shift;
my $ctx = Devel::Declare::Context::Simple->new->init(@_);
return unless $TryCatch::PARSE_CATCH_NEXT;
$TryCatch::PARSE_CATCH_NEXT = 0;
# Hide Devel::Declare from carp;
local $Carp::Internal{'Devel::Declare'} = 1;
local $Carp::Internal{'B::Hooks::EndOfScope'} = 1;
local $Carp::Internal{'TryCatch'} = 1;
$ctx->skipspace;
my $linestr = $ctx->get_linestr;
my $len = length "->catch";
my $sub = substr($linestr, $ctx->offset, $len);
die "_parse_catch expects to find '->catch' in linestr, found: "
. substr($linestr, $ctx->offset, $len)
unless $sub eq '->catch';
$ctx->inc_offset($len);
$ctx->skipspace;
my $var_code = "";
my @conditions;
# optional ()
if (substr($linestr, $ctx->offset, 1) eq '(') {
my $substr = substr($linestr, $ctx->offset+1);
my ($param, $left) = Parse::Method::Signatures->param($substr);
die "can't handle un-named vars yet" unless $param->can('variable_name');
my $name = $param->variable_name;
$var_code .= "my $name= \$@;";
if ($param->has_type_constraints) {
my $parser = TryCatch::TypeParser->new(package => $pack);
my $tc = $parser->visit($param->type_constraints->data);
$TC_LIBRARY->{"$tc"} = $tc;
push @conditions, "'$tc'";
}
if ($param->has_constraints) {
foreach my $con (@{$param->constraints}) {
# This is far less than optimal;
push @conditions, "sub $con";
}
}
substr($linestr, $ctx->offset, length($linestr) - $ctx->offset - length($left), '');
$ctx->set_linestr($linestr);
$ctx->skipspace;
if (substr($linestr, $ctx->offset, 1) ne ')') {
croak "')' expected after catch signature";
}
substr($linestr, $ctx->offset, 1, '');
$ctx->set_linestr($linestr);
$ctx->skipspace;
}
croak "block required after catch"
unless substr($linestr, $ctx->offset, 1) eq '{';
substr($linestr, $ctx->offset+1,0) =
q# BEGIN { TryCatch::catch_postlude() }# . $var_code;
push @conditions, "sub ";
substr($linestr, $ctx->offset,0) = '(' . join(', ', @conditions);
$ctx->set_linestr($linestr);
if (! $CHECK_OP_DEPTH++) {
$CHECK_OP_HOOK = TryCatch::XS::install_return_op_check();
}
}
1;
__END__
=head1 NAME
TryCatch - first class try catch semantics for Perl, with no source filters.
=head1 AUTHOR
Ash Berlin <ash@cpan.org>
=head1 LICENSE
Licensed under the same terms as Perl itself.