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
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: