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 get_call(scope_ast, pt, ast_gen): """ Translate function calls. PT looks like this: expression call expression identifier (value) = <function name> expression comma (optional) expression param1 expression param2 ... We turn them into this AST: expression call expression scoped_name (value) = <function name> expression parameters (optional) expression param1 expression param2 ... """ # Translate function calls. node = Node(scope_ast, "expression", leaf="call", source=pt) expression_identifier = pt.children[0] assert expression_identifier.leaf == "id_expression" node.add_child(get_scoped_name(node, expression_identifier, ast_gen)) # Add parameters. if len(pt.children) == 2: node.add_child(_get_call_params(node, pt.children[1], ast_gen)) return node
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 enum_type(self, ast, pt, defining_scope): type_node = TypeNode(ast, source = pt) type_node.add_attribute('meta_type', 'enum') enum_list = [] for child in pt.children: if child.type == 'identifier': type_node.leaf = child.leaf elif child.type == 'enumerator_list': for enum in child['enumerator']: enum_list.append(enum.leaf) type_node.add_attribute('enumeration', enum_list) # All the enum internal names get added to the outer scope (Do they?!) if defining_scope: # Add the enum itself, too. defining_scope.add_child(type_node) for count, child_pt in enumerate(pt.the('enumerator_list')['enumerator']): enum_item_ast = TypeNode(defining_scope, leaf = child_pt.leaf, source = child) defining_scope.add_child(enum_item_ast) enum_item_ast.add_attribute('meta_type', 'enum_member') enum_item_ast.add_attribute('value', count) self._dupes_check(enum_item_ast) enum_item_target = Node(enum_item_ast, 'target') enum_item_target.add_child(type_node) enum_item_ast.add_child(enum_item_target) return type_node
def make_scoped_name_ast(scope_ast, pt): assert pt.name == 'scoped_name' ast = Node(None, "expression", leaf = 'scoped_name') # Sanity check for child in pt.children: assert child.name in ('scope_operator', 'identifier') # Build the name. scoped_name_list = [child.leaf for child in pt.children] value = ''.join(scoped_name_list) # Check that it's valid target_asts = infogripper.getAllNodes(value, scope_ast, None) if len(target_asts) > 1: raise error.AmbiguousScopedNameError(value, scope_ast, target_asts) elif len(target_asts) == 0: raise error.NameNotFoundError(value, pt) target_wrapper = Node(scope_ast, 'target', [target_asts[0]]) ast.add_child(target_wrapper) ast.add_attribute('value', value) return ast
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 case_stmt_list(self, ast, pt): """ switch case expression [expression...] declarator [case...] """ # Find the appropriate scope for resolving scoped names, if any are present in # the case statement list. Switches on enums are constrained to the scope of # the enum (I think). switch_ast = Node(ast, 'switch', source = pt) for case_pt in pt.children: case_ast = Node(switch_ast, 'case', source = case_pt) switch_ast.add_child(case_ast) for choice_pt in case_pt['const_exp']: expr_ast = self.getExpression(case_ast, choice_pt) case_ast.add_child(expr_ast) typeref, new_type = self.type_spec(case_ast, case_pt.the('element_spec').the('type_spec')) if new_type: case_ast.add_child(typeref) decl = case_pt.the('element_spec').the('declarator') inst_list = self.create_instance_list(case_ast, typeref, [decl]) case_ast.add_children(inst_list) return switch_ast
def addASTChild(self, currentAST, child): if not child: return rootnode = Node(None, sys._getframe(1).f_code.co_name, source_line=self.LT(1).getLine(), source_file=self.getFilename()) if child.node: if not currentAST.root: rootnode.children = [child.node] else: rootnode = child.node # Node(sys._getframe(1).f_code.co_name, children=[child.node]) child.node = rootnode child.node.leaf = child.getText() if child.node is None: print child if not currentAST.root: currentAST.root = child elif not currentAST.child: currentAST.root.setFirstChild(child) else: currentAST.root.node.add_child(child.node) currentAST.child.setNextSibling(child) currentAST.child = child currentAST.advanceChildToEnd()
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 addASTChild(self,currentAST, child): if not child: return rootnode = Node(None,sys._getframe(1).f_code.co_name, source_line = self.LT(1).getLine(), source_file = self.getFilename()) if child.node: if not currentAST.root: rootnode.children = [child.node] else: rootnode = child.node # Node(sys._getframe(1).f_code.co_name, children=[child.node]) child.node = rootnode child.node.leaf = child.getText() if child.node is None: print child if not currentAST.root: currentAST.root = child elif not currentAST.child: currentAST.root.setFirstChild(child) else: currentAST.root.node.add_child(child.node) currentAST.child.setNextSibling(child) currentAST.child = child currentAST.advanceChildToEnd()
def create_special_MIG(arch_info, ast): poly_list =( ("MACH_MSG_TYPE_PORT_RECEIVE", '32', 'MACH_MSG_TYPE_PORT_internal', 'MACH_MSG_TYPE_POLYMORPHIC'), ("MACH_MSG_TYPE_PORT_SEND", '32', 'MACH_MSG_TYPE_PORT_internal', 'MACH_MSG_TYPE_POLYMORPHIC'), ("MACH_MSG_TYPE_PORT_SEND_ONCE", '32', 'MACH_MSG_TYPE_SEND_internal', 'MACH_MSG_TYPE_POLYMORPHIC'), ("MACH_MSG_TYPE_COPY_SEND", '32', 'MACH_MSG_TYPE_SEND_internal', 'MACH_MSG_TYPE_PORT_SEND'), ("MACH_MSG_TYPE_MAKE_SEND", '32', 'MACH_MSG_TYPE_SEND_internal', 'MACH_MSG_TYPE_PORT_SEND'), ("MACH_MSG_TYPE_MOVE_SEND", '32', 'MACH_MSG_TYPE_SEND_internal', 'MACH_MSG_TYPE_PORT_SEND'), ("MACH_MSG_TYPE_MAKE_SEND_ONCE", '32', 'MACH_MSG_TYPE_SEND_internal', 'MACH_MSG_TYPE_PORT_SEND_ONCE'), ("MACH_MSG_TYPE_MOVE_SEND_ONCE", '32', 'MACH_MSG_TYPE_SEND_internal', 'MACH_MSG_TYPE_PORT_SEND_ONCE'), ("MACH_MSG_TYPE_MOVE_RECEIVE", '32', 'MACH_MSG_TYPE_RECEIVE_internal', 'MACH_MSG_TYPE_PORT_RECEIVE') ) type_list = ast.children for name, size, sender, receiver in poly_list: newType = TypeNode(ast, None, source_file = '<builtin>') newType.leaf = name newType.add_attribute('meta_type', 'polymorphic') info = Node(newType, 'info') newType.add_child(info) index = [element.leaf for element in type_list].index(sender) info.add_attribute('sender_type',type_list[index] ) index = [element.leaf for element in type_list].index(receiver) info.add_attribute('receiver_type',type_list[index] ) type_list.append(newType) ast.add_child(newType)
def _rename_params(self, ast): if self.get_is_pagefault(ast): new_ast = ast.copy() new_ast.children = [] for child in ast.children: if child.name != 'parameter': new_ast.children.append(child.copy()) elif child.the('target').the('type').leaf != 'fpage': new_ast.children.append(child.copy()) else: # Create a new magical node with the correct name... fp_param = Node(new_ast, 'parameter', leaf=child.leaf) fp_param.add_attribute( 'direction', child.get_single_attribute('direction')) target = Node(fp_param, 'target') target.add_child( infogripper.getTypenode('idl4_mapitem', ast)) fp_param.add_child(target) new_ast.children.append(fp_param) return new_ast else: return 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 type_from_declarator(self, parent_ast, declarator): """ Construct a partial type. Only used for typdefs currently. """ typenode = TypeNode(parent_ast, None, source = declarator) # Don't know the meta type here if declarator.my_children_are('simple_declarator'): typenode.leaf = declarator.the('simple_declarator').leaf #typenode.add_attribute('target_type', typeref) else: #complex_declarator newtype = TypeNode(parent_ast, source = declarator) newtype.add_attribute('meta_type','array') array_dcl = declarator.the('complex_declarator').the('array_declarator') array_dim = [] shape = Node(newtype, 'shape', source = declarator) newtype.add_child(shape) for dimension in array_dcl.children: exp_node = dimension.the('positive_int_const').the('const_exp') expr = self.getExpression(shape, exp_node) if expr.attribute('value') < 0: raise DimensionOutOfRangeError(dimension) shape.add_child(expr) typenode.leaf = array_dcl.leaf typenode.add_child(newtype) assert typenode.leaf != None return typenode
def create(self, token): if isinstance(token, int): return Node(None, token) elif isinstance(token, Node): # it's one of ours... return token else: node_name = token.getText() return Node(None, node_name)
def list_contents(self): l = None def add_listterm(t, parent): child = Node(None, "structure", leaf='.', children=[t]) parent.add_child(child) return child try: ## for error handling pass t = self.term() l = Node(None, "structure", leaf='.', children=[t]) subnode = l while True: if (self.LA(1) == COMMA): pass self.match(COMMA) t = self.term() subnode = add_listterm(t, subnode) else: break la1 = self.LA(1) if False: pass elif la1 and la1 in [BAR]: pass self.match(BAR) la1 = self.LA(1) if False: pass elif la1 and la1 in [VARIABLE]: pass t2 = self.variable() elif la1 and la1 in [LSQUARE]: pass t2 = self.prologlist() else: raise antlr.NoViableAltException(self.LT(1), self.getFilename()) subnode.add_child(t2) elif la1 and la1 in [RSQUARE]: pass else: raise antlr.NoViableAltException(self.LT(1), self.getFilename()) if len(subnode.children) == 1: subnode.add_child(Node(None, "structure", leaf='.')) except antlr.RecognitionException, ex: self.reportError(ex) self.consume() self.consumeUntil(_tokenSet_10)
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 get_conditional_expression(scope_ast, pt, ast_gen): """ Conditional expressions (ternary expression) """ node = Node(scope_ast, "expression", leaf="ternary_if", source=pt) cond_pt, true_pt, false_pt = pt.children node.add_child(get_expression(node, cond_pt, ast_gen)) node.add_child(get_expression(node, true_pt, ast_gen)) node.add_child(get_expression(node, false_pt, ast_gen)) return node
def string_type(self, ast, pt, wstring = False): typenode = Node(ast, 'type', source = pt) if wstring: target = Node(typenode, 'customised', [infogripper.getTypenode('wstring', ast)]) else: target = Node(typenode, 'customised', [infogripper.getTypenode('string', ast)]) typenode.add_child(target) pos_const_node = pt.the('positive_int_const') if pos_const_node != None: expr = self.getExpression(ast, pos_const_node.the('const_exp')) typenode.add_attribute('max_length', expr.leaf) # This is a new, anonymous type. return typenode
def dictionary(self): d = None try: ## for error handling pass self.match(LCURLY) self.match(RCURLY) d = Node(None, "dictionary"); d._internal_data = {} except antlr.RecognitionException, ex: self.reportError(ex) self.consume() self.consumeUntil(_tokenSet_1)
def dictionary(self): d = None try: ## for error handling pass self.match(LCURLY) self.match(RCURLY) d = Node(None, "dictionary") d._internal_data = {} except antlr.RecognitionException, ex: self.reportError(ex) self.consume() self.consumeUntil(_tokenSet_1)
def get_scoped_name(scope_ast, pt, ast_gen): the_name = pt.get_single_attribute("value") scoped_name = Node(scope_ast, "expression", leaf="scoped_name", source=pt) scoped_name.add_attribute("value", the_name) # Try to find a target. type_ast = getNode(the_name, scope_ast) # It's not the end of the world if we don't find a target. if type_ast: target_ast = Node(scoped_name, "target", [type_ast]) scoped_name.add_child(target_ast) return scoped_name
def get_expression(scope_ast, pt, ast_gen): if pt.name == "expression" and pt.leaf == "" and len(pt.children) == 1: pt = pt.children[0] if pt.name == "expression" and pt.leaf in OK_ALREADY: node = Node(scope_ast, "expression", leaf=pt.leaf, source=pt) for child_pt in pt.children: node.add_child(get_expression(node, child_pt, ast_gen)) return node elif pt.name == "expression" and pt.leaf in HANDLERS: return HANDLERS[pt.leaf](scope_ast, pt, ast_gen) else: pt.print_tree() raise Error("Couldn't translate PT")
def get_expression(scope_ast, pt, ast_gen): if pt.name == 'expression' and pt.leaf == '' and len(pt.children) == 1: pt = pt.children[0] if pt.name == 'expression' and pt.leaf in OK_ALREADY: node = Node(scope_ast, 'expression', leaf=pt.leaf, source=pt) for child_pt in pt.children: node.add_child(get_expression(node, child_pt, ast_gen)) return node elif pt.name == 'expression' and pt.leaf in HANDLERS: return HANDLERS[pt.leaf](scope_ast, pt, ast_gen) else: pt.print_tree() raise Error("Couldn't translate PT")
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 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 get_cast(scope_ast, pt, ast_gen): """ Casts """ node = Node(scope_ast, "expression", leaf="cast", source=pt) type_ast = ast_gen.get_type(scope_ast, pt) assert type_ast is not None # Indirection stays the same indirection_ast = Node(scope_ast, "indirection", leaf=pt.leaf) # Expression is converted. expr_ast = get_expression(node, pt.get_child("expression"), ast_gen) # And we're done node.add_children([type_ast, indirection_ast, expr_ast]) return node
def get_cast(scope_ast, pt, ast_gen): """ Casts """ node = Node(scope_ast, 'expression', leaf='cast', source=pt) type_ast = ast_gen.get_type(scope_ast, pt) assert type_ast is not None # Indirection stays the same indirection_ast = Node(scope_ast, 'indirection', leaf=pt.leaf) # Expression is converted. expr_ast = get_expression(node, pt.get_child('expression'), ast_gen) # And we're done node.add_children([type_ast, indirection_ast, expr_ast]) return node
def attr_dcl(self, ast, pt): attr_node = Node(ast, 'attribute', None, source = pt) ast.add_child(attr_node) type_node, new_type = self._get_target_type(attr_node, pt.children[0].the('param_type_spec'), defining_scope = attr_node) if pt.children[0].type == 'readonly_attr_spec': attr_node.add_attribute('mode', 'readonly') attr_node.add_child(self.attr_declarator(attr_node, pt.children[0].the('readonly_attr_declarator'))) #self.attr_declarator(pt.children[0].the('readonly_attr_declarator'), type_node) elif pt.children[0].type == 'attr_spec': attr_node.add_attribute('mode', 'readwrite') attr_node.add_child(self.attr_declarator(attr_node, pt.children[0].the('attr_declarator'))) #attr_node.add_child(self.attr_declarator(pt.children[0].the('attr_declarator')), type_node) else: attr_node = UnknownNode(None, pt.children[0], source = pt.children[0])
def not_expr(self): e = None try: ## for error handling pass hitnot = False la1 = self.LA(1) if False: pass elif la1 and la1 in [NOT]: pass self.match(NOT) hitnot = True elif la1 and la1 in [ LBRACKET, ATOM, VARIABLE, NUMBER, LCURLY, LSQUARE ]: pass else: raise antlr.NoViableAltException(self.LT(1), self.getFilename()) e = self.relation_expr() if hitnot: e = Node(None, "expression", leaf="not", children=[e]) except antlr.RecognitionException, ex: self.reportError(ex) self.consume() self.consumeUntil(_tokenSet_6)
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 _get_call_params(scope_ast, pt, ast_gen): params_ast = Node(scope_ast, "expression", leaf="parameters", source=pt) if pt.leaf == "comma": for param_pt in pt.get_children_named("expression"): param_expr_ast = get_expression(params_ast, param_pt, ast_gen) if param_expr_ast is None: param_pt.print_tree() params_ast.add_child(param_expr_ast) elif pt.name == "expression": params_ast.add_child(get_expression(params_ast, pt, ast_gen)) else: # hm pt.print_tree() raise Error("Unable to decode parameter list") return params_ast
def addASTChild(self, currentAST, child): if child is None: # don't bother return if currentAST.root is None: root_name = sys._getframe(1).f_code.co_name currentAST.root = Node(None, root_name) currentAST.root.add_child(child)
def make_literal_ast(scope_ast, pt): assert pt.name == 'literal' child_pt = pt.children[0] ast = Node(None, "expression") ast_type_name = TYPENAME_MAP.get(child_pt.name) if ast_type_name is None: raise Error("Unknown literal type %s" % child_pt.name) target_type = infogripper.getNode(ast_type_name, scope_ast, 'type') if target_type is None: raise Error("Couldn't find type %s in AST" % (ast_type_name)) if target_type.has_attribute('smallest') and target_type.has_attribute('largest'): # FIXME: Shouldn't be doing this. ast_type_name = smallest_type_hack(get_python_value(child_pt.leaf)) target_type = infogripper.getNode(ast_type_name, scope_ast, 'type') # FIXME: Use of eval is evil ast.add_attribute('value', get_python_value(child_pt.leaf)) # Remember the original name for later. ast.add_attribute('display_name', child_pt.name) ast.add_child(target_type) return ast
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 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 create_basictype_ast(arch_name, typetype): ast = Node(None, 'MagpieAST', None) arch_info = construct_for_arch(arch_name, typetype) if typetype == 'idl4': names = CORBA_NAMES + C_NAMES aliases = C_ALIAS if typetype == 'mig': names = CORBA_NAMES + MIG_NAMES aliases = MIG_ALIAS for name in names: newType = TypeNode(ast, None, source_file = '<builtin>') newType.leaf = name newType.add_attribute('meta_type', 'basic') newType.add_attribute('size', arch_info.size_in_bits(name)) if arch_info.smallest(name) is not None: newType.add_attribute('smallest', arch_info.smallest(name)) if arch_info.largest(name) is not None: newType.add_attribute('largest', arch_info.largest(name)) ast.add_child(newType) for alias, name in aliases: newType = TypeNode(ast, None, 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(newType, 'target', [type_list[index]]) newType.add_child(target) ast.add_child(newType) if typetype == 'idl4': create_special_C(arch_info, ast) if typetype == 'mig': create_special_MIG(arch_info, ast) return ast
def clauses(self): l = None l = Node(None, "clauses") try: ## for error handling pass c=self.clause() l.add_child(c) while True: if (self.LA(1)==DEFINEDAS or self.LA(1)==FULLSTOP or self.LA(1)==ATOM): pass c=self.clause() l.add_child(c) else: break except antlr.RecognitionException, ex: self.reportError(ex) self.consume() self.consumeUntil(_tokenSet_0)
def get_call(scope_ast, pt, ast_gen): """ Translate function calls. PT looks like this: expression call expression identifier (value) = <function name> expression comma (optional) expression param1 expression param2 ... We turn them into this AST: expression call expression scoped_name (value) = <function name> expression parameters (optional) expression param1 expression param2 ... """ # Translate function calls. node = Node(scope_ast, 'expression', leaf='call', source=pt) expression_identifier = pt.children[0] assert expression_identifier.leaf == 'id_expression' node.add_child(get_scoped_name(node, expression_identifier, ast_gen)) # Add parameters. if len(pt.children) == 2: node.add_child(_get_call_params(node, pt.children[1], ast_gen)) return node
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 _make_struct(ast, name, *args): struct = TypeNode(ast, leaf = name, source_file = '<builtin>') struct.add_attribute('meta_type', 'struct') members = Node(struct, 'members') struct.add_child(members) for arg_type, arg_name in args: inst_node = Node(None, 'type_instance', leaf = arg_name) target = Node(inst_node, 'target') type_node = getTypenode(arg_type, ast) target.add_child(type_node) inst_node.add_child(target) members.add_child(inst_node) return struct
def variable(self): t = None v = None try: ## for error handling pass v = self.LT(1) self.match(VARIABLE) t = Node(None, "variable", leaf=v.getText()) except antlr.RecognitionException, ex: self.reportError(ex) self.consume() self.consumeUntil(_tokenSet_1)
def number(self): n = None nt = None try: ## for error handling pass nt = self.LT(1) self.match(NUMBER) n = Node(None, "number", leaf=nt.getText()) except antlr.RecognitionException, ex: self.reportError(ex) self.consume() self.consumeUntil(_tokenSet_1)
def get_scoped_name(scope_ast, pt, ast_gen): the_name = pt.get_single_attribute('value') scoped_name = Node(scope_ast, 'expression', leaf='scoped_name', source=pt) scoped_name.add_attribute('value', the_name) # Try to find a target. type_ast = getNode(the_name, scope_ast) # It's not the end of the world if we don't find a target. if type_ast: target_ast = Node(scoped_name, 'target', [type_ast]) scoped_name.add_child(target_ast) return scoped_name
def node(self, name, children=None, leaf=None, result=None, token=None): if not token: token = sys._getframe(1).f_locals.get('wt') if token: t_line = token.getLine() else: t_line = None t_file = self.getFilename() return Node(None, name, children, leaf, result=result, source_line=t_line, source_file=t_file)
def clause(self): c = None c = Node(None, "clause") try: ## for error handling pass if (self.LA(1) == ATOM) and (self.LA(2) == LBRACKET): pass s = self.structure() c.add_child(s) elif (self.LA(1) == ATOM) and (self.LA(2) == DEFINEDAS or self.LA(2) == FULLSTOP): pass a = self.atom() c.add_child(a) elif (self.LA(1) == DEFINEDAS or self.LA(1) == FULLSTOP): pass else: raise antlr.NoViableAltException(self.LT(1), self.getFilename()) la1 = self.LA(1) if False: pass elif la1 and la1 in [DEFINEDAS]: pass self.match(DEFINEDAS) e = self.expression() c.add_child(e) elif la1 and la1 in [FULLSTOP]: pass else: raise antlr.NoViableAltException(self.LT(1), self.getFilename()) self.match(FULLSTOP) except antlr.RecognitionException, ex: self.reportError(ex) self.consume() self.consumeUntil(_tokenSet_2)
def relation_expr(self): e = None try: ## for error handling pass e = self.basic_expr() while True: if ((self.LA(1) >= LESSTHANOREQ and self.LA(1) <= EQUAL)): pass o = self.relation_op() e2 = self.basic_expr() e = Node(None, "expression", leaf=o, children=[e, e2]) else: break except antlr.RecognitionException, ex: self.reportError(ex) self.consume() self.consumeUntil(_tokenSet_6)
def _get_call_params(scope_ast, pt, ast_gen): params_ast = Node(scope_ast, 'expression', leaf='parameters', source=pt) if pt.leaf == 'comma': for param_pt in pt.get_children_named('expression'): param_expr_ast = get_expression(params_ast, param_pt, ast_gen) if param_expr_ast is None: param_pt.print_tree() params_ast.add_child(param_expr_ast) elif pt.name == 'expression': params_ast.add_child(get_expression(params_ast, pt, ast_gen)) else: # hm pt.print_tree() raise Error("Unable to decode parameter list") return params_ast
def create_basictype_ast(arch_name, typetype): ast = Node(None, 'MagpieAST', None) arch_info = construct_for_arch(arch_name, typetype) if typetype == 'idl4': names = CORBA_NAMES + C_NAMES aliases = C_ALIAS if typetype == 'mig': names = CORBA_NAMES + MIG_NAMES aliases = MIG_ALIAS for name in names: newType = TypeNode(ast, None, source_file='<builtin>') newType.leaf = name newType.add_attribute('meta_type', 'basic') newType.add_attribute('size', arch_info.size_in_bits(name)) if arch_info.smallest(name) is not None: newType.add_attribute('smallest', arch_info.smallest(name)) if arch_info.largest(name) is not None: newType.add_attribute('largest', arch_info.largest(name)) ast.add_child(newType) for alias, name in aliases: newType = TypeNode(ast, None, 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(newType, 'target', [type_list[index]]) newType.add_child(target) ast.add_child(newType) if typetype == 'idl4': create_special_C(arch_info, ast) if typetype == 'mig': create_special_MIG(arch_info, ast) return ast
def prologlist(self): l = None try: ## for error handling if (self.LA(1) == LSQUARE) and (self.LA(2) == RSQUARE): pass self.match(LSQUARE) self.match(RSQUARE) l = Node(None, "structure", leaf='.') elif (self.LA(1) == LSQUARE) and (_tokenSet_9.member(self.LA(2))): pass self.match(LSQUARE) c = self.list_contents() self.match(RSQUARE) l = c else: raise antlr.NoViableAltException(self.LT(1), self.getFilename()) except antlr.RecognitionException, ex: self.reportError(ex) self.consume() self.consumeUntil(_tokenSet_1)
def clauses(self): l = None l = Node(None, "clauses") try: ## for error handling pass c = self.clause() l.add_child(c) while True: if (self.LA(1) == DEFINEDAS or self.LA(1) == FULLSTOP or self.LA(1) == ATOM): pass c = self.clause() l.add_child(c) else: break except antlr.RecognitionException, ex: self.reportError(ex) self.consume() self.consumeUntil(_tokenSet_0)
def get_literal(scope_ast, pt, ast_gen): """ Translate a constant that looks like this: expression literal int 0 (or "float 3.1415", etc) to: expression literal (value) = <constant> type (backref) """ # Extract type type_list = [pt.child().name] type_ast = ast_gen.get_type_from_list(scope_ast, type_list) value = pt.child().leaf node = Node(scope_ast, 'expression', leaf='literal', source=pt) node.add_attribute('value', value) node.add_child(type_ast) return node
def create_special_MIG(arch_info, ast): poly_list = (("MACH_MSG_TYPE_PORT_RECEIVE", '32', 'MACH_MSG_TYPE_PORT_internal', 'MACH_MSG_TYPE_POLYMORPHIC'), ("MACH_MSG_TYPE_PORT_SEND", '32', 'MACH_MSG_TYPE_PORT_internal', 'MACH_MSG_TYPE_POLYMORPHIC'), ("MACH_MSG_TYPE_PORT_SEND_ONCE", '32', 'MACH_MSG_TYPE_SEND_internal', 'MACH_MSG_TYPE_POLYMORPHIC'), ("MACH_MSG_TYPE_COPY_SEND", '32', 'MACH_MSG_TYPE_SEND_internal', 'MACH_MSG_TYPE_PORT_SEND'), ("MACH_MSG_TYPE_MAKE_SEND", '32', 'MACH_MSG_TYPE_SEND_internal', 'MACH_MSG_TYPE_PORT_SEND'), ("MACH_MSG_TYPE_MOVE_SEND", '32', 'MACH_MSG_TYPE_SEND_internal', 'MACH_MSG_TYPE_PORT_SEND'), ("MACH_MSG_TYPE_MAKE_SEND_ONCE", '32', 'MACH_MSG_TYPE_SEND_internal', 'MACH_MSG_TYPE_PORT_SEND_ONCE'), ("MACH_MSG_TYPE_MOVE_SEND_ONCE", '32', 'MACH_MSG_TYPE_SEND_internal', 'MACH_MSG_TYPE_PORT_SEND_ONCE'), ("MACH_MSG_TYPE_MOVE_RECEIVE", '32', 'MACH_MSG_TYPE_RECEIVE_internal', 'MACH_MSG_TYPE_PORT_RECEIVE')) type_list = ast.children for name, size, sender, receiver in poly_list: newType = TypeNode(ast, None, source_file='<builtin>') newType.leaf = name newType.add_attribute('meta_type', 'polymorphic') info = Node(newType, 'info') newType.add_child(info) index = [element.leaf for element in type_list].index(sender) info.add_attribute('sender_type', type_list[index]) index = [element.leaf for element in type_list].index(receiver) info.add_attribute('receiver_type', type_list[index]) type_list.append(newType) ast.add_child(newType)
def symtable_add(self, name, *nodes): node = Node(None, 'unknown', children = nodes) self.symbolTable.add(name, node)
def expr_node(parent, oper, child): container = Node(None, "expression", leaf=oper, children=[parent, child]) return container
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 _make_struct(ast, name, *args): struct = TypeNode(ast, leaf=name, source_file='<builtin>') struct.add_attribute('meta_type', 'struct') members = Node(struct, 'members') struct.add_child(members) for arg_type, arg_name in args: inst_node = Node(None, 'type_instance', leaf=arg_name) target = Node(inst_node, 'target') type_node = getTypenode(arg_type, ast) target.add_child(type_node) inst_node.add_child(target) members.add_child(inst_node) return struct
def add_listterm(t, parent): child = Node(None, "structure", leaf='.', children=[t]) parent.add_child(child) return child