def value_inheritance_spec(self, ast, pt): if pt.children[0].type == 'value_value_inheritance_spec': inh_node = Node(ast, 'inheritance', None, source = pt.children[0]) inh_node.add_attribute('relation', 'value') if pt.children[0].the('value_value_inheritance_spec') != None: inh_node.add_attribute('modifier', pt.children[0].the('value_value_inheritance_spec').leaf) name_list = [] target_list = [] for name in pt.the('value_value_inheritance_spec')['value_name']: newname = self.scoped_name(name.the('scoped_name')) name_list.append(newname) target_list.append(getTypenode(newname, ast)) inh_node.leaf = name_list inh_node.add_attribute('target_scope_list',target_list) return inh_node elif pt.children[0].type == 'value_interface_inheritance_spec': inh_node = Node(ast, 'inheritance', None, source = pt.children[0]) inh_node.add_attribute('relation', 'interface') name_list = [] target_list = [] for name in pt.the('value_interface_inheritance_spec')['interface_name']: newname = self.scoped_name(name.the('scoped_name')) name_list.append(newname) target_list.append(getTypenode(newname, ast)) inh_node.add_attribute('target_scope_list',target_list) inh_node.leaf = name_list return inh_node else: return UnknownNode(None, pt, source = pt)
def import_dcl(self, pt): ast = Node(None, None, source = pt) ast.type = "cimport" #pt.leaf """ if pt.leaf == 'import': #imported_scope child = pt.the('imported_scope').children[0] elif pt.leaf == 'cimport': child = pt.the('anglequoted_scope').children[0] """ child = pt.children[0].children[0] assert child is not None if child.type == 'string_literal': ast.leaf = child.leaf elif child.type == 'scoped_name': ast.leaf = self.scoped_name(child) elif child.type == 'anglequoted_string_literal': ast.leaf = child.leaf else: ast = Node(None, child, source = pt) ast.type = 'import' return ast
def component(self,ast, pt, decorators): assert not decorators component = Node(ast, 'component', None, source = pt) component.leaf = pt.the('identifier').leaf dcl = pt.the('component_dcl') if dcl != None: inh = dcl.the('component_inheritance_spec') if inh != None: inher_node = Node(component, 'inherits', leaf = self.scoped_name(interface, scope_name), source = dcl) component.add_child(inher_ndoe) inher_node.add_child(infogripper.find_scope(interface, inher)) sup = dcl.the('supported_interface_spec') if sup != None: supnode = Node(component, 'support', source = sup) name_list = [] for name in sup['scoped_name']: newname = self.scoped_name(name) name_list.append(newname) target_list.append(getTypenode(newname, component)) supnode.leaf = name_list supnode.add_attribute('target_scope_list', target_list) component.add_child(supnode) bod = dcl.the('component_body') if bod != None: exp_list = self.component_body(component, bod) if exp_list != []: component.add_children(exp_list) ast.add_child(component)
def create_instance_list(self, ast, typeref, declarators): instance_list = [] for child in declarators: instance = Node(ast, 'type_instance', None, source = child) if child.children[0].type == 'simple_declarator': instance.leaf = child.children[0].leaf instance.add_child(typeref) else: #complex_declarator newtype = TypeNode(ast, source = pt) newtype.add_attribute('meta_type','array') array_dcl = child.the('complex_declarator').the('array_declarator') array_dim = [] for dimension in array_dcl.children: exp_node = dimension.the('positive_int_const').the('const_exp') expr = self.getExpression(ast, exp_node) array_dim.append(expr.leaf) ''' if res != None: array_dcl.append(str(res)) else: array_dcl.append(exp_str) ''' newtype.add_attribute('shape',array_dim) newtype.add_child(typeref) instance.add_child(newtype) instance.leaf = array_dcl.leaf instance_list.append(instance) return instance_list
def op_dcl(self,ast, pt): fcn = Node(ast, 'function', None, source = pt) ast.add_child(fcn) for child in pt.children: if child.type == 'decorator': #print child dec_children = self.decorator_elements(fcn, child) dec_node = Node(fcn, 'decorator', dec_children, source = child) fcn.add_child(dec_node) elif child.type == 'op_attribute': fcn.add_attribute('attribute', child.leaf) elif child.type == 'op_type_spec': #if child.leaf == 'void': # target = getTypenode('void', fcn) #print 'trying to get typenode for: %s' %(child.the('param_type_spec')) #else: typenode = TypeNode(fcn, source = pt) typenode.name = 'return_type' fcn.add_child(typenode) target, new_type = self._get_target_type(typenode, child.the('param_type_spec'), defining_scope = typenode) # Add the target explicitly. typenode.add_child(target) if child.the('allow_indirection'): typenode.add_attribute('indirection', child.the('allow_indirection').leaf) elif child.the('ref_indirection'): typenode.add_attribute('indirection', ['*']) elif child.type == 'op_dcl': fcn.leaf = child.leaf elif child.type == 'parameter_dcls': fcn.add_children(self.parameter_dcls(fcn, child)) elif child.type == 'raises_expr': raises = Node(fcn, name='raises', source = child) leaflist = [] target_list = [] for node in child.the('scoped_name_list').children: if node.type == 'scoped_name': newname = self.scoped_name(node) leaflist.append(newname) target_list.append(getTypenode(newname, fcn)) raises.leaf = leaflist raises.add_attribute('target_scope_list', target_list) fcn.add_child(raises) elif child.type == 'context_expr': context = Node(fcn, name='context', source = child) leaflist = [] for node in child.the('string_literal_list').children: if node.type == 'string_literal': leaflist.append(node.leaf) context.leaf = leaflist fcn.add_child(context) else: fcn = UnknownNode(None, child, source = child) # Add it and include its symbol. self._dupes_check(fcn)
def attr_raises_expr(self, pt): raisesnode = Node(None, None, source = pt) for child in pt.children: if child.type == 'get_excep_expr': raisesnode.leaf = 'getraises' exception_list = self.exception_list(child.the('exception_list')) raisesnode.add_attribute('exception_list', exception_list) elif child.type == 'set_excep_expr': raisesnode.leaf = 'setraises' exception_list = self.exception_list(child.the('exception_list')) raisesnode.add_attribute('exception_list', exception_list) else: raisesnode = UnknownNode(None, child, source = pt) return raisesnode
def param_dcl(self, ast, pt): """ Param decls look like constant declarations: refer to "const_dcl". 1. parameter blah (3) +- meta_type = ['param'] +- direction = ['in'] +- indirection = ['*'] +- target = [<Node object type:unsigned int>] """ param_ast = Node(ast, 'parameter', None, source = pt) # Add direction param_ast.add_attribute('direction', pt.leaf) for child in pt.children: if child.type == 'param_type_spec': #param_ast.add_attribute('target',self.param_type_spec(ast, child)) target_type, is_new_type = self._get_target_type(param_ast, child, defining_scope = param_ast) param_ast.add_child(target_type) elif child.type == 'allow_indirection': indirection_list = [child.leaf] for indirection in child['allow_indirection']: indirection_list.append(indirection.leaf) param_ast.add_attribute('indirection', indirection_list) elif child.type == 'simple_declarator': param_ast.leaf = child.leaf else: param_ast.add_child(UnkownNode(None, child, source = pt)) # Check we're not passing raw void parameters. if not self.is_valid_parameter_spec(param_ast): raise InvalidTypeUsageError(target_type.the('type').leaf, child) return param_ast
def structure(self): s = None f = None try: ## for error handling pass s = Node(None, "structure") f = self.LT(1) self.match(ATOM) s.leaf = f.getText() self.match(LBRACKET) la1 = self.LA(1) if False: pass elif la1 and la1 in [ATOM,VARIABLE,NUMBER,LCURLY,LSQUARE]: pass t=self.termlist() s.children.extend(t) elif la1 and la1 in [RBRACKET]: pass else: raise antlr.NoViableAltException(self.LT(1), self.getFilename()) self.match(RBRACKET) except antlr.RecognitionException, ex: self.reportError(ex) self.consume() self.consumeUntil(_tokenSet_3)
def structure(self): s = None f = None try: ## for error handling pass s = Node(None, "structure") f = self.LT(1) self.match(ATOM) s.leaf = f.getText() self.match(LBRACKET) la1 = self.LA(1) if False: pass elif la1 and la1 in [ATOM, VARIABLE, NUMBER, LCURLY, LSQUARE]: pass t = self.termlist() s.children.extend(t) elif la1 and la1 in [RBRACKET]: pass else: raise antlr.NoViableAltException(self.LT(1), self.getFilename()) self.match(RBRACKET) except antlr.RecognitionException, ex: self.reportError(ex) self.consume() self.consumeUntil(_tokenSet_3)
def type_id_dcl(self, ast, pt, decorators): assert not decorators type_id = Node(ast, name='typeid', source = pt) type_id.leaf = self.scoped_name(ast, pt.the('scoped_name')) type_id.add_attribute('value', pt.the('string_literal').leaf) ast.add_child(type_id) self._dupes_check(type_id)
def except_dcl(self, ast, pt, decorators): excep_node = Node(ast, 'exception', None, source = pt) excep_node.leaf = pt.the('identifier').leaf if pt.the('opt_member_list') != None: excep_node.add_children(self.member_list(ast, pt.the('opt_member_list'))) ast.add(excep_node) self._dupes_check(excep_node)
def create_special_C(arch_info, ast): type_list = ast.children struct_t = TypeNode(ast, source_file = '<builtin>') struct_t.leaf = 'idl4_server_environment' struct_t.add_attribute('meta_type','struct') members = Node(struct_t, 'members') typeinst = Node(struct_t, 'type_instance') typeinst.leaf = '_action' index = [element.leaf for element in type_list].index('signed int') typeinst.add_attribute('target_type',type_list[index] ) members.add_child(typeinst) typeinst = Node(struct_t, 'type_instance') typeinst.leaf = '_data' index = [element.leaf for element in type_list].index('void') typeinst.add_attribute('target_type',type_list[index] ) members.add_child(typeinst) # Create IDL4 scope for giggles. idl4 = Node(ast, 'type', leaf = 'idl4', source_file = '<builtin>') idl4.add_attribute('meta_type', 'private') pagefault = Node(idl4, 'type', leaf = 'pagefault') idl4.add_child(pagefault) ast.add_child(idl4) #FIXME: CORBA-C Type-Hack!!! aliases = ( ('Object', 'signed int'), ('any', 'signed int'), ('ValueBase', 'signed int'), ('Word', 'signed int') ) # Create aliases to C nodes. for alias, name in aliases: newType = TypeNode(ast, source_file = '<builtin>') newType.leaf = alias newType.add_attribute('meta_type', 'alias') type_list = ast.children index = [element.leaf for element in type_list].index(name) target_node = Node(newType, 'target') target_node.add_child(type_list[index]) newType.add_child(target_node) ast.add_child(newType) # Explicitly add the IDL4 mapitem struct, yuck yuck mapitem = _make_struct(ast, 'idl4_mapitem', ('unsigned long int', 'base'), ('unsigned long int', 'fpage')) ast.add_child(mapitem)
def finder_dcl(self, ast, pt): fac_node = Node(ast, None, name = 'finder', source = pt) fac_node.leaf = pt.the('identifier').leaf fac_node.add_children(self.init_param_decls(fac_node, pt.the('init_param_decls'))) if pt.the('raises_expr') != None: raises = Node(fac_node, name='raises', source = pt) leaflist = [] target_list = [] for node in child.the('scoped_name_list').children: if node.type == 'scoped_name': newname = self.scoped_name(node) target_list = getTypenode(newname, ast) leaflist.append(newname) raises.leaf = leaflist raises.add_attribute('target_scope_list', target_list) fac_node.add_child(raises) return fac_node
def component_body(self, ast, pt): exp_list = [] for exp in pt.children: exp_node = Node(ast, 'export', source = exp) if exp.children[0].type == 'provides_dcl': exp_node.add_attribute('type', 'provides') exp_node.leaf = exp.children[0].the('identifier').leaf if exp.children[0].the('interface_type').the('scoped_name') != None: scopedname = self.scoped_name(exp.children[0].the('interface_type').the('scoped_name')) exp_node.add_attribute('name', scopedname) else: exp_node.add_attribute('name', 'Object') elif exp.children[0].type == 'uses_dcl': typestr = '' for child in exp.children[0]['uses_dcl']: typestr += child.leaf exp_node.add_attribute('type', typestr) exp_node.leaf = exp.children[0].the('identifier').leaf if exp.children[0].the('interface_type').the('scoped_name') != None: scopedname = self.scoped_name(exp.children[0].the('interface_type').the('scoped_name')) exp_node.add_attribute('name', scopedname) else: exp_node.add_attribute('name', 'Object') elif exp.children[0].type == 'emits_dcl': exp_node.add_attribute('type', 'emits') exp_node.leaf = exp.children[0].the('identifier').leaf scopedname = self.scoped_name(exp.children[0].the('scoped_name')) exp_node.add_attribute('name', scopedname) elif exp.children[0].type == 'publishes_dcl': exp_node.add_attribute('type', 'publishes') exp_node.leaf = exp.children[0].the('identifier').leaf scopedname = self.scoped_name(exp.children[0].the('scoped_name')) exp_node.add_attribute('name', scopedname) elif exp.children[0].type == 'consumes_dcl': exp_node.add_attribute('type', 'consumes') exp_node.leaf = exp.children[0].the('identifier').leaf scopedname = self.scoped_name(exp.children[0].the('scoped_name')) exp_node.add_attribute('name', scopedname) elif exp.children[0].type == 'attr_dcl': # FIXME: Not used? self.attr_dcl(exp_node, exp.children[0]) else: exp_list.append(UnknownChild(None, exp)) exp_list.append(exp_node) return exp_list
def create_special_C(arch_info, ast): type_list = ast.children struct_t = TypeNode(ast, source_file='<builtin>') struct_t.leaf = 'idl4_server_environment' struct_t.add_attribute('meta_type', 'struct') members = Node(struct_t, 'members') typeinst = Node(struct_t, 'type_instance') typeinst.leaf = '_action' index = [element.leaf for element in type_list].index('signed int') typeinst.add_attribute('target_type', type_list[index]) members.add_child(typeinst) typeinst = Node(struct_t, 'type_instance') typeinst.leaf = '_data' index = [element.leaf for element in type_list].index('void') typeinst.add_attribute('target_type', type_list[index]) members.add_child(typeinst) # Create IDL4 scope for giggles. idl4 = Node(ast, 'type', leaf='idl4', source_file='<builtin>') idl4.add_attribute('meta_type', 'private') pagefault = Node(idl4, 'type', leaf='pagefault') idl4.add_child(pagefault) ast.add_child(idl4) #FIXME: CORBA-C Type-Hack!!! aliases = (('Object', 'signed int'), ('any', 'signed int'), ('ValueBase', 'signed int'), ('Word', 'signed int')) # Create aliases to C nodes. for alias, name in aliases: newType = TypeNode(ast, source_file='<builtin>') newType.leaf = alias newType.add_attribute('meta_type', 'alias') type_list = ast.children index = [element.leaf for element in type_list].index(name) target_node = Node(newType, 'target') target_node.add_child(type_list[index]) newType.add_child(target_node) ast.add_child(newType) # Explicitly add the IDL4 mapitem struct, yuck yuck mapitem = _make_struct(ast, 'idl4_mapitem', ('unsigned long int', 'base'), ('unsigned long int', 'fpage')) ast.add_child(mapitem)
def decorator_element(self, ast, pt): dec_node = Node(None, 'annotation', source = pt) for child in pt.children: if child.type == 'identifier': dec_node.leaf = child.leaf elif child.type == 'expr_list': dec_node.add_children(self.expr_list(ast, child, True)) else: dec_node.add_child(UnknownNode(child, source = pt)) return dec_node
def attr_declarator(self, ast, pt): decl = Node(ast, 'declarator', None, source = pt) decl.leaf = [] for child in pt.children: if child.type == 'simple_declarator': decl.leaf.append(child.leaf) elif child.type == 'attr_raises_expr': decl.add_child(self.attr_raises_expr(decl, child)) else: decl.add_child(UnkownNode(None, child, source = pt)) return decl
def init_param_decls(self, ast, pt): decl_list = [] for child in pt['init_param_decl']: param_node = Node(ast, 'parameter', None, source = child) type_ast, new_type = self._get_target_type(param_node, child.the('param_type_spec'), defining_scope = param_node) param_node.leaf = child.the('simple_declarator').leaf if child.the('init_param_attribute') != None: param_node.add_attribute('attribute', child.the('init_param_attribute').leaf) decl_list.append(param_node) return decl_list
def value_dcl(self, ast, pt): node = Node(ast, 'valuetype', None, source = pt) for child in pt.children: if child.type == 'identifier': node.leaf = child.leaf elif child.type == 'value_inheritance_spec': val_inh_node = self.value_inheritance_spec(node, self, child) node.add_child(val_inh_node) else: node.add_child(UnknownNode(child, source = pt)) return node
def home_dcl(self, ast, pt, decorators): assert not decorators homenode = Node(ast, 'home', source = pt) # # HEADER # homeheader = pt.the('home_header') homenode.leaf = homeheader.the('identifier').leaf if homeheader.the('home_inheritance_spec') != None: inhnode = Node(homenode, 'inheritance', source = homeheader) inhnode.leaf = self.scoped_name(homeheader.the('home_inheritance_spec').the('scoped_name')) inhnode.add_attribute homenode.add_child(inhnode) if homeheader.the('supported_interface_spec') != None: supnode = Node(homenode, 'support', source = homeheader) namelist = [] for name in homeheader.the('supported_interface_spec')['scoped_name']: namelist.append(self.scoped_name(name)) supnode.leaf = namelist homenode.add_child(supnode) mannode = Node(homenode, None, name='manages', source = pt) mannode.leaf = self.scoped_name(homheader.the('scoped_name')) mannode.add_attribute('target_scope', infogripper.find_scope(mannode.leaf)) if homeheader.the('primary_key_spec') != None: mannode.add_attribute('primarykey', infogripper.find_scope(self.scoped_name(homenode, homeheader.the('primary_key_spec').the('scoped_name')))) homenode.add_child(mannode) # # BODY # homebody = pt.the('home_body') for child in homebody.children: if child.children[0].type == 'export': self.export(homenode, child.children[0]) elif child.children[0].type == 'factory_dcl': homenbode.add_child(self.factory_dcl(homenode, child.children[0])) elif child.children[0].type == 'finder_dcl': homenode.add_child(self.finder_dcl(homenode, child.children[0])) else: homenode.add_child(UnknownNode(None, child.children[0]), source = pt) ast.add_child(homenode)
def event_def(self, ast, pt, decorators): assert not decorators event_node = Node(ast, 'event', source = pt) child = pt.children[0] if child.type == 'event_abs': event_node.add_attribute('modifier', 'abstract') event_node.leaf = child.the('event_abs_dcl').the('event_header').the('identifier').leaf if child.the('event_abs_dcl') != None: event_node.add_child(self.value_inheritance_spec(child.the('event_abs_dcl').the('value_inheritance_spec'))) export_node = Node(event_node, 'export', exp_list, source = child) for exp_child in child.the('event_abs_dcl')[export]: self.export(export_node, exp_child) if export_node.children: event_node.add_child(export_node) elif child.type == 'event_custom': event_node.add_attribute('modifier', 'custom') event_node.leaf = child.the('event_custom').the('event_header').the('identifier').leaf if child.the('event_elem_dcl') != None: event_node.add_child(self.value_inheritance_spec(child.the('event_elem_dcl').the('value_inheritance_spec'))) export_node = Node(event_node, 'export', exp_list, source = child) for exp_child in child.the('event_elem_dcl')[export]: self.export(export_node, exp_child) if export_node.children: event_node.add_child(export_node) elif child.type == 'event_dcl': event_node.leaf = child.the('event_dcl').the('event_header').the('identifier').leaf if child.the('event_elem_dcl') != None: event_node.add_child(self.value_inheritance_spec(child.the('event_elem_dcl').the('value_inheritance_spec'))) export_node = Node(event_node, 'export', exp_list, source = child) for exp_child in child.the('event_elem_dcl')[export]: self.export(export_node, exp_child) if export_node.children: event_node.add_child(export_node) else: event_node = UnknownNode(None, child, source = pt) ast.add_child(event_node)
def state_member(self, ast, pt): type_node = TypeNode(ast, None, source = pt) if pt.the('state_member') != None: type_node.leaf = child.leaf type_node.add_child(self.type_spec(ast, pt.the('type_spec'))) decls = pt.the('declarators') decl_list = [] for child in decls.children: decl_list.append(self.getDeclarator(child)) dec_node = Node(type_node, 'declarator', None, source = pt) dec_node.leaf = decl_list type_node.add_child(dec_node) return type_node
def interf(self,ast, pt, decorators): # ... create the type interface = Node(ast, 'type', source = pt) interface.add_attribute('meta_type', 'interface') # ... add modifiers if pt.leaf == 'abstract' or pt.leaf == 'local': interface.add_attribute('modifier', pt.leaf) # ... set interface name interface.leaf = pt.the('identifier').leaf # If the interface already exists in incomplete form, use that instead. old_interfaces = infogripper.getAllNodes(interface.leaf, ast, 'type') if old_interfaces: # Found a previous declaration; use that. assert len(old_interfaces) == 1 interface = old_interfaces[0] else: # Okay, it doesn't exist already: add it. ast.add_child(interface) # While we're building it, add the "incomplete" attribute. interface.add_attribute('incomplete', True) is_incomplete = True for child in pt.children: if child.type == 'interface_dcl': is_incomplete = False #print child if child.the('interface_header') != None: scope_list = child.the('interface_header').the('interface_inheritance_spec').the('scoped_name_list') for scope_name in scope_list['scoped_name']: inher_node = Node(interface, 'inherits', leaf = self.scoped_name(scope_name), source = child) inher_node.add_child(infogripper.find_scope(interface, inher_node.leaf)) # Ensure we're not inheriting from the same class twice. self.check_not_inherited(interface, inher_node.children[0], error_infonode = scope_name) interface.add_child(inher_node) if child.the('interface_body') != None: for export_child in child.the('interface_body').children: self.export(interface, export_child) # Remove "incomplete" if the interface was fully-defined above. if not is_incomplete: interface.del_attributes('incomplete') interface.add_children(decorators) self._dupes_check(interface)
def atom(self): t = None a = None try: ## for error handling pass a = self.LT(1) self.match(ATOM) t = Node(None, "atom", leaf=a.getText()) if t.leaf.startswith("'"): t.leaf = t.leaf[1:-1] except antlr.RecognitionException, ex: self.reportError(ex) self.consume() self.consumeUntil(_tokenSet_3)
def atom(self): t = None a = None try: ## for error handling pass a = self.LT(1) self.match(ATOM) t = Node(None, "atom", leaf = a.getText()) if t.leaf.startswith("'"): t.leaf = t.leaf[1:-1] except antlr.RecognitionException, ex: self.reportError(ex) self.consume() self.consumeUntil(_tokenSet_3)
def value_abs_dcl(self, at, pt): node = Node(ast, 'valuetype', None, source = pt) node.add_attribute('modifier', 'abstract') for child in pt.children: if child.type == 'identifier': node.leaf = child.leaf elif child.type == 'value_abs_full_dcl': val_inh_node = self.value_inheritance_spec(node, self, child.the('value_inheritance_spec')) export_node = Node(val_inh_node, 'export', source = child) for exp_child in child[export]: self.export(export_node, exp_child) if export_node.children: val_inh_node.add_child(export_node) else: node.add_child(UnknownNode(None, child, source = pt)) return node
def const_dcl(self, ast, pt, decorators): """ Create a constant declaration. They look like this: 1. type_instance blah (3) +- meta_type = ['const'] 2. +- value = [1] 3. info (4) +- target = [<Node object type:unsigned int>] """ assert not decorators # 1: Set up the base. const_node = Node(ast, 'type_instance', source = pt) const_node.add_attribute('meta_type', 'const') const_node.leaf = pt.the('identifier').leaf # 2: Now store the target of the decl. target_type, is_new_type = self._get_target_type(const_node, pt.the('const_type'), defining_scope = const_node) # ... NB if we have a new type it will be added here already as a "type" node. # But we always add as a "target" node too. const_node.add_child(target_type) # 3: Add the expression result wrapped in a declarator (for c++ compat.) declarator_wrapper = Node(const_node, 'declarator', source = pt) const_node.add_child(declarator_wrapper) expr = self.getExpression(const_node, pt.the('const_exp')) declarator_wrapper.add_child(expr) # 4: Ensure the type of the expression can be coerced to the type of the target. if not infogripper.can_coerce_expr(expr, target_type.the('type')): raise CannotReconcileTypesError(expr.the('type').leaf, target_type.the('type').leaf, const_node) # 5: Hurrah! ast.add_child(const_node) self._dupes_check(const_node)
def module(self, ast, pt, decorators): assert not decorators module_name = pt.the('identifier').leaf mod = infogripper.getNode(module_name, ast, 'module') mod_is_new = False if mod is None: mod = Node(ast, 'module', None, source = pt) ast.add_child(mod) mod_is_new = True for child in pt.children: if child.type == 'identifier': mod.leaf = child.leaf # Modules can be re-opened, hence the check: if mod_is_new: self._dupes_check(mod) elif child.type == 'definition_list': for def_child in child.children: self.decorated_def(mod, def_child) #def_child = self.definition(def_child) #mod.add_child(def_child) else: mod.add_child(UnknownNode(None,child, source = pt))
def value_def(self, ast, pt, decorators): assert not decorators if pt.children[0].type == 'value_dcl': ast.add_child(self.value_dcl(ast, pt.children[0])) elif pt.children[0].type == 'value_abs_dcl': ast.add_child(self.value_abs_dcl(ast, pt.children[0])) elif pt.children[0].type == 'value_box_dcl': val_node = Node(ast, 'valuetype', None, source = pt.children[0]) val_node.leaf = pt.children[0].the('identifier').leaf val_node.add_child(self.type_spec(val_node, pt.children[0].the('type_spec'))) ast.add_child(val_node) elif pt.children[0].type == 'value_custom_dcl': val_node = self.value_dcl(ast, pt.children[0].the('value_dcl')) val_node.add_attribute('modifier', 'custom') ast.add_child(val_node) elif pt.children[0].type == 'value_forward_dcl': val_node = Node(ast, 'valuetype', None, source = pt.children[0]) val_node.leaf = val_node.leaf = pt.children[0].the('identifier').leaf val_node.add_attribute('modifier', 'forward') ast.add_child(val_node) else: ast.add_child(UnknownNode(None, pt.children[0].type, source = pt.children[0]))
def pop_and_add(scope_ast, oper, operands): # Pop and create new tree node = Node(None, 'expression') node.leaf = OPER_NAMES[oper] for count in range(get_arity(oper)): node.add_front(operands.pop()) # Work out the type for the resulting expression. type_ast = get_expr_type(node.children[0]) for new_type_expr in node.children[1:]: new_type_ast = get_expr_type(new_type_expr) if infogripper.can_coerce_expr(new_type_expr, type_ast): type_ast = new_type_ast else: raise error.CannotReconcileTypesError(new_type_ast.leaf, type_ast.leaf, scope_ast) # Store a value, if we can. This over-rides the above discovered type. # FIXME: This is a cheesy hack. if type_ast.has_attribute('smallest') and type_ast.has_attribute('largest'): # It's a scalar type, we can play with it. if len(node.children) > 1: result = oper.join(['(%s)' % (child.attribute('value')) for child in node.children]) else: # FIXME: Even cheesier result = "%s%s" % (PYTHON_UNARY[oper] , node.children[0].attribute('value')) try: result = eval(result) node.add_attribute('value', result) except: pass smallest_type_name = smallest_type_hack(result) if smallest_type_name: type_ast = infogripper.getNode(smallest_type_name, scope_ast, 'type') node.add_child(type_ast) operands.append(node)