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