0001: /*
0002: * Janino - An embedded Java[TM] compiler
0003: *
0004: * Copyright (c) 2001-2007, Arno Unkrig
0005: * All rights reserved.
0006: *
0007: * Redistribution and use in source and binary forms, with or without
0008: * modification, are permitted provided that the following conditions
0009: * are met:
0010: *
0011: * 1. Redistributions of source code must retain the above copyright
0012: * notice, this list of conditions and the following disclaimer.
0013: * 2. Redistributions in binary form must reproduce the above
0014: * copyright notice, this list of conditions and the following
0015: * disclaimer in the documentation and/or other materials
0016: * provided with the distribution.
0017: * 3. The name of the author may not be used to endorse or promote
0018: * products derived from this software without specific prior
0019: * written permission.
0020: *
0021: * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
0022: * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
0023: * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
0024: * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
0025: * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
0026: * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
0027: * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
0028: * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
0029: * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
0030: * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
0031: * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
0032: */
0033:
0034: package org.codehaus.janino;
0035:
0036: import java.io.*;
0037: import java.util.*;
0038:
0039: import org.codehaus.janino.Java.CompilationUnit;
0040: import org.codehaus.janino.Java.FunctionDeclarator;
0041: import org.codehaus.janino.Java.SwitchStatement;
0042:
0043: /**
0044: * @author Eugene Kuleshov
0045: */
0046: public class AstGeneratorVisitor implements
0047: Visitor.ComprehensiveVisitor {
0048: private static final int TAB_SIZE = 4;
0049:
0050: private static final String TAB_FILLER = " ";
0051:
0052: private final String name;
0053: private final PrintWriter pw;
0054:
0055: private int level = 0;
0056: private Set instances = new HashSet();
0057:
0058: public static void main(String[] args) throws Exception {
0059: for (int i = 0; i < args.length; ++i) {
0060: Java.CompilationUnit cu = new Parser(new Scanner(args[i]))
0061: .parseCompilationUnit();
0062: OutputStreamWriter w = new OutputStreamWriter(System.out);
0063: new AstGeneratorVisitor(w, "MyAstGenerator")
0064: .generateCompilationUnit(cu);
0065: w.flush();
0066: }
0067: }
0068:
0069: public AstGeneratorVisitor(Writer w, String name) {
0070: this .pw = new PrintWriter(w);
0071: this .name = name;
0072: }
0073:
0074: public void generateCompilationUnit(Java.CompilationUnit cu) {
0075: write("package org.codehaus.janino;");
0076: write();
0077: write("import org.codehaus.janino.Java;");
0078: write("import org.codehaus.janino.Mod;");
0079: write("import java.util.*;");
0080: write();
0081:
0082: write("public class " + this .name + " implements "
0083: + AstCompilationUnitGenerator.class.getName() + " {");
0084: this .level++;
0085: write("public static final String FILE_NAME = \""
0086: + cu.optionalFileName + "\";");
0087: write();
0088:
0089: write("public Java.CompilationUnit generate() throws Exception {");
0090: this .level++;
0091:
0092: write("Java.CompilationUnit cu = new Java.CompilationUnit(\""
0093: + cu.optionalFileName + "\");");
0094:
0095: if (cu.optionalPackageDeclaration != null) {
0096: write("cu.setPackageDeclaration(new Java.PackageDeclaration("
0097: + getLocation(cu.optionalPackageDeclaration)
0098: + ", \""
0099: + cu.optionalPackageDeclaration.packageName
0100: + "\"));");
0101: }
0102:
0103: for (Iterator it = cu.importDeclarations.iterator(); it
0104: .hasNext();) {
0105: write("cu.addImportDeclaration(generateImportDeclaration"
0106: + getSuffix(it.next()) + "());");
0107: }
0108:
0109: for (Iterator it = cu.packageMemberTypeDeclarations.iterator(); it
0110: .hasNext();) {
0111: Java.PackageMemberTypeDeclaration pmtd = (Java.PackageMemberTypeDeclaration) it
0112: .next();
0113: write("cu.addPackageMemberTypeDeclaration(generateMemberTypeDeclaration"
0114: + getSuffix(pmtd) + "(cu));");
0115: }
0116:
0117: write("return cu;");
0118: this .level--;
0119: write("}");
0120: write();
0121:
0122: // generator methods for child nodes
0123: for (Iterator it = cu.importDeclarations.iterator(); it
0124: .hasNext();) {
0125: ((CompilationUnit.ImportDeclaration) it.next())
0126: .accept(this );
0127: }
0128: for (Iterator it = cu.packageMemberTypeDeclarations.iterator(); it
0129: .hasNext();) {
0130: ((Java.PackageMemberTypeDeclaration) it.next())
0131: .accept(this );
0132: }
0133:
0134: // helper methods
0135: write("private Location getLocation(int line, int column) {");
0136: this .level++;
0137: write("return new Location(FILE_NAME, (short) line, (short) column);");
0138: this .level--;
0139: write("}");
0140: write();
0141:
0142: this .level--;
0143: write("}");
0144: write();
0145: }
0146:
0147: public void visitSingleTypeImportDeclaration(
0148: CompilationUnit.SingleTypeImportDeclaration stid) {
0149: write("private Java.SingleTypeImportDeclaration generateImportDeclaration"
0150: + getSuffix(stid) + "() throws Exception {");
0151: this .level++;
0152: write("return new Java.SingleTypeImportDeclaration("
0153: + getLocation(stid) + ", "
0154: + arrayToString(stid.identifiers) + ");");
0155: this .level--;
0156: write("}");
0157: write();
0158: }
0159:
0160: public void visitTypeImportOnDemandDeclaration(
0161: CompilationUnit.TypeImportOnDemandDeclaration tiodd) {
0162: write("private Java.TypeImportOnDemandDeclaration generateImportDeclaration"
0163: + getSuffix(tiodd) + "() throws Exception {");
0164: this .level++;
0165: write("return new Java.TypeImportOnDemandDeclaration("
0166: + getLocation(tiodd) + ", "
0167: + arrayToString(tiodd.identifiers) + ");");
0168: this .level--;
0169: write("}");
0170: write();
0171: }
0172:
0173: public void visitAnonymousClassDeclaration(
0174: Java.AnonymousClassDeclaration acd) {
0175: write("private Java.AnonymousClassDeclaration generateLocalClassDeclaration"
0176: + getSuffix(acd)
0177: + "(Java.Scope scope) throws Exception {");
0178: this .level++;
0179:
0180: write("Java.AnonymousClassDeclaration declaration = "
0181: + "new Java.AnonymousClassDeclaration("
0182: + getLocation(acd) + ", scope, " + "generateType"
0183: + getSuffix(acd.baseType) + "(scope));");
0184:
0185: generateClassDeclarationBody(acd);
0186:
0187: write("return declaration;");
0188: this .level--;
0189: write("}");
0190: write();
0191:
0192: // generator methods
0193: acd.baseType.accept((Visitor.TypeVisitor) this );
0194:
0195: generateClassDeclarationBodyMethods(acd);
0196: }
0197:
0198: public void visitLocalClassDeclaration(
0199: Java.LocalClassDeclaration lcd) {
0200: write("private Java.LocalClassDeclaration generateLocalClassDeclaration"
0201: + getSuffix(lcd)
0202: + "(Java.Block scope) throws Exception {");
0203: this .level++;
0204:
0205: write("Java.LocalClassDeclaration declaration = "
0206: + "new Java.LocalClassDeclaration("
0207: + getLocation(lcd)
0208: + ", scope, "
0209: + getModifiers(lcd.modifiers)
0210: + ", \""
0211: + lcd.name
0212: + "\", "
0213: + (lcd.optionalExtendedType == null ? "null, "
0214: : "generateType"
0215: + getSuffix(lcd.optionalExtendedType)
0216: + "(scope), ")
0217: + getGenerateTypes(lcd.implementedTypes, "scope")
0218: + ");");
0219:
0220: generateClassDeclarationBody(lcd);
0221:
0222: write("return declaration;");
0223: this .level--;
0224: write("}");
0225: write();
0226:
0227: // generator methods
0228: if (lcd.optionalExtendedType != null) {
0229: lcd.optionalExtendedType.accept((Visitor.TypeVisitor) this );
0230: }
0231:
0232: generateTypes(lcd.implementedTypes);
0233:
0234: generateClassDeclarationBodyMethods(lcd);
0235: }
0236:
0237: public void visitPackageMemberClassDeclaration(
0238: Java.PackageMemberClassDeclaration pmcd) {
0239: write("private Java.PackageMemberTypeDeclaration generateMemberTypeDeclaration"
0240: + getSuffix(pmcd)
0241: + "(Java.CompilationUnit cu) throws Exception {");
0242: this .level++;
0243:
0244: write("Java.PackageMemberClassDeclaration declaration = "
0245: + "new Java.PackageMemberClassDeclaration("
0246: + getLocation(pmcd)
0247: + ", cu, "
0248: + printStringLiteral(pmcd.getDocComment())
0249: + ", "
0250: + getModifiers(pmcd.modifiers)
0251: + ", \""
0252: + pmcd.name
0253: + "\", "
0254: + (pmcd.optionalExtendedType == null ? "null"
0255: : "generateType"
0256: + getSuffix(pmcd.optionalExtendedType)
0257: + "(cu)") + ", "
0258: + getGenerateTypes(pmcd.implementedTypes, "cu") + ");");
0259: write();
0260:
0261: generateClassDeclarationBody(pmcd);
0262:
0263: write("return declaration;");
0264: this .level--;
0265: write("}");
0266: write();
0267:
0268: generateTypes(pmcd.implementedTypes);
0269: if (pmcd.optionalExtendedType != null) {
0270: pmcd.optionalExtendedType
0271: .accept((Visitor.TypeVisitor) this );
0272: }
0273:
0274: generateClassDeclarationBodyMethods(pmcd);
0275: }
0276:
0277: public void visitMemberInterfaceDeclaration(
0278: Java.MemberInterfaceDeclaration mid) {
0279: write("private Java.MemberInterfaceDeclaration generateMemberTypeDeclaration"
0280: + getSuffix(mid)
0281: + "(Java.NamedTypeDeclaration declaringType) throws Exception {");
0282: this .level++;
0283:
0284: write("Java.MemberInterfaceDeclaration declaration = "
0285: + "new Java.MemberInterfaceDeclaration("
0286: + getLocation(mid) + ", declaringType, "
0287: + printStringLiteral(mid.getDocComment()) + ", "
0288: + getModifiers(mid.modifiers) + ", \"" + mid.name
0289: + "\", "
0290: + getGenerateTypes(mid.extendedTypes, "declaringType")
0291: + ");");
0292:
0293: generateAbstractTypeDeclarationBody(mid);
0294:
0295: for (Iterator it = mid.constantDeclarations.iterator(); it
0296: .hasNext();) {
0297: Java.FieldDeclaration fd = (Java.FieldDeclaration) it
0298: .next();
0299: write("declaration.addConstantDeclaration(generateFieldDeclaration"
0300: + getSuffix(fd) + "(declaration));");
0301: }
0302:
0303: write("return declaration;");
0304: this .level--;
0305: write("}");
0306: write();
0307:
0308: generateTypes(mid.extendedTypes);
0309: generateAbstractTypeDeclarationBodyMethods(mid);
0310:
0311: for (Iterator it = mid.constantDeclarations.iterator(); it
0312: .hasNext();) {
0313: ((Java.FieldDeclaration) it.next())
0314: .accept((Visitor.TypeBodyDeclarationVisitor) this );
0315: }
0316: }
0317:
0318: public void visitPackageMemberInterfaceDeclaration(
0319: Java.PackageMemberInterfaceDeclaration pmid) {
0320: write("private Java.PackageMemberInterfaceDeclaration generateMemberTypeDeclaration"
0321: + getSuffix(pmid)
0322: + "(Java.CompilationUnit cu) throws Exception {");
0323: this .level++;
0324:
0325: write("Java.PackageMemberInterfaceDeclaration declaration = "
0326: + "new Java.PackageMemberInterfaceDeclaration(null, cu, "
0327: + printStringLiteral(pmid.getDocComment()) + ", "
0328: + getModifiers(pmid.modifiers) + ", \"" + pmid.name
0329: + "\", " + getGenerateTypes(pmid.extendedTypes, "cu")
0330: + ");");
0331:
0332: generateAbstractTypeDeclarationBody(pmid);
0333:
0334: for (Iterator it = pmid.constantDeclarations.iterator(); it
0335: .hasNext();) {
0336: Java.FieldDeclaration fd = (Java.FieldDeclaration) it
0337: .next();
0338: write("declaration.addConstantDeclaration(generateFieldDeclaration"
0339: + getSuffix(fd) + "(declaration));");
0340: }
0341:
0342: write("return declaration;");
0343:
0344: this .level--;
0345: write("}");
0346: write();
0347:
0348: generateTypes(pmid.extendedTypes);
0349: generateAbstractTypeDeclarationBodyMethods(pmid);
0350:
0351: for (Iterator it = pmid.constantDeclarations.iterator(); it
0352: .hasNext();) {
0353: ((Java.FieldDeclaration) it.next())
0354: .accept((Visitor.TypeBodyDeclarationVisitor) this );
0355: }
0356: }
0357:
0358: public void visitMemberClassDeclaration(
0359: Java.MemberClassDeclaration mcd) {
0360: write("private Java.MemberClassDeclaration generateMemberTypeDeclaration"
0361: + getSuffix(mcd)
0362: + "(Java.NamedTypeDeclaration declaringType) throws Exception {");
0363: this .level++;
0364:
0365: write("Java.MemberClassDeclaration declaration = new Java.MemberClassDeclaration("
0366: + getLocation(mcd)
0367: + ", declaringType, "
0368: + printStringLiteral(mcd.getDocComment())
0369: + ", "
0370: + getModifiers(mcd.modifiers)
0371: + ", \""
0372: + mcd.name
0373: + "\", "
0374: + (mcd.optionalExtendedType == null ? "null"
0375: : "generateType"
0376: + getSuffix(mcd.optionalExtendedType)
0377: + "(declaringType)")
0378: + ", "
0379: + getGenerateTypes(mcd.implementedTypes,
0380: "declaringType") + ");");
0381:
0382: generateClassDeclarationBody(mcd);
0383:
0384: write("return declaration;");
0385: this .level--;
0386: write("}");
0387: write();
0388:
0389: if (mcd.optionalExtendedType != null) {
0390: mcd.optionalExtendedType.accept((Visitor.TypeVisitor) this );
0391: }
0392: generateTypes(mcd.implementedTypes);
0393: generateClassDeclarationBodyMethods(mcd);
0394: }
0395:
0396: public void visitConstructorDeclarator(Java.ConstructorDeclarator cd) {
0397: write("private Java.ConstructorDeclarator generateConstructorDeclarator"
0398: + getSuffix(cd)
0399: + "(Java.ClassDeclaration declaringClass) throws Exception {");
0400: this .level++;
0401:
0402: write("Java.ConstructorDeclarator declaration = new Java.ConstructorDeclarator("
0403: + getLocation(cd)
0404: + ", "
0405: + "declaringClass, "
0406: + printStringLiteral(cd.getDocComment())
0407: + ", "
0408: + getModifiers(cd.modifiers)
0409: + ", "
0410: + (cd.formalParameters == null ? "null"
0411: : (cd.formalParameters.length == 0 ? "new Java.FormalParameter[0]"
0412: : "generateFormalParameters"
0413: + getSuffix(cd.formalParameters)
0414: + "(declaringClass)"))
0415: + ", "
0416: + getGenerateTypes(cd.thrownExceptions,
0417: "declaringClass") + ");");
0418:
0419: if (cd.optionalBody != null) {
0420: write("Java.Block body = generateStatement"
0421: + getSuffix(cd.optionalBody) + "(declaration);");
0422: } else {
0423: write("Java.Block body = new Java.Block(" + getLocation(cd)
0424: + ", declaration);");
0425: }
0426: write("declaration.setBody(body);");
0427: if (cd.optionalConstructorInvocation != null) {
0428: Java.ConstructorInvocation ci = cd.optionalConstructorInvocation;
0429: write("declaration.setExplicitConstructorInvocation(generateConstructorInvocation"
0430: + getSuffix(ci)
0431: + "(declaringClass, declaration, body));");
0432: }
0433:
0434: write("return declaration;");
0435: this .level--;
0436: write("}");
0437: write();
0438:
0439: // generate methods
0440: generateFormalParameters(cd.formalParameters);
0441: generateTypes(cd.thrownExceptions);
0442:
0443: if (cd.optionalConstructorInvocation != null) {
0444: cd.optionalConstructorInvocation
0445: .accept((Visitor.BlockStatementVisitor) this );
0446: }
0447: if (cd.optionalBody != null) {
0448: cd.optionalBody.accept(this );
0449: }
0450: }
0451:
0452: public void visitInitializer(Java.Initializer i) {
0453: write("private Java.Initializer generateFieldDeclaration"
0454: + getSuffix(i)
0455: + "(Java.TypeDeclaration declaringType) throws Exception {");
0456: this .level++;
0457:
0458: write("Java.Initializer declaration = "
0459: + "new Java.Initializer(" + getLocation(i)
0460: + ", declaringType, " + (i.statiC ? "true" : "false")
0461: + ");");
0462: if (i.block != null) {
0463: write("declaration.setBlock(generateStatement"
0464: + getSuffix(i.block) + "(declaration));");
0465: }
0466:
0467: write("return declaration;");
0468: this .level--;
0469: write("}");
0470: write();
0471:
0472: if (i.block != null) {
0473: i.block.accept(this );
0474: }
0475: }
0476:
0477: public void visitMethodDeclarator(Java.MethodDeclarator md) {
0478: write("private Java.MethodDeclarator generateMethodDeclarator"
0479: + getSuffix(md)
0480: + "(Java.AbstractTypeDeclaration declaringType) throws Exception {");
0481: this .level++;
0482:
0483: if (md.optionalBody == null) {
0484: write("return new Java.MethodDeclarator("
0485: + getLocation(md)
0486: + ", "
0487: + "declaringType, "
0488: + printStringLiteral(md.getDocComment())
0489: + ", "
0490: + getModifiers(md.modifiers)
0491: + ", "
0492: + (md.type == null ? "null" : "generateType"
0493: + getSuffix(md.type) + "(declaringType)")
0494: + ", \""
0495: + md.name
0496: + "\", "
0497: + (md.formalParameters == null ? "null"
0498: : (md.formalParameters.length == 0 ? "new Java.FormalParameter[0]"
0499: : "generateFormalParameters"
0500: + getSuffix(md.formalParameters)
0501: + "(declaringType)"))
0502: + ", "
0503: + getGenerateTypes(md.thrownExceptions,
0504: "declaringType") + ");");
0505:
0506: } else {
0507: write("Java.MethodDeclarator declaration = new Java.MethodDeclarator("
0508: + getLocation(md)
0509: + ", "
0510: + "declaringType, "
0511: + printStringLiteral(md.getDocComment())
0512: + ", "
0513: + getModifiers(md.modifiers)
0514: + ", "
0515: + (md.type == null ? "null" : "generateType"
0516: + getSuffix(md.type) + "(declaringType)")
0517: + ", \""
0518: + md.name
0519: + "\", "
0520: + (md.formalParameters == null ? "null"
0521: : (md.formalParameters.length == 0 ? "new Java.FormalParameter[0]"
0522: : "generateFormalParameters"
0523: + getSuffix(md.formalParameters)
0524: + "(declaringType)"))
0525: + ", "
0526: + getGenerateTypes(md.thrownExceptions,
0527: "declaringType") + ");");
0528: write("declaration.setBody(generateStatement"
0529: + getSuffix(md.optionalBody) + "(declaration));");
0530: write("return declaration;");
0531:
0532: }
0533:
0534: this .level--;
0535: write("}");
0536: write();
0537:
0538: if (md.type != null) {
0539: md.type.accept((Visitor.TypeVisitor) this );
0540: }
0541: generateTypes(md.thrownExceptions);
0542: generateFormalParameters(md.formalParameters);
0543: if (md.optionalBody != null) {
0544: md.optionalBody.accept(this );
0545: }
0546: }
0547:
0548: public void visitFieldDeclaration(Java.FieldDeclaration fd) {
0549: write("private Java.FieldDeclaration generateFieldDeclaration"
0550: + getSuffix(fd)
0551: + "(Java.AbstractTypeDeclaration declaringType) throws Exception {");
0552: this .level++;
0553:
0554: write("Java.FieldDeclaration declaration = new Java.FieldDeclaration("
0555: + getLocation(fd)
0556: + ", "
0557: + "declaringType, "
0558: + printStringLiteral(fd.getDocComment())
0559: + ", "
0560: + getModifiers(fd.modifiers)
0561: + ", "
0562: + "generateType"
0563: + getSuffix(fd.type) + "(declaringType)" + ");");
0564: write("declaration.setVariableDeclarators(generateVariableDeclarators"
0565: + getSuffix(fd.variableDeclarators)
0566: + "(declaration, declaration));");
0567:
0568: write("return declaration;");
0569: this .level--;
0570: write("}");
0571: write();
0572:
0573: fd.type.accept((Visitor.TypeVisitor) this );
0574: generateVariableDeclarators(fd.variableDeclarators);
0575: }
0576:
0577: public void visitLabeledStatement(Java.LabeledStatement ls) {
0578: write("private Java.LabeledStatement generateStatement"
0579: + getSuffix(ls)
0580: + "(Java.Block scope) throws Exception {");
0581: this .level++;
0582:
0583: write("Java.LabeledStatement statement = new Java.LabeledStatement("
0584: + getLocation(ls) + ", scope, \"" + ls.label + "\");");
0585: write("statement.setBody(generateStatement"
0586: + getSuffix(ls.body) + "(scope));");
0587: write("return statement;");
0588:
0589: this .level--;
0590: write("}");
0591: write();
0592:
0593: ls.body.accept(this );
0594: }
0595:
0596: public void visitBlock(Java.Block b) {
0597: write("private Java.Block generateStatement" + getSuffix(b)
0598: + "(Java.Scope scope) throws Exception {");
0599: this .level++;
0600:
0601: write("Java.Block statement = new Java.Block(" + getLocation(b)
0602: + ", scope);");
0603:
0604: for (Iterator it = b.statements.iterator(); it.hasNext();) {
0605: Java.BlockStatement bs = (Java.BlockStatement) it.next();
0606: write("statement.addStatement(generateStatement"
0607: + getSuffix(bs) + "(statement));");
0608: }
0609:
0610: write("return statement;");
0611: this .level--;
0612: write("}");
0613: write();
0614:
0615: for (Iterator it = b.statements.iterator(); it.hasNext();) {
0616: ((Java.BlockStatement) it.next()).accept(this );
0617: }
0618: }
0619:
0620: public void visitExpressionStatement(Java.ExpressionStatement es) {
0621: write("private Java.ExpressionStatement generateStatement"
0622: + getSuffix(es)
0623: + "(Java.BlockStatement enclosingBlockStatement) throws Exception {");
0624: this .level++;
0625:
0626: write("return new Java.ExpressionStatement(generateAtom"
0627: + getSuffix(es.rvalue)
0628: + "(enclosingBlockStatement), enclosingBlockStatement);");
0629: this .level--;
0630: write("}");
0631: write();
0632:
0633: es.rvalue.accept((Visitor.RvalueVisitor) this );
0634: }
0635:
0636: public void visitIfStatement(Java.IfStatement is) {
0637: write("private Java.IfStatement generateStatement"
0638: + getSuffix(is)
0639: + "(Java.Block scope) throws Exception {");
0640: this .level++;
0641:
0642: write("return new Java.IfStatement("
0643: + getLocation(is)
0644: + ", scope, "
0645: + "generateAtom"
0646: + getSuffix(is.condition)
0647: + "(scope), "
0648: + "generateStatement"
0649: + getSuffix(is.thenStatement)
0650: + "(scope), "
0651: + (is.optionalElseStatement == null ? "null"
0652: : "generateStatement"
0653: + getSuffix(is.optionalElseStatement)
0654: + "(scope)") + ");");
0655: this .level--;
0656: write("}");
0657: write();
0658:
0659: is.condition.accept((Visitor.RvalueVisitor) this );
0660: is.thenStatement.accept(this );
0661: if (is.optionalElseStatement != null) {
0662: is.optionalElseStatement.accept(this );
0663: }
0664: }
0665:
0666: public void visitForStatement(Java.ForStatement fs) {
0667: write("private Java.ForStatement generateStatement"
0668: + getSuffix(fs)
0669: + "(Java.Block scope) throws Exception {");
0670: this .level++;
0671:
0672: write("Java.ForStatement statement = new Java.ForStatement("
0673: + getLocation(fs) + ", scope);");
0674: write("statement.set("
0675: + (fs.optionalInit == null ? "null"
0676: : "generateStatement"
0677: + getSuffix(fs.optionalInit)
0678: + "(scope)")
0679: + ", "
0680: + (fs.optionalCondition == null ? "null"
0681: : "generateAtom"
0682: + getSuffix(fs.optionalCondition)
0683: + "(scope)")
0684: + ", "
0685: + getGenerateRvalues(fs.optionalUpdate, "statement")
0686: + ", "
0687: + (fs.body == null ? "null" : "generateStatement"
0688: + getSuffix(fs.body) + "(scope)") + ");");
0689: write("return statement;");
0690: this .level--;
0691: write("}");
0692: write();
0693:
0694: if (fs.optionalInit != null) {
0695: fs.optionalInit.accept(this );
0696: }
0697: if (fs.optionalCondition != null) {
0698: fs.optionalCondition.accept((Visitor.RvalueVisitor) this );
0699: }
0700: generateRvalues(fs.optionalUpdate);
0701: if (fs.body != null) {
0702: fs.body.accept(this );
0703: }
0704: }
0705:
0706: public void visitWhileStatement(Java.WhileStatement ws) {
0707: write("private Java.WhileStatement generateStatement"
0708: + getSuffix(ws)
0709: + "(Java.Block scope) throws Exception {");
0710: this .level++;
0711:
0712: write("Java.WhileStatement statement = new Java.WhileStatement("
0713: + getLocation(ws)
0714: + ", scope, "
0715: + "generateAtom"
0716: + getSuffix(ws.condition) + "(scope));");
0717: if (ws.body != null) {
0718: write("statement.setBody(generateStatement"
0719: + getSuffix(ws.body) + "(scope));");
0720: }
0721: write("return statement;");
0722: this .level--;
0723: write("}");
0724: write();
0725:
0726: ws.condition.accept((Visitor.RvalueVisitor) this );
0727: if (ws.body != null) {
0728: ws.body.accept(this );
0729: }
0730: }
0731:
0732: public void visitTryStatement(Java.TryStatement ts) {
0733: write("private Java.TryStatement generateStatement"
0734: + getSuffix(ts)
0735: + "(Java.Scope scope) throws Exception {");
0736: this .level++;
0737:
0738: write("Java.TryStatement statement = new Java.TryStatement("
0739: + getLocation(ts) + ", scope);");
0740: write("statement.setBody(generateStatement"
0741: + getSuffix(ts.body) + "(statement));");
0742:
0743: for (Iterator it = ts.catchClauses.iterator(); it.hasNext();) {
0744: Java.CatchClause cc = (Java.CatchClause) it.next();
0745: write("statement.addCatchClause(new Java.CatchClause("
0746: + "generateFormalParameter"
0747: + getSuffix(cc.caughtException) + "(statement), "
0748: + "generateStatement" + getSuffix(cc.body)
0749: + "(statement)));");
0750: }
0751:
0752: if (ts.optionalFinally != null) {
0753: write("statement.setFinally(generateStatement"
0754: + getSuffix(ts.optionalFinally) + "(statement));");
0755: }
0756:
0757: write("return statement;");
0758: this .level--;
0759: write("}");
0760: write();
0761:
0762: // generate methods
0763: ts.body.accept(this );
0764:
0765: if (ts.optionalFinally != null) {
0766: ts.optionalFinally.accept(this );
0767: }
0768:
0769: for (Iterator it = ts.catchClauses.iterator(); it.hasNext();) {
0770: Java.CatchClause cc = (Java.CatchClause) it.next();
0771: this .generateFormalParameter(cc.caughtException);
0772: cc.body.accept(this );
0773: }
0774: }
0775:
0776: public void visitSwitchStatement(Java.SwitchStatement ss) {
0777: write("private Java.SwitchStatement generateStatement"
0778: + getSuffix(ss)
0779: + "(Java.Block scope) throws Exception {");
0780: this .level++;
0781:
0782: write("Java.SwitchStatement statement = new Java.SwitchStatement("
0783: + getLocation(ss) + ", scope);");
0784: write("statement.setCondition(generateAtom"
0785: + getSuffix(ss.condition) + "(scope));");
0786:
0787: for (Iterator it = ss.sbsgs.iterator(); it.hasNext();) {
0788: SwitchStatement.SwitchBlockStatementGroup sbgs = (SwitchStatement.SwitchBlockStatementGroup) it
0789: .next();
0790: write("statement.addSwitchBlockStatementGroup(generateSwitchBlockStatementGroup"
0791: + getSuffix(sbgs) + "(scope));");
0792: }
0793:
0794: write("return statement;");
0795: this .level--;
0796: write("}");
0797: write();
0798:
0799: ss.condition.accept((Visitor.RvalueVisitor) this );
0800:
0801: for (Iterator it = ss.sbsgs.iterator(); it.hasNext();) {
0802: generateSwitchBlockStatementGroup((SwitchStatement.SwitchBlockStatementGroup) it
0803: .next());
0804: }
0805: }
0806:
0807: public void visitSynchronizedStatement(Java.SynchronizedStatement ss) {
0808: write("private Java.SynchronizedStatement generateStatement"
0809: + getSuffix(ss)
0810: + "(Java.BlockStatement enclosingBlockStatement) throws Exception {");
0811: this .level++;
0812:
0813: write("Java.SynchronizedStatement statement = new Java.SynchronizedStatement("
0814: + getLocation(ss)
0815: + ", enclosingBlockStatement, "
0816: + "generateAtom"
0817: + getSuffix(ss.expression)
0818: + "(enclosingBlockStatement));");
0819: write("statement.setBody(generateStatement"
0820: + getSuffix(ss.body) + "(statement));");
0821:
0822: write("return statement;");
0823: this .level--;
0824: write("}");
0825: write();
0826:
0827: //
0828: ss.expression.accept((Visitor.RvalueVisitor) this );
0829: ss.body.accept(this );
0830: }
0831:
0832: public void visitDoStatement(Java.DoStatement ds) {
0833: write("private Java.DoStatement generateStatement"
0834: + getSuffix(ds)
0835: + "(Java.Scope scope) throws Exception {");
0836: this .level++;
0837:
0838: write("Java.DoStatement statement = new Java.DoStatement("
0839: + getLocation(ds) + ", scope);");
0840: write("statement.setBody(generateStatement"
0841: + getSuffix(ds.body) + "(scope));");
0842: write("statement.setCondition(generateAtom"
0843: + getSuffix(ds.condition) + "(statement));");
0844:
0845: write("return statement;");
0846: this .level--;
0847: write("}");
0848: write();
0849:
0850: ds.body.accept(this );
0851: ds.condition.accept((Visitor.RvalueVisitor) this );
0852: }
0853:
0854: public void visitLocalVariableDeclarationStatement(
0855: Java.LocalVariableDeclarationStatement lvds) {
0856: write("private Java.LocalVariableDeclarationStatement generateStatement"
0857: + getSuffix(lvds)
0858: + "(Java.Block declaringBlock) throws Exception {");
0859: this .level++;
0860:
0861: write("return new Java.LocalVariableDeclarationStatement("
0862: + getLocation(lvds) + ", declaringBlock, "
0863: + getModifiers(lvds.modifiers) + ", " + "generateType"
0864: + getSuffix(lvds.type) + "(declaringBlock), "
0865: + "generateVariableDeclarators"
0866: + getSuffix(lvds.variableDeclarators)
0867: + "(declaringBlock, declaringBlock));");
0868:
0869: this .level--;
0870: write("}");
0871: write();
0872:
0873: lvds.type.accept((Visitor.TypeVisitor) this );
0874: generateVariableDeclarators(lvds.variableDeclarators);
0875: }
0876:
0877: public void visitReturnStatement(Java.ReturnStatement rs) {
0878: write("private Java.ReturnStatement generateStatement"
0879: + getSuffix(rs)
0880: + "(Java.BlockStatement enclosingBlockStatement) throws Exception {");
0881: this .level++;
0882: write("return new Java.ReturnStatement("
0883: + getLocation(rs)
0884: + ", enclosingBlockStatement, "
0885: + (rs.optionalReturnValue == null ? "null"
0886: : "generateAtom"
0887: + getSuffix(rs.optionalReturnValue)
0888: + "(enclosingBlockStatement)") + ");");
0889: this .level--;
0890: write("}");
0891: write();
0892:
0893: //
0894: if (rs.optionalReturnValue != null) {
0895: rs.optionalReturnValue.accept((Visitor.RvalueVisitor) this );
0896: }
0897: }
0898:
0899: public void visitThrowStatement(Java.ThrowStatement ts) {
0900: write("private Java.ThrowStatement generateStatement"
0901: + getSuffix(ts)
0902: + "(Java.BlockStatement enclosingBlockStatement) throws Exception {");
0903: this .level++;
0904: write("return new Java.ThrowStatement(" + getLocation(ts)
0905: + ", enclosingBlockStatement, " + "generateAtom"
0906: + getSuffix(ts.expression)
0907: + "(enclosingBlockStatement));");
0908: this .level--;
0909: write("}");
0910: write();
0911:
0912: ts.expression.accept((Visitor.RvalueVisitor) this );
0913: }
0914:
0915: public void visitBreakStatement(Java.BreakStatement bs) {
0916: write("private Java.BreakStatement generateStatement"
0917: + getSuffix(bs)
0918: + "(Java.Scope scope) throws Exception {");
0919: this .level++;
0920: write("return new Java.BreakStatement("
0921: + getLocation(bs)
0922: + ", scope, "
0923: + (bs.optionalLabel == null ? "null" : "\""
0924: + bs.optionalLabel + "\"") + ");");
0925: this .level--;
0926: write("}");
0927: write();
0928: }
0929:
0930: public void visitContinueStatement(Java.ContinueStatement cs) {
0931: write("private Java.ContinueStatement generateStatement"
0932: + getSuffix(cs)
0933: + "(Java.Scope scope) throws Exception {");
0934: this .level++;
0935: write("return new Java.ContinueStatement("
0936: + getLocation(cs)
0937: + ", scope, "
0938: + (cs.optionalLabel == null ? "null" : "\""
0939: + cs.optionalLabel + "\"") + ");");
0940: this .level--;
0941: write("}");
0942: write();
0943: }
0944:
0945: public void visitEmptyStatement(Java.EmptyStatement es) {
0946: write("private Java.EmptyStatement generateStatement"
0947: + getSuffix(es)
0948: + "(Java.Scope scope) throws Exception {");
0949: this .level++;
0950: write("return new Java.EmptyStatement(" + getLocation(es)
0951: + ", scope);");
0952: this .level--;
0953: write("}");
0954: write();
0955: }
0956:
0957: public void visitLocalClassDeclarationStatement(
0958: Java.LocalClassDeclarationStatement lcds) {
0959: write("private Java.LocalClassDeclarationStatement generateStatement"
0960: + getSuffix(lcds)
0961: + "(Java.Block scope) throws Exception {");
0962: this .level++;
0963: write("return new Java.LocalClassDeclarationStatement(scope, "
0964: + "generateLocalClassDeclaration" + getSuffix(lcds.lcd)
0965: + "(scope));");
0966: this .level--;
0967: write("}");
0968: write();
0969:
0970: lcds.lcd.accept(this );
0971: }
0972:
0973: public void generateVariableDeclarator(Java.VariableDeclarator vd) {
0974: write("private Java.VariableDeclarator generateVariableDeclarator"
0975: + getSuffix(vd)
0976: + "(Java.Scope scope, Java.BlockStatement enclosingBlockStatement) throws Exception {");
0977: this .level++;
0978: write("return new Java.VariableDeclarator("
0979: + getLocation(vd)
0980: + ", \""
0981: + vd.name
0982: + "\", "
0983: + vd.brackets
0984: + ", "
0985: + (vd.optionalInitializer == null ? "null"
0986: : "generateAtom"
0987: + getSuffix(vd.optionalInitializer)
0988: + "(enclosingBlockStatement)") + ");");
0989: this .level--;
0990: write("}");
0991: write();
0992:
0993: if (vd.optionalInitializer != null) {
0994: vd.optionalInitializer.accept((Visitor.RvalueVisitor) this );
0995: }
0996: }
0997:
0998: public void generateFormalParameter(
0999: FunctionDeclarator.FormalParameter fp) {
1000: write("private Java.FormalParameter generateFormalParameter"
1001: + getSuffix(fp)
1002: + "(Java.Scope scope) throws Exception {");
1003: this .level++;
1004: write("return new Java.FormalParameter(" + fp.finaL
1005: + ", generateType" + getSuffix(fp.type) + "(scope), \""
1006: + fp.name + "\");");
1007: this .level--;
1008: write("}");
1009: write();
1010:
1011: fp.type.accept((Visitor.TypeVisitor) this );
1012: }
1013:
1014: public void visitNewAnonymousClassInstance(
1015: Java.NewAnonymousClassInstance naci) {
1016: write("private Java.NewAnonymousClassInstance generateAtom"
1017: + getSuffix(naci)
1018: + "(Java.BlockStatement enclosingBlockStatement) throws Exception {");
1019: this .level++;
1020: write("return new Java.NewAnonymousClassInstance("
1021: + getLocation(naci)
1022: + ", enclosingBlockStatement, "
1023: + (naci.optionalQualification == null ? "null"
1024: : "generateAtom"
1025: + getSuffix(naci.optionalQualification)
1026: + "(enclosingBlockStatement)")
1027: + ", "
1028: + "generateLocalClassDeclaration"
1029: + getSuffix(naci.anonymousClassDeclaration)
1030: + "(enclosingBlockStatement)"
1031: + ", "
1032: + getGenerateRvalues(naci.arguments,
1033: "enclosingBlockStatement") + ");");
1034: this .level--;
1035: write("}");
1036: write();
1037:
1038: if (naci.optionalQualification != null) {
1039: naci.optionalQualification
1040: .accept((Visitor.RvalueVisitor) this );
1041: }
1042: naci.anonymousClassDeclaration.accept(this );
1043: generateRvalues(naci.arguments);
1044: }
1045:
1046: public void visitMethodInvocation(Java.MethodInvocation mi) {
1047: write("private Java.MethodInvocation generateAtom"
1048: + getSuffix(mi)
1049: + "(Java.BlockStatement enclosingBlockStatement) throws Exception {");
1050: this .level++;
1051: write("return new Java.MethodInvocation("
1052: + getLocation(mi)
1053: + ", enclosingBlockStatement, "
1054: + (mi.optionalTarget == null ? "null" : "generateAtom"
1055: + getSuffix(mi.optionalTarget)
1056: + "(enclosingBlockStatement)")
1057: + ", "
1058: + "\""
1059: + mi.methodName
1060: + "\", "
1061: + getGenerateRvalues(mi.arguments,
1062: "enclosingBlockStatement") + ");");
1063: this .level--;
1064: write("}");
1065: write();
1066:
1067: if (mi.optionalTarget != null) {
1068: mi.optionalTarget.accept(this );
1069: }
1070: generateRvalues(mi.arguments);
1071: }
1072:
1073: public void visitAlternateConstructorInvocation(
1074: Java.AlternateConstructorInvocation aci) {
1075: if (this .instances.contains(aci))
1076: return;
1077: this .instances.add(aci);
1078:
1079: write("private Java.AlternateConstructorInvocation generateConstructorInvocation"
1080: + getSuffix(aci)
1081: + "("
1082: + "Java.ClassDeclaration declaringClass, Java.ConstructorDeclarator declaringConstructor, Java.BlockStatement enclosingBlockStatement) throws Exception {");
1083: this .level++;
1084:
1085: write("return new Java.AlternateConstructorInvocation("
1086: + getLocation(aci)
1087: + ", "
1088: + "declaringClass, declaringConstructor, "
1089: + getGenerateRvalues(aci.arguments, "new Java.Block("
1090: + getLocation(aci) + ", declaringConstructor)")
1091: + ");");
1092: this .level--;
1093: write("}");
1094: write();
1095:
1096: generateRvalues(aci.arguments);
1097: }
1098:
1099: public void visitSuperConstructorInvocation(
1100: Java.SuperConstructorInvocation sci) {
1101: if (this .instances.contains(sci))
1102: return;
1103: this .instances.add(sci);
1104:
1105: write("private Java.SuperConstructorInvocation generateConstructorInvocation"
1106: + getSuffix(sci)
1107: + "("
1108: + "Java.ClassDeclaration declaringClass, Java.ConstructorDeclarator declaringConstructor, Java.BlockStatement enclosingBlockStatement) throws Exception {");
1109: this .level++;
1110:
1111: // TODO verify sci.declaringClass and sci.declaringConstructor names
1112: write("return new Java.SuperConstructorInvocation("
1113: + getLocation(sci)
1114: + ", declaringClass, declaringConstructor, "
1115: + (sci.optionalQualification == null ? "null"
1116: : "generateAtom"
1117: + getSuffix(sci.optionalQualification)
1118: + "(declaringClass)")
1119: + ", "
1120: + getGenerateRvalues(sci.arguments, "new Java.Block("
1121: + getLocation(sci) + ", declaringConstructor)")
1122: + ");");
1123:
1124: this .level--;
1125: write("}");
1126: write();
1127:
1128: if (sci.optionalQualification != null) {
1129: sci.optionalQualification
1130: .accept((Visitor.RvalueVisitor) this );
1131: }
1132: generateRvalues(sci.arguments);
1133: }
1134:
1135: public void visitNewClassInstance(Java.NewClassInstance nci) {
1136: if (this .instances.contains(nci))
1137: return;
1138: this .instances.add(nci);
1139:
1140: write("private Java.NewClassInstance generateAtom"
1141: + getSuffix(nci)
1142: + "(Java.BlockStatement enclosingBlockStatement) throws Exception {");
1143: this .level++;
1144:
1145: write("return new Java.NewClassInstance("
1146: + getLocation(nci)
1147: + ", enclosingBlockStatement, "
1148: + (nci.optionalQualification == null ? "null"
1149: : "generateAtom"
1150: + getSuffix(nci.optionalQualification)
1151: + "(enclosingBlockStatement)")
1152: + ", "
1153: + "generateType"
1154: + getSuffix(nci.type)
1155: + "(enclosingBlockStatement), "
1156: + getGenerateRvalues(nci.arguments,
1157: "enclosingBlockStatement") + ");");
1158:
1159: this .level--;
1160: write("}");
1161: write();
1162:
1163: if (nci.optionalQualification != null) {
1164: nci.optionalQualification
1165: .accept((Visitor.RvalueVisitor) this );
1166: }
1167: nci.type.accept((Visitor.TypeVisitor) this );
1168: generateRvalues(nci.arguments);
1169: }
1170:
1171: public void visitAssignment(Java.Assignment a) {
1172: if (this .instances.contains(a))
1173: return;
1174: this .instances.add(a);
1175:
1176: write("private Java.Assignment generateAtom"
1177: + getSuffix(a)
1178: + "(Java.BlockStatement enclosingBlockStatement) throws Exception {");
1179: this .level++;
1180: write("return new Java.Assignment(" + getLocation(a) + ", "
1181: + "generateAtom" + getSuffix(a.lhs)
1182: + "(enclosingBlockStatement), \"" + a.operator + "\", "
1183: + "generateAtom" + getSuffix(a.rhs)
1184: + "(enclosingBlockStatement));");
1185: this .level--;
1186: write("}");
1187: write();
1188:
1189: a.lhs.accept((Visitor.LvalueVisitor) this );
1190: a.rhs.accept((Visitor.RvalueVisitor) this );
1191: }
1192:
1193: public void visitArrayInitializer(Java.ArrayInitializer ai) {
1194: if (this .instances.contains(ai))
1195: return;
1196: this .instances.add(ai);
1197:
1198: write("private Java.ArrayInitializer generateAtom"
1199: + getSuffix(ai)
1200: + "(Java.BlockStatement enclosingBlockStatement) throws Exception {");
1201: this .level++;
1202: write("return new Java.ArrayInitializer("
1203: + getLocation(ai)
1204: + ", "
1205: + "generateType"
1206: + getSuffix(ai.arrayType)
1207: + "(enclosingBlockStatement), "
1208: +
1209: // "generateRvalues"+getSuffix(ai.values)+"(enclosingBlockStatement));");
1210: getGenerateRvalues(ai.values, "enclosingBlockStatement")
1211: + ");");
1212: this .level--;
1213: write("}");
1214: write();
1215:
1216: ai.arrayType.accept(this );
1217: generateRvalues(ai.values);
1218: }
1219:
1220: public void visitSimpleType(Java.SimpleType st) {
1221: if (this .instances.contains(st))
1222: return;
1223: this .instances.add(st);
1224:
1225: write("private Java.SimpleType generateType" + getSuffix(st)
1226: + "(Java.Scope scope) throws Exception {");
1227: this .level++;
1228: write("return new Java.SimpleType(" + getLocation(st)
1229: + ", generateIClass" + st.iClass + "(scope));");
1230: this .level--;
1231: write("}");
1232: write();
1233:
1234: generateIClass(st.iClass);
1235: }
1236:
1237: public void visitBasicType(Java.BasicType bt) {
1238: if (this .instances.contains(bt))
1239: return;
1240: this .instances.add(bt);
1241:
1242: write("private Java.BasicType generateType" + getSuffix(bt)
1243: + "(Java.Scope scope) throws Exception {");
1244: this .level++;
1245:
1246: String s = "";
1247: switch (bt.index) {
1248: case Java.BasicType.VOID:
1249: s = "Java.BasicType.VOID";
1250: break;
1251: case Java.BasicType.BYTE:
1252: s = "Java.BasicType.BYTE";
1253: break;
1254: case Java.BasicType.SHORT:
1255: s = "Java.BasicType.SHORT";
1256: break;
1257: case Java.BasicType.CHAR:
1258: s = "Java.BasicType.CHAR";
1259: break;
1260: case Java.BasicType.INT:
1261: s = "Java.BasicType.INT";
1262: break;
1263: case Java.BasicType.LONG:
1264: s = "Java.BasicType.LONG";
1265: break;
1266: case Java.BasicType.FLOAT:
1267: s = "Java.BasicType.FLOAT";
1268: break;
1269: case Java.BasicType.DOUBLE:
1270: s = "Java.BasicType.DOUBLE";
1271: break;
1272: case Java.BasicType.BOOLEAN:
1273: s = "Java.BasicType.BOOLEAN";
1274: break;
1275: }
1276: write("return new Java.BasicType(" + getLocation(bt) + ", " + s
1277: + ");");
1278: this .level--;
1279: write("}");
1280: write();
1281: }
1282:
1283: public void visitReferenceType(Java.ReferenceType rt) {
1284: if (this .instances.contains(rt))
1285: return;
1286: this .instances.add(rt);
1287:
1288: write("private Java.ReferenceType generateType" + getSuffix(rt)
1289: + "(Java.Scope scope) throws Exception {");
1290: this .level++;
1291: write("return new Java.ReferenceType(" + getLocation(rt)
1292: + ", scope, " + arrayToString(rt.identifiers) + ");");
1293: this .level--;
1294: write("}");
1295: write();
1296: }
1297:
1298: public void visitRvalueMemberType(Java.RvalueMemberType rmt) {
1299: if (this .instances.contains(rmt))
1300: return;
1301: this .instances.add(rmt);
1302:
1303: write("private Java.VariableDeclarator generateAtom"
1304: + getSuffix(rmt)
1305: + "(Java.Scope scope) throws Exception {");
1306: this .level++;
1307: write("return new Java.RvalueMemberType(" + getLocation(rmt)
1308: + ", " + "generateAtom" + getSuffix(rmt.rvalue)
1309: + "(scope), " + "\"" + rmt.identifier + "\");");
1310: this .level--;
1311: write("}");
1312: write();
1313:
1314: rmt.rvalue.accept((Visitor.RvalueVisitor) this );
1315: }
1316:
1317: public void visitArrayType(Java.ArrayType at) {
1318: if (this .instances.contains(at))
1319: return;
1320: this .instances.add(at);
1321:
1322: write("private Java.ArrayType generateType" + getSuffix(at)
1323: + "(Java.Scope scope) throws Exception {");
1324: this .level++;
1325: write("return new Java.ArrayType(generateType"
1326: + getSuffix(at.componentType) + "(scope).toType());");
1327: this .level--;
1328: write("}");
1329: write();
1330:
1331: at.componentType.accept((Visitor.TypeVisitor) this );
1332: }
1333:
1334: public void visitAmbiguousName(Java.AmbiguousName an) {
1335: if (this .instances.contains(an))
1336: return;
1337: this .instances.add(an);
1338:
1339: write("// bridge method for AmbiguousName to Type conversion");
1340: write("private Java.Type generateType" + getSuffix(an.toType())
1341: + "(Java.Scope scope) throws Exception {");
1342: this .level++;
1343: write("return generateAtom" + getSuffix(an)
1344: + "(scope).toType();");
1345: this .level--;
1346: write("}");
1347:
1348: write("private Java.AmbiguousName generateAtom" + getSuffix(an)
1349: + "(Java.Scope scope) throws Exception {");
1350: this .level++;
1351: write("return new Java.AmbiguousName(" + getLocation(an)
1352: + ", scope, " + arrayToString(an.identifiers) + ", "
1353: + an.n + ");");
1354: this .level--;
1355: write("}");
1356:
1357: write("private Java.AmbiguousName generateAtom"
1358: + getSuffix(an)
1359: + "(Java.BlockStatement enclosingBlockStatement) throws Exception {");
1360: this .level++;
1361: write("return new Java.AmbiguousName(" + getLocation(an)
1362: + ", enclosingBlockStatement, "
1363: + arrayToString(an.identifiers) + ", " + an.n + ");");
1364: this .level--;
1365: write("}");
1366: write();
1367: }
1368:
1369: public void visitPackage(Java.Package p) {
1370: if (this .instances.contains(p))
1371: return;
1372: this .instances.add(p);
1373:
1374: write("private Java.Package generatePackage" + getSuffix(p)
1375: + "() throws Exception {");
1376: this .level++;
1377: write("return new Java.Package(" + getLocation(p) + ", \""
1378: + p.name + "\");");
1379: this .level--;
1380: write("}");
1381: write();
1382: }
1383:
1384: public void visitLocalVariableAccess(Java.LocalVariableAccess lva) {
1385: if (this .instances.contains(lva))
1386: return;
1387: this .instances.add(lva);
1388:
1389: write("private Java.VariableDeclarator generateVariableDeclarator"
1390: + getSuffix(lva)
1391: + "(Java.Scope scope, Java.BlockStatement enclosingBlockStatement) throws Exception {");
1392: this .level++;
1393: Java.LocalVariable lv = lva.localVariable;
1394: write("return new Java.LocalVariableAccess(" + getLocation(lva)
1395: + ", " + "new Java.LocalVariable(" + lv.finaL + ", "
1396: + "generateIClass" + getSuffix(lv.type) + "(), "
1397: + lv.localVariableArrayIndex + "));");
1398: this .level--;
1399: write("}");
1400: write();
1401:
1402: generateIClass(lv.type);
1403: }
1404:
1405: public void visitFieldAccess(Java.FieldAccess fa) {
1406: if (this .instances.contains(fa))
1407: return;
1408: this .instances.add(fa);
1409:
1410: write("private Java.VariableDeclarator generateVariableDeclarator"
1411: + getSuffix(fa)
1412: + "(Java.Scope scope, Java.BlockStatement enclosingBlockStatement) throws Exception {");
1413: this .level++;
1414:
1415: // TODO verify retrieveal of IField instance
1416: write("Java.Atom atom = generateAtom" + getSuffix(fa.lhs)
1417: + "(scope)");
1418: write("return new Java.FieldAccess(" + getLocation(fa) + ", "
1419: + "atom, Java.findIField(atom.getType(), \""
1420: + fa.field.getName() + "\", " + getLocation(fa) + "));");
1421: this .level--;
1422: write("}");
1423: write();
1424:
1425: fa.lhs.accept(this );
1426: }
1427:
1428: public void visitArrayLength(Java.ArrayLength al) {
1429: if (this .instances.contains(al))
1430: return;
1431: this .instances.add(al);
1432:
1433: write("private Java.ArrayLength generateAtom"
1434: + getSuffix(al)
1435: + "(Java.BlockStatement enclosingBlockStatement) throws Exception {");
1436: this .level++;
1437: write("return new Java.ArrayAccessExpression("
1438: + getLocation(al) + ", generateAtom"
1439: + getSuffix(al.lhs) + "(enclosingBlockStatement));");
1440: this .level--;
1441: write("}");
1442: write();
1443:
1444: al.lhs.accept((Visitor.RvalueVisitor) this );
1445: }
1446:
1447: public void visitThisReference(Java.ThisReference tr) {
1448: if (this .instances.contains(tr))
1449: return;
1450: this .instances.add(tr);
1451:
1452: write("private Java.ThisReference generateAtom"
1453: + getSuffix(tr)
1454: + "(Java.BlockStatement enclosingBlockStatement) throws Exception {");
1455: this .level++;
1456: write("return new Java.ThisReference(" + getLocation(tr)
1457: + ", enclosingBlockStatement);");
1458: this .level--;
1459: write("}");
1460: write();
1461: }
1462:
1463: public void visitQualifiedThisReference(
1464: Java.QualifiedThisReference qtr) {
1465: if (this .instances.contains(qtr))
1466: return;
1467: this .instances.add(qtr);
1468:
1469: write("private Java.QualifiedThisReference generateAtom"
1470: + getSuffix(qtr)
1471: + "(Java.BlockStatement enclosingBlockStatement) throws Exception {");
1472: this .level++;
1473: write("return new Java.QualifiedThisReference("
1474: + getLocation(qtr) + ", enclosingBlockStatement, "
1475: + "generateType" + getSuffix(qtr.qualification)
1476: + "(enclosingBlockStatement).toType()" + ");");
1477: this .level--;
1478: write("}");
1479: write();
1480:
1481: qtr.qualification.accept((Visitor.TypeVisitor) this );
1482: }
1483:
1484: public void visitClassLiteral(Java.ClassLiteral cl) {
1485: if (this .instances.contains(cl))
1486: return;
1487: this .instances.add(cl);
1488:
1489: write("private Java.ClassLiteral generateAtom"
1490: + getSuffix(cl)
1491: + "(Java.BlockStatement enclosingBlockStatement) throws Exception {");
1492: this .level++;
1493: write("return new Java.ClassLiteral(" + getLocation(cl)
1494: + ", enclosingBlockStatement, " + "generateType"
1495: + getSuffix(cl.type)
1496: + "(enclosingBlockStatement).toType());");
1497: this .level--;
1498: write("}");
1499: write();
1500:
1501: cl.type.accept((Visitor.TypeVisitor) this );
1502: }
1503:
1504: public void visitConditionalExpression(Java.ConditionalExpression ce) {
1505: if (this .instances.contains(ce))
1506: return;
1507: this .instances.add(ce);
1508:
1509: write("private Java.ConditionalExpression generateAtom"
1510: + getSuffix(ce)
1511: + "(Java.BlockStatement enclosingBlockStatement) throws Exception {");
1512: this .level++;
1513: write("return new Java.ConditionalExpression("
1514: + getLocation(ce) + ", " + "generateAtom"
1515: + getSuffix(ce.lhs) + "(enclosingBlockStatement), "
1516: + "generateAtom" + getSuffix(ce.mhs)
1517: + "(enclosingBlockStatement), " + "generateAtom"
1518: + getSuffix(ce.rhs) + "(enclosingBlockStatement));");
1519: this .level--;
1520: write("}");
1521: write();
1522:
1523: ce.lhs.accept((Visitor.RvalueVisitor) this );
1524: ce.mhs.accept((Visitor.RvalueVisitor) this );
1525: ce.rhs.accept((Visitor.RvalueVisitor) this );
1526: }
1527:
1528: public void visitCrement(Java.Crement c) {
1529: if (this .instances.contains(c))
1530: return;
1531: this .instances.add(c);
1532:
1533: write("private Java.Crement generateAtom"
1534: + getSuffix(c)
1535: + "(Java.BlockStatement enclosingBlockStatement) throws Exception {");
1536: this .level++;
1537: if (c.pre) {
1538: write("return new Java.Crement(" + getLocation(c) + ", \""
1539: + c.operator + "\", " + "generateAtom"
1540: + getSuffix(c.operand)
1541: + "(enclosingBlockStatement)" + ");");
1542: } else {
1543: write("return new Java.Crement(" + getLocation(c) + ", "
1544: + "generateAtom" + getSuffix(c.operand)
1545: + "(enclosingBlockStatement), " + "\"" + c.operator
1546: + "\");");
1547: }
1548: this .level--;
1549: write("}");
1550: write();
1551:
1552: c.operand.accept((Visitor.LvalueVisitor) this );
1553: }
1554:
1555: public void visitArrayAccessExpression(
1556: Java.ArrayAccessExpression aae) {
1557: if (this .instances.contains(aae))
1558: return;
1559: this .instances.add(aae);
1560:
1561: write("private Java.ArrayAccessExpression generateAtom"
1562: + getSuffix(aae)
1563: + "(Java.BlockStatement enclosingBlockStatement) throws Exception {");
1564: this .level++;
1565:
1566: write("return new Java.ArrayAccessExpression("
1567: + getLocation(aae) + ", " + "generateAtom"
1568: + getSuffix(aae.lhs) + "(enclosingBlockStatement), "
1569: + "generateAtom" + getSuffix(aae.index)
1570: + "(enclosingBlockStatement)" + ");");
1571:
1572: this .level--;
1573: write("}");
1574: write();
1575:
1576: aae.lhs.accept((Visitor.RvalueVisitor) this );
1577: aae.index.accept((Visitor.RvalueVisitor) this );
1578: }
1579:
1580: public void visitFieldAccessExpression(
1581: Java.FieldAccessExpression fae) {
1582: if (this .instances.contains(fae))
1583: return;
1584: this .instances.add(fae);
1585:
1586: write("private Java.FieldAccessExpression generateAtom"
1587: + getSuffix(fae)
1588: + "(Java.BlockStatement enclosingBlockStatement) throws Exception {");
1589: this .level++;
1590: write("return new Java.FieldAccessExpression("
1591: + getLocation(fae) + ", " + "enclosingBlockStatement, "
1592: + "generateAtom" + getSuffix(fae.lhs)
1593: + "(enclosingBlockStatement), " + "\"" + fae.fieldName
1594: + "\");");
1595: this .level--;
1596: write("}");
1597: write();
1598:
1599: fae.lhs.accept(this );
1600: }
1601:
1602: public void visitUnaryOperation(Java.UnaryOperation uo) {
1603: if (this .instances.contains(uo))
1604: return;
1605: this .instances.add(uo);
1606:
1607: write("private Java.UnaryOperation generateAtom"
1608: + getSuffix(uo)
1609: + "(Java.BlockStatement enclosingBlockStatement) throws Exception {");
1610: this .level++;
1611: write("return new Java.UnaryOperation(" + getLocation(uo)
1612: + ", \"" + uo.operator + "\", " + "generateAtom"
1613: + getSuffix(uo.operand) + "(enclosingBlockStatement));");
1614: this .level--;
1615: write("}");
1616: write();
1617:
1618: uo.operand.accept((Visitor.RvalueVisitor) this );
1619: }
1620:
1621: public void visitInstanceof(Java.Instanceof io) {
1622: if (this .instances.contains(io))
1623: return;
1624: this .instances.add(io);
1625:
1626: write("private Java.Instanceof generateAtom"
1627: + getSuffix(io)
1628: + "(Java.BlockStatement enclosingBlockStatement) throws Exception {");
1629: this .level++;
1630: write("return new Java.Instanceof(" + getLocation(io) + ", "
1631: + "generateAtom" + getSuffix(io.lhs)
1632: + "(enclosingBlockStatement), " + "generateType"
1633: + getSuffix(io.rhs) + "(enclosingBlockStatement));");
1634: this .level--;
1635: write("}");
1636: write();
1637:
1638: io.lhs.accept((Visitor.RvalueVisitor) this );
1639: io.rhs.accept((Visitor.TypeVisitor) this );
1640: }
1641:
1642: public void visitBinaryOperation(Java.BinaryOperation bo) {
1643: if (this .instances.contains(bo))
1644: return;
1645: this .instances.add(bo);
1646:
1647: write("private Java.BinaryOperation generateAtom"
1648: + getSuffix(bo)
1649: + "(Java.BlockStatement enclosingBlockStatement) throws Exception {");
1650: this .level++;
1651: write("return new Java.BinaryOperation(" + getLocation(bo)
1652: + ", " + "generateAtom" + getSuffix(bo.lhs)
1653: + "(enclosingBlockStatement), " + "\"" + bo.op + "\", "
1654: + "generateAtom" + getSuffix(bo.rhs)
1655: + "(enclosingBlockStatement)" + ");");
1656: this .level--;
1657: write("}");
1658: write();
1659:
1660: bo.lhs.accept((Visitor.RvalueVisitor) this );
1661: bo.rhs.accept((Visitor.RvalueVisitor) this );
1662: }
1663:
1664: public void visitCast(Java.Cast c) {
1665: if (this .instances.contains(c))
1666: return;
1667: this .instances.add(c);
1668:
1669: write("private Java.Cast generateAtom"
1670: + getSuffix(c)
1671: + "(Java.BlockStatement enclosingBlockStatement) throws Exception {");
1672: this .level++;
1673: write("return new Java.Cast(" + getLocation(c) + ", "
1674: + "generateType" + getSuffix(c.targetType)
1675: + "(enclosingBlockStatement).toType(), "
1676: + "generateAtom" + getSuffix(c.value)
1677: + "(enclosingBlockStatement));");
1678: this .level--;
1679: write("}");
1680: write();
1681:
1682: c.targetType.accept((Visitor.TypeVisitor) this );
1683: c.value.accept((Visitor.RvalueVisitor) this );
1684: }
1685:
1686: public void visitSuperclassMethodInvocation(
1687: Java.SuperclassMethodInvocation smi) {
1688: if (this .instances.contains(smi))
1689: return;
1690: this .instances.add(smi);
1691:
1692: write("private Java.SuperclassMethodInvocation generateAtom"
1693: + getSuffix(smi)
1694: + "(Java.BlockStatement enclosingBlockStatement) throws Exception {");
1695: this .level++;
1696: write("return new Java.SuperclassMethodInvocation("
1697: + getLocation(smi)
1698: + ", enclosingBlockStatement, \""
1699: + smi.methodName
1700: + "\", "
1701: + getGenerateRvalues(smi.arguments,
1702: "enclosingBlockStatement") + ");");
1703: this .level--;
1704: write("}");
1705: write();
1706:
1707: generateRvalues(smi.arguments);
1708: }
1709:
1710: public void visitParameterAccess(Java.ParameterAccess pa) {
1711: if (this .instances.contains(pa))
1712: return;
1713: this .instances.add(pa);
1714:
1715: write("private Java.ParameterAccess generateAtom"
1716: + getSuffix(pa)
1717: + "(Java.BlockStatement enclosingBlockStatement) throws Exception {");
1718: this .level++;
1719: // TODO resolve a correct functionDeclarator name
1720: Java.FunctionDeclarator d = pa.declaringFunction;
1721: write("final Java.FunctionDeclarator declarator = null; // TODO "
1722: + d.getClass().getName() + " : " + d.toString());
1723: write("return new Java.ParameterAccess(" + getLocation(pa)
1724: + ", declarator, \"" + pa.name + "\");");
1725: this .level--;
1726: write("}");
1727: write();
1728: }
1729:
1730: public void visitNewArray(Java.NewArray na) {
1731: if (this .instances.contains(na))
1732: return;
1733: this .instances.add(na);
1734:
1735: write("private Java.NewArray generateAtom"
1736: + getSuffix(na)
1737: + "(Java.BlockStatement enclosingBlockStatement) throws Exception {");
1738: this .level++;
1739: write("return new Java.NewArray("
1740: + getLocation(na)
1741: + ", "
1742: + "generateType"
1743: + getSuffix(na.type)
1744: + "(enclosingBlockStatement), "
1745: + getGenerateRvalues(na.dimExprs,
1746: "enclosingBlockStatement") + ", " + na.dims
1747: + ");");
1748: this .level--;
1749: write("}");
1750: write();
1751:
1752: na.type.accept((Visitor.TypeVisitor) this );
1753: generateRvalues(na.dimExprs);
1754: }
1755:
1756: public void visitLiteral(Java.Literal l) {
1757: if (this .instances.contains(l))
1758: return;
1759: this .instances.add(l);
1760:
1761: write("private Java.Literal generateAtom"
1762: + getSuffix(l)
1763: + "(Java.BlockStatement enclosingBlockStatement) throws Exception {");
1764: this .level++;
1765:
1766: String s = "null";
1767: Object v = l.value;
1768: if (v instanceof String) {
1769: s = "\"" + escape((String) v) + "\"";
1770: } else if (v instanceof Integer) {
1771: s = "new Integer(" + v + ")";
1772: } else if (v instanceof Long) {
1773: s = "new Long(" + v + "L)";
1774: } else if (v instanceof Float) {
1775: s = "new Float(" + v + "f)";
1776: } else if (v instanceof Double) {
1777: s = "new Double(" + v + "d)";
1778: } else if (v instanceof Character) {
1779: s = "new Character(\'"
1780: + escape(((Character) v).charValue()) + "\')";
1781: } else if (v instanceof Boolean) {
1782: s = ((Boolean) v).booleanValue() ? "Boolean.TRUE"
1783: : "Boolean.FALSE";
1784: }
1785:
1786: write("return new Java.Literal(" + getLocation(l) + ", " + s
1787: + ");");
1788: this .level--;
1789: write("}");
1790: write();
1791: }
1792:
1793: public void visitConstantValue(Java.ConstantValue cv) {
1794: if (this .instances.contains(cv))
1795: return;
1796: this .instances.add(cv);
1797:
1798: write("private Java.ConstantValue generateAtom"
1799: + getSuffix(cv)
1800: + "(Java.BlockStatement enclosingBlockStatement) throws Exception {");
1801: this .level++;
1802:
1803: String s = "";
1804: Object v = cv.constantValue;
1805: if (v instanceof Integer) {
1806: s = "new Integer(" + v + ")";
1807: } else if (v instanceof Long) {
1808: s = "new Long(" + v + ")";
1809: } else if (v instanceof Float) {
1810: s = "new Float(" + v + "f)";
1811: } else if (v instanceof Double) {
1812: s = "new Double(" + v + "d)";
1813: } else if (v instanceof String) {
1814: s = "\"" + v + "\"";
1815: } else if (v instanceof Character) {
1816: s = "new Character(" + v + ")";
1817: } else if (v instanceof Boolean) {
1818: s = "new Boolean(" + v + ")";
1819: } else if (v == Java.Rvalue.CONSTANT_VALUE_NULL) {
1820: s = "null";
1821: }
1822:
1823: write("return new Java.ConstantValue(" + getLocation(cv) + ", "
1824: + s + ");");
1825: this .level--;
1826: write("}");
1827: write();
1828: }
1829:
1830: public void visitParenthesizedExpression(
1831: Java.ParenthesizedExpression pe) {
1832: if (this .instances.contains(pe))
1833: return;
1834: this .instances.add(pe);
1835:
1836: write("private Java.ParenthesizedExpression generateAtom"
1837: + getSuffix(pe)
1838: + "(Java.BlockStatement enclosingBlockStatement) throws Exception {");
1839: this .level++;
1840: write("return new Java.ParenthesizedExpression("
1841: + getLocation(pe) + ", " + "generateAtom"
1842: + getSuffix(pe.value) + "(enclosingBlockStatement));");
1843: this .level--;
1844: write("}");
1845: write();
1846:
1847: pe.value.accept((Visitor.RvalueVisitor) this );
1848: }
1849:
1850: // Helpers
1851:
1852: private void generateClassDeclarationBody(Java.ClassDeclaration cd) {
1853: for (Iterator it = cd.constructors.iterator(); it.hasNext();) {
1854: Java.ConstructorDeclarator cde = (Java.ConstructorDeclarator) it
1855: .next();
1856: write("declaration.addConstructor(generateConstructorDeclarator"
1857: + getSuffix(cde) + "(declaration));");
1858: }
1859:
1860: generateAbstractTypeDeclarationBody(cd);
1861:
1862: for (Iterator it = cd.variableDeclaratorsAndInitializers
1863: .iterator(); it.hasNext();) {
1864: Java.TypeBodyDeclaration tbd = (Java.TypeBodyDeclaration) it
1865: .next();
1866: write("declaration.addVariableDeclaratorOrInitializer(generateFieldDeclaration"
1867: + getSuffix(tbd) + "(declaration));");
1868: }
1869: }
1870:
1871: private void generateClassDeclarationBodyMethods(
1872: Java.ClassDeclaration cd) {
1873: for (Iterator it = cd.constructors.iterator(); it.hasNext();) {
1874: ((Java.ConstructorDeclarator) it.next()).accept(this );
1875: }
1876: for (Iterator it = cd.variableDeclaratorsAndInitializers
1877: .iterator(); it.hasNext();) {
1878: ((Java.TypeBodyDeclaration) it.next()).accept(this );
1879: }
1880:
1881: generateAbstractTypeDeclarationBodyMethods(cd);
1882: }
1883:
1884: private void generateAbstractTypeDeclarationBody(
1885: Java.AbstractTypeDeclaration atd) {
1886: for (Iterator it = atd.declaredMethods.iterator(); it.hasNext();) {
1887: Java.MethodDeclarator md = (Java.MethodDeclarator) it
1888: .next();
1889: write("declaration.addDeclaredMethod(generateMethodDeclarator"
1890: + getSuffix(md) + "(declaration));");
1891: }
1892:
1893: for (Iterator it = atd.declaredClassesAndInterfaces.iterator(); it
1894: .hasNext();) {
1895: Java.MemberTypeDeclaration mtd = (Java.MemberTypeDeclaration) it
1896: .next();
1897: write("declaration.addMemberTypeDeclaration(generateMemberTypeDeclaration"
1898: + getSuffix(mtd) + "(declaration));");
1899: }
1900: }
1901:
1902: private void generateAbstractTypeDeclarationBodyMethods(
1903: Java.AbstractTypeDeclaration atd) {
1904: for (Iterator it = atd.declaredMethods.iterator(); it.hasNext();) {
1905: ((Java.MethodDeclarator) it.next()).accept(this );
1906: }
1907: for (Iterator it = atd.declaredClassesAndInterfaces.iterator(); it
1908: .hasNext();) {
1909: ((Java.MemberTypeDeclaration) it.next())
1910: .accept((Visitor.TypeBodyDeclarationVisitor) this );
1911: }
1912: }
1913:
1914: private void generateTypes(Java.Type[] types) {
1915: if (types == null || types.length == 0)
1916: return;
1917:
1918: write("private Java.Type[] generateTypes" + getSuffix(types)
1919: + "(Java.Scope scope) throws Exception {");
1920: this .level++;
1921: write("return new Java.Type[] {");
1922: this .level++;
1923: this .level++;
1924: for (int i = 0; i < types.length; i++) {
1925: write("generateType" + getSuffix(types[i]) + "(scope),");
1926: }
1927: this .level--;
1928: write("};");
1929: this .level--;
1930: this .level--;
1931: write("}");
1932: write();
1933:
1934: // generate methods
1935: for (int i = 0; i < types.length; i++) {
1936: types[i].accept((Visitor.TypeVisitor) this );
1937: }
1938: }
1939:
1940: private void generateFormalParameters(
1941: FunctionDeclarator.FormalParameter[] parameters) {
1942: if (parameters == null || parameters.length == 0)
1943: return;
1944:
1945: write("private Java.FormalParameter[] generateFormalParameters"
1946: + getSuffix(parameters)
1947: + "(Java.Scope scope) throws Exception {");
1948: this .level++;
1949:
1950: write("return new Java.FormalParameter[] {");
1951: this .level++;
1952: this .level++;
1953: for (int i = 0; i < parameters.length; i++) {
1954: write("generateFormalParameter" + getSuffix(parameters[i])
1955: + "(scope),");
1956: }
1957: this .level--;
1958: write("};");
1959: this .level--;
1960: this .level--;
1961: write("}");
1962: write();
1963:
1964: for (int i = 0; i < parameters.length; i++) {
1965: this .generateFormalParameter(parameters[i]);
1966: }
1967: }
1968:
1969: private void generateVariableDeclarators(
1970: Java.VariableDeclarator[] variables) {
1971: if (variables == null)
1972: return;
1973:
1974: write("private Java.VariableDeclarator[] generateVariableDeclarators"
1975: + getSuffix(variables)
1976: + "(Java.Scope scope, Java.BlockStatement enclosingBlockStatement) throws Exception {");
1977: this .level++;
1978:
1979: write("Java.VariableDeclarator[] variables = new Java.VariableDeclarator["
1980: + variables.length + "];");
1981: for (int i = 0; i < variables.length; ++i) {
1982: write("variables[" + i + "] = generateVariableDeclarator"
1983: + getSuffix(variables[i])
1984: + "(scope, enclosingBlockStatement);");
1985: }
1986:
1987: write("return variables;");
1988: this .level--;
1989: write("}");
1990: write();
1991:
1992: for (int i = 0; i < variables.length; ++i) {
1993: this .generateVariableDeclarator(variables[i]);
1994: }
1995: }
1996:
1997: private void generateSwitchBlockStatementGroup(
1998: SwitchStatement.SwitchBlockStatementGroup sbsg) {
1999: write("private Java.SwitchBlockStatementGroup generateSwitchBlockStatementGroup"
2000: + getSuffix(sbsg)
2001: + "(Java.Block statement) throws Exception {");
2002: this .level++;
2003:
2004: write("Java.SwitchBlockStatementGroup group = new Java.SwitchBlockStatementGroup("
2005: + getLocation(sbsg) + ");");
2006:
2007: for (Iterator it = sbsg.caseLabels.iterator(); it.hasNext();) {
2008: Java.Rvalue rv = (Java.Rvalue) it.next();
2009: write("group.addSwitchLabel(generateAtom" + getSuffix(rv)
2010: + "(statement));");
2011: }
2012:
2013: if (sbsg.hasDefaultLabel) {
2014: write("group.hasDefaultLabel = " + sbsg.hasDefaultLabel
2015: + ";");
2016: }
2017:
2018: write("List blockStatements = new ArrayList();");
2019: for (Iterator it = sbsg.blockStatements.iterator(); it
2020: .hasNext();) {
2021: Java.BlockStatement bs = (Java.BlockStatement) it.next();
2022: write("blockStatements.add(generateStatement"
2023: + getSuffix(bs) + "(statement));");
2024: }
2025: write("group.setBlockStatements(blockStatements);");
2026:
2027: write("return group;");
2028: this .level--;
2029: write("}");
2030: write();
2031:
2032: //
2033: for (Iterator it = sbsg.caseLabels.iterator(); it.hasNext();) {
2034: ((Java.Rvalue) it.next())
2035: .accept((Visitor.RvalueVisitor) this );
2036: }
2037: for (Iterator it = sbsg.blockStatements.iterator(); it
2038: .hasNext();) {
2039: ((Java.BlockStatement) it.next()).accept(this );
2040: }
2041: }
2042:
2043: private void generateRvalues(Java.Rvalue[] values) {
2044: if (values == null || values.length == 0)
2045: return;
2046:
2047: write("private Java.Rvalue[] generateRvalues"
2048: + getSuffix(values)
2049: + "(Java.BlockStatement enclosingBlockStatement) throws Exception {");
2050: this .level++;
2051:
2052: write("return new Java.Rvalue[] {");
2053: this .level++;
2054: this .level++;
2055: for (int i = 0; i < values.length; i++) {
2056: write("generateAtom" + getSuffix(values[i])
2057: + "(enclosingBlockStatement),");
2058: }
2059: this .level--;
2060:
2061: write("};");
2062: this .level--;
2063: this .level--;
2064: write("}");
2065: write();
2066:
2067: for (int i = 0; i < values.length; i++) {
2068: values[i].accept((Visitor.RvalueVisitor) this );
2069: }
2070: }
2071:
2072: private void generateIClass(IClass type) {
2073: write("private IClass generateIClass" + getSuffix(type)
2074: + "() throws Exception {");
2075: this .level++;
2076: // TODO implement generation of iclass instance
2077: write("return null;");
2078: this .level--;
2079: write("}");
2080: write();
2081: }
2082:
2083: private static final short[] MODS = { Mod.PUBLIC, Mod.PRIVATE,
2084: Mod.PROTECTED, Mod.STATIC, Mod.FINAL, Mod.SUPER,
2085: Mod.SYNCHRONIZED, Mod.VOLATILE, Mod.TRANSIENT, Mod.NATIVE,
2086: Mod.INTERFACE, Mod.ABSTRACT, Mod.STRICTFP };
2087:
2088: private static final String[] MOD_NAMES = { "Mod.PUBLIC",
2089: "Mod.PRIVATE", "Mod.PROTECTED", "Mod.STATIC", "Mod.FINAL",
2090: "Mod.SUPER", "Mod.SYNCHRONIZED", "Mod.VOLATILE",
2091: "Mod.TRANSIENT", "Mod.NATIVE", "Mod.INTERFACE",
2092: "Mod.ABSTRACT", "Mod.STRICTFP" };
2093:
2094: private String getModifiers(short modifiers) {
2095: if (modifiers == 0) {
2096: return "Mod.NONE";
2097: }
2098:
2099: StringBuffer sb = new StringBuffer("(short)(");
2100: String sep = "";
2101: for (int i = 0; i < MODS.length; i++) {
2102: if ((modifiers & MODS[i]) > 0) {
2103: sb.append(sep).append(MOD_NAMES[i]);
2104: sep = " | ";
2105: }
2106: }
2107: sb.append(")");
2108: return sb.toString();
2109: }
2110:
2111: private String arrayToString(String[] a) {
2112: StringBuffer sb = new StringBuffer("new String[] { ");
2113: String sep = "";
2114: for (int i = 0; i < a.length; i++) {
2115: sb.append(sep).append("\"" + a[i] + "\"");
2116: sep = ", ";
2117: }
2118: return sb.append("}").toString();
2119: }
2120:
2121: private String getLocation(Java.Locatable locatable) {
2122: Location l = locatable.getLocation();
2123: // return "new Scanner.Location(\""+l.getFileName()+"\",
2124: // (short)"+l.getLineNumber()+", (short)"+l.getColumnNumber()+")";
2125: return "getLocation(" + l.getLineNumber() + ", "
2126: + l.getColumnNumber() + ")";
2127: }
2128:
2129: private void write(String s) {
2130: int n = this .level * TAB_SIZE;
2131: while (n >= TAB_FILLER.length()) {
2132: this .pw.print(TAB_FILLER);
2133: n -= TAB_FILLER.length();
2134: }
2135: if (n > 0) {
2136: this .pw.print(TAB_FILLER.substring(0, n));
2137: }
2138:
2139: this .pw.println(s);
2140: }
2141:
2142: private void write() {
2143: this .pw.println();
2144: }
2145:
2146: private String escape(String s) {
2147: StringBuffer sb = new StringBuffer();
2148: for (int i = 0; i < s.length(); i++) {
2149: sb.append(escape(s.charAt(i)));
2150: }
2151: return sb.toString();
2152: }
2153:
2154: private String escape(char c) {
2155: switch (c) {
2156: case '\"':
2157: return "\\\"";
2158: case '\'':
2159: return "\\\'";
2160: case '\n':
2161: return "\\n";
2162: case '\r':
2163: return "\\r";
2164: case '\t':
2165: return "\\t";
2166: case '\\':
2167: return "\\\\";
2168: // TODO add the rest of escapes
2169: // TODO escape unicodes
2170: default:
2171: return String.valueOf(c);
2172: }
2173: }
2174:
2175: private String getSuffix(Object o) {
2176: return String.valueOf(System.identityHashCode(o));
2177: }
2178:
2179: private String getGenerateTypes(Java.Type[] types, String scope) {
2180: return (types == null ? "null"
2181: : (types.length == 0 ? "new Java.Type[0]"
2182: : "generateTypes" + getSuffix(types) + "("
2183: + scope + ")"));
2184: }
2185:
2186: private String getGenerateRvalues(Java.Rvalue[] values, String scope) {
2187: return (values == null ? "null"
2188: : (values.length == 0 ? "new Java.Rvalue[0]"
2189: : "generateRvalues" + getSuffix(values) + "("
2190: + scope + ")"));
2191: }
2192:
2193: private String printStringLiteral(String s) {
2194: if (s == null)
2195: return "null";
2196: StringBuffer sb = new StringBuffer("\"");
2197: for (int i = 0; i < s.length(); ++i) {
2198: char c = s.charAt(i);
2199: int idx = "\r\n\t\\\"\b".indexOf(c);
2200: if (idx != -1) {
2201: sb.append('\\').append("rnt\\\"b".charAt(idx));
2202: } else {
2203: sb.append(c);
2204: }
2205: }
2206: sb.append('"');
2207: return sb.toString();
2208: }
2209: }
|