string ParseClassMember_MethodParam(AstNode node) { string returntype = ""; foreach (var v in node.Children) { if (v is PrimitiveType) { returntype = Tools.getPrimitiveTypeName(v.ToString()); } else if (v is MemberType) { returntype = getMemberTypeName(v.ToString()); } else if (v is Identifier) { this.DefineValue(v.ToString(), returntype); Append(v.ToString() + ":" + returntype); } else { logger.LogError("not support ParseClassMember_MethodParam element:" + v.GetType().Name + "|" + v.NodeType + "|" + v.StartLocation.Line); } break; } return(returntype); }
void ParseClassMember_Property(AstNode property) { string returntype = ""; string methodname = ""; int getpublic = 0; int setpublic = 0; bool bstatic = false; bool get = false; bool set = false; AstNode getBlock = null; AstNode setBlock = null; foreach (var v in property.Children) { if (v is Comment) { Append(v.ToString().Replace("\r\n", "")); } else if (v is CSharpModifierToken) { if (v.ToString() == "public") { getpublic = setpublic = 1; } else if (v.ToString() == "private") { getpublic = setpublic = -1; } else if (v.ToString() == "static") { bstatic = true; } else { logger.LogWarnning("not support modifier,skip it:" + v.ToString() + "|" + v.StartLocation.Line); } } else if (v is PrimitiveType) { returntype = Tools.getPrimitiveTypeName(v.ToString()); } else if (v is MemberType) { returntype = getMemberTypeName(v.ToString()); } else if (v is Identifier) { methodname = v.ToString(); } else if (v is CSharpTokenNode) { if (v.ToString() == "{")//param begin; { //property begin; } else if (v.ToString() == "}")//paramend; { //property End; var typeinfo = proj.types[getCurClassName()]; if (typeinfo.classtype == "interface") {//接口不嵌入代码 if (get) { if (getpublic < 0) { Append("private "); } else if (getpublic > 0) { Append("public "); } if (bstatic) { Append("static "); } //只能转化成函数了,typescript interface 不支持访问器 Append("get_" + methodname + "():" + returntype + ";"); AppendLine(); } if (set) { if (setpublic < 0) { Append("private "); } else if (setpublic > 0) { Append("public "); } if (bstatic) { Append("static "); } Append("set_" + methodname + "(value:" + returntype + ");"); AppendLine(); } } else {//嵌入代码 if (getBlock == null && setBlock == null) { Append("_" + methodname + ":" + returntype + ";"); AppendLine(); } if (get) { if (getpublic < 0) { Append("private "); } else if (getpublic > 0) { Append("public "); } if (bstatic) { Append("static "); } Append("get_" + methodname + "():" + returntype); AppendLine(); if (getBlock != null) { ParseBlockStatement(getBlock, null); AppendLine(); } else {//自动嵌入 Append("{"); AppendLine(); Append(" return this._" + methodname + ";"); AppendLine(); Append("}"); AppendLine(); } } if (set) { if (setpublic < 0) { Append("private "); } else if (setpublic > 0) { Append("public "); } if (bstatic) { Append("static "); } Append("set_" + methodname + "(value:" + returntype + ")"); AppendLine(); if (setBlock != null) { ExpressOption option = new ExpressOption(); option.insetfunc = returntype; ParseBlockStatement(setBlock, option); AppendLine(); } else {//自动嵌入 Append("{"); AppendLine(); Append(" this._" + methodname + "=value;"); AppendLine(); Append("}"); AppendLine(); } } } } else { Append(v.ToString()); } } else if (v is NewLineNode) { //AppendLine(); } else if (v is Accessor) { bool _get = false; foreach (var sv in v.Children) { int m = 0; if (sv is CSharpModifierToken) { if (sv.ToString() == "public") { m = 1; } else if (sv.ToString() == "private") { m = -1; } } else if (sv is CSharpTokenNode) { if (sv.ToString() == "get") { _get = true; get = true; if (m != 0) { getpublic = m; } } else if (sv.ToString() == "set") { _get = false; set = true; if (m != 0) { setpublic = m; } } } else if (sv is BlockStatement) { if (_get) { getBlock = sv; } else { setBlock = sv; } } } } else { logger.LogError("not support class property element:" + v.GetType().Name + "|" + v.NodeType + "|" + v.StartLocation.Line); } } }
///field /// void ParseClassMember_Field(AstNode field) { string returntype = ""; int bpublic = 0; bool bstatic = false; foreach (var v in field.Children) { if (v is Comment) { Append(v.ToString().Replace("\r\n", "")); } else if (v is CSharpModifierToken) { if (v.ToString() == "public") { bpublic = 1; } else if (v.ToString() == "private") { bpublic = -1; } else if (v.ToString() == "static") { bstatic = true; } else { logger.LogWarnning("not support modifier,skip it:" + v.ToString() + "|" + v.StartLocation.Line); } } else if (v is PrimitiveType) { returntype = Tools.getPrimitiveTypeName(v.ToString()); } else if (v is MemberType) { returntype = getMemberTypeName(v.ToString()); } else if (v is VariableInitializer) { if (bpublic < 0) { Append("private "); } else if (bpublic > 0) { Append("public "); } if (bstatic) { Append("static "); } ParseVariableInitializer(v as VariableInitializer, returntype); } else if (v is CSharpTokenNode) { if (v.ToString() == ";") { Append(";"); break; } } else if (v is NewLineNode) { AppendLine(); } else { logger.LogError("not support class field element:" + v.GetType().Name + "|" + v.NodeType + "|" + v.StartLocation.Line); } break; } }
string ParseClassMember_Method(AstNode method, bool bConstructor) { string returntype = ""; string methodname = null; int bpublic = 0; bool bstatic = false; List <string> _param = new List <string>(); StringBuilder old = null; foreach (var v in method.Children) { if (v is Comment) { Append(v.ToString().Replace("\r\n", "")); } else if (v is CSharpModifierToken) { if (v.ToString() == "public") { bpublic = 1; } else if (v.ToString() == "private") { bpublic = -1; } else if (v.ToString() == "static") { bstatic = true; } else { logger.LogWarnning("not support modifier,skip it:" + v.ToString() + "|" + v.StartLocation.Line); } } else if (v is PrimitiveType) { returntype = Tools.getPrimitiveTypeName(v.ToString()); } else if (v is MemberType) { returntype = getMemberTypeName(v.ToString()); } else if (v is Identifier) { methodname = v.ToString(); if (bpublic < 0) { Append("private "); } else if (bpublic > 0) { Append("public "); } if (bstatic) { Append("static "); } } else if (v is CSharpTokenNode) { if (v.ToString() == "(")//param begin; { Append(""); old = this.builder; this.builder = new StringBuilder(); Append("("); this.PushValueLayer(); } else if (v.ToString() == ")")//paramend; { if (string.IsNullOrEmpty(returntype)) { Append(")"); } else { Append("):" + returntype); } string paramstr = builder.ToString(); this.builder = old; if (bConstructor) { Append("constructor"); } else { var _method = getMethodInfoByMember(getCurClassName(), methodname, _param.ToArray()); Append(_method.olname); } Append(paramstr); } else if (v.ToString() == ";") { Append(";"); break; } else { Append(v.ToString()); } } else if (v is NewLineNode) { AppendLine(); } else if (v is BlockStatement) { ParseBlockStatement(v, null);//代码执行完毕 this.PopValueLayer(); } else if (v is ParameterDeclaration) { string ptype = ParseClassMember_MethodParam(v); _param.Add(ptype); } else { logger.LogError("not support class method element:" + v.GetType().Name + "|" + v.NodeType + "|" + v.StartLocation.Line); } } return(returntype); }
/// <summary> /// 各种表达式 /// </summary> /// <param name="expr"></param> string ParseObjectCreateExpression(AstNode expr) { string newtype = ""; foreach (var v in expr.Children) { if (v is Comment) { Append(v.ToString().Replace("\r\n", "")); } else if (v is PrimitiveType) { newtype = Tools.getPrimitiveTypeName(v.ToString()); Append(newtype); } else if (v is MemberType) { newtype = getMemberTypeName(v.ToString()); Append(newtype); } else if (v is SimpleType) { newtype = getSimpleTypeName((v as SimpleType).Identifier); Append(newtype); } else if (v is CSharpTokenNode) { if (v.ToString() == "(")//param begin; { Append("("); } else if (v.ToString() == ")")//paramend; { Append(")"); } else if (v.ToString() == "new") { Append("new "); } else { Append(v.ToString()); } } else if (v is NewLineNode) { AppendLine(); } else if (v is BlockStatement) { ParseBlockStatement(v, null); } else if (v is IdentifierExpression) {//add param ParseIdentifierExpression(v as IdentifierExpression, null); } else { logger.LogError("not support ParseObjectCreateExpression element:" + v.GetType().Name + "|" + v.NodeType + "|" + v.StartLocation.Line); } } return newtype; }