forked from grncdr/uri-template
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.pegjs
46 lines (36 loc) · 932 Bytes
/
index.pegjs
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
{
var cls = require('./lib/classes')
var Template = cls.Template
var expression = cls.expression
}
uriTemplate
= pieces:(nonexpression / expression)* { return new Template(pieces) }
expression
= '{' op:op params:paramList '}' { return expression(op, params) }
op
= [/;:.?&+#] / ''
pathExpression
= "{/"
paramList
= hd:param rst:(',' p:param { return p; })* { rst.unshift(hd); return rst; }
param
= chars:[a-zA-Z0-9_.%]+ clm:(cut / listMarker)? e:extension?
{ clm = clm || {};
return {
name: chars.join(''),
explode: clm.listMarker,
cut: clm.cut,
extended: e
} }
cut
= cut:substr
{ return {cut: cut}; }
listMarker
= listMarker:'*'
{ return {listMarker: listMarker}; }
substr
= ':' digits:[0-9]+ { return parseInt(digits.join('')) }
nonexpression
= chars:[^{]+ { return chars.join(''); }
extension
= '(' chars:[^)]+ ')' { return chars.join('') }