1 package Web::Dispatch::Parser;
6 if ($ENV{WEB_DISPATCH_PARSER_DEBUG}) {
7 no warnings 'redefine';
13 use Web::Dispatch::Predicates;
17 is => 'lazy', default => quote_sub q{ {} }
20 sub diag { if (DEBUG) { warn $_[0] } }
23 my ($self, $error) = @_;
24 my $hat = (' ' x (pos||0)).'^';
25 die "Error parsing dispatch specification: ${error}\n
31 my ($self, $spec) = @_;
32 $spec =~ s/\s+//g; # whitespace is not valid
33 return $self->_cache->{$spec} ||= $self->_parse_spec($spec);
37 my ($self, $spec, $nested) = @_;
41 push @match, $self->_parse_spec_section($_)
42 or $self->_blam("Unable to work out what the next section is");
44 $self->_blam("Found closing ) with no opening (") unless $nested;
47 last PARSE if (pos == length);
48 $match[-1] = $self->_parse_spec_combinator($_, $match[-1])
49 or $self->_blam('No valid combinator - expected + or |');
50 } until (pos == length) }; # accept trailing whitespace
51 if ($nested and pos == length) {
53 $self->_blam("No closing ) found for opening (");
55 return $match[0] if (@match == 1);
56 return match_and(@match);
60 sub _parse_spec_combinator {
61 my ($self, $spec, $match) = @_;
71 push @match, $self->_parse_spec_section($_)
72 or $self->_blam("Unable to work out what the next section is");
73 last PARSE if (pos == length);
74 last PARSE unless /\G\|/gc; # give up when next thing isn't |
75 } until (pos == length) }; # accept trailing whitespace
76 return match_or(@match);
82 sub _parse_spec_section {
86 # GET POST PUT HEAD ...
89 return $self->_http_method_match($_, $1);
94 return $self->_url_path_match($_);
99 return $self->_url_extension_match($_, $1);
104 return $self->_parse_spec($_, pos);
110 my $match = $self->_parse_spec_section($_);
112 return {} unless $match->(@_);
119 return $self->_parse_param_handler($_, 'query');
123 return $self->_parse_param_handler($_, 'body');
125 return; # () will trigger the blam in our caller
128 sub _http_method_match {
129 my ($self, $str, $method) = @_;
130 match_method($method);
133 sub _url_path_match {
138 PATH: while (/\G\//gc) {
144 push @path, $self->_url_path_segment_match($_)
145 or $self->_blam("Couldn't parse path match segment");
147 if (@path && !$end) {
148 length and $_ .= '(?:\.\w+)?' for $path[-1];
150 my $re = '^('.join('/','',@path).')'.$end.'$';
153 return match_path_strip($re);
155 return match_path($re);
161 sub _url_path_segment_match {
164 # trailing / -> require / on end of URL
165 /\G(?:(?=[+|\)])|$)/gc and
167 # word chars only -> exact path part match
170 # ** -> capture unlimited path parts
173 # * -> capture path part
180 sub _url_extension_match {
181 my ($self, $str, $extension) = @_;
182 if ($extension eq '*') {
184 if ((my $tmp = shift->{PATH_INFO}) =~ s/\.(\w+)$//) {
185 ({ PATH_INFO => $tmp }, $1);
192 if ((my $tmp = shift->{PATH_INFO}) =~ s/\.\Q${extension}\E$//) {
193 ({ PATH_INFO => $tmp });
201 sub _parse_param_handler {
202 my ($self, $spec, $type) = @_;
204 require Web::Simple::ParamParser;
205 my $unpacker = Web::Simple::ParamParser->can("get_unpacked_${type}_from");
208 my (@required, @single, %multi, $star, $multistar, %positional, $have_kw);
214 my $is_kw = /\G\:/gc;
218 my $multi = /\G\@/gc;
224 $self->_blam("* is always named; no need to supply :") if $is_kw;
226 $multi ? ($multistar = 1) : ($star = 1);
230 if ($star && $multistar) {
231 $self->_blam("Can't use * and \@* in the same parameter match");
235 # @foo= or foo= or @foo~ or foo~
237 /\G(\w+)/gc or $self->_blam('Expected parameter name');
241 # check for = or ~ on the end
244 ? push(@required, $name)
245 : (/\G\~/gc or $self->_blam('Expected = or ~ after parameter name'));
247 # record the key in the right category depending on the multi (@) flag
249 $multi ? ($multi{$name} = 1) : (push @single, $name);
251 # record positional or keyword
253 $is_kw ? ($have_kw = 1) : ($positional{$name} = $pos_idx++);
258 my $raw = $unpacker->($_[0]);
259 foreach my $name (@required) {
260 return unless exists $raw->{$name};
263 my @p = (undef) x $pos_idx;
267 ? (grep { !exists $multi{$_} } keys %$raw)
271 if (exists $raw->{$name}) {
272 if (exists $positional{$name}) {
273 $p[$positional{$name}] = $raw->{$name}->[-1];
275 $p{$name} = $raw->{$name}->[-1];
283 ? (grep { !exists $done{$_} && !exists $multi{$_} } keys %$raw)
287 if (exists $positional{$name}) {
288 $p[$positional{$name}] = $raw->{$name}||[];
290 $p{$name} = $raw->{$name}||[];
293 $p[$pos_idx] = \%p if $have_kw;