Commits

Anonymous committed 42418ae

Apply pep8 and pyflakes fixes.

  • Participants
  • Parent commits 79fd758

Comments (0)

Files changed (5)

File src/coldwater.py

 from unlikely.parser import ClassBaseParser
 from unlikely.stdlib import stdlib
 
+
 def load(filename, options):
     f = open(filename, "r")
     scanner = Scanner(f.read())
         print "---AST---"
         print str(stdlib)
 
+
 def main(argv):
-    optparser = OptionParser("[python] coldwater.py {options} {source.dtr}\n" + __doc__)
+    usage = "[python] coldwater.py {options} {source.unlikely}"
+    optparser = OptionParser(usage + "\n" + __doc__)
     optparser.add_option("-a", "--dump-ast",
                          action="store_true", dest="dump_ast", default=False,
                          help="dump AST after source is parsed")

File src/unlikely/ast.py

 $Id: ast.py 318 2010-01-07 01:49:38Z cpressey $
 """
 
+
 class ArtefactExistsError(Exception):
-    """An exception indicating that a proposed artefact (class, method, property, ...) already exists."""
+    """An exception indicating that a proposed artefact (class, method,
+    property, ...) already exists.
+
+    """
     pass
 
+
 class ArtefactNotFoundError(Exception):
-    """An exception indicating that a needed artefact (class, method, property, ...) does not exist."""
+    """An exception indicating that a needed artefact (class, method,
+    property, ...) does not exist.
+
+    """
     pass
 
+
 class BadModifierError(Exception):
     """An exception indicating that a specified modifier is not valid."""
     pass
 
+
 class IncompatibleTypeError(Exception):
-    """An exception indicating that the types of two connected subexpressions not compatible."""
-    pass
+    """An exception indicating that the types of two connected subexpressions
+    are not compatible.
 
-class ClassRelationshipError(Exception):
-    """An exception indicating that the specified relationship between two classes is illegal."""
-    pass
-
-
-class AST:
-    """
-    Class representing nodes in an abstract syntax tree.
     """
     pass
 
 
+class ClassRelationshipError(Exception):
+    """An exception indicating that the specified relationship between two
+    classes is illegal.
+
+    """
+    pass
+
+
+class AST:
+    """Class representing nodes in an abstract syntax tree."""
+    pass
+
+
 class ClassBase(AST):
-    """
-    A collection of Unlikely class definitions.
-    """
+    """A collection of Unlikely class definitions."""
     def __init__(self):
         self.class_defn_map = {}
-    
+
     def __str__(self):
         s = ""
         for class_name in self.class_defn_map:
             s = s + str(self.class_defn_map[class_name]) + " "
         return "ClassBase { " + s + "}"
 
-    def add_class_defn_by_name(self, class_name, superclass_name=None, modifiers=None):
-        """
-        A factory method.  Call this instead of ClassDefn().
+    def add_class_defn_by_name(self, class_name, superclass_name=None,
+                               modifiers=None):
+        """A factory method.  Call this instead of ClassDefn().
         If a class was declared forward, this will return the stub.
         The third and fourth arguments are conveniences for stdlib.
