Skip to content

Commit f8856dd

Browse files
refactor!: add raw-body type
- drop support of form-body (can't distinguish from raw-body) - support multipart-form-data - fix several issues
1 parent 48668eb commit f8856dd

File tree

9 files changed

+10303
-19163
lines changed

9 files changed

+10303
-19163
lines changed

.editorconfig

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,5 +42,8 @@ indent_size = 8
4242
trim_trailing_whitespace = false
4343
indent_size = 2
4444

45+
[*.scm]
46+
indent_size = 2
47+
4548
[test*]
4649
trim_trailing_whitespace = false

Makefile

Lines changed: 9 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

grammar.js

Lines changed: 61 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,16 @@ const PUNCTUATION = /[^\n\r\p{Z}\p{L}\p{N}]/u;
33
const WS = /\p{Zs}+/u;
44
const NL = token(choice("\n", "\r", "\r\n", "\0"));
55
const LINE_TAIL = token(seq(/.*/, NL));
6+
const ESCAPED = token(/\\[^\n\r]/);
67

78
module.exports = grammar({
89
name: "http",
910

1011
extras: (_) => [],
11-
conflicts: ($) => [[$.target_url]],
12+
conflicts: ($) => [
13+
[$.target_url],
14+
[$._raw_body],
15+
],
1216
inline: ($) => [$._target_url_line],
1317

1418
rules: {
@@ -23,16 +27,17 @@ module.exports = grammar({
2327
NL: (_) => NL,
2428
LINE_TAIL: (_) => LINE_TAIL,
2529

30+
_comment_prefix: (_) =>
31+
choice(
32+
token(prec(2, /#\s*/)),
33+
token(prec(2, /\/\/\s*/)),
34+
),
2635
comment: ($) =>
2736
seq(
28-
choice(
29-
repeat1(token(prec(1, "#"))),
30-
token(prec(1, seq("//", repeat("/")))),
31-
),
32-
optional(token(prec(1, WS))),
37+
$._comment_prefix,
3338
choice(
3439
seq(
35-
token(prec(1, "@")),
40+
token(prec(2, "@")),
3641
field("name", $.identifier),
3742
optional(
3843
seq(
@@ -48,26 +53,11 @@ module.exports = grammar({
4853
),
4954
request_separator: ($) =>
5055
seq(
51-
token(prec(1, "###")),
56+
token(prec(3, /###+\p{Zs}*/)),
5257
optional(token(prec(1, WS))),
5358
optional(field("value", $.value)),
5459
NL,
5560
),
56-
_comment_variable_declaration: ($) =>
57-
seq(
58-
optional(token(prec(1, WS))),
59-
choice(
60-
seq(
61-
token(prec(1, "@")),
62-
field("name", $.identifier),
63-
optional(WS),
64-
"=",
65-
optional(token(prec(1, WS))),
66-
field("value", $.value),
67-
),
68-
LINE_TAIL,
69-
),
70-
),
7161

7262
section: ($) =>
7363
prec.right(choice(
@@ -95,7 +85,7 @@ module.exports = grammar({
9585

9686
// LIST http verb is arbitrary and required to use vaultproject
9787
method: (_) =>
98-
/(OPTIONS|GET|HEAD|POST|PUT|DELETE|TRACE|CONNECT|PATCH|LIST)/,
88+
/(OPTIONS|GET|HEAD|POST|PUT|DELETE|TRACE|CONNECT|PATCH|LIST|GRAPHQL|WEBSOCKET)/,
9989

10090
http_version: (_) => token(prec(0, /HTTP\/[\d\.]+/)),
10191

@@ -123,29 +113,28 @@ module.exports = grammar({
123113
status_text: (_) =>
124114
/(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)/,
125115
response: ($) =>
126-
seq($.http_version, WS, $.status_code, WS, $.status_text),
116+
seq($.http_version, WS, $.status_code, WS, $.status_text, NL),
127117

128118
request: ($) =>
129119
prec.right(seq(
130120
optional(seq(field("method", $.method), WS)),
131121
field("url", $.target_url),
132122
optional(seq(WS, field("version", $.http_version))),
133123
NL, repeat($.comment),
134-
optional(seq($.response, NL, repeat($.comment))),
124+
optional($.response),
135125
repeat(field("header", $.header)),
136-
optional(seq(repeat1($._blank_line), repeat($.comment))),
137126
optional(
138-
field(
139-
"body",
140-
choice(
141-
$.form_data,
142-
$.external_body,
143-
$.xml_body,
144-
$.json_body,
145-
$.graphql_data,
146-
$.multipart_form_data,
147-
// TODO: raw body
148-
// $.raw_body,
127+
seq(
128+
repeat1($._blank_line),
129+
repeat($.comment),
130+
optional(
131+
field("body", choice(
132+
$.raw_body,
133+
$.multipart_form_data,
134+
$.xml_body,
135+
$.json_body,
136+
$.graphql_body,
137+
))
149138
),
150139
),
151140
),
@@ -188,8 +177,8 @@ module.exports = grammar({
188177
token(prec(1, "}}")),
189178
),
190179

191-
pre_request_script: ($) => seq("<", WS, $.script, NL),
192-
res_handler_script: ($) => seq(token(prec(1, ">")), WS, $.script, NL),
180+
pre_request_script: ($) => seq("<", WS, choice($.script, $.path), NL),
181+
res_handler_script: ($) => seq(token(prec(3, ">")), WS, choice($.script, $.path), NL),
193182
script: (_) =>
194183
seq(
195184
token(prec(1, "{%")),
@@ -211,62 +200,68 @@ module.exports = grammar({
211200

212201
xml_body: (_) =>
213202
seq(
214-
token(prec(1, /<\S/)),
203+
token(prec(2, /<[^\s@]/)),
215204
repeat1(LINE_TAIL),
216205
),
217206

218207
json_body: (_) =>
219208
seq(
220-
token(prec(1, choice("{", "["))),
209+
token(prec(2, /[{\[]\s+/)),
221210
repeat1(LINE_TAIL),
222211
),
223212

224-
graphql_data: ($) => seq($.graphql_body, optional($.json_body)),
225-
graphql_body: (_) =>
213+
graphql_body: ($) => seq($.graphql_data, optional($.json_body)),
214+
graphql_data: (_) =>
226215
seq(
227-
choice("query", "mutation"),
228-
WS,
216+
token(prec(2, seq(choice("query", "mutation"), WS, /.*\{/, NL))),
229217
repeat1(LINE_TAIL),
230218
),
231219

232220
external_body: ($) =>
233221
seq(
234-
token(prec(1, "<")),
222+
token(prec(2, "<")),
235223
optional(seq("@", field("name", $.identifier))),
236224
WS,
237-
field("path", $.value),
238-
NL,
239-
),
240-
241-
form_data: ($) =>
242-
seq(
243-
$.query_param,
244-
repeat(seq(optional(NL), token(prec(1, "&")), $.query_param)),
245-
NL,
225+
field("path", $.path),
246226
),
247227

248228
multipart_form_data: ($) =>
249229
prec.right(seq(
250-
token(prec(1, "--")),
251-
choice(
252-
LINE_TAIL,
253-
),
230+
token(prec(2, "--")),
231+
LINE_TAIL,
254232
repeat(
255233
choice(
256234
$._blank_line,
257235
$.comment,
258-
// TODO: `external_body` should end before `LINE_TAIL`
259-
$.external_body,
260-
LINE_TAIL,
236+
seq($.external_body, choice(WS, NL)),
237+
token(prec(1, LINE_TAIL)),
261238
),
262239
),
263240
)),
264241

242+
raw_body: ($) => $._raw_body,
243+
_raw_body: ($) =>
244+
seq(
245+
choice(
246+
token(prec(1, LINE_TAIL)),
247+
seq($.external_body, NL),
248+
seq($._comment_prefix, $._not_comment),
249+
),
250+
optional($._raw_body),
251+
),
252+
_not_comment: (_) => token(seq(/[^@]*/, NL)),
253+
265254
header_entity: (_) => /[\w\-]+/,
266255
identifier: (_) => /[A-Za-z_.\$\d\u00A1-\uFFFF-]+/,
267-
number: (_) => /[0-9]+/,
268-
string: (_) => /"[^"\n\r]*"/,
269-
boolean: (_) => choice("true", "false"),
256+
path: ($) =>
257+
prec.right(repeat1(
258+
choice(
259+
WORD_CHAR,
260+
PUNCTUATION,
261+
$.variable,
262+
ESCAPED,
263+
)
264+
)),
270265
value: ($) =>
271266
repeat1(
272267
choice(

queries/highlights.scm

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,3 @@
1-
; Keywords
2-
; (scheme) @module
3-
41
; Methods
52
(method) @function.method
63

@@ -12,17 +9,16 @@
129
(variable_declaration
1310
name: (identifier) @variable)
1411

15-
; Parameters
16-
(query_param
17-
key: (_) @variable.parameter)
18-
1912
; Operators
20-
[
21-
"="
22-
"&"
23-
"@"
24-
"<"
25-
] @operator
13+
(comment
14+
"=" @operator)
15+
(variable_declaration
16+
"=" @operator)
17+
18+
; keywords
19+
(comment
20+
"@" @keyword
21+
name: (_) @keyword)
2622

2723
; Literals
2824
(request
@@ -40,11 +36,15 @@
4036
"}}"
4137
] @punctuation.bracket
4238

43-
":" @punctuation.delimiter
39+
(header
40+
":" @punctuation.delimiter)
4441

4542
; external JSON body
4643
(external_body
4744
path: (_) @string.special.path)
4845

4946
; Comments
50-
(comment) @comment @spell
47+
[
48+
(comment)
49+
(request_separator)
50+
] @comment @spell

queries/injections.scm

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,20 @@
99
((xml_body) @injection.content
1010
(#set! injection.language "xml"))
1111

12-
((graphql_body) @injection.content
12+
((graphql_data) @injection.content
1313
(#set! injection.language "graphql"))
1414

15-
; Lua scripting
15+
; Script (default to javascript)
1616
((script) @injection.content
1717
(#offset! @injection.content 0 2 0 -2)
18-
(#set! injection.language "lua"))
18+
(#set! injection.language "javascript"))
19+
20+
; Script with other languages
21+
((comment
22+
name: (_) @_name
23+
(#eq? @_name "lang")
24+
value: (_) @injection.language)
25+
.
26+
(_
27+
(script) @injection.content
28+
(#offset! @injection.content 0 2 0 -2)))

0 commit comments

Comments
 (0)