[GRASS-SVN] r69025 - grass/branches/releasebranch_7_2/lib/python/temporal

svn_grass at osgeo.org svn_grass at osgeo.org
Fri Jul 22 05:33:52 PDT 2016


Author: huhabla
Date: 2016-07-22 05:33:52 -0700 (Fri, 22 Jul 2016)
New Revision: 69025

Modified:
   grass/branches/releasebranch_7_2/lib/python/temporal/temporal_operator.py
Log:
temporal framework: Better error handling for temporal operators


Modified: grass/branches/releasebranch_7_2/lib/python/temporal/temporal_operator.py
===================================================================
--- grass/branches/releasebranch_7_2/lib/python/temporal/temporal_operator.py	2016-07-22 10:57:47 UTC (rev 69024)
+++ grass/branches/releasebranch_7_2/lib/python/temporal/temporal_operator.py	2016-07-22 12:33:52 UTC (rev 69025)
@@ -24,32 +24,32 @@
     >>> p = TemporalOperatorParser()
     >>> expression =  "{&&, during}"
     >>> p.parse(expression, optype = 'boolean')
-    >>> print((p.relations, p.temporal, p.function,p.aggregate))
+    >>> print((p.relations, p.temporal, p.function, p.aggregate))
     (['during'], 'l', '&&', '&')
     >>> p = TemporalOperatorParser()
     >>> expression =  "{||, equal | during}"
     >>> p.parse(expression, optype = 'boolean')
-    >>> print((p.relations, p.temporal, p.function,p.aggregate))
+    >>> print((p.relations, p.temporal, p.function, p.aggregate))
     (['equal', 'during'], 'l', '||', '|')
     >>> p = TemporalOperatorParser()
     >>> expression =  "{||, equal | during, &}"
     >>> p.parse(expression, optype = 'boolean')
-    >>> print((p.relations, p.temporal, p.function,p.aggregate))
+    >>> print((p.relations, p.temporal, p.function, p.aggregate))
     (['equal', 'during'], 'l', '||', '&')
     >>> p = TemporalOperatorParser()
     >>> expression =  "{&&, during, |}"
     >>> p.parse(expression, optype = 'boolean')
-    >>> print((p.relations, p.temporal, p.function,p.aggregate))
+    >>> print((p.relations, p.temporal, p.function, p.aggregate))
     (['during'], 'l', '&&', '|')
     >>> p = TemporalOperatorParser()
     >>> expression =  "{&&, during, |, r}"
     >>> p.parse(expression, optype = 'boolean')
-    >>> print((p.relations, p.temporal, p.function,p.aggregate))
+    >>> print((p.relations, p.temporal, p.function, p.aggregate))
     (['during'], 'r', '&&', '|')
     >>> p = TemporalOperatorParser()
     >>> expression =  "{&&, during, u}"
     >>> p.parse(expression, optype = 'boolean')
-    >>> print((p.relations, p.temporal, p.function,p.aggregate))
+    >>> print((p.relations, p.temporal, p.function, p.aggregate))
     (['during'], 'u', '&&', '&')
     >>> p = TemporalOperatorParser()
     >>> expression =  "{:, during, r}"
@@ -101,6 +101,36 @@
     >>> p.parse(expression, optype = 'overlay')
     >>> print((p.relations, p.temporal, p.function))
     (['overlaps', 'overlapped'], 'r', '^')
