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 condition: ... consequence: (block)
  alternative: (else_clause (if_expression condition: (let_condition pattern:  ... value: ...)
                                           consequence: (block)
      alternative: (else_clause (if_expression condition: ... consequence: (block)
          alternative: (else_clause (block)))))))
```

After, it looks like this:

``` if ... {
} else if let ... = ... {
} else if ... {
} else {
}
(if_expression condition: ... consequence: (block)
  (else_clause condition: (let_condition pattern: ... value: ...)
               consequence: (block))
  (else_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 (else_clause) only appears in the tail of an
(if_expression) and never at the top level in the condition/consequence, hence writing (else_clause) in a query unambiguously selects nodes at the tail. And the previous two problems are solved:

- `(else_clause "else" "if")` can match now, and it will not match a
  final `else {}` clause. Previously it was an impossible pattern.
- `(if_expression)` will only match once in a chain of `if {} else if {}
  ...`s, with the match over the entire expression. No need for hacky
  tricks to avoid matches on inner `if_expression`s.
  • Loading branch information
cormacrelf committed Dec 4, 2022
1 parent 3275f51 commit a94c996
Show file tree
Hide file tree
Showing 5 changed files with 65,969 additions and 64,747 deletions.
98 changes: 60 additions & 38 deletions corpus/expressions.txt
Original file line number Diff line number Diff line change
Expand Up @@ -481,7 +481,10 @@ If expressions
================================================================================

fn main() {
if n == 2 {
}
if n == 1 {
} else if let Some(k) = z {
} else if n == 2 {
} else {
}
Expand All @@ -502,41 +505,36 @@ if foo && bar || baz {}
body: (block
(expression_statement
(if_expression
condition: (binary_expression
left: (identifier)
right: (integer_literal))
condition: (binary_expression left: (identifier) right: (integer_literal))
consequence: (block)))
(expression_statement
(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_clause
condition: (let_condition
pattern: (tuple_struct_pattern type: (identifier) (identifier))
value: (identifier))
consequence: (block))
(else_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))
consequence: (block
(integer_literal))
alternative: (else_clause
(block
(integer_literal)))))
condition: (binary_expression left: (identifier) right: (integer_literal))
consequence: (block (integer_literal))
(else_clause (block (integer_literal)))))
(expression_statement
(if_expression
condition: (binary_expression
left: (identifier)
right: (identifier))
condition: (binary_expression left: (identifier) right: (identifier))
consequence: (block)))
(expression_statement
(if_expression
condition: (binary_expression
left: (binary_expression
left: (identifier)
right: (identifier))
left: (binary_expression left: (identifier) right: (identifier))
right: (identifier))
consequence: (block))))

Expand All @@ -558,23 +556,52 @@ if let Some(a) = b
(if_expression
condition: (let_chain
(let_condition
pattern: (tuple_struct_pattern
type: (identifier)
(identifier))
pattern: (tuple_struct_pattern type: (identifier) (identifier))
value: (identifier))
(identifier)
(identifier)
(let_condition
pattern: (tuple_struct_pattern
type: (identifier)
(identifier))
pattern: (tuple_struct_pattern type: (identifier) (identifier))
value: (identifier)))
consequence: (block))))

================================================================================
If let expressions
================================================================================

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_expression
condition: (let_condition
pattern: (tuple_struct_pattern type: (identifier) (identifier))
value: (identifier))
consequence: (block (identifier))
(else_clause
condition: (let_condition pattern: (identifier) value: (identifier))
consequence: (block (integer_literal)))
(else_clause
condition: (binary_expression left: (identifier) right: (integer_literal))
consequence: (block (integer_literal)))
(else_clause
(block (integer_literal))))))

================================================================================
If let chains
================================================================================

if let ("Bacon", b) = dish {
}

