@@ -3,12 +3,16 @@ const PUNCTUATION = /[^\n\r\p{Z}\p{L}\p{N}]/u;
33const WS = / \p{ Zs} + / u;
44const NL = token ( choice ( "\n" , "\r" , "\r\n" , "\0" ) ) ;
55const LINE_TAIL = token ( seq ( / .* / , NL ) ) ;
6+ const ESCAPED = token ( / \\ [ ^ \n \r ] / ) ;
67
78module . 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- / ( O P T I O N S | G E T | H E A D | P O S T | P U T | D E L E T E | T R A C E | C O N N E C T | P A T C H | L I S T ) / ,
88+ / ( O P T I O N S | G E T | H E A D | P O S T | P U T | D E L E T E | T R A C E | C O N N E C T | P A T C H | L I S T | G R A P H Q L | W E B S O C K E T ) / ,
9989
10090 http_version : ( _ ) => token ( prec ( 0 , / H T T P \/ [ \d \. ] + / ) ) ,
10191
@@ -123,29 +113,28 @@ module.exports = grammar({
123113 status_text : ( _ ) =>
124114 / ( C o n t i n u e | S w i t c h i n g P r o t o c o l s | P r o c e s s i n g | O K | C r e a t e d | A c c e p t e d | N o n - A u t h o r i t a t i v e I n f o r m a t i o n | N o C o n t e n t | R e s e t C o n t e n t | P a r t i a l C o n t e n t | M u l t i - S t a t u s | A l r e a d y R e p o r t e d | I M U s e d | M u l t i p l e C h o i c e s | M o v e d P e r m a n e n t l y | F o u n d | S e e O t h e r | N o t M o d i f i e d | U s e P r o x y | S w i t c h P r o x y | T e m p o r a r y R e d i r e c t | P e r m a n e n t R e d i r e c t | B a d R e q u e s t | U n a u t h o r i z e d | P a y m e n t R e q u i r e d | F o r b i d d e n | N o t F o u n d | M e t h o d N o t A l l o w e d | N o t A c c e p t a b l e | P r o x y A u t h e n t i c a t i o n R e q u i r e d | R e q u e s t T i m e o u t | C o n f l i c t | G o n e | L e n g t h R e q u i r e d | P r e c o n d i t i o n F a i l e d | P a y l o a d T o o L a r g e | U R I T o o L o n g | U n s u p p o r t e d M e d i a T y p e | R a n g e N o t S a t i s f i a b l e | E x p e c t a t i o n F a i l e d | I ' m a t e a p o t | M i s d i r e c t e d R e q u e s t | U n p r o c e s s a b l e E n t i t y | L o c k e d | F a i l e d D e p e n d e n c y | T o o E a r l y | U p g r a d e R e q u i r e d | P r e c o n d i t i o n R e q u i r e d | T o o M a n y R e q u e s t s | R e q u e s t H e a d e r F i e l d s T o o L a r g e | U n a v a i l a b l e F o r L e g a l R e a s o n s | I n t e r n a l S e r v e r E r r o r | N o t I m p l e m e n t e d | B a d G a t e w a y | S e r v i c e U n a v a i l a b l e | G a t e w a y T i m e o u t | H T T P V e r s i o n N o t S u p p o r t e d | V a r i a n t A l s o N e g o t i a t e s | I n s u f f i c i e n t S t o r a g e | L o o p D e t e c t e d | N o t E x t e n d e d | N e t w o r k A u t h e n t i c a t i o n R e q u i r e d ) / ,
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 - Z a - 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 (
0 commit comments