Skip to content

Commit

Permalink
Designate "else"/"else if" branches inside of if statements (#91)
Browse files Browse the repository at this point in the history
* 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 <resolritter@users.noreply.github.com>
  • Loading branch information
resolritter and resolritter committed Nov 4, 2020
1 parent 40620bf commit cf47ff8
Show file tree
Hide file tree
Showing 7 changed files with 63,599 additions and 63,080 deletions.
15 changes: 8 additions & 7 deletions corpus/expressions.txt
Original file line number Diff line number Diff line change
Expand Up @@ -340,20 +340,21 @@ let y = if x == 5 { 10 } else { 15 };
left: (identifier)
right: (integer_literal))
consequence: (block)
alternative: (if_expression
condition: (binary_expression
left: (identifier)
right: (integer_literal))
consequence: (block)
alternative: (block)))))
alternative: (else_clause
(if_expression
condition: (binary_expression
left: (identifier)
right: (integer_literal))
consequence: (block)
alternative: (else_clause (block)))))))
(let_declaration
pattern: (identifier)
value: (if_expression
condition: (binary_expression
left: (identifier)
right: (integer_literal))
consequence: (block (integer_literal))
alternative: (block (integer_literal)))))
alternative: (else_clause (block (integer_literal))))))

============================================
If let expressions
Expand Down
10 changes: 5 additions & 5 deletions grammar.js
Original file line number Diff line number Diff line change
Expand Up @@ -1065,7 +1065,7 @@ module.exports = grammar({
'if',
field('condition', $._expression),
field('consequence', $.block),
optional($._else_tail)
optional(field("alternative", $.else_clause))
),

if_let_expression: $ => seq(
Expand All @@ -1075,16 +1075,16 @@ module.exports = grammar({
'=',
field('value', $._expression),
field('consequence', $.block),
optional($._else_tail)
optional(field('alternative', $.else_clause))
),

_else_tail: $ => seq(
else_clause: $ => seq(
'else',
field('alternative', choice(
choice(
$.block,
$.if_expression,
$.if_let_expression
))
)
),

match_expression: $ => seq(
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
"nan": "^2.8.0"
},
"devDependencies": {
"tree-sitter-cli": "^0.16.7"
"tree-sitter-cli": "^0.17.3"
},
"scripts": {
"test": "tree-sitter test && script/parse-examples",
Expand Down
52 changes: 28 additions & 24 deletions src/grammar.json
Original file line number Diff line number Diff line change
Expand Up @@ -6185,8 +6185,12 @@
"type": "CHOICE",
"members": [
{
"type": "SYMBOL",
"name": "_else_tail"
"type": "FIELD",
"name": "alternative",
"content": {
"type": "SYMBOL",
"name": "else_clause"
}
},
{
"type": "BLANK"
Expand Down Expand Up @@ -6238,8 +6242,12 @@
"type": "CHOICE",
"members": [
{
"type": "SYMBOL",
"name": "_else_tail"
"type": "FIELD",
"name": "alternative",
"content": {
"type": "SYMBOL",
"name": "else_clause"
}
},
{
"type": "BLANK"
Expand All @@ -6248,33 +6256,29 @@
}
]
},
"_else_tail": {
"else_clause": {
"type": "SEQ",
"members": [
{
"type": "STRING",
"value": "else"
},
{
"type": "FIELD",
"name": "alternative",
"content": {
"type": "CHOICE",
"members": [
{
"type": "SYMBOL",
"name": "block"
},
{
"type": "SYMBOL",
"name": "if_expression"
},
{
"type": "SYMBOL",
"name": "if_let_expression"
}
]
}
"type": "CHOICE",
"members": [
{
"type": "SYMBOL",
"name": "block"
},
{
"type": "SYMBOL",
"name": "if_expression"
},
{
"type": "SYMBOL",
"name": "if_let_expression"
}
]
}
]
},
Expand Down
43 changes: 25 additions & 18 deletions src/node-types.json
Original file line number Diff line number Diff line change
Expand Up @@ -1175,6 +1175,29 @@
}
}
},
{
"type": "else_clause",
"named": true,
"fields": {},
"children": {
"multiple": false,
"required": true,
"types": [
{
"type": "block",
"named": true
},
{
"type": "if_expression",
"named": true
},
{
"type": "if_let_expression",
"named": true
}
]
}
},
{
"type": "empty_statement",
"named": true,
Expand Down Expand Up @@ -1987,15 +2010,7 @@
"required": false,
"types": [
{
"type": "block",
"named": true
},
{
"type": "if_expression",
"named": true
},
{
"type": "if_let_expression",
"type": "else_clause",
"named": true
}
]
Expand Down Expand Up @@ -2031,15 +2046,7 @@
"required": false,
"types": [
{
"type": "block",
"named": true
},
{
"type": "if_expression",
"named": true
},
{
"type": "if_let_expression",
"type": "else_clause",
"named": true
}
]
Expand Down

0 comments on commit cf47ff8

Please sign in to comment.