finish recognization part of bmap binding

This commit is contained in:
yyc12345 2023-11-02 12:40:50 +08:00
parent 68bffefe5a
commit 57e6f14067
6 changed files with 175 additions and 20 deletions

View File

@ -6,9 +6,9 @@ public class ExpFctDecl {
public VariableType mFctRetType; public VariableType mFctRetType;
public Vector<ExpFctParamDecl> mFctParams; public Vector<ExpFctParamDecl> mFctParams;
public ExpFctDecl(String fct_name, VariableType ret_type) { public ExpFctDecl() {
mFctName = fct_name; mFctName = "";
mFctRetType = ret_type; mFctRetType = new VariableType();
mFctParams = new Vector<ExpFctParamDecl>(); mFctParams = new Vector<ExpFctParamDecl>();
} }

View File

@ -3,10 +3,12 @@ public class ExpFctParamDecl {
public VariableType mVarType; public VariableType mVarType;
public String mVarName; public String mVarName;
public boolean mIsInput;
public ExpFctParamDecl(VariableType vtype, String vname) { public ExpFctParamDecl() {
mVarType = vtype; mVarType = new VariableType();
mVarName = vname; mVarName = "";
mIsInput = true;
} }
} }

View File

@ -16,6 +16,6 @@ fctArg
; ;
varType varType
: EXPFCTS_IDENTIFIER ('::' EXPFCTS_IDENTIFIER)* '*'? : EXPFCTS_IDENTIFIER ('::' EXPFCTS_IDENTIFIER)* '*'*
; ;

View File

@ -1,6 +1,123 @@
import java.util.Collections;
import java.util.Vector;
import java.util.stream.Collectors;
import org.antlr.v4.runtime.*; import org.antlr.v4.runtime.*;
import org.antlr.v4.runtime.tree.*; import org.antlr.v4.runtime.tree.*;
public class ExpFctsWalker extends ExpFctsParserBaseListener { public class ExpFctsWalker extends ExpFctsParserBaseListener {
public ExpFctsWalker() {
mFctList = new Vector<ExpFctDecl>();
mCurrentFct = null;
mCurrentParam = null;
}
public Vector<ExpFctDecl> getResult() {
return mFctList;
}
private Vector<ExpFctDecl> mFctList;
private ExpFctDecl mCurrentFct;
private ExpFctParamDecl mCurrentParam;
@Override
public void enterProgram(ExpFctsParser.ProgramContext ctx) {
mFctList.clear();
}
@Override
public void enterFctDecl(ExpFctsParser.FctDeclContext ctx) {
mCurrentFct = new ExpFctDecl();
}
@Override
public void exitFctDecl(ExpFctsParser.FctDeclContext ctx) {
mCurrentFct.mFctName = ctx.EXPFCTS_IDENTIFIER().getText();
mFctList.add(mCurrentFct);
mCurrentFct = null;
}
@Override
public void exitFctArgFileDecl(ExpFctsParser.FctArgFileDeclContext ctx) {
ExpFctParamDecl decl = new ExpFctParamDecl();
decl.mVarName = ctx.EXPFCTS_IDENTIFIER().getText();
decl.mIsInput = true;
decl.mVarType.fromCType("BMap::BMFile*");
mCurrentFct.mFctParams.add(decl);
}
@Override
public void exitFctArgMeshTransDecl(ExpFctsParser.FctArgMeshTransDeclContext ctx) {
ExpFctParamDecl decl = new ExpFctParamDecl();
decl.mVarName = ctx.EXPFCTS_IDENTIFIER().getText();
decl.mIsInput = true;
decl.mVarType.fromCType("BMap::BMMeshTransition*");
mCurrentFct.mFctParams.add(decl);
}
@Override
public void exitFctArgObjDecl(ExpFctsParser.FctArgObjDeclContext ctx) {
ExpFctParamDecl first_decl = new ExpFctParamDecl();
first_decl.mVarName = ctx.EXPFCTS_IDENTIFIER(0).getText();
first_decl.mIsInput = true;
first_decl.mVarType.fromCType("BMap::BMFile*");
mCurrentFct.mFctParams.add(first_decl);
ExpFctParamDecl second_decl = new ExpFctParamDecl();
second_decl.mVarName = ctx.EXPFCTS_IDENTIFIER(1).getText();
second_decl.mIsInput = true;
second_decl.mVarType.fromCType("LibCmo::CK2::CK_ID");
mCurrentFct.mFctParams.add(second_decl);
}
@Override
public void enterFctArgParamIn(ExpFctsParser.FctArgParamInContext ctx) {
mCurrentParam = new ExpFctParamDecl();
}
@Override
public void exitFctArgParamIn(ExpFctsParser.FctArgParamInContext ctx) {
mCurrentParam.mVarName = ctx.EXPFCTS_IDENTIFIER().getText();
mCurrentParam.mIsInput = true;
mCurrentFct.mFctParams.add(mCurrentParam);
mCurrentParam = null;
}
@Override
public void enterFctArgParamOut(ExpFctsParser.FctArgParamOutContext ctx) {
mCurrentParam = new ExpFctParamDecl();
}
@Override
public void exitFctArgParamOut(ExpFctsParser.FctArgParamOutContext ctx) {
mCurrentParam.mVarName = ctx.EXPFCTS_IDENTIFIER().getText();
mCurrentParam.mIsInput = false;
// set to its pointer type
//mCurrentParam.mVarType = mCurrentParam.mVarType.getPointerOfThis();
mCurrentFct.mFctParams.add(mCurrentParam);
mCurrentParam = null;
}
@Override
public void exitVarType(ExpFctsParser.VarTypeContext ctx) {
// get namespace parts and join them
String ctype = ctx.EXPFCTS_IDENTIFIER().stream().map(value -> value.getText())
.collect(Collectors.joining("::"));
// add star if necessary
if (ctx.EXPFCTS_STAR() != null) {
ctype += String.join("", Collections.nCopies(ctx.EXPFCTS_STAR().size(), "*"));
}
if (!mCurrentFct.mFctRetType.isValid()) {
// fill function ret type first
mCurrentFct.mFctRetType.fromCType(ctype);
} else {
// otherwise, fill param data
mCurrentParam.mVarType.fromCType(ctype);
}
}
} }

