def translate_packages(ast): for p in ast.findall("TypeImportDec"): if DEBUG: print p if len(p) == 1 and p[0][0].name == "Id": pkg = p[0][0][0] module = p[0][1][0] rpkg = rename_pkg(pkg, module) if rpkg != pkg: p[0][0].replace(aterm.decode('Id("%s")' % (rpkg))) if DEBUG: print pkg, "-->", p[0][0] print " ", p # import a second time so fully-qualified name works too reimport = aterm.decode('Id("import %s")' % pkg) p.add_after(reimport) reimport.add_after(aterm.decode('Id("java_module(%s)")' % pkg)) for p in ast.findall("PackageDec"): packagePath = ".".join([id[0] for id in p[1][0]]) packagePath = rename_pkg(packagePath) baseName = packagePath.split(".")[0] parents = [p for p in p.parents()] if parents[0].name == "Some": putafter = parents[0] else: putafter = p for q in ast.findall(("TypeImportOnDemandDec", "TypeImportDec")): putafter = q.up up = putafter.up after = up[putafter.pos() + 1] after.append(aterm.decode(r'Id("java.set_this_package(\"%s\")")' % packagePath))
def test_annotation(self): t = decode("T()") self.assertEquals(t.annotation, None) t = decode("T(){}") self.assertEquals(t.annotation, None) t = decode("A(B(){C()}){1}") self.assert_(isinstance(t, ATerm)) self.assertEquals(t.annotation, 1) self.assertEquals(t[0].annotation.encode(),"C()")
def test_simple(self): t = decode('T') self.assert_(isinstance(t, ATerm)) self.assertEquals(t.name, 'T') self.assertEquals(len(t), 0) t = decode('T()') self.assert_(isinstance(t, ATerm)) self.assertEquals(t.name, 'T') self.assertEquals(len(t), 0) t = decode('()') self.assert_(isinstance(t, ATuple)) self.assertEquals(t.name, '()') self.assertEquals(len(t), 0)
def test_strings(self): t = decode('T("")') self.assert_(isinstance(t, ATerm)) self.assertEquals(len(t[0]), 0) t = decode('T("\\"")') self.assert_(isinstance(t, ATerm)) self.assertEquals(len(t[0]), 1) t = decode('T("\\"\\"")') self.assert_(isinstance(t, ATerm)) self.assertEquals(len(t[0]), 2) t = decode('T("äö")') self.assert_(isinstance(t, ATerm)) self.assertEquals(len(t[0]), 2)
def test_find(self): t = decode('A(B(C(D())),[])') r = [i for i in t.findall('C') ] self.assertEquals(len(r), 1) self.assertEquals(r[0][0].name,"D") r = [i for i in t.findall(['B','C']) ] self.assertEquals(len(r), 2)
def test_pos(self): t = decode('A(B(),C(),D())') self.assertEquals(t[0].name, "B") self.assertEquals(t[0].pos(), 0) self.assertEquals(t[1].name, "C") self.assertEquals(t[1].pos(), 1) self.assertEquals(t[2].name, "D") self.assertEquals(t[2].pos(), 2)
def convert_comments(ast): """ - converts java comments to python commets - moves comments further down in ast tree to change its position from before class/method in java to in class/function in pythoin """ make_docstring_for = ['ClassDec','ConstrDec','MethodDec','AbstractMethodDec','AnnoDec','InterfaceDec'] for exp in ast.walk(): if isinstance(exp,aterm.ATerm) and exp.annotation is not None: p = '/'.join(exp.path()) + '/' + exp.name comment = exp.annotation[1] # logger.debug(repr(comment)) if exp.name in make_docstring_for and comment.startswith('/**'): #try: if exp.name == 'AbstractMethodDec': #print "convert from",exp.pp() exp = convert_abstract_method(exp) #print "to",exp.pp() if exp.name == "MethodDec": if exp[1].name == 'NoMethodBody': exp[1]= aterm.decode('Block([])') #find destination for docstring if exp.name == 'ClassDec': dest = exp.ClassBody[0] elif exp.name == "ConstrDec": dest = exp.ConstrBody[1] elif exp.name == "AnnoDec" or exp.name == "InterfaceDec": dest = exp[1] else: dest = exp.Block[0] docstring = make_docstring(exp.annotation[1]) exp.annotation = None dest.insert(0,docstring) #except Exception,e: # print "unable to create docstring for", java_front.pp_aterm(exp) # print e else: exp.annotation[1] = convert_comment(exp.annotation[1]) # move comments out of inner constructs not_wanted = ["Case","[]","()"] for exp in ast.walk(): if isinstance(exp,aterm.ATerm): if exp.annotation is not None: logging.debug(exp.name, exp.path()) if exp.name in not_wanted: for dest in exp.parents(): if not dest.name in not_wanted: break append_annotation(dest,exp.annotation) exp.annotation = None
def concat_ids(ast): "replaces AmbNames by Ids" for exp in itertools.chain(aterm.reverse(ast.findall("AmbName")),aterm.reverse(ast.findall("PackageOrTypeName"))): if len(exp)==1: exp.replace(exp[0]) elif len(exp)==2: if exp[0].name == "Id" and exp[1].name == "Id": exp.replace(aterm.decode('Id("%s.%s")' % (exp[0][0],exp[1][0]) )) return ast
def fix_annonclass(ast): for ni in ast.findall('NewInstance'): ##print "QQQQ", ni[3] if ni[3].name == "Some": parents = [ni] + [p for p in ni.parents()] for p in parents: if p.name in can_add_classdec: #add declaration for anon class cblock = aterm.decode(class_dec) cblock[1].extend(ni[3][0].copy()) parents[parents.index(p) - 2].add_before(cblock) #remove inline declaration nblock = aterm.decode(class_make_instance) ni.replace(nblock) return ast
def translate_packages(ast): for p in ast.findall("TypeImportDec"): if DEBUG: print p if len(p)==1 and p[0][0].name=="Id": pkg = p[0][0][0] module = p[0][1][0] rpkg = rename_pkg(pkg,module) if rpkg != pkg: p[0][0].replace(aterm.decode('Id("%s")' % (rpkg)) ) if DEBUG: print pkg,"-->",p[0][0] print " ",p
def convert_abstract_method(exp): if exp.name != "AbstractMethodDec": raise RuntimeError('AbstractMethodDec expected') md = aterm.ATerm('MethodDec') exp.replace(md) exp.name = 'MethodDecHead' md.append(exp) md.append(aterm.decode('Block([])')) exp[0].insert(0,aterm.ATerm('Abstract')) md.annotation = exp.annotation exp.annotation = None return md
def de(self,l,r): lval = decode(l) self.assertEquals()
def de(self,src,enc=None): d = decode(src) if enc is None: enc = src self.assertEquals(repr(d),enc)
def test_tree(self): t = decode('A(B(C()),[])') self.assertEquals(t[0].name, "B") self.assertEquals(t[0].up, t) self.assertEquals(t[1].up, t)
def test_allatonce(self): t = decode('T("", "bla", "bla" , S([ "", "bla", "bla" ] ))') self.assert_(isinstance(t, ATerm)) self.assertEquals(t.name, "T") self.assertEquals(len(t), 4)
def test_simple(self): t = decode("T") self.assert_(isinstance(t, ATerm)) self.assertEquals(t.name, "T") self.assertEquals(len(t), 0)
import fixstradd import addinit import fixcomments import fixenum import fix_values import rename_methods import fix_expressions import fix_annonclass import fix_cast def run(ast): rename.run(ast) fix_values.run(ast) fix_expressions.run(ast) fixnames.run(ast) rename_methods.run(ast) fixstradd.run(ast) addinit.run(ast) # translatepackages.run(ast) #after addinit fixenum.run(ast) fix_annonclass.run(ast) fix_cast.run(ast) fixcomments.run(ast) # run at least after addinit, to have docstrings in first position if __name__ == "__main__": ast = aterm.decode(unicode(sys.stdin.read(), "utf8")) run(ast) print ast.encode().encode("utf8")
def test_walkback(self): t = decode('A(B(),B(C(D(),E())),F())') e = t.findfirst('E') wb = ''.join([ n.name for n in e.walkback()]) self.assertEquals(wb,'DCBBA')
def parse_java(fname): pj = run_cmd(['parse-java','--preserve-comments','-i',fname]) if len(pj.err)>0: raise ParseError('parse-java:'+pj.err) return aterm.decode(pj.res)
# change invocation .toString() for i in e.findall("Invoke"): try: if i[0][0][1][0] == "toString": i.replace( make_string(i[0][0][0]) ) except: pass def fix_str_add(ast): "replace str + expr with str + str(expr)" for p in aterm.reverse(ast.findall("Plus")): if len(p)==2: if is_string(p[0]) or is_string(p[1]): if not is_string(p[0]): p[0].replace(make_string(p[0])) if not is_string(p[1]): p[1].replace(make_string(p[1])) def run(ast): fix_toString(ast) fix_str_add(ast) if __name__ == '__main__': ast = aterm.decode(sys.stdin.read()) run(ast) if not DEBUG: print ast
import fixnames import translatepackages import fixstradd import addinit import fixcomments import fixenum import fix_values import rename_methods import fix_expressions import fix_annonclass def run(ast): rename.run(ast) fix_values.run(ast) fix_expressions.run(ast) fixnames.run(ast) rename_methods.run(ast) translatepackages.run(ast) fixstradd.run(ast) addinit.run(ast) fixenum.run(ast) fix_annonclass.run(ast) fixcomments.run(ast) #run at least after addinit, to have docstrings in first position if __name__ == '__main__': ast = aterm.decode(unicode(sys.stdin.read(),'utf8')) run(ast) print ast.encode().encode('utf8')
def test_copy(self): a = decode('A(B(1),B(C(D("äöäö"),E())),F())') b = a.copy() self.assertEquals(a.encode(),b.encode())
def make_string(e): return aterm.decode(u"ToStr(%s)" % e.encode())
def test_walk(self): t = decode("A(B(C(),D()))") w = ''.join([n.name for n in t.walk()]) self.assertEquals(w, "ABCD")