+
         """
         if class_name in self.class_defn_map:
             class_defn = self.class_defn_map[class_name]
             class_defn.set_superclass_by_name(superclass_name)
         return class_defn
 
-    def add_forward_class_defn_by_name(self, class_name):
-        if class_name in self.class_defn_map:
-            raise ArtefactExistsError, "class " + class_name
-        superclass_defn = self.lookup_class_defn(superclass_name)
-        class_defn = ClassDefn(self, class_name, superclass_defn, modifiers)
-        self.class_defn_map[class_name] = class_defn
-        return class_defn
-
     def lookup_class_defn(self, class_name):
         if class_name in self.class_defn_map:
             return self.class_defn_map[class_name]
-        raise ArtefactNotFoundError, "class " + class_name
+        raise ArtefactNotFoundError("class " + class_name)
 
 
 class ClassDefn(AST):
         superclass = self.classbase.lookup_class_defn(superclass_name)
         if not self.has_modifier("forcible"):
             if superclass.has_modifier("final"):
-                raise ClassRelationshipError, "cannot inherit from final " + superclass_name
-        if self.superclass is not None and self.superclass.name != superclass_name:
-            raise ClassRelationshipError, "class " + self.name + " already has superclass " + self.superclass.name
+                raise ClassRelationshipError("cannot inherit from final " +
+                                             superclass_name)
+        if (self.superclass is not None and
+            self.superclass.name != superclass_name):
+            raise ClassRelationshipError("class " + self.name +
+                                         " already has superclass " +
+                                         self.superclass.name)
         self.superclass = superclass
         if len(self.dependant_names) == 0:
             for dependant_name in superclass.dependant_names:
                 self.dependant_names.append(dependant_name)
-                self.dependant_map[dependant_name] = superclass.dependant_map[dependant_name]
+                self.dependant_map[dependant_name] = \
+                  superclass.dependant_map[dependant_name]
         return superclass
 
     def add_dependant_by_name(self, dependant_name):
         if dependant_name in self.dependant_map:
-            raise ClassRelationshipError, "dependant " + dependant_name + " already declared"
+            raise ClassRelationshipError("dependant " + dependant_name +
+                                         " already declared")
         dependant = self.classbase.lookup_class_defn(dependant_name)
         self.dependant_map[dependant.name] = dependant
         self.dependant_names.append(dependant.name)
             self.prop_defn_map[prop_name] = prop_defn
             prop_defn.type_class_defn = self.lookup_class_defn(type_class_name)
             return prop_defn
-        raise ArtefactExistsError, "property " + prop_defn.name
+        raise ArtefactExistsError("property " + prop_defn.name)
 
     def add_method_defn_by_name(self, method_name):
         """
         Factory method.  Call this instead of MethodDefn().
         """
         if method_name in self.method_defn_map:
-            raise ArtefactExistsError, "method " + method_defn.name
+            raise ArtefactExistsError("method " + method_name)
         try:
             overridden_method_defn = self.lookup_method_defn(method_name)
         except ArtefactNotFoundError:
             overridden_method_defn = None
-        if self.is_saturated() and overridden_method_defn is None and self.superclass is not None:
-            raise ClassRelationshipError, "new method " + method_name + " not allowed on saturated " + self.name
+        if (self.is_saturated() and overridden_method_defn is None and
+            self.superclass is not None):
+            raise ClassRelationshipError("new method " + method_name +
+                                         " not allowed on saturated " +
+                                         self.name)
         method_defn = MethodDefn(self, method_name)
         self.method_defn_map[method_defn.name] = method_defn
         return method_defn
 
     def add_modifier(self, modifier):
-        if modifier not in ["final", "saturated", "abstract","forcible"]:
-            raise BadModifierError, modifier
+        if modifier not in ["final", "saturated", "abstract", "forcible"]:
+            raise BadModifierError(modifier)
         self.modifiers.append(modifier)
 
     def has_modifier(self, modifier):
         return True
 
     def lookup_class_defn(self, class_name):
-        """
-        Note that this first looks up the class definition in the dependant classes
-        of this class: all classes referred to by a class *must* be injected!
-        And then the dependants of the superclass of this class.
-        This doesn't apply for final classes, since injecting them doesn't make any sense.
+        """Note that this first looks up the class definition in the dependant
+        classes of this class: all classes referred to by a class *must* be
+        injected!  And then the dependants of the superclass of this class.
+        This doesn't apply for final classes, since injecting them doesn't
+        make any sense.
+
         """
         if class_name[0].isdigit():
             class_defn = self.classbase.add_class_defn_by_name(class_name)
         class_defn = self.classbase.lookup_class_defn(class_name)
         if class_defn is not None and not class_defn.must_be_injected():
             return class_defn
-        raise ArtefactNotFoundError, "dependant class " + class_name
+        raise ArtefactNotFoundError("dependant class " + class_name)
 
     def lookup_prop_defn(self, prop_name):
         if prop_name in self.prop_defn_map:
             return self.prop_defn_map[prop_name]
         if self.superclass is not None:
             return self.superclass.lookup_prop_defn(prop_name)
-        raise ArtefactNotFoundError, "property " + prop_name
+        raise ArtefactNotFoundError("property " + prop_name)
 
     def lookup_method_defn(self, method_name):
         if method_name in self.method_defn_map:
             return self.method_defn_map[method_name]
         if self.superclass is not None:
             return self.superclass.lookup_method_defn(method_name)
-        raise ArtefactNotFoundError, "method " + method_name
+        raise ArtefactNotFoundError("method " + method_name)
 
     def is_subclass_of(self, class_defn):
         if self == class_defn:
         if not self.has_modifier("abstract"):
             for method_defn_name in map:
                 if map[method_defn_name].has_modifier("abstract"):
-                    message = "concrete class " + self.name + " does not implement abstract method " + method_defn_name
-                    raise ClassRelationshipError, message
+                    message = ("concrete class " + self.name +
+                               " does not implement abstract method " +
+                               method_defn_name)
+                    raise ClassRelationshipError(message)
         else:
             all_concrete = True
             for method_defn_name in map:
                 if map[method_defn_name].has_modifier("abstract"):
                     all_concrete = False
             if all_concrete:
-                raise ClassRelationshipError, "abstract class " + self.name + " has no abstract methods"
+                raise ClassRelationshipError("abstract class " + self.name +
+                                             " has no abstract methods")
 
 
 class PropDefn(AST):
         Factory method.  Call this instead of ParamDecl().
         """
         if param_name in self.param_decl_map:
-            raise ArtefactExistsError, "param " + param_name
+            raise ArtefactExistsError("param " + param_name)
         prop_defn = self.lookup_prop_defn(param_name)
         type_class_defn = self.lookup_class_defn(type_class_name)
         if prop_defn.type_class_defn != type_class_defn:
-            raise IncompatibleTypeError, param_name + " param is a " + type_class_name + " but property is a " + prop_defn.type_class_defn.name
+            raise IncompatibleTypeError(param_name + " param is a " +
+                                        type_class_name +
+                                        " but property is a " +
+                                        prop_defn.type_class_defn.name)
         param_decl = ParamDecl(self, param_name, type_class_defn)
         self.param_decl_map[param_name] = param_decl
         self.param_names.append(param_name)
 
     def add_modifier(self, modifier):
         if modifier not in ["abstract"]:
-            raise BadModifierError, modifier
+            raise BadModifierError(modifier)
         self.modifiers.append(modifier)
 
     def has_modifier(self, modifier):
 
     def lookup_prop_defn(self, prop_name):
         return self.class_defn.lookup_prop_defn(prop_name)
-    
+
     def get_param_decl_by_index(self, index):
         param_name = self.param_names[index]
         param_decl = self.param_decl_map[param_name]
 
     def typecheck(self):
         if len(self.param_exprs) != len(self.method_defn.param_names):
-            message = "continue provides " + str(len(self.param_exprs))
-            message += " params, " + str(len(self.method_defn.param_names)) + " needed"
-            raise IncompatibleTypeError, message
+            message = ("continue provides " + str(len(self.param_exprs)) +
+                       " params, " + str(len(self.method_defn.param_names)) +
+                       " needed")
+            raise IncompatibleTypeError(message)
         i = 0
         for param_expr in self.param_exprs:
             param_decl = self.method_defn.get_param_decl_by_index(i)
             arg_type_class_defn = param_expr.get_type_class_defn()
             param_type_class_defn = param_decl.type_class_defn
             if not arg_type_class_defn.is_subclass_of(param_type_class_defn):
-                message = arg_type_class_defn.name + " not a subclass of " + param_type_class_defn.name
-                raise IncompatibleTypeError, message
+                message = (arg_type_class_defn.name + " not a subclass of " +
+                           param_type_class_defn.name)
+                raise IncompatibleTypeError(message)
             i += 1
 
 
     def __init__(self, parent, type_class_name):
         assert isinstance(parent, Assignment) or isinstance(parent, Continue)
         self.parent = parent
-        self.type_class_defn = self.parent.method_defn.lookup_class_defn(type_class_name)
+        self.type_class_defn = \
+          self.parent.method_defn.lookup_class_defn(type_class_name)
         self.dependencies = []
 
     def add_dependency_by_name(self, class_name):
 
     def typecheck(self):
         if len(self.dependencies) != len(self.type_class_defn.dependant_names):
-            message = "instantiation specifies " + str(len(self.dependencies))
-            message += " classes, " + str(len(self.type_class_defn.dependant_names)) + " needed ("
-            message += ",".join(self.type_class_defn.dependant_names) + ")"
-            raise IncompatibleTypeError, message
+            message = ("instantiation specifies " +
+                       str(len(self.dependencies)) + " classes, " +
+                       str(len(self.type_class_defn.dependant_names)) +
+                       " needed (" +
+                       ",".join(self.type_class_defn.dependant_names) + ")")
+            raise IncompatibleTypeError(message)
         i = 0
         for dependency in self.dependencies:
-            dependant_class_defn = self.type_class_defn.get_dependant_by_index(i)
+            dependant_class_defn = \
+              self.type_class_defn.get_dependant_by_index(i)
             if not dependency.is_subclass_of(dependant_class_defn):
-                message = dependency.name + " not a subclass of " + dependant_class_defn.name
-                raise IncompatibleTypeError, message
+                message = (dependency.name + " not a subclass of " +
+                           dependant_class_defn.name)
+                raise IncompatibleTypeError(message)
             i += 1
 
 

File src/unlikely/parser.py

 Based on the following EBNF grammar:
 
 ClassBase   ::= {ClassDefn}.
-ClassDefn   ::= "class" ClassName<NEW> "(" [ClassName {"," ClassName}] ")" "extends" ClassName
-                ["{" {PropDefn} {MethodDefn} "}"] ["is" ClassMod {"and" ClassMod}].
+ClassDefn   ::= "class" ClassName<NEW> "(" [ClassName {"," ClassName}] ")"
+                "extends" ClassName ["{" {PropDefn} {MethodDefn} "}"]
+                ["is" ClassMod {"and" ClassMod}].
 ClassMod    ::= "final" | "saturated" | "abstract".
 PropDefn    ::= ClassName PropName<NEW> ";".
 MethodDefn  ::= "method" MethodName<NEW> "(" [ParamDecl {"," ParamDecl}] ")"
 Expr        ::= ConstrExpr | QualName.
 ConstrExpr  ::= "new" (ClassName) "(" [ClassName {"," ClassName}] ")".
 QualName    ::= PropName {"." PropName}.
-Constant    ::= <<sequence of decimal digits>> | <<sequence of arbitrary characters between double quotes>>.
+Constant    ::= <<sequence of decimal digits>>
+              | <<sequence of arbitrary characters between double quotes>>.
 """
 
-import ast
 
 class Parser(object):
     """A recursive-descent parser for Unlikely.
 
 
 class ClassDefnParser(Parser):
-    # ClassDefn ::= "class" ClassName<NEW> "(" [ClassName {"," ClassName}] ")" "extends" ClassName
-    #               ["{" {PropDefn} {MethodDefn} "}"] ["is" ClassMod {"and" ClassMod}].
-
     def __init__(self, scanner, classbase):
         Parser.__init__(self, scanner)
         self.classbase = classbase
 
 
 class PropDefnParser(Parser):
-    # PropDefn ::= ClassName PropName<NEW> ";".
-
     def __init__(self, scanner, class_defn):
         Parser.__init__(self, scanner)
         self.class_defn = class_defn
 
 
 class MethodDefnParser(Parser):
-    # MethodDefn ::= "method" MethodName<NEW> "(" [ParamDecl {"," ParamDecl}] ")"
-    #                ("{" {Assignment} Continue "}" | "is" "abstract").
-
     def __init__(self, scanner, class_defn):
         Parser.__init__(self, scanner)
         self.class_defn = class_defn
-  
+
     def parse(self):
         self.scanner.expect("method")
         method_name = self.scanner.grab()
                 self.scanner.expect("and")
                 method_defn.add_modifier(self.scanner.grab())
         else:
-            self.scanner.error("expected '{' or 'is', but found " + token)
+            self.scanner.error("expected '{' or 'is', but found " +
+                               self.scanner.token)
 
 
 class ParamDeclParser(Parser):
-    # ParamDecl ::= ClassName PropName.
-
     def __init__(self, scanner, method_defn):
         Parser.__init__(self, scanner)
         self.method_defn = method_defn
     def parse(self):
         type_class_name = self.scanner.grab()
         prop_name = self.scanner.grab()
-        param_decl = self.method_defn.add_param_decl_by_name(prop_name, type_class_name)
+        self.method_defn.add_param_decl_by_name(prop_name, type_class_name)
 
 
 class AssignmentParser(Parser):
-    # Assignment ::= QualName "=" Expr ";".
-
     def __init__(self, scanner, method_defn):
         Parser.__init__(self, scanner)
         self.method_defn = method_defn
 
 
 class ContinueParser(Parser):
-    # Continue ::= "goto" PropName "." MethodName "(" [Expr {"," Expr}] ")" ";".
-
     def __init__(self, scanner, method_defn):
         Parser.__init__(self, scanner)
         self.method_defn = method_defn
         self.scanner.expect("(")
         expr_parser = ExprParser(self.scanner, continue_)
         if self.scanner.token != ")":
-            param_expr = expr_parser.parse()
+            expr_parser.parse()
             while self.scanner.token == ",":
                 self.scanner.expect(",")
-                param_expr = expr_parser.parse()
+                expr_parser.parse()
         self.scanner.expect(")")
         self.scanner.expect(";")
         continue_.typecheck()
 
 
 class ExprParser(Parser):
-    # Expr ::= ConstrExpr | QualName.
-
     def __init__(self, scanner, parent):
         Parser.__init__(self, scanner)
         self.parent = parent
 
 
 class ConstructionParser(Parser):
-    # ConstrExpr ::= "new" (ClassName) "(" [ClassName {"," ClassName}] ")".
-
     def __init__(self, scanner, parent):
         Parser.__init__(self, scanner)
         self.parent = parent
 
 
 class QualNameParser(Parser):
-    # QualName ::= PropName {"." PropName}.
-
     def __init__(self, scanner, parent):
         Parser.__init__(self, scanner)
         self.parent = parent

File src/unlikely/scanner.py

 $Id: scanner.py 318 2010-01-07 01:49:38Z cpressey $
 """
 
+
 class Scanner(object):
     """
     A lexical scanner.
             self._token = self._input[0]
             self._input = self._input[1:]
             self.toktype = "op"
-    
+
     def get_token(self):
         return self._token
-    
+
     token = property(get_token)
 
     def expect(self, str):

File src/unlikely/stdlib.py

 
 stdlib = ast.ClassBase()
 
-continuation = stdlib.add_class_defn_by_name("Continuation", None, ["saturated","abstract"])
-program = stdlib.add_class_defn_by_name("Program", "Continuation", ["abstract"])
+continuation = stdlib.add_class_defn_by_name("Continuation", None,
+                                             ["saturated", "abstract"])
+program = stdlib.add_class_defn_by_name("Program", "Continuation",
+                                        ["abstract"])
 
 chain = stdlib.add_class_defn_by_name("Chain", "Program", ["abstract"])
 
 passive = stdlib.add_class_defn_by_name("Passive", "Chain", ["abstract"])
 
-# Now that we have Passive we patch property and method into Continuation and all its subclasses so far
+# Now that we have Passive we patch property and method into Continuation
+# and all its subclasses so far
 continuation.add_dependant_by_name("Passive")
 program.add_dependant_by_name("Passive")
 chain.add_dependant_by_name("Passive")
 accumulator = continuation.add_prop_defn_by_name("accumulator", "Passive")
 continue_ = continuation.add_method_defn_by_name("continue")
 continue_.add_modifier("abstract")
-continue_.add_param_decl_by_name("accumulator","Passive")
+continue_.add_param_decl_by_name("accumulator", "Passive")
 
 # now we're done patching, we can continue defining further classes
 stop = stdlib.add_class_defn_by_name("Stop", "Program", ["final"])
 
-boolean_ = stdlib.add_class_defn_by_name("Boolean", "Passive", ["abstract","final"])
-true_ = stdlib.add_class_defn_by_name("True", "Boolean", ["final","forcible"])
-false_ = stdlib.add_class_defn_by_name("False", "Boolean", ["final","forcible"])
+boolean_ = stdlib.add_class_defn_by_name("Boolean", "Passive",
+                                         ["abstract", "final"])
+true_ = stdlib.add_class_defn_by_name("True", "Boolean",
+                                      ["final", "forcible"])
+false_ = stdlib.add_class_defn_by_name("False", "Boolean",
+                                       ["final", "forcible"])
 
-integer_ = stdlib.add_class_defn_by_name("Integer", "Passive", ["abstract","final"])
-# we have special logic elsewhere to recognize the countably infinite number of subclasses of Integer
-string_ = stdlib.add_class_defn_by_name("String", "Passive", ["abstract","final"])
-# we have special logic elsewhere to recognize the countably infinite number of subclasses of String
+integer_ = stdlib.add_class_defn_by_name("Integer", "Passive",
+                                         ["abstract", "final"])
+# we have special logic elsewhere to recognize the countably infinite
+# number of subclasses of Integer
+string_ = stdlib.add_class_defn_by_name("String", "Passive",
+                                        ["abstract", "final"])
+# we have special logic elsewhere to recognize the countably infinite
+# number of subclasses of String
 
-binary_operation = stdlib.add_class_defn_by_name("BinaryOperation", "Chain", ["abstract"])
+binary_operation = stdlib.add_class_defn_by_name("BinaryOperation", "Chain",
+                                                 ["abstract"])
 binary_operation.add_prop_defn_by_name("value", "Passive")
 
 add = stdlib.add_class_defn_by_name("Add", "BinaryOperation")
 input_ = stdlib.add_class_defn_by_name("Input", "Chain")
 
 branch = stdlib.add_class_defn_by_name("Branch", "Chain", ["abstract"])
-branch.add_prop_defn_by_name("else","Chain")
+branch.add_prop_defn_by_name("else", "Chain")
 
 if_ = stdlib.add_class_defn_by_name("If", "Branch")
 
 switch = stdlib.add_class_defn_by_name("Switch", "Branch", ["abstract"])
-switch.add_prop_defn_by_name("state","Passive")
+switch.add_prop_defn_by_name("state", "Passive")
 
 loop = stdlib.add_class_defn_by_name("Loop", "Switch", ["abstract"])
 
 while_loop = stdlib.add_class_defn_by_name("WhileLoop", "Loop")
-while_loop.add_prop_defn_by_name("test","Chain")
+while_loop.add_prop_defn_by_name("test", "Chain")
 
 for_loop = stdlib.add_class_defn_by_name("ForLoop", "Loop")
-for_loop.add_prop_defn_by_name("value","Integer")
-for_loop.add_prop_defn_by_name("delta","Integer")
-for_loop.add_prop_defn_by_name("finish","Integer")
+for_loop.add_prop_defn_by_name("value", "Integer")
+for_loop.add_prop_defn_by_name("delta", "Integer")
+for_loop.add_prop_defn_by_name("finish", "Integer")