View File

@ -1,9 +1,27 @@
import java.io.FileInputStream;
import java.nio.charset.StandardCharsets;
import java.util.Vector;
import org.antlr.v4.runtime.*; import org.antlr.v4.runtime.*;
import org.antlr.v4.runtime.tree.*; import org.antlr.v4.runtime.tree.*;
public class MainRunner { public class MainRunner {
public static void main(String[] args) throws Exception { public static void main(String[] args) throws Exception {
// get interface structture
FileInputStream fs = new FileInputStream("dest/BMExports.hpp");
CharStream antlrfs = CharStreams.fromStream(fs, StandardCharsets.UTF_8);
ExpFctsLexer lexer = new ExpFctsLexer(antlrfs);
CommonTokenStream tokens = new CommonTokenStream(lexer);
ExpFctsParser parser = new ExpFctsParser(tokens);
ParseTree tree = parser.program();
ParseTreeWalker walker = new ParseTreeWalker();
ExpFctsWalker worker = new ExpFctsWalker();
walker.walk(worker, tree);
fs.close();
Vector<ExpFctDecl> result = worker.getResult();
// print message. // print message.
System.out.println("DONE!"); System.out.println("DONE!");

View File

@ -1,21 +1,26 @@
import java.util.Collections; import java.util.Collections;
import java.util.Vector;
import java.util.stream.Collectors;
public class VariableType { public class VariableType {
/** /**
* The base type of this variable which remove all ending stars. * The base type of this variable which remove all ending stars. Each item is a
* part of namespace string. If no namespace, this Vector will only have one
* item.
*/ */
private String mBaseType; private Vector<String> mBaseType;
/** /**
* The pointer level of this type. It is equal with the count of stars. * The pointer level of this type. It is equal with the count of stars.
*/ */
private int mPointerLevel; private int mPointerLevel;
public VariableType(String ctype) { public VariableType() {
fromCType(ctype); mBaseType = new Vector<String>();
mPointerLevel = 0;
} }
private VariableType(String base_type, int pointer_level) { private VariableType(Vector<String> base_type, int pointer_level) {
mBaseType = base_type; mBaseType.addAll(base_type);
mPointerLevel = pointer_level; mPointerLevel = pointer_level;
} }
@ -23,33 +28,46 @@ public class VariableType {
if (ctype.isEmpty()) if (ctype.isEmpty())
throw new IllegalArgumentException("empty string can not be parsed."); throw new IllegalArgumentException("empty string can not be parsed.");
// get pointer part and name part
int len = ctype.length(); int len = ctype.length();
int star_pos = ctype.indexOf('*'); int star_pos = ctype.indexOf('*');
String namepart;
if (star_pos == -1) { if (star_pos == -1) {
// no star // no star
mBaseType = ctype; namepart = ctype;
mPointerLevel = 0; mPointerLevel = 0;
} else { } else {
// has star // has star
if (star_pos == 0) if (star_pos == 0)
throw new IllegalArgumentException("base type not found."); throw new IllegalArgumentException("base type not found.");
mBaseType = ctype.substring(0, star_pos); namepart = ctype.substring(0, star_pos);
mPointerLevel = len - star_pos; mPointerLevel = len - star_pos;
} }
// resolve name part
mBaseType.clear();
for (String item : namepart.split("::")) {
mBaseType.add(item);
}
} }
public String toCType() { public String toCType() {
return mBaseType + String.join("", Collections.nCopies(mPointerLevel, "*")); return mBaseType.stream().collect(Collectors.joining("::"))
+ String.join("", Collections.nCopies(mPointerLevel, "*"));
} }
public String getBaseType() { public String getBaseType() {
return mBaseType; return mBaseType.lastElement();
} }
public boolean isPointer() { public boolean isPointer() {
return mPointerLevel != 0; return mPointerLevel != 0;
} }
public boolean isValid() {
return mBaseType.size() != 0;
}
public VariableType getPointerOfThis() { public VariableType getPointerOfThis() {
return new VariableType(mBaseType, mPointerLevel); return new VariableType(mBaseType, mPointerLevel);
} }