cjheath / treetop

A Ruby-based parsing DSL based on parsing expression grammars.

Home Page:https://cjheath.github.io/treetop

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Generated Ruby code contains invalid synthax in Ruby 3.0

andsel opened this issue · comments

Using the following grammar:

my_grammar.treetop

grammar MyGrammar
  rule config
    (_ plugin_section)*  
  end

  rule comment
    (whitespace? "#" [^\r\n]* "\r"? "\n")+ 
  end

  rule _
    (comment / whitespace)* 
  end

  rule whitespace
    [ \t\r\n]+ 
  end

  rule plugin_section
    plugin_type _ "{"
      _ (plugin _)*
    "}"
    
  end

  rule plugin_type
    ("input" / "filter" / "output")
  end

  rule plugin
    name 
  end

  rule name
    (
      ([A-Za-z0-9_-]+ )
    )
  end
end

and compiling with

tt my_grammar.treetop

Generates my_grammar.rb which contains method named _1 which is not anymore valid syntax in Ruby 3.0, was deprecated since Ruby 2.7

module PluginSection1
    def plugin_type
      elements[0]
    end

    def _1
      elements[1]
    end

    def _2
      elements[3]
    end

  end

Usage of methods named with _, which now represents "numbered parameters" generates an error like:

_1 is reserved for numbered parameter

Rename rule _. I can't make a global change to the names of accessor methods.

Thanks @cjheath that worked!

Not an issue in the library