b3a6dce62d11566d5b2d01f0a787b92b68b2214b
[p5sagit/Devel-Declare.git] / lib / Devel / Declare.pm
1 package Devel::Declare;
2 # ABSTRACT: Adding keywords to perl, in perl
3
4 use strict;
5 use warnings;
6 use 5.008001;
7
8 our $VERSION = '0.006014';
9
10 use constant DECLARE_NAME => 1;
11 use constant DECLARE_PROTO => 2;
12 use constant DECLARE_NONE => 4;
13 use constant DECLARE_PACKAGE => 8+1; # name implicit
14
15 use vars qw(%declarators %declarator_handlers @ISA);
16 use base qw(DynaLoader);
17 use Scalar::Util 'set_prototype';
18 use B::Hooks::OP::Check 0.19;
19
20 bootstrap Devel::Declare;
21
22 @ISA = ();
23
24 initialize();
25
26 sub import {
27   my ($class, %args) = @_;
28   my $target = caller;
29   if (@_ == 1) { # "use Devel::Declare;"
30     no strict 'refs';
31     foreach my $name (qw(NAME PROTO NONE PACKAGE)) {
32       *{"${target}::DECLARE_${name}"} = *{"DECLARE_${name}"};
33     }
34   } else {
35     $class->setup_for($target => \%args);
36   }
37 }
38
39 sub unimport {
40   my ($class) = @_;
41   my $target = caller;
42   $class->teardown_for($target);
43 }
44
45 sub setup_for {
46   my ($class, $target, $args) = @_;
47   setup();
48   foreach my $key (keys %$args) {
49     my $info = $args->{$key};
50     my ($flags, $sub);
51     if (ref($info) eq 'ARRAY') {
52       ($flags, $sub) = @$info;
53     } elsif (ref($info) eq 'CODE') {
54       $flags = DECLARE_NAME;
55       $sub = $info;
56     } elsif (ref($info) eq 'HASH') {
57       $flags = 1;
58       $sub = $info;
59     } else {
60       die "Info for sub ${key} must be [ \$flags, \$sub ] or \$sub or handler hashref";
61     }
62     $declarators{$target}{$key} = $flags;
63     $declarator_handlers{$target}{$key} = $sub;
64   }
65 }
66
67 sub teardown_for {
68   my ($class, $target) = @_;
69   delete $declarators{$target};
70   delete $declarator_handlers{$target};
71 }
72
73 my $temp_name;
74 my $temp_save;
75
76 sub init_declare {
77   my ($usepack, $use, $inpack, $name, $proto, $traits) = @_;
78   my ($name_h, $XX_h, $extra_code)
79        = $declarator_handlers{$usepack}{$use}->(
80            $usepack, $use, $inpack, $name, $proto, defined(wantarray), $traits
81          );
82   ($temp_name, $temp_save) = ([], []);
83   if ($name) {
84     $name = "${inpack}::${name}" unless $name =~ /::/;
85     shadow_sub($name, $name_h);
86   }
87   if ($XX_h) {
88     shadow_sub("${inpack}::X", $XX_h);
89   }
90   if (defined wantarray) {
91     return $extra_code || '0;';
92   } else {
93     return;
94   }
95 }
96
97 sub shadow_sub {
98   my ($name, $cr) = @_;
99   push(@$temp_name, $name);
100   no strict 'refs';
101   my ($pack, $pname) = ($name =~ m/(.+)::([^:]+)/);
102   push(@$temp_save, $pack->can($pname));
103   no warnings 'redefine';
104   no warnings 'prototype';
105   *{$name} = $cr;
106   set_in_declare(~~@{$temp_name||[]});
107 }
108
109 sub done_declare {
110   no strict 'refs';
111   my $name = shift(@{$temp_name||[]});
112   die "done_declare called with no temp_name stack" unless defined($name);
113   my $saved = shift(@$temp_save);
114   $name =~ s/(.*):://;
115   my $temp_pack = $1;
116   delete ${"${temp_pack}::"}{$name};
117   if ($saved) {
118     no warnings 'prototype';
119     *{"${temp_pack}::${name}"} = $saved;
120   }
121   set_in_declare(~~@{$temp_name||[]});
122 }
123
124 sub build_sub_installer {
125   my ($class, $pack, $name, $proto) = @_;
126   return eval "
127     package ${pack};
128     my \$body;
129     sub ${name} (${proto}) :lvalue {\n"
130     .'  if (wantarray) {
131         goto &$body;
132       }
133       my $ret = $body->(@_);
134       return $ret;
135     };
136     sub { ($body) = @_; };';
137 }
138
139 sub setup_declarators {
140   my ($class, $pack, $to_setup) = @_;
141   die "${class}->setup_declarators(\$pack, \\\%to_setup)"
142     unless defined($pack) && ref($to_setup) eq 'HASH';
143   my %setup_for_args;
144   foreach my $name (keys %$to_setup) {
145     my $info = $to_setup->{$name};
146     my $flags = $info->{flags} || DECLARE_NAME;
147     my $run = $info->{run};
148     my $compile = $info->{compile};
149     my $proto = $info->{proto} || '&';
150     my $sub_proto = $proto;
151     # make all args optional to enable lvalue for DECLARE_NONE
152     $sub_proto =~ s/;//; $sub_proto = ';'.$sub_proto;
153     #my $installer = $class->build_sub_installer($pack, $name, $proto);
154     my $installer = $class->build_sub_installer($pack, $name, '@');
155     $installer->(sub :lvalue {
156 #{ no warnings 'uninitialized'; warn 'INST: '.join(', ', @_)."\n"; }
157       if (@_) {
158         if (ref $_[0] eq 'HASH') {
159           shift;
160           if (wantarray) {
161             my @ret = $run->(undef, undef, @_);
162             return @ret;
163           }
164           my $r = $run->(undef, undef, @_);
165           return $r;
166         } else {
167           return @_[1..$#_];
168         }
169       }
170       return my $sv;
171     });
172     $setup_for_args{$name} = [
173       $flags,
174       sub {
175         my ($usepack, $use, $inpack, $name, $proto, $shift_hashref, $traits) = @_;
176         my $extra_code = $compile->($name, $proto, $traits);
177         my $main_handler = sub { shift if $shift_hashref;
178           ("DONE", $run->($name, $proto, @_));
179         };
180         my ($name_h, $XX);
181         if (defined $proto) {
182           $name_h = sub :lvalue { return my $sv; };
183           $XX = $main_handler;
184         } elsif (defined $name && length $name) {
185           $name_h = $main_handler;
186         }
187         $extra_code ||= '';
188         $extra_code = '}, sub {'.$extra_code;
189         return ($name_h, $XX, $extra_code);
190       }
191     ];
192   }
193   $class->setup_for($pack, \%setup_for_args);
194 }
195
196 sub install_declarator {
197   my ($class, $target_pack, $target_name, $flags, $filter, $handler) = @_;
198   $class->setup_declarators($target_pack, {
199     $target_name => {
200       flags => $flags,
201       compile => $filter,
202       run => $handler,
203    }
204   });
205 }
206
207 sub linestr_callback_rv2cv {
208   my ($name, $offset) = @_;
209   $offset += toke_move_past_token($offset);
210   my $pack = get_curstash_name();
211   my $flags = $declarators{$pack}{$name};
212   my ($found_name, $found_proto);
213   if ($flags & DECLARE_NAME) {
214     $offset += toke_skipspace($offset);
215     my $linestr = get_linestr();
216     if (substr($linestr, $offset, 2) eq '::') {
217       substr($linestr, $offset, 2) = '';
218       set_linestr($linestr);
219     }
220     if (my $len = toke_scan_word($offset, $flags & DECLARE_PACKAGE)) {
221       $found_name = substr($linestr, $offset, $len);
222       $offset += $len;
223     }
224   }
225   if ($flags & DECLARE_PROTO) {
226     $offset += toke_skipspace($offset);
227     my $linestr = get_linestr();
228     if (substr($linestr, $offset, 1) eq '(') {
229       my $length = toke_scan_str($offset);
230       $found_proto = get_lex_stuff();
231       clear_lex_stuff();
232       my $replace =
233         ($found_name ? ' ' : '=')
234         .'X'.(' ' x length($found_proto));
235       $linestr = get_linestr();
236       substr($linestr, $offset, $length) = $replace;
237       set_linestr($linestr);
238       $offset += $length;
239     }
240   }
241   my @args = ($pack, $name, $pack, $found_name, $found_proto);
242   $offset += toke_skipspace($offset);
243   my $linestr = get_linestr();
244   if (substr($linestr, $offset, 1) eq '{') {
245     my $ret = init_declare(@args);
246     $offset++;
247     if (defined $ret && length $ret) {
248       substr($linestr, $offset, 0) = $ret;
249       set_linestr($linestr);
250     }
251   } else {
252     init_declare(@args);
253   }
254   #warn "linestr now ${linestr}";
255 }
256
257 sub linestr_callback_const {
258   my ($name, $offset) = @_;
259   my $pack = get_curstash_name();
260   my $flags = $declarators{$pack}{$name};
261   if ($flags & DECLARE_NAME) {
262     $offset += toke_move_past_token($offset);
263     $offset += toke_skipspace($offset);
264     if (toke_scan_word($offset, $flags & DECLARE_PACKAGE)) {
265       my $linestr = get_linestr();
266       substr($linestr, $offset, 0) = '::';
267       set_linestr($linestr);
268     }
269   }
270 }
271
272 sub linestr_callback {
273   my $type = shift;
274   my $name = $_[0];
275   my $pack = get_curstash_name();
276   my $handlers = $declarator_handlers{$pack}{$name};
277   if (ref $handlers eq 'CODE') {
278     my $meth = "linestr_callback_${type}";
279     __PACKAGE__->can($meth)->(@_);
280   } elsif (ref $handlers eq 'HASH') {
281     if ($handlers->{$type}) {
282       $handlers->{$type}->(@_);
283     }
284   } else {
285     die "PANIC: unknown thing in handlers for $pack $name: $handlers";
286   }
287 }
288
289 =head1 NAME
290
291 Devel::Declare - Adding keywords to perl, in perl
292
293 =head1 SYNOPSIS
294
295   use Method::Signatures;
296   # or ...
297   use MooseX::Declare;
298   # etc.
299
300   # Use some new and exciting syntax like:
301   method hello (Str :$who, Int :$age where { $_ > 0 }) {
302     $self->say("Hello ${who}, I am ${age} years old!");
303   }
304
305 =head1 DESCRIPTION
306
307 L<Devel::Declare> can install subroutines called declarators which locally take
308 over Perl's parser, allowing the creation of new syntax.
309
310 This document describes how to create a simple declarator.
311
312 =head1 USAGE
313
314 We'll demonstrate the usage of C<Devel::Declare> with a motivating example: a new
315 C<method> keyword, which acts like the builtin C<sub>, but automatically unpacks
316 C<$self> and the other arguments.
317
318   package My::Methods;
319   use Devel::Declare;
320
321 =head2 Creating a declarator with C<setup_for>
322
323 You will typically create
324
325   sub import {
326     my $class = shift;
327     my $caller = caller;
328
329     Devel::Declare->setup_for(
330         $caller,
331         { method => { const => \&parser } }
332     );
333     no strict 'refs';
334     *{$caller.'::method'} = sub (&) {};
335   }
336
337 Starting from the end of this import routine, you'll see that we're creating a
338 subroutine called C<method> in the caller's namespace.  Yes, that's just a normal
339 subroutine, and it does nothing at all (yet!)  Note the prototype C<(&)> which means
340 that the caller would call it like so:
341
342     method {
343         my ($self, $arg1, $arg2) = @_;
344         ...
345     }
346
347 However we want to be able to call it like this
348
349     method foo ($arg1, $arg2) {
350         ...
351     }
352
353 That's why we call C<setup_for> above, to register the declarator 'method' with a custom
354 parser, as per the next section.  It acts on an optype, usually C<'const'> as above.
355 (Other valid values are C<'check'> and C<'rv2cv'>).
356
357 For a simpler way to install new methods, see also L<Devel::Declare::MethodInstaller::Simple>
358
359 =head2 Writing a parser subroutine
360
361 This subroutine is called at I<compilation> time, and allows you to read the custom
362 syntaxes that we want (in a syntax that may or may not be valid core Perl 5) and
363 munge it so that the result will be parsed by the C<perl> compiler.
364
365 For this example, we're defining some globals for convenience:
366
367     our ($Declarator, $Offset);
368
369 Then we define a parser subroutine to handle our declarator.  We'll look at this in
370 a few chunks.
371
372     sub parser {
373       local ($Declarator, $Offset) = @_;
374
375 C<Devel::Declare> provides some very low level utility methods to parse character
376 strings.  We'll define some useful higher level routines below for convenience,
377 and we can use these to parse the various elements in our new syntax.
378
379 Notice how our parser subroutine is invoked at compile time,
380 when the C<perl> parser is pointed just I<before> the declarator name.
381
382       skip_declarator;          # step past 'method'
383       my $name = strip_name;    # strip out the name 'foo', if present
384       my $proto = strip_proto;  # strip out the prototype '($arg1, $arg2)', if present
385
386 Now we can prepare some code to 'inject' into the new subroutine.  For example we
387 might want the method as above to have C<my ($self, $arg1, $arg2) = @_> injected at
388 the beginning of it.  We also do some clever stuff with scopes that we'll look
389 at shortly.
390
391       my $inject = make_proto_unwrap($proto);
392       if (defined $name) {
393         $inject = scope_injector_call().$inject;
394       }
395       inject_if_block($inject);
396
397 We've now managed to change C<method ($arg1, $arg2) { ... }> into C<method {
398 injected_code; ... }>.  This will compile...  but we've lost the name of the
399 method!
400
401 In a cute (or horrifying, depending on your perspective) trick, we temporarily
402 change the definition of the subroutine C<method> itself, to specialise it with
403 the C<$name> we stripped, so that it assigns the code block to that name.
404
405 Even though the I<next> time C<method> is compiled, it will be
406 redefined again, C<perl> caches these definitions in its parse
407 tree, so we'll always get the right one!
408
409 Note that we also handle the case where there was no name, allowing
410 an anonymous method analogous to an anonymous subroutine.
411
412       if (defined $name) {
413         $name = join('::', Devel::Declare::get_curstash_name(), $name)
414           unless ($name =~ /::/);
415         shadow(sub (&) { no strict 'refs'; *{$name} = shift; });
416       } else {
417         shadow(sub (&) { shift });
418       }
419     }
420
421
422 =head2 Parser utilities in detail
423
424 For simplicity, we're using global variables like C<$Offset> in these examples.
425 You may prefer to look at L<Devel::Declare::Context::Simple>, which
426 encapsulates the context much more cleanly.
427
428 =head3 C<skip_declarator>
429
430 This simple parser just moves across a 'token'.  The common case is
431 to skip the declarator, i.e.  to move to the end of the string
432 'method' and before the prototype and code block.
433
434     sub skip_declarator {
435       $Offset += Devel::Declare::toke_move_past_token($Offset);
436     }
437
438 =head4 C<toke_move_past_token>
439
440 This builtin parser simply moves past a 'token' (matching C</[a-zA-Z_]\w*/>)
441 It takes an offset into the source document, and skips past the token.
442 It returns the number of characters skipped.
443
444 =head3 C<strip_name>
445
446 This parser skips any whitespace, then scans the next word (again matching a
447 'token').  We can then analyse the current line, and manipulate it (using pure
448 Perl).  In this case we take the name of the method out, and return it.
449
450     sub strip_name {
451       skipspace;
452       if (my $len = Devel::Declare::toke_scan_word($Offset, 1)) {
453         my $linestr = Devel::Declare::get_linestr();
454         my $name = substr($linestr, $Offset, $len);
455         substr($linestr, $Offset, $len) = '';
456         Devel::Declare::set_linestr($linestr);
457         return $name;
458       }
459       return;
460     }
461
462 =head4 C<toke_scan_word>
463
464 This builtin parser, given an offset into the source document,
465 matches a 'token' as above but does not skip.  It returns the
466 length of the token matched, if any.
467
468 =head4 C<get_linestr>
469
470 This builtin returns the full text of the current line of the source document.
471
472 =head4 C<set_linestr>
473
474 This builtin sets the full text of the current line of the source document.
475 Beware that injecting a newline into the middle of the line is likely
476 to fail in surprising ways.  Generally, Perl's parser can rely on the
477 `current line' actually being only a single line.  Use other kinds of
478 whitespace instead, in the code that you inject.
479
480 =head3 C<skipspace>
481
482 This parser skips whitsepace.
483
484     sub skipspace {
485       $Offset += Devel::Declare::toke_skipspace($Offset);
486     }
487
488 =head4 C<toke_skipspace>
489
490 This builtin parser, given an offset into the source document,
491 skips over any whitespace, and returns the number of characters
492 skipped.
493
494 =head3 C<strip_proto>
495
496 This is a more complex parser that checks if it's found something that
497 starts with C<'('> and returns everything till the matching C<')'>.
498
499     sub strip_proto {
500       skipspace;
501
502       my $linestr = Devel::Declare::get_linestr();
503       if (substr($linestr, $Offset, 1) eq '(') {
504         my $length = Devel::Declare::toke_scan_str($Offset);
505         my $proto = Devel::Declare::get_lex_stuff();
506         Devel::Declare::clear_lex_stuff();
507         $linestr = Devel::Declare::get_linestr();
508         substr($linestr, $Offset, $length) = '';
509         Devel::Declare::set_linestr($linestr);
510         return $proto;
511       }
512       return;
513     }
514
515 =head4 C<toke_scan_str>
516
517 This builtin parser uses Perl's own parsing routines to match a "stringlike"
518 expression.  Handily, this includes bracketed expressions (just think about
519 things like C<q(this is a quote)>).
520
521 Also it Does The Right Thing with nested delimiters (like C<q(this (is (a) quote))>).
522
523 It returns the effective length of the expression matched.  Really, what
524 it returns is the difference in position between where the string started,
525 within the buffer, and where it finished.  If the string extended across
526 multiple lines then the contents of the buffer may have been completely
527 replaced by the new lines, so this position difference is not the same
528 thing as the actual length of the expression matched.  However, because
529 moving backward in the buffer causes problems, the function arranges
530 for the effective length to always be positive, padding the start of
531 the buffer if necessary.
532
533 Use C<get_lex_stuff> to get the actual matched text, the content of
534 the string.  Because of the behaviour around multiline strings, you
535 can't reliably get this from the buffer.  In fact, after the function
536 returns, you can't rely on any content of the buffer preceding the end
537 of the string.
538
539 If the string being scanned is not well formed (has no closing delimiter),
540 C<toke_scan_str> returns C<undef>.  In this case you cannot rely on the
541 contents of the buffer.
542
543 =head4 C<get_lex_stuff>
544
545 This builtin returns what was matched by C<toke_scan_str>.  To avoid segfaults,
546 you should call C<clear_lex_stuff> immediately afterwards.
547
548 =head2 Munging the subroutine
549
550 Let's look at what we need to do in detail.
551
552 =head3 C<make_proto_unwrap>
553
554 We may have defined our method in different ways, which will result
555 in a different value for our prototype, as parsed above.  For example:
556
557     method foo         {  # undefined
558     method foo ()      {  # ''
559     method foo ($arg1) {  # '$arg1'
560
561 We deal with them as follows, and return the appropriate C<my ($self, ...) = @_;>
562 string.
563
564     sub make_proto_unwrap {
565       my ($proto) = @_;
566       my $inject = 'my ($self';
567       if (defined $proto) {
568         $inject .= ", $proto" if length($proto);
569         $inject .= ') = @_; ';
570       } else {
571         $inject .= ') = shift;';
572       }
573       return $inject;
574     }
575
576 =head3 C<inject_if_block>
577
578 Now we need to inject it after the opening C<'{'> of the method body.
579 We can do this with the building blocks we defined above like C<skipspace>
580 and C<get_linestr>.
581
582     sub inject_if_block {
583       my $inject = shift;
584       skipspace;
585       my $linestr = Devel::Declare::get_linestr;
586       if (substr($linestr, $Offset, 1) eq '{') {
587         substr($linestr, $Offset+1, 0) = $inject;
588         Devel::Declare::set_linestr($linestr);
589       }
590     }
591
592 =head3 C<scope_injector_call>
593
594 We want to be able to handle both named and anonymous methods.  i.e.
595
596     method foo () { ... }
597     my $meth = method () { ... };
598
599 These will then get rewritten as
600
601     method { ... }
602     my $meth = method { ... };
603
604 where 'method' is a subroutine that takes a code block.  Spot the problem?
605 The first one doesn't have a semicolon at the end of it!  Unlike 'sub' which
606 is a builtin, this is just a normal statement, so we need to terminate it.
607 Luckily, using C<B::Hooks::EndOfScope>, we can do this!
608
609   use B::Hooks::EndOfScope;
610
611 We'll add this to what gets 'injected' at the beginning of the method source.
612
613   sub scope_injector_call {
614     return ' BEGIN { MethodHandlers::inject_scope }; ';
615   }
616
617 So at the beginning of every method, we are passing a callback that will get invoked
618 at the I<end> of the method's compilation... i.e. exactly then the closing C<'}'>
619 is compiled.
620
621   sub inject_scope {
622     on_scope_end {
623       my $linestr = Devel::Declare::get_linestr;
624       my $offset = Devel::Declare::get_linestr_offset;
625       substr($linestr, $offset, 0) = ';';
626       Devel::Declare::set_linestr($linestr);
627     };
628   }
629
630 =head2 Shadowing each method.
631
632 =head3 C<shadow>
633
634 We override the current definition of 'method' using C<shadow>.
635
636     sub shadow {
637       my $pack = Devel::Declare::get_curstash_name;
638       Devel::Declare::shadow_sub("${pack}::${Declarator}", $_[0]);
639     }
640
641 For a named method we invoked like this:
642
643     shadow(sub (&) { no strict 'refs'; *{$name} = shift; });
644
645 So in the case of a C<method foo { ... }>, this call would redefine C<method>
646 to be a subroutine that exports 'sub foo' as the (munged) contents of C<{...}>.
647
648 The case of an anonymous method is also cute:
649
650     shadow(sub (&) { shift });
651
652 This means that
653
654     my $meth = method () { ... };
655
656 is rewritten with C<method> taking the codeblock, and returning it as is to become
657 the value of C<$meth>.
658
659 =head4 C<get_curstash_name>
660
661 This returns the package name I<currently being compiled>.
662
663 =head4 C<shadow_sub>
664
665 Handles the details of redefining the subroutine.
666
667 =head1 SEE ALSO
668
669 One of the best ways to learn C<Devel::Declare> is still to look at
670 modules that use it:
671
672 L<http://cpants.perl.org/dist/used_by/Devel-Declare>.
673
674 =head1 AUTHORS
675
676 Matt S Trout - E<lt>mst@shadowcat.co.ukE<gt> - original author
677
678 Company: http://www.shadowcat.co.uk/
679 Blog: http://chainsawblues.vox.com/
680
681 Florian Ragwitz E<lt>rafl@debian.orgE<gt> - maintainer
682
683 osfameron E<lt>osfameron@cpan.orgE<gt> - first draft of documentation
684
685 =head1 COPYRIGHT AND LICENSE
686
687 This library is free software under the same terms as perl itself
688
689 Copyright (c) 2007, 2008, 2009  Matt S Trout
690
691 Copyright (c) 2008, 2009  Florian Ragwitz
692
693 stolen_chunk_of_toke.c based on toke.c from the perl core, which is
694
695 Copyright (C) 1991, 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999,
696 2000, 2001, 2002, 2003, 2004, 2005, 2006, by Larry Wall and others
697
698 =cut
699
700 1;