Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Newer
Older
100644 97 lines (87 sloc) 2.5 kB
037fd78 @ruoso first commit
authored
1 use Faz::Action;
2 use Faz::Action::Public;
3 # the dispatcher catalogs all actions, and is responsible for
4 # actually trying to invoke them
5 role Faz::Dispatcher {
6 has %!actions;
7 has @!public;
605c169 @ruoso the compiled regex should be private
authored
8 has $!regex;
037fd78 @ruoso first commit
authored
9
10 method register-action (Faz::Action $a) {
11 fail 'Duplicated action'
12 if %!actions.exists($a.private-name);
13 %!actions{$a.private-name} = $a;
14 if $a ~~ Faz::Action::Public {
15 @!public = (@!public, $a).sort: { $_.priority }
16 }
17 }
18
19 # this method freezes the regexes, combining them into a single
20 # regular expression that will evaluate the request and return the
21 # desired action.
22 method compile {
23
24 my sub buildspec($act) {
14156c0 @ruoso moving onward... getting weirdness
authored
25 my &action_capture = $act.regex;
26 my &closure = -> $/ {
27 make $act;
28 my $in = $/.new($/);
29 $in.to = $in.from; $in;
30 };
037fd78 @ruoso first commit
authored
31 if $act.parent {
14156c0 @ruoso moving onward... getting weirdness
authored
32 my &parent_action_capture = buildspec($act.parent);
a734592 @ruoso work around last rakudo bugs... IT WORKS!
authored
33 my $t = token { <parent_action_capture> <action_capture> <?closure> };
34 return $t;
037fd78 @ruoso first commit
authored
35 } else {
a734592 @ruoso work around last rakudo bugs... IT WORKS!
authored
36 my $t = token { <action_capture> <?closure> };
37 return $t;
037fd78 @ruoso first commit
authored
38 }
39 }
40
605c169 @ruoso the compiled regex should be private
authored
41 my @subregexes = map { buildspec($_) }, @!public;
037fd78 @ruoso first commit
authored
42
14156c0 @ruoso moving onward... getting weirdness
authored
43 my &subrx = sub ($/) {
44 for @subregexes -> &eachrx {
6db6cb4 @ruoso some tests
authored
45 my $match = Match.new($/);
46 my $result = eachrx($match);
40aca7f @ruoso send in debug info for pmichaud
authored
47 if $result {
48fda6b @ruoso use more information from pmichaud
authored
48 return $result;
40aca7f @ruoso send in debug info for pmichaud
authored
49 };
037fd78 @ruoso first commit
authored
50 };
48fda6b @ruoso use more information from pmichaud
authored
51 return Match.new($/);
52 };
037fd78 @ruoso first commit
authored
53
6db6cb4 @ruoso some tests
authored
54 warn 'Compiling regexes.';
14156c0 @ruoso moving onward... getting weirdness
authored
55 $!regex = token { <subrx> };
037fd78 @ruoso first commit
authored
56
605c169 @ruoso the compiled regex should be private
authored
57 # I get a null pmc in isa_pmc() if without this line...
58 1;
037fd78 @ruoso first commit
authored
59 }
60
61 method dispatch() {
6db6cb4 @ruoso some tests
authored
62 unless defined $!regex {
63 warn 'Compiling regexes at dispatch time.';
64 self.compile;
65 }
0536e77 @ruoso rakudo implements the $*foo syntax as globals... so lets workaround t…
authored
66 if $*request.uri.path ~~ $!regex {
14156c0 @ruoso moving onward... getting weirdness
authored
67 my %named = %($<subrx><action_capture>);
68 my @pos = @($<subrx><action_capture>);
69 %named<parent_action_capture> = $<subrx><parent_action_capture>;
70 self.run-action($<subrx>.ast, |@pos, |%named );
037fd78 @ruoso first commit
authored
71 } else {
6db6cb4 @ruoso some tests
authored
72 warn "No action matched '{$*request.uri.path}'";
73 1;
037fd78 @ruoso first commit
authored
74 }
75 }
76
14007bc @ruoso It Works! there are a few issues in rakudo yet...
authored
77 method run-action($action is context, *@pos, *%named) {
037fd78 @ruoso first commit
authored
78 my $errors is context<rw>;
14007bc @ruoso It Works! there are a few issues in rakudo yet...
authored
79 {
80 $action.*begin(|@pos, |%named);
81 $action.*execute(|@pos, |%named);
037fd78 @ruoso first commit
authored
82 CATCH {
14156c0 @ruoso moving onward... getting weirdness
authored
83 say $!;
84 $errors = $! if $!;
037fd78 @ruoso first commit
authored
85 }
86 }
afbf20d @ruoso s/end/finish/ in order to avoid name clash with Any
authored
87 $action.*finish(|@pos, |%named);
1f67a07 @ruoso document rakudo TODOs
authored
88 # TODO: rakudo don't know how to handle control exceptions yet.
037fd78 @ruoso first commit
authored
89 # CONTROL {
90 # when Faz::ControlExceptionDetach {
91 # self.run-action(%!actions{$_.path}, |$_.capture);
92 # }
93 # }
14007bc @ruoso It Works! there are a few issues in rakudo yet...
authored
94 1;
037fd78 @ruoso first commit
authored
95 }
96 }
Something went wrong with that request. Please try again.