tree-sitter / tree-sitter-cli

CLI tool for creating and testing tree-sitter parsers
MIT License
45 stars 15 forks source link

How to repeat a sequence optionally including a newline #54

Closed jonhue closed 5 years ago

jonhue commented 5 years ago

I have a grammar that is structured like this:

    package: $ => seq($.statement_list, /(\r?\n)*/),

    statement: $ => choice(
      // ...
    ),
    statement_list: $ => seq(
      $.statement,
      repeat(seq($.newline, $.statement))
    ),

    newline: $ => /\r?\n|\r/

But when I try to parse a multiline file, say:

1
1

the parser throws an error. It appears to be interpreting this as two statement lists (even though it is just one) and then throws an error because a package can only consist of one statement list.

What is the proper way of doing this?