+    >>> p = TemporalOperatorParser()
+    >>> expression =  "{&&, equal | during | contains | starts, &}"
+    >>> p.parse(expression, optype = 'boolean')
+    >>> print((p.relations, p.temporal, p.function, p.aggregate))
+    (['equal', 'during', 'contains', 'starts'], 'l', '&&', '&')
+    >>> p = TemporalOperatorParser()
+    >>> expression =  "{&&, equal | during | contains | starts, &&&&&}"
+    >>> p.parse(expression, optype = 'boolean')
+    Traceback (most recent call last):
+    SyntaxError: Unexpected syntax error in expression "{&&, equal | during | contains | starts, &&&&&}" at position 42 near &
+    >>> p = TemporalOperatorParser()
+    >>> expression =  "{+, starting}"
+    >>> p.parse(expression)
+    Traceback (most recent call last):
+    SyntaxError: syntax error on line 1 position 4 near 'starting'
+    >>> p = TemporalOperatorParser()
+    >>> expression =  "{nope, start, |, l}"
+    >>> p.parse(expression)
+    Traceback (most recent call last):
+    SyntaxError: syntax error on line 1 position 1 near 'nope'
+    >>> p = TemporalOperatorParser()
+    >>> expression =  "{++, start, |, l}"
+    >>> p.parse(expression)
+    Traceback (most recent call last):
+    SyntaxError: Unexpected syntax error in expression "{++, start, |, l}" at position 2 near +
+    >>> p = TemporalOperatorParser()
+    >>> expression =  "{^, over, right}"
+    >>> p.parse(expression, optype='rter')
+    Traceback (most recent call last):
+    SyntaxError: Unknown optype rter, must be one of ['select', 'boolean', 'raster', 'hash', 'relation', 'overlay']
 
 """
 from __future__ import print_function
@@ -163,7 +193,7 @@
     t_T_NOT_SELECT   = r'!:'
     t_COMMA          = r','
     t_LEFTREF        = '^[l|left]'
-    t_RIGHTREF       ='^[r|right]'
+    t_RIGHTREF       = '^[r|right]'
     t_UNION          = '^[u|union]'
     t_DISJOINT       = '^[d|disjoint]'
     t_INTERSECT      = '^[i|intersect]'
@@ -182,7 +212,7 @@
     t_CRPAREN        = r'\}'
 
     # These are the things that should be ignored.
-    t_ignore = ' \t'
+    t_ignore = ' \t\n'
 
     # Track line numbers.
     def t_newline(self, t):
@@ -191,8 +221,7 @@
 
     def t_NAME(self, t):
         r'[a-zA-Z_][a-zA-Z_0-9]*'
-        self.temporal_symbol(t)
-        return t
+        return self.temporal_symbol(t)
 
     # Parse symbols
     def temporal_symbol(self, t):
@@ -214,14 +243,14 @@
         elif t.value == 'i' or t.value == 'intersect':
             t.value = 'i'
             t.type = 'INTERSECT'
-        #else:
-        #    t.type = 'NAME'
+        else:
+            self.t_error(t)
         return(t)
 
     # Handle errors.
     def t_error(self, t):
-        raise SyntaxError("syntax error on line %d near '%s'" %
-                          (t.lineno, t.value))
+        raise SyntaxError("syntax error on line %d position %i near '%s'" %
+                          (t.lineno, t.lexpos, t.value))
 
     # Build the lexer
     def build(self,**kwargs):
@@ -246,12 +275,14 @@
         self.lexer = TemporalOperatorLexer()
         self.lexer.build()
         self.parser = yacc.yacc(module=self)
-        self.relations = None   # Temporal relations equals, contain, during, ...
-        self.temporal  = None   # Temporal operation like intersect, left, right, ...
-        self.function  = None   # Actual operation
-        self.aggregate = None   # Aggregation function
+        self.relations = None   # Temporal relations (equals, contain, during, ...)
+        self.temporal  = None   # Temporal operation (intersect, left, right, ...)
+        self.function  = None   # Actual operation (+, -, /, *, ... )
+        self.aggregate = None   # Aggregation function (|, &)
 
-    def parse(self, expression,  optype='relation'):
+        self.optype_list = ["select", "boolean", "raster", "hash", "relation", "overlay"]
+
+    def parse(self, expression, optype='relation'):
         """Parse the expression and fill the object variables
 
         :param expression:
