summaryrefslogtreecommitdiff
path: root/lib/assertions.pm
blob: 0ced4bc228808a4c967aa7621584ac80707a6528 (plain)
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
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
package assertions;

our $VERSION = '0.02';

# 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 _carp {
    require warnings;
    if (warnings::enabled('assertions')) {
	require Carp;
	Carp::carp(@_);
    }
}

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) {
			_carp "assertion status '_' referenced but not previously defined";
		    }
		    $t=($^H & $hint) ? 1 : 0;
		}
		elsif ($t ne '0' and $t ne '1') {
		    $t = ( grep { ref $_ eq 'Regexp'
				      ? $t=~$_
				      : $_->check($t)
				} @{^ASSERTING} ) ? 1 : 0;
		}

		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 {
    @_ > 1
	and _carp($_[0]."->unimport arguments are being ignored");
    $^H &= ~$hint;
}

sub enabled {
    if (@_) {
	if ($_[0]) {
	    $^H |= $hint;
	}
	else {
	    $^H &= ~$hint;
	}
	$^H |= $seen_hint;
    }
    return $^H & $hint ? 1 : 0;
}

sub seen {
    if (@_) {
	if ($_[0]) {
	    $^H |= $seen_hint;
	}
	else {
	    $^H &= ~$seen_hint;
	}
    }
    return $^H & $seen_hint ? 1 : 0;
}

1;

__END__


=head1 NAME

assertions - select assertions in blocks of code

=head1 SYNOPSIS

  sub assert (&) : assertion { &{$_[0]}() }

  use assertions 'foo';
  assert { print "asserting 'foo'\n" };

  {
      use assertions qw( foo bar );
      assert { print "asserting 'foo' and 'bar'\n" };
  }

  {
      use assertions qw( bar );
      assert { print "asserting only 'bar'\n" };
  }

  {
      use assertions '_ && bar';
      assert { print "asserting 'foo' && 'bar'\n" };
  }

  assert { print "asserting 'foo' again\n" };

=head1 DESCRIPTION

The C<assertions> pragma specifies the tags used to enable and disable
the execution of assertion subroutines.

An assertion subroutine is declared with the C<:assertion> attribute.
This subroutine is not normally executed: it's optimized away by perl
at compile-time.

The C<assertions> pragma associates to its lexical scope one or
several assertion tags. Then, to activate the execution of the
assertions subroutines in this scope, these tags must be given to perl
via the B<-A> command-line option. For instance, if...

  use assertions 'foobar';

is used, assertions on the same lexical scope will only be executed
when perl is called as...

  perl -A=foobar script.pl

Regular expressions can also be used within the -A
switch. For instance...

  perl -A='foo.*' script.pl

will activate assertions tagged as C<foo>, C<foobar>, C<foofoo>, etc.

=head2 Selecting assertions

Selecting which tags are required to activate assertions inside a
lexical scope, is done with the arguments passed on the C<use
assertions> sentence.

If no arguments are given, the package name is used as the assertion tag:

  use assertions;

is equivalent to

  use assertions __PACKAGE__;

When several tags are given, all of them have to be activated via the
C<-A> switch to activate assertion execution on that lexical scope,
i.e.:

  use assertions qw(Foo Bar);

Constants C<1> and C<0> can be used to force unconditional activation
or deactivation respectively:

  use assertions '0';
  use assertions '1';

Operators C<&&> and C<||> and parenthesis C<(...)> can be used to
construct logical expressions:

  use assertions 'foo && bar';
  use assertions 'foo || bar';
  use assertions 'foo && (bar || doz)';

(note that the logical operators and the parens have to be included
inside the quoted string).

Finally, the special tag C<_> refers to the current assertion
activation state:

  use assertions 'foo';
  use assertions '_ && bar;

is equivalent to

  use assertions 'foo && bar';

=head2 Handling assertions your own way

The C<assertions> module also provides a set of low level functions to
allow for custom assertion handling modules.

Those functions are not exported and have to be fully qualified with
the package name when called, for instance:

  require assertions;
  assertions::enabled(1);

(note that C<assertions> is loaded with the C<require> keyword
to avoid calling C<assertions::import()>).

Those functions have to be called at compile time (they are
useless at runtime).

=over 4

=item enabled($on)

activates or deactivates assertion execution. For instance:

  package assertions::always;

  require assertions;
  sub import { assertions::enabled(1) }

  1;

This function calls C<assertion::seen(1)> also (see below).

=item enabled()

returns a true value when assertion execution is active.

=item seen($on)

A warning is generated when an assertion subroutine is found before
any assertion selection code. This function is used to just tell perl
that assertion selection code has been seen and that the warning is
not required for the currently compiling lexical scope.

=item seen()

returns true if any assertion selection module (or code) has been
called before on the currently compiling lexical scope.

=back

=head1 COMPATIBILITY

Support for assertions is only available in perl from version 5.9. On
previous perl versions this module will do nothing, though it will not
harm either.

L<assertions::compat> provides an alternative way to use assertions
compatible with lower versions of perl.


=head1 SEE ALSO

L<perlrun>, L<assertions::activate>, L<assertions::compat>.

=head1 AUTHOR

Salvador FandiE<ntilde>o, E<lt>sfandino@yahoo.comE<gt>

=head1 COPYRIGHT AND LICENSE

Copyright 2002, 2005 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