parse function returns a callable string

The python string returned by the parser are now directly callable.

They accept an optional parameter for the context.
This commit is contained in:
Salvo 'LtWorf' Tomaselli 2015-07-09 23:35:45 +02:00
parent f5e8e442a0
commit 4f85a545bf

View File

@ -79,6 +79,13 @@ class TokenizerException (Exception):
class ParserException (Exception): class ParserException (Exception):
pass pass
class CallableString(str):
def __call__(self, context=None):
'''
context is a dictionary where to
each name is associated the relative relation
'''
return eval(self,context)
class node (object): class node (object):
@ -168,7 +175,7 @@ class node (object):
'''This method converts the expression into a python code string, which '''This method converts the expression into a python code string, which
will require the relation module to be executed.''' will require the relation module to be executed.'''
if self.name in b_operators: if self.name in b_operators:
return '%s.%s(%s)' % (self.left.toPython(), op_functions[self.name], self.right.toPython()) return CallableString('%s.%s(%s)' % (self.left.toPython(), op_functions[self.name], self.right.toPython()))
elif self.name in u_operators: elif self.name in u_operators:
prop = self.prop prop = self.prop
@ -181,10 +188,8 @@ class node (object):
else: # Selection else: # Selection
prop = '\"%s\"' % prop prop = '\"%s\"' % prop
return '%s.%s(%s)' % (self.child.toPython(), op_functions[self.name], prop) return CallableString('%s.%s(%s)' % (self.child.toPython(), op_functions[self.name], prop))
else: return CallableString(self.name)
return self.name
pass
def printtree(self, level=0): def printtree(self, level=0):
'''returns a representation of the tree using indentation''' '''returns a representation of the tree using indentation'''