c96f2393f11a5f0fc682eb08173b033b91267e36
[catagits/CatalystX-Declare.git] / lib / CatalystX / Declarative / Keyword / Action.pm
1 use MooseX::Declare;
2
3 class CatalystX::Declarative::Keyword::Action
4     with MooseX::Declare::Syntax::KeywordHandling {
5
6
7     use Carp                qw( croak );
8     use Perl6::Junction     qw( any );
9     use Data::Dump          qw( pp );
10     use MooseX::Types::Util qw( has_available_type_export );
11     use Class::Inspector;
12     use Class::MOP;
13
14
15     use constant STOP_PARSING   => '__MXDECLARE_STOP_PARSING__';
16     use constant UNDER_VAR      => '$CatalystX::Declarative::SCOPE::UNDER';
17
18     use aliased 'MooseX::Method::Signatures::Meta::Method';
19     use aliased 'MooseX::MethodAttributes::Role::Meta::Method', 'AttributeRole';
20
21
22     method parse (Object $ctx) {
23
24         # somewhere to put the attributes
25         my %attributes;
26         my @populators;
27         my $skipped_declarator;
28
29         # parse declarations
30         until (do { $ctx->skipspace; $ctx->peek_next_char } eq any qw( ; { } )) {
31
32             $ctx->skipspace;
33             
34             # optional commas
35             if ($ctx->peek_next_char eq ',') {
36
37                 my $linestr = $ctx->get_linestr;
38                 substr($linestr, $ctx->offset, 1) = '';
39                 $ctx->set_linestr($linestr);
40
41                 next;
42             }
43
44             # next thing should be an option name
45             my $option = (
46                 $skipped_declarator 
47                 ? $ctx->strip_name 
48                 : do { 
49                     $ctx->skip_declarator; 
50                     $skipped_declarator++;
51                     $ctx->declarator;
52                 })
53               or croak "Expected option token, not " . substr($ctx->get_linestr, $ctx->offset);
54
55             # we need to be able to handle the rest
56             my $handler = $self->can("_handle_${option}_option")
57                 or croak "Unknown action option: $option";
58
59             # call the handler
60             my $populator = $self->$handler($ctx, \%attributes);
61
62             if ($populator and $populator eq STOP_PARSING) {
63
64                 return $ctx->shadow(sub (&) {
65                     my ($body) = @_;
66                     return $body->();
67                 });
68             }
69
70             push @populators, $populator
71                 if defined $populator;
72         }
73
74         croak "Need an action specification"
75             unless exists $attributes{Signature};
76
77         my $name   = $attributes{Subname};
78         my $method = Method->wrap(
79             signature       => qq{($attributes{Signature})},
80             package_name    => $ctx->get_curstash_name,
81             name            => $name,
82         );
83
84         AttributeRole->meta->apply($method);
85
86         $_->($method)
87             for @populators;
88
89         $attributes{PathPart} ||= "'$name'";
90
91         delete $attributes{CaptureArgs}
92             if exists $attributes{Args};
93
94         $attributes{CaptureArgs} = 0
95             unless exists $attributes{Args}
96                 or exists $attributes{CaptureArgs};
97
98         if ($ctx->peek_next_char eq '{') {
99             $ctx->inject_if_block($ctx->scope_injector_call . $method->injectable_code);
100         }
101         else {
102             $ctx->inject_code_parts_here(
103                 sprintf '{ %s%s }',
104                     $ctx->scope_injector_call,
105                     $method->injectable_code,
106             );
107         }
108
109         my @attributes = map { 
110             join('',
111                 $_,
112                 sprintf('(%s)',
113                     ref($attributes{ $_ }) eq 'ARRAY'
114                     ? join(' ', @{ $attributes{ $_ } })
115                     : $attributes{ $_ }
116                 ),
117             );
118         } keys %attributes;
119
120         return $ctx->shadow(sub (&) {
121             my $class = caller;
122
123             $method->_set_actual_body(shift);
124             $method->{attributes} = \@attributes;
125     
126             $class->meta->add_method($name, $method);
127             $class->meta->register_method_attributes($class->can($method->name), \@attributes);
128         });
129     }
130
131     method _handle_with_option (Object $ctx, HashRef $attrs) {
132
133         my $role = $ctx->strip_name
134             or croak "Expected bareword role specification for action after with";
135
136         # we need to fish for aliases here since we are still unclean
137         if (defined(my $alias = $self->_check_for_available_import($ctx, $role))) {
138             $role = $alias;
139         }
140
141         push @{ $attrs->{CatalystX_Declarative_ActionRoles} ||= [] }, $role;
142
143         return;
144     }
145
146     method _handle_isa_option (Object $ctx, HashRef $attrs) {
147
148         my $class = $ctx->strip_name
149             or croak "Expected bareword action class specification for action after isa";
150
151         if (defined(my $alias = $self->_check_for_available_import($ctx, $class))) {
152             $class = $alias;
153         }
154
155         $attrs->{CatalystX_Declarative_ActionClass} = $class;
156
157         return;
158     }
159
160     method _check_for_available_import (Object $ctx, Str $name) {
161
162         if (my $code = $ctx->get_curstash_name->can($name)) {
163             return $code->();
164         }
165
166         return undef;
167     }
168
169     method _handle_action_option (Object $ctx, HashRef $attrs) {
170
171         # action name
172         my $name = $ctx->strip_name
173             or croak "Anonymous actions not yet supported";
174
175         # signature
176         my $proto = $ctx->strip_proto || '';
177         $proto = join(', ', 'Object $self: Object $ctx', $proto || ());
178
179         $attrs->{Subname}   = $name;
180         $attrs->{Signature} = $proto;
181
182         if (defined $CatalystX::Declarative::SCOPE::UNDER) {
183             $attrs->{Chained} ||= $CatalystX::Declarative::SCOPE::UNDER;
184         }
185
186         return;
187     }
188
189     method _handle_final_option (Object $ctx, HashRef $attrs) {
190
191         return $self->_build_flag_populator($ctx, $attrs, 'final');
192     }
193
194     method _handle_is_option (Object $ctx, HashRef $attrs) {
195
196         my $what = $ctx->strip_name
197             or croak "Expected symbol token after is symbol, not " . substr($ctx->get_linestr, $ctx->offset);
198
199         return $self->_build_flag_populator($ctx, $attrs, $what);
200     }
201
202     method _build_flag_populator (Object $ctx, HashRef $attrs, Str $what) {
203
204         return sub {
205             my $method = shift;
206
207             if ($what eq any qw( end endpoint final )) {
208                 my $count = $self->_count_positional_arguments($method);
209                 $attrs->{Args} = defined($count) ? $count : '';
210             }
211             elsif ($what eq 'private') {
212                 $attrs->{Private} = 1;
213             }
214         };
215     }
216
217     method _handle_under_option (Object $ctx, HashRef $attrs) {
218
219         my $target = $self->_strip_actionpath($ctx);
220         $ctx->skipspace;
221
222         if ($ctx->peek_next_char eq '{' and $self->identifier eq 'under') {
223             $ctx->inject_if_block(
224                 sprintf '%s; local %s; BEGIN { %s = qq(%s) };',
225                     $ctx->scope_injector_call,
226                     UNDER_VAR,
227                     UNDER_VAR,
228                     $target,
229             );
230             return STOP_PARSING;
231         }
232
233         $attrs->{Chained} = "'$target'";
234
235         return sub {
236             my $method = shift;
237
238             my $count = $self->_count_positional_arguments($method);
239             $attrs->{CaptureArgs} = $count
240                 if defined $count;
241         };
242     }
243
244     method _handle_chains_option (Object $ctx, HashRef $attrs) {
245
246         $ctx->skipspace;
247         $ctx->strip_name eq 'to'
248             or croak "Expected to token after chains symbol, not " . substr($ctx->get_linestr, $ctx->offset);
249
250         return $self->_handle_under_option($ctx, $attrs);
251     }
252
253     method _handle_as_option (Object $ctx, HashRef $attrs) {
254
255         $ctx->skipspace;
256
257         my $path = $self->_strip_actionpath($ctx);
258         $attrs->{PathPart} = "'$path'";
259
260         return;
261     }
262
263     method _count_positional_arguments (Object $method) {
264         my $signature = $method->_parsed_signature;
265
266         if ($signature->has_positional_params) {
267             my $count = @{ scalar($signature->positional_params) };
268
269             if ($count and ($signature->positional_params)[-1]->sigil eq '@') {
270                 return undef;
271             }
272
273             return $count - 1;
274         }
275
276         return 0;
277     }
278
279     method _strip_actionpath (Object $ctx) {
280
281         $ctx->skipspace;
282         my $linestr = $ctx->get_linestr;
283         my $rest    = substr($linestr, $ctx->offset);
284
285         if ($rest =~ /^ ( [_a-z] [_a-z0-9]* ) \b/ix) {
286             substr($linestr, $ctx->offset, length($1)) = '';
287             $ctx->set_linestr($linestr);
288             return $1;
289         }
290         elsif ($rest =~ /^ ' ( (?:[.:;,_a-z0-9]|\/)* ) ' /ix) {
291             substr($linestr, $ctx->offset, length($1) + 2) = '';
292             $ctx->set_linestr($linestr);
293             return $1;
294         }
295         else {
296             croak "Invalid syntax for action path: $rest";
297         }
298     }
299 }
300
301
302