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 match_true() 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 {
135 my (@path, @names, $seen_nameless);
138 PATH: while (/\G\//gc) {
145 my ($segment) = $self->_url_path_segment_match($_)
146 or $self->_blam("Couldn't parse path match segment");
149 ($segment, $keep_dot, my $name) = @$segment;
150 if (defined($name)) {
151 $self->_blam("Can't mix positional and named captures in path match")
155 $self->_blam("Can't mix positional and named captures in path match")
160 push @path, $segment;
170 last PATH if $keep_dot;
172 if (@path && !$end && !$keep_dot) {
173 length and $_ .= '(?:\.\w+)?' for $path[-1];
175 my $re = '^('.join('/','',@path).')'.$end.'$';
178 return match_path_strip($re, @names ? \@names : ());
180 return match_path($re, @names ? \@names : ());
186 sub _url_path_segment_match {
189 # trailing / -> require / on end of URL
190 /\G(?:(?=[+|\)])|$)/gc and
192 # word chars only -> exact path part match
195 (?: # start matching at a space followed by:
196 [\w\-] # word chars or dashes
199 (?!\.) # not followed by another period
201 + # then grab as far as possible
205 # ** -> capture unlimited path parts
206 /\G\*\*(?:(\.\*)?\:(\w+))?/gc and
207 return [ '(.*?[^/])', $1, $2 ];
208 # * -> capture path part
209 # *:name -> capture named path part
210 /\G\*(?:(\.\*)?\:(\w+))?/gc and
211 return [ '([^/]+?)', $1, $2 ];
213 # :name -> capture named path part
215 return [ '([^/]+?)', 0, $1 ];
220 sub _parse_param_handler {
221 my ($self, $spec, $type) = @_;
224 my (@required, @single, %multi, $star, $multistar, %positional, $have_kw);
231 my $is_kw = /\G\:/gc;
235 my $multi = /\G\@/gc;
241 $self->_blam("* is always named; no need to supply :") if $is_kw;
244 $self->_blam("Can only use one * or \@* in a parameter match");
247 $spec{star} = { multi => $multi };
250 # @foo= or foo= or @foo~ or foo~
252 /\G([\w.]*)/gc or $self->_blam('Expected parameter name');
256 # check for = or ~ on the end
259 ? push(@{$spec{required}||=[]}, $name)
260 : (/\G\~/gc or $self->_blam('Expected = or ~ after parameter name'));
262 # record positional or keyword
264 push @{$spec{$is_kw ? 'named' : 'positional'}||=[]},
265 { name => $name, multi => $multi };
269 return Web::Dispatch::Predicates->can("match_${type}")->(\%spec);