-
Notifications
You must be signed in to change notification settings - Fork 3
/
grammar.pegjs
185 lines (148 loc) · 3.65 KB
/
grammar.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
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
/*
GraphQL Grammar
*/
{
var extend = require('extend')
}
GraphQL
= ws value:definitionOrCall* ws { return value; }
ws "whitespace" = [ \t\n\r]*
beginObject = ws "{" ws
endObject = ws "}" ws
valueSeparator = ws "," ws
callSeparator = ws "." ws
paramNameSeparator = ws ":" ws
// Values
value = false
/ true
/ null
/ queryParam
/ number
/ string
/ name // for backwards compatibility of strings without using quotes
/ undefined
nameOrUndf = name
/ undefined
undefined = "" { return undefined; }
false = "false" { return false; }
null = "null" { return null; }
true = "true" { return true; }
// Numbers
number "number"
= minus? int frac? exp? { return parseFloat(text()); }
decimal_point = "."
digit1_9 = [1-9]
e = [eE]
exp = e (minus / plus)? DIGIT+
frac = decimal_point DIGIT+
int = zero / (digit1_9 DIGIT*)
minus = "-"
plus = "+"
zero = "0"
// Strings
name "name"
= chars:nameChar+ {
var str = chars.join("");
var num = parseInt(str);
if (!isNaN(num) && num.toString() === str) return num;
else return str;
}
nameChar = [0-9a-zA-Z/_-]
string "string"
= quotation_mark chars:char* quotation_mark { return chars.join(""); }
char
= unescaped
/ escape
sequence:(
'"'
/ "\\"
/ "/"
/ "b" { return "\b"; }
/ "f" { return "\f"; }
/ "n" { return "\n"; }
/ "r" { return "\r"; }
/ "t" { return "\t"; }
/ "u" digits:$(HEXDIG HEXDIG HEXDIG HEXDIG) {
return String.fromCharCode(parseInt(digits, 16));
}
)
{ return sequence; }
escape = "\\"
quotation_mark = '"'
unescaped = [\x20-\x21\x23-\x5B\x5D-\u10FFFF]
/* ----- Core ABNF Rules ----- */
/* See RFC 4234, Appendix B (http://tools.ietf.org/html/rfc4627). */
DIGIT = [0-9]
HEXDIG = [0-9a-f]i
// Objects
object
= beginObject
members:(
first:member
rest:(valueSeparator m:member { return m; })*
{
var result = {}, i, newData;
if (first.name !== undefined) {
newData = {};
newData[first.name] = first.value || true;
extend(true, result, newData);
}
for (i = 0; i < rest.length; i++) {
if (rest[i].name !== undefined) {
newData = {};
newData[rest[i].name] = rest[i].value || true;
extend(true, result, newData);
}
}
return result;
}
)?
endObject
{ return members !== null ? members: {}; }
member
= name:name ws value:childDefinitionOrCall {
return { name: name, value: value };
}
/ name:nameOrUndf {
return { name: name };
}
// Definitions
definition "definition"
= type:nameOrUndf
members:object {
return { type: type, fields: members }
}
childDefinition
= members:object {
return { fields: members }
}
// Calls
params
= (first:param
rest:(valueSeparator m:param { return m; })*
{
var result = {}, i;
result[first.name] = first.value;
for (i = 0; i < rest.length; i++) {
result[rest[i].name] = rest[i].value;
}
return result;
})?
param
= name:name paramNameSeparator value:value {
return { name: name, value: value };
}
call
= ws type:name ws "(" params:params ")" members:object {
return { type: type, params: params || {}, fields: members }
}
childCall
= ws "(" params:params ")" members:object {
return { params: params || {}, fields: members }
}
// Query Params
queryParam = "<" name:name ">" {
return { type: "queryParam", name: name }
}
definitionOrCall = definition / call
childDefinitionOrCall = childDefinition / childCall