@@ -259,30 +290,38 @@
                        - select   { :, during,   r}
                        - boolean  {&&, contains, |}
                        - raster   { *, equal,    |}
-                       - vector   { |, starts,   &}
+                       - overlay  { |, starts,   &}
                        - hash     { #, during,   l}
                        - relation {during}
         :return:
         """
         self.optype = optype
+
+        if optype not in self.optype_list:
+            raise SyntaxError("Unknown optype %s, must be one of %s"%(self.optype, str(self.optype_list)))
+        self.expression = expression
         self.parser.parse(expression)
-        #
 
     # Error rule for syntax errors.
     def p_error(self, t):
-        raise SyntaxError("Unexpected syntax error")
+        raise SyntaxError("Unexpected syntax error in expression"
+                          " \"%s\" at position %i near %s"%(self.expression,
+                                                            t.lexpos,
+                                                            t.value))
 
     # Get the tokens from the lexer class
     tokens = TemporalOperatorLexer.tokens
 
     def p_relation_operator(self, t):
+        # {during}
+        # {during | equal | starts}
         """
         operator : CLPAREN relation CRPAREN
                  | CLPAREN relationlist CRPAREN
         """
         # Check for correct type.
         if not self.optype == 'relation':
-            raise SyntaxError("invalid syntax")
+            raise SyntaxError("Wrong optype \"%s\" must be \"relation\""%self.optype)
         else:
             # Set three operator components.
             if isinstance(t[2], list):
@@ -295,6 +334,8 @@
             t[0] = t[2]
 
     def p_relation_bool_operator(self, t):
+        # {||, during}
+        # {&&, during | equal | starts}
         """
         operator : CLPAREN OR  OR  COMMA relation     CRPAREN
                  | CLPAREN AND AND COMMA relation     CRPAREN
@@ -302,7 +343,7 @@
                  | CLPAREN AND AND COMMA relationlist CRPAREN
         """
         if not self.optype == 'boolean':
-            raise SyntaxError("invalid syntax")
+            raise SyntaxError("Wrong optype \"%s\" must be \"boolean\""%self.optype)
         else:
             # Set three operator components.
             if isinstance(t[5], list):
@@ -316,6 +357,8 @@
             t[0] = t[2]
 
     def p_relation_bool_combi_operator(self, t):
+        # {||, during, &}
+        # {&&, during | equal | starts, |}
         """
         operator : CLPAREN OR  OR  COMMA relation     COMMA OR  CRPAREN
                  | CLPAREN OR  OR  COMMA relation     COMMA AND CRPAREN
@@ -327,7 +370,7 @@
                  | CLPAREN AND AND COMMA relationlist COMMA AND CRPAREN
         """
         if not self.optype == 'boolean':
-            raise SyntaxError("invalid syntax")
+            raise SyntaxError("Wrong optype \"%s\" must be \"boolean\""%self.optype)
         else:
             # Set three operator components.
             if isinstance(t[5], list):
@@ -341,6 +384,8 @@
             t[0] = t[2]
 
     def p_relation_bool_combi_operator2(self, t):
+        # {||, during, left}
+        # {&&, during | equal | starts, union}
         """
         operator : CLPAREN OR  OR  COMMA relation     COMMA temporal CRPAREN
                  | CLPAREN AND AND COMMA relation     COMMA temporal CRPAREN
@@ -348,7 +393,7 @@
                  | CLPAREN AND AND COMMA relationlist COMMA temporal CRPAREN
         """
         if not self.optype == 'boolean':
-            raise SyntaxError("invalid syntax")
+            raise SyntaxError("Wrong optype \"%s\" must be \"boolean\""%self.optype)
         else:
             # Set three operator components.
             if isinstance(t[5], list):
@@ -362,6 +407,8 @@
             t[0] = t[2]
 
     def p_relation_bool_combi_operator3(self, t):
+        # {||, during, |, left}
+        # {&&, during | equal | starts, &, union}
         """
         operator : CLPAREN OR  OR  COMMA relation     COMMA OR  COMMA temporal CRPAREN
                  | CLPAREN OR  OR  COMMA relation     COMMA AND COMMA temporal CRPAREN
@@ -373,7 +420,7 @@
                  | CLPAREN AND AND COMMA relationlist COMMA AND COMMA temporal CRPAREN
         """
         if not self.optype == 'boolean':
-            raise SyntaxError("invalid syntax")
+            raise SyntaxError("Wrong optype \"%s\" must be \"relation\""%self.optype)
         else:
             # Set three operator components.
             if isinstance(t[5], list):
@@ -387,6 +434,10 @@
             t[0] = t[2]
 
     def p_select_relation_operator(self, t):
+        # {!:}
+        # { :, during}
+        # {!:, during | equal | starts}
+        # { :, during | equal | starts, l}
         """
         operator : CLPAREN select CRPAREN
                  | CLPAREN select COMMA relation     CRPAREN
@@ -395,7 +446,7 @@
                  | CLPAREN select COMMA relationlist COMMA temporal CRPAREN
         """
         if not self.optype == 'select':
-            raise SyntaxError("invalid syntax")
+            raise SyntaxError("Wrong optype \"%s\" must be \"select\""%self.optype)
         else:
             if len(t) == 4:
                 # Set three operator components.
@@ -419,6 +470,10 @@
             t[0] = t[2]
 
     def p_hash_relation_operator(self, t):
+        # {#}
+        # {#, during}
+        # {#, during | equal | starts}
+        # {#, during | equal | starts, l}
         """
         operator : CLPAREN HASH CRPAREN
                  | CLPAREN HASH COMMA relation     CRPAREN
@@ -427,7 +482,7 @@
                  | CLPAREN HASH COMMA relationlist COMMA temporal CRPAREN
         """
         if not self.optype == 'hash':
-            raise SyntaxError("invalid syntax")
+            raise SyntaxError("Wrong optype \"%s\" must be \"hash\""%self.optype)
         else:
             if len(t) == 4:
                 # Set three operator components.
@@ -451,7 +506,10 @@
             t[0] = t[2]
 
     def p_raster_relation_operator(self, t):
-        # The expression should always return a list of maps.
+        # {+}
+        # {-, during}
+        # {*, during | equal | starts}
+        # {/, during | equal | starts, l}
         """
         operator : CLPAREN arithmetic CRPAREN
                  | CLPAREN arithmetic COMMA relation     CRPAREN
@@ -460,7 +518,7 @@
                  | CLPAREN arithmetic COMMA relationlist COMMA temporal CRPAREN
         """
         if not self.optype == 'raster':
-            raise SyntaxError("invalid syntax")
+            raise SyntaxError("Wrong optype \"%s\" must be \"raster\""%self.optype)
         else:
             if len(t) == 4:
                 # Set three operator components.
@@ -484,7 +542,10 @@
             t[0] = t[2]
 
     def p_overlay_relation_operator(self, t):
-        # The expression should always return a list of maps.
+        # {+}
+        # {-, during}
+        # {~, during | equal | starts}
+        # {^, during | equal | starts, l}
         """
         operator : CLPAREN overlay CRPAREN
                  | CLPAREN overlay COMMA relation     CRPAREN
@@ -493,7 +554,7 @@
                  | CLPAREN overlay COMMA relationlist COMMA temporal CRPAREN
         """
         if not self.optype == 'overlay':
-            raise SyntaxError("invalid syntax")
+            raise SyntaxError("Wrong optype \"%s\" must be \"overlay\""%self.optype)
         else:
             if len(t) == 4:
                 # Set three operator components.
@@ -534,7 +595,7 @@
         t[0] = t[1]
 
     def p_over(self, t):
-        # The list of relations.
+        # The the over keyword
         """
         relation : OVER
         """



More information about the grass-commit mailing list