Changed AS1/2 P-code action parameters are now separated by commas, code without commas is still accepted

This commit is contained in:
Jindra Petřík
2023-10-16 09:36:06 +02:00
parent d0a69b1850
commit cca0161766
18 changed files with 1217 additions and 1209 deletions
@@ -265,7 +265,7 @@ public abstract class Action implements GraphSourceItem {
* @throws ActionParseException When read object is not String
*/
protected String lexString(FlasmLexer lex) throws IOException, ActionParseException {
ASMParsedSymbol symb = lex.yylex();
ASMParsedSymbol symb = lex.lex();
if (symb.type != ASMParsedSymbol.TYPE_STRING) {
throw new ActionParseException("String expected", lex.yyline());
}
@@ -280,7 +280,7 @@ public abstract class Action implements GraphSourceItem {
* @throws ActionParseException When read object is not Block startServer
*/
protected void lexBlockOpen(FlasmLexer lex) throws IOException, ActionParseException {
ASMParsedSymbol symb = lex.yylex();
ASMParsedSymbol symb = lex.lex();
if (symb.type != ASMParsedSymbol.TYPE_BLOCK_START) {
throw new ActionParseException("Block startServer ", lex.yyline());
}
@@ -295,7 +295,7 @@ public abstract class Action implements GraphSourceItem {
* @throws ActionParseException When read object is not Identifier
*/
protected String lexIdentifier(FlasmLexer lex) throws IOException, ActionParseException {
ASMParsedSymbol symb = lex.yylex();
ASMParsedSymbol symb = lex.lex();
if (symb.type != ASMParsedSymbol.TYPE_IDENTIFIER) {
throw new ActionParseException("Identifier expected", lex.yyline());
}
@@ -311,7 +311,7 @@ public abstract class Action implements GraphSourceItem {
* @throws ActionParseException When read object is not long value
*/
protected long lexLong(FlasmLexer lex) throws IOException, ActionParseException {
ASMParsedSymbol symb = lex.yylex();
ASMParsedSymbol symb = lex.lex();
if (symb.type != ASMParsedSymbol.TYPE_INTEGER) {
throw new ActionParseException("Integer expected", lex.yyline());
}
@@ -327,12 +327,19 @@ public abstract class Action implements GraphSourceItem {
* @throws ActionParseException When read object is not boolean value
*/
protected boolean lexBoolean(FlasmLexer lex) throws IOException, ActionParseException {
ASMParsedSymbol symb = lex.yylex();
ASMParsedSymbol symb = lex.lex();
if (symb.type != ASMParsedSymbol.TYPE_BOOLEAN) {
throw new ActionParseException("Boolean expected", lex.yyline());
}
}
return (Boolean) symb.value;
}
protected void lexOptionalComma(FlasmLexer lex) throws IOException, ActionParseException {
ASMParsedSymbol symb = lex.lex();
if (symb.type != ASMParsedSymbol.TYPE_COMMA) {
lex.pushback(symb);
}
}
/**
* Gets action converted to bytes
@@ -646,7 +653,7 @@ public abstract class Action implements GraphSourceItem {
add = "; ofs" + Helper.formatAddress(offset) + add;
add = "";*/
if ((a instanceof ActionPush) && lastPush) {
writer.appendNoHilight(" ");
writer.appendNoHilight(", ");
((ActionPush) a).paramsToStringReplaced(list, importantOffsets, exportMode, writer);
} else {
if (lastPush) {
@@ -59,6 +59,8 @@ public class ASMParsedSymbol {
public static final int TYPE_EOL = 16;
public static final int TYPE_CONSTANT_LITERAL = 17;
public static final int TYPE_COMMA = 18;
public ASMParsedSymbol(int type, Object value) {
this.type = type;
@@ -68,4 +70,9 @@ public class ASMParsedSymbol {
public ASMParsedSymbol(int type) {
this.type = type;
}
@Override
public String toString() {
return "symbol[type="+type+", value="+value+"]";
}
}
@@ -373,7 +373,7 @@ public class ASMParser {
Stack<GraphSourceItemContainer> containers = new Stack<>();
List<String> emptyList = new ArrayList<>();
while (true) {
ASMParsedSymbol symb = lexer.yylex();
ASMParsedSymbol symb = lexer.lex();
if (symb.type == ASMParsedSymbol.TYPE_BLOCK_END) {
if (containers.isEmpty()) {
throw new ActionParseException("Block end without start", lexer.yyline());
@@ -407,7 +407,7 @@ public class ASMParser {
list.add(cpool);
while (true) {
ASMParsedSymbol symb = lexer.yylex();
ASMParsedSymbol symb = lexer.lex();
if (symb.type == ASMParsedSymbol.TYPE_LABEL) {
labels.add(new Label((String) symb.value, address));
} else if (symb.type == ASMParsedSymbol.TYPE_COMMENT) {
File diff suppressed because it is too large Load Diff
@@ -76,6 +76,7 @@ public class ActionGetURL extends Action {
public ActionGetURL(FlasmLexer lexer, String charset) throws IOException, ActionParseException {
super(0x83, 0, charset);
urlString = lexString(lexer);
lexOptionalComma(lexer);
targetString = lexString(lexer);
}
@@ -97,7 +98,7 @@ public class ActionGetURL extends Action {
@Override
public String toString() {
return "GetUrl \"" + Helper.escapeActionScriptString(urlString) + "\" \"" + Helper.escapeActionScriptString(targetString) + "\"";
return "GetUrl \"" + Helper.escapeActionScriptString(urlString) + "\", \"" + Helper.escapeActionScriptString(targetString) + "\"";
}
@Override
@@ -77,7 +77,7 @@ public class ActionWaitForFrame extends Action implements ActionStore {
@Override
public String getASMSource(ActionList container, Set<Long> knownAddreses, ScriptExportMode exportMode) {
String ret = "WaitForFrame " + frame + " " + skipCount;
String ret = "WaitForFrame " + frame + ", " + skipCount;
return ret;
}
@@ -100,7 +100,8 @@ public class ActionWaitForFrame extends Action implements ActionStore {
public ActionWaitForFrame(FlasmLexer lexer, String charset) throws IOException, ActionParseException {
super(0x8A, -1, charset);
frame = (int) lexLong(lexer);
skipCount = (int) lexLong(lexer);
lexOptionalComma(lexer);
skipCount = (int) lexLong(lexer);
skipped = new ArrayList<>();
}
@@ -86,7 +86,7 @@ public class ActionGetURL2 extends Action {
@Override
public String toString() {
return "GetURL2 " + loadVariablesFlag + " " + loadTargetFlag + " " + sendVarsMethod;
return "GetURL2 " + loadVariablesFlag + ", " + loadTargetFlag + ", " + sendVarsMethod;
}
@Override
@@ -110,7 +110,9 @@ public class ActionGetURL2 extends Action {
public ActionGetURL2(FlasmLexer lexer, String charset) throws IOException, ActionParseException {
super(0x9A, -1, charset);
loadVariablesFlag = lexBoolean(lexer);
lexOptionalComma(lexer);
loadTargetFlag = lexBoolean(lexer);
lexOptionalComma(lexer);
sendVarsMethod = (int) lexLong(lexer);
}
@@ -98,14 +98,16 @@ public class ActionGotoFrame2 extends Action {
@Override
public String toString() {
return "GotoFrame2 " + sceneBiasFlag + " " + playFlag + " " + (sceneBiasFlag ? " " + sceneBias : "");
return "GotoFrame2 " + sceneBiasFlag + ", " + playFlag + ", " + (sceneBiasFlag ? ", " + sceneBias : "");
}
public ActionGotoFrame2(FlasmLexer lexer, String charset) throws IOException, ActionParseException {
super(0x9F, -1, charset);
sceneBiasFlag = lexBoolean(lexer);
lexOptionalComma(lexer);
playFlag = lexBoolean(lexer);
if (sceneBiasFlag) {
lexOptionalComma(lexer);
sceneBias = (int) lexLong(lexer);
}
}
@@ -265,10 +265,15 @@ public class ActionPush extends Action {
super(0x96, 0, charset);
this.constantPool = constantPool;
values = new ArrayList<>();
int count = 0;
int count = 0;
loop:
while (true) {
ASMParsedSymbol symb = lexer.yylex();
boolean valueExpected = false;
ASMParsedSymbol symb = lexer.lex();
if (symb.type == ASMParsedSymbol.TYPE_COMMA) {
symb = lexer.lex();
valueExpected = true;
}
switch (symb.type) {
case ASMParsedSymbol.TYPE_STRING:
count++;
@@ -290,7 +295,9 @@ public class ActionPush extends Action {
break;
case ASMParsedSymbol.TYPE_EOL:
case ASMParsedSymbol.TYPE_EOF:
if (count == 0) {
if (valueExpected) {
throw new ActionParseException("Value expected", lexer.yyline());
} else if (count == 0) {
throw new ActionParseException("Arguments expected", lexer.yyline());
} else {
break loop;
@@ -342,13 +349,11 @@ public class ActionPush extends Action {
}
public GraphTextWriter paramsToString(GraphTextWriter writer) {
int pos = 0;
for (int i = 0; i < values.size(); i++) {
if (pos > 0) {
writer.appendNoHilight(" ");
if (i > 0) {
writer.appendNoHilight(", ");
}
writer.append(toString(i), getAddress() + pos + 1, getFileOffset());
pos++;
writer.append(toString(i), getAddress() + i + 1, getFileOffset());
}
return writer;
}
@@ -62,14 +62,24 @@ public class ActionConstantPool extends Action {
public ActionConstantPool(FlasmLexer lexer, String charset) throws IOException, ActionParseException {
super(0x88, 0, charset);
boolean first = true;
while (true) {
ASMParsedSymbol symb = lexer.yylex();
boolean valueRequired = false;
ASMParsedSymbol symb = lexer.lex();
if (!first && symb.type == ASMParsedSymbol.TYPE_COMMA) {
symb = lexer.lex();
valueRequired = true;
}
if (symb.type == ASMParsedSymbol.TYPE_STRING) {
constantPool.add((String) symb.value);
} else {
lexer.yypushback(lexer.yylength());
if (valueRequired) {
throw new ActionParseException("String expected", lexer.yyline());
}
lexer.pushback(symb);
break;
}
}
first = false;
}
}
@@ -109,6 +119,9 @@ public class ActionConstantPool extends Action {
StringBuilder ret = new StringBuilder();
ret.append("ConstantPool");
for (int i = 0; i < constantPool.size(); i++) {
if (i > 0) {
ret.append(",");
}
ret.append(" \"").append(Helper.escapeActionScriptString(constantPool.get(i))).append("\"");
}
return ret.toString();
@@ -97,8 +97,10 @@ public class ActionDefineFunction extends Action implements GraphSourceItemConta
public ActionDefineFunction(FlasmLexer lexer, String charset) throws IOException, ActionParseException {
super(0x9B, -1, charset);
functionName = lexString(lexer);
lexOptionalComma(lexer);
int numParams = (int) lexLong(lexer);
for (int i = 0; i < numParams; i++) {
lexOptionalComma(lexer);
paramNames.add(lexString(lexer));
}
lexBlockOpen(lexer);
@@ -138,10 +140,11 @@ public class ActionDefineFunction extends Action implements GraphSourceItemConta
public String getASMSource(ActionList container, Set<Long> knownAddreses, ScriptExportMode exportMode) {
StringBuilder paramStr = new StringBuilder();
for (int i = 0; i < paramNames.size(); i++) {
paramStr.append(", ");
paramStr.append("\"").append(Helper.escapeActionScriptString(paramNames.get(i))).append("\" ");
}
return "DefineFunction \"" + Helper.escapeActionScriptString(functionName) + "\" " + paramNames.size() + " " + paramStr + " {" + (codeSize == 0 ? "\r\n}" : "");
return "DefineFunction \"" + Helper.escapeActionScriptString(functionName) + "\", " + paramNames.size() + paramStr + " {" + (codeSize == 0 ? "\r\n}" : "");
}
@Override
@@ -145,19 +145,32 @@ public class ActionDefineFunction2 extends Action implements GraphSourceItemCont
public ActionDefineFunction2(FlasmLexer lexer, String charset) throws IOException, ActionParseException {
super(0x8E, -1, charset);
functionName = lexString(lexer);
lexOptionalComma(lexer);
int numParams = (int) lexLong(lexer);
lexOptionalComma(lexer);
registerCount = (int) lexLong(lexer);
lexOptionalComma(lexer);
preloadParentFlag = lexBoolean(lexer);
lexOptionalComma(lexer);
preloadRootFlag = lexBoolean(lexer);
lexOptionalComma(lexer);
suppressSuperFlag = lexBoolean(lexer);
lexOptionalComma(lexer);
preloadSuperFlag = lexBoolean(lexer);
lexOptionalComma(lexer);
suppressArgumentsFlag = lexBoolean(lexer);
lexOptionalComma(lexer);
preloadArgumentsFlag = lexBoolean(lexer);
lexOptionalComma(lexer);
suppressThisFlag = lexBoolean(lexer);
lexOptionalComma(lexer);
preloadThisFlag = lexBoolean(lexer);
lexOptionalComma(lexer);
preloadGlobalFlag = lexBoolean(lexer);
for (int i = 0; i < numParams; i++) {
lexOptionalComma(lexer);
paramRegisters.add((int) lexLong(lexer));
lexOptionalComma(lexer);
paramNames.add(lexString(lexer));
}
lexBlockOpen(lexer);
@@ -227,19 +240,20 @@ public class ActionDefineFunction2 extends Action implements GraphSourceItemCont
public String getASMSource(ActionList container, Set<Long> knownAddreses, ScriptExportMode exportMode) {
StringBuilder paramStr = new StringBuilder();
for (int i = 0; i < paramNames.size(); i++) {
paramStr.append(paramRegisters.get(i)).append(" \"").append(Helper.escapeActionScriptString(paramNames.get(i))).append("\" ");
paramStr.append(", ");
paramStr.append(paramRegisters.get(i)).append(", \"").append(Helper.escapeActionScriptString(paramNames.get(i))).append("\"");
}
return ("DefineFunction2 \"" + Helper.escapeActionScriptString(functionName) + "\" " + paramRegisters.size() + " " + registerCount
+ " " + preloadParentFlag
+ " " + preloadRootFlag
+ " " + suppressSuperFlag
+ " " + preloadSuperFlag
+ " " + suppressArgumentsFlag
+ " " + preloadArgumentsFlag
+ " " + suppressThisFlag
+ " " + preloadThisFlag
+ " " + preloadGlobalFlag).trim() + " " + paramStr + " {" + (codeSize == 0 ? "\r\n}" : "");
return ("DefineFunction2 \"" + Helper.escapeActionScriptString(functionName) + "\", " + paramRegisters.size() + ", " + registerCount
+ ", " + preloadParentFlag
+ ", " + preloadRootFlag
+ ", " + suppressSuperFlag
+ ", " + preloadSuperFlag
+ ", " + suppressArgumentsFlag
+ ", " + preloadArgumentsFlag
+ ", " + suppressThisFlag
+ ", " + preloadThisFlag
+ ", " + preloadGlobalFlag).trim() + paramStr + " {" + (codeSize == 0 ? "\r\n}" : "");
}
@Override
@@ -151,7 +151,7 @@ public class ActionTry extends Action implements GraphSourceItemContainer {
super(0x8F, 0, charset);
this.version = version;
ASMParsedSymbol symb = lexer.yylex();
ASMParsedSymbol symb = lexer.lex();
if (symb.type == ASMParsedSymbol.TYPE_STRING) {
catchInRegisterFlag = false;
catchName = (String) symb.value;
@@ -238,7 +238,7 @@ public class ActionTry extends Action implements GraphSourceItemContainer {
@Override
public boolean parseDivision(long size, FlasmLexer lexer) {
try {
ASMParsedSymbol symb = lexer.yylex();
ASMParsedSymbol symb = lexer.lex();
//catchBlockFlag = false;
if (symb.type == ASMParsedSymbol.TYPE_INSTRUCTION_NAME) {
if (((String) symb.value).toLowerCase().equals("catch")) {
@@ -259,14 +259,14 @@ public class ActionTry extends Action implements GraphSourceItemContainer {
return true;
} else {
//finallyBlockFlag = false;
lexer.yypushback(lexer.yylength());
lexer.pushback(symb);
}
} else {
//finallyBlockFlag = false;
lexer.yypushback(lexer.yylength());
lexer.pushback(symb);
}
} else {
lexer.yypushback(lexer.yylength());
lexer.pushback(symb);
}
} catch (IOException | ActionParseException ex) {
//ignored
@@ -277,7 +277,6 @@ public class ActionTry extends Action implements GraphSourceItemContainer {
} else if (catchBlockFlag) {
catchSize = size - getHeaderSize() - trySize;
}
lexer.yybegin(0);
return false;
}