@ -291,7 +291,21 @@ def _find_matching_parenthesis(expression, start=0, openpar=u'(', closepar=u')')
close parenthesis to the 1 st open parenthesis found
close parenthesis to the 1 st open parenthesis found
starting from start ( 0 by default ) '''
starting from start ( 0 by default ) '''
par_count = 0 # Count of parenthesis
par_count = 0 # Count of parenthesis
string = False
escape = False
for i in range ( start , len ( expression ) ) :
for i in range ( start , len ( expression ) ) :
if expression [ i ] == ' \' ' and not escape :
string = not string
if expression [ i ] == ' \\ ' and not escape :
escape = True
else :
escape = False
if string :
continue
if expression [ i ] == openpar :
if expression [ i ] == openpar :
par_count + = 1
par_count + = 1
elif expression [ i ] == closepar :
elif expression [ i ] == closepar :
@ -299,6 +313,30 @@ def _find_matching_parenthesis(expression, start=0, openpar=u'(', closepar=u')')
if par_count == 0 :
if par_count == 0 :
return i # Closing parenthesis of the parameter
return i # Closing parenthesis of the parameter
def _find_token ( haystack , needle ) :
'''
Like the string function find , but
ignores tokens that are within a string
literal .
'''
r = - 1
string = False
escape = False
for i in range ( len ( haystack ) ) :
if haystack [ i ] == ' \' ' and not escape :
string = not string
if haystack [ i ] == ' \\ ' and not escape :
escape = True
else :
escape = False
if string :
continue
if haystack [ i : ] . startswith ( needle ) :
return i
return r
def tokenize ( expression ) :
def tokenize ( expression ) :
''' This function converts a relational expression into a list where
''' This function converts a relational expression into a list where
@ -331,7 +369,7 @@ def tokenize(expression):
par = expression . find (
par = expression . find (
' ( ' , _find_matching_parenthesis ( expression ) )
' ( ' , _find_matching_parenthesis ( expression ) )
else : # Expression without parenthesis, so adding what's between start and parenthesis as whole
else : # Expression without parenthesis, so adding what's between start and parenthesis as whole
par = expression. find ( ' ( ' )
par = _find_token( expression , ' ( ' )
items . append ( expression [ : par ] . strip ( ) )
items . append ( expression [ : par ] . strip ( ) )
# Inserting parameter of the operator
# Inserting parameter of the operator