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 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 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 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 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 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 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 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_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 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 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 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 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 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 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 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_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 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 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 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 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_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 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 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 expr_node(parent, oper, child): container = Node(None, "expression", leaf=oper, children=[parent, child]) return container
def symtable_add(self, name, *nodes): node = Node(None, 'unknown', children = nodes) self.symbolTable.add(name, node)
def add_listterm(t, parent): child = Node(None, "structure", leaf='.', children=[t]) parent.add_child(child) return child