Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
from pyparsing import Literal, CaselessLiteral, Word, delimitedList, Optional, \
Combine, Group, alphas, nums, alphanums, ParseException, Forward, oneOf, quotedString, \
ZeroOrMore, restOfLine, Keyword
# define SQL tokens
command = Forward()
selectStmt = Forward()
insertStmt = Forward()
selectToken = Keyword("select", caseless=True)
insertToken = Keyword("insert", caseless=True)
fromToken = Keyword("from", caseless=True)
intoToken = Keyword("into", caseless=True)
valuesToken = Keyword("values", caseless=True)
ident = Word( alphas, alphanums + "_$" ).setName("identifier")
columnName = ( delimitedList( ident, ".", combine=True ) )
columnNameList = Group( delimitedList( columnName ) )
tableName = ( delimitedList( ident, ".", combine=True ) )
tableNameList = Group( delimitedList( tableName ) )
whereExpression = Forward()
joinExpression = Forward()
and_ = Keyword("and", caseless=True)
or_ = Keyword("or", caseless=True)
in_ = Keyword("in", caseless=True)
on_ = Keyword("on", caseless=True)
E = CaselessLiteral("E")
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")
def _or(values):
output = values[0]
for v in values[1:]:
output |= v
return output
interval = (
Keyword("interval", caseless=True).suppress().setDebugActions(*debug) +
(realNum | intNum)("count").setDebugActions(*debug) +
_or([Keyword(d, caseless=True)("duration") for d in durations])
).addParseAction(to_interval_call).setDebugActions(*debug)
compound = (
Keyword("null", caseless=True).setName("null").setDebugActions(*debug) |
(Keyword("not", caseless=True)("op").setDebugActions(*debug) + expr("params")).addParseAction(to_json_call) |
(Keyword("distinct", caseless=True)("op").setDebugActions(*debug) + expr("params")).addParseAction(to_json_call) |
(Keyword("date", caseless=True).setDebugActions(*debug) + sqlString("params")).addParseAction(to_date_call) |
interval |
case |
(Literal("(").suppress() + ordered_sql + Literal(")").suppress()) |
(Literal("(").suppress() + Group(delimitedList(expr)) + Literal(")").suppress()) |
realNum.setName("float").setDebugActions(*debug) |
intNum.setName("int").setDebugActions(*debug) |
(Literal("~")("op").setDebugActions(*debug) + expr("params")).addParseAction(to_json_call) |
(Literal("-")("op").setDebugActions(*debug) + expr("params")).addParseAction(to_json_call) |
sqlString.setName("string").setDebugActions(*debug) |
call_function |
ident.copy().setName("variable").setDebugActions(*debug)
)
expr << Group(infixNotation(
tupleFilter = (
ZeroOrMore( Keyword("complemented") | Keyword("covering") | Keyword("non-covering") ) +
(Keyword("parent") + (qName | xpathExpression) |
Keyword("ancestor") + (qName | xpathExpression) |
Keyword("sibling") + variableRef) + separator
).setParseAction(compileTupleFilter).ignore(xfsComment).setName("tuple-filter").setDebug(debugParsing)
valueFilter = (
Optional( Keyword("complemented") ) +
Keyword("nilled")
).setParseAction(compileValueFilter).ignore(xfsComment).setName("value-filter").setDebug(debugParsing)
aspectCoverFilter = (
Optional( Keyword("complemented") ) +
Keyword("aspect-cover") +
OneOrMore( Keyword("all") | Keyword("concept") | Keyword("entity-identifier") | Keyword("location") |
Keyword("period") | Keyword("unit") | Keyword("dimensions") |
Keyword("dimension") + (qName | xpathExpression) |
Keyword("exclude-dimension")+ (qName | xpathExpression) ) +
separator
).setParseAction(compileAspectCoverFilter).ignore(xfsComment).setName("aspect-cover-filter").setDebug(debugParsing)
relationAxis = (Keyword("child-or-self") | Keyword("child") | Keyword("descendant-or-self") | Keyword("descendant") |
Keyword("parent-or-self") | Keyword("parent") | Keyword("ancestor-or-self") | Keyword("ancestor") |
Keyword("sibling-or-self") | Keyword("sibling-or-descendant") | Keyword("sibling") )
conceptRelationFilter = (
Optional( Keyword("complemented") ) +
Keyword("concept-relation") + (
(variableRef | qName | xpathExpression) +
return AnnotatedToken(name, t.asList())
return annotation
# Reserved words
ANY = Keyword('ANY')
DEFINED_BY = Keyword('DEFINED BY')
DEFINITIONS = Keyword('DEFINITIONS')
BEGIN = Keyword('BEGIN')
END = Keyword('END')
OPTIONAL = Keyword('OPTIONAL')
DEFAULT = Keyword('DEFAULT')
TRUE = Keyword('TRUE')
FALSE = Keyword('FALSE')
UNIVERSAL = Keyword('UNIVERSAL')
APPLICATION = Keyword('APPLICATION')
PRIVATE = Keyword('PRIVATE')
MIN = Keyword('MIN')
MAX = Keyword('MAX')
IMPLICIT = Keyword('IMPLICIT')
EXPLICIT = Keyword('EXPLICIT')
EXPLICIT_TAGS = Keyword('EXPLICIT TAGS')
IMPLICIT_TAGS = Keyword('IMPLICIT TAGS')
AUTOMATIC_TAGS = Keyword('AUTOMATIC TAGS')
EXTENSIBILITY_IMPLIED = Keyword('EXTENSIBILITY IMPLIED')
COMPONENTS_OF = Keyword('COMPONENTS OF')
ELLIPSIS = Keyword('...')
SIZE = Keyword('SIZE')
OF = Keyword('OF')
IMPORTS = Keyword('IMPORTS')
EXPORTS = Keyword('EXPORTS')
FROM = Keyword('FROM')
PTR +
NAME("typename") +
RPAREN +
LPAREN +
(VOID | pp.delimitedList(ARG, delim=COMMA)("arguments")) +
RPAREN +
SEMI)
# Global variables.
# ------------------------------------------------------------------------------
MJAPI_STRING_ARRAY = (
MJAPI +
EXTERN +
CONST +
pp.Keyword("char") +
PTR +
NAME("name") +
pp.OneOrMore(ARRAY_DIM)("dims") +
SEMI)
MJAPI_FUNCTION_PTR = MJAPI + EXTERN + NAME("typename") + NAME("name") + SEMI
EXPORTS = Keyword('EXPORTS')
FROM = Keyword('FROM')
# Built-in types
SEQUENCE = Keyword('SEQUENCE')
SET = Keyword('SET')
CHOICE = Keyword('CHOICE')
ENUMERATED = Keyword('ENUMERATED')
BIT_STRING = Keyword('BIT STRING')
BOOLEAN = Keyword('BOOLEAN')
REAL = Keyword('REAL')
OCTET_STRING = Keyword('OCTET STRING')
CHARACTER_STRING = Keyword('CHARACTER STRING')
NULL = Keyword('NULL')
INTEGER = Keyword('INTEGER')
OBJECT_IDENTIFIER = Keyword('OBJECT IDENTIFIER')
# Restricted string types
BMPString = Keyword('BMPString')
GeneralString = Keyword('GeneralString')
GraphicString = Keyword('GraphicString')
IA5String = Keyword('IA5String')
ISO646String = Keyword('ISO646String')
NumericString = Keyword('NumericString')
PrintableString = Keyword('PrintableString')
TeletexString = Keyword('TeletexString')
T61String = Keyword('T61String')
UniversalString = Keyword('UniversalString')
UTF8String = Keyword('UTF8String')
VideotexString = Keyword('VideotexString')
VisibleString = Keyword('VisibleString')
Keyword("exclude-dimension")+ (qName | xpathExpression) ) +
separator
).setParseAction(compileAspectCoverFilter).ignore(xfsComment).setName("aspect-cover-filter").setDebug(debugParsing)
relationAxis = (Keyword("child-or-self") | Keyword("child") | Keyword("descendant-or-self") | Keyword("descendant") |
Keyword("parent-or-self") | Keyword("parent") | Keyword("ancestor-or-self") | Keyword("ancestor") |
Keyword("sibling-or-self") | Keyword("sibling-or-descendant") | Keyword("sibling") )
conceptRelationFilter = (
Optional( Keyword("complemented") ) +
Keyword("concept-relation") + (
(variableRef | qName | xpathExpression) +
Optional(Keyword("linkrole") + (quotedString | xpathExpression)) +
Optional(Keyword("arcrole") + (quotedString | xpathExpression)) +
Optional(Keyword("axis") + relationAxis) +
Optional(Keyword("generations") + nonNegativeInteger) +
Optional(Keyword("test") + xpathExpression)
) + separator
).setParseAction(compileConceptRelationFilter).ignore(xfsComment).setName("concept-relation-filter").setDebug(debugParsing)
booleanFilter = (
Optional( Keyword("complemented") ) +
(Keyword("and") | Keyword("or")) + Suppress(Literal("{")) +
OneOrMore(filter) +
Suppress(Literal("}")) +
separator
).setParseAction(compileBooleanFilter).ignore(xfsComment).setName("boolean-filter").setDebug(debugParsing)
declaredFilterReference = ( Keyword("filter") + variableRef ).setParseAction(compileFilterReference).ignore(xfsComment).setName("filter-reference").setDebug(debugParsing)
filters = ( conceptFilter |
CONTAINING = Keyword('CONTAINING').setName('CONTAINING')
ENCODED_BY = Keyword('ENCODED_BY').setName('ENCODED_BY')
IMPLICIT = Keyword('IMPLICIT').setName('IMPLICIT')
EXPLICIT = Keyword('EXPLICIT').setName('EXPLICIT')
OBJECT_IDENTIFIER = Keyword('OBJECT IDENTIFIER').setName('OBJECT IDENTIFIER')
UNIVERSAL = Keyword('UNIVERSAL').setName('UNIVERSAL')
APPLICATION = Keyword('APPLICATION').setName('APPLICATION')
PRIVATE = Keyword('PRIVATE').setName('PRIVATE')
SET = Keyword('SET').setName('SET')
ANY_DEFINED_BY = Keyword('ANY DEFINED BY').setName('ANY DEFINED BY')
EXTENSIBILITY_IMPLIED = Keyword('EXTENSIBILITY IMPLIED').setName(
'EXTENSIBILITY IMPLIED')
BOOLEAN = Keyword('BOOLEAN').setName('BOOLEAN')
TRUE = Keyword('TRUE').setName('TRUE')
FALSE = Keyword('FALSE').setName('FALSE')
CLASS = Keyword('CLASS').setName('CLASS')
WITH_SYNTAX = Keyword('WITH SYNTAX').setName('WITH SYNTAX')
UNIQUE = Keyword('UNIQUE').setName('UNIQUE')
NULL = Keyword('NULL').setName('NULL')
WITH_COMPONENT = Keyword('WITH COMPONENT').setName('WITH COMPONENT')
WITH_COMPONENTS = Keyword('WITH COMPONENTS').setName('WITH COMPONENTS')
COMPONENTS_OF = Keyword('COMPONENTS OF').setName('COMPONENTS OF')
PRESENT = Keyword('PRESENT').setName('PRESENT')
ABSENT = Keyword('ABSENT').setName('ABSENT')
ALL = Keyword('ALL').setName('ALL')
EXCEPT = Keyword('EXCEPT').setName('EXCEPT')
MIN = Keyword('MIN').setName('MIN')
MAX = Keyword('MAX').setName('MAX')
INCLUDES = Keyword('INCLUDES').setName('INCLUDES')
PATTERN = Keyword('PATTERN').setName('PATTERN')
CONSTRAINED_BY = Keyword('CONSTRAINED BY').setName('CONSTRAINED BY')
UNION = Keyword('UNION').setName('UNION')
class CreateEdgeIndex(ParsedCommand):
direction = None
name = None
edge = None
vertex = None
property = None
def to_string(self):
# graph.schema().vertexLabel('reviewer').buildEdgeIndex('ratedByStars', rated).direction(OUT). byPropertyKey('stars').add()
s = self.schema
s += "edge_label = schema.edgeLabel('{}')\n".format(self.edge)
s += "schema.vertexLabel('{vertex}').buildEdgeIndex('{name}', edge_label).direction({direction}).byPropertyKey('{property}').add()".format(vertex=self.vertex, name=self.name, direction=self.direction, property=self.property)
return s
create = Keyword('create', caseless=True)
property = Keyword('property', caseless=True)
vertex = Keyword('vertex', caseless=True)
edge = Keyword('edge', caseless=True)
graph = Keyword('graph', caseless=True)
graphs = Keyword('graphs', caseless=True)
show = Keyword('show', caseless=True)
drop = Keyword("drop", caseless=True)
index = Keyword('index', caseless=True)
label = Keyword('label', caseless=True)
on_ = Keyword("on", caseless=True).suppress()
use = Keyword('use', caseless=True).suppress()
describe = Keyword("desc", caseless=True) | \
Keyword("describe", caseless=True)