Hilight currently selected argument in docs for AS3

This commit is contained in:
Jindra Petřík
2023-10-15 23:54:54 +02:00
parent 20f2c75f8f
commit 7e336b98fb
9 changed files with 3024 additions and 3165 deletions

View File

@@ -22,6 +22,8 @@ package com.jpexs.decompiler.flash.abc.avm2.parser.pcode;
*/
public class ParsedSymbol {
public int pos;
public int type;
public Object value;
@@ -218,12 +220,14 @@ public class ParsedSymbol {
public static final int TYPE_KEYWORD_PROTECTEDNS_BLOCK = 97;
public ParsedSymbol(int type, Object value) {
public ParsedSymbol(int pos, int type, Object value) {
this.pos = pos;
this.type = type;
this.value = value;
}
public ParsedSymbol(int type) {
public ParsedSymbol(int pos, int type) {
this.pos = pos;
this.type = type;
}
}

View File

@@ -19,7 +19,9 @@ package com.jpexs.decompiler.flash.docs;
import com.jpexs.helpers.Cache;
import com.jpexs.helpers.Helper;
import com.jpexs.helpers.utf8.Utf8Helper;
import java.io.IOException;
import java.io.InputStream;
import java.io.StringReader;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
@@ -73,5 +75,69 @@ public class AbstractDocs {
dateFormat.setTimeZone(TimeZone.getTimeZone("UTC"));
return dateFormat.format(date);
}
protected static String hilightArgument(String docs, int argumentIndex) {
if (argumentIndex < 0) {
return docs;
}
String opHeader = "<span class=\"instruction-operands\">";
int opIndex = docs.indexOf(opHeader) + opHeader.length();
int opEndIndex = docs.indexOf("</span>", opIndex);
String operandDocs = docs.substring(opIndex, opEndIndex).trim();
if (operandDocs.isEmpty()) {
return docs;
}
DocsOperandLexer lexer = new DocsOperandLexer(new StringReader(operandDocs));
try {
ParsedSymbol symb;
int pos = 0;
int endPos = 0;
int startPos = 0;
while (true) {
startPos = lexer.yychar();
symb = lexer.lex();
if (symb.type == ParsedSymbol.TYPE_BRACKET_OPEN) {
while (symb.type != ParsedSymbol.TYPE_BRACKET_CLOSE && symb.type != ParsedSymbol.TYPE_EOF) {
symb = lexer.lex();
}
endPos = lexer.yychar() + 1;
break;
}
if (symb.type == ParsedSymbol.TYPE_IDENTIFIER) {
symb = lexer.lex();
endPos = lexer.yychar();
if (symb.type == ParsedSymbol.TYPE_COLON) {
do {
symb = lexer.lex();
if (symb.type != ParsedSymbol.TYPE_IDENTIFIER && symb.type != ParsedSymbol.TYPE_STAR) {
throw new IOException("type identifier expected");
}
symb = lexer.lex();
endPos = lexer.yychar();
} while (symb.type == ParsedSymbol.TYPE_PIPE);
}
if (pos == argumentIndex) {
break;
}
if (symb.type == ParsedSymbol.TYPE_COMMA) {
pos++;
} else {
break;
}
}
}
String hilightedOperandDocs = operandDocs.substring(0, startPos)
+ "<strong class=\"selected-operand\">" + operandDocs.substring(startPos, endPos) + "</strong>"
+ operandDocs.substring(endPos);
docs = docs.substring(0, opIndex) + hilightedOperandDocs + docs.substring(opEndIndex);
} catch (IOException ex) {
//ignore
}
return docs;
}
}

View File

@@ -164,71 +164,7 @@ public class As12PCodeDocs extends AbstractDocs {
}
}
return identName.toString();
}
private static String hilightArgument(String docs, int argumentIndex) {
if (argumentIndex < 0) {
return docs;
}
String opHeader = "<span class=\"instruction-operands\">";
int opIndex = docs.indexOf(opHeader) + opHeader.length();
int opEndIndex = docs.indexOf("</span>", opIndex);
String operandDocs = docs.substring(opIndex, opEndIndex).trim();
if (operandDocs.isEmpty()) {
return docs;
}
DocsOperandLexer lexer = new DocsOperandLexer(new StringReader(operandDocs));
try {
ParsedSymbol symb;
int pos = 0;
int endPos = 0;
int startPos = 0;
while (true) {
startPos = lexer.yychar();
symb = lexer.lex();
if (symb.type == ParsedSymbol.TYPE_BRACKET_OPEN) {
while (symb.type != ParsedSymbol.TYPE_BRACKET_CLOSE && symb.type != ParsedSymbol.TYPE_EOF) {
symb = lexer.lex();
}
endPos = lexer.yychar() + 1;
break;
}
if (symb.type == ParsedSymbol.TYPE_IDENTIFIER) {
symb = lexer.lex();
endPos = lexer.yychar();
if (symb.type == ParsedSymbol.TYPE_COLON) {
do {
symb = lexer.lex();
if (symb.type != ParsedSymbol.TYPE_IDENTIFIER && symb.type != ParsedSymbol.TYPE_STAR) {
throw new IOException("type identifier expected");
}
symb = lexer.lex();
endPos = lexer.yychar();
} while (symb.type == ParsedSymbol.TYPE_PIPE);
}
if (pos == argumentIndex) {
break;
}
if (symb.type == ParsedSymbol.TYPE_COMMA) {
pos++;
} else {
break;
}
}
}
String hilightedOperandDocs = operandDocs.substring(0, startPos)
+ "<strong class=\"selected-operand\">" + operandDocs.substring(startPos, endPos) + "</strong>"
+ operandDocs.substring(endPos);
docs = docs.substring(0, opIndex) + hilightedOperandDocs + docs.substring(opEndIndex);
} catch (IOException ex) {
//ignore
}
return docs;
}
}
public static String getDocsForIns(String insName, boolean ui, boolean standalone, boolean nightMode, int argumentToHilight) {
insName = insName.toLowerCase();

View File

@@ -83,18 +83,18 @@ public class As3PCodeDocs extends AbstractDocs {
return identName.toString();
}
public static String getDocsForIns(String insName, boolean showDataSize, boolean ui, boolean withStyle, boolean nightMode) {
public static String getDocsForIns(String insName, boolean showDataSize, boolean ui, boolean withStyle, boolean nightMode, int argumentToHilight) {
if (!nameToDef.containsKey(insName)) {
return null;
}
return getDocsForIns(nameToDef.get(insName), showDataSize, ui, withStyle, nightMode);
return getDocsForIns(nameToDef.get(insName), showDataSize, ui, withStyle, nightMode, argumentToHilight);
}
public static String getDocsForIns(InstructionDefinition def, boolean showDataSize, boolean ui, boolean standalone, boolean nightMode) {
public static String getDocsForIns(InstructionDefinition def, boolean showDataSize, boolean ui, boolean standalone, boolean nightMode, int argumentToHilight) {
final String cacheKey = def.instructionName + "|" + (showDataSize ? 1 : 0) + "|" + (ui ? 1 : 0) + "|" + (standalone ? 1 : 0);
String v = docsCache.get(cacheKey);
if (v != null) {
return v;
return hilightArgument(v, argumentToHilight);
}
StringBuilder sb = new StringBuilder();
@@ -122,7 +122,7 @@ public class As3PCodeDocs extends AbstractDocs {
String stack = def.hasFlag(AVM2InstructionFlag.UNKNOWN_STACK) ? getProperty("ui.unknown") : stackBefore + "<span class=\"stack-to\">" + getProperty("ui.stack.to") + "</span>" + stackAfter;
String operandsDoc = def.hasFlag(AVM2InstructionFlag.UNKNOWN_OPERANDS) ? getProperty("ui.unknown") : getProperty("instruction." + insName + ".operands");
if (standalone) {
sb.append("<body class=\"");
if (nightMode) {
@@ -144,12 +144,18 @@ public class As3PCodeDocs extends AbstractDocs {
if (def.hasFlag(AVM2InstructionFlag.UNKNOWN_OPERANDS)) {
sb.append(" ").append(getProperty("ui.unknown")).append(NEWLINE);
} else if (ui && insName.equals("lookupswitch")) {
sb.append(" ");
sb.append("<span class=\"instruction-operands\">");
sb.append(getProperty("instruction.lookupswitch.operands.ui"));
sb.append("</span>");
} else {
String[] operandsDocs = operandsDoc.split(", ?");
boolean first = true;
if (def.operands.length > 0) {
sb.append(" ");
}
sb.append("<span class=\"instruction-operands\">");
for (int i = 0; i < def.operands.length; i++) {
int op = def.operands[i];
String opDoc = operandsDocs[i];
@@ -191,6 +197,7 @@ public class As3PCodeDocs extends AbstractDocs {
}
}
}
sb.append("</span>");
}
sb.append("</div>").append(NEWLINE);
@@ -229,7 +236,7 @@ public class As3PCodeDocs extends AbstractDocs {
}
String r = sb.toString();
docsCache.put(cacheKey, r);
return r;
return hilightArgument(r, argumentToHilight);
}
public static String getJs() {
@@ -288,7 +295,7 @@ public class As3PCodeDocs extends AbstractDocs {
continue;
}
sb.append("\t\t\t<li class=\"instruction-item\">").append(NEWLINE);
sb.append("\t\t\t\t").append(getDocsForIns(def, true, false, false, nightMode).trim().replace(NEWLINE, NEWLINE + "\t\t\t\t")).append(NEWLINE);
sb.append("\t\t\t\t").append(getDocsForIns(def, true, false, false, nightMode, -1).trim().replace(NEWLINE, NEWLINE + "\t\t\t\t")).append(NEWLINE);
sb.append("\t\t\t</li>").append(NEWLINE);
}
sb.append("\t\t</ul>").append(NEWLINE);

View File

@@ -167,6 +167,13 @@ operandType.namespaceIndex = Index into namespace constant pool
operandType.namespaceIndex.name = namespaceIndex
operandType.namespaceIndex.uiName = namespace
operandType.number = Number
operandType.number.name = number
operandType.number.uiName = uint
operandType.U30 = U30
operandType.U30.name = U30
operandType.U30.uiName = uint
#----------------------- Instructions
@@ -331,6 +338,8 @@ instruction.lookupswitch.description =
instruction.lookupswitch.stackBefore = index
instruction.lookupswitch.stackAfter =
instruction.lookupswitch.operands = defaultTarget, caseCount, case0Target, case1Target, ...
# special case for UI
instruction.lookupswitch.operands.ui = defaultTarget:labelName, [case0Target:labelName, case1Target:labelName, ...]
instruction.pushwith.shortDescription = Push with onto scope stack
instruction.pushwith.description =