def __parse_method(cls, line, inheritance_type): method = Method() if "virtual" in line: line = line.replace("virtual", "") method.is_virtual = True if "=0" in line: line = line.replace("=0", "") if method.is_virtual: method.is_pure_virtual = True else: raise BaseClsException( "Only virtual methods can be specified as pure virtual") if "static" in line: line = line.replace("static", "") method.is_static = True type, name, params = re.findall(r"(.*) (.*)\((.*)\).*", line, re.DOTALL)[0] method.type = type.strip() method.name = name.strip() param_list = [] if params != "void" and params: for param in params.split(","): if param: ret_type, name = param.rsplit(" ", 1) param_list.append((ret_type, name)) method.params = param_list cls.add_method(method, inheritance_type)
def get_attribute(self, name): for key, value in self.attributes.items(): for y in value: if y.name == name: return y, key raise BaseClsException("Attribute " + name + " was not found in class " + self.name)
def __solve_using_statement(self, attribute): cls = self.get_parent_class(attribute.inheritance_class_id) if not cls: raise BaseClsException("Given parent class " + attribute.inheritance_class_id + " was not found") parent_attr, access_modifier = cls.get_attribute(attribute.name) # remove the old reference from list, default location is private self.attributes[InheritanceType.private] = list( filter(lambda x: attribute.name != x.name, self.attributes[InheritanceType.private])) # now copy all useful info into subclass parameter attribute.type = parent_attr.type attribute.is_static = parent_attr.is_static attribute.inherit_from = parent_attr.inherit_from # now remove all other mambers with the same name for x in InheritanceType.getTypes(): self.attributes[x] = list( filter(lambda x: attribute.name != x.name, self.attributes[x])) self.methods[x] = list( filter(lambda x: attribute.name != x.name, self.methods[x])) self.attributes[access_modifier].append(attribute)
def actualize(self, other): if not self.is_defined: self.attributes = other.get_attributes() self.methods = other.get_methods() self.is_defined = True else: raise BaseClsException("Redefininiton of class " + self.name)
def getStringForType(type): if type == InheritanceType.public: return "public" elif type == InheritanceType.protected: return "protected" elif type == InheritanceType.private: return "private" else: raise BaseClsException("Unknown member visibility type")
def check_conflicts(self): for x in InheritanceType.getTypes(): for y in self.attributes[x] + self.methods[x]: others = self.get_members_with_name(y.name) if len(others) != 1: methods = list( filter(lambda x: isinstance(x, Method), others)) for method in methods: if not method.is_virtual: raise BaseClsException( "Inheritance conflict with member " + y.name)
def send_members_to_children(self): if self.children: public_methods = deepcopy(self.methods[InheritanceType.public]) protected_methods = deepcopy( self.methods[InheritanceType.protected]) private_pure_virtual_methods = deepcopy([ x for x in self.methods[InheritanceType.private] if x.is_pure_virtual ]) public_attrs = deepcopy(self.attributes[InheritanceType.public]) protected_attrs = deepcopy( self.attributes[InheritanceType.protected]) for x in public_methods + protected_methods + private_pure_virtual_methods + public_attrs + protected_attrs: if not x.inherit_from: x.inherit_from = self.name for child in self.children: if child[0] == InheritanceType.private: child[1].attributes[ InheritanceType. private] += public_attrs + protected_attrs child[1].methods[ InheritanceType. private] += public_methods + protected_methods elif child[0] == InheritanceType.protected: child[1].attributes[ InheritanceType. protected] += public_attrs + protected_attrs child[1].methods[ InheritanceType. protected] += public_methods + protected_methods elif child[0] == InheritanceType.public: child[1].attributes[InheritanceType.public] += public_attrs child[1].attributes[ InheritanceType.protected] += protected_attrs child[1].methods[InheritanceType.public] += public_methods child[1].methods[ InheritanceType.protected] += protected_methods else: raise BaseClsException("Unknown inheritance type") # now add the pure virtual methods child[1].methods[ InheritanceType.private] += private_pure_virtual_methods if self.children: for child in [x[1] for x in self.children]: child.send_members_to_children() #make class abstract if it contains anything pure virtual self.check_abstract_x_concrete()
def getTypeFromString(string): #empty == no modifier = public if not string: return "public" if string == "public": return InheritanceType.public elif string == "protected": return InheritanceType.protected elif string == "private": return InheritanceType.private else: raise BaseClsException("Unknown access modifier: " +string)
def find_class_by_name(classes, name): for cls in classes: if cls.name == name: return cls raise BaseClsException("Parent class of " + name + " not found")