Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add more grammars #222

Merged
merged 4 commits into from Apr 21, 2024
Merged

Add more grammars #222

merged 4 commits into from Apr 21, 2024

Conversation

nurse
Copy link
Member

@nurse nurse commented Jun 12, 2023

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.

  • Option: ?
  • Many: *
  • Grouping: ( )

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

* Option: ?
* Many: *
* Many1: +
* Grouping: ( ... )
@tenderlove
Copy link
Member

💯

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

@nobu
Copy link
Member

nobu commented Oct 31, 2023

Should we bump up to 1.8, or 2.0?

@nurse nurse requested a review from yui-knk January 5, 2024 14:27
@yui-knk yui-knk merged commit b41adfb into ruby:master Apr 21, 2024
26 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants