Explorar el Código

Make sure that compiler also works on Py3

Yentl Van Tendeloo hace 7 años
padre
commit
2ed6ac19d1

+ 2 - 5
interface/HUTN/hutn_compiler/compiler.py

@@ -35,14 +35,11 @@ def md5digest(data):
     hasher.update(data)
     return hasher.hexdigest()
 
-def fetch_cached(data, mode=None):
+def fetch_cached(data):
     try:
         md5 = md5digest(data)
         cache_folder = os.path.abspath("%s/../caches/" % (os.path.dirname(os.path.abspath(__file__))))
-        if mode is None:
-            picklefile = cache_folder + "/%s.pickle" % md5
-        else:
-            picklefile = cache_folder + "/%s_%s.pickle" % (mode, md5)
+        picklefile = cache_folder + "/%s.pickle" % md5
         with open(picklefile, "rb") as f:
             return pickle.load(f)
     except:

+ 11 - 2
interface/HUTN/hutn_compiler/hutnparser.py

@@ -23,6 +23,7 @@ Modifications by Daniel Riegelhaupt:
 """
 
 import re
+import sys
 
 from copy import deepcopy 
 
@@ -30,6 +31,13 @@ from hutn_compiler.position import Position
 
 line_cache = {}
 
+def get_buffer(base, start):
+    if sys.version_info >= (3, ):
+        # TODO find an efficient implementation for Python3...
+        return base[start:]
+    else:
+        return buffer(base, start)
+
 class Tree(object):
     def __init__(self, head, tail, startpos, endpos, inputfile = None):
         self.head = head
@@ -505,7 +513,8 @@ class Parser(object):
             #lr = Parser.LR([], rule, None, deepcopy(self.lrstack))
             lr = Parser.LR([], rule, None, None if not self.lrstack else self.lrstack.copy())
             self.lrstack = lr
-            self.memotable.update({(rule, j): [{'tree': lr, 'startpos': j, 'endpos': j}]})
+            #self.memotable.update({(rule, j): [{'tree': lr, 'startpos': j, 'endpos': j}]})
+            self.memotable[(rule, j)] = [{'tree': lr, 'startpos': j, 'endpos': j}]
 
             newresults = self.eval(rule, j)
             self.lrstack = self.lrstack.next
@@ -645,7 +654,7 @@ class Parser(object):
 
         rule = self.tokens[rulename]
         #mobj = re.match(rule['reg'], self.input[j:])
-        mobj = re.match(rule['reg'], buffer(self.input, j))
+        mobj = re.match(rule['reg'], get_buffer(self.input, j))
         #Changed by daniel instead of re.match(reg) did re.match(re.compile(reg).patern)
         #this is to avoid problems with \ before i did this i had the match the character \ by doing [\\\\]
         # because to write only two slashes it would have to be r'[\\]' which cant be done directly in hte grammar so it had to be in string form