Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
cSymbolPrefix = pp.alphas + '_'
cSymbolSuffix = cSymbolPrefix + pp.nums
cNumbersDec = pp.nums
cNumbersHex = pp.hexnums
cNumbersBin = '01'
cNumbersOct = '01234567'
cModuleName = pp.alphas + '_' + pp.nums + '.'
## token types
# symbols
tModule = pp.Word(cModuleName)
tModuleSeparator = pp.oneOf('! `')
tSymbol = pp.Combine(pp.Word(cSymbolPrefix) + pp.Optional(pp.Word(cSymbolSuffix)))
# integer types
tInteger = pp.Word(pp.nums)
tZero = pp.Literal('0')
tBaseBinary = pp.Literal('y')
tBaseHex = pp.Literal('x')
tBaseOctal = pp.Literal('o')
# grouping
tBegin, tEnd = pp.Literal('('), pp.Literal(')')
# operations
tPlus = pp.Literal('+')
tMinus = pp.Literal('-')
tMultiply = pp.Literal('*')
tDivide = pp.Literal('/')
tXor = pp.Literal('^')
tOr = pp.Literal('|')
tAnd = pp.Literal('&')
# ParserElement.enablePackrat()
def Command(char):
return oneOf([char.upper(), char.lower()])
def Arguments(token):
return Group(token)
def Sequence(token):
""" A sequence of the token"""
return OneOrMore(token + maybeComma)
digit_sequence = Word(nums)
sign = oneOf("+ -")
def convertToFloat(s, loc, toks):
try:
return float(toks[0])
except Exception:
raise ParseException(loc, "invalid float format %s" % toks[0])
exponent = CaselessLiteral("e") + Optional(sign) + Word(nums)
# note that almost all these fields are optional,
# and this can match almost anything. We rely on Pythons built-in
# float() function to clear out invalid values - loosely matching like this
columnNameList = Group( delimitedList( columnName ) )
tableName = Upcase( delimitedList( ident, ".", combine=True ) )
tableNameList = Group( delimitedList( tableName ) )
whereExpression = Forward()
and_ = Keyword("and", caseless=True)
or_ = Keyword("or", caseless=True)
in_ = Keyword("in", caseless=True)
E = CaselessLiteral("E")
binop = oneOf("= != < > >= <= eq ne lt le gt ge", caseless=True)
arithSign = Word("+-",exact=1)
realNum = Combine( Optional(arithSign) + ( Word( nums ) + "." + Optional( Word(nums) ) |
( "." + Word(nums) ) ) +
Optional( E + Optional(arithSign) + Word(nums) ) )
intNum = Combine( Optional(arithSign) + Word( nums ) +
Optional( E + Optional("+") + Word(nums) ) )
columnRval = realNum | intNum | quotedString | columnName # need to add support for alg expressions
whereCondition = Group(
( columnName + binop + columnRval ) |
( columnName + in_ + "(" + delimitedList( columnRval ) + ")" ) |
( columnName + in_ + "(" + selectStmt + ")" ) |
( "(" + whereExpression + ")" )
)
whereExpression << whereCondition + ZeroOrMore( ( and_ | or_ ) + whereExpression )
# define the grammar
selectStmt << ( selectToken +
( '*' | columnNameList ).setResultsName( "columns" ) +
fromToken +
tableNameList.setResultsName( "tables" ) +
syntax=Word(NONDIGIT , DIGIT + NONDIGIT) ^ QIDENT.ebnf(),
action=lambda s,l,t: IDENT(t[0])
)
QIDENT.ebnf(
syntax=Suppress("'") + OneOrMore(QCHAR ^ SESCAPE) + Suppress("'"),
action=lambda s,l,t: QIDENT(t[0])
)
STRING.ebnf(
syntax=Suppress('"') + Combine(OneOrMore(SCHAR ^ SESCAPE)) + Suppress('"'),
action=lambda s,l,t: STRING(t[0])
)
UNSIGNED_INTEGER.ebnf(
syntax=Word(nums),
action=lambda s,l,t: UNSIGNED_INTEGER(t[0])
)
UNSIGNED_NUMBER.ebnf(
syntax = Combine(UNSIGNED_INTEGER.ebnf() + Optional(Literal(".") + Optional(UNSIGNED_INTEGER.ebnf()))).setResultsName("mantissa") + Optional((Literal("e") ^ Literal("E")) + Optional(Literal("+") ^ Literal("-")).setResultsName('exponent_sign') + Combine(UNSIGNED_INTEGER.ebnf()).setResultsName('exponent')),
action=lambda s,l,t: UNSIGNED_NUMBER(**dict(t))
)
key=toks[2]
else:
n=0
key=toks[1]
assert(dict.has_key(key))
#increment dict[key] by n
out=[str(a+n) for a in dict[key]]
#print toks, "dict->", out
return out
point = Literal(".")
e = CaselessLiteral("E")
inum = Combine(Word("+-"+nums,nums)+
Optional(e+Word( "+-"+nums, nums ) ) )
fnum = Combine( Word( "+-"+nums, nums ) +
Optional(point+Optional(Word(nums))) +
Optional(e+Word("+-"+nums,nums ) ) )
semi = Literal(";")
lpar = Literal("(").suppress()
rpar = Literal(")").suppress()
callpar = Literal("=")+Optional(inum)+Word(alphas,max=1)
defpar = Literal("=")+Word(alphas,max=1)
comment = semi + Optional(restOfLine)
terms=Forward()
atom=fnum | (lpar+ terms+rpar)
seq=(atom+":"+atom).setParseAction(ev1)
rep=(inum+"*"+atom).setParseAction(ev2)
terms << OneOrMore(rep | seq | atom)
numlist=OneOrMore(terms|(defpar+terms+
defpar).setParseAction(ev3) |
callpar.setParseAction(ev4))
#!
#Startup configuration:
#!
#ver 07.5.05hT53
#!
#module 1 bi-0-port-m4-management-module
#module 2 bi-8-port-gig-module
#there is a lot more that foundry is including in the output that should be ignored
interface_keyword = pp.Keyword("interface")
unwanted = pp.SkipTo(interface_keyword, include=False).suppress()
#unwanted = pp.ZeroOrMore(bang ^ comment ^ aaa_line ^ module_line ^ startup_line ^ ver_line)
octet = pp.Word(pp.nums, max=3)
ipaddr = pp.Combine(octet + "." + octet + "." + octet + "." + octet)
address = ipaddr
netmask = ipaddr
cidr = pp.Literal("/").suppress() + pp.Word(pp.nums, max=2)
# Description
desc_keyword = pp.Keyword("description")
description = pp.Dict( pp.Group(desc_keyword + pp.Group(pp.restOfLine)) )
# Addresses
#cisco example:
# ip address 172.29.188.27 255.255.255.224 secondary
#
#foundry example:
# ip address 10.62.161.187/26
'>': "gt",
'&': "and",
'|': "or",
}
# Both are of comparison to access in same manner when evaluating
comparison = (lt | le | eq | ne | ge | gt).setResultsName("comparison")
stringComparison = (eq | ne).setResultsName("comparison")
#
#quoted string is already present
float_string = Word(nums).setResultsName("value").setParseAction(lambda t: float(t[0]))
integer_string = Word(nums).setResultsName("value").setParseAction(lambda t: int(t[0]))
#TODO: use numString, and make integer if fiull stop
#TODO: allow parentheses? - should be ok as pass to the python parser
ipField = Word(nums, max=3)
ipAddress = Combine( ipField + "." + ipField + "." + ipField + "." + ipField ).setResultsName("ipAddress")
boolean_and = Literal("&").setResultsName("&")
boolean_or = Literal("|").setResultsName("|")
boolean = (boolean_and | boolean_or).setResultsName("boolean")
self._boolean = boolean # need to use in checking
#TODO fix this matching 2a.ab when that should match a string
numericQuery = Group(attribute + comparison + float_string).setResultsName( "numericQuery")
stringValues = (attribute_unnamed | quotedString.setParseAction(removeQuotes)
).setResultsName("value")
stringQuery = Group(attribute + stringComparison + stringValues).setResultsName( "stringQuery")
wildcardQuery = wildcard.setResultsName("wildcardQuery")
def jsParse(inStr):
# This disaster is a context-free grammar parser for parsing javascript object literals.
# It needs to be able to handle a lot of the definitional messes you find in in-the-wild
# javascript object literals.
# Unfortunately, Javascript is /way/ more tolerant then JSON when it comes to object literals
# so we can't just parse objects using python's `json` library.
TRUE = pp.Keyword("true").setParseAction( pp.replaceWith(True) )
FALSE = pp.Keyword("false").setParseAction( pp.replaceWith(False) )
NULL = pp.Keyword("null").setParseAction( pp.replaceWith(None) )
jsonString = pp.quotedString.setParseAction( pp.removeQuotes )
jsonNumber = pp.Combine( pp.Optional('-') + ( '0' | pp.Word('123456789',pp.nums) ) +
pp.Optional( '.' + pp.Word(pp.nums) ) +
pp.Optional( pp.Word('eE',exact=1) + pp.Word(pp.nums+'+-',pp.nums) ) )
jsonObject = pp.Forward()
jsonValue = pp.Forward()
jsonDict = pp.Forward()
jsonArray = pp.Forward()
jsonElements = pp.Forward()
rawText = pp.Regex('[a-zA-Z_$][0-9a-zA-Z_$]*')
commaToNull = pp.Word(',,', exact=1).setParseAction(pp.replaceWith(None))
jsonElements << pp.ZeroOrMore(commaToNull) + pp.Optional(jsonObject) + pp.ZeroOrMore((pp.Suppress(',') + jsonObject) | commaToNull)
jsonValue << ( jsonString | jsonNumber | TRUE | FALSE | NULL )
super(CaselessPreservingLiteral,self).__init__( matchString.upper() )
self.name = "'%s'" % matchString
self.errmsg = "Expected " + self.name
def parseImpl( self, instring, loc, doActions=True ):
test = instring[ loc:loc+self.matchLen ]
if test.upper() == self.match:
return loc+self.matchLen, test
#~ raise ParseException( instring, loc, self.errmsg )
raise ParseException( instring, loc, self.errmsg, self )
def Sequence(token):
""" A sequence of the token"""
return OneOrMore(token+maybeComma)
digit_sequence = Word(nums)
sign = oneOf("+ -")
def convertToFloat(s, loc, toks):
try:
return float(toks[0])
except:
raise ParseException(loc, "invalid float format %s"%toks[0])
exponent = CaselessLiteral("e")+Optional(sign)+Word(nums)
#note that almost all these fields are optional,
#and this can match almost anything. We rely on Pythons built-in
#float() function to clear out invalid values - loosely matching like this
#speeds up parsing quite a lot
floatingPointConstant = Combine(
def abc_grammar (): # header, voice and lyrics grammar for ABC
#-----------------------------------------------------------------
# expressions that catch and skip some syntax errors (see corresponding parse expressions)
#-----------------------------------------------------------------
b1 = Word (u"-,'<>\u2019#", exact=1) # catch misplaced chars in chords
b2 = Regex ('[^H-Wh-w~=]*') # same in user defined symbol definition
b3 = Regex ('[^=]*') # same, second part
#-----------------------------------------------------------------
# ABC header (field_str elements are matched later with reg. epr's)
#-----------------------------------------------------------------
number = Word (nums).setParseAction (lambda t: int (t[0]))
field_str = Regex (r'[^]]*') # match anything until end of field
field_str.setParseAction (lambda t: t[0].strip ()) # and strip spacing
userdef_symbol = Word (srange ('[H-Wh-w~]'), exact=1)
fieldId = oneOf ('K L M Q P I T C O A Z N G H R B D F S E r Y') # info fields
X_field = Literal ('X') + Suppress (':') + field_str
U_field = Literal ('U') + Suppress (':') + b2 + Optional (userdef_symbol, 'H') + b3 + Suppress ('=') + field_str
V_field = Literal ('V') + Suppress (':') + Word (alphanums + '_') + field_str
inf_fld = fieldId + Suppress (':') + field_str
ifield = Suppress ('[') + (X_field | U_field | V_field | inf_fld) + Suppress (']')
abc_header = OneOrMore (ifield) + StringEnd ()
#---------------------------------------------------------------------------------
# I:score with recursive part groups and {* grand staff marker
#---------------------------------------------------------------------------------