finish recognization part of bmap binding
This commit is contained in:
parent
68bffefe5a
commit
57e6f14067
|
@ -6,9 +6,9 @@ public class ExpFctDecl {
|
|||
public VariableType mFctRetType;
|
||||
public Vector<ExpFctParamDecl> mFctParams;
|
||||
|
||||
public ExpFctDecl(String fct_name, VariableType ret_type) {
|
||||
mFctName = fct_name;
|
||||
mFctRetType = ret_type;
|
||||
public ExpFctDecl() {
|
||||
mFctName = "";
|
||||
mFctRetType = new VariableType();
|
||||
mFctParams = new Vector<ExpFctParamDecl>();
|
||||
}
|
||||
|
||||
|
|
|
@ -3,10 +3,12 @@ public class ExpFctParamDecl {
|
|||
|
||||
public VariableType mVarType;
|
||||
public String mVarName;
|
||||
public boolean mIsInput;
|
||||
|
||||
public ExpFctParamDecl(VariableType vtype, String vname) {
|
||||
mVarType = vtype;
|
||||
mVarName = vname;
|
||||
public ExpFctParamDecl() {
|
||||
mVarType = new VariableType();
|
||||
mVarName = "";
|
||||
mIsInput = true;
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -16,6 +16,6 @@ fctArg
|
|||
;
|
||||
|
||||
varType
|
||||
: EXPFCTS_IDENTIFIER ('::' EXPFCTS_IDENTIFIER)* '*'?
|
||||
: EXPFCTS_IDENTIFIER ('::' EXPFCTS_IDENTIFIER)* '*'*
|
||||
;
|
||||
|
||||
|
|
|
@ -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.tree.*;
|
||||
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -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.tree.*;
|
||||
|
||||
public class MainRunner {
|
||||
|
||||
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.
|
||||
System.out.println("DONE!");
|
||||
|
|
|
@ -1,21 +1,26 @@
|
|||
import java.util.Collections;
|
||||
import java.util.Vector;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
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.
|
||||
*/
|
||||
private int mPointerLevel;
|
||||
|
||||
public VariableType(String ctype) {
|
||||
fromCType(ctype);
|
||||
public VariableType() {
|
||||
mBaseType = new Vector<String>();
|
||||
mPointerLevel = 0;
|
||||
}
|
||||
|
||||
private VariableType(String base_type, int pointer_level) {
|
||||
mBaseType = base_type;
|
||||
private VariableType(Vector<String> base_type, int pointer_level) {
|
||||
mBaseType.addAll(base_type);
|
||||
mPointerLevel = pointer_level;
|
||||
}
|
||||
|
||||
|
@ -23,33 +28,46 @@ public class VariableType {
|
|||
if (ctype.isEmpty())
|
||||
throw new IllegalArgumentException("empty string can not be parsed.");
|
||||
|
||||
// get pointer part and name part
|
||||
int len = ctype.length();
|
||||
int star_pos = ctype.indexOf('*');
|
||||
String namepart;
|
||||
if (star_pos == -1) {
|
||||
// no star
|
||||
mBaseType = ctype;
|
||||
namepart = ctype;
|
||||
mPointerLevel = 0;
|
||||
} else {
|
||||
// has star
|
||||
if (star_pos == 0)
|
||||
throw new IllegalArgumentException("base type not found.");
|
||||
mBaseType = ctype.substring(0, star_pos);
|
||||
namepart = ctype.substring(0, star_pos);
|
||||
mPointerLevel = len - star_pos;
|
||||
}
|
||||
|
||||
// resolve name part
|
||||
mBaseType.clear();
|
||||
for (String item : namepart.split("::")) {
|
||||
mBaseType.add(item);
|
||||
}
|
||||
}
|
||||
|
||||
public String toCType() {
|
||||
return mBaseType + String.join("", Collections.nCopies(mPointerLevel, "*"));
|
||||
return mBaseType.stream().collect(Collectors.joining("::"))
|
||||
+ String.join("", Collections.nCopies(mPointerLevel, "*"));
|
||||
}
|
||||
|
||||
public String getBaseType() {
|
||||
return mBaseType;
|
||||
return mBaseType.lastElement();
|
||||
}
|
||||
|
||||
public boolean isPointer() {
|
||||
return mPointerLevel != 0;
|
||||
}
|
||||
|
||||
public boolean isValid() {
|
||||
return mBaseType.size() != 0;
|
||||
}
|
||||
|
||||
public VariableType getPointerOfThis() {
|
||||
return new VariableType(mBaseType, mPointerLevel);
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue
Block a user