tree-sitter / tree-sitter-javascript

Javascript grammar for tree-sitter
MIT License
314 stars 108 forks source link

Nested do statement gives parse error #263

Closed helixbass closed 9 months ago

helixbass commented 9 months ago

The following piece of code is valid but it is parsed incorrectly:

do do ; while (a) while (a)

Here's a link to the TypeScript Playground showing that the snippet above is valid JavaScript or TypeScript:

https://www.typescriptlang.org/play?#code/CYewBKYNxg7gFgSwDYFMwAoCGBKOS1Ncg

The output of tree-sitter parse is the following:

(program [0, 0] - [1, 0]
  (ERROR [0, 0] - [0, 27]
    (do_statement [0, 3] - [0, 27]
      body: (empty_statement [0, 6] - [0, 7])
      (ERROR [0, 14] - [0, 23]
        (parenthesized_expression [0, 14] - [0, 17]
          (identifier [0, 15] - [0, 16]))
        (identifier [0, 18] - [0, 23]))
      condition: (parenthesized_expression [0, 24] - [0, 27]
        (identifier [0, 25] - [0, 26])))))
/Users/jrosse/prj/tmp-js/tmp.js 0 ms    (ERROR [0, 0] - [0, 27])
helixbass commented 9 months ago

Similar (presumably) case (that is also giving a parse error):

for (a in b) { do ; while (a) break; }
(program [0, 0] - [1, 0]
  (for_in_statement [0, 0] - [0, 38]
    left: (identifier [0, 5] - [0, 6])
    right: (identifier [0, 10] - [0, 11])
    body: (statement_block [0, 13] - [0, 38]
      (do_statement [0, 15] - [0, 36]
        body: (empty_statement [0, 18] - [0, 19])
        condition: (parenthesized_expression [0, 26] - [0, 29]
          (identifier [0, 27] - [0, 28]))
        (ERROR [0, 30] - [0, 35]
          (identifier [0, 30] - [0, 35]))))))
/Users/jrosse/prj/tmp-js/tmp.js 0 ms    (ERROR [0, 30] - [0, 35])

(let me know if you'd like me to open a separate issue for it)

helixbass commented 9 months ago

Thanks I see these parsing successfully now