From e4460dedf904882952d6ea66e35ffb168ed7bdfe Mon Sep 17 00:00:00 2001 From: sunlian Date: Fri, 11 Apr 2025 17:33:52 +0800 Subject: [PATCH 1/4] add ts callback test Signed-off-by: sunlian --- .../typescript/TypeScriptCustomListener.java | 43 +++ .../src/main/java/gen/GenerateFactory.java | 5 +- .../src/main/java/grammar/ParamObj.java | 33 ++ .../src/test/java/parse/ParseTsTest.java | 337 ++++++++++++++++++ 4 files changed, 417 insertions(+), 1 deletion(-) 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 fb976478..5edeafbe 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 @@ -1271,6 +1271,49 @@ public class TypeScriptCustomListener extends TypeScriptParserBaseListener imple this.currentToken = TsToken.TS_TOKEN_VAR; } } + String nameStr = ctx.identifierOrKeyWord().getText(); + if (ctx.typeAnnotation() != null && ctx.typeAnnotation().type_().functionType() != null) { + FuncObj fo = new FuncObj(); + fo.setName(nameStr); + this.funcObjList.add(fo); + String retType = ctx.typeAnnotation().type_().functionType().type_().getText(); + fo.setRetValue(retType); + if (ctx.typeAnnotation().type_().functionType().parameterList() != null) { + List pacl = + ctx.typeAnnotation().type_().functionType().parameterList().parameter(); + for (TypeScriptParser.ParameterContext paItem : pacl) { + String paNameStr = paItem.requiredParameter().identifierOrPattern().getText(); + String paTypeStr = paItem.requiredParameter().typeAnnotation().type_().getText(); + ParamObj paSfItem = new ParamObj(); + paSfItem.setName(paNameStr); + paSfItem.setType(paTypeStr); + if (paItem.requiredParameter().typeAnnotation().type_().functionType() != null) { + TypeScriptParser.TypeAnnotationContext tacItem = paItem.requiredParameter().typeAnnotation(); + String subFunRetType = paItem.requiredParameter().typeAnnotation(). + type_().functionType().type_().getText(); + String subFunParam = paItem.requiredParameter().typeAnnotation().type_(). + functionType().parameterList() == null ? "" : paItem.requiredParameter(). + typeAnnotation().type_().functionType().parameterList().getText(); + FuncObj subFoItem = new FuncObj(); + subFoItem.setName(""); + subFoItem.setRetValue(subFunRetType); + + if (!subFunParam.isEmpty()) { + List paCtxList = paItem.requiredParameter(). + typeAnnotation().type_().functionType().parameterList().parameter(); + for (TypeScriptParser.ParameterContext paCtx : paCtxList) { + String subType = paCtx.requiredParameter().typeAnnotation().type_().getText(); + String subName = paCtx.requiredParameter().identifierOrPattern().getText(); + subFoItem.addParam(subName, subType); + } + } + paSfItem.addFunc(subFoItem); + + } + fo.addParam(paSfItem); + } + } + } } private void setFuncParamStr(FuncObj fo, List secl) { diff --git a/src/intellij_plugin/ohosgen/src/main/java/gen/GenerateFactory.java b/src/intellij_plugin/ohosgen/src/main/java/gen/GenerateFactory.java index 5759fd71..a7d3a30d 100644 --- a/src/intellij_plugin/ohosgen/src/main/java/gen/GenerateFactory.java +++ b/src/intellij_plugin/ohosgen/src/main/java/gen/GenerateFactory.java @@ -36,7 +36,10 @@ public class GenerateFactory { */ public static GeneratorBase getGenerator(String type) { return switch (type.toUpperCase(Locale.ROOT)) { - case "CPP" -> new GenCppFile(); + case "CPPH" -> new GenCppHFile(); + case "CH" -> new GenCHFile(); + case "NAPICPP" -> new GenNapiCppFile(); + case "AKICPP" -> new GenAkiCppFile(); case "DTS" -> new GenDtsFile(); default -> { System.out.println("Unsupported parser type: " + type); 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 f34fb106..03491f6b 100644 --- a/src/intellij_plugin/ohosgen/src/main/java/grammar/ParamObj.java +++ b/src/intellij_plugin/ohosgen/src/main/java/grammar/ParamObj.java @@ -70,6 +70,11 @@ public class ParamObj extends GBaseObject { */ private List paList; + /** + * 回调函数定义 + */ + private List foList; + /** * 构造函数 */ @@ -77,6 +82,7 @@ public class ParamObj extends GBaseObject { this.decorator = TsToken.TS_TOKEN_REQUIRED; vList = new CopyOnWriteArrayList<>(); paList = new CopyOnWriteArrayList<>(); + foList = new CopyOnWriteArrayList<>(); } /** @@ -276,4 +282,31 @@ public class ParamObj extends GBaseObject { public void addParam(ParamObj pa) { this.paList.add(pa); } + + /** + * 获取回调函数列表 + * + * @return 回调函数列表 + */ + public List getFoList() { + return foList; + } + + /** + * 设置回调函数列表 + * + * @param foList 回调函数列表 + */ + public void setFoList(List foList) { + this.foList = foList; + } + + /** + * 增加回调函数方法 + * + * @param fo 回调函数 + */ + public void addFunc(FuncObj fo) { + this.foList.add(fo); + } } 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 61c0d0de..692a30a1 100644 --- a/src/intellij_plugin/ohosgen/src/test/java/parse/ParseTsTest.java +++ b/src/intellij_plugin/ohosgen/src/test/java/parse/ParseTsTest.java @@ -223,6 +223,39 @@ class ParseTsTest { " let i = p!;\n" + "}"; + String testFunc30 = "export const AsyncTaskReturnVoid: () => Promise;"; + + String testFunc31 = "export const AsyncTaskLongLongFunctionReturnLong: (min: number, max: number, " + + "func: (a: number, b: number, c: string) => number) => Promise;"; + + String testFunc32 = "export const AsyncTaskLongReturnLong: (a: number) => Promise;"; + + String testFunc33 = "export const CallbackInvoke: (func: () => void) => void;"; + + String testFunc34 = "export const CallbackInvokeFromThread: (func: () => void) => void;"; + + String testFunc35 = "export const CallbackReturnVoid: (func: () => void) => void;"; + + String testFunc36 = "export const SafetyCallbackReturnVoid: (func: () => void) => void;"; + + String testFunc37 = "export const CallbackReturnBool_True: (func: (value: boolean) => boolean) => boolean;"; + + String testFunc38 = "export const SafetyCallbackReturnBool_True: (func: (value: boolean) => boolean) => boolean;"; + + String testFunc39 = "export const CallbackReturnInt_Int: (func: (value: number) => number) => number;"; + + String testFunc40 = "export const CallbackReturnString_String: (func: (value: string) => string) => string;"; + + String testFunc41 = "export const SafetyCallbackReturnString_String: (func: (value: string) => string) => string;"; + + String testFunc42 = "export const CallbackReturnDouble_Double: (func: (value: number) => number) => number;"; + + String testFunc43 = "export const SafetyCallbackReturnDouble_Double: " + + "(func: (value: number) => number) => number;"; + + String testFunc44 = "export const SafetyCallbackReturnDouble_Double_Num_X2: " + + "(func: (value: number) => number) => number;"; + String testType2 = "// TypeAlias\n" + "type Employee = {\n" + " type: \"employee\" | \"manager\";\n" + @@ -897,6 +930,310 @@ class ParseTsTest { assertEquals(TsToken.TS_TOKEN_OPTIONAL, pol.get(0).getDecorator()); } + @Test + void parseCStreamFunc_30() { + ParseBase parser = ParseFactory.getParser("ts2cpp"); + String testFunc = testFunc30; + CodePointCharStream cStream = CharStreams.fromString(testFunc); + ParseObj po = parser.parseCStream(cStream); + List fol = po.getFuncList(); + assertEquals(1, fol.size()); + assertEquals("AsyncTaskReturnVoid", fol.get(0).getName()); + assertEquals("Promise", fol.get(0).getRetValue()); + List pol = fol.get(0).getParamList(); + assertEquals(0, pol.size()); + + } + + @Test + void parseCStreamFunc_31() { + ParseBase parser = ParseFactory.getParser("ts2cpp"); + String testFunc = testFunc31; + CodePointCharStream cStream = CharStreams.fromString(testFunc); + ParseObj po = parser.parseCStream(cStream); + List fol = po.getFuncList(); + assertEquals(1, fol.size()); + assertEquals("AsyncTaskLongLongFunctionReturnLong", fol.get(0).getName()); + assertEquals("Promise", fol.get(0).getRetValue()); + List pol = fol.get(0).getParamList(); + assertEquals(3, pol.size()); + assertEquals("min", pol.get(0).getName()); + assertEquals("number", pol.get(0).getType()); + assertEquals("max", pol.get(1).getName()); + assertEquals("number", pol.get(1).getType()); + assertEquals("func", pol.get(2).getName()); + assertEquals("(a:number,b:number,c:string)=>number", pol.get(2).getType()); + assertEquals("number", pol.get(2).getFoList().get(0).getRetValue()); + assertEquals("a", pol.get(2).getFoList().get(0).getParamList().get(0).getName()); + assertEquals("number", pol.get(2).getFoList().get(0).getParamList().get(0).getType()); + assertEquals("b", pol.get(2).getFoList().get(0).getParamList().get(1).getName()); + assertEquals("number", pol.get(2).getFoList().get(0).getParamList().get(1).getType()); + assertEquals("c", pol.get(2).getFoList().get(0).getParamList().get(2).getName()); + assertEquals("string", pol.get(2).getFoList().get(0).getParamList().get(2).getType()); + assertEquals(TsToken.TS_TOKEN_REQUIRED, pol.get(0).getDecorator()); + } + + @Test + void parseCStreamFunc_32() { + ParseBase parser = ParseFactory.getParser("ts2cpp"); + String testFunc = testFunc32; + CodePointCharStream cStream = CharStreams.fromString(testFunc); + ParseObj po = parser.parseCStream(cStream); + List fol = po.getFuncList(); + assertEquals(1, fol.size()); + assertEquals("AsyncTaskLongReturnLong", fol.get(0).getName()); + assertEquals("Promise", fol.get(0).getRetValue()); + List pol = fol.get(0).getParamList(); + assertEquals(1, pol.size()); + assertEquals("a", pol.get(0).getName()); + assertEquals("number", pol.get(0).getType()); + assertEquals(TsToken.TS_TOKEN_REQUIRED, pol.get(0).getDecorator()); + } + + @Test + void parseCStreamFunc_33() { + ParseBase parser = ParseFactory.getParser("ts2cpp"); + String testFunc = testFunc33; + CodePointCharStream cStream = CharStreams.fromString(testFunc); + ParseObj po = parser.parseCStream(cStream); + List fol = po.getFuncList(); + assertEquals(1, fol.size()); + assertEquals("CallbackInvoke", fol.get(0).getName()); + assertEquals("void", fol.get(0).getRetValue()); + List pol = fol.get(0).getParamList(); + assertEquals(1, pol.size()); + assertEquals("func", pol.get(0).getName()); + assertEquals("()=>void", pol.get(0).getType()); + assertEquals("void", pol.get(0).getFoList().get(0).getRetValue()); + assertEquals(0, pol.get(0).getFoList().get(0).getParamList().size()); + assertEquals(TsToken.TS_TOKEN_REQUIRED, pol.get(0).getDecorator()); + } + + @Test + void parseCStreamFunc_34() { + ParseBase parser = ParseFactory.getParser("ts2cpp"); + String testFunc = testFunc34; + CodePointCharStream cStream = CharStreams.fromString(testFunc); + ParseObj po = parser.parseCStream(cStream); + List fol = po.getFuncList(); + assertEquals(1, fol.size()); + assertEquals("CallbackInvokeFromThread", fol.get(0).getName()); + assertEquals("void", fol.get(0).getRetValue()); + List pol = fol.get(0).getParamList(); + assertEquals(1, pol.size()); + assertEquals("func", pol.get(0).getName()); + assertEquals("()=>void", pol.get(0).getType()); + assertEquals("void", pol.get(0).getFoList().get(0).getRetValue()); + assertEquals(0, pol.get(0).getFoList().get(0).getParamList().size()); + assertEquals(TsToken.TS_TOKEN_REQUIRED, pol.get(0).getDecorator()); + } + + @Test + void parseCStreamFunc_35() { + ParseBase parser = ParseFactory.getParser("ts2cpp"); + String testFunc = testFunc35; + CodePointCharStream cStream = CharStreams.fromString(testFunc); + ParseObj po = parser.parseCStream(cStream); + List fol = po.getFuncList(); + assertEquals(1, fol.size()); + assertEquals("CallbackReturnVoid", fol.get(0).getName()); + assertEquals("void", fol.get(0).getRetValue()); + List pol = fol.get(0).getParamList(); + assertEquals(1, pol.size()); + assertEquals("func", pol.get(0).getName()); + assertEquals("()=>void", pol.get(0).getType()); + assertEquals("void", pol.get(0).getFoList().get(0).getRetValue()); + assertEquals(0, pol.get(0).getFoList().get(0).getParamList().size()); + assertEquals(TsToken.TS_TOKEN_REQUIRED, pol.get(0).getDecorator()); + } + + @Test + void parseCStreamFunc_36() { + ParseBase parser = ParseFactory.getParser("ts2cpp"); + String testFunc = testFunc36; + CodePointCharStream cStream = CharStreams.fromString(testFunc); + ParseObj po = parser.parseCStream(cStream); + List fol = po.getFuncList(); + assertEquals(1, fol.size()); + assertEquals("SafetyCallbackReturnVoid", fol.get(0).getName()); + assertEquals("void", fol.get(0).getRetValue()); + List pol = fol.get(0).getParamList(); + assertEquals(1, pol.size()); + assertEquals("func", pol.get(0).getName()); + assertEquals("()=>void", pol.get(0).getType()); + assertEquals("void", pol.get(0).getFoList().get(0).getRetValue()); + assertEquals(0, pol.get(0).getFoList().get(0).getParamList().size()); + assertEquals(TsToken.TS_TOKEN_REQUIRED, pol.get(0).getDecorator()); + } + + @Test + void parseCStreamFunc_37() { + ParseBase parser = ParseFactory.getParser("ts2cpp"); + String testFunc = testFunc37; + CodePointCharStream cStream = CharStreams.fromString(testFunc); + ParseObj po = parser.parseCStream(cStream); + List fol = po.getFuncList(); + assertEquals(1, fol.size()); + assertEquals("CallbackReturnBool_True", fol.get(0).getName()); + assertEquals("boolean", fol.get(0).getRetValue()); + List pol = fol.get(0).getParamList(); + assertEquals(1, pol.size()); + assertEquals("func", pol.get(0).getName()); + assertEquals("(value:boolean)=>boolean", pol.get(0).getType()); + assertEquals("boolean", pol.get(0).getFoList().get(0).getRetValue()); + assertEquals(1, pol.get(0).getFoList().get(0).getParamList().size()); + assertEquals("value", pol.get(0).getFoList().get(0).getParamList().get(0).getName()); + assertEquals("boolean", pol.get(0).getFoList().get(0).getParamList().get(0).getType()); + assertEquals(TsToken.TS_TOKEN_REQUIRED, pol.get(0).getDecorator()); + } + + @Test + void parseCStreamFunc_38() { + ParseBase parser = ParseFactory.getParser("ts2cpp"); + String testFunc = testFunc38; + CodePointCharStream cStream = CharStreams.fromString(testFunc); + ParseObj po = parser.parseCStream(cStream); + List fol = po.getFuncList(); + assertEquals(1, fol.size()); + assertEquals("SafetyCallbackReturnBool_True", fol.get(0).getName()); + assertEquals("boolean", fol.get(0).getRetValue()); + List pol = fol.get(0).getParamList(); + assertEquals(1, pol.size()); + assertEquals("func", pol.get(0).getName()); + assertEquals("(value:boolean)=>boolean", pol.get(0).getType()); + assertEquals("boolean", pol.get(0).getFoList().get(0).getRetValue()); + assertEquals(1, pol.get(0).getFoList().get(0).getParamList().size()); + assertEquals("value", pol.get(0).getFoList().get(0).getParamList().get(0).getName()); + assertEquals("boolean", pol.get(0).getFoList().get(0).getParamList().get(0).getType()); + assertEquals(TsToken.TS_TOKEN_REQUIRED, pol.get(0).getDecorator()); + } + + @Test + void parseCStreamFunc_39() { + ParseBase parser = ParseFactory.getParser("ts2cpp"); + String testFunc = testFunc39; + CodePointCharStream cStream = CharStreams.fromString(testFunc); + ParseObj po = parser.parseCStream(cStream); + List fol = po.getFuncList(); + assertEquals(1, fol.size()); + assertEquals("CallbackReturnInt_Int", fol.get(0).getName()); + assertEquals("number", fol.get(0).getRetValue()); + List pol = fol.get(0).getParamList(); + assertEquals(1, pol.size()); + assertEquals("func", pol.get(0).getName()); + assertEquals("(value:number)=>number", pol.get(0).getType()); + assertEquals("number", pol.get(0).getFoList().get(0).getRetValue()); + assertEquals(1, pol.get(0).getFoList().get(0).getParamList().size()); + assertEquals("value", pol.get(0).getFoList().get(0).getParamList().get(0).getName()); + assertEquals("number", pol.get(0).getFoList().get(0).getParamList().get(0).getType()); + assertEquals(TsToken.TS_TOKEN_REQUIRED, pol.get(0).getDecorator()); + } + + @Test + void parseCStreamFunc_40() { + ParseBase parser = ParseFactory.getParser("ts2cpp"); + String testFunc = testFunc40; + CodePointCharStream cStream = CharStreams.fromString(testFunc); + ParseObj po = parser.parseCStream(cStream); + List fol = po.getFuncList(); + assertEquals(1, fol.size()); + assertEquals("CallbackReturnString_String", fol.get(0).getName()); + assertEquals("string", fol.get(0).getRetValue()); + List pol = fol.get(0).getParamList(); + assertEquals(1, pol.size()); + assertEquals("func", pol.get(0).getName()); + assertEquals("(value:string)=>string", pol.get(0).getType()); + assertEquals("string", pol.get(0).getFoList().get(0).getRetValue()); + assertEquals(1, pol.get(0).getFoList().get(0).getParamList().size()); + assertEquals("value", pol.get(0).getFoList().get(0).getParamList().get(0).getName()); + assertEquals("string", pol.get(0).getFoList().get(0).getParamList().get(0).getType()); + assertEquals(TsToken.TS_TOKEN_REQUIRED, pol.get(0).getDecorator()); + } + + @Test + void parseCStreamFunc_41() { + ParseBase parser = ParseFactory.getParser("ts2cpp"); + String testFunc = testFunc41; + CodePointCharStream cStream = CharStreams.fromString(testFunc); + ParseObj po = parser.parseCStream(cStream); + List fol = po.getFuncList(); + assertEquals(1, fol.size()); + assertEquals("SafetyCallbackReturnString_String", fol.get(0).getName()); + assertEquals("string", fol.get(0).getRetValue()); + List pol = fol.get(0).getParamList(); + assertEquals(1, pol.size()); + assertEquals("func", pol.get(0).getName()); + assertEquals("(value:string)=>string", pol.get(0).getType()); + assertEquals("string", pol.get(0).getFoList().get(0).getRetValue()); + assertEquals(1, pol.get(0).getFoList().get(0).getParamList().size()); + assertEquals("value", pol.get(0).getFoList().get(0).getParamList().get(0).getName()); + assertEquals("string", pol.get(0).getFoList().get(0).getParamList().get(0).getType()); + assertEquals(TsToken.TS_TOKEN_REQUIRED, pol.get(0).getDecorator()); + } + + @Test + void parseCStreamFunc_42() { + ParseBase parser = ParseFactory.getParser("ts2cpp"); + String testFunc = testFunc42; + CodePointCharStream cStream = CharStreams.fromString(testFunc); + ParseObj po = parser.parseCStream(cStream); + List fol = po.getFuncList(); + assertEquals(1, fol.size()); + assertEquals("CallbackReturnDouble_Double", fol.get(0).getName()); + assertEquals("number", fol.get(0).getRetValue()); + List pol = fol.get(0).getParamList(); + assertEquals(1, pol.size()); + assertEquals("func", pol.get(0).getName()); + assertEquals("(value:number)=>number", pol.get(0).getType()); + assertEquals("number", pol.get(0).getFoList().get(0).getRetValue()); + assertEquals(1, pol.get(0).getFoList().get(0).getParamList().size()); + assertEquals("value", pol.get(0).getFoList().get(0).getParamList().get(0).getName()); + assertEquals("number", pol.get(0).getFoList().get(0).getParamList().get(0).getType()); + assertEquals(TsToken.TS_TOKEN_REQUIRED, pol.get(0).getDecorator()); + } + + @Test + void parseCStreamFunc_43() { + ParseBase parser = ParseFactory.getParser("ts2cpp"); + String testFunc = testFunc43; + CodePointCharStream cStream = CharStreams.fromString(testFunc); + ParseObj po = parser.parseCStream(cStream); + List fol = po.getFuncList(); + assertEquals(1, fol.size()); + assertEquals("SafetyCallbackReturnDouble_Double", fol.get(0).getName()); + assertEquals("number", fol.get(0).getRetValue()); + List pol = fol.get(0).getParamList(); + assertEquals(1, pol.size()); + assertEquals("func", pol.get(0).getName()); + assertEquals("(value:number)=>number", pol.get(0).getType()); + assertEquals("number", pol.get(0).getFoList().get(0).getRetValue()); + assertEquals(1, pol.get(0).getFoList().get(0).getParamList().size()); + assertEquals("value", pol.get(0).getFoList().get(0).getParamList().get(0).getName()); + assertEquals("number", pol.get(0).getFoList().get(0).getParamList().get(0).getType()); + assertEquals(TsToken.TS_TOKEN_REQUIRED, pol.get(0).getDecorator()); + } + + @Test + void parseCStreamFunc_44() { + ParseBase parser = ParseFactory.getParser("ts2cpp"); + String testFunc = testFunc44; + CodePointCharStream cStream = CharStreams.fromString(testFunc); + ParseObj po = parser.parseCStream(cStream); + List fol = po.getFuncList(); + assertEquals(1, fol.size()); + assertEquals("SafetyCallbackReturnDouble_Double_Num_X2", fol.get(0).getName()); + assertEquals("number", fol.get(0).getRetValue()); + List pol = fol.get(0).getParamList(); + assertEquals(1, pol.size()); + assertEquals("func", pol.get(0).getName()); + assertEquals("(value:number)=>number", pol.get(0).getType()); + assertEquals("number", pol.get(0).getFoList().get(0).getRetValue()); + assertEquals(1, pol.get(0).getFoList().get(0).getParamList().size()); + assertEquals("value", pol.get(0).getFoList().get(0).getParamList().get(0).getName()); + assertEquals("number", pol.get(0).getFoList().get(0).getParamList().get(0).getType()); + assertEquals(TsToken.TS_TOKEN_REQUIRED, pol.get(0).getDecorator()); + } + @Test void parseCStreamInterface() { ParseBase parser = ParseFactory.getParser("ts2cpp"); -- Gitee From a7fcb29bc2a87e4be1bc5833d96b0b5dd0bfc5ff Mon Sep 17 00:00:00 2001 From: sunlian Date: Fri, 11 Apr 2025 17:54:02 +0800 Subject: [PATCH 2/4] fix code check Signed-off-by: sunlian --- .../typescript/TypeScriptCustomListener.java | 84 ++++++++++--------- 1 file changed, 44 insertions(+), 40 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 5edeafbe..24c710a1 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 @@ -1221,6 +1221,49 @@ public class TypeScriptCustomListener extends TypeScriptParserBaseListener imple return fo; } + private void setCbFunc(TypeScriptParser.VariableDeclarationContext ctx, String nameStr) { + FuncObj fo = new FuncObj(); + fo.setName(nameStr); + this.funcObjList.add(fo); + String retType = ctx.typeAnnotation().type_().functionType().type_().getText(); + fo.setRetValue(retType); + if (ctx.typeAnnotation().type_().functionType().parameterList() != null) { + List pacl = + ctx.typeAnnotation().type_().functionType().parameterList().parameter(); + for (TypeScriptParser.ParameterContext paItem : pacl) { + String paNameStr = paItem.requiredParameter().identifierOrPattern().getText(); + String paTypeStr = paItem.requiredParameter().typeAnnotation().type_().getText(); + ParamObj paSfItem = new ParamObj(); + paSfItem.setName(paNameStr); + paSfItem.setType(paTypeStr); + if (paItem.requiredParameter().typeAnnotation().type_().functionType() != null) { + TypeScriptParser.TypeAnnotationContext tacItem = paItem.requiredParameter().typeAnnotation(); + String subFunRetType = paItem.requiredParameter().typeAnnotation(). + type_().functionType().type_().getText(); + String subFunParam = paItem.requiredParameter().typeAnnotation().type_(). + functionType().parameterList() == null ? "" : paItem.requiredParameter(). + typeAnnotation().type_().functionType().parameterList().getText(); + FuncObj subFoItem = new FuncObj(); + subFoItem.setName(""); + subFoItem.setRetValue(subFunRetType); + + if (!subFunParam.isEmpty()) { + List paCtxList = paItem.requiredParameter(). + typeAnnotation().type_().functionType().parameterList().parameter(); + for (TypeScriptParser.ParameterContext paCtx : paCtxList) { + String subType = paCtx.requiredParameter().typeAnnotation().type_().getText(); + String subName = paCtx.requiredParameter().identifierOrPattern().getText(); + subFoItem.addParam(subName, subType); + } + } + paSfItem.addFunc(subFoItem); + + } + fo.addParam(paSfItem); + } + } + } + private void setVariableSingleExpression(TypeScriptParser.VariableDeclarationContext ctx, String varName) { for (TypeScriptParser.SingleExpressionContext sec : ctx.singleExpression()) { String varType = sec.start.getText(); @@ -1273,46 +1316,7 @@ public class TypeScriptCustomListener extends TypeScriptParserBaseListener imple } String nameStr = ctx.identifierOrKeyWord().getText(); if (ctx.typeAnnotation() != null && ctx.typeAnnotation().type_().functionType() != null) { - FuncObj fo = new FuncObj(); - fo.setName(nameStr); - this.funcObjList.add(fo); - String retType = ctx.typeAnnotation().type_().functionType().type_().getText(); - fo.setRetValue(retType); - if (ctx.typeAnnotation().type_().functionType().parameterList() != null) { - List pacl = - ctx.typeAnnotation().type_().functionType().parameterList().parameter(); - for (TypeScriptParser.ParameterContext paItem : pacl) { - String paNameStr = paItem.requiredParameter().identifierOrPattern().getText(); - String paTypeStr = paItem.requiredParameter().typeAnnotation().type_().getText(); - ParamObj paSfItem = new ParamObj(); - paSfItem.setName(paNameStr); - paSfItem.setType(paTypeStr); - if (paItem.requiredParameter().typeAnnotation().type_().functionType() != null) { - TypeScriptParser.TypeAnnotationContext tacItem = paItem.requiredParameter().typeAnnotation(); - String subFunRetType = paItem.requiredParameter().typeAnnotation(). - type_().functionType().type_().getText(); - String subFunParam = paItem.requiredParameter().typeAnnotation().type_(). - functionType().parameterList() == null ? "" : paItem.requiredParameter(). - typeAnnotation().type_().functionType().parameterList().getText(); - FuncObj subFoItem = new FuncObj(); - subFoItem.setName(""); - subFoItem.setRetValue(subFunRetType); - - if (!subFunParam.isEmpty()) { - List paCtxList = paItem.requiredParameter(). - typeAnnotation().type_().functionType().parameterList().parameter(); - for (TypeScriptParser.ParameterContext paCtx : paCtxList) { - String subType = paCtx.requiredParameter().typeAnnotation().type_().getText(); - String subName = paCtx.requiredParameter().identifierOrPattern().getText(); - subFoItem.addParam(subName, subType); - } - } - paSfItem.addFunc(subFoItem); - - } - fo.addParam(paSfItem); - } - } + setCbFunc(ctx, nameStr); } } -- Gitee From cbaef62d9d5c93e266f87d8310fd10a759fcbfc8 Mon Sep 17 00:00:00 2001 From: sunlian Date: Fri, 11 Apr 2025 18:09:21 +0800 Subject: [PATCH 3/4] fix code check Signed-off-by: sunlian --- .../typescript/TypeScriptCustomListener.java | 66 +++++++++---------- 1 file changed, 32 insertions(+), 34 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 24c710a1..8eddd775 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 @@ -1225,43 +1225,41 @@ public class TypeScriptCustomListener extends TypeScriptParserBaseListener imple FuncObj fo = new FuncObj(); fo.setName(nameStr); this.funcObjList.add(fo); - String retType = ctx.typeAnnotation().type_().functionType().type_().getText(); - fo.setRetValue(retType); - if (ctx.typeAnnotation().type_().functionType().parameterList() != null) { - List pacl = - ctx.typeAnnotation().type_().functionType().parameterList().parameter(); - for (TypeScriptParser.ParameterContext paItem : pacl) { - String paNameStr = paItem.requiredParameter().identifierOrPattern().getText(); - String paTypeStr = paItem.requiredParameter().typeAnnotation().type_().getText(); - ParamObj paSfItem = new ParamObj(); - paSfItem.setName(paNameStr); - paSfItem.setType(paTypeStr); - if (paItem.requiredParameter().typeAnnotation().type_().functionType() != null) { - TypeScriptParser.TypeAnnotationContext tacItem = paItem.requiredParameter().typeAnnotation(); - String subFunRetType = paItem.requiredParameter().typeAnnotation(). - type_().functionType().type_().getText(); - String subFunParam = paItem.requiredParameter().typeAnnotation().type_(). - functionType().parameterList() == null ? "" : paItem.requiredParameter(). - typeAnnotation().type_().functionType().parameterList().getText(); - FuncObj subFoItem = new FuncObj(); - subFoItem.setName(""); - subFoItem.setRetValue(subFunRetType); - - if (!subFunParam.isEmpty()) { - List paCtxList = paItem.requiredParameter(). - typeAnnotation().type_().functionType().parameterList().parameter(); - for (TypeScriptParser.ParameterContext paCtx : paCtxList) { - String subType = paCtx.requiredParameter().typeAnnotation().type_().getText(); - String subName = paCtx.requiredParameter().identifierOrPattern().getText(); - subFoItem.addParam(subName, subType); - } - } - paSfItem.addFunc(subFoItem); - + fo.setRetValue(ctx.typeAnnotation().type_().functionType().type_().getText()); + if (ctx.typeAnnotation().type_().functionType().parameterList() == null) { + return; + } + List pacl = + ctx.typeAnnotation().type_().functionType().parameterList().parameter(); + for (TypeScriptParser.ParameterContext paItem : pacl) { + ParamObj paSfItem = new ParamObj(); + paSfItem.setName(paItem.requiredParameter().identifierOrPattern().getText()); + paSfItem.setType(paItem.requiredParameter().typeAnnotation().type_().getText()); + fo.addParam(paSfItem); + if (paItem.requiredParameter().typeAnnotation().type_().functionType() == null) { + continue; + } + String subFunRetType = paItem.requiredParameter().typeAnnotation(). + type_().functionType().type_().getText(); + String subFunParam = paItem.requiredParameter().typeAnnotation().type_(). + functionType().parameterList() == null ? "" : paItem.requiredParameter(). + typeAnnotation().type_().functionType().parameterList().getText(); + FuncObj subFoItem = new FuncObj(); + subFoItem.setName(""); + subFoItem.setRetValue(subFunRetType); + + if (!subFunParam.isEmpty()) { + List paCtxList = paItem.requiredParameter(). + typeAnnotation().type_().functionType().parameterList().parameter(); + for (TypeScriptParser.ParameterContext paCtx : paCtxList) { + String subType = paCtx.requiredParameter().typeAnnotation().type_().getText(); + String subName = paCtx.requiredParameter().identifierOrPattern().getText(); + subFoItem.addParam(subName, subType); } - fo.addParam(paSfItem); } + paSfItem.addFunc(subFoItem); } + } private void setVariableSingleExpression(TypeScriptParser.VariableDeclarationContext ctx, String varName) { -- Gitee From 96f27fa8faea4aeea5b11c0d2c298094e72da7e0 Mon Sep 17 00:00:00 2001 From: sunlian Date: Mon, 14 Apr 2025 09:00:37 +0800 Subject: [PATCH 4/4] fix code check Signed-off-by: sunlian --- .../typescript/TypeScriptCustomListener.java | 22 +++++++++---------- 1 file changed, 11 insertions(+), 11 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 8eddd775..bed06721 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 @@ -1188,7 +1188,9 @@ public class TypeScriptCustomListener extends TypeScriptParserBaseListener imple } } - private void setFuncExpressionParam(TypeScriptParser.ArrowFunctionDeclarationContext afdc, FuncObj fo) { + private void setFuncExpressionParam(TypeScriptParser.FunctionExpressionContext fec, FuncObj fo) { + TypeScriptParser.AnonymousFunctionContext afc = fec.anonymousFunction(); + TypeScriptParser.ArrowFunctionDeclarationContext afdc = afc.arrowFunctionDeclaration(); if (afdc.arrowFunctionParameters().formalParameterList() != null) { List fpacl = afdc.arrowFunctionParameters().formalParameterList().formalParameterArg(); @@ -1218,11 +1220,13 @@ public class TypeScriptCustomListener extends TypeScriptParserBaseListener imple fo.setAlias(varName); this.currentObject = fo; this.funcObjList.add(fo); + this.currentIdentifier = varName; return fo; } - private void setCbFunc(TypeScriptParser.VariableDeclarationContext ctx, String nameStr) { + private void setCbFunc(TypeScriptParser.VariableDeclarationContext ctx) { FuncObj fo = new FuncObj(); + String nameStr = ctx.identifierOrKeyWord().getText(); fo.setName(nameStr); this.funcObjList.add(fo); fo.setRetValue(ctx.typeAnnotation().type_().functionType().type_().getText()); @@ -1266,14 +1270,11 @@ public class TypeScriptCustomListener extends TypeScriptParserBaseListener imple for (TypeScriptParser.SingleExpressionContext sec : ctx.singleExpression()) { String varType = sec.start.getText(); if (varType.equals(TsToken.TS_TOKEN_FUNCTION)) { - this.currentIdentifier = varName; createFuncObj(varName); } else if ((sec instanceof TypeScriptParser.FunctionExpressionContext fec) && fec.anonymousFunction() != null) { - TypeScriptParser.AnonymousFunctionContext afc = fec.anonymousFunction(); - TypeScriptParser.ArrowFunctionDeclarationContext afdc = afc.arrowFunctionDeclaration(); FuncObj fo = createFuncObj(varName); - setFuncExpressionParam(afdc, fo); + setFuncExpressionParam(fec, fo); } else if (sec instanceof TypeScriptParser.ParenthesizedExpressionContext pec) { FuncObj fo = createFuncObj(varName); List secl = pec.expressionSequence().singleExpression(); @@ -1283,15 +1284,14 @@ public class TypeScriptCustomListener extends TypeScriptParserBaseListener imple } } else if (sec instanceof TypeScriptParser.IdentifierExpressionContext iec && iec.singleExpression() != null) { - TypeScriptParser.SingleExpressionContext secItem = iec.singleExpression(); - if (secItem instanceof TypeScriptParser.GenericTypesContext gtc) { + if (iec.singleExpression() instanceof TypeScriptParser.GenericTypesContext gtc) { FuncObj fo = createFuncObj(varName); fo.setName(varType); fo.addTemplate(gtc.typeArguments().getText()); setFuncParamStr(fo, gtc.expressionSequence().singleExpression()); } - if (secItem instanceof TypeScriptParser.ParenthesizedExpressionContext pec) { + if (iec.singleExpression() instanceof TypeScriptParser.ParenthesizedExpressionContext pec) { ParamObj paObj = new ParamObj(); paObj.setName(ctx.identifierOrKeyWord().getText()); paObj.setStrValue(sec.getText()); @@ -1312,9 +1312,9 @@ public class TypeScriptCustomListener extends TypeScriptParserBaseListener imple this.currentToken = TsToken.TS_TOKEN_VAR; } } - String nameStr = ctx.identifierOrKeyWord().getText(); + if (ctx.typeAnnotation() != null && ctx.typeAnnotation().type_().functionType() != null) { - setCbFunc(ctx, nameStr); + setCbFunc(ctx); } } -- Gitee