def _create_filter_parser(): and_kw = Keyword('AND') or_kw = Keyword('OR') variable = Literal('?') + Word(alphanums + '_').leaveWhitespace() uri_term = NotAny(Literal('"')) + Word(printables, excludeChars='>*') uri_part = Keyword('*') ^ uri_term ^ variable literal_term = QuotedString(quoteChar='"', escChar='\\') triple = Group(Literal('<').suppress() + uri_part.setResultsName('subj') + uri_part.setResultsName('pred') + (Group(uri_part).setResultsName('obj') ^ Group(literal_term).setResultsName('objlit')) + Literal('>').suppress()) expr = Forward() atom = (triple.setResultsName('triple') | Literal('(').suppress() + expr + Literal(')').suppress()) and_group = Group(atom + ZeroOrMore(and_kw.suppress() + atom)) or_group = Group(atom + ZeroOrMore(or_kw.suppress() + atom)) expr << (and_group.setResultsName('and') ^ or_group.setResultsName('or')) return expr
def SPICE_BNF(): global bnf if not bnf: # punctuation colon = Literal(":").suppress() lbrace = Literal("{").suppress() rbrace = Literal("}").suppress() lbrack = Literal("[").suppress() rbrack = Literal("]").suppress() lparen = Literal("(").suppress() rparen = Literal(")").suppress() equals = Literal("=").suppress() comma = Literal(",").suppress() semi = Literal(";").suppress() # primitive types int8_ = Keyword("int8").setParseAction(replaceWith(ptypes.int8)) uint8_ = Keyword("uint8").setParseAction(replaceWith(ptypes.uint8)) int16_ = Keyword("int16").setParseAction(replaceWith(ptypes.int16)) uint16_ = Keyword("uint16").setParseAction(replaceWith(ptypes.uint16)) int32_ = Keyword("int32").setParseAction(replaceWith(ptypes.int32)) uint32_ = Keyword("uint32").setParseAction(replaceWith(ptypes.uint32)) int64_ = Keyword("int64").setParseAction(replaceWith(ptypes.int64)) uint64_ = Keyword("uint64").setParseAction(replaceWith(ptypes.uint64)) # keywords enum32_ = Keyword("enum32").setParseAction(replaceWith(32)) enum16_ = Keyword("enum16").setParseAction(replaceWith(16)) enum8_ = Keyword("enum8").setParseAction(replaceWith(8)) flags32_ = Keyword("flags32").setParseAction(replaceWith(32)) flags16_ = Keyword("flags16").setParseAction(replaceWith(16)) flags8_ = Keyword("flags8").setParseAction(replaceWith(8)) channel_ = Keyword("channel") server_ = Keyword("server") client_ = Keyword("client") protocol_ = Keyword("protocol") typedef_ = Keyword("typedef") struct_ = Keyword("struct") message_ = Keyword("message") image_size_ = Keyword("image_size") bytes_ = Keyword("bytes") cstring_ = Keyword("cstring") switch_ = Keyword("switch") default_ = Keyword("default") case_ = Keyword("case") 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_ ^ typename ).setName("type") flagsBody = enumBody = Group( lbrace + delimitedList(Group(enumname + Optional(equals + integer))) + Optional(comma) + rbrace ) messageSpec = ( Group(message_ + messageBody + attributes).setParseAction( lambda toks: ptypes.MessageType(None, toks[0][1], toks[0][2]) ) | typename ) channelParent = Optional(colon + typename, default=None) channelMessage = Group( messageSpec + identifier + Optional(equals + integer, default=None) + semi ).setParseAction(lambda toks: ptypes.ChannelMember(toks[0][1], toks[0][0], toks[0][2])) channelBody = channelParent + Group( lbrace + ZeroOrMore(server_ + colon | client_ + colon | channelMessage) + rbrace ) enum_ = enum32_ | enum16_ | enum8_ flags_ = flags32_ | flags16_ | flags8_ enumDef = Group(enum_ + identifier + enumBody + attributes - semi).setParseAction( lambda toks: ptypes.EnumType(toks[0][0], toks[0][1], toks[0][2], toks[0][3]) ) flagsDef = Group(flags_ + identifier + flagsBody + attributes - semi).setParseAction( lambda toks: ptypes.FlagsType(toks[0][0], toks[0][1], toks[0][2], toks[0][3]) ) messageDef = Group(message_ + identifier + messageBody + attributes - semi).setParseAction( lambda toks: ptypes.MessageType(toks[0][1], toks[0][2], toks[0][3]) ) channelDef = Group(channel_ + identifier + channelBody + attributes - semi).setParseAction( lambda toks: ptypes.ChannelType(toks[0][1], toks[0][2], toks[0][3], toks[0][4]) ) structDef = Group(struct_ + identifier + structBody + attributes - semi).setParseAction( lambda toks: ptypes.StructType(toks[0][1], toks[0][2], toks[0][3]) ) typedefDef = Group(typedef_ + identifier + typeSpec + attributes - semi).setParseAction( lambda toks: ptypes.TypeAlias(toks[0][1], toks[0][2], toks[0][3]) ) definitions = typedefDef | structDef | enumDef | flagsDef | messageDef | channelDef protocolChannel = Group(typename + identifier + Optional(equals + integer, default=None) + semi).setParseAction( lambda toks: ptypes.ProtocolMember(toks[0][1], toks[0][0], toks[0][2]) ) protocolDef = Group( protocol_ + identifier + Group(lbrace + ZeroOrMore(protocolChannel) + rbrace) + semi ).setParseAction(lambda toks: ptypes.ProtocolType(toks[0][1], toks[0][2])) bnf = ZeroOrMore(definitions) + protocolDef + StringEnd() singleLineComment = "//" + restOfLine bnf.ignore(singleLineComment) bnf.ignore(cStyleComment) return bnf
def SPICE_BNF(): global bnf if not bnf: # punctuation colon = Literal(":").suppress() lbrace = Literal("{").suppress() rbrace = Literal("}").suppress() lbrack = Literal("[").suppress() rbrack = Literal("]").suppress() lparen = Literal("(").suppress() rparen = Literal(")").suppress() equals = Literal("=").suppress() comma = Literal(",").suppress() semi = Literal(";").suppress() # primitive types int8_ = Keyword("int8").setParseAction(replaceWith(ptypes.int8)) uint8_ = Keyword("uint8").setParseAction(replaceWith(ptypes.uint8)) int16_ = Keyword("int16").setParseAction(replaceWith(ptypes.int16)) uint16_ = Keyword("uint16").setParseAction(replaceWith(ptypes.uint16)) int32_ = Keyword("int32").setParseAction(replaceWith(ptypes.int32)) uint32_ = Keyword("uint32").setParseAction(replaceWith(ptypes.uint32)) int64_ = Keyword("int64").setParseAction(replaceWith(ptypes.int64)) uint64_ = Keyword("uint64").setParseAction(replaceWith(ptypes.uint64)) # keywords channel_ = Keyword("channel") enum32_ = Keyword("enum32").setParseAction(replaceWith(32)) enum16_ = Keyword("enum16").setParseAction(replaceWith(16)) enum8_ = Keyword("enum8").setParseAction(replaceWith(8)) flags32_ = Keyword("flags32").setParseAction(replaceWith(32)) flags16_ = Keyword("flags16").setParseAction(replaceWith(16)) flags8_ = Keyword("flags8").setParseAction(replaceWith(8)) channel_ = Keyword("channel") server_ = Keyword("server") client_ = Keyword("client") protocol_ = Keyword("protocol") typedef_ = Keyword("typedef") struct_ = Keyword("struct") message_ = Keyword("message") image_size_ = Keyword("image_size") bytes_ = Keyword("bytes") cstring_ = Keyword("cstring") switch_ = Keyword("switch") default_ = Keyword("default") case_ = Keyword("case") 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_ ^ typename).setName("type") flagsBody = enumBody = Group( lbrace + delimitedList(Group(enumname + Optional(equals + integer))) + Optional(comma) + rbrace) messageSpec = Group(message_ + messageBody + attributes ).setParseAction(lambda toks: ptypes.MessageType( None, toks[0][1], toks[0][2])) | typename channelParent = Optional(colon + typename, default=None) channelMessage = Group(messageSpec + identifier + Optional(equals + integer, default=None) + semi) \ .setParseAction(lambda toks: ptypes.ChannelMember(toks[0][1], toks[0][0], toks[0][2])) channelBody = channelParent + Group(lbrace + ZeroOrMore( server_ + colon | client_ + colon | channelMessage) + rbrace) enum_ = (enum32_ | enum16_ | enum8_) flags_ = (flags32_ | flags16_ | flags8_) enumDef = Group(enum_ + identifier + enumBody + attributes - semi).setParseAction(lambda toks: ptypes.EnumType( toks[0][0], toks[0][1], toks[0][2], toks[0][3])) flagsDef = Group(flags_ + identifier + flagsBody + attributes - semi).setParseAction(lambda toks: ptypes.FlagsType( toks[0][0], toks[0][1], toks[0][2], toks[0][3])) messageDef = Group(message_ + identifier + messageBody + attributes - semi).setParseAction( lambda toks: ptypes.MessageType( toks[0][1], toks[0][2], toks[0][3])) channelDef = Group(channel_ + identifier + channelBody - semi).setParseAction( lambda toks: ptypes.ChannelType( toks[0][1], toks[0][2], toks[0][3])) structDef = Group(struct_ + identifier + structBody + attributes - semi).setParseAction(lambda toks: ptypes.StructType( toks[0][1], toks[0][2], toks[0][3])) typedefDef = Group(typedef_ + identifier + typeSpec + attributes - semi).setParseAction(lambda toks: ptypes.TypeAlias( toks[0][1], toks[0][2], toks[0][3])) definitions = typedefDef | structDef | enumDef | flagsDef | messageDef | channelDef protocolChannel = Group(typename + identifier + Optional(equals + integer, default=None) + semi) \ .setParseAction(lambda toks: ptypes.ProtocolMember(toks[0][1], toks[0][0], toks[0][2])) protocolDef = Group(protocol_ + identifier + Group(lbrace + ZeroOrMore(protocolChannel) + rbrace) + semi) \ .setParseAction(lambda toks: ptypes.ProtocolType(toks[0][1], toks[0][2])) bnf = ZeroOrMore(definitions) + protocolDef + StringEnd() singleLineComment = "//" + restOfLine bnf.ignore(singleLineComment) bnf.ignore(cStyleComment) return bnf
tokens[1] = date_time del tokens[2] TIMESTAMP = quote + POSITIVE_NUMBER + quote TIMESTAMP.setParseAction(parseTimestamp) UTC_FLAG = Dict( Group( ( int_ + Keyword('inUtc') + equals + UINT_VALUE ).setParseAction(parseFlag) ) ) COMPOSED_DATE = ( date.suppress() + lcurly + int_ + day.suppress() + equals + UINT_VALUE + int_ + month.suppress() + equals + UINT_VALUE + int_ + year.suppress() + equals + UINT_VALUE + rcurly + Optional(COMMENT) ) COMPOSED_DATE.setParseAction(parseDate) COMPOSED_TIME = ( time.suppress() + lcurly + int_ + hour.suppress() + equals + UINT_VALUE + int_ + min_.suppress() + equals + UINT_VALUE + int_ + sec.suppress() + equals + UINT_VALUE + rcurly + Optional(COMMENT) ) COMPOSED_TIME.setParseAction(parseTime)
insert_kw = Keyword('insert', caseless=True) into_kw = Keyword('into', caseless=True) # define sql reserved words reserved_words = ( update_kw | volatile_kw | create_kw | table_kw | as_kw | from_kw | where_kw | join_kw | on_kw | left_kw | right_kw | cross_kw | outer_kw | on_kw | insert_kw | into_kw ) ident = ~reserved_words + Word(alphas, alphanums + '_$.').setName('identifier') function = ( (Word(alphas) + '(' + Word(alphanums + '*_$.') + ')') + Optional(as_kw.suppress() + delimitedList(ident, '.', combine=True)) ) column_name = delimitedList(ident, '.', combine=True) column_name_list = Group(delimitedList(column_name)) # To make it simple, enclode all expressions in parens. expression = "(" + OneOrMore( Word(alphanums + '_-.') | Word('-+*')) + ")" # column from the select statement with `as` keyword support. # the quoted string handles cases like : SELECT 'CA' AS state select_column = ( ( delimitedList(ident, '.', combine=True).setResultsName('parsed_name') | quotedString | expression ) + Optional(as_kw.suppress() + delimitedList(ident, '.', combine=True)).setResultsName('parsed_alias') ).setParseAction(_flat_alias)
# BOOLEAN | # dblQuotedString | # (LBRACE + restOfLine)) # ASGN = VAR + Suppress("=") + VAL + SkipTo(LineEnd()).suppress() # TRACE = OneOrMore(Group(Group(INFO) + SEP.suppress() + Group(ASGN))) # PROP = Suppress(INFO) + STUFF + Suppress(SkipTo(StringEnd())) HEADER = Regex( r'(?:State (?P<state>\d+) )?file (?P<file>[^\s]+) function (?P<function>[^\s]+) line (?P<line>\d+) (?:thread (?P<thread>\d+))?' ) # noqa: E501 HEADER_OLD = Regex( r'(?:State (?P<state>\d+) )?file (?P<file>[^\s]+) line (?P<line>\d+) function (?P<function>[^\s]+) (?:thread (?P<thread>\d+))?' ) # noqa: E501 SEP = Keyword("----------------------------------------------------") ASGN = Regex(r'(?P<lhs>[^\s=]+)=(?P<rhs>.+)') TRACE = OneOrMore(Group(Group(HEADER) + SEP.suppress() + Group(ASGN))).ignore( OneOrMore(SKIP)) # noqa: E501 TRACE_OLD = OneOrMore(Group(Group(HEADER_OLD) + SEP.suppress() + Group(ASGN))).ignore(OneOrMore(SKIP)) # noqa: E501 PROP = Suppress(SkipTo(LineEnd())) + Suppress(SkipTo( LineStart())) + STUFF + Suppress(SkipTo(StringEnd())) # noqa: E501 def pprint_agent(info, tid): return f"{info.spawn[int(tid)]} {tid}" def translateCPROVER54(cex, info): yield from translateCPROVER(cex, info, parser=TRACE_OLD)
lambda s, l, t: DropGraph(name=t.name)) describe_graph = (describe + graph).\ setParseAction(lambda s, l, t: DescribeGraph()) def vi(s,l,t): return CreateVertexIndex(label=t.label, name=t.index_name, fields=t.fields, type=t.type) create_vertex_index = (create + index_type('type') + index + \ ident('index_name') + on_ + vertex.suppress() + ident('label') + lparen + delimitedList(ident, ",")('fields') + rparen).\ setParseAction(vi) def cei(s, l, t): return CreateEdgeIndex(direction=t.direction.upper(), name=t.name, edge=t.edge, vertex=t.vertex, property=t.property) create_edge_index = (create + direction("direction") + index + ident('name') + on_ + vertex + ident('vertex') + on_ + edge + ident('edge') +
natural_kw = Keyword('natural', caseless=True) on_kw = Keyword('on', caseless=True) insert_kw = Keyword('insert', caseless=True) into_kw = Keyword('into', caseless=True) # define sql reserved words reserved_words = (update_kw | volatile_kw | create_kw | table_kw | as_kw | from_kw | where_kw | join_kw | on_kw | left_kw | right_kw | cross_kw | outer_kw | on_kw | insert_kw | into_kw) ident = ~reserved_words + Word(alphas, alphanums + '_$.').setName('identifier') function = ( (Word(alphas) + '(' + Word(alphanums + '*_$.') + ')') + Optional(as_kw.suppress() + delimitedList(ident, '.', combine=True))) column_name = delimitedList(ident, '.', combine=True) column_name_list = Group(delimitedList(column_name)) # To make it simple, enclode all expressions in parens. expression = "(" + OneOrMore(Word(alphanums + '_-.') | Word('-+*')) + ")" # column from the select statement with `as` keyword support. # the quoted string handles cases like : SELECT 'CA' AS state select_column = ( (delimitedList(ident, '.', combine=True).setResultsName('parsed_name') | quotedString | expression) + Optional(as_kw.suppress() + delimitedList(ident, '.', combine=True) ).setResultsName('parsed_alias')).setParseAction(_flat_alias)