tree-sitter / tree-sitter-cli

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

optional rule bug? #4

Closed Baael closed 8 years ago

Baael commented 8 years ago

I got a little problem, I am wondering if I made mistake somewhere or it is a bug.

This code:


  _indent:    -> "\n<"
  _outdent:   -> "\n>"
  terminator: -> "\n"

  # program  ::= block+
  program: -> seq @_indent, repeat(@_statement), @_outdent

  block:   -> seq @_indent, repeat(@_statement), @_outdent

  _statement: -> choice(
    @class
    @block
  )

  # primitive

  identifier: -> /[\a_$][\a\d_$]*/

  # class

  # class ::= 'class' _accessibles  extends? block?

  class: -> seq 'class', @identifier
class TestName

Generates:

(program
  (terminator)
  (terminator)
  (terminator)
  (class (identifier))
  (terminator)
)

While whenever I will add "optional" rule

  class: -> seq 'class', optional(@identifier)

it generates:

(program (class) (class) (class) (identifier) (terminator))
Baael commented 8 years ago

similar bug:

this is not working at all (hangs compiler), or is producing similar bug to previous

    _callable: -> choice(
      @identifier
      @accessor
      @this
      @this_property
      )
    func_call:  -> prec 3, seq @_callable, @body
    invocation: -> prec.right 1, seq  @_callable, ' ',choice @invocation, @_statement

while this works perfectly:


    func_call:  -> prec 3, seq choice(
      @identifier
      @accessor
      @this
      @this_property
      ), @body

    invocation: -> prec.right 1, seq choice(
      @identifier
      @accessor
      @this
      @this_property
      ), ' ',choice @invocation, @_statement

# a a.a(a) => (invocation (identifier) (func_call (accessor (identifier) (identifier)) (body (identifier)))
maxbrunsfeld commented 8 years ago

Could you share the whole grammar that includes these rules? I'd like to run compile myself and see what I can figure out.

Baael commented 8 years ago

I had issues with precedences, I was trying to fix it in the evening, and now I cannot reproduce bug :) When it will apear again I will place grammar here ASAP.

maxbrunsfeld commented 8 years ago

Ok thanks @Baael. I'm going to close this out for now, but we can reopen if it happens to you again.