Back to index

enigmail  1.4.3
Classes | Public Member Functions | Public Attributes | Private Member Functions
Expression.Expression Class Reference

List of all members.

Classes

class  __AST
class  __ASTLeaf
class  ParseError

Public Member Functions

def __init__
def evaluate

Public Attributes

 content
 offset
 e

Private Member Functions

def __get_equality
def __get_unary
def __get_value
def __ignore_whitespace
def __strip

Detailed Description

Definition at line 54 of file Expression.py.


Constructor & Destructor Documentation

def Expression.Expression.__init__ (   self,
  expression_string 
)
Create a new expression with this string.
The expression will already be parsed into an Abstract Syntax Tree.

Definition at line 55 of file Expression.py.

00055 
00056   def __init__(self, expression_string):
00057     """
00058     Create a new expression with this string.
00059     The expression will already be parsed into an Abstract Syntax Tree.
00060     """
00061     self.content = expression_string
00062     self.offset = 0
00063     self.__ignore_whitespace()
00064     self.e = self.__get_equality()
00065     if self.content:
00066       raise Expression.ParseError, self


Member Function Documentation

def Expression.Expression.__get_equality (   self) [private]
Production: unary ( ( '==' | '!=' ) unary ) ?

Definition at line 67 of file Expression.py.

00067 
00068   def __get_equality(self):
00069     """
00070     Production: unary ( ( '==' | '!=' ) unary ) ?
00071     """
00072     if not len(self.content):
00073       return None
00074     rv = Expression.__AST("equality")
00075     # unary 
00076     rv.append(self.__get_unary())
00077     self.__ignore_whitespace()
00078     if not re.match('[=!]=', self.content):
00079       # no equality needed, short cut to our prime unary
00080       return rv[0]
00081     # append operator
00082     rv.append(Expression.__ASTLeaf('op', self.content[:2]))
00083     self.__strip(2)
00084     self.__ignore_whitespace()
00085     rv.append(self.__get_unary())
00086     self.__ignore_whitespace()
00087     return rv

Here is the call graph for this function:

def Expression.Expression.__get_unary (   self) [private]
Production: '!'? value

Definition at line 88 of file Expression.py.

00088 
00089   def __get_unary(self):
00090     """
00091     Production: '!'? value
00092     """
00093     # eat whitespace right away, too
00094     not_ws = re.match('!\s*', self.content)
00095     if not not_ws:
00096       return self.__get_value()
00097     rv = Expression.__AST('not')
00098     self.__strip(not_ws.end())
00099     rv.append(self.__get_value())
00100     self.__ignore_whitespace()
00101     return rv

Here is the call graph for this function:

Here is the caller graph for this function:

def Expression.Expression.__get_value (   self) [private]
Production: ( [0-9]+ | \w+)
Note that the order is important, and the expression is kind-of
ambiguous as \w includes 0-9. One could make it unambiguous by
removing 0-9 from the first char of a string literal.

Definition at line 102 of file Expression.py.

00102 
00103   def __get_value(self):
00104     """
00105     Production: ( [0-9]+ | \w+)
00106     Note that the order is important, and the expression is kind-of
00107     ambiguous as \w includes 0-9. One could make it unambiguous by
00108     removing 0-9 from the first char of a string literal.
00109     """
00110     rv = None
00111     word_len = re.match('[0-9]*', self.content).end()
00112     if word_len:
00113       value = int(self.content[:word_len])
00114       rv = Expression.__ASTLeaf('int', value)
00115     else:
00116       word_len = re.match('\w*', self.content).end()
00117       if word_len:
00118         rv = Expression.__ASTLeaf('string', self.content[:word_len])
00119       else:
00120         raise Expression.ParseError, self
00121     self.__strip(word_len)
00122     self.__ignore_whitespace()
00123     return rv

Here is the call graph for this function:

Here is the caller graph for this function:

Definition at line 124 of file Expression.py.

00124 
00125   def __ignore_whitespace(self):
00126     ws_len = re.match('\s*', self.content).end()
00127     self.__strip(ws_len)
00128     return

Here is the call graph for this function:

Here is the caller graph for this function:

def Expression.Expression.__strip (   self,
  length 
) [private]
Remove a given amount of chars from the input and update
the offset.

Definition at line 129 of file Expression.py.

00129 
00130   def __strip(self, length):
00131     """
00132     Remove a given amount of chars from the input and update
00133     the offset.
00134     """
00135     self.content = self.content[length:]
00136     self.offset += length
  

Here is the caller graph for this function:

def Expression.Expression.evaluate (   self,
  context 
)
Evaluate the expression with the given context

Definition at line 137 of file Expression.py.

00137 
00138   def evaluate(self, context):
00139     """
00140     Evaluate the expression with the given context
00141     """
00142     
00143     # Helper function to evaluate __get_equality results
00144     def eval_equality(tok):
00145       left = opmap[tok[0].type](tok[0])
00146       right = opmap[tok[2].type](tok[2])
00147       rv = left == right
00148       if tok[1].value == '!=':
00149         rv = not rv
00150       return rv
00151     # Mapping from token types to evaluator functions
00152     # Apart from (non-)equality, all these can be simple lambda forms.
00153     opmap = {
00154       'equality': eval_equality,
00155       'not': lambda tok: not opmap[tok[0].type](tok[0]),
00156       'string': lambda tok: context[tok.value],
00157       'int': lambda tok: tok.value}
00158 
00159     return opmap[self.e.type](self.e);
  

Member Data Documentation

Definition at line 60 of file Expression.py.

Definition at line 63 of file Expression.py.

Definition at line 61 of file Expression.py.


The documentation for this class was generated from the following file: