[Python-checkins] python/nondist/sandbox/ast asdl.py,1.3,1.4 asdl_c.py,1.2,1.3

jhylton@sourceforge.net jhylton@sourceforge.net
Tue, 09 Apr 2002 20:14:00 -0700


Update of /cvsroot/python/python/nondist/sandbox/ast
In directory usw-pr-cvs1:/tmp/cvs-serv5173

Modified Files:
	asdl.py asdl_c.py 
Log Message:
Move VisitorBase from asdl_c to asdl.

Add Check visitor in asdl that looks for errors.  So far the only
error checking is to verify that the same constructor name isn't used
more than once (even in different types).  It's not sensible to reuse
names, because you couldn't tell which type was implied from the
constructor alone.




Index: asdl.py
===================================================================
RCS file: /cvsroot/python/python/nondist/sandbox/ast/asdl.py,v
retrieving revision 1.3
retrieving revision 1.4
diff -C2 -d -r1.3 -r1.4
*** asdl.py	9 Apr 2002 19:13:53 -0000	1.3
--- asdl.py	10 Apr 2002 03:13:58 -0000	1.4
***************
*** 11,14 ****
--- 11,17 ----
  #__metaclass__ = type
  
+ import os
+ import traceback
+ 
  import spark
  
***************
*** 300,303 ****
--- 303,376 ----
          return "Product(%s)" % self.fields
  
+ class VisitorBase(object):
+ 
+     def __init__(self, skip=0):
+         self.cache = {}
+         self.skip = skip
+ 
+     def visit(self, object, *args):
+         meth = self._dispatch(object)
+         if meth is None:
+             return
+         try:
+             meth(object, *args)
+         except Exception, err:
+             print "Error visiting", repr(object)
+             print err
+             traceback.print_exc()
+             # XXX hack
+             if hasattr(self, 'file'):
+                 self.file.flush()
+             os._exit(1)
+ 
+     def _dispatch(self, object):
+         assert isinstance(object, AST), repr(object)
+         klass = object.__class__
+         meth = self.cache.get(klass)
+         if meth is None:
+             methname = "visit" + klass.__name__
+             if self.skip:
+                 meth = getattr(self, methname, None)
+             else:
+                 meth = getattr(self, methname)
+             self.cache[object.__class__] = meth
+         return meth
+ 
+ class Check(VisitorBase):
+ 
+     def __init__(self):
+         super(Check, self).__init__(skip=1)
+         self.cons = {}
+         self.errors = 0
+ 
+     def visitModule(self, mod):
+         for dfn in mod.dfns:
+             self.visit(dfn)
+ 
+     def visitType(self, type):
+         self.visit(type.value, type.name)
+ 
+     def visitSum(self, sum, name):
+         for t in sum.types:
+             self.visit(t, name)
+ 
+     def visitConstructor(self, cons, name):
+         key = str(cons.name)
+         conflict = self.cons.get(key)
+         if conflict is None:
+             self.cons[key] = name
+         else:
+             print "Redefinition of constructor %s" % key
+             print "Defined in %s and %s" % (conflict, name)
+             self.errors += 1
+ 
+ def check(mod):
+     v = Check()
+     v.visit(mod)
+     if v.errors:
+         return False
+     else:
+         return True
+ 
  def parse(file):
      scanner = ASDLScanner()
***************
*** 328,331 ****
          print "module", mod.name
          print len(mod.dfns), "definitions"
!         for dfn in mod.dfns:
!             print dfn
--- 401,407 ----
          print "module", mod.name
          print len(mod.dfns), "definitions"
!         if not check(mod):
!             print "Check failed"
!         else:
!             for dfn in mod.dfns:
!                 print dfn.type

Index: asdl_c.py
===================================================================
RCS file: /cvsroot/python/python/nondist/sandbox/ast/asdl_c.py,v
retrieving revision 1.2
retrieving revision 1.3
diff -C2 -d -r1.2 -r1.3
*** asdl_c.py	9 Apr 2002 19:13:53 -0000	1.2
--- asdl_c.py	10 Apr 2002 03:13:58 -0000	1.3
***************
*** 24,53 ****
          return "%s_ty" % name
  
- class VisitorBase(object):
- 
-     def __init__(self):
-         self.cache = {}
- 
-     def visit(self, object, *args):
-         meth = self._dispatch(object)
-         try:
-             meth(object, *args)
-         except Exception, err:
-             print "Error visiting", repr(object)
-             print err
-             traceback.print_exc()
-             self.file.flush()
-             os._exit(1)
- 
-     def _dispatch(self, object):
-         assert isinstance(object, asdl.AST), repr(object)
-         klass = object.__class__
-         meth = self.cache.get(klass)
-         if meth is None:
-             methname = "visit" + klass.__name__
-             meth = getattr(self, methname)
-             self.cache[object.__class__] = meth
-         return meth
- 
  def reflow_lines(s, depth):
      """Reflow the line s indented depth tabs.
--- 24,27 ----
***************
*** 83,87 ****
      return lines
  
! class EmitVisitor(VisitorBase):
      """Visit that emits lines"""
  
--- 57,61 ----
      return lines
  
! class EmitVisitor(asdl.VisitorBase):
      """Visit that emits lines"""