Expand Down Expand Up @@ -1270,10 +1297,7 @@ let three_ranges = [const { (0..=5).into_inner() }; 3];
(empty_statement)
(expression_statement
(if_expression
condition: (binary_expression
left: (unary_expression
(identifier))
right: (integer_literal))
condition: (binary_expression left: (unary_expression (identifier)) right: (integer_literal))
consequence: (block
(expression_statement
(const_block
Expand All @@ -1285,9 +1309,7 @@ let three_ranges = [const { (0..=5).into_inner() }; 3];
field: (field_identifier))
arguments: (arguments
(integer_literal))))))))
alternative: (else_clause
(block
(identifier)))))
(else_clause (block (identifier)))))
(let_declaration
pattern: (identifier)
value: (array_expression
Expand Down
36 changes: 21 additions & 15 deletions grammar.js
Original file line number Diff line number Diff line change
Expand Up @@ -1096,9 +1096,29 @@ module.exports = grammar({
'if',
field('condition', $._condition),
field('consequence', $.block),
optional(field("alternative", $.else_clause))
repeat($.else_clause),
)),

_else_if: $ => prec.right(seq(
'if',
field('condition', $._condition),
field('consequence', $.block),
)),

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

_condition: $ => choice(
$._expression,
$.let_condition,
alias($._let_chain, $.let_chain),
),

let_condition: $ => seq(
'let',
field('pattern', $._pattern),
Expand All @@ -1114,20 +1134,6 @@ module.exports = grammar({
seq($._expression, '&&', $.let_condition),
)),

_condition: $ => choice(
$._expression,
$.let_condition,
alias($._let_chain, $.let_chain),
),

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

match_expression: $ => seq(
'match',
field('value', $._expression),
Expand Down
136 changes: 78 additions & 58 deletions src/grammar.json
Original file line number Diff line number Diff line change
Expand Up @@ -6337,24 +6337,88 @@
}
},
{
"type": "CHOICE",
"members": [
{
"type": "FIELD",
"name": "alternative",
"content": {
"type": "SYMBOL",
"name": "else_clause"
}
},
{
"type": "BLANK"
}
]
"type": "REPEAT",
"content": {
"type": "SYMBOL",
"name": "else_clause"
}
}
]
}
},
"_else_if": {
"type": "PREC_RIGHT",
"value": 0,
"content": {
"type": "SEQ",
"members": [
{
"type": "STRING",
"value": "if"
},
{
"type": "FIELD",
"name": "condition",
"content": {
"type": "SYMBOL",
"name": "_condition"
}
},
{
"type": "FIELD",
"name": "consequence",
"content": {
"type": "SYMBOL",
"name": "block"
}
}
]
}
},
"else_clause": {
"type": "SEQ",
"members": [
{
"type": "STRING",
"value": "else"
},
{
"type": "CHOICE",
"members": [
{
"type": "SYMBOL",
"name": "block"
},
{
"type": "SYMBOL",
"name": "_else_if"
}
]
}
]
},
"_condition": {
"type": "CHOICE",
"members": [
{
"type": "SYMBOL",
"name": "_expression"
},
{
"type": "SYMBOL",
"name": "let_condition"
},
{
"type": "ALIAS",
"content": {
"type": "SYMBOL",
"name": "_let_chain"
},
"named": true,
"value": "let_chain"
}
]
},
"let_condition": {
"type": "SEQ",
"members": [
Expand Down Expand Up @@ -6482,50 +6546,6 @@
]
}
},
"_condition": {
"type": "CHOICE",
"members": [
{
"type": "SYMBOL",
"name": "_expression"
},
{
"type": "SYMBOL",
"name": "let_condition"
},
{
"type": "ALIAS",
"content": {
"type": "SYMBOL",
"name": "_let_chain"
},
"named": true,
"value": "let_chain"
}
]
},
"else_clause": {
"type": "SEQ",
"members": [
{
"type": "STRING",
"value": "else"
},
{
"type": "CHOICE",
"members": [
{
"type": "SYMBOL",
"name": "block"
},
{
"type": "SYMBOL",
"name": "if_expression"
}
]
}
]
},
"match_expression": {
"type": "SEQ",
"members": [
Expand Down
Loading

0 comments on commit a94c996

Please sign in to comment.