AS3: Better semicolon insertion

This commit is contained in:
Jindra Petk
2012-12-20 21:02:11 +01:00
parent 3d775b283b
commit d7843ea832
31 changed files with 104 additions and 39 deletions

View File

@@ -533,20 +533,20 @@ public class ABC {
return null;
}
private String addTabs(String s, int tabs) {
private String addTabs(String s, int tabs) {
String parts[] = s.split("\r\n");
if (!s.contains("\r\n")) {
parts = s.split("\n");
}
}
String ret = "";
for (int i = 0; i < parts.length; i++) {
for (int t = 0; t < tabs; t++) {
ret += IDENT_STRING;
}
}
ret += parts[i];
if (i < parts.length - 1) {
ret += "\r\n";
}
}
}
return ret;
}

View File

@@ -833,7 +833,7 @@ public class AVM2Code {
String ret = "";
for (int d = 0; d < stack.size(); d++) {
TreeItem o = stack.get(d);
ret += o.toString(constants) + "\r\n";
ret += o.toStringSemicoloned(constants) + "\r\n";
}
return ret;
}
@@ -1172,7 +1172,7 @@ public HashMap<Integer,String> getLocalRegNamesFromDebug(ABC abc){
throw new UnknownJumpException(stack, ip, output);
}
AVM2Instruction ins = code.get(ip);
//Ify s vice podminkama
//Ifs with multiple conditions
if (ins.definition instanceof JumpIns) {
if (ins.operands[0] == 0) {
ip++;
@@ -1468,6 +1468,12 @@ public HashMap<Integer,String> getLocalRegNamesFromDebug(ABC abc){
if (ip - 1 >= start) {
insBefore = code.get(ip - 1);
}
if (insAfter.definition instanceof ConvertBIns) { //SWF compiled with debug contain convert_b
ip++;
addr=pos2adr(ip);
insAfter = code.get(ip + 1);
}
boolean isAnd = false;
if (insAfter.definition instanceof IfFalseIns) {
//stack.add("(" + stack.pop() + ")&&");
@@ -1847,6 +1853,12 @@ public HashMap<Integer,String> getLocalRegNamesFromDebug(ABC abc){
} catch (Exception ex) {
}
for (int p = 0; p < parts.length; p++) {
if(p==parts.length-1){
if(parts[p].equals(""))
{
continue;
}
}
String strippedP = Highlighting.stripHilights(parts[p]);
if (strippedP.endsWith(":") && (!strippedP.startsWith("case ")) && (!strippedP.equals("default:"))) {
String loopname = strippedP.substring(0, strippedP.length() - 1);
@@ -1880,7 +1892,7 @@ public HashMap<Integer,String> getLocalRegNamesFromDebug(ABC abc){
level++;
sub += tabString(level) + parts[p] + "\r\n";
level++;
} else if (strippedP.equals("}")) {
} else if (strippedP.equals("}")||strippedP.equals("};")) {
level--;
sub += tabString(level) + parts[p] + "\r\n";
level--;

View File

@@ -37,7 +37,7 @@ public class BreakTreeItem extends TreeItem {
@Override
public String toString(ConstantPool constants) {
return hilight("break") + " loop" + loopPos + ";";
return hilight("break") + " loop" + loopPos;
}
}

View File

@@ -46,7 +46,7 @@ public class CallPropertyTreeItem extends TreeItem {
}
args = args + arguments.get(a).toString(constants);
}
return formatProperty(constants, receiver, propertyName) + hilight("(") + args + hilight(")") + (isVoid ? ";" : "");
return formatProperty(constants, receiver, propertyName) + hilight("(") + args + hilight(")");
}

View File

@@ -49,7 +49,7 @@ public class CallSuperTreeItem extends TreeItem {
}
String calee = receiver.toString(constants) + ".";
if (Highlighting.stripHilights(calee).equals("this.")) calee = "";
return calee + hilight("super.") + multiname.toString(constants) + hilight("(") + args + hilight(")") + (isVoid ? ";" : "");
return calee + hilight("super.") + multiname.toString(constants) + hilight("(") + args + hilight(")");
}

View File

@@ -45,7 +45,7 @@ public class ConstructSuperTreeItem extends TreeItem {
}
String calee = object.toString(constants) + ".";
if (Highlighting.stripHilights(calee).equals("this.")) calee = "";
return calee + hilight("super(") + argStr + hilight(");");
return calee + hilight("super(") + argStr + hilight(")");
}

View File

@@ -37,7 +37,7 @@ public class ContinueTreeItem extends TreeItem {
@Override
public String toString(ConstantPool constants) {
return hilight("continue") + " " + (isKnown ? "loop" : "unk") + loopPos + ";";
return hilight("continue") + " " + (isKnown ? "loop" : "unk") + loopPos;
}
}

View File

@@ -32,7 +32,7 @@ public class DecLocalTreeItem extends TreeItem {
@Override
public String toString(ConstantPool constants) {
return InstructionDefinition.localRegName(regIndex) + hilight("--") + ";";
return InstructionDefinition.localRegName(regIndex) + hilight("--");
}

View File

@@ -32,7 +32,7 @@ public class IncLocalTreeItem extends TreeItem {
@Override
public String toString(ConstantPool constants) {
return InstructionDefinition.localRegName(regIndex) + hilight("++") + ";";
return InstructionDefinition.localRegName(regIndex) + hilight("++");
}

View File

@@ -35,7 +35,7 @@ public class InitPropertyTreeItem extends TreeItem implements SetTypeTreeItem{
@Override
public String toString(ConstantPool constants) {
return formatProperty(constants, object, propertyName) + hilight("=") + value.toString(constants) + ";";
return formatProperty(constants, object, propertyName) + hilight("=") + value.toString(constants);
}
public TreeItem getObject() {

View File

@@ -35,7 +35,7 @@ public class NewFunctionTreeItem extends TreeItem {
@Override
public String toString(ConstantPool constants) {
return hilight("new function(" + paramStr + "):" + returnStr + "\r\n{\r\n") + functionBody + hilight("}\r\n");
return hilight("new function(" + paramStr + "):" + returnStr + "\r\n{\r\n") + functionBody +"\r\n"+ hilight("}");
}

View File

@@ -31,7 +31,7 @@ public class ReturnValueTreeItem extends TreeItem {
@Override
public String toString(ConstantPool constants) {
return hilight("return ") + value.toString(constants) + ";";
return hilight("return ") + value.toString(constants);
}

View File

@@ -29,7 +29,7 @@ public class ReturnVoidTreeItem extends TreeItem {
@Override
public String toString(ConstantPool constants) {
return hilight("return") + ";";
return hilight("return");
}

View File

@@ -33,7 +33,7 @@ public class SetGlobalSlotTreeItem extends TreeItem {
@Override
public String toString(ConstantPool constants) {
return hilight("setglobalslot(" + slotId + ",") + value.toString(constants) + hilight(")") + ";";
return hilight("setglobalslot(" + slotId + ",") + value.toString(constants) + hilight(")");
}

View File

@@ -34,7 +34,7 @@ public class SetLocalTreeItem extends TreeItem implements SetTypeTreeItem {
@Override
public String toString(ConstantPool constants) {
return hilight(InstructionDefinition.localRegName(regIndex) + "=") + value.toString(constants) + ";";
return hilight(InstructionDefinition.localRegName(regIndex) + "=") + value.toString(constants);
}
public TreeItem getObject() {

View File

@@ -35,7 +35,7 @@ public class SetPropertyTreeItem extends TreeItem implements SetTypeTreeItem{
@Override
public String toString(ConstantPool constants) {
return formatProperty(constants, object, propertyName) + hilight("=") + value.toString(constants) + ";";
return formatProperty(constants, object, propertyName) + hilight("=") + value.toString(constants);
}
public TreeItem getObject() {

View File

@@ -48,7 +48,7 @@ public class SetSlotTreeItem extends TreeItem implements SetTypeTreeItem {
}
}
}
return ret + hilight(slotName.getName(constants)) + hilight("=") + value.toString(constants) + ";";
return ret + hilight(slotName.getName(constants)) + hilight("=") + value.toString(constants);
}
public TreeItem getObject() {

View File

@@ -39,7 +39,7 @@ public class SetSuperTreeItem extends TreeItem {
public String toString(ConstantPool constants) {
String calee = object.toString(constants) + ".";
if (Highlighting.stripHilights(calee).equals("this.")) calee = "";
return calee + hilight("super.") + propertyName.toString(constants) + hilight("=") + value.toString(constants) + ";";
return calee + hilight("super.") + propertyName.toString(constants) + hilight("=") + value.toString(constants);
}

View File

@@ -41,7 +41,7 @@ public abstract class TreeItem {
public static final int PRECEDENCE_ASSIGMENT = 14;
public static final int PRECEDENCE_COMMA = 15;
public static final int NOPRECEDENCE = 16;
public int precedence = NOPRECEDENCE;
public AVM2Instruction instruction;
@@ -53,7 +53,18 @@ public abstract class TreeItem {
public abstract String toString(ConstantPool constants);
public String toStringSemicoloned(ConstantPool constants)
{
return toString(constants)+(needsSemicolon()?";":"");
}
public boolean needsSemicolon()
{
return true;
}
protected String hilight(String str) {
if (instruction == null)

View File

@@ -31,6 +31,13 @@ public class DoWhileTreeItem extends LoopTreeItem implements Block {
public List<TreeItem> commands;
public TreeItem expression;
@Override
public boolean needsSemicolon() {
return false;
}
public DoWhileTreeItem(AVM2Instruction instruction, int loopBreak, int loopContinue, List<TreeItem> commands, TreeItem expression) {
super(instruction, loopBreak, loopContinue);
this.expression = expression;
@@ -43,7 +50,7 @@ public class DoWhileTreeItem extends LoopTreeItem implements Block {
ret += "loop" + loopBreak + ":\r\n";
ret += hilight("do\r\n{") + "\r\n";
for (TreeItem ti : commands) {
ret += ti.toString(constants) + "\r\n";
ret += ti.toStringSemicoloned(constants) + "\r\n";
}
ret += hilight("}\r\nwhile(") + expression.toString(constants) + hilight(");") + "\r\n";
ret += ":loop" + loopBreak;

View File

@@ -54,6 +54,11 @@ public class ForEachTreeItem extends LoopTreeItem implements Block {
this.expression = expression;
this.commands = commands;
}
@Override
public boolean needsSemicolon() {
return false;
}
@Override
public String toString(ConstantPool constants) {
@@ -61,7 +66,7 @@ public class ForEachTreeItem extends LoopTreeItem implements Block {
ret += "loop" + loopBreak + ":\r\n";
ret += hilight("for ") + expression.toString(constants) + "\r\n{\r\n";
for (TreeItem ti : commands) {
ret += ti.toString(constants) + "\r\n";
ret += ti.toStringSemicoloned(constants) + "\r\n";
}
ret += hilight("}") + "\r\n";
ret += ":loop" + loopBreak;

View File

@@ -70,13 +70,18 @@ public class ForTreeItem extends LoopTreeItem implements Block {
}
ret += hilight(")") + "\r\n{\r\n";
for (TreeItem ti : commands) {
ret += ti.toString(constants) + "\r\n";
ret += ti.toStringSemicoloned(constants) + "\r\n";
}
ret += hilight("}") + "\r\n";
ret += ":loop" + loopBreak;
return ret;
}
@Override
public boolean needsSemicolon() {
return false;
}
public List<ContinueTreeItem> getContinues() {
List<ContinueTreeItem> ret = new ArrayList<ContinueTreeItem>();
for (TreeItem ti : commands) {

View File

@@ -43,19 +43,24 @@ public class IfTreeItem extends TreeItem implements Block {
String ret = "";
ret = hilight("if(") + expression.toString(constants) + hilight(")\r\n{\r\n");
for (TreeItem ti : onTrue) {
ret += ti.toString(constants) + "\r\n";
ret += ti.toStringSemicoloned(constants) + "\r\n";
}
ret += hilight("}");
if (onFalse.size() > 0) {
ret += hilight("\r\nelse\r\n{\r\n");
for (TreeItem ti : onFalse) {
ret += ti.toString(constants) + "\r\n";
ret += ti.toStringSemicoloned(constants) + "\r\n";
}
ret += hilight("}");
}
return ret;
}
@Override
public boolean needsSemicolon() {
return false;
}
public List<ContinueTreeItem> getContinues() {
List<ContinueTreeItem> ret = new ArrayList<ContinueTreeItem>();
for (TreeItem ti : onTrue) {

View File

@@ -30,4 +30,8 @@ public abstract class LoopTreeItem extends TreeItem {
this.loopBreak = loopBreak;
this.loopContinue = loopContinue;
}
@Override
public boolean needsSemicolon() {
return false;
}
}

View File

@@ -51,7 +51,7 @@ public class SwitchTreeItem extends LoopTreeItem implements Block {
ret += "case " + caseValues.get(i).toString(constants) + ":\r\n";
ret += AVM2Code.IDENTOPEN + "\r\n";
for (int j = 0; j < caseCommands.get(i).size(); j++) {
ret += caseCommands.get(i).get(j).toString(constants) + "\r\n";
ret += caseCommands.get(i).get(j).toStringSemicoloned(constants) + "\r\n";
}
ret += AVM2Code.IDENTCLOSE + "\r\n";
}
@@ -59,7 +59,7 @@ public class SwitchTreeItem extends LoopTreeItem implements Block {
ret += hilight("default") + ":\r\n";
ret += AVM2Code.IDENTOPEN + "\r\n";
for (int j = 0; j < defaultCommands.size(); j++) {
ret += defaultCommands.get(j).toString(constants) + "\r\n";
ret += defaultCommands.get(j).toStringSemicoloned(constants) + "\r\n";
}
ret += AVM2Code.IDENTCLOSE + "\r\n";
}
@@ -68,6 +68,11 @@ public class SwitchTreeItem extends LoopTreeItem implements Block {
return ret;
}
@Override
public boolean needsSemicolon() {
return false;
}
public List<ContinueTreeItem> getContinues() {
List<ContinueTreeItem> ret = new ArrayList<ContinueTreeItem>();

View File

@@ -46,21 +46,21 @@ public class TryTreeItem extends TreeItem implements Block {
String ret = "";
ret += "try\r\n{\r\n";
for (TreeItem ti : tryCommands) {
ret += ti.toString(constants) + "\r\n";
ret += ti.toStringSemicoloned(constants) + "\r\n";
}
ret += "}";
for (int e = 0; e < catchExceptions.size(); e++) {
ret += "\r\ncatch(" + catchExceptions.get(e).getVarName(constants) + ":" + catchExceptions.get(e).getTypeName(constants) + ")\r\n{\r\n";
List<TreeItem> commands = catchCommands.get(e);
for (TreeItem ti : commands) {
ret += ti.toString(constants) + "\r\n";
ret += ti.toStringSemicoloned(constants) + "\r\n";
}
ret += "}";
}
if (finallyCommands.size() > 0) {
ret += "\r\nfinally\r\n{\r\n";
for (TreeItem ti : finallyCommands) {
ret += ti.toString(constants) + "\r\n";
ret += ti.toStringSemicoloned(constants) + "\r\n";
}
ret += "}";
}
@@ -99,4 +99,9 @@ public class TryTreeItem extends TreeItem implements Block {
}
return ret;
}
@Override
public boolean needsSemicolon() {
return false;
}
}

View File

@@ -43,7 +43,7 @@ public class WhileTreeItem extends LoopTreeItem implements Block {
ret += "loop" + loopBreak + ":\r\n";
ret += hilight("while(") + expression.toString(constants) + hilight(")") + "\r\n{\r\n";
for (TreeItem ti : commands) {
ret += ti.toString(constants) + "\r\n";
ret += ti.toStringSemicoloned(constants) + "\r\n";
}
ret += hilight("}") + "\r\n";
ret += ":loop" + loopBreak;
@@ -62,4 +62,9 @@ public class WhileTreeItem extends LoopTreeItem implements Block {
}
return ret;
}
@Override
public boolean needsSemicolon() {
return false;
}
}

View File

@@ -86,6 +86,7 @@ public class MethodBody implements Cloneable {
try {
HashMap<Integer,String> localRegNames=code.getLocalRegNamesFromDebug(abc);
s += code.toSource(isStatic, classIndex, abc, constants, method_info, this, hilight);
s=s.trim();
if(hilight)
{
s=Highlighting.hilighMethod(s, this.method_info);

View File

@@ -36,7 +36,7 @@ public class BreakTreeItem extends TreeItem {
@Override
public String toString(ConstantPool constants) {
return hilight("break") + " loop" + loopPos + ";";
return hilight("break") + " loop" + loopPos;
}
}

View File

@@ -37,7 +37,7 @@ public class ContinueTreeItem extends TreeItem {
@Override
public String toString(ConstantPool constants) {
return hilight(isBreak ? "break" : "continue") + " " + (isKnown ? "loop" : "unk") + loopPos + ";";
return hilight(isBreak ? "break" : "continue") + " " + (isKnown ? "loop" : "unk") + loopPos;
}
}

View File

@@ -31,7 +31,7 @@ public class DefineLocalTreeItem extends TreeItem {
@Override
public String toString(ConstantPool constants) {
if (value == null) return "var " + stripQuotes(name) + ";";
return "var " + stripQuotes(name) + "=" + value.toString(constants) + ";";
if (value == null) return "var " + stripQuotes(name);
return "var " + stripQuotes(name) + "=" + value.toString(constants);
}
}