ruby / racc

Racc is an LALR(1) parser generator. It is written in Ruby itself, and generates ruby programs.
Other
541 stars 88 forks source link

Add more grammars #222

Closed nurse closed 6 months ago

nurse commented 1 year ago

This PR adds following new grammars which allow users to write a grammar shorter. I think these extensions are very straight forward as ANTLR4 shows these in a example on their top page.

With the extension, here is an example:

class Parser
rule

singleStatement
    : querySpecification EOF

querySpecification
    : SELECT (DISTINCT | ALL)? identifier (',' identifier)*
    {
      result = val
    }

identifier
    : IDENTIFIER
end
---- header
require 'strscan'

---- inner
def parse(str)
  @ss = StringScanner.new(str)
  @eof = false
  do_parse
end
TOKEN = %w[
  SELECT
  DISTINCT
  ALL
]

def next_token
  return nil if @eof
  @ss.skip(/\s+/)
  case token = @ss.scan(/\w+|[,]/)
  when nil
    @eof = true
    [:EOF, nil]
  when *TOKEN
    [token.intern, token]
  when /[A-Za-z_][A-Za-z0-9_@:]*/
    [:IDENTIFIER, token]
  else
    [token, token]
  end
end
tenderlove commented 1 year ago

💯

I would definitely use this, and I think it would simplify some real world Racc grammars like this one.

nobu commented 1 year ago

Should we bump up to 1.8, or 2.0?