tree-sitter / tree-sitter-python

Python grammar for tree-sitter
MIT License
374 stars 138 forks source link

Unable to parse match statements #211

Closed SadCheeto closed 1 year ago

SadCheeto commented 1 year ago

Hi there.

I ran into an issue with parsing match statements in python. Unlike other languages it does not seem to have a unique Node type for match statements (where languages such as C# and Java have switch statements).

I have been used the playground to see the structure of the tree produced.

You can use the following piece of python code to recreate the problem:

match term:
    case pattern1:
        print(1)
    case pattern2:
        print(2)
    case pattern3:
        print(3)
    case default:
        print(0)

The resulting tree of parsing the before mentioned code piece looks like this:

[module](https://tree-sitter.github.io/tree-sitter/playground#) [0, 0] - [10, 0]
  [expression_statement](https://tree-sitter.github.io/tree-sitter/playground#) [0, 0] - [1, 5]
    [assignment](https://tree-sitter.github.io/tree-sitter/playground#) [0, 0] - [1, 5]
      left: [identifier](https://tree-sitter.github.io/tree-sitter/playground#) [0, 0] - [0, 5]
      [ERROR](https://tree-sitter.github.io/tree-sitter/playground#) [0, 6] - [0, 10]
        [identifier](https://tree-sitter.github.io/tree-sitter/playground#) [0, 6] - [0, 10]
      type: [type](https://tree-sitter.github.io/tree-sitter/playground#) [1, 1] - [1, 5]
        [identifier](https://tree-sitter.github.io/tree-sitter/playground#) [1, 1] - [1, 5]
  [expression_statement](https://tree-sitter.github.io/tree-sitter/playground#) [1, 6] - [2, 13]
    [assignment](https://tree-sitter.github.io/tree-sitter/playground#) [1, 6] - [2, 13]
      left: [identifier](https://tree-sitter.github.io/tree-sitter/playground#) [1, 6] - [1, 14]
      type: [type](https://tree-sitter.github.io/tree-sitter/playground#) [2, 5] - [2, 13]
        [call](https://tree-sitter.github.io/tree-sitter/playground#) [2, 5] - [2, 13]
          function: [identifier](https://tree-sitter.github.io/tree-sitter/playground#) [2, 5] - [2, 10]
          arguments: [argument_list](https://tree-sitter.github.io/tree-sitter/playground#) [2, 10] - [2, 13]
            [integer](https://tree-sitter.github.io/tree-sitter/playground#) [2, 11] - [2, 12]
  [expression_statement](https://tree-sitter.github.io/tree-sitter/playground#) [3, 4] - [3, 8]
    [identifier](https://tree-sitter.github.io/tree-sitter/playground#) [3, 4] - [3, 8]
  [expression_statement](https://tree-sitter.github.io/tree-sitter/playground#) [3, 9] - [4, 16]
    [assignment](https://tree-sitter.github.io/tree-sitter/playground#) [3, 9] - [4, 16]
      left: [identifier](https://tree-sitter.github.io/tree-sitter/playground#) [3, 9] - [3, 17]
      type: [type](https://tree-sitter.github.io/tree-sitter/playground#) [4, 8] - [4, 16]
        [call](https://tree-sitter.github.io/tree-sitter/playground#) [4, 8] - [4, 16]
          function: [identifier](https://tree-sitter.github.io/tree-sitter/playground#) [4, 8] - [4, 13]
          arguments: [argument_list](https://tree-sitter.github.io/tree-sitter/playground#) [4, 13] - [4, 16]
            [integer](https://tree-sitter.github.io/tree-sitter/playground#) [4, 14] - [4, 15]
  [expression_statement](https://tree-sitter.github.io/tree-sitter/playground#) [5, 4] - [5, 8]
    [identifier](https://tree-sitter.github.io/tree-sitter/playground#) [5, 4] - [5, 8]
  [expression_statement](https://tree-sitter.github.io/tree-sitter/playground#) [5, 9] - [6, 16]
    [assignment](https://tree-sitter.github.io/tree-sitter/playground#) [5, 9] - [6, 16]
      left: [identifier](https://tree-sitter.github.io/tree-sitter/playground#) [5, 9] - [5, 17]
      type: [type](https://tree-sitter.github.io/tree-sitter/playground#) [6, 8] - [6, 16]
        [call](https://tree-sitter.github.io/tree-sitter/playground#) [6, 8] - [6, 16]
          function: [identifier](https://tree-sitter.github.io/tree-sitter/playground#) [6, 8] - [6, 13]
          arguments: [argument_list](https://tree-sitter.github.io/tree-sitter/playground#) [6, 13] - [6, 16]
            [integer](https://tree-sitter.github.io/tree-sitter/playground#) [6, 14] - [6, 15]
  [expression_statement](https://tree-sitter.github.io/tree-sitter/playground#) [7, 4] - [7, 8]
    [identifier](https://tree-sitter.github.io/tree-sitter/playground#) [7, 4] - [7, 8]
  [expression_statement](https://tree-sitter.github.io/tree-sitter/playground#) [7, 9] - [8, 16]
    [assignment](https://tree-sitter.github.io/tree-sitter/playground#) [7, 9] - [8, 16]
      left: [identifier](https://tree-sitter.github.io/tree-sitter/playground#) [7, 9] - [7, 16]
      type: [type](https://tree-sitter.github.io/tree-sitter/playground#) [8, 8] - [8, 16]
        [call](https://tree-sitter.github.io/tree-sitter/playground#) [8, 8] - [8, 16]
          function: [identifier](https://tree-sitter.github.io/tree-sitter/playground#) [8, 8] - [8, 13]
          arguments: [argument_list](https://tree-sitter.github.io/tree-sitter/playground#) [8, 13] - [8, 16]
            [integer](https://tree-sitter.github.io/tree-sitter/playground#) [8, 14] - [8, 15]

Here are some pictures from the playground as well:

image image
amaanq commented 1 year ago

Playground seems to be using an old version, this parses fine on master