Skip to content

Conversation

@aspeddro
Copy link
Collaborator

@aspeddro aspeddro commented Jan 20, 2023

This PR is a proposal to improve AST to switch_match

Currently, the exposed nodes in the tree are few. By exposing more nodes I can get better features like a better indentation.

Example:

switch n {
| 1 | 2 => Ok
| _ => Failed
}

Before:

(source_file [0, 0] - [5, 0]
  (expression_statement [0, 0] - [3, 1]
    (switch_expression [0, 0] - [3, 1]
      (value_identifier [0, 7] - [0, 8])
      (switch_match [1, 0] - [1, 13]
        (number [1, 2] - [1, 3])
        (number [1, 6] - [1, 7])
        (expression_statement [1, 11] - [1, 13]
          (variant [1, 11] - [1, 13]
            (variant_identifier [1, 11] - [1, 13]))))
      (switch_match [2, 0] - [2, 13]
        (value_identifier [2, 2] - [2, 3])
        (expression_statement [2, 7] - [2, 13]
          (variant [2, 7] - [2, 13]
            (variant_identifier [2, 7] - [2, 13])))))))

After:

(source_file [0, 0] - [5, 0]
  (expression_statement [0, 0] - [3, 1]
    (switch_expression [0, 0] - [3, 1]
      (value_identifier [0, 7] - [0, 8])
      (switch_match [1, 0] - [1, 13]
        pattern: (or_pattern [1, 2] - [1, 7]
          (number [1, 2] - [1, 3])
          (number [1, 6] - [1, 7]))
        body: (sequence_expression [1, 11] - [1, 13]
          (expression_statement [1, 11] - [1, 13]
            (variant [1, 11] - [1, 13]
              (variant_identifier [1, 11] - [1, 13])))))
      (switch_match [2, 0] - [2, 13]
        pattern: (value_identifier [2, 2] - [2, 3])
        body: (sequence_expression [2, 7] - [2, 13]
          (expression_statement [2, 7] - [2, 13]
            (variant [2, 7] - [2, 13]
              (variant_identifier [2, 7] - [2, 13]))))))))

Changes on grammar:

  • Add or_pattern, range_pattern and exception_pattern
  • Expose parenthesized_pattern
  • Add two fields for swtich_match: pattern and body
  • Remove the following rules: _switch_pattern, _switch_exception_pattern, _parenthesized_switch_pattern, _switch_value_pattern
  • Rename rule switch_pattern_condition to guard

expose more node
add field for switch case

switch n {
| (1 | 2) => 1
| (n if n > 10 | n if n < 42) | 50 | (n if n > 100) => n
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This match case is not valid, see Playground

The if or when condition cannot occur inside parenthesized_pattern.

@aspeddro aspeddro changed the title rewrite switch match improve switch match Jan 20, 2023
$._parenthesized_switch_pattern,
$._switch_value_pattern,
$._switch_range_pattern,
field('body', alias($._one_or_more_statements, $.sequence_expression)),
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This alias is so that the switch_match body has only one field.

Copy link
Collaborator

@nkrkv nkrkv left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice! Many thanks

@nkrkv nkrkv merged commit f07c6f8 into rescript-lang:main Feb 3, 2023
@aspeddro aspeddro deleted the add-more-fields branch April 16, 2023 23:12
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants