/
rfc3986.m
248 lines (203 loc) · 6.6 KB
/
rfc3986.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
% Bower - a frontend for the Notmuch email system
% Copyright (C) 2015 Peter Wang
:- module rfc3986.
:- interface.
:- import_module char.
:- import_module maybe.
:- type uri_components
---> uri_components(
% All strings are left in percent-encoded form.
scheme :: maybe(string),
authority :: maybe(string),
path :: string,
query :: maybe(string),
fragment :: maybe(string)
).
:- pred split_uri(string::in, uri_components::out) is semidet.
:- pred valid_uri_char(char::in) is semidet.
:- pred unreserved_char(char::in) is semidet.
%-----------------------------------------------------------------------------%
%-----------------------------------------------------------------------------%
:- implementation.
:- import_module pair.
:- import_module parsing_utils.
:- import_module string.
:- import_module unit.
%-----------------------------------------------------------------------------%
% Emulate the regular expression from Appendix B:
% (or we could just use it...)
%
% ^(([^:/?#]+):)?(//([^/?#]*))?([^?#]*)(\?([^#]*))?(#(.*))?
split_uri(Input, URI) :-
promise_equivalent_solutions [Result] (
parsing_utils.parse(Input, no_skip_whitespace, uri, Result)
),
Result = ok(URI).
:- pred no_skip_whitespace(src::in, unit::out, ps::in, ps::out) is semidet.
no_skip_whitespace(_Src, unit, !PS) :-
semidet_true.
:- pred uri(src::in, uri_components::out, ps::in, ps::out) is semidet.
uri(Src, URI, !PS) :-
optional(scheme, Src, Scheme, !PS),
optional(authority, Src, Authority, !PS),
optional(path_and_query, Src, PathAndQuery, !PS),
optional(fragment, Src, Fragment, !PS),
eof(Src, _, !PS),
(
PathAndQuery = yes(Path - Query)
;
PathAndQuery = no,
Path = "",
Query = no
),
URI = uri_components(Scheme, Authority, Path, Query, Fragment).
:- pred scheme(src::in, string::out, ps::in, ps::out) is semidet.
scheme(Src, Scheme, !PS) :-
one_or_more(scheme_char, Src, Chars, !PS),
next_char(Src, ':', !PS),
string.from_char_list(Chars, Scheme).
:- pred scheme_char(src::in, char::out, ps::in, ps::out) is semidet.
scheme_char(Src, Char, !PS) :-
valid_char(Src, Char, !PS),
Char \= (':'),
Char \= ('/'),
Char \= ('?'),
Char \= ('#').
:- pred authority(src::in, string::out, ps::in, ps::out) is semidet.
authority(Src, Authority, !PS) :-
next_char(Src, '/', !PS),
next_char(Src, '/', !PS),
zero_or_more(authority_char, Src, Chars, !PS),
string.from_char_list(Chars, Authority).
:- pred authority_char(src::in, char::out, ps::in, ps::out) is semidet.
authority_char(Src, Char, !PS) :-
valid_char(Src, Char, !PS),
Char \= ('/'),
Char \= ('?'),
Char \= ('#').
:- pred path_and_query(src::in, pair(string, maybe(string))::out,
ps::in, ps::out) is semidet.
path_and_query(Src, Path - Query, !PS) :-
path(Src, Path, !PS),
optional(query, Src, Query, !PS).
:- pred path(src::in, string::out, ps::in, ps::out) is semidet.
path(Src, Path, !PS) :-
zero_or_more(path_char, Src, PathChars, !PS),
string.from_char_list(PathChars, Path).
:- pred path_char(src::in, char::out, ps::in, ps::out) is semidet.
path_char(Src, Char, !PS) :-
valid_char(Src, Char, !PS),
Char \= ('?'),
Char \= ('#').
:- pred query(src::in, string::out, ps::in, ps::out) is semidet.
query(Src, Query, !PS) :-
next_char(Src, '?', !PS),
zero_or_more(query_char, Src, Chars, !PS),
string.from_char_list(Chars, Query).
:- pred query_char(src::in, char::out, ps::in, ps::out) is semidet.
query_char(Src, Char, !PS) :-
valid_char(Src, Char, !PS),
Char \= ('#').
:- pred fragment(src::in, string::out, ps::in, ps::out) is semidet.
fragment(Src, Fragment, !PS) :-
next_char(Src, '#', !PS),
zero_or_more(fragment_char, Src, Chars, !PS),
string.from_char_list(Chars, Fragment).
:- pred fragment_char(src::in, char::out, ps::in, ps::out) is semidet.
fragment_char(Src, Char, !PS) :-
valid_char(Src, Char, !PS).
:- pred valid_char(src::in, char::out, ps::in, ps::out) is semidet.
valid_char(Src, C, !PS) :-
next_char(Src, C, !PS),
valid_uri_char(C).
%-----------------------------------------------------------------------------%
valid_uri_char('!'). % sub-delims
valid_uri_char('*'). % sub-delims
valid_uri_char(''''). % sub-delims
valid_uri_char('('). % sub-delims
valid_uri_char(')'). % sub-delims
valid_uri_char(';'). % sub-delims
valid_uri_char(':'). % gen-delims
valid_uri_char('@'). % gen-delims
valid_uri_char('&'). % sub-delims
valid_uri_char('='). % sub-delims
valid_uri_char('+'). % sub-delims
valid_uri_char('$'). % sub-delims
valid_uri_char(','). % sub-delims
valid_uri_char('/'). % gen-delims
valid_uri_char('?'). % gen-delims
valid_uri_char('#'). % gen-delims
valid_uri_char('['). % gen-delims
valid_uri_char(']'). % gen-delims
valid_uri_char('%'). % percent-encoding
valid_uri_char(C) :-
unreserved_char(C).
%-----------------------------------------------------------------------------%
unreserved_char('A').
unreserved_char('B').
unreserved_char('C').
unreserved_char('D').
unreserved_char('E').
unreserved_char('F').
unreserved_char('G').
unreserved_char('H').
unreserved_char('I').
unreserved_char('J').
unreserved_char('K').
unreserved_char('L').
unreserved_char('M').
unreserved_char('N').
unreserved_char('O').
unreserved_char('P').
unreserved_char('Q').
unreserved_char('R').
unreserved_char('S').
unreserved_char('T').
unreserved_char('U').
unreserved_char('V').
unreserved_char('W').
unreserved_char('X').
unreserved_char('Y').
unreserved_char('Z').
unreserved_char('a').
unreserved_char('b').
unreserved_char('c').
unreserved_char('d').
unreserved_char('e').
unreserved_char('f').
unreserved_char('g').
unreserved_char('h').
unreserved_char('i').
unreserved_char('j').
unreserved_char('k').
unreserved_char('l').
unreserved_char('m').
unreserved_char('n').
unreserved_char('o').
unreserved_char('p').
unreserved_char('q').
unreserved_char('r').
unreserved_char('s').
unreserved_char('t').
unreserved_char('u').
unreserved_char('v').
unreserved_char('w').
unreserved_char('x').
unreserved_char('y').
unreserved_char('z').
unreserved_char('0').
unreserved_char('1').
unreserved_char('2').
unreserved_char('3').
unreserved_char('4').
unreserved_char('5').
unreserved_char('6').
unreserved_char('7').
unreserved_char('8').
unreserved_char('9').
unreserved_char('-').
unreserved_char('.').
unreserved_char('_').
unreserved_char('~').
%-----------------------------------------------------------------------------%
% vim: ft=mercury ts=4 sts=4 sw=4 et