I am trying to parse the EBNF below (commentated in the code) and I'm struggling to resolve the STRING part of the optional comments.. (written as extra comment in my test string)
from pyparsing import *
# SQL HINT EBNF
'''
{ /*+ hint [ string ]
[ hint [ string ] ]... */
| --+ hint [ string ]
[ hint [ string ]...
}
'''
test_string = "/*+ALL_ROWS extra comment FIRST_ROWS CACHE*/"
LCOMMENT = Literal("/*+")
RCOMMENT = Literal("*/")
grammar = Forward()
hint_all_rows = Literal("ALL_ROWS")
hint_first_rows = Literal("FIRST_ROWS")
hint_cache = Literal("CACHE")
comment_in_hint = Word(printables)
all_hints = (hint_all_rows | hint_first_rows | hint_cache)+ ZeroOrMore(comment_in_hint)
grammar << all_hints + ZeroOrMore(grammar)
all_grammar = LCOMMENT + grammar + RCOMMENT
p = all_grammar.parseString(test_string)
print p