Skip to content

Commit 49e1cee

Browse files
style: format
1 parent c98a647 commit 49e1cee

File tree

1 file changed

+92
-126
lines changed

1 file changed

+92
-126
lines changed

grammar.js

Lines changed: 92 additions & 126 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ const PREC = {
88
GRAPQL_JSON_PREFIX: 4,
99
COMMENT_PREFIX: 5,
1010
REQ_SEPARATOR: 9,
11-
}
11+
};
1212

1313
const WORD_CHAR = /[\p{L}\p{N}]/u;
1414
const PUNCTUATION = /[^\n\r\p{Z}\p{L}\p{N}]/u;
@@ -17,14 +17,8 @@ const NL = token(choice("\n", "\r", "\r\n", "\0"));
1717
const LINE_TAIL = token(seq(/.*/, NL));
1818
const ESCAPED = token(/\\[^\n\r]/);
1919
const COMMENT_PREFIX = token(
20-
prec(
21-
PREC.COMMENT_PREFIX,
22-
choice(
23-
/#\s*/,
24-
/\/\/\s*/,
25-
)
26-
)
27-
)
20+
prec(PREC.COMMENT_PREFIX, choice(/#\s*/, /\/\/\s*/)),
21+
);
2822

2923
module.exports = grammar({
3024
name: "http",
@@ -39,10 +33,7 @@ module.exports = grammar({
3933
inline: ($) => [$._target_url_line],
4034

4135
rules: {
42-
document: ($) =>
43-
repeat(
44-
$.section,
45-
),
36+
document: ($) => repeat($.section),
4637
// NOTE: just for debugging purpose
4738
WORD_CHAR: (_) => WORD_CHAR,
4839
PUNCTUATION: (_) => PUNCTUATION,
@@ -94,13 +85,12 @@ module.exports = grammar({
9485
),
9586

9687
section: ($) =>
97-
prec.right(choice(
98-
seq(
99-
$.request_separator,
100-
optional($._section_content),
88+
prec.right(
89+
choice(
90+
seq($.request_separator, optional($._section_content)),
91+
$._section_content,
10192
),
102-
$._section_content,
103-
)),
93+
),
10494

10595
// NOTE: grammatically, each request section should contain only single `$.request` node
10696
// we are allowing multiple `$.request` nodes here to lower the parser size
@@ -123,73 +113,66 @@ module.exports = grammar({
123113
http_version: (_) => token(prec(0, /HTTP\/[\d\.]+/)),
124114

125115
_target_url_line: ($) =>
126-
repeat1(
127-
choice(
128-
WORD_CHAR,
129-
PUNCTUATION,
130-
$.variable,
131-
),
132-
),
116+
repeat1(choice(WORD_CHAR, PUNCTUATION, $.variable)),
133117
target_url: ($) =>
134-
seq(
135-
$._target_url_line,
136-
repeat(
137-
seq(
138-
NL,
139-
WS,
140-
$._target_url_line,
141-
),
142-
),
143-
),
118+
seq($._target_url_line, repeat(seq(NL, WS, $._target_url_line))),
144119

145120
status_code: (_) => /[1-5]\d{2}/,
146121
status_text: (_) =>
147122
/(Continue|Switching Protocols|Processing|OK|Created|Accepted|Non-Authoritative Information|No Content|Reset Content|Partial Content|Multi-Status|Already Reported|IM Used|Multiple Choices|Moved Permanently|Found|See Other|Not Modified|Use Proxy|Switch Proxy|Temporary Redirect|Permanent Redirect|Bad Request|Unauthorized|Payment Required|Forbidden|Not Found|Method Not Allowed|Not Acceptable|Proxy Authentication Required|Request Timeout|Conflict|Gone|Length Required|Precondition Failed|Payload Too Large|URI Too Long|Unsupported Media Type|Range Not Satisfiable|Expectation Failed|I'm a teapot|Misdirected Request|Unprocessable Entity|Locked|Failed Dependency|Too Early|Upgrade Required|Precondition Required|Too Many Requests|Request Header Fields Too Large|Unavailable For Legal Reasons|Internal Server Error|Not Implemented|Bad Gateway|Service Unavailable|Gateway Timeout|HTTP Version Not Supported|Variant Also Negotiates|Insufficient Storage|Loop Detected|Not Extended|Network Authentication Required)/,
148123
response: ($) =>
149-
seq($.http_version, WS, $.status_code, WS, optional($.status_text),
150-
NL),
124+
seq(
125+
$.http_version,
126+
WS,
127+
$.status_code,
128+
WS,
129+
optional($.status_text),
130+
NL,
131+
),
151132

152133
request: ($) =>
153-
prec.right(seq(
154-
optional(seq(field("method", $.method), WS)),
155-
field("url", $.target_url),
156-
optional(seq(WS, field("version", $.http_version))),
157-
NL,
158-
repeat($.comment),
159-
optional($.response),
160-
repeat(field("header", $.header)),
161-
optional(
162-
seq(
163-
repeat1($._blank_line),
164-
prec.right(repeat(
165-
choice(
166-
alias($.var_comment, $.comment),
167-
field("body", choice(
168-
$.raw_body,
169-
$.multipart_form_data,
170-
$.xml_body,
171-
$.json_body,
172-
$.graphql_body,
173-
$._external_body,
174-
)),
175-
NL,
176-
$.res_handler_script,
134+
prec.right(
135+
seq(
136+
optional(seq(field("method", $.method), WS)),
137+
field("url", $.target_url),
138+
optional(seq(WS, field("version", $.http_version))),
139+
NL,
140+
repeat($.comment),
141+
optional($.response),
142+
repeat(field("header", $.header)),
143+
optional(
144+
seq(
145+
repeat1($._blank_line),
146+
prec.right(
147+
repeat(
148+
choice(
149+
alias($.var_comment, $.comment),
150+
field(
151+
"body",
152+
choice(
153+
$.raw_body,
154+
$.multipart_form_data,
155+
$.xml_body,
156+
$.json_body,
157+
$.graphql_body,
158+
$._external_body,
159+
),
160+
),
161+
NL,
162+
$.res_handler_script,
163+
),
164+
),
177165
),
178-
)),
166+
),
179167
),
180168
),
181-
)),
169+
),
182170

183171
query_param: ($) =>
184172
prec.right(
185173
seq(
186174
field("key", $.value),
187-
optional(
188-
seq(
189-
"=",
190-
optional(field("value", $.value)),
191-
),
192-
),
175+
optional(seq("=", optional(field("value", $.value)))),
193176
),
194177
),
195178

@@ -199,11 +182,7 @@ module.exports = grammar({
199182
optional(WS),
200183
":",
201184
optional(token(prec(1, WS))),
202-
optional(
203-
field("value", choice(
204-
$.value,
205-
)),
206-
),
185+
optional(field("value", choice($.value))),
207186
NL,
208187
),
209188

@@ -220,8 +199,12 @@ module.exports = grammar({
220199
pre_request_script: ($) =>
221200
seq("<", WS, choice($.script, $.path), token(repeat1(NL))),
222201
res_handler_script: ($) =>
223-
seq(token(prec(PREC.REQ_SEPARATOR, ">")), WS, choice($.script, $.path),
224-
token(repeat1(NL))),
202+
seq(
203+
token(prec(PREC.REQ_SEPARATOR, ">")),
204+
WS,
205+
choice($.script, $.path),
206+
token(repeat1(NL)),
207+
),
225208
script: (_) =>
226209
seq(
227210
token(prec(1, "{%")),
@@ -242,37 +225,32 @@ module.exports = grammar({
242225
),
243226

244227
xml_body: ($) =>
245-
seq(
246-
token(prec(PREC.BODY_PREFIX, /<[^\s@]/)),
247-
$._raw_body,
248-
),
228+
seq(token(prec(PREC.BODY_PREFIX, /<[^\s@]/)), $._raw_body),
249229

250230
json_body: ($) =>
251-
seq(
252-
token(prec(PREC.BODY_PREFIX, /[{\[]\s+/)),
253-
$._raw_body,
254-
),
231+
seq(token(prec(PREC.BODY_PREFIX, /[{\[]\s+/)), $._raw_body),
255232

256233
graphql_body: ($) =>
257-
prec.right(seq($.graphql_data, optional(alias($.graphql_json_body, $.json_body)))),
234+
prec.right(
235+
seq(
236+
$.graphql_data,
237+
optional(alias($.graphql_json_body, $.json_body)),
238+
),
239+
),
258240
graphql_data: ($) =>
259241
seq(
260242
token(
261-
prec(PREC.BODY_PREFIX, seq(choice("query", "mutation"), WS, /.*\{/, NL)),
243+
prec(
244+
PREC.BODY_PREFIX,
245+
seq(choice("query", "mutation"), WS, /.*\{/, NL),
246+
),
262247
),
263248
$._raw_body,
264249
),
265250
graphql_json_body: ($) =>
266-
seq(
267-
token(prec(PREC.GRAPQL_JSON_PREFIX, /[{\[]\s+/)),
268-
$._raw_body,
269-
),
251+
seq(token(prec(PREC.GRAPQL_JSON_PREFIX, /[{\[]\s+/)), $._raw_body),
270252

271-
_external_body: ($) =>
272-
seq(
273-
$.external_body,
274-
NL,
275-
),
253+
_external_body: ($) => seq($.external_body, NL),
276254
external_body: ($) =>
277255
seq(
278256
token(prec(PREC.BODY_PREFIX, "<")),
@@ -282,22 +260,23 @@ module.exports = grammar({
282260
),
283261

284262
multipart_form_data: ($) =>
285-
prec.right(seq(
286-
token(prec(PREC.BODY_PREFIX, "--")),
287-
token(prec(1, LINE_TAIL)),
288-
repeat(
289-
choice(
290-
// $._blank_line,
291-
$.comment,
292-
seq($.external_body, choice(WS, NL)),
293-
token(prec(2, /<[^\s@]/)),
294-
token(prec(2, "--")),
295-
token(prec(2, /[{\[]\s+/)),
296-
token(prec(1, LINE_TAIL)),
297-
token(prec(2, NL)),
263+
prec.right(
264+
seq(
265+
token(prec(PREC.BODY_PREFIX, "--")),
266+
token(prec(1, LINE_TAIL)),
267+
repeat(
268+
choice(
269+
$.comment,
270+
seq($.external_body, choice(WS, NL)),
271+
token(prec(2, /<[^\s@]/)),
272+
token(prec(2, "--")),
273+
token(prec(2, /[{\[]\s+/)),
274+
token(prec(1, LINE_TAIL)),
275+
token(prec(2, NL)),
276+
),
298277
),
299278
),
300-
)),
279+
),
301280

302281
raw_body: ($) =>
303282
seq(
@@ -320,23 +299,10 @@ module.exports = grammar({
320299
header_entity: (_) => /[\w\-]+/,
321300
identifier: (_) => /[A-Za-z_.\$\d\u00A1-\uFFFF-]+/,
322301
path: ($) =>
323-
prec.right(repeat1(
324-
choice(
325-
WORD_CHAR,
326-
PUNCTUATION,
327-
$.variable,
328-
ESCAPED,
329-
),
330-
)),
331-
value: ($) =>
332-
repeat1(
333-
choice(
334-
WORD_CHAR,
335-
PUNCTUATION,
336-
$.variable,
337-
WS,
338-
),
302+
prec.right(
303+
repeat1(choice(WORD_CHAR, PUNCTUATION, $.variable, ESCAPED)),
339304
),
305+
value: ($) => repeat1(choice(WORD_CHAR, PUNCTUATION, $.variable, WS)),
340306
_blank_line: (_) => seq(optional(WS), token(prec(-1, NL))),
341307
},
342308
});

0 commit comments

Comments
 (0)