doc patch for '/foo...'
[catagits/Web-Simple.git] / lib / Web / Dispatch / Parser.pm
1 package Web::Dispatch::Parser;
2
3 sub DEBUG () { 0 }
4
5 BEGIN {
6   if ($ENV{WEB_DISPATCH_PARSER_DEBUG}) {
7     no warnings 'redefine';
8     *DEBUG = sub () { 1 }
9   }
10 }
11
12 use Sub::Quote;
13 use Web::Dispatch::Predicates;
14 use Moo;
15
16 has _cache => (
17   is => 'lazy', default => quote_sub q{ {} }
18 );
19
20 sub diag { if (DEBUG) { warn $_[0] } }
21
22 sub _blam {
23   my ($self, $error) = @_;
24   my $hat = (' ' x (pos||0)).'^';
25   die "Error parsing dispatch specification: ${error}\n
26 ${_}
27 ${hat} here\n";
28 }
29
30 sub parse {
31   my ($self, $spec) = @_;
32   $spec =~ s/\s+//g; # whitespace is not valid
33   return $self->_cache->{$spec} ||= $self->_parse_spec($spec);
34 }
35
36 sub _parse_spec {
37   my ($self, $spec, $nested) = @_;
38   return sub { {} } unless length($spec);
39   for ($_[1]) {
40     my @match;
41     PARSE: { do {
42       push @match, $self->_parse_spec_section($_)
43         or $self->_blam("Unable to work out what the next section is");
44       if (/\G\)/gc) {
45         $self->_blam("Found closing ) with no opening (") unless $nested;
46         last PARSE;
47       }
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) {
53       pos = $nested - 1;
54       $self->_blam("No closing ) found for opening (");
55     }
56     return $match[0] if (@match == 1);
57     return match_and(@match);
58   }
59 }
60
61 sub _parse_spec_combinator {
62   my ($self, $spec, $match) = @_;
63   for ($_[1]) {
64
65     /\G\+/gc and
66       return $match;
67
68     /\G\|/gc and
69       return do {
70         my @match = $match;
71         PARSE: { do {
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);
78       };
79   }
80   return;
81 }
82
83 sub _parse_spec_section {
84   my ($self) = @_;
85   for ($_[1]) {
86
87     # ~
88
89     /\G~/gc and
90       return match_path('^$');
91
92     # GET POST PUT HEAD ...
93
94     /\G([A-Z]+)/gc and
95       return match_method($1);
96
97     # /...
98
99     /\G(?=\/)/gc and
100       return $self->_url_path_match($_);
101
102     # .* and .html
103
104     /\G\.(\*|\w+)/gc and
105       return match_extension($1);
106
107     # (...)
108
109     /\G\(/gc and
110       return $self->_parse_spec($_, pos);
111
112     # !something
113
114     /\G!/gc and
115       return match_not($self->_parse_spec_section($_));
116
117     # ?<param spec>
118     /\G\?/gc and
119       return $self->_parse_param_handler($_, 'query');
120
121     # %<param spec>
122     /\G\%/gc and
123       return $self->_parse_param_handler($_, 'body');
124
125     # *<param spec>
126     /\G\*/gc and
127       return $self->_parse_param_handler($_, 'uploads');
128   }
129   return; # () will trigger the blam in our caller
130 }
131
132 sub _url_path_match {
133   my ($self) = @_;
134   for ($_[1]) {
135     my @path;
136     my $end = '';
137     my $keep_dot;
138     PATH: while (/\G\//gc) {
139       /\G\.\.\./gc
140         and do {
141           $end = '(/.*)';
142           last PATH;
143         };
144       push @path, $self->_url_path_segment_match($_)
145         or $self->_blam("Couldn't parse path match segment");
146       /\G\.\.\./gc
147         and do {
148           $end = '(.*)';
149           last PATH;
150         };
151       /\G\.\*/gc
152         and do {
153           $keep_dot = 1;
154           last PATH;
155         };
156     }
157     if (@path && !$end && !$keep_dot) {
158       length and $_ .= '(?:\.\w+)?' for $path[-1];
159     }
160     my $re = '^('.join('/','',@path).')'.$end.'$';
161     $re = qr/$re/;
162     if ($end) {
163       return match_path_strip($re);
164     } else {
165       return match_path($re);
166     }
167   }
168   return;
169 }
170
171 sub _url_path_segment_match {
172   my ($self) = @_;
173   for ($_[1]) {
174     # trailing / -> require / on end of URL
175     /\G(?:(?=[+|\)])|$)/gc and
176       return '';
177     # word chars only -> exact path part match
178     /
179         \G(
180             (?:             # start matching at a space followed by:
181                     [\w\-]  # word chars or dashes
182                 |           # OR
183                     \.      # a period
184                     (?!\.)  # not followed by another period
185             )
186             +               # then grab as far as possible
187         )
188     /gcx and
189       return "\Q$1";
190     # ** -> capture unlimited path parts
191     /\G\*\*/gc and
192       return '(.*?[^/])';
193     # * -> capture path part
194     /\G\*/gc and
195       return '([^/]+?)';
196   }
197   return ();
198 }
199
200 sub _parse_param_handler {
201   my ($self, $spec, $type) = @_;
202
203   for ($_[1]) {
204     my (@required, @single, %multi, $star, $multistar, %positional, $have_kw);
205     my %spec;
206     my $pos_idx = 0;
207     PARAM: { do {
208
209       # ?:foo or ?@:foo
210
211       my $is_kw = /\G\:/gc;
212
213       # ?@foo or ?@*
214
215       my $multi = /\G\@/gc;
216
217       # @* or *
218
219       if (/\G\*/gc) {
220
221         $self->_blam("* is always named; no need to supply :") if $is_kw;
222
223         if ($star) {
224           $self->_blam("Can only use one * or \@* in a parameter match");
225         }
226
227         $spec{star} = { multi => $multi };
228       } else {
229
230         # @foo= or foo= or @foo~ or foo~
231
232         /\G(\w+)/gc or $self->_blam('Expected parameter name');
233
234         my $name = $1;
235
236         # check for = or ~ on the end
237
238         /\G\=/gc
239           ? push(@{$spec{required}||=[]}, $name)
240           : (/\G\~/gc or $self->_blam('Expected = or ~ after parameter name'));
241
242         # record positional or keyword
243
244         push @{$spec{$is_kw ? 'named' : 'positional'}||=[]},
245           { name => $name, multi => $multi };
246       }
247     } while (/\G\&/gc) }
248
249     return Web::Dispatch::Predicates->can("match_${type}")->(\%spec);
250   }
251 }
252
253 1;