Skip to content

Commit 40cc284

Browse files
committed
Tools(GeekSpecDSL): Add DSL Parser
1 parent 8ab44a6 commit 40cc284

File tree

1 file changed

+78
-0
lines changed

1 file changed

+78
-0
lines changed

geekspec_dsl_parser.pegjs

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
{
2+
let filterIsNotArray = (xs => xs.filter(x => !Array.isArray(x)));
3+
let isNotUndef = (s => s != 'undefined');
4+
let head = xs => xs[0]
5+
}
6+
7+
GeekSpec = specs:(_ ApiSpec _)* { return specs.map(x => head(filterIsNotArray(x))); }
8+
9+
entityName = letter+
10+
interfaceName = letter+
11+
urlPath "url letters" = (letter / [:/.{}?&=%])+
12+
argName = letter+
13+
possibleValue = letter+
14+
fieldName = letter+
15+
16+
httpMethod
17+
= "GET" / "POST"
18+
/ "PUT" / "DELETE"
19+
/ "PATCH" / "OPTIONS"
20+
/ "HEAD"
21+
22+
ApiSpec =
23+
name:interfaceName _ '(' _ a:Arg? as:(_ "," _ Arg)* _ ')'
24+
_ rtxd:('->' _ (ReturnType / Dict))? _ '=' _ url:urlPath {
25+
return {
26+
method: "GET",
27+
name: name.join(''),
28+
args: a == null ? [] : [a].concat(as.map(ra => ra[3])),
29+
return : isNotUndef(typeof rtxd) ? (rtxd == null ? null : rtxd[2]) : null,
30+
url: url.join('')
31+
};
32+
}
33+
/
34+
method:httpMethod '@' name:interfaceName _ '(' _ a:Arg? as:(_ "," _ Arg)* _ ')'
35+
_ rtxd:('->' _ (ReturnType / Dict))? _ '=' _ url:urlPath {
36+
return {
37+
method: method,
38+
name: name.join(''),
39+
args: a == null ? [] : [a].concat(as.map(ra => ra[3])),
40+
return : isNotUndef(typeof rtxd) ? rtxd[2] : null,
41+
url: url.join('')
42+
};
43+
}
44+
45+
Arg = name:argName _ q:('?' / "!"?) _ ov:OptionVals? {
46+
return {
47+
name: name.join(''),
48+
required: q != "?",
49+
options: ov
50+
};
51+
}
52+
53+
OptionVals = '{' _ pv:possibleValue? pvs:(_ ',' _ possibleValue)* _ '}' {
54+
return pv == null ? [] : [pv.join('')].concat(pvs.map(rpv => rpv[3].join('')));
55+
}
56+
57+
ReturnType
58+
= axo:("array" / "object") ':' a:Atom { return { type: axo, of: ((typeof a != 'string') ? a.join('') : a) }; }
59+
60+
Atom = "boolean" / "number"
61+
/ "string" / "datetime"
62+
/ "plain" / entityName
63+
64+
Dict = '[' _ di:DictItem? dis:(_ ',' _ DictItem)* _ ']' {
65+
return di == null ? [] : [di].concat(dis.map(rd => rd[3]))
66+
}
67+
68+
DictItem
69+
= ReturnType
70+
/ '$' name:fieldName ':' rt:ReturnType {
71+
return { name: name.join(''), type: rt };
72+
}
73+
74+
_ "whitespace"
75+
= [ \t\n\r]*
76+
77+
letter "letter"
78+
= [A-Za-z0-9_\-]

0 commit comments

Comments
 (0)