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) = @_;
38 return sub { {} } unless length($spec);
42 push @match, $self->_parse_spec_section($_)
43 or $self->_blam("Unable to work out what the next section is");
45 $self->_blam("Found closing ) with no opening (") unless $nested;
48 last PARSE if (pos == length);
49 $match[-1] = $self->_parse_spec_combinator($_, $match[-1])
50 or $self->_blam('No valid combinator - expected + or |');
51 } until (pos == length) }; # accept trailing whitespace
52 if ($nested and pos == length) {
54 $self->_blam("No closing ) found for opening (");
56 return $match[0] if (@match == 1);
57 return match_and(@match);
61 sub _parse_spec_combinator {
62 my ($self, $spec, $match) = @_;
72 push @match, $self->_parse_spec_section($_)
73 or $self->_blam("Unable to work out what the next section is");
74 last PARSE if (pos == length);
75 last PARSE unless /\G\|/gc; # give up when next thing isn't |
76 } until (pos == length) }; # accept trailing whitespace
77 return match_or(@match);
83 sub _parse_spec_section {
90 return match_path('^$');
92 # GET POST PUT HEAD ...
95 return match_method($1);
100 return $self->_url_path_match($_);
105 return match_extension($1);
110 return $self->_parse_spec($_, pos);
115 return match_not($self->_parse_spec_section($_));
119 return $self->_parse_param_handler($_, 'query');
123 return $self->_parse_param_handler($_, 'body');
127 return $self->_parse_param_handler($_, 'uploads');
129 return; # () will trigger the blam in our caller
132 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");
157 if (@path && !$end && !$keep_dot) {
158 length and $_ .= '(?:\.\w+)?' for $path[-1];
160 my $re = '^('.join('/','',@path).')'.$end.'$';
163 return match_path_strip($re);
165 return match_path($re);
171 sub _url_path_segment_match {
174 # trailing / -> require / on end of URL
175 /\G(?:(?=[+|\)])|$)/gc and
177 # word chars only -> exact path part match
180 (?: # start matching at a space followed by:
181 [\w\-] # word chars or dashes
184 (?!\.) # not followed by another period
186 + # then grab as far as possible
190 # ** -> capture unlimited path parts
193 # * -> capture path part
200 sub _parse_param_handler {
201 my ($self, $spec, $type) = @_;
204 my (@required, @single, %multi, $star, $multistar, %positional, $have_kw);
211 my $is_kw = /\G\:/gc;
215 my $multi = /\G\@/gc;
221 $self->_blam("* is always named; no need to supply :") if $is_kw;
224 $self->_blam("Can only use one * or \@* in a parameter match");
227 $spec{star} = { multi => $multi };
230 # @foo= or foo= or @foo~ or foo~
232 /\G(\w+)/gc or $self->_blam('Expected parameter name');
236 # check for = or ~ on the end
239 ? push(@{$spec{required}||=[]}, $name)
240 : (/\G\~/gc or $self->_blam('Expected = or ~ after parameter name'));
242 # record positional or keyword
244 push @{$spec{$is_kw ? 'named' : 'positional'}||=[]},
245 { name => $name, multi => $multi };
249 return Web::Dispatch::Predicates->can("match_${type}")->(\%spec);