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
|
package assertions;
our $VERSION = '0.01';
# use strict;
# use warnings;
my $hint=0x01000000;
my $seen_hint=0x02000000;
sub syntax_error ($$) {
my ($expr, $why)=@_;
require Carp;
Carp::croak("syntax error on assertion filter '$expr' ($why)");
}
sub my_warn ($) {
my $error=shift;
require warnings;
if (warnings::enabled('assertions')) {
require Carp;
Carp::carp($error);
}
}
sub calc_expr {
my $expr=shift;
my @tokens=split / \s*
( && # and
| \|\| # or
| \( # parents
| \) )
\s*
| \s+ # spaces out
/x, $expr;
# print STDERR "tokens: -", join('-',@tokens), "-\n";
my @now=1;
my @op='start';
for my $t (@tokens) {
next if (!defined $t or $t eq '');
if ($t eq '(') {
unshift @now, 1;
unshift @op, 'start';
}
else {
if ($t eq '||') {
defined $op[0]
and syntax_error $expr, 'consecutive operators';
$op[0]='||';
}
elsif ($t eq '&&') {
defined $op[0]
and syntax_error $expr, 'consecutive operators';
$op[0]='&&';
}
else {
if ($t eq ')') {
@now==1 and
syntax_error $expr, 'unbalanced parens';
defined $op[0] and
syntax_error $expr, "key missing after operator '$op[0]'";
$t=shift @now;
shift @op;
}
elsif ($t eq '_') {
unless ($^H & $seen_hint) {
my_warn "assertion status '_' referenced but not previously defined";
}
$t=($^H & $hint) ? 1 : 0;
}
elsif ($t ne '0' and $t ne '1') {
# print STDERR "'$t' resolved as ";
$t=grep ({ $t=~$_ } @{^ASSERTING}) ? 1 : 0;
# print STDERR "$t\n";
}
defined $op[0] or
syntax_error $expr, 'operator expected';
if ($op[0] eq 'start') {
$now[0]=$t;
}
elsif ($op[0] eq '||') {
$now[0]||=$t;
}
else {
$now[0]&&=$t;
}
undef $op[0];
}
}
}
@now==1 or syntax_error $expr, 'unbalanced parens';
defined $op[0] and syntax_error $expr, "expression ends on operator '$op[0]'";
return $now[0];
}
sub import {
# print STDERR "\@_=", join("|", @_), "\n";
shift;
@_=(scalar(caller)) unless @_;
foreach my $expr (@_) {
unless (calc_expr $expr) {
# print STDERR "assertions deactived";
$^H &= ~$hint;
$^H |= $seen_hint;
return;
}
}
# print STDERR "assertions actived";
$^H |= $hint|$seen_hint;
}
sub unimport {
$^H &= ~$hint;
}
1;
__END__
=head1 NAME
assertions - selects assertions
=head1 SYNOPSIS
sub assert (&) : assertion { &{$_[0]}() }
use assertions 'foo';
assert { print "asserting 'foo'\n" };
{
use assertions qw( foo bar );
assert { print "asserting 'foo' & 'bar'\n" };
}
{
use assertions qw( bar );
assert { print "asserting 'bar'\n" };
}
{
use assertions ' _ && bar ';
assert { print "asserting 'foo' && 'bar'\n" };
}
assert { print "asserting 'foo' again\n" };
=head1 ABSTRACT
C<assertions> pragma selects the tags used to control assertion
execution.
=head1 DESCRIPTION
=head2 EXPORT
None by default.
=head1 SEE ALSO
=head1 AUTHOR
Salvador FandiE<ntilde>o, E<lt>sfandino@yahoo.comE<gt>
=head1 COPYRIGHT AND LICENSE
Copyright 2002 by Salvador FandiE<ntilde>o
This library is free software; you can redistribute it and/or modify
it under the same terms as Perl itself.
=cut
|