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 {
87 # GET POST PUT HEAD ...
90 return match_method($1);
95 return $self->_url_path_match($_);
100 return match_extension($1);
105 return $self->_parse_spec($_, pos);
110 return match_not($self->_parse_spec_section($_));
114 return $self->_parse_param_handler($_, 'query');
118 return $self->_parse_param_handler($_, 'body');
122 return $self->_parse_param_handler($_, 'uploads');
124 return; # () will trigger the blam in our caller
127 sub _url_path_match {
133 PATH: while (/\G\//gc) {
139 push @path, $self->_url_path_segment_match($_)
140 or $self->_blam("Couldn't parse path match segment");
147 if (@path && !$end && !$keep_dot) {
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 _parse_param_handler {
181 my ($self, $spec, $type) = @_;
184 my (@required, @single, %multi, $star, $multistar, %positional, $have_kw);
191 my $is_kw = /\G\:/gc;
195 my $multi = /\G\@/gc;
201 $self->_blam("* is always named; no need to supply :") if $is_kw;
204 $self->_blam("Can only use one * or \@* in a parameter match");
207 $spec{star} = { multi => $multi };
210 # @foo= or foo= or @foo~ or foo~
212 /\G(\w+)/gc or $self->_blam('Expected parameter name');
216 # check for = or ~ on the end
219 ? push(@{$spec{required}||=[]}, $name)
220 : (/\G\~/gc or $self->_blam('Expected = or ~ after parameter name'));
222 # record positional or keyword
224 push @{$spec{$is_kw ? 'named' : 'positional'}||=[]},
225 { name => $name, multi => $multi };
229 return Web::Dispatch::Predicates->can("match_${type}")->(\%spec);