Skip to content

Commit

Permalink
remake else if as repeat($._else_clause)
Browse files Browse the repository at this point in the history
Before, it looked like this:

```
if ... {
} else if let ... = ... {
} else if ... {
} else {
}
(if_expression ... consequence: (block)
  alternative: (else_clause (if_let_expression ... consequence: (block)
      alternative: (else_clause (if_expression consequence: (block)
          alternative: (else_clause (block)))))))
```

After, it looks like this:

```
if ... {
} else if let ... = ... {
} else if ... {
} else {
}
(if_expression condition: ... consequence: (block)
  (else_if_let_clause pattern: ... value: ... consequence: (block))
  (else_if_clause condition: ... consequence: (block))
  (else_clause (block)))
```

Previously, the "else" and "if" were not adjacent tokens, and therefore
could not be grouped together in an `("else" "if") @match` query.
The main motivation here is highlighting each if/else if/else branch
with vim-matchup.

It was also difficult to query only a complete if/else expression, and
exclude if_expressions that were simply the "if" in an "else if". It is
maybe wrong to say that the latter is actually an expression, but
moreover if you wanted to query only the former, you would have to
either list all the contexts where an if_expression can occur (except
else_clause) or use an #if-not? to exclude the
`(else_clause (if_expression) @not_this)`. Again, the motivation is
attempting to navigate between if/else branches in the editor using vim
matchup, which requires matching one single `(if_expression) @scope.if`
to link all the branches to, and not creating a bunch of smaller scopes
on all the if_expressions contained within.

The resulting tree is flatter. There is no need for the alternative:
field name as each of the three clause types can only appear in the tail
of an if[_let]_expression. And both of the above problems are solved.
  • Loading branch information
cormacrelf committed Mar 18, 2021
1 parent a3e6768 commit 3346c07
Show file tree
Hide file tree
Showing 5 changed files with 64,829 additions and 62,164 deletions.
67 changes: 51 additions & 16 deletions corpus/expressions.txt
Original file line number Diff line number Diff line change
Expand Up @@ -332,7 +332,10 @@ If expressions
============================================

fn main() {
if n == 2 {
}
if n == 1 {
} else if let Some(k) = z {
} else if n == 2 {
} else {
}
Expand All @@ -348,28 +351,60 @@ let y = if x == 5 { 10 } else { 15 };
parameters: (parameters)
body: (block
(if_expression
condition: (binary_expression
left: (identifier)
right: (integer_literal))
condition: (binary_expression left: (identifier) right: (integer_literal))
consequence: (block))
(if_expression
condition: (binary_expression left: (identifier) right: (integer_literal))
consequence: (block)
alternative: (else_clause
(if_expression
condition: (binary_expression
left: (identifier)
right: (integer_literal))
consequence: (block)
alternative: (else_clause (block)))))))
(else_if_let_clause
pattern: (tuple_struct_pattern type: (identifier) (identifier))
value: (identifier)
consequence: (block))
(else_if_clause
condition: (binary_expression left: (identifier) right: (integer_literal))
consequence: (block))
(else_clause (block)))))
(let_declaration
pattern: (identifier)
value: (if_expression
condition: (binary_expression
left: (identifier)
right: (integer_literal))
condition: (binary_expression left: (identifier) right: (integer_literal))
consequence: (block (integer_literal))
alternative: (else_clause (block (integer_literal))))))
(else_clause (block (integer_literal))))))

============================================
If let as an expression
============================================

let x = if let Some(a) = dish {
a
} else if let None = dish {
99
} else if n == 8 {
9
} else {
7
};

---

(source_file
(let_declaration
pattern: (identifier)
value: (if_let_expression
pattern: (tuple_struct_pattern type: (identifier) (identifier))
value: (identifier)
consequence: (block (identifier))
(else_if_let_clause
pattern: (identifier)
value: (identifier)
consequence: (block (integer_literal)))
(else_if_clause
condition: (binary_expression left: (identifier) right: (integer_literal))
consequence: (block (integer_literal)))
(else_clause (block (integer_literal))))))

============================================
If let expressions
If let control flow
============================================

