Skip to content

Commit 6b74f88

Browse files
committed
fix: template expression precedence
1 parent 99298c8 commit 6b74f88

File tree

7 files changed

+7565
-6142
lines changed

7 files changed

+7565
-6142
lines changed

CHANGELOG.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,13 @@
11
# Changelog
22

3+
## 1.0.0 - 2022-12-02
4+
5+
breaking:
6+
* remove `template_if_branch` and `template_else_branch`
7+
8+
fix:
9+
* fix precedence in template expressions
10+
311
## 0.7.0 - 2022-06-02
412

513
housekeeping:

docs/tree-sitter-hcl.wasm

2.52 KB
Binary file not shown.

grammar.js

Lines changed: 9 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,6 @@ const
1616
module.exports = grammar({
1717
name: 'hcl',
1818

19-
conflicts: $ => [
20-
],
21-
2219
externals: $ => [
2320
$.quoted_template_start,
2421
$.quoted_template_end,
@@ -269,12 +266,6 @@ module.exports = grammar({
269266
$.heredoc_template,
270267
),
271268

272-
_template: $ => prec.left(repeat1(choice(
273-
$.template_literal,
274-
$.template_interpolation,
275-
$.template_directive,
276-
))),
277-
278269
quoted_template: $ => prec(PREC.quoted_template, seq(
279270
$.quoted_template_start,
280271
optional($._template),
@@ -292,6 +283,12 @@ module.exports = grammar({
292283

293284
strip_marker: $ => '~',
294285

286+
_template: $ => repeat1(choice(
287+
$.template_interpolation,
288+
$.template_directive,
289+
$.template_literal,
290+
)),
291+
295292
template_literal: $ => prec.right(repeat1(
296293
$._template_literal_chunk,
297294
)),
@@ -304,7 +301,6 @@ module.exports = grammar({
304301
$.template_interpolation_end,
305302
),
306303

307-
// TODO
308304
template_directive: $ => choice(
309305
$.template_for,
310306
$.template_if,
@@ -337,15 +333,12 @@ module.exports = grammar({
337333
),
338334

339335
template_if: $ => seq(
340-
$.template_if_branch,
341-
optional($.template_else_branch),
336+
$.template_if_intro,
337+
optional($._template),
338+
optional(seq($.template_else_intro, optional($._template))),
342339
$.template_if_end,
343340
),
344341

345-
template_if_branch: $ => seq(
346-
$.template_if_intro, $._template
347-
),
348-
349342
template_if_intro: $ => seq(
350343
$.template_directive_start,
351344
optional($.strip_marker),
@@ -355,10 +348,6 @@ module.exports = grammar({
355348
$.template_directive_end
356349
),
357350

358-
template_else_branch: $ => seq(
359-
$.template_else_intro, $._template
360-
),
361-
362351
template_else_intro: $ => seq(
363352
$.template_directive_start,
364353
optional($.strip_marker),

src/grammar.json

Lines changed: 50 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -1187,30 +1187,6 @@
11871187
}
11881188
]
11891189
},
1190-
"_template": {
1191-
"type": "PREC_LEFT",
1192-
"value": 0,
1193-
"content": {
1194-
"type": "REPEAT1",
1195-
"content": {
1196-
"type": "CHOICE",
1197-
"members": [
1198-
{
1199-
"type": "SYMBOL",
1200-
"name": "template_literal"
1201-
},
1202-
{
1203-
"type": "SYMBOL",
1204-
"name": "template_interpolation"
1205-
},
1206-
{
1207-
"type": "SYMBOL",
1208-
"name": "template_directive"
1209-
}
1210-
]
1211-
}
1212-
}
1213-
},
12141190
"quoted_template": {
12151191
"type": "PREC",
12161192
"value": 1,
@@ -1286,6 +1262,26 @@
12861262
"type": "STRING",
12871263
"value": "~"
12881264
},
1265+
"_template": {
1266+
"type": "REPEAT1",
1267+
"content": {
1268+
"type": "CHOICE",
1269+
"members": [
1270+
{
1271+
"type": "SYMBOL",
1272+
"name": "template_interpolation"
1273+
},
1274+
{
1275+
"type": "SYMBOL",
1276+
"name": "template_directive"
1277+
},
1278+
{
1279+
"type": "SYMBOL",
1280+
"name": "template_literal"
1281+
}
1282+
]
1283+
}
1284+
},
12891285
"template_literal": {
12901286
"type": "PREC_RIGHT",
12911287
"value": 0,
@@ -1504,36 +1500,52 @@
15041500
"members": [
15051501
{
15061502
"type": "SYMBOL",
1507-
"name": "template_if_branch"
1503+
"name": "template_if_intro"
15081504
},
15091505
{
15101506
"type": "CHOICE",
15111507
"members": [
15121508
{
15131509
"type": "SYMBOL",
1514-
"name": "template_else_branch"
1510+
"name": "_template"
15151511
},
15161512
{
15171513
"type": "BLANK"
15181514
}
15191515
]
15201516
},
15211517
{
1522-
"type": "SYMBOL",
1523-
"name": "template_if_end"
1524-
}
1525-
]
1526-
},
1527-
"template_if_branch": {
1528-
"type": "SEQ",
1529-
"members": [
1530-
{
1531-
"type": "SYMBOL",
1532-
"name": "template_if_intro"
1518+
"type": "CHOICE",
1519+
"members": [
1520+
{
1521+
"type": "SEQ",
1522+
"members": [
1523+
{
1524+
"type": "SYMBOL",
1525+
"name": "template_else_intro"
1526+
},
1527+
{
1528+
"type": "CHOICE",
1529+
"members": [
1530+
{
1531+
"type": "SYMBOL",
1532+
"name": "_template"
1533+
},
1534+
{
1535+
"type": "BLANK"
1536+
}
1537+
]
1538+
}
1539+
]
1540+
},
1541+
{
1542+
"type": "BLANK"
1543+
}
1544+
]
15331545
},
15341546
{
15351547
"type": "SYMBOL",
1536-
"name": "_template"
1548+
"name": "template_if_end"
15371549
}
15381550
]
15391551
},
@@ -1582,19 +1594,6 @@
15821594
}
15831595
]
15841596
},
1585-
"template_else_branch": {
1586-
"type": "SEQ",
1587-
"members": [
1588-
{
1589-
"type": "SYMBOL",
1590-
"name": "template_else_intro"
1591-
},
1592-
{
1593-
"type": "SYMBOL",
1594-
"name": "_template"
1595-
}
1596-
]
1597-
},
15981597
"template_else_intro": {
15991598
"type": "SEQ",
16001599
"members": [

src/node-types.json

Lines changed: 2 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -737,33 +737,6 @@
737737
]
738738
}
739739
},
740-
{
741-
"type": "template_else_branch",
742-
"named": true,
743-
"fields": {},
744-
"children": {
745-
"multiple": true,
746-
"required": true,
747-
"types": [
748-
{
749-
"type": "template_directive",
750-
"named": true
751-
},
752-
{
753-
"type": "template_else_intro",
754-
"named": true
755-
},
756-
{
757-
"type": "template_interpolation",
758-
"named": true
759-
},
760-
{
761-
"type": "template_literal",
762-
"named": true
763-
}
764-
]
765-
}
766-
},
767740
{
768741
"type": "template_else_intro",
769742
"named": true,
@@ -900,31 +873,16 @@
900873
"required": true,
901874
"types": [
902875
{
903-
"type": "template_else_branch",
876+
"type": "template_directive",
904877
"named": true
905878
},
906879
{
907-
"type": "template_if_branch",
880+
"type": "template_else_intro",
908881
"named": true
909882
},
910883
{
911884
"type": "template_if_end",
912885
"named": true
913-
}
914-
]
915-
}
916-
},
917-
{
918-
"type": "template_if_branch",
919-
"named": true,
920-
"fields": {},
921-
"children": {
922-
"multiple": true,
923-
"required": true,
924-
"types": [
925-
{
926-
"type": "template_directive",
927-
"named": true
928886
},
929887
{
930888
"type": "template_if_intro",

0 commit comments

Comments
 (0)