Skip to content

Commit 50c0856

Browse files
fix: name lexing precedences
1 parent f6c9174 commit 50c0856

File tree

4 files changed

+5924
-6073
lines changed

4 files changed

+5924
-6073
lines changed

grammar.js

Lines changed: 46 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,30 @@
1+
/// <reference types="tree-sitter-cli/dsl" />
2+
// @ts-check
3+
4+
const PREC = {
5+
VAR_COMMENT_PREFIX: 2,
6+
BODY_PREFIX: 2,
7+
RAW_BODY: 3,
8+
GRAPQL_JSON_PREFIX: 4,
9+
COMMENT_PREFIX: 5,
10+
REQ_SEPARATOR: 9,
11+
}
12+
113
const WORD_CHAR = /[\p{L}\p{N}]/u;
214
const PUNCTUATION = /[^\n\r\p{Z}\p{L}\p{N}]/u;
315
const WS = /\p{Zs}+/u;
416
const NL = token(choice("\n", "\r", "\r\n", "\0"));
517
const LINE_TAIL = token(seq(/.*/, NL));
618
const ESCAPED = token(/\\[^\n\r]/);
19+
const COMMENT_PREFIX = token(
20+
prec(
21+
PREC.COMMENT_PREFIX,
22+
choice(
23+
/#\s*/,
24+
/\/\/\s*/,
25+
)
26+
)
27+
)
728

829
module.exports = grammar({
930
name: "http",
@@ -28,15 +49,11 @@ module.exports = grammar({
2849
WS: (_) => WS,
2950
NL: (_) => NL,
3051
LINE_TAIL: (_) => LINE_TAIL,
52+
COMMENT_PREFIX: (_) => COMMENT_PREFIX,
3153

32-
_comment_prefix: (_) =>
33-
choice(
34-
token(prec(2, /#\s*/)),
35-
token(prec(2, /\/\/\s*/)),
36-
),
3754
comment: ($) =>
3855
seq(
39-
$._comment_prefix,
56+
COMMENT_PREFIX,
4057
choice(
4158
seq(
4259
token(prec(2, "@")),
@@ -55,7 +72,7 @@ module.exports = grammar({
5572
),
5673
var_comment: ($) =>
5774
seq(
58-
$._comment_prefix,
75+
COMMENT_PREFIX,
5976
token(prec(2, "@")),
6077
field("name", $.identifier),
6178
optional(
@@ -70,7 +87,7 @@ module.exports = grammar({
7087

7188
request_separator: ($) =>
7289
seq(
73-
token(prec(3, /###+\p{Zs}*/)),
90+
token(prec(PREC.REQ_SEPARATOR, /###+\p{Zs}*/)),
7491
optional(token(prec(1, WS))),
7592
optional(field("value", $.value)),
7693
NL,
@@ -203,7 +220,7 @@ module.exports = grammar({
203220
pre_request_script: ($) =>
204221
seq("<", WS, choice($.script, $.path), token(repeat1(NL))),
205222
res_handler_script: ($) =>
206-
seq(token(prec(3, ">")), WS, choice($.script, $.path),
223+
seq(token(prec(PREC.REQ_SEPARATOR, ">")), WS, choice($.script, $.path),
207224
token(repeat1(NL))),
208225
script: (_) =>
209226
seq(
@@ -224,26 +241,31 @@ module.exports = grammar({
224241
NL,
225242
),
226243

227-
xml_body: (_) =>
244+
xml_body: ($) =>
228245
seq(
229-
token(prec(2, /<[^\s@]/)),
230-
repeat1(token(prec(2, LINE_TAIL))),
246+
token(prec(PREC.BODY_PREFIX, /<[^\s@]/)),
247+
$._raw_body,
231248
),
232249

233-
json_body: (_) =>
250+
json_body: ($) =>
234251
seq(
235-
token(prec(2, /[{\[]\s+/)),
236-
repeat1(token(prec(2, LINE_TAIL))),
252+
token(prec(PREC.BODY_PREFIX, /[{\[]\s+/)),
253+
$._raw_body,
237254
),
238255

239256
graphql_body: ($) =>
240-
prec.right(seq($.graphql_data, optional($.json_body))),
241-
graphql_data: (_) =>
257+
prec.right(seq($.graphql_data, optional(alias($.graphql_json_body, $.json_body)))),
258+
graphql_data: ($) =>
242259
seq(
243260
token(
244-
prec(2, seq(choice("query", "mutation"), WS, /.*\{/, NL)),
261+
prec(PREC.BODY_PREFIX, seq(choice("query", "mutation"), WS, /.*\{/, NL)),
245262
),
246-
repeat1(token(prec(2, LINE_TAIL))),
263+
$._raw_body,
264+
),
265+
graphql_json_body: ($) =>
266+
seq(
267+
token(prec(PREC.GRAPQL_JSON_PREFIX, /[{\[]\s+/)),
268+
$._raw_body,
247269
),
248270

249271
_external_body: ($) =>
@@ -253,15 +275,15 @@ module.exports = grammar({
253275
),
254276
external_body: ($) =>
255277
seq(
256-
token(prec(2, "<")),
278+
token(prec(PREC.BODY_PREFIX, "<")),
257279
optional(seq("@", field("name", $.identifier))),
258280
WS,
259281
field("path", $.path),
260282
),
261283

262284
multipart_form_data: ($) =>
263285
prec.right(seq(
264-
token(prec(2, "--")),
286+
token(prec(PREC.BODY_PREFIX, "--")),
265287
token(prec(1, LINE_TAIL)),
266288
repeat(
267289
choice(
@@ -281,15 +303,15 @@ module.exports = grammar({
281303
seq(
282304
choice(
283305
token(prec(1, seq(/.+/, NL))),
284-
seq($._comment_prefix, $._not_comment),
306+
seq(COMMENT_PREFIX, $._not_comment),
285307
),
286308
optional($._raw_body),
287309
),
288310
_raw_body: ($) =>
289311
seq(
290312
choice(
291-
token(prec(1, LINE_TAIL)),
292-
seq($._comment_prefix, $._not_comment),
313+
token(prec(PREC.RAW_BODY, LINE_TAIL)),
314+
seq(COMMENT_PREFIX, $._not_comment),
293315
),
294316
optional($._raw_body),
295317
),

0 commit comments

Comments
 (0)