if let ("Bacon", b) = dish {
Expand Down Expand Up @@ -798,7 +833,7 @@ let three_ranges = [const { (0..=5).into_inner() }; 3];
field: (field_identifier))
arguments: (arguments
(integer_literal)))))))
alternative: (else_clause
(else_clause
(block
(identifier))))
(let_declaration
Expand Down
34 changes: 27 additions & 7 deletions grammar.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ module.exports = grammar({
$._literal_pattern,
$._declaration_statement,
$._pattern,
$._else_clause,
],

inline: $ => [
Expand Down Expand Up @@ -1069,7 +1070,7 @@ module.exports = grammar({
'if',
field('condition', $._expression),
field('consequence', $.block),
optional(field("alternative", $.else_clause))
repeat($._else_clause),
),

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

_else_clause: $ => choice(
$.else_if_clause,
$.else_if_let_clause,
$.else_clause,
),

else_if_clause: $ => seq(
'else',
'if',
field('condition', $._expression),
field('consequence', $.block),
),

else_if_let_clause: $ => seq(
'else',
'if',
'let',
field('pattern', $._pattern),
'=',
field('value', $._expression),
field('consequence', $.block),
),

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

match_expression: $ => seq(
Expand Down
149 changes: 105 additions & 44 deletions src/grammar.json
Original file line number Diff line number Diff line change
Expand Up @@ -6190,20 +6190,11 @@
}
},
{
"type": "CHOICE",
"members": [
{
"type": "FIELD",
"name": "alternative",
"content": {
"type": "SYMBOL",
"name": "else_clause"
}
},
{
"type": "BLANK"
}
]
"type": "REPEAT",
"content": {
"type": "SYMBOL",
"name": "_else_clause"
}
}
]
},
Expand Down Expand Up @@ -6247,20 +6238,102 @@
}
},
{
"type": "CHOICE",
"members": [
{
"type": "FIELD",
"name": "alternative",
"content": {
"type": "SYMBOL",
"name": "else_clause"
}
},
{
"type": "BLANK"
}
]
"type": "REPEAT",
"content": {
"type": "SYMBOL",
"name": "_else_clause"
}
}
]
},
"_else_clause": {
"type": "CHOICE",
"members": [
{
"type": "SYMBOL",
"name": "else_if_clause"
},
{
"type": "SYMBOL",
"name": "else_if_let_clause"
},
{
"type": "SYMBOL",
"name": "else_clause"
}
]
},
"else_if_clause": {
"type": "SEQ",
"members": [
{
"type": "STRING",
"value": "else"
},
{
"type": "STRING",
"value": "if"
},
{
"type": "FIELD",
"name": "condition",
"content": {
"type": "SYMBOL",
"name": "_expression"
}
},
{
"type": "FIELD",
"name": "consequence",
"content": {
"type": "SYMBOL",
"name": "block"
}
}
]
},
"else_if_let_clause": {
"type": "SEQ",
"members": [
{
"type": "STRING",
"value": "else"
},
{
"type": "STRING",
"value": "if"
},
{
"type": "STRING",
"value": "let"
},
{
"type": "FIELD",
"name": "pattern",
"content": {
"type": "SYMBOL",
"name": "_pattern"
}
},
{
"type": "STRING",
"value": "="
},
{
"type": "FIELD",
"name": "value",
"content": {
"type": "SYMBOL",
"name": "_expression"
}
},
{
"type": "FIELD",
"name": "consequence",
"content": {
"type": "SYMBOL",
"name": "block"
}
}
]
},
Expand All @@ -6272,21 +6345,8 @@
"value": "else"
},
{
"type": "CHOICE",
"members": [
{
"type": "SYMBOL",
"name": "block"
},
{
"type": "SYMBOL",
"name": "if_expression"
},
{
"type": "SYMBOL",
"name": "if_let_expression"
}
]
"type": "SYMBOL",
"name": "block"
}
]
},
Expand Down Expand Up @@ -8348,7 +8408,8 @@
"_literal",
"_literal_pattern",
"_declaration_statement",
"_pattern"
"_pattern",
"_else_clause"
]
}

Loading

0 comments on commit 3346c07

Please sign in to comment.