From 6ec181ce6a9af37a31682b5cbf07e09516a10ad5 Mon Sep 17 00:00:00 2001 From: sunlian Date: Thu, 10 Apr 2025 16:14:06 +0800 Subject: [PATCH 1/4] fix parse and gen testcase Signed-off-by: sunlian --- .../typescript/TypeScriptCustomListener.java | 223 ++++++++++- .../ohosgen/src/main/java/gen/GenCppFile.java | 75 +++- .../src/main/java/grammar/ParamObj.java | 28 ++ .../src/main/java/grammar/TypeObj.java | 29 ++ .../ohosgen/src/main/java/parse/ParseTs.java | 1 + .../src/main/java/utils/StringUtils.java | 9 + .../ohosgen/src/main/java/utils/TsToken.java | 258 ++++++++++++ .../src/test/java/gen/GenCppFileTest.java | 186 ++++++++- .../src/test/java/parse/ParseTsTest.java | 371 +++++++++++++++++- 9 files changed, 1156 insertions(+), 24 deletions(-) diff --git a/src/intellij_plugin/ohosgen/src/main/java/antlr/typescript/TypeScriptCustomListener.java b/src/intellij_plugin/ohosgen/src/main/java/antlr/typescript/TypeScriptCustomListener.java index d4e68a2f..dde3fb77 100644 --- a/src/intellij_plugin/ohosgen/src/main/java/antlr/typescript/TypeScriptCustomListener.java +++ b/src/intellij_plugin/ohosgen/src/main/java/antlr/typescript/TypeScriptCustomListener.java @@ -48,6 +48,7 @@ public class TypeScriptCustomListener extends TypeScriptParserBaseListener imple private List typeObjList; private List unionObjList; private List interfaceObjList; + private List varObjList; /** * 构造函数 @@ -60,6 +61,7 @@ public class TypeScriptCustomListener extends TypeScriptParserBaseListener imple typeObjList = new CopyOnWriteArrayList<>(); unionObjList = new CopyOnWriteArrayList<>(); interfaceObjList = new CopyOnWriteArrayList<>(); + varObjList = new CopyOnWriteArrayList<>(); } /** @@ -233,6 +235,24 @@ public class TypeScriptCustomListener extends TypeScriptParserBaseListener imple this.unionObjList = unionObjList; } + /** + * 获取变量列表 + * + * @return 变量列表 + */ + public List getVarObjList() { + return varObjList; + } + + /** + * 设置变量列表 + * + * @param varObjList 变量列表 + */ + public void setVarObjList(List varObjList) { + this.varObjList = varObjList; + } + @Override public void enterFunctionType(TypeScriptParser.FunctionTypeContext ctx) { super.enterFunctionType(ctx); @@ -255,12 +275,70 @@ public class TypeScriptCustomListener extends TypeScriptParserBaseListener imple public void enterTypeAnnotation(TypeScriptParser.TypeAnnotationContext ctx) { super.enterTypeAnnotation(ctx); System.out.println("enterTypeAnnotation: " + ctx.getText()); + if (this.currentObject instanceof TypeObj to) { + if (ctx.type_() != null && ctx.type_().unionOrIntersectionOrPrimaryType() != null) { + TypeScriptParser. UnionOrIntersectionOrPrimaryTypeContext uipt = + ctx.type_().unionOrIntersectionOrPrimaryType(); + int cCnt = uipt.getChildCount(); + ParamObj lastPa = to.getLastParamObj(); + for (int i = 0; i < cCnt; i++) { + String keyStr = uipt.getChild(i).getText(); + if (!TsToken.isTsToken(keyStr)) { + lastPa.setStrValue(keyStr); + } else if (TsToken.isTsVarType(keyStr)){ + lastPa.setType(keyStr); + } + + } + } + } } @Override public void enterPropertyName(TypeScriptParser.PropertyNameContext ctx) { super.enterPropertyName(ctx); System.out.println("enterPropertyName: " + ctx.getText()); + if (this.currentObject instanceof TypeObj to) { + ParamObj pa = new ParamObj(); + pa.setName(ctx.getText()); + to.addParam(pa); + } + } + + @Override + public void enterPropertyExpressionAssignment(TypeScriptParser.PropertyExpressionAssignmentContext ctx) { + super.enterPropertyExpressionAssignment(ctx); + System.out.println("enterPropertyExpressionAssignment: " + ctx.getText()); + if (this.currentObject instanceof ParamObj po && !po.getType().contains(TsToken.TS_TOKEN_BRACKET)) { + ParamObj pa = new ParamObj(); + pa.setName(ctx.propertyName().getText()); + pa.setStrValue(ctx.singleExpression().getText()); + po.addParam(pa); + } + } + + @Override + public void enterArgumentsExpression(TypeScriptParser.ArgumentsExpressionContext ctx) { + super.enterArgumentsExpression(ctx); + System.out.println("enterArgumentsExpression: " + ctx.getText()); + } + + @Override + public void enterArgumentList(TypeScriptParser.ArgumentListContext ctx) { + super.enterArgumentList(ctx); + System.out.println("enterArgumentList: " + ctx.getText()); + } + + @Override + public void enterMultiplicativeExpression(TypeScriptParser.MultiplicativeExpressionContext ctx) { + super.enterMultiplicativeExpression(ctx); + System.out.println("enterMultiplicativeExpression: " + ctx.getText()); + } + + @Override + public void enterGeneratorFunctionDeclaration(TypeScriptParser.GeneratorFunctionDeclarationContext ctx) { + super.enterGeneratorFunctionDeclaration(ctx); + System.out.println("enterGeneratorFunctionDeclaration: " + ctx.getText()); } @Override @@ -351,6 +429,8 @@ public class TypeScriptCustomListener extends TypeScriptParserBaseListener imple } } else if (ctx.singleExpression() != null) { setVariableSingleExpression(ctx, varName); + } else { + System.out.println("else 变量名: " + varName); } System.out.println("------------------------------"); } @@ -359,6 +439,78 @@ public class TypeScriptCustomListener extends TypeScriptParserBaseListener imple public void enterExpressionStatement(TypeScriptParser.ExpressionStatementContext ctx) { super.enterExpressionStatement(ctx); System.out.println("enterExpressionStatement: " + ctx.getText()); + List secl = ctx.expressionSequence().singleExpression(); + for (TypeScriptParser.SingleExpressionContext secItem : secl) { + int cCnt = secItem.getChildCount(); + + } + } + + @Override + public void enterAssignmentExpression(TypeScriptParser.AssignmentExpressionContext ctx) { + super.enterAssignmentExpression(ctx); + System.out.println("enterAssignmentExpression: " + ctx.getText()); + List secl = ctx.singleExpression(); + if (secl.size() > 1) { + String name = secl.get(0).getText(); + String value = secl.get(1).getText(); + ParamObj pa = new ParamObj(); + pa.setName(name); + pa.setStrValue(value); + this.varObjList.add(pa); + } + } + + @Override + public void enterGeneratorsExpression(TypeScriptParser.GeneratorsExpressionContext ctx) { + super.enterGeneratorsExpression(ctx); + System.out.println("enterGeneratorsExpression: " + ctx.getText()); + } + + @Override + public void enterObjectLiteral(TypeScriptParser.ObjectLiteralContext ctx) { + super.enterObjectLiteral(ctx); + System.out.println("enterObjectLiteral: " + ctx.getText()); + if (this.currentObject instanceof ParamObj pa && pa.getType().contains(TsToken.TS_TOKEN_BRACKET)) { + List pacl = ctx.propertyAssignment(); + ParamObj paObj = new ParamObj(); + for (TypeScriptParser.PropertyAssignmentContext item : pacl) { + int cCnt = item.getChildCount(); + if (cCnt > 2) { + ParamObj paItem = new ParamObj(); + String nameStr = item.getChild(0).getText(); + String valStr = item.getChild(2).getText(); + paItem.setName(nameStr); + paItem.setStrValue(valStr); + paObj.addParam(paItem); + } + } + pa.addParam(paObj); + } + } + + @Override + public void enterArrayLiteralExpression(TypeScriptParser.ArrayLiteralExpressionContext ctx) { + super.enterArrayLiteralExpression(ctx); + System.out.println("enterArrayLiteralExpression: " + ctx.getText()); + } + + @Override + public void enterArrayLiteral(TypeScriptParser.ArrayLiteralContext ctx) { + super.enterArrayLiteral(ctx); + System.out.println("enterArrayLiteral: " + ctx.getText()); + } + + @Override + public void enterComputedPropertyExpressionAssignment(TypeScriptParser.ComputedPropertyExpressionAssignmentContext ctx) { + super.enterComputedPropertyExpressionAssignment(ctx); + System.out.println("enterComputedPropertyExpressionAssignment: " + ctx.getText()); + } + + @Override + public void enterParenthesizedExpression(TypeScriptParser.ParenthesizedExpressionContext ctx) { + super.enterParenthesizedExpression(ctx); + System.out.println("enterParenthesizedExpression: " + ctx.getText()); } @Override @@ -742,6 +894,11 @@ public class TypeScriptCustomListener extends TypeScriptParserBaseListener imple super.enterTypeAliasDeclaration(ctx); String typeName = ctx.identifier().getText(); System.out.println("Type: " + typeName); + TypeObj to = new TypeObj(); + to.setName(typeName); + this.typeObjList.add(to); + this.currentObject = to; + this.currentToken = TsToken.TS_TOKEN_TYPE; TypeScriptParser.TypeParametersContext tpc = ctx.typeParameters(); if (tpc != null) { System.out.println("Type params: " + tpc.getText()); @@ -752,8 +909,19 @@ public class TypeScriptCustomListener extends TypeScriptParserBaseListener imple TypeScriptParser.UnionOrIntersectionOrPrimaryTypeContext upt = typeContext.unionOrIntersectionOrPrimaryType(); - if (upt != null) { + if (upt != null && upt.getChildCount() > 1) { System.out.println("Type uoiop: " + upt.getText()); + int cCnt = upt.getChildCount(); + ParamObj pa = new ParamObj(); + for (int i = 0; i < cCnt; i++) { + String keyStr = upt.getChild(i).getText(); + if (!TsToken.isTsToken(keyStr)) { + pa.setStrValue(keyStr); + } else if (TsToken.isTsVarType(keyStr)){ + pa.setType(keyStr); + } + } + to.addParam(pa); } TypeScriptParser.TypeGenericContext tgc = typeContext.typeGeneric(); if (tgc != null) { @@ -1016,7 +1184,6 @@ public class TypeScriptCustomListener extends TypeScriptParserBaseListener imple if (afdc.arrowFunctionParameters().formalParameterList() != null) { List fpacl = afdc.arrowFunctionParameters().formalParameterList().formalParameterArg(); - for (TypeScriptParser.FormalParameterArgContext fpac : fpacl) { String name = fpac.assignable().getText(); String type = fpac.typeAnnotation() != null ? @@ -1025,6 +1192,17 @@ public class TypeScriptCustomListener extends TypeScriptParserBaseListener imple System.out.println("addparam: " + fo.toJsonString()); } } + + if (afdc.arrowFunctionParameters().formalParameterList() != null && + afdc.arrowFunctionParameters().formalParameterList().lastFormalParameterArg() != null) { + TypeScriptParser.LastFormalParameterArgContext lpac = + afdc.arrowFunctionParameters().formalParameterList().lastFormalParameterArg(); + + String paType = lpac.typeAnnotation().type_().getText(); + String paName = lpac.identifier().getText(); + paName = lpac.Ellipsis() != null ? lpac.Ellipsis().getText() + paName : paName; + fo.addParam(paName, paType); + } } private FuncObj createFuncObj(String varName) { @@ -1063,20 +1241,39 @@ public class TypeScriptCustomListener extends TypeScriptParserBaseListener imple continue; } - FuncObj fo = createFuncObj(varName); - fo.setName(varType); - - if (!(secItem instanceof TypeScriptParser.GenericTypesContext gtc)) { - continue; - } + if (secItem instanceof TypeScriptParser.GenericTypesContext gtc) { + FuncObj fo = createFuncObj(varName); + fo.setName(varType); - String temp = gtc.typeArguments().getText(); - fo.addTemplate(temp); + String temp = gtc.typeArguments().getText(); + fo.addTemplate(temp); - TypeScriptParser.ExpressionSequenceContext esc = gtc.expressionSequence(); - List secl = esc.singleExpression(); - setFuncParamStr(fo, secl); + TypeScriptParser.ExpressionSequenceContext esc = gtc.expressionSequence(); + List secl = esc.singleExpression(); + setFuncParamStr(fo, secl); + } + if (secItem instanceof TypeScriptParser.ParenthesizedExpressionContext pec) { + ParamObj paObj = new ParamObj(); + paObj.setName(ctx.identifierOrKeyWord().getText()); + paObj.setStrValue(sec.getText()); + this.varObjList.add(paObj); + } + } else { + System.out.println("const 变量名: " + varName); + ParamObj pa = new ParamObj(); + pa.setName(varName); + String typeName = (ctx.typeAnnotation() != null && ctx.typeAnnotation().type_() != null) ? + ctx.typeAnnotation().type_().getText() : ""; + pa.setType(typeName); + int cCnt = typeName.contains(TsToken.TS_TOKEN_BRACKET) ? + 0 : sec.getChildCount(); + for (int i = 0; i < cCnt; i++) { + pa.setStrValue(sec.getChild(i).getText()); + } + this.varObjList.add(pa); + this.currentObject = pa; + this.currentToken = TsToken.TS_TOKEN_VAR; } } } diff --git a/src/intellij_plugin/ohosgen/src/main/java/gen/GenCppFile.java b/src/intellij_plugin/ohosgen/src/main/java/gen/GenCppFile.java index 0b9c0439..22ba0e03 100644 --- a/src/intellij_plugin/ohosgen/src/main/java/gen/GenCppFile.java +++ b/src/intellij_plugin/ohosgen/src/main/java/gen/GenCppFile.java @@ -89,6 +89,8 @@ public class GenCppFile extends GeneratorBase { private static final String CPP_UNDER_LINE = "_"; private static final String CPP_SEMICOLON = ";"; private static final String CPP_COLON = ":"; + private static final String CPP_ELLIPSIS = "..."; + private static final String CPP_DOT = "."; private static final String CPP_LEFT_BRACE = "{"; private static final String CPP_RIGHT_BRACE = "}"; private static final String CPP_LEFT_PARENTHESES = "("; @@ -98,11 +100,25 @@ public class GenCppFile extends GeneratorBase { private static final String CPP_LEFT_ANGLE_BRACKET = "<"; private static final String CPP_RIGHT_ANGLE_BRACKET = ">"; + private static final String CPP_STD_STRING = "std::string"; + private static final String CPP_STD_VECTOR = "std::vector"; + private static final String CPP_STD_LIST = "std::list"; + private static final String CPP_STD_ARRAY = "std::array"; + private static final String CPP_STD_STACK = "std::stack"; + private static final String CPP_STD_QUEUE = "std::queue"; + private static final String CPP_STD_PAIR = "std::pair"; + private static final String CPP_STD_MAP = "std::map"; + private static final String CPP_STD_SET = "std::set"; + private static final String CPP_STD_DEQUE = "std::deque"; + private static final String CPP_STD_MULTIMAP = "std::multimap"; + private static final String CPP_STD_MULTISET = "std::multiset"; + private static final String CPP_STR_SUFFIX = "STR"; private static final String CPP_FILE_PREFIX = "ag_"; private static final String CPP_FILE_H_SUFFIX = ".h"; private static final String CPP_FILE_CPP_SUFFIX = ".cpp"; private static final String CPP_FILE_C_SUFFIX = ".c"; + private static final String CPP_STRUCT_SUFFIX = "ST"; private String interfaceContent = ""; private String enumContent = ""; @@ -605,14 +621,61 @@ public class GenCppFile extends GeneratorBase { String resContent = ""; for (ParamObj po : pol) { String paName = po.getName(); - String paType = ts2CppKey(po.getType()); + String paType = ts2CppKey(po.getType()).isEmpty() ? CPP_AUTO_TOKEN : ts2CppKey(po.getType()); String paValue = po.getStrValue(0); - int i = 0; - resContent += CPP_NEW_LINE + CPP_CONST_TOKEN + - CPP_BLANK_SPACE + paType + CPP_BLANK_SPACE + paName + - CPP_EQUAL + paValue; + List paList = po.getPaList(); + if (paList.isEmpty()) { + resContent += CPP_NEW_LINE + CPP_EXTENDS_TOKEN + CPP_BLANK_SPACE + CPP_CONST_TOKEN + + CPP_BLANK_SPACE + paType + CPP_BLANK_SPACE + paName + + CPP_EQUAL + paValue; + + resContent += CPP_SEMICOLON + CPP_NEW_LINE; + } else if (paList.get(0).getPaList().isEmpty()) { + String valType = StringUtils.isAllDigits(paList.get(0).getStrValue(0)) ? + CPP_NUMBER_TOKEN : CPP_STD_STRING; + resContent += CPP_NEW_LINE + CPP_EXTENDS_TOKEN + CPP_BLANK_SPACE + CPP_CONST_TOKEN + + CPP_BLANK_SPACE + CPP_STD_MAP + CPP_LEFT_ANGLE_BRACKET + CPP_STD_STRING + + CPP_COMMA + CPP_BLANK_SPACE + valType + CPP_RIGHT_BRACE + CPP_BLANK_SPACE + + paName + CPP_EQUAL + CPP_LEFT_BRACE; + for (ParamObj paItem : paList) { + String pName = paItem.getName(); + String pVal = paItem.getStrValue(0); + resContent += CPP_NEW_LINE + CPP_TAB_SPACE + CPP_LEFT_BRACE + CPP_DOUBLE_QUOTATION + + pName + CPP_DOUBLE_QUOTATION + CPP_COMMA + CPP_BLANK_SPACE + pVal + + CPP_RIGHT_BRACE + CPP_COMMA; + } + resContent = StringUtils.removeLastCharacter(resContent, 1); + resContent += CPP_NEW_LINE + CPP_RIGHT_BRACE + CPP_SEMICOLON + CPP_NEW_LINE; + } else if (!(paList.get(0).getPaList().isEmpty())) { + resContent += CPP_NEW_LINE + CPP_STRUCT_TOKEN + CPP_BLANK_SPACE + paName + + CPP_STRUCT_SUFFIX + CPP_BLANK_SPACE + CPP_LEFT_BRACE; + List paramList = paList.get(0).getPaList(); + for (ParamObj paItem : paramList) { + String paStr = paItem.getName(); + String paVal = paItem.getStrValue(0); + String typeStr = StringUtils.isAllDigits(paVal) ? + CPP_NUMBER_TOKEN : CPP_STD_STRING; + typeStr = StringUtils.isBoolean(paVal) ? CPP_BOOLEAN_TOKEN : typeStr; + resContent += CPP_NEW_LINE + CPP_TAB_SPACE + typeStr + CPP_BLANK_SPACE + paStr + CPP_SEMICOLON; + } + resContent += CPP_NEW_LINE + CPP_RIGHT_BRACE + CPP_SEMICOLON + CPP_NEW_LINE; + + resContent += CPP_NEW_LINE + CPP_CONST_TOKEN + CPP_BLANK_SPACE + CPP_STD_VECTOR + + CPP_LEFT_ANGLE_BRACKET + paName + CPP_STRUCT_SUFFIX + CPP_RIGHT_ANGLE_BRACKET + + CPP_BLANK_SPACE + paName + CPP_EQUAL + CPP_LEFT_BRACE; + for (ParamObj paramListItem : paList) { + List subParamList = paramListItem.getPaList(); + resContent += CPP_NEW_LINE + CPP_TAB_SPACE + CPP_LEFT_BRACE; + for (ParamObj paItem : subParamList) { + String paVal = paItem.getStrValue(0); + resContent += paVal + CPP_COMMA + CPP_BLANK_SPACE; + } + resContent = StringUtils.removeLastCharacter(resContent, 2); + resContent += CPP_RIGHT_BRACE + CPP_COMMA; + } + resContent += CPP_NEW_LINE + CPP_RIGHT_BRACE + CPP_SEMICOLON + CPP_NEW_LINE; + } - resContent += CPP_SEMICOLON + CPP_NEW_LINE; } this.constContent = resContent; System.out.println("genVarList : " + resContent); diff --git a/src/intellij_plugin/ohosgen/src/main/java/grammar/ParamObj.java b/src/intellij_plugin/ohosgen/src/main/java/grammar/ParamObj.java index 5dfeea44..0105fc1d 100644 --- a/src/intellij_plugin/ohosgen/src/main/java/grammar/ParamObj.java +++ b/src/intellij_plugin/ohosgen/src/main/java/grammar/ParamObj.java @@ -65,12 +65,18 @@ public class ParamObj extends GBaseObject { */ private List vList; + /** + * 结构体初始化 + */ + private List paList; + /** * 构造函数 */ public ParamObj() { this.decorator = TsToken.TS_TOKEN_REQUIRED; vList = new CopyOnWriteArrayList<>(); + paList = new CopyOnWriteArrayList<>(); } /** @@ -243,4 +249,26 @@ public class ParamObj extends GBaseObject { public void setAsList(int[] asList) { this.asList = asList; } + + /** + * 获取初始化结构 + * + * @return 初始化结构 + */ + public List getPaList() { + return paList; + } + + /** + * 设置初始化结构 + * + * @param paList 初始化结构 + */ + public void setPaList(List paList) { + this.paList = paList; + } + + public void addParam(ParamObj pa) { + this.paList.add(pa); + } } diff --git a/src/intellij_plugin/ohosgen/src/main/java/grammar/TypeObj.java b/src/intellij_plugin/ohosgen/src/main/java/grammar/TypeObj.java index 559a5d74..3a76dd74 100644 --- a/src/intellij_plugin/ohosgen/src/main/java/grammar/TypeObj.java +++ b/src/intellij_plugin/ohosgen/src/main/java/grammar/TypeObj.java @@ -33,6 +33,7 @@ public class TypeObj extends GBaseObject { private String name; private String alias; private List paramList; + private ParamObj lastParamObj; private List funcList; private List typeList; @@ -165,4 +166,32 @@ public class TypeObj extends GBaseObject { public void addTypeValue(String value) { this.typeList.add(value); } + + /** + * 增加参数对象 + * + * @param pa 参数对象 + */ + public void addParam(ParamObj pa) { + this.paramList.add(pa); + this.lastParamObj = pa; + } + + /** + * 获取最近的参数对象 + * + * @return 最近参数对象 + */ + public ParamObj getLastParamObj() { + return this.lastParamObj; + } + + /** + * 设置最近参数对象 + * + * @param lastParamObj 最近参数对象 + */ + public void setLastParamObj(ParamObj lastParamObj) { + this.lastParamObj = lastParamObj; + } } diff --git a/src/intellij_plugin/ohosgen/src/main/java/parse/ParseTs.java b/src/intellij_plugin/ohosgen/src/main/java/parse/ParseTs.java index 6110b474..6371319d 100644 --- a/src/intellij_plugin/ohosgen/src/main/java/parse/ParseTs.java +++ b/src/intellij_plugin/ohosgen/src/main/java/parse/ParseTs.java @@ -165,6 +165,7 @@ public class ParseTs extends ParseBase implements CustomEventListener { po.setStructList(tcl.getStructObjList()); po.setTypeList(tcl.getTypeObjList()); po.setUnionList(tcl.getUnionObjList()); + po.setVarList(tcl.getVarObjList()); return po; } diff --git a/src/intellij_plugin/ohosgen/src/main/java/utils/StringUtils.java b/src/intellij_plugin/ohosgen/src/main/java/utils/StringUtils.java index 81cdb08e..3acf988f 100644 --- a/src/intellij_plugin/ohosgen/src/main/java/utils/StringUtils.java +++ b/src/intellij_plugin/ohosgen/src/main/java/utils/StringUtils.java @@ -71,4 +71,13 @@ public class StringUtils { } return str; } + + public static boolean isAllDigits(String str) { + return str != null && str.matches("\\d+"); + } + + public static boolean isBoolean(String str) { + String lowerStr = str.toLowerCase(Locale.ROOT); + return lowerStr.equals("true") || lowerStr.equals("false"); + } } diff --git a/src/intellij_plugin/ohosgen/src/main/java/utils/TsToken.java b/src/intellij_plugin/ohosgen/src/main/java/utils/TsToken.java index f8530c2a..8308689c 100644 --- a/src/intellij_plugin/ohosgen/src/main/java/utils/TsToken.java +++ b/src/intellij_plugin/ohosgen/src/main/java/utils/TsToken.java @@ -15,6 +15,8 @@ package utils; +import java.util.List; + /** *

类名:该类用于xxx

* description typescript token @@ -376,4 +378,260 @@ public class TsToken { */ public static final String TS_TOKEN_YIELD = "yield"; + /** + * less than token + */ + public static final String TS_TOKEN_LessThan = "<"; + + /** + * more than token + */ + public static final String TS_TOKEN_MoreThan = ">"; + + /** + * less than equals token + */ + public static final String TS_TOKEN_LessThanEquals = "<="; + + /** + * greater than equals token + */ + public static final String TS_TOKEN_GreaterThanEquals = ">="; + + /** + * equals token + */ + public static final String TS_TOKEN_Equals = "=="; + + /** + * not equals token + */ + public static final String TS_TOKEN_NotEquals = "!="; + + /** + * identity equals token + */ + public static final String TS_TOKEN_IdentityEquals = "==="; + + /** + * identity not equals token + */ + public static final String TS_TOKEN_IdentityNotEquals = "!=="; + + /** + * bit and token + */ + public static final String TS_TOKEN_BitAnd = "&"; + + /** + * BitXOr token + */ + public static final String TS_TOKEN_BitXOr = "^"; + + /** + * BitOr token + */ + public static final String TS_TOKEN_BitOr = "|"; + + /** + * And token + */ + public static final String TS_TOKEN_And= "&&"; + + /** + * Or token + */ + public static final String TS_TOKEN_Or = "||"; + + /** + * MultiplyAssign token + */ + public static final String TS_TOKEN_MultiplyAssign = "*="; + + /** + * DivideAssign token + */ + public static final String TS_TOKEN_DivideAssign = "/="; + + /** + * ModulusAssign token + */ + public static final String TS_TOKEN_ModulusAssign = "%="; + + /** + * PlusAssign token + */ + public static final String TS_TOKEN_PlusAssign = "+="; + + /** + * MinusAssign token + */ + public static final String TS_TOKEN_MinusAssign = "-="; + + /** + * LeftShiftArithmeticAssign token + */ + public static final String TS_TOKEN_LeftShiftArithmeticAssign = "<<="; + + /** + * RightShiftArithmeticAssign token + */ + public static final String TS_TOKEN_RightShiftArithmeticAssign = ">>="; + + /** + * RightShiftLogicalAssign token + */ + public static final String TS_TOKEN_RightShiftLogicalAssign = ">>>="; + + /** + * BitAndAssign token + */ + public static final String TS_TOKEN_BitAndAssign = "&="; + + /** + * BitXorAssign token + */ + public static final String TS_TOKEN_BitXorAssign = "^="; + + /** + * BitOrAssign token + */ + public static final String TS_TOKEN_BitOrAssign = "|="; + + /** + * PowerAssign token + */ + public static final String TS_TOKEN_PowerAssign = "**="; + + /** + * NullishCoalescingAssign token + */ + public static final String TS_TOKEN_NullishCoalescingAssign = "??="; + + /** + * await token + */ + public static final String TS_TOKEN_ARROW = "=>"; + + /** + * bracket token + */ + public static final String TS_TOKEN_BRACKET = "[]"; + + public static final List tsTokenList = List.of( + TS_TOKEN_OPTIONAL, + TS_TOKEN_REQUIRED, + TS_TOKEN_REST_PARAM, + TS_TOKEN_ENUM, + TS_TOKEN_CLASS, + TS_TOKEN_EXTENDS, + TS_TOKEN_SUPER, + TS_TOKEN_CONST, + TS_TOKEN_EXPORT, + TS_TOKEN_IMPORT, + TS_TOKEN_IMPLEMENT, + TS_TOKEN_LET, + TS_TOKEN_PRIVATE, + TS_TOKEN_PUBLIC, + TS_TOKEN_INTERFACE, + TS_TOKEN_PACKAGE, + TS_TOKEN_PROTECTED, + TS_TOKEN_STATIC, + TS_TOKEN_ANY, + TS_TOKEN_NUMBER, + TS_TOKEN_NEVER, + TS_TOKEN_BOOLEAN, + TS_TOKEN_STRING, + TS_TOKEN_UNIQUE, + TS_TOKEN_SYMBOL, + TS_TOKEN_UNDEFINED, + TS_TOKEN_OBJECT, + TS_TOKEN_OF, + TS_TOKEN_KEYOF, + TS_TOKEN_TYPE, + TS_TOKEN_CONSTRUCTOR, + TS_TOKEN_NAMESPACE, + TS_TOKEN_REQUIRE, + TS_TOKEN_MODULE, + TS_TOKEN_DECLARE, + TS_TOKEN_ABSTRACT, + TS_TOKEN_IS, + TS_TOKEN_NULL, + TS_TOKEN_BREAK, + TS_TOKEN_DO, + TS_TOKEN_INSTANCEOF, + TS_TOKEN_TYPEOF, + TS_TOKEN_CASE, + TS_TOKEN_ELSE, + TS_TOKEN_NEW, + TS_TOKEN_VAR, + TS_TOKEN_CATCH, + TS_TOKEN_FINALLY, + TS_TOKEN_RETURN, + TS_TOKEN_VOID, + TS_TOKEN_CONTINUE, + TS_TOKEN_FOR, + TS_TOKEN_SWITCH, + TS_TOKEN_WHILE, + TS_TOKEN_DEBUGGER, + TS_TOKEN_FUNCTION, + TS_TOKEN_THIS, + TS_TOKEN_WITH, + TS_TOKEN_DEFAULT, + TS_TOKEN_IF, + TS_TOKEN_THROW, + TS_TOKEN_DELETE, + TS_TOKEN_IN, + TS_TOKEN_TRY, + TS_TOKEN_AS, + TS_TOKEN_FROM, + TS_TOKEN_READONLY, + TS_TOKEN_ASYNC, + TS_TOKEN_AWAIT, + TS_TOKEN_YIELD, + TS_TOKEN_LessThan, + TS_TOKEN_MoreThan, + TS_TOKEN_LessThanEquals, + TS_TOKEN_GreaterThanEquals, + TS_TOKEN_Equals, + TS_TOKEN_NotEquals, + TS_TOKEN_IdentityEquals, + TS_TOKEN_IdentityNotEquals, + TS_TOKEN_BitAnd, + TS_TOKEN_BitXOr, + TS_TOKEN_BitOr, + TS_TOKEN_And, + TS_TOKEN_Or, + TS_TOKEN_MultiplyAssign, + TS_TOKEN_DivideAssign, + TS_TOKEN_ModulusAssign, + TS_TOKEN_PlusAssign, + TS_TOKEN_MinusAssign, + TS_TOKEN_LeftShiftArithmeticAssign, + TS_TOKEN_RightShiftArithmeticAssign, + TS_TOKEN_RightShiftLogicalAssign, + TS_TOKEN_BitAndAssign, + TS_TOKEN_BitXorAssign, + TS_TOKEN_BitOrAssign, + TS_TOKEN_PowerAssign, + TS_TOKEN_NullishCoalescingAssign, + TS_TOKEN_ARROW + ); + + public static final List tsVarTypeList = List.of( + TS_TOKEN_ANY, + TS_TOKEN_NUMBER, + TS_TOKEN_NEVER, + TS_TOKEN_BOOLEAN, + TS_TOKEN_STRING + ); + + public static boolean isTsToken(String key) { + return TsToken.tsTokenList.contains(key); + } + + public static boolean isTsVarType(String key) { + return TsToken.tsVarTypeList.contains(key); + } + } diff --git a/src/intellij_plugin/ohosgen/src/test/java/gen/GenCppFileTest.java b/src/intellij_plugin/ohosgen/src/test/java/gen/GenCppFileTest.java index 9efb0b8f..10d74561 100644 --- a/src/intellij_plugin/ohosgen/src/test/java/gen/GenCppFileTest.java +++ b/src/intellij_plugin/ohosgen/src/test/java/gen/GenCppFileTest.java @@ -839,6 +839,183 @@ class GenCppFileTest { } } + @Test + void getVarContent1() { + ParamObj paObj = new ParamObj(); + paObj.setName("employeeName"); + paObj.setStrValue("\"John\""); + + List pol = new CopyOnWriteArrayList<>(); + pol.add(paObj); + ParseObj po = new ParseObj(); + po.setVarList(pol); + GeneratorBase gb = GenerateFactory.getGenerator("CPP"); + gb.genVarList(po.getVarList()); + + if (gb instanceof GenCppFile gdf) { + String constContent = gdf.getConstContent(); + System.out.println("getVar: " + constContent); + String expect = "\nextends const auto employeeName = \"John\";\n"; + assertEquals(expect, constContent); + } + } + + @Test + void getVarContent2() { + ParamObj paObj = new ParamObj(); + paObj.setName("employeeName"); + paObj.setType("string"); + paObj.setStrValue("\"John\""); + + List pol = new CopyOnWriteArrayList<>(); + pol.add(paObj); + ParseObj po = new ParseObj(); + po.setVarList(pol); + GeneratorBase gb = GenerateFactory.getGenerator("CPP"); + gb.genVarList(po.getVarList()); + + if (gb instanceof GenCppFile gdf) { + String constContent = gdf.getConstContent(); + System.out.println("getVar: " + constContent); + String expect = "\nextends const char* employeeName = \"John\";\n"; + assertEquals(expect, constContent); + } + } + + @Test + void getVarContent3() { + ParamObj paObj = new ParamObj(); + paObj.setName("num1"); + paObj.setType("number"); + paObj.setStrValue("1"); + + List pol = new CopyOnWriteArrayList<>(); + pol.add(paObj); + ParseObj po = new ParseObj(); + po.setVarList(pol); + GeneratorBase gb = GenerateFactory.getGenerator("CPP"); + gb.genVarList(po.getVarList()); + + if (gb instanceof GenCppFile gdf) { + String constContent = gdf.getConstContent(); + System.out.println("getVar: " + constContent); + String expect = "\nextends const int num1 = 1;\n"; + assertEquals(expect, constContent); + } + } + + @Test + void getVarContent4() { + ParamObj paObj = new ParamObj(); + paObj.setName("playerCodes"); + + ParamObj paItem1 = new ParamObj(); + paItem1.setName("player1"); + paItem1.setStrValue("9"); + paObj.addParam(paItem1); + ParamObj paItem2 = new ParamObj(); + paItem2.setName("player2"); + paItem2.setStrValue("10"); + paObj.addParam(paItem2); + + List pol = new CopyOnWriteArrayList<>(); + pol.add(paObj); + ParseObj po = new ParseObj(); + po.setVarList(pol); + GeneratorBase gb = GenerateFactory.getGenerator("CPP"); + gb.genVarList(po.getVarList()); + + if (gb instanceof GenCppFile gdf) { + String constContent = gdf.getConstContent(); + System.out.println("getVar: " + constContent); + String expect = "\nextends const std::map pol = new CopyOnWriteArrayList<>(); + pol.add(paObj); + ParseObj po = new ParseObj(); + po.setVarList(pol); + GeneratorBase gb = GenerateFactory.getGenerator("CPP"); + gb.genVarList(po.getVarList()); + + if (gb instanceof GenCppFile gdf) { + String constContent = gdf.getConstContent(); + System.out.println("getVar: " + constContent); + String expect = "\nextends const auto playerCodes.player2 = 11;\n"; + assertEquals(expect, constContent); + } + } + + @Test + void getVarContent6() { + ParamObj paObj = new ParamObj(); + paObj.setName("ROUTES"); + paObj.setType("any[]"); + + ParamObj paListItem1 = new ParamObj(); + ParamObj paItem1 = new ParamObj(); + paItem1.setName("path"); + paItem1.setStrValue("'/dashboard'"); + paListItem1.addParam(paItem1); + ParamObj paItem2 = new ParamObj(); + paItem2.setName("title"); + paItem2.setStrValue("'Dashboard'"); + paListItem1.addParam(paItem2); + ParamObj paItem3 = new ParamObj(); + paItem3.setName("allowAnonymous"); + paItem3.setStrValue("false"); + paListItem1.addParam(paItem3); + paObj.addParam(paListItem1); + + ParamObj paListItem2 = new ParamObj(); + ParamObj paItem21 = new ParamObj(); + paItem21.setName("path"); + paItem21.setStrValue("'/deals'"); + paListItem2.addParam(paItem21); + ParamObj paItem22 = new ParamObj(); + paItem22.setName("title"); + paItem22.setStrValue("'Deals'"); + paListItem2.addParam(paItem22); + ParamObj paItem23 = new ParamObj(); + paItem23.setName("allowAnonymous"); + paItem23.setStrValue("true"); + paListItem2.addParam(paItem23); + paObj.addParam(paListItem2); + + List pol = new CopyOnWriteArrayList<>(); + pol.add(paObj); + ParseObj po = new ParseObj(); + po.setVarList(pol); + GeneratorBase gb = GenerateFactory.getGenerator("CPP"); + gb.genVarList(po.getVarList()); + + if (gb instanceof GenCppFile gdf) { + String constContent = gdf.getConstContent(); + System.out.println("getVar: " + constContent); + String expect = "\nstruct ROUTESST {\n" + + "\tstd::string path;\n" + + "\tstd::string title;\n" + + "\tboolean allowAnonymous;\n" + + "};\n" + + "\n" + + "const std::vector ROUTES = {\n" + + "\t{'/dashboard', 'Dashboard', false},\n" + + "\t{'/deals', 'Deals', true},\n" + + "};\n"; + assertEquals(expect, constContent); + } + } @Test void getConstContent() { @@ -857,7 +1034,7 @@ class GenCppFileTest { if (gb instanceof GenCppFile gdf) { String varContent = gdf.getConstContent(); System.out.println("genVar: " + varContent); - String expect = "\nconst int TestParam = 100;\n"; + String expect = "\nextends const int TestParam = 100;\n"; assertEquals(expect, varContent); } } @@ -879,7 +1056,7 @@ class GenCppFileTest { if (gb instanceof GenCppFile gdf) { String varContent = gdf.getConstContent(); System.out.println("genVar: " + varContent); - String expect = "\nconst int TestParam = 100;\n"; + String expect = "\nextends const int TestParam = 100;\n"; assertEquals(expect, varContent); } } @@ -914,7 +1091,7 @@ class GenCppFileTest { if (gb instanceof GenCppFile gdf) { String varContent = gdf.getConstContent(); System.out.println("genVar: " + varContent); - String expect = "\nconst int TestParam = 100;\n"; + String expect = "\nextends const int TestParam = 100;\n"; assertEquals(expect, varContent); } } @@ -1065,6 +1242,7 @@ class GenCppFileTest { @Test void genTypeList() { + TypeObj to = new TypeObj(); } @Test @@ -1110,7 +1288,7 @@ class GenCppFileTest { if (gb instanceof GenCppFile gdf) { String varContent = gdf.getConstContent(); System.out.println("genVar: " + varContent); - String expect = "\nconst int TestParam = 100;\n"; + String expect = "\nextends const int TestParam = 100;\n"; assertEquals(expect, varContent); } } diff --git a/src/intellij_plugin/ohosgen/src/test/java/parse/ParseTsTest.java b/src/intellij_plugin/ohosgen/src/test/java/parse/ParseTsTest.java index 3a6c8a54..26ace44c 100644 --- a/src/intellij_plugin/ohosgen/src/test/java/parse/ParseTsTest.java +++ b/src/intellij_plugin/ohosgen/src/test/java/parse/ParseTsTest.java @@ -919,7 +919,7 @@ class ParseTsTest { } @Test - void parseCStreamType() { + void parseCStreamType1() { ParseBase parser = ParseFactory.getParser("ts2cpp"); String testType = "export type TestShap_t = TestShape;"; CodePointCharStream cStream = CharStreams.fromString(testType); @@ -932,4 +932,373 @@ class ParseTsTest { assertEquals(1, tl.size()); assertEquals("TestShape", tl.get(0)); } + + @Test + void parseCStreamType2() { + ParseBase parser = ParseFactory.getParser("ts2cpp"); + String testType = testType2; + CodePointCharStream cStream = CharStreams.fromString(testType); + ParseObj po = parser.parseCStream(cStream); + List tol = po.getTypeList(); + assertEquals(1, tol.size()); + TypeObj toItem = tol.get(0); + assertEquals("Employee", toItem.getName()); + List pal = toItem.getParamList(); + assertEquals(6, pal.size()); + assertEquals("type", pal.get(0).getName()); + assertEquals("\"employee\"", pal.get(0).getStrValue(0)); + assertEquals("\"manager\"", pal.get(0).getStrValue(1)); + assertEquals("typeId", pal.get(1).getName()); + assertEquals("1", pal.get(1).getStrValue(0)); + assertEquals("2", pal.get(1).getStrValue(1)); + assertEquals("id", pal.get(2).getName()); + assertEquals("string", pal.get(2).getType()); + assertEquals("name", pal.get(3).getName()); + assertEquals("string", pal.get(3).getType()); + assertEquals("address", pal.get(4).getName()); + assertEquals("string", pal.get(4).getType()); + assertEquals("phone", pal.get(5).getName()); + assertEquals("string", pal.get(5).getType()); + } + + @Test + void parseCStreamType3() { + ParseBase parser = ParseFactory.getParser("ts2cpp"); + String testType = testType3; + CodePointCharStream cStream = CharStreams.fromString(testType); + ParseObj po = parser.parseCStream(cStream); + List tol = po.getTypeList(); + assertEquals(1, tol.size()); + TypeObj toItem = tol.get(0); + assertEquals("EmployeeType", toItem.getName()); + assertEquals("\"employee\"", toItem.getParamList().get(0).getStrValue(0)); + assertEquals("\"manager\"", toItem.getParamList().get(0).getStrValue(1)); + + } + + @Test + void parseCStreamType4() { + ParseBase parser = ParseFactory.getParser("ts2cpp"); + String testType = testType4; + CodePointCharStream cStream = CharStreams.fromString(testType); + ParseObj po = parser.parseCStream(cStream); + List tol = po.getTypeList(); + assertEquals(1, tol.size()); + TypeObj toItem = tol.get(0); + assertEquals("EmployeeNameType", toItem.getName()); + assertEquals("Employee[\"name\"]", toItem.getTypeList().get(0)); + + } + + @Test + void parseCStreamType5() { + ParseBase parser = ParseFactory.getParser("ts2cpp"); + String testType = testType5; + CodePointCharStream cStream = CharStreams.fromString(testType); + ParseObj po = parser.parseCStream(cStream); + List tol = po.getTypeList(); + assertEquals(1, tol.size()); + TypeObj toItem = tol.get(0); + assertEquals("EmployeeMap", toItem.getName()); + assertEquals("Map", toItem.getTypeList().get(0)); + + } + + @Test + void parseCStreamType6() { + ParseBase parser = ParseFactory.getParser("ts2cpp"); + String testType = testType6; + CodePointCharStream cStream = CharStreams.fromString(testType); + ParseObj po = parser.parseCStream(cStream); + List tol = po.getTypeList(); + assertEquals(1, tol.size()); + TypeObj toItem = tol.get(0); + assertEquals("EmployeeMapKey", toItem.getName()); + assertEquals("keyofEmployeeMap", toItem.getTypeList().get(0)); + + } + + @Test + void parseCStreamConst1() { + ParseBase parser = ParseFactory.getParser("ts2cpp"); + String testType = testVariable1; + CodePointCharStream cStream = CharStreams.fromString(testType); + ParseObj po = parser.parseCStream(cStream); + List vol = po.getVarList(); + assertEquals(1, vol.size()); + ParamObj voItem = vol.get(0); + assertEquals("employeeName", voItem.getName()); + assertEquals("\"John\"", voItem.getStrValue(0)); + } + + @Test + void parseCStreamConst2() { + ParseBase parser = ParseFactory.getParser("ts2cpp"); + String testType = testVariable2; + CodePointCharStream cStream = CharStreams.fromString(testType); + ParseObj po = parser.parseCStream(cStream); + List vol = po.getVarList(); + assertEquals(1, vol.size()); + ParamObj voItem = vol.get(0); + assertEquals("employeeName", voItem.getName()); + assertEquals("string", voItem.getType()); + assertEquals("\"John\"", voItem.getStrValue(0)); + } + + @Test + void parseCStreamConst3() { + ParseBase parser = ParseFactory.getParser("ts2cpp"); + String testType = testVariable3; + CodePointCharStream cStream = CharStreams.fromString(testType); + ParseObj po = parser.parseCStream(cStream); + List vol = po.getVarList(); + assertEquals(1, vol.size()); + ParamObj voItem = vol.get(0); + assertEquals("num1", voItem.getName()); + assertEquals("number", voItem.getType()); + assertEquals("1", voItem.getStrValue(0)); + } + + @Test + void parseCStreamConst4() { + ParseBase parser = ParseFactory.getParser("ts2cpp"); + String testType = testVariable4; + CodePointCharStream cStream = CharStreams.fromString(testType); + ParseObj po = parser.parseCStream(cStream); + List vol = po.getVarList(); + assertEquals(2, vol.size()); + ParamObj voItem = vol.get(0); + assertEquals("playerCodes", voItem.getName()); + assertEquals("player1", voItem.getPaList().get(0).getName()); + assertEquals("9", voItem.getPaList().get(0).getStrValue(0)); + assertEquals("player2", voItem.getPaList().get(1).getName()); + assertEquals("10", voItem.getPaList().get(1).getStrValue(0)); + assertEquals("player3", voItem.getPaList().get(2).getName()); + assertEquals("13", voItem.getPaList().get(2).getStrValue(0)); + assertEquals("player4", voItem.getPaList().get(3).getName()); + assertEquals("20", voItem.getPaList().get(3).getStrValue(0)); + + ParamObj voItem1 = vol.get(1); + assertEquals("playerCodes.player2", voItem1.getName()); + assertEquals("11", voItem1.getStrValue(0)); + } + + @Test + void parseCStreamConst5() { + ParseBase parser = ParseFactory.getParser("ts2cpp"); + String testType = testVariable5; + CodePointCharStream cStream = CharStreams.fromString(testType); + ParseObj po = parser.parseCStream(cStream); + List vol = po.getVarList(); + assertEquals(1, vol.size()); + ParamObj voItem = vol.get(0); + assertEquals("playerCodes", voItem.getName()); + assertEquals("player1", voItem.getPaList().get(0).getName()); + assertEquals("50", voItem.getPaList().get(0).getStrValue(0)); + assertEquals("player2", voItem.getPaList().get(1).getName()); + assertEquals("10", voItem.getPaList().get(1).getStrValue(0)); + assertEquals("player3", voItem.getPaList().get(2).getName()); + assertEquals("13", voItem.getPaList().get(2).getStrValue(0)); + assertEquals("player4", voItem.getPaList().get(3).getName()); + assertEquals("20", voItem.getPaList().get(3).getStrValue(0)); + } + + @Test + void parseCStreamConst6() { + ParseBase parser = ParseFactory.getParser("ts2cpp"); + String testType = testVariable6; + CodePointCharStream cStream = CharStreams.fromString(testType); + ParseObj po = parser.parseCStream(cStream); + List vol = po.getVarList(); + assertEquals(1, vol.size()); + ParamObj voItem = vol.get(0); + assertEquals("playerCodesArray", voItem.getName()); + assertEquals("player1", voItem.getPaList().get(0).getName()); + assertEquals("50", voItem.getPaList().get(0).getStrValue(0)); + assertEquals("player2", voItem.getPaList().get(1).getName()); + assertEquals("playerCodes[Test]", voItem.getPaList().get(1).getStrValue(0)); + assertEquals("player3", voItem.getPaList().get(2).getName()); + assertEquals("13", voItem.getPaList().get(2).getStrValue(0)); + assertEquals("player4", voItem.getPaList().get(3).getName()); + assertEquals("20", voItem.getPaList().get(3).getStrValue(0)); + } + + @Test + void parseCStreamConst7() { + ParseBase parser = ParseFactory.getParser("ts2cpp"); + String testType = testVariable7; + CodePointCharStream cStream = CharStreams.fromString(testType); + ParseObj po = parser.parseCStream(cStream); + List vol = po.getVarList(); + assertEquals(1, vol.size()); + ParamObj voItem = vol.get(0); + assertEquals("ROUTES", voItem.getName()); + assertEquals("any[]", voItem.getType()); + List paList = voItem.getPaList(); + assertEquals(6, paList.size()); + + assertEquals("path", paList.get(0).getPaList().get(0).getName()); + assertEquals("title", paList.get(0).getPaList().get(1).getName()); + assertEquals("icon", paList.get(0).getPaList().get(2).getName()); + assertEquals("class", paList.get(0).getPaList().get(3).getName()); + assertEquals("allowAnonymous", paList.get(0).getPaList().get(4).getName()); + assertEquals("'/dashboard'", paList.get(0).getPaList().get(0).getStrValue(0)); + assertEquals("'Dashboard'", paList.get(0).getPaList().get(1).getStrValue(0)); + assertEquals("'dashboard'", paList.get(0).getPaList().get(2).getStrValue(0)); + assertEquals("''", paList.get(0).getPaList().get(3).getStrValue(0)); + assertEquals("false", paList.get(0).getPaList().get(4).getStrValue(0)); + + assertEquals("path", paList.get(1).getPaList().get(0).getName()); + assertEquals("title", paList.get(1).getPaList().get(1).getName()); + assertEquals("icon", paList.get(1).getPaList().get(2).getName()); + assertEquals("class", paList.get(1).getPaList().get(3).getName()); + assertEquals("allowAnonymous", paList.get(1).getPaList().get(4).getName()); + assertEquals("'/deals'", paList.get(1).getPaList().get(0).getStrValue(0)); + assertEquals("'Deals'", paList.get(1).getPaList().get(1).getStrValue(0)); + assertEquals("'assignment'", paList.get(1).getPaList().get(2).getStrValue(0)); + assertEquals("''", paList.get(1).getPaList().get(3).getStrValue(0)); + assertEquals("false", paList.get(1).getPaList().get(4).getStrValue(0)); + + assertEquals("path", paList.get(2).getPaList().get(0).getName()); + assertEquals("title", paList.get(2).getPaList().get(1).getName()); + assertEquals("icon", paList.get(2).getPaList().get(2).getName()); + assertEquals("class", paList.get(2).getPaList().get(3).getName()); + assertEquals("allowAnonymous", paList.get(2).getPaList().get(4).getName()); + assertEquals("'/pipeline'", paList.get(2).getPaList().get(0).getStrValue(0)); + assertEquals("'Pipeline'", paList.get(2).getPaList().get(1).getStrValue(0)); + assertEquals("'timeline'", paList.get(2).getPaList().get(2).getStrValue(0)); + assertEquals("''", paList.get(2).getPaList().get(3).getStrValue(0)); + assertEquals("false", paList.get(2).getPaList().get(4).getStrValue(0)); + + assertEquals("path", paList.get(3).getPaList().get(0).getName()); + assertEquals("title", paList.get(3).getPaList().get(1).getName()); + assertEquals("icon", paList.get(3).getPaList().get(2).getName()); + assertEquals("class", paList.get(3).getPaList().get(3).getName()); + assertEquals("allowAnonymous", paList.get(3).getPaList().get(4).getName()); + assertEquals("'/language-resolver'", paList.get(3).getPaList().get(0).getStrValue(0)); + assertEquals("'Language'", paList.get(3).getPaList().get(1).getStrValue(0)); + assertEquals("'translate'", paList.get(3).getPaList().get(2).getStrValue(0)); + assertEquals("''", paList.get(3).getPaList().get(3).getStrValue(0)); + assertEquals("false", paList.get(3).getPaList().get(4).getStrValue(0)); + + assertEquals("path", paList.get(4).getPaList().get(0).getName()); + assertEquals("title", paList.get(4).getPaList().get(1).getName()); + assertEquals("icon", paList.get(4).getPaList().get(2).getName()); + assertEquals("class", paList.get(4).getPaList().get(3).getName()); + assertEquals("allowAnonymous", paList.get(4).getPaList().get(4).getName()); + assertEquals("'/commit-analysis'", paList.get(4).getPaList().get(0).getStrValue(0)); + assertEquals("'Commit History'", paList.get(4).getPaList().get(1).getStrValue(0)); + assertEquals("'tune'", paList.get(4).getPaList().get(2).getStrValue(0)); + assertEquals("''", paList.get(4).getPaList().get(3).getStrValue(0)); + assertEquals("false", paList.get(4).getPaList().get(4).getStrValue(0)); + + assertEquals("path", paList.get(5).getPaList().get(0).getName()); + assertEquals("title", paList.get(5).getPaList().get(1).getName()); + assertEquals("icon", paList.get(5).getPaList().get(2).getName()); + assertEquals("class", paList.get(5).getPaList().get(3).getName()); + assertEquals("allowAnonymous", paList.get(5).getPaList().get(4).getName()); + assertEquals("'/login'", paList.get(5).getPaList().get(0).getStrValue(0)); + assertEquals("'Log In'", paList.get(5).getPaList().get(1).getStrValue(0)); + assertEquals("'lock'", paList.get(5).getPaList().get(2).getStrValue(0)); + assertEquals("''", paList.get(5).getPaList().get(3).getStrValue(0)); + assertEquals("true", paList.get(5).getPaList().get(4).getStrValue(0)); + } + + @Test + void parseCStreamConst8() { + ParseBase parser = ParseFactory.getParser("ts2cpp"); + String testType = testVariable8; + CodePointCharStream cStream = CharStreams.fromString(testType); + ParseObj po = parser.parseCStream(cStream); + List vol = po.getVarList(); + assertEquals(1, vol.size()); + ParamObj voItem = vol.get(0); + assertEquals("Components", voItem.getName()); + assertEquals("_.chain([_.values(ROUTES)asany[]]).flatten()." + + "filter((item)=>item.name&&(item.name.toLowerCase().endsWith('component')))." + + "value", voItem.getStrValue(0)); + } + + @Test + void parseCStreamConst9() { + ParseBase parser = ParseFactory.getParser("ts2cpp"); + String testType = testVariable9; + CodePointCharStream cStream = CharStreams.fromString(testType); + ParseObj po = parser.parseCStream(cStream); + List vol = po.getVarList(); + assertEquals(1, vol.size()); + ParamObj voItem = vol.get(0); + assertEquals("fileLanguages", voItem.getName()); + assertEquals("_.uniqBy", voItem.getStrValue(0)); + assertEquals("([...this.fileLanguages,...Components],p=>p.fileId)", voItem.getStrValue(1)); + } + + @Test + void parseCStreamConst10() { + ParseBase parser = ParseFactory.getParser("ts2cpp"); + String testType = testVariable10; + CodePointCharStream cStream = CharStreams.fromString(testType); + ParseObj po = parser.parseCStream(cStream); + List vol = po.getVarList(); + assertEquals(1, vol.size()); + ParamObj voItem = vol.get(0); + assertEquals("languageMap", voItem.getName()); + + } + + @Test + void parseCStreamConst11() { + ParseBase parser = ParseFactory.getParser("ts2cpp"); + String testType = testVariable11; + CodePointCharStream cStream = CharStreams.fromString(testType); + ParseObj po = parser.parseCStream(cStream); + List vol = po.getVarList(); + assertEquals(1, vol.size()); + ParamObj voItem = vol.get(0); + assertEquals("schema", voItem.getName()); + + } + + @Test + void parseCStreamConst12() { + ParseBase parser = ParseFactory.getParser("ts2cpp"); + String testType = testVariable12; + CodePointCharStream cStream = CharStreams.fromString(testType); + ParseObj po = parser.parseCStream(cStream); + List vol = po.getVarList(); + assertEquals(1, vol.size()); + ParamObj voItem = vol.get(0); + assertEquals("codesByType", voItem.getName()); + + } + + @Test + void parseCStreamConst13() { + ParseBase parser = ParseFactory.getParser("ts2cpp"); + String testType = testVariable13; + CodePointCharStream cStream = CharStreams.fromString(testType); + ParseObj po = parser.parseCStream(cStream); + List fol = po.getFuncList(); + assertEquals(1, fol.size()); + FuncObj foItem = fol.get(0); + assertEquals("post", foItem.getAlias()); + assertEquals("void", foItem.getRetValue()); + List paList = foItem.getParamList(); + + assertEquals("any[]", paList.get(0).getType()); + assertEquals("...args", paList.get(0).getName()); + } + + @Test + void parseCStreamConst14() { + ParseBase parser = ParseFactory.getParser("ts2cpp"); + String testType = testVariable14; + CodePointCharStream cStream = CharStreams.fromString(testType); + ParseObj po = parser.parseCStream(cStream); + List vol = po.getVarList(); + assertEquals(1, vol.size()); + ParamObj voItem = vol.get(0); + assertEquals("function", voItem.getName()); + assertEquals("([x]:any)=>x", voItem.getStrValue(0)); + } } \ No newline at end of file -- Gitee From debe98684adcddf8cdf76dd6999fd0ebd84c6119 Mon Sep 17 00:00:00 2001 From: sunlian Date: Thu, 10 Apr 2025 16:51:27 +0800 Subject: [PATCH 2/4] fix code check Signed-off-by: sunlian --- .../typescript/TypeScriptCustomListener.java | 45 +++++------ .../ohosgen/src/main/java/gen/GenCppFile.java | 63 ++++++++------- .../src/main/java/grammar/ParamObj.java | 5 ++ .../src/main/java/utils/StringUtils.java | 12 +++ .../ohosgen/src/main/java/utils/TsToken.java | 78 ++++++++++++------- .../src/test/java/gen/GenCppFileTest.java | 15 +--- .../src/test/java/parse/ParseTsTest.java | 17 +++- 7 files changed, 139 insertions(+), 96 deletions(-) diff --git a/src/intellij_plugin/ohosgen/src/main/java/antlr/typescript/TypeScriptCustomListener.java b/src/intellij_plugin/ohosgen/src/main/java/antlr/typescript/TypeScriptCustomListener.java index dde3fb77..dd5cb4cc 100644 --- a/src/intellij_plugin/ohosgen/src/main/java/antlr/typescript/TypeScriptCustomListener.java +++ b/src/intellij_plugin/ohosgen/src/main/java/antlr/typescript/TypeScriptCustomListener.java @@ -285,7 +285,7 @@ public class TypeScriptCustomListener extends TypeScriptParserBaseListener imple String keyStr = uipt.getChild(i).getText(); if (!TsToken.isTsToken(keyStr)) { lastPa.setStrValue(keyStr); - } else if (TsToken.isTsVarType(keyStr)){ + } else if (TsToken.isTsVarType(keyStr)) { lastPa.setType(keyStr); } @@ -889,6 +889,20 @@ public class TypeScriptCustomListener extends TypeScriptParserBaseListener imple } } + private void addUnionParam(TypeScriptParser.UnionOrIntersectionOrPrimaryTypeContext upt, TypeObj to) { + int cCnt = upt.getChildCount(); + ParamObj pa = new ParamObj(); + for (int i = 0; i < cCnt; i++) { + String keyStr = upt.getChild(i).getText(); + if (!TsToken.isTsToken(keyStr)) { + pa.setStrValue(keyStr); + } else if (TsToken.isTsVarType(keyStr)){ + pa.setType(keyStr); + } + } + to.addParam(pa); + } + @Override public void enterTypeAliasDeclaration(TypeScriptParser.TypeAliasDeclarationContext ctx) { super.enterTypeAliasDeclaration(ctx); @@ -911,17 +925,7 @@ public class TypeScriptCustomListener extends TypeScriptParserBaseListener imple typeContext.unionOrIntersectionOrPrimaryType(); if (upt != null && upt.getChildCount() > 1) { System.out.println("Type uoiop: " + upt.getText()); - int cCnt = upt.getChildCount(); - ParamObj pa = new ParamObj(); - for (int i = 0; i < cCnt; i++) { - String keyStr = upt.getChild(i).getText(); - if (!TsToken.isTsToken(keyStr)) { - pa.setStrValue(keyStr); - } else if (TsToken.isTsVarType(keyStr)){ - pa.setType(keyStr); - } - } - to.addParam(pa); + addUnionParam(upt, to); } TypeScriptParser.TypeGenericContext tgc = typeContext.typeGeneric(); if (tgc != null) { @@ -1214,13 +1218,11 @@ public class TypeScriptCustomListener extends TypeScriptParserBaseListener imple } private void setVariableSingleExpression(TypeScriptParser.VariableDeclarationContext ctx, String varName) { - List sel = ctx.singleExpression(); - for (TypeScriptParser.SingleExpressionContext sec : sel) { + for (TypeScriptParser.SingleExpressionContext sec : ctx.singleExpression()) { String varType = sec.start.getText(); if (varType.equals(TsToken.TS_TOKEN_FUNCTION)) { this.currentIdentifier = varName; createFuncObj(varName); - break; } else if ((sec instanceof TypeScriptParser.FunctionExpressionContext fec) && fec.anonymousFunction() != null) { TypeScriptParser.AnonymousFunctionContext afc = fec.anonymousFunction(); @@ -1244,13 +1246,8 @@ public class TypeScriptCustomListener extends TypeScriptParserBaseListener imple if (secItem instanceof TypeScriptParser.GenericTypesContext gtc) { FuncObj fo = createFuncObj(varName); fo.setName(varType); - - String temp = gtc.typeArguments().getText(); - fo.addTemplate(temp); - - TypeScriptParser.ExpressionSequenceContext esc = gtc.expressionSequence(); - List secl = esc.singleExpression(); - setFuncParamStr(fo, secl); + fo.addTemplate(gtc.typeArguments().getText()); + setFuncParamStr(fo, gtc.expressionSequence().singleExpression()); } if (secItem instanceof TypeScriptParser.ParenthesizedExpressionContext pec) { @@ -1260,14 +1257,12 @@ public class TypeScriptCustomListener extends TypeScriptParserBaseListener imple this.varObjList.add(paObj); } } else { - System.out.println("const 变量名: " + varName); ParamObj pa = new ParamObj(); pa.setName(varName); String typeName = (ctx.typeAnnotation() != null && ctx.typeAnnotation().type_() != null) ? ctx.typeAnnotation().type_().getText() : ""; pa.setType(typeName); - int cCnt = typeName.contains(TsToken.TS_TOKEN_BRACKET) ? - 0 : sec.getChildCount(); + int cCnt = typeName.contains(TsToken.TS_TOKEN_BRACKET) ? 0 : sec.getChildCount(); for (int i = 0; i < cCnt; i++) { pa.setStrValue(sec.getChild(i).getText()); } diff --git a/src/intellij_plugin/ohosgen/src/main/java/gen/GenCppFile.java b/src/intellij_plugin/ohosgen/src/main/java/gen/GenCppFile.java index 22ba0e03..ada58cbe 100644 --- a/src/intellij_plugin/ohosgen/src/main/java/gen/GenCppFile.java +++ b/src/intellij_plugin/ohosgen/src/main/java/gen/GenCppFile.java @@ -609,6 +609,38 @@ public class GenCppFile extends GeneratorBase { this.unionContent = resContent; }; + private String genVarArrayList(String tmpContent, String paName, List paList) { + String resContent = tmpContent; + resContent += CPP_NEW_LINE + CPP_STRUCT_TOKEN + CPP_BLANK_SPACE + paName + + CPP_STRUCT_SUFFIX + CPP_BLANK_SPACE + CPP_LEFT_BRACE; + List paramList = paList.get(0).getPaList(); + for (ParamObj paItem : paramList) { + String paStr = paItem.getName(); + String paVal = paItem.getStrValue(0); + String typeStr = StringUtils.isAllDigits(paVal) ? + CPP_NUMBER_TOKEN : CPP_STD_STRING; + typeStr = StringUtils.isBoolean(paVal) ? CPP_BOOLEAN_TOKEN : typeStr; + resContent += CPP_NEW_LINE + CPP_TAB_SPACE + typeStr + CPP_BLANK_SPACE + paStr + CPP_SEMICOLON; + } + resContent += CPP_NEW_LINE + CPP_RIGHT_BRACE + CPP_SEMICOLON + CPP_NEW_LINE; + + resContent += CPP_NEW_LINE + CPP_CONST_TOKEN + CPP_BLANK_SPACE + CPP_STD_VECTOR + + CPP_LEFT_ANGLE_BRACKET + paName + CPP_STRUCT_SUFFIX + CPP_RIGHT_ANGLE_BRACKET + + CPP_BLANK_SPACE + paName + CPP_EQUAL + CPP_LEFT_BRACE; + for (ParamObj paramListItem : paList) { + List subParamList = paramListItem.getPaList(); + resContent += CPP_NEW_LINE + CPP_TAB_SPACE + CPP_LEFT_BRACE; + for (ParamObj paItem : subParamList) { + String paVal = paItem.getStrValue(0); + resContent += paVal + CPP_COMMA + CPP_BLANK_SPACE; + } + resContent = StringUtils.removeLastCharacter(resContent, 2); + resContent += CPP_RIGHT_BRACE + CPP_COMMA; + } + resContent += CPP_NEW_LINE + CPP_RIGHT_BRACE + CPP_SEMICOLON + CPP_NEW_LINE; + return resContent; + }; + /** * 生成输出内容 * @@ -626,8 +658,7 @@ public class GenCppFile extends GeneratorBase { List paList = po.getPaList(); if (paList.isEmpty()) { resContent += CPP_NEW_LINE + CPP_EXTENDS_TOKEN + CPP_BLANK_SPACE + CPP_CONST_TOKEN + - CPP_BLANK_SPACE + paType + CPP_BLANK_SPACE + paName + - CPP_EQUAL + paValue; + CPP_BLANK_SPACE + paType + CPP_BLANK_SPACE + paName + CPP_EQUAL + paValue; resContent += CPP_SEMICOLON + CPP_NEW_LINE; } else if (paList.get(0).getPaList().isEmpty()) { @@ -647,33 +678,7 @@ public class GenCppFile extends GeneratorBase { resContent = StringUtils.removeLastCharacter(resContent, 1); resContent += CPP_NEW_LINE + CPP_RIGHT_BRACE + CPP_SEMICOLON + CPP_NEW_LINE; } else if (!(paList.get(0).getPaList().isEmpty())) { - resContent += CPP_NEW_LINE + CPP_STRUCT_TOKEN + CPP_BLANK_SPACE + paName + - CPP_STRUCT_SUFFIX + CPP_BLANK_SPACE + CPP_LEFT_BRACE; - List paramList = paList.get(0).getPaList(); - for (ParamObj paItem : paramList) { - String paStr = paItem.getName(); - String paVal = paItem.getStrValue(0); - String typeStr = StringUtils.isAllDigits(paVal) ? - CPP_NUMBER_TOKEN : CPP_STD_STRING; - typeStr = StringUtils.isBoolean(paVal) ? CPP_BOOLEAN_TOKEN : typeStr; - resContent += CPP_NEW_LINE + CPP_TAB_SPACE + typeStr + CPP_BLANK_SPACE + paStr + CPP_SEMICOLON; - } - resContent += CPP_NEW_LINE + CPP_RIGHT_BRACE + CPP_SEMICOLON + CPP_NEW_LINE; - - resContent += CPP_NEW_LINE + CPP_CONST_TOKEN + CPP_BLANK_SPACE + CPP_STD_VECTOR + - CPP_LEFT_ANGLE_BRACKET + paName + CPP_STRUCT_SUFFIX + CPP_RIGHT_ANGLE_BRACKET + - CPP_BLANK_SPACE + paName + CPP_EQUAL + CPP_LEFT_BRACE; - for (ParamObj paramListItem : paList) { - List subParamList = paramListItem.getPaList(); - resContent += CPP_NEW_LINE + CPP_TAB_SPACE + CPP_LEFT_BRACE; - for (ParamObj paItem : subParamList) { - String paVal = paItem.getStrValue(0); - resContent += paVal + CPP_COMMA + CPP_BLANK_SPACE; - } - resContent = StringUtils.removeLastCharacter(resContent, 2); - resContent += CPP_RIGHT_BRACE + CPP_COMMA; - } - resContent += CPP_NEW_LINE + CPP_RIGHT_BRACE + CPP_SEMICOLON + CPP_NEW_LINE; + resContent = genVarArrayList(resContent, paName, paList); } } diff --git a/src/intellij_plugin/ohosgen/src/main/java/grammar/ParamObj.java b/src/intellij_plugin/ohosgen/src/main/java/grammar/ParamObj.java index 0105fc1d..f34fb106 100644 --- a/src/intellij_plugin/ohosgen/src/main/java/grammar/ParamObj.java +++ b/src/intellij_plugin/ohosgen/src/main/java/grammar/ParamObj.java @@ -268,6 +268,11 @@ public class ParamObj extends GBaseObject { this.paList = paList; } + /** + * 增加子参数 + * + * @param pa 子参数 + */ public void addParam(ParamObj pa) { this.paList.add(pa); } diff --git a/src/intellij_plugin/ohosgen/src/main/java/utils/StringUtils.java b/src/intellij_plugin/ohosgen/src/main/java/utils/StringUtils.java index 3acf988f..5b874484 100644 --- a/src/intellij_plugin/ohosgen/src/main/java/utils/StringUtils.java +++ b/src/intellij_plugin/ohosgen/src/main/java/utils/StringUtils.java @@ -72,10 +72,22 @@ public class StringUtils { return str; } + /** + * 判读是否为数字 + * + * @param str 字符串 + * @return 是数字返回 true + */ public static boolean isAllDigits(String str) { return str != null && str.matches("\\d+"); } + /** + * 判断是否为布尔 + * + * @param str 字符串 + * @return 是布尔返回true + */ public static boolean isBoolean(String str) { String lowerStr = str.toLowerCase(Locale.ROOT); return lowerStr.equals("true") || lowerStr.equals("false"); diff --git a/src/intellij_plugin/ohosgen/src/main/java/utils/TsToken.java b/src/intellij_plugin/ohosgen/src/main/java/utils/TsToken.java index 8308689c..8c6a7d54 100644 --- a/src/intellij_plugin/ohosgen/src/main/java/utils/TsToken.java +++ b/src/intellij_plugin/ohosgen/src/main/java/utils/TsToken.java @@ -446,67 +446,67 @@ public class TsToken { /** * MultiplyAssign token */ - public static final String TS_TOKEN_MultiplyAssign = "*="; + public static final String TS_TOKEN_MULTIPLYASSIGN = "*="; /** * DivideAssign token */ - public static final String TS_TOKEN_DivideAssign = "/="; + public static final String TS_TOKEN_DIVIDEASSIGN = "/="; /** * ModulusAssign token */ - public static final String TS_TOKEN_ModulusAssign = "%="; + public static final String TS_TOKEN_MODULUSASSIGN = "%="; /** * PlusAssign token */ - public static final String TS_TOKEN_PlusAssign = "+="; + public static final String TS_TOKEN_PLUSASSIGN = "+="; /** * MinusAssign token */ - public static final String TS_TOKEN_MinusAssign = "-="; + public static final String TS_TOKEN_MIMUSASSIGN = "-="; /** * LeftShiftArithmeticAssign token */ - public static final String TS_TOKEN_LeftShiftArithmeticAssign = "<<="; + public static final String TS_TOKEN_LEFTSHIFTARITHMETICASSIGN = "<<="; /** * RightShiftArithmeticAssign token */ - public static final String TS_TOKEN_RightShiftArithmeticAssign = ">>="; + public static final String TS_TOKEN_RIGHTSHIFTARITHMETICASSIGN = ">>="; /** * RightShiftLogicalAssign token */ - public static final String TS_TOKEN_RightShiftLogicalAssign = ">>>="; + public static final String TS_TOKEN_RIGHTSHIFTLOGICALASSIGN = ">>>="; /** * BitAndAssign token */ - public static final String TS_TOKEN_BitAndAssign = "&="; + public static final String TS_TOKEN_BITANDASSIGN = "&="; /** * BitXorAssign token */ - public static final String TS_TOKEN_BitXorAssign = "^="; + public static final String TS_TOKEN_BITXORASSIGN = "^="; /** * BitOrAssign token */ - public static final String TS_TOKEN_BitOrAssign = "|="; + public static final String TS_TOKEN_BITORASSIGN = "|="; /** * PowerAssign token */ - public static final String TS_TOKEN_PowerAssign = "**="; + public static final String TS_TOKEN_POWERASSIGN = "**="; /** * NullishCoalescingAssign token */ - public static final String TS_TOKEN_NullishCoalescingAssign = "??="; + public static final String TS_TOKEN_NULLISHCOALESCINGASSIGN = "??="; /** * await token @@ -518,7 +518,10 @@ public class TsToken { */ public static final String TS_TOKEN_BRACKET = "[]"; - public static final List tsTokenList = List.of( + /** + * key list + */ + public static final List TS_TOKEN_LIST = List.of( TS_TOKEN_OPTIONAL, TS_TOKEN_REQUIRED, TS_TOKEN_REST_PARAM, @@ -602,23 +605,26 @@ public class TsToken { TS_TOKEN_BitOr, TS_TOKEN_And, TS_TOKEN_Or, - TS_TOKEN_MultiplyAssign, - TS_TOKEN_DivideAssign, - TS_TOKEN_ModulusAssign, - TS_TOKEN_PlusAssign, - TS_TOKEN_MinusAssign, - TS_TOKEN_LeftShiftArithmeticAssign, - TS_TOKEN_RightShiftArithmeticAssign, - TS_TOKEN_RightShiftLogicalAssign, - TS_TOKEN_BitAndAssign, - TS_TOKEN_BitXorAssign, - TS_TOKEN_BitOrAssign, - TS_TOKEN_PowerAssign, - TS_TOKEN_NullishCoalescingAssign, + TS_TOKEN_MULTIPLYASSIGN, + TS_TOKEN_DIVIDEASSIGN, + TS_TOKEN_MODULUSASSIGN, + TS_TOKEN_PLUSASSIGN, + TS_TOKEN_MIMUSASSIGN, + TS_TOKEN_LEFTSHIFTARITHMETICASSIGN, + TS_TOKEN_RIGHTSHIFTARITHMETICASSIGN, + TS_TOKEN_RIGHTSHIFTLOGICALASSIGN, + TS_TOKEN_BITANDASSIGN, + TS_TOKEN_BITXORASSIGN, + TS_TOKEN_BITORASSIGN, + TS_TOKEN_POWERASSIGN, + TS_TOKEN_NULLISHCOALESCINGASSIGN, TS_TOKEN_ARROW ); - public static final List tsVarTypeList = List.of( + /** + * var key list + */ + public static final List TS_VAR_TYPE_LIST = List.of( TS_TOKEN_ANY, TS_TOKEN_NUMBER, TS_TOKEN_NEVER, @@ -626,12 +632,24 @@ public class TsToken { TS_TOKEN_STRING ); + /** + * 判断是否是关键字 + * + * @param key 字符串 + * @return 若是ts关键字返回true + */ public static boolean isTsToken(String key) { - return TsToken.tsTokenList.contains(key); + return TsToken.TS_TOKEN_LIST.contains(key); } + /** + * 判断是否为变量类型 + * + * @param key 字符串 + * @return 若是变量类型返回true + */ public static boolean isTsVarType(String key) { - return TsToken.tsVarTypeList.contains(key); + return TsToken.TS_VAR_TYPE_LIST.contains(key); } } diff --git a/src/intellij_plugin/ohosgen/src/test/java/gen/GenCppFileTest.java b/src/intellij_plugin/ohosgen/src/test/java/gen/GenCppFileTest.java index 10d74561..565240fa 100644 --- a/src/intellij_plugin/ohosgen/src/test/java/gen/GenCppFileTest.java +++ b/src/intellij_plugin/ohosgen/src/test/java/gen/GenCppFileTest.java @@ -968,10 +968,7 @@ class GenCppFileTest { paItem1.setName("path"); paItem1.setStrValue("'/dashboard'"); paListItem1.addParam(paItem1); - ParamObj paItem2 = new ParamObj(); - paItem2.setName("title"); - paItem2.setStrValue("'Dashboard'"); - paListItem1.addParam(paItem2); + ParamObj paItem3 = new ParamObj(); paItem3.setName("allowAnonymous"); paItem3.setStrValue("false"); @@ -983,10 +980,7 @@ class GenCppFileTest { paItem21.setName("path"); paItem21.setStrValue("'/deals'"); paListItem2.addParam(paItem21); - ParamObj paItem22 = new ParamObj(); - paItem22.setName("title"); - paItem22.setStrValue("'Deals'"); - paListItem2.addParam(paItem22); + ParamObj paItem23 = new ParamObj(); paItem23.setName("allowAnonymous"); paItem23.setStrValue("true"); @@ -1005,13 +999,12 @@ class GenCppFileTest { System.out.println("getVar: " + constContent); String expect = "\nstruct ROUTESST {\n" + "\tstd::string path;\n" + - "\tstd::string title;\n" + "\tboolean allowAnonymous;\n" + "};\n" + "\n" + "const std::vector ROUTES = {\n" + - "\t{'/dashboard', 'Dashboard', false},\n" + - "\t{'/deals', 'Deals', true},\n" + + "\t{'/dashboard', false},\n" + + "\t{'/deals', true},\n" + "};\n"; assertEquals(expect, constContent); } diff --git a/src/intellij_plugin/ohosgen/src/test/java/parse/ParseTsTest.java b/src/intellij_plugin/ohosgen/src/test/java/parse/ParseTsTest.java index 26ace44c..61c0d0de 100644 --- a/src/intellij_plugin/ohosgen/src/test/java/parse/ParseTsTest.java +++ b/src/intellij_plugin/ohosgen/src/test/java/parse/ParseTsTest.java @@ -1124,7 +1124,7 @@ class ParseTsTest { } @Test - void parseCStreamConst7() { + void parseCStreamConst7_1() { ParseBase parser = ParseFactory.getParser("ts2cpp"); String testType = testVariable7; CodePointCharStream cStream = CharStreams.fromString(testType); @@ -1169,6 +1169,21 @@ class ParseTsTest { assertEquals("'timeline'", paList.get(2).getPaList().get(2).getStrValue(0)); assertEquals("''", paList.get(2).getPaList().get(3).getStrValue(0)); assertEquals("false", paList.get(2).getPaList().get(4).getStrValue(0)); + } + + @Test + void parseCStreamConst7_2() { + ParseBase parser = ParseFactory.getParser("ts2cpp"); + String testType = testVariable7; + CodePointCharStream cStream = CharStreams.fromString(testType); + ParseObj po = parser.parseCStream(cStream); + List vol = po.getVarList(); + assertEquals(1, vol.size()); + ParamObj voItem = vol.get(0); + assertEquals("ROUTES", voItem.getName()); + assertEquals("any[]", voItem.getType()); + List paList = voItem.getPaList(); + assertEquals(6, paList.size()); assertEquals("path", paList.get(3).getPaList().get(0).getName()); assertEquals("title", paList.get(3).getPaList().get(1).getName()); -- Gitee From f3be072efee33d33d094f8f1924e14852788687d Mon Sep 17 00:00:00 2001 From: sunlian Date: Thu, 10 Apr 2025 17:49:17 +0800 Subject: [PATCH 3/4] fix code fix Signed-off-by: sunlian --- .../typescript/TypeScriptCustomListener.java | 39 +++++++------- .../ohosgen/src/main/java/utils/TsToken.java | 52 +++++++++---------- 2 files changed, 45 insertions(+), 46 deletions(-) diff --git a/src/intellij_plugin/ohosgen/src/main/java/antlr/typescript/TypeScriptCustomListener.java b/src/intellij_plugin/ohosgen/src/main/java/antlr/typescript/TypeScriptCustomListener.java index dd5cb4cc..18b6d3af 100644 --- a/src/intellij_plugin/ohosgen/src/main/java/antlr/typescript/TypeScriptCustomListener.java +++ b/src/intellij_plugin/ohosgen/src/main/java/antlr/typescript/TypeScriptCustomListener.java @@ -271,25 +271,28 @@ public class TypeScriptCustomListener extends TypeScriptParserBaseListener imple System.out.println("enterUnion: " + ctx.getText()); } + private void setTypeParam(TypeScriptParser.TypeAnnotationContext ctx, TypeObj to) { + TypeScriptParser. UnionOrIntersectionOrPrimaryTypeContext uipt = + ctx.type_().unionOrIntersectionOrPrimaryType(); + int cCnt = uipt.getChildCount(); + ParamObj lastPa = to.getLastParamObj(); + for (int i = 0; i < cCnt; i++) { + String keyStr = uipt.getChild(i).getText(); + if (!TsToken.isTsToken(keyStr)) { + lastPa.setStrValue(keyStr); + } else if (TsToken.isTsVarType(keyStr)) { + lastPa.setType(keyStr); + } + + } + } @Override public void enterTypeAnnotation(TypeScriptParser.TypeAnnotationContext ctx) { super.enterTypeAnnotation(ctx); System.out.println("enterTypeAnnotation: " + ctx.getText()); if (this.currentObject instanceof TypeObj to) { if (ctx.type_() != null && ctx.type_().unionOrIntersectionOrPrimaryType() != null) { - TypeScriptParser. UnionOrIntersectionOrPrimaryTypeContext uipt = - ctx.type_().unionOrIntersectionOrPrimaryType(); - int cCnt = uipt.getChildCount(); - ParamObj lastPa = to.getLastParamObj(); - for (int i = 0; i < cCnt; i++) { - String keyStr = uipt.getChild(i).getText(); - if (!TsToken.isTsToken(keyStr)) { - lastPa.setStrValue(keyStr); - } else if (TsToken.isTsVarType(keyStr)) { - lastPa.setType(keyStr); - } - - } + setTypeParam(ctx, to); } } } @@ -1234,15 +1237,11 @@ public class TypeScriptCustomListener extends TypeScriptParserBaseListener imple List secl = pec.expressionSequence().singleExpression(); for (TypeScriptParser.SingleExpressionContext secItem : secl) { - String name = secItem.getText(); - fo.addParam(name, ""); + fo.addParam(secItem.getText(), ""); } - } else if (sec instanceof TypeScriptParser.IdentifierExpressionContext iec) { + } else if (sec instanceof TypeScriptParser.IdentifierExpressionContext iec && + iec.singleExpression() != null) { TypeScriptParser.SingleExpressionContext secItem = iec.singleExpression(); - if (secItem == null) { - continue; - } - if (secItem instanceof TypeScriptParser.GenericTypesContext gtc) { FuncObj fo = createFuncObj(varName); fo.setName(varType); diff --git a/src/intellij_plugin/ohosgen/src/main/java/utils/TsToken.java b/src/intellij_plugin/ohosgen/src/main/java/utils/TsToken.java index 8c6a7d54..b9436aa7 100644 --- a/src/intellij_plugin/ohosgen/src/main/java/utils/TsToken.java +++ b/src/intellij_plugin/ohosgen/src/main/java/utils/TsToken.java @@ -381,67 +381,67 @@ public class TsToken { /** * less than token */ - public static final String TS_TOKEN_LessThan = "<"; + public static final String TS_TOKEN_LESSTHAN = "<"; /** * more than token */ - public static final String TS_TOKEN_MoreThan = ">"; + public static final String TS_TOKEN_MORETHAN = ">"; /** * less than equals token */ - public static final String TS_TOKEN_LessThanEquals = "<="; + public static final String TS_TOKEN_LESSTHANEQUALS = "<="; /** * greater than equals token */ - public static final String TS_TOKEN_GreaterThanEquals = ">="; + public static final String TS_TOKEN_GREATERTHANEQUALS = ">="; /** * equals token */ - public static final String TS_TOKEN_Equals = "=="; + public static final String TS_TOKEN_EQUALS = "=="; /** * not equals token */ - public static final String TS_TOKEN_NotEquals = "!="; + public static final String TS_TOKEN_NOTEQUALS = "!="; /** * identity equals token */ - public static final String TS_TOKEN_IdentityEquals = "==="; + public static final String TS_TOKEN_IDENTITYEQUALS = "==="; /** * identity not equals token */ - public static final String TS_TOKEN_IdentityNotEquals = "!=="; + public static final String TS_TOKEN_IDENTITYNOTEQUALS = "!=="; /** * bit and token */ - public static final String TS_TOKEN_BitAnd = "&"; + public static final String TS_TOKEN_BITAND = "&"; /** * BitXOr token */ - public static final String TS_TOKEN_BitXOr = "^"; + public static final String TS_TOKEN_BITXOR = "^"; /** * BitOr token */ - public static final String TS_TOKEN_BitOr = "|"; + public static final String TS_TOKEN_BITOR = "|"; /** * And token */ - public static final String TS_TOKEN_And= "&&"; + public static final String TS_TOKEN_AND = "&&"; /** * Or token */ - public static final String TS_TOKEN_Or = "||"; + public static final String TS_TOKEN_OR = "||"; /** * MultiplyAssign token @@ -592,19 +592,19 @@ public class TsToken { TS_TOKEN_ASYNC, TS_TOKEN_AWAIT, TS_TOKEN_YIELD, - TS_TOKEN_LessThan, - TS_TOKEN_MoreThan, - TS_TOKEN_LessThanEquals, - TS_TOKEN_GreaterThanEquals, - TS_TOKEN_Equals, - TS_TOKEN_NotEquals, - TS_TOKEN_IdentityEquals, - TS_TOKEN_IdentityNotEquals, - TS_TOKEN_BitAnd, - TS_TOKEN_BitXOr, - TS_TOKEN_BitOr, - TS_TOKEN_And, - TS_TOKEN_Or, + TS_TOKEN_LESSTHAN, + TS_TOKEN_MORETHAN, + TS_TOKEN_LESSTHANEQUALS, + TS_TOKEN_GREATERTHANEQUALS, + TS_TOKEN_EQUALS, + TS_TOKEN_NOTEQUALS, + TS_TOKEN_IDENTITYEQUALS, + TS_TOKEN_IDENTITYNOTEQUALS, + TS_TOKEN_BITAND, + TS_TOKEN_BITXOR, + TS_TOKEN_BITOR, + TS_TOKEN_AND, + TS_TOKEN_OR, TS_TOKEN_MULTIPLYASSIGN, TS_TOKEN_DIVIDEASSIGN, TS_TOKEN_MODULUSASSIGN, -- Gitee From f2a7d661972ac63f22b81013987d2d0777e3789c Mon Sep 17 00:00:00 2001 From: sunlian Date: Thu, 10 Apr 2025 17:58:47 +0800 Subject: [PATCH 4/4] fix code fix Signed-off-by: sunlian --- .../main/java/antlr/typescript/TypeScriptCustomListener.java | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/intellij_plugin/ohosgen/src/main/java/antlr/typescript/TypeScriptCustomListener.java b/src/intellij_plugin/ohosgen/src/main/java/antlr/typescript/TypeScriptCustomListener.java index 18b6d3af..fb976478 100644 --- a/src/intellij_plugin/ohosgen/src/main/java/antlr/typescript/TypeScriptCustomListener.java +++ b/src/intellij_plugin/ohosgen/src/main/java/antlr/typescript/TypeScriptCustomListener.java @@ -505,7 +505,8 @@ public class TypeScriptCustomListener extends TypeScriptParserBaseListener imple } @Override - public void enterComputedPropertyExpressionAssignment(TypeScriptParser.ComputedPropertyExpressionAssignmentContext ctx) { + public void enterComputedPropertyExpressionAssignment( + TypeScriptParser.ComputedPropertyExpressionAssignmentContext ctx) { super.enterComputedPropertyExpressionAssignment(ctx); System.out.println("enterComputedPropertyExpressionAssignment: " + ctx.getText()); } @@ -899,7 +900,7 @@ public class TypeScriptCustomListener extends TypeScriptParserBaseListener imple String keyStr = upt.getChild(i).getText(); if (!TsToken.isTsToken(keyStr)) { pa.setStrValue(keyStr); - } else if (TsToken.isTsVarType(keyStr)){ + } else if (TsToken.isTsVarType(keyStr)) { pa.setType(keyStr); } } -- Gitee