Atitit dsl对于数组的处理以及main函数的参数赋值
1.1. 词法解析..添加了[] 方括号的解析支持
public List getTokensV3() {
code_char_arr = code.toCharArray();
for (char c : code_char_arr) {
// get next char,,then change stat
// jude cur char and cur stat...then if or not chage stat
if (c == '[' && !this.curStat.equals("strStart"))
{
this.curStat="sqBrkStart";
Token tk2=new Token("[").setType("spltr");
tokens.add(tk2);
this.curTokenTxt = "";
continue;
}
if (c == ']' && !this.curStat.equals("strStart"))
{
this.curStat="sqBrkEnd";
Token tk2=new Token("]").setType("spltr");
tokens.add(tk2);
this.curTokenTxt = "";
continue;
}
1.2. Ast建立.添加了数组参数的支持..使用了递归下降法..getparam use recurs desc meth... Get arrayParam ya use recdesc meth..
if (isConstrutsced == false) { // static //first
// new method invoke exp
String className = refx.getClassName(text);
className = javaUtil.clr(className);
mi.Exp = new SimpleName(className); // cls name
mi.Name = refx.getMethodName(text);
isConstrutsced = true;
// param push
List<Expression> params=buildAstV2_params(tokens);
mi.arguments=params;
return mi;
} //end static
private List<Expression> buildAstV2_params(List<Token> tokens2) {
//List<Expression> li=Lists.newArrayList();
m_index++;
Token cur = tokens.get(m_index);
//build array param
if (cur.getText().equals("[") && cur.getType().equals("spltr"))
{
//params.add(e);
ArrayCreation ac=getArrayCreation();
params.add(ac);
return buildAstV2_params(tokens2);
}
//close param
if (cur.getText().equals(")") && cur.Type.equals("op")) {
//PARAMS close
return listUtil.clone(params);
}
//add param
if(cur.getText().equals(",") )
return buildAstV2_params(tokens2);
params.add(cur);
return buildAstV2_params(tokens2);
}
private ArrayCreation getArrayCreation() {
//m_index++;
ArrayCreation ac =new ArrayCreation();
ArrayInitializer ai=new ArrayInitializer();
List<Object> params=Lists.newArrayList();
// param push
while (true) {
m_index++;
Tokencur = tokens.get(m_index);
if (cur.getText().equals("]"))
{
//System.out.println("dbg");
}
if (cur.getText().equals("]") && cur.Type.equals("spltr")) {
ai.expressions=params;
ac.optionalInitializer=ai;
return ac;
}
if(cur.getText().equals(","))
continue;
params.add(cur.Text);
if(cur.getType().equals("str"))
ac.arrayType="string";
}
//return null;
}
1.3. Ast解析 的支持
private Object SimpleName_staticProcess(Expression ast) {
MethodInvocation mi = (MethodInvocation) ast;
Expression exp = mi.Exp;
Class<?> threadClazz = null;
if (exp instanceof SimpleName) {
SimpleName sn = (SimpleName) exp;
try {
threadClazz = Class.forName(sn.IDENTIFIER);
} catch (ClassNotFoundException e) {
ExUtil.throwExV2(e);
}
}
mi.arguments=arrParamProcess(mi.arguments);
private List arrParamProcess(List arguments) {
List li=Lists.newArrayList();
for (Object object : arguments) {
if(object instanceof ArrayCreation)
{
Object arr=getArr(( ArrayCreation)object );
li.add(arr);
}
}
return li;
}
private Object getArr(ArrayCreation ac) {
if(ac.arrayType.equals("string"))
{
List<String> li=Lists.newArrayList();
List li_tmp=ac.optionalInitializer.expressions;
for (Object object : li_tmp) {
li.add((String) object);
}
return li.toArray(new String[ li.size()]);
}
return null;
}
作者:: 绰号:老哇的爪子 ( 全名::Attilax Akbar Al Rapanui 阿提拉克斯 阿克巴 阿尔 拉帕努伊 )
汉字名:艾提拉(艾龙), EMAIL:1466519819@qq.com
转载请注明来源: http://blog.csdn.net/attilax
Atiend