Browse Source

Trying to fix the negation AssertionError (work in progress).

Sander Mathijs van Veen 14 years ago
parent
commit
76b7149de4
4 changed files with 27 additions and 8 deletions
  1. 0 3
      src/node.py
  2. 13 3
      src/parser.py
  3. 4 2
      src/rules/negation.py
  4. 10 0
      tests/test_parser.py

+ 0 - 3
src/node.py

@@ -165,9 +165,6 @@ class ExpressionNode(Node, ExpressionBase):
         self.type = TYPE_OPERATOR
         self.op = OP_MAP[args[0]]
 
-        if hasattr(self.op, '__call__'):
-            self.op = self.op(args)
-
     def __str__(self):  # pragma: nocover
         return generate_line(self)
 

+ 13 - 3
src/parser.py

@@ -74,13 +74,20 @@ class Parser(BisonParser):
         BisonParser.__init__(self, **kwargs)
         self.interactive = kwargs.get('interactive', 0)
         self.timeout = kwargs.get('timeout', 0)
-        self.possibilities = self.last_possibilities = []
 
+        self.reset()
+
+    def reset(self):
         self.read_buffer = ''
         self.read_queue = Queue.Queue()
 
         self.subtree_map = {}
         self.root_node = None
+        self.possibilities = self.last_possibilities = []
+
+    def run(self, *args, **kwargs):
+        self.reset()
+        return super(Parser, self).run(*args, **kwargs)
 
     # Override default read method with a version that prompts for input.
     def read(self, nbytes):
@@ -201,6 +208,7 @@ class Parser(BisonParser):
             handlers = []
 
         if retval.negated:
+            print retval, 'OP_NEG', retval.negated
             handlers += RULES[OP_NEG]
 
         for handler in handlers:
@@ -372,6 +380,10 @@ class Parser(BisonParser):
         if option == 4:  # rule: exp MINUS exp
             node = values[2]
             node.negated += 1
+
+            # Explicit call the hook handler on the created unary negation.
+            node = self.hook_handler('binary', 4, names, values, node)
+
             return Node('+', values[0], node)
 
         raise BisonSyntaxError('Unsupported option %d in target "%s".'
@@ -415,8 +427,6 @@ class Parser(BisonParser):
             yylloc.first_column = yycolumn; \
             yylloc.last_column = yycolumn + yyleng; \
             yycolumn += yyleng;
-
-    /*[a-zA-Z][0-9]+ { returntoken(CONCAT_POW); }*/
     %}
 
     %option yylineno

+ 4 - 2
src/rules/negation.py

@@ -1,4 +1,4 @@
-from ..node import Scope, nary_node, OP_ADD, OP_MUL, OP_DIV
+from ..node import Scope, OP_ADD, OP_MUL, OP_DIV, TYPE_OPERATOR
 from ..possibilities import Possibility as P, MESSAGES
 from ..translate import _
 
@@ -46,7 +46,9 @@ def match_negate_polynome(node):
     --a       ->  a
     -(a + b)  ->  -a - b
     """
-    assert node.negated
+    if not node.negated and node.type == TYPE_OPERATOR:
+        print 'operator\'s negated childs:', [n.negated for n in node]
+    assert node.negated, str(node.negated) + '; ' + str(node)
 
     p = []
 

+ 10 - 0
tests/test_parser.py

@@ -23,3 +23,13 @@ class TestParser(unittest.TestCase):
 
     def test_line(self):
         self.assertEqual(line(Parser, '4-a'), '4 - a')
+
+    def test_reset_after_failure(self):
+        parser = ParserWrapper(Parser)
+        parser.run(['-(3a+6b)'])
+        possibilities = parser.parser.possibilities
+        print possibilities
+
+        parser.run(['5+2*6'])
+        possibilities = parser.parser.possibilities
+        print possibilities