Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
SOME = (Suppress(Keyword("SOME")) + Suppress("(") + omcValue + Suppress(")"))
omcString = QuotedString(quoteChar='"', escChar='\\', multiline=True).setParseAction(convertString)
omcNumber = Combine(Optional('-') + ('0' | Word('123456789', nums)) +
Optional('.' + Word(nums)) +
Optional(Word('eE', exact=1) + Word(nums + '+-', nums)))
#ident = Word(alphas + "_", alphanums + "_") | Combine("'" + Word(alphanums + "!#$%&()*+,-./:;<>=?@[]^{}|~ ") + "'")
ident = Word(alphas + "_", alphanums + "_") | QuotedString(quoteChar='\'', escChar='\\').setParseAction(convertString2)
fqident = Forward()
fqident << ((ident + "." + fqident) | ident)
omcValues = delimitedList(omcValue)
omcTuple = Group(Suppress('(') + Optional(omcValues) + Suppress(')')).setParseAction(convertTuple)
omcArray = Group(Suppress('{') + Optional(omcValues) + Suppress('}')).setParseAction(convertTuple)
omcValue << (omcString | omcNumber | omcRecord | omcArray | omcTuple | SOME | TRUE | FALSE | NONE | Combine(fqident))
recordMember = delimitedList(Group(ident + Suppress('=') + omcValue))
omcRecord << Group(Suppress('record') + Suppress(fqident) + Dict(recordMember) + Suppress('end') + Suppress(fqident) + Suppress(';')).setParseAction(convertDict)
omcGrammar = Optional(omcValue) + StringEnd()
omcNumber.setParseAction(convertNumbers)
def parseString(string):
res = omcGrammar.parseString(string)
if len(res) == 0:
return
return res[0]
if __name__ == "__main__":
testdata = """
ivar_section = Suppress("IVAR") + _ivar_section_body
ivar_section.setParseAction(lambda s, l, t: InputVariables(t[0]))
_frozenvar_declaration = (identifier + Suppress(":") + _simple_type_specifier
+ Suppress(";"))
_frozenvar_section_body = OneOrMore(_frozenvar_declaration)
_frozenvar_section_body.setParseAction(lambda s, l, t:
OrderedDict(zip(t[::2], t[1::2])))
frozenvar_section = Suppress("FROZENVAR") + _frozenvar_section_body
frozenvar_section.setParseAction(lambda s, l, t: FrozenVariables(t[0]))
# DEFINE and CONSTANTS
_array_expression = Forward()
_array_expression <<= ( Suppress("[") + Group(delimitedList(next_expression))
+ Suppress("]")
| Suppress("[") + Group(delimitedList(_array_expression))
+ Suppress("]"))
_define = _array_expression | next_expression
_define_declaration = (identifier + Suppress(":=") + _define + Suppress(";"))
def _handle_define_body(s, l, t):
b = OrderedDict()
for identifier, body in zip(t[::2], t[1::2]):
if not isinstance(body, Expression):
body = ArrayExpr(body)
b[identifier] = body
return b
_define_section_body = OneOrMore(_define_declaration)
_define_section_body.setParseAction(_handle_define_body)
define_section = Suppress("DEFINE") + _define_section_body
define_section.setParseAction(lambda s, l, t: Defines(t[0]))
identifier = Word( alphas, alphanums + "_" )
enumname = Word( alphanums + "_" )
integer = ( Combine( CaselessLiteral("0x") + Word( nums+"abcdefABCDEF" ) ) |
Word( nums+"+-", nums ) ).setName("int").setParseAction(cvtInt)
typename = identifier.copy().setParseAction(lambda toks : ptypes.TypeRef(str(toks[0])))
# This is just normal "types", i.e. not channels or messages
typeSpec = Forward()
attributeValue = integer ^ identifier
attribute = Group(Combine ("@" + identifier) + Optional(lparen + delimitedList(attributeValue) + rparen))
attributes = Group(ZeroOrMore(attribute))
arraySizeSpecImage = Group(image_size_ + lparen + integer + comma + identifier + comma + identifier + rparen)
arraySizeSpecBytes = Group(bytes_ + lparen + identifier + comma + identifier + rparen)
arraySizeSpecCString = Group(cstring_ + lparen + rparen)
arraySizeSpec = lbrack + Optional(identifier ^ integer ^ arraySizeSpecImage ^ arraySizeSpecBytes ^arraySizeSpecCString, default="") + rbrack
variableDef = Group(typeSpec + Optional("*", default=None) + identifier + Optional(arraySizeSpec, default=None) + attributes - semi) \
.setParseAction(parseVariableDef)
switchCase = Group(Group(OneOrMore(default_.setParseAction(replaceWith(None)) + colon | Group(case_.suppress() + Optional("!", default="") + identifier) + colon)) + variableDef) \
.setParseAction(lambda toks: ptypes.SwitchCase(toks[0][0], toks[0][1]))
switchBody = Group(switch_ + lparen + delimitedList(identifier,delim='.', combine=True) + rparen + lbrace + Group(OneOrMore(switchCase)) + rbrace + identifier + attributes - semi) \
.setParseAction(lambda toks: ptypes.Switch(toks[0][1], toks[0][2], toks[0][3], toks[0][4]))
messageBody = structBody = Group(lbrace + ZeroOrMore(variableDef | switchBody) + rbrace)
structSpec = Group(struct_ + identifier + structBody + attributes).setParseAction(lambda toks: ptypes.StructType(toks[0][1], toks[0][2], toks[0][3]))
# have to use longest match for type, in case a user-defined type name starts with a keyword type, like "channel_type"
typeSpec << ( structSpec ^ int8_ ^ uint8_ ^ int16_ ^ uint16_ ^
int32_ ^ uint32_ ^ int64_ ^ uint64_ ^
|
(
(_FOREIGN_KEY)("type")
+ _LPAR + Group(delimitedList(Optional(_SUPPRESS_QUOTE) + Word(alphanums + "_") + Optional(_SUPPRESS_QUOTE)))("constraint_columns") + _RPAR
+ Optional(Suppress(_REFERENCES)
+ Optional(_SUPPRESS_QUOTE) + Word(alphanums + "_")("references_table") + Optional(_SUPPRESS_QUOTE)
+ _LPAR + Group(delimitedList(Optional(_SUPPRESS_QUOTE) + Word(alphanums + "_") + Optional(_SUPPRESS_QUOTE)))("references_columns") + _RPAR
)
)
)
)("constraint")
|
Group(
Optional(_SUPPRESS_QUOTE) + Word(alphanums + "_")("name") + Optional(_SUPPRESS_QUOTE)
+ Group(
Group(
Word(alphanums + "_")
+ Optional(CaselessKeyword("WITHOUT TIME ZONE") ^ CaselessKeyword("WITH TIME ZONE") ^ CaselessKeyword("PRECISION") ^ CaselessKeyword("VARYING"))
)("type_name")
+ Optional(_LPAR + Regex(r"[\d\*]+\s*,*\s*\d*")("length") + Optional(_CHAR_SEMANTICS | _BYTE_SEMANTICS)("semantics") + _RPAR)
+ Optional(_TYPE_UNSIGNED)("unsigned")
+ Optional(_TYPE_ZEROFILL)("zerofill")
)("type")
+ Optional(Word(r"\[\]"))("array_brackets")
+ Optional(Regex(_COLUMN_CONSTRAINT, re.IGNORECASE))("constraint")
)("column")
|
_COMMENT
)
)("columns")
_DDL_PARSE_EXPR = Forward()
comma = pp.Literal(",")
lparen = pp.Literal("(")
rparen = pp.Literal(")")
lbrack = pp.Literal("[")
rbrack = pp.Literal("]")
mins = pp.Literal("-")
struct = pp.Keyword('struct')
template = pp.Keyword('template')
final = pp.Keyword('final')("final")
stub = pp.Keyword('stub')("stub")
with_colon = pp.Word(pp.alphanums + "_" + ":")
btype = with_colon
type = pp.Forward()
nestedParens = pp.nestedExpr('<', '>')
tmpl = pp.Group(btype("template_name") + langle.suppress() + pp.Group(pp.delimitedList(type)) + rangle.suppress())
type << (tmpl | btype)
enum_lit = pp.Keyword('enum')
enum_class = pp.Group(enum_lit + cls)
ns = pp.Keyword("namespace")
enum_init = equals.suppress() + pp.Optional(mins) + number
enum_value = pp.Group(identifier + pp.Optional(enum_init))
enum_values = pp.Group(lbrace + pp.delimitedList(enum_value) + pp.Optional(comma) + rbrace)
content = pp.Forward()
member_name = pp.Combine(pp.Group(identifier + pp.Optional(lparen + rparen)))
attrib = pp.Group(lbrack.suppress() + lbrack.suppress() + pp.SkipTo(']') + rbrack.suppress() + rbrack.suppress())
opt_attribute = pp.Optional(attrib)("attribute")
namespace = pp.Group(ns("type") + identifier("name") + lbrace + pp.Group(pp.OneOrMore(content))("content") + rbrace)
enum = pp.Group(enum_class("type") + identifier("name") + colon.suppress() + identifier("underline_type") + enum_values("enum_values") + pp.Optional(semi).suppress())
default_value = equals.suppress() + pp.SkipTo(';')
#-----------------------------------------
list_open = pp.Literal('[').suppress()
list_close = pp.Literal(']').suppress()
dict_open = pp.Literal('{').suppress()
dict_close = pp.Literal('}').suppress()
color_datai = pp.Group(pfloat * 3)
#-----------------------------------------------
ambient_intensity = pp.Literal('ambientIntensity') + pfloat
diffuse_color = pp.Literal('diffuseColor') + color_datai
specular_color = pp.Literal('specularColor') + color_datai
transparency = pp.Literal('transparency') + pfloat
shininess = pp.Literal('shininess') + pfloat
material_values = pp.Group(pp.OneOrMore(
ambient_intensity | diffuse_color | specular_color |
transparency | shininess))
material = (
pword + pp.Literal('Material') +
dict_open +
material_values +
dict_close)
material_values.parseString("""
ambientIntensity 0.210
diffuseColor 0.596 0.667 0.686
specularColor 0.500 0.500 0.500
transparency 0.000
shininess 0.600
""")
material.parseString("""
def combine_nested(opener, closer, content, name=None):
r"""
opener, closer, content = '(', ')', nest_body
"""
ret1 = pp.Forward()
group = pp.Group(opener + pp.ZeroOrMore(content) + closer)
ret2 = ret1 << group
ret3 = ret2
if name is not None:
ret3 = ret2.setResultsName(name)
return ret3
udpInstance = Group(
Group(identifier + Optional(range | subscrRef))
+ LPAR
+ Group(delimitedList(expr))
+ RPAR
)
udpInstantiation = Group(
identifier
- Optional(driveStrength)
+ Optional(delay)
+ delimitedList(udpInstance)
+ SEMI
).setName("udpInstantiation")
parameterValueAssignment = Group(
Literal("#") + LPAR + Group(delimitedList(expr)) + RPAR
)
namedPortConnection = Group(DOT + identifier + LPAR + expr + RPAR).setName(
"namedPortConnection"
) # .setDebug()
assert r".\abc (abc )" == namedPortConnection
modulePortConnection = expr | empty
# ~ moduleInstance = Group( Group ( identifier + Optional(range) ) +
# ~ ( delimitedList( modulePortConnection ) |
# ~ delimitedList( namedPortConnection ) ) )
inst_args = Group(
LPAR
+ (delimitedList(namedPortConnection) | delimitedList(modulePortConnection))
+ RPAR
).setName("inst_args")
moduleInstance = Group(Group(identifier + Optional(range)) + inst_args).setName(
almprop = Group(ALMPROP + ZeroOrMore(proptype) + COLON + propval)
evtprop = Group(EVTPROP + ZeroOrMore(proptype) + COLON + propval).suppress() \
| "CATEGORIES" + COLON + propval.setResultsName("categories") \
| "CLASS" + COLON + propval.setResultsName("class") \
| "DESCRIPTION" + COLON + propval.setResultsName("description") \
| "DTSTART" + proptype + COLON + propval.setResultsName("begin") \
| "DTEND" + proptype + COLON + propval.setResultsName("end") \
| "LOCATION" + COLON + propval.setResultsName("location") \
| "PRIORITY" + COLON + propval.setResultsName("priority") \
| "STATUS" + COLON + propval.setResultsName("status") \
| "SUMMARY" + COLON + propval.setResultsName("summary") \
| "URL" + COLON + propval.setResultsName("url") \
calprops = Group(OneOrMore(calprop)).suppress()
evtprops = Group(OneOrMore(evtprop))
almprops = Group(OneOrMore(almprop)).suppress()
alarm = BEGIN + ALARM + almprops + END + ALARM
event = BEGIN + EVENT + evtprops + Optional(alarm) + END + EVENT
events = Group(OneOrMore(event))
calendar = BEGIN + CALENDAR + calprops + ZeroOrMore(event) + END + CALENDAR
calendars = OneOrMore(calendar)
# PARSE ACTIONS
def gotEvent(s,loc,toks):
for event in toks:
print (event['summary'], "from", event["begin"], "to", event["end"])
event.setParseAction(gotEvent)
Optional(LIMIT.suppress().setDebugActions(*debug) + expr("limit")) +
Optional(OFFSET.suppress().setDebugActions(*debug) + expr("offset"))
)
)
ordered_sql << Group(
Group(Group(
unordered_sql +
ZeroOrMore((UNION_ALL | UNION) + unordered_sql)
)("union"))("from") +
Optional(ORDER_BY.suppress().setDebugActions(*debug) + delimitedList(Group(sortColumn))("orderby").setName("orderby")) +
Optional(LIMIT.suppress().setDebugActions(*debug) + expr("limit")) +
Optional(OFFSET.suppress().setDebugActions(*debug) + expr("offset"))
).addParseAction(to_union_call)
statement = Group(Group(Optional(
WITH.suppress().setDebugActions(*debug) +
delimitedList(
Group(
ident("name").setDebugActions(*debug) +
AS.suppress().setDebugActions(*debug) +
Literal("(").suppress().setDebugActions(*debug) +
ordered_sql("value").setDebugActions(*debug) +
Literal(")").suppress().setDebugActions(*debug)
)
)
))("with") + ordered_sql("query")).addParseAction(to_with_clause)
SQLParser = statement
# IGNORE SOME COMMENTS
oracleSqlComment = Literal("--") + restOfLine