Skip to content

Commit cf47ff8

Browse files
authored
Designate "else"/"else if" branches inside of if statements (#91)
* designate "else_clause" inside of if statements * upgrade tree-sitter-cli tree-sitter/tree-sitter-javascript#143 (review) * regenerate files for else_clause Co-authored-by: resolritter <[email protected]>
1 parent 40620bf commit cf47ff8

File tree

7 files changed

+63599
-63080
lines changed

7 files changed

+63599
-63080
lines changed

corpus/expressions.txt

+8-7
Original file line numberDiff line numberDiff line change
@@ -340,20 +340,21 @@ let y = if x == 5 { 10 } else { 15 };
340340
left: (identifier)
341341
right: (integer_literal))
342342
consequence: (block)
343-
alternative: (if_expression
344-
condition: (binary_expression
345-
left: (identifier)
346-
right: (integer_literal))
347-
consequence: (block)
348-
alternative: (block)))))
343+
alternative: (else_clause
344+
(if_expression
345+
condition: (binary_expression
346+
left: (identifier)
347+
right: (integer_literal))
348+
consequence: (block)
349+
alternative: (else_clause (block)))))))
349350
(let_declaration
350351
pattern: (identifier)
351352
value: (if_expression
352353
condition: (binary_expression
353354
left: (identifier)
354355
right: (integer_literal))
355356
consequence: (block (integer_literal))
356-
alternative: (block (integer_literal)))))
357+
alternative: (else_clause (block (integer_literal))))))
357358

358359
============================================
359360
If let expressions

grammar.js

+5-5
Original file line numberDiff line numberDiff line change
@@ -1065,7 +1065,7 @@ module.exports = grammar({
10651065
'if',
10661066
field('condition', $._expression),
10671067
field('consequence', $.block),
1068-
optional($._else_tail)
1068+
optional(field("alternative", $.else_clause))
10691069
),
10701070

10711071
if_let_expression: $ => seq(
@@ -1075,16 +1075,16 @@ module.exports = grammar({
10751075
'=',
10761076
field('value', $._expression),
10771077
field('consequence', $.block),
1078-
optional($._else_tail)
1078+
optional(field('alternative', $.else_clause))
10791079
),
10801080

1081-
_else_tail: $ => seq(
1081+
else_clause: $ => seq(
10821082
'else',
1083-
field('alternative', choice(
1083+
choice(
10841084
$.block,
10851085
$.if_expression,
10861086
$.if_let_expression
1087-
))
1087+
)
10881088
),
10891089

10901090
match_expression: $ => seq(

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
"nan": "^2.8.0"
1414
},
1515
"devDependencies": {
16-
"tree-sitter-cli": "^0.16.7"
16+
"tree-sitter-cli": "^0.17.3"
1717
},
1818
"scripts": {
1919
"test": "tree-sitter test && script/parse-examples",

src/grammar.json

+28-24
Original file line numberDiff line numberDiff line change
@@ -6185,8 +6185,12 @@
61856185
"type": "CHOICE",
61866186
"members": [
61876187
{
6188-
"type": "SYMBOL",
6189-
"name": "_else_tail"
6188+
"type": "FIELD",
6189+
"name": "alternative",
6190+
"content": {
6191+
"type": "SYMBOL",
6192+
"name": "else_clause"
6193+
}
61906194
},
61916195
{
61926196
"type": "BLANK"
@@ -6238,8 +6242,12 @@
62386242
"type": "CHOICE",
62396243
"members": [
62406244
{
6241-
"type": "SYMBOL",
6242-
"name": "_else_tail"
6245+
"type": "FIELD",
6246+
"name": "alternative",
6247+
"content": {
6248+
"type": "SYMBOL",
6249+
"name": "else_clause"
6250+
}
62436251
},
62446252
{
62456253
"type": "BLANK"
@@ -6248,33 +6256,29 @@
62486256
}
62496257
]
62506258
},
6251-
"_else_tail": {
6259+
"else_clause": {
62526260
"type": "SEQ",
62536261
"members": [
62546262
{
62556263
"type": "STRING",
62566264
"value": "else"
62576265
},
62586266
{
6259-
"type": "FIELD",
6260-
"name": "alternative",
6261-
"content": {
6262-
"type": "CHOICE",
6263-
"members": [
6264-
{
6265-
"type": "SYMBOL",
6266-
"name": "block"
6267-
},
6268-
{
6269-
"type": "SYMBOL",
6270-
"name": "if_expression"
6271-
},
6272-
{
6273-
"type": "SYMBOL",
6274-
"name": "if_let_expression"
6275-
}
6276-
]
6277-
}
6267+
"type": "CHOICE",
6268+
"members": [
6269+
{
6270+
"type": "SYMBOL",
6271+
"name": "block"
6272+
},
6273+
{
6274+
"type": "SYMBOL",
6275+
"name": "if_expression"
6276+
},
6277+
{
6278+
"type": "SYMBOL",
6279+
"name": "if_let_expression"
6280+
}
6281+
]
62786282
}
62796283
]
62806284
},

src/node-types.json

+25-18
Original file line numberDiff line numberDiff line change
@@ -1175,6 +1175,29 @@
11751175
}
11761176
}
11771177
},
1178+
{
1179+
"type": "else_clause",
1180+
"named": true,
1181+
"fields": {},
1182+
"children": {
1183+
"multiple": false,
1184+
"required": true,
1185+
"types": [
1186+
{
1187+
"type": "block",
1188+
"named": true
1189+
},
1190+
{
1191+
"type": "if_expression",
1192+
"named": true
1193+
},
1194+
{
1195+
"type": "if_let_expression",
1196+
"named": true
1197+
}
1198+
]
1199+
}
1200+
},
11781201
{
11791202
"type": "empty_statement",
11801203
"named": true,
@@ -1987,15 +2010,7 @@
19872010
"required": false,
19882011
"types": [
19892012
{
1990-
"type": "block",
1991-
"named": true
1992-
},
1993-
{
1994-
"type": "if_expression",
1995-
"named": true
1996-
},
1997-
{
1998-
"type": "if_let_expression",
2013+
"type": "else_clause",
19992014
"named": true
20002015
}
20012016
]
@@ -2031,15 +2046,7 @@
20312046
"required": false,
20322047
"types": [
20332048
{
2034-
"type": "block",
2035-
"named": true
2036-
},
2037-
{
2038-
"type": "if_expression",
2039-
"named": true
2040-
},
2041-
{
2042-
"type": "if_let_expression",
2049+
"type": "else_clause",
20432050
"named": true
20442051
}
20452052
]

0 commit comments

Comments
 (0)