From bc89037a4be28cbb77411b75cd5501d129e1e8b6 Mon Sep 17 00:00:00 2001 From: wangshi Date: Fri, 14 Mar 2025 16:03:08 +0800 Subject: [PATCH 1/3] fix abstract class Signed-off-by: wangshi --- .../typescript/TypeScriptCustomListener.java | 70 ++++++++- .../src/main/java/grammar/ParamObj.java | 19 +++ .../src/test/java/parse/ParseTsTest.java | 144 +++++++++++++++++- 3 files changed, 230 insertions(+), 3 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 c7a19b09..422ae200 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 @@ -19,8 +19,10 @@ import antlr.ParseBaseListener; import com.google.gson.Gson; import com.google.gson.GsonBuilder; import grammar.*; +import it.unimi.dsi.fastutil.bytes.F; import org.antlr.v4.runtime.tree.ParseTree; import org.antlr.v4.runtime.ParserRuleContext; +import org.apache.groovy.parser.antlr4.GroovyParser; import utils.Constants; import utils.TsToken; @@ -40,6 +42,7 @@ public class TypeScriptCustomListener extends TypeScriptParserBaseListener imple private final int currentLanguage = Constants.PARSE_TS_LANGUAGE; private String currentToken = ""; private GBaseObject currentObject; + private String currentIdentifier = ""; private List enumObjList; private List classObjList; private List funcObjList; @@ -272,6 +275,7 @@ public class TypeScriptCustomListener extends TypeScriptParserBaseListener imple public void enterIdentifier(TypeScriptParser.IdentifierContext ctx) { super.enterIdentifier(ctx); System.out.println("enterIdentifier: " + ctx.getText()); + this.currentIdentifier = ctx.getText(); } @Override @@ -391,7 +395,33 @@ public class TypeScriptCustomListener extends TypeScriptParserBaseListener imple // 提取构造函数参数列表 String res = ctx.formalParameterList().getText(); System.out.println("Construct: " + res); - + TypeScriptParser.FormalParameterListContext fplc = ctx.formalParameterList(); + if (fplc != null && (this.currentObject instanceof ClassObj co)) { + int cnt = fplc.getChildCount(); + FuncObj fo = new FuncObj(); + fo.setName("constructor"); + fo.setRetValue("void"); + co.addFunc(fo); + for (int i = 0; i < cnt; i++) { + ParseTree pt = fplc.getChild(i); + if (pt instanceof TypeScriptParser.FormalParameterArgContext fpac) { + String type = ""; + if (fpac.typeAnnotation() != null && fpac.typeAnnotation().stop != null) { + type = fpac.typeAnnotation().stop.getText(); + } + + String name = ""; + if (fpac.assignable() != null) { + name = fpac.assignable().getText(); + } + + if (type.isEmpty()) { + type = name; + } + fo.addParam(name, type); + } + } + } } @Override @@ -457,6 +487,31 @@ public class TypeScriptCustomListener extends TypeScriptParserBaseListener imple public void enterCallSignature(TypeScriptParser.CallSignatureContext ctx) { super.enterCallSignature(ctx); System.out.println("enterCallSignature: " + ctx.getText()); + if (this.currentToken.equals(TsToken.TS_TOKEN_CLASS) && + (this.currentObject instanceof ClassObj co)) { + String typeName = ctx.typeAnnotation().stop.getText(); + FuncObj fo = new FuncObj(); + fo.setRetValue(typeName); + fo.setName(this.currentIdentifier); + TypeScriptParser.ParameterListContext plc = ctx.parameterList(); + int childCnt = plc.getChildCount(); + for (int i = 0; i < childCnt; i++) { + ParseTree pt = plc.getChild(i); + if (pt instanceof TypeScriptParser.ParameterContext pc) { + String paramName = pc.start.getText(); + String paramType = pc.stop.getText(); + fo.addParam(paramName, paramType); + } + } + co.addFunc(fo); + } + + } + + @Override + public void enterTypeName(TypeScriptParser.TypeNameContext ctx) { + super.enterTypeName(ctx); + System.out.println("enterTypeName: " + ctx.getText()); } @Override @@ -500,6 +555,7 @@ public class TypeScriptCustomListener extends TypeScriptParserBaseListener imple public void enterClassElement(TypeScriptParser.ClassElementContext ctx) { super.enterClassElement(ctx); System.out.println("Class element: " + ctx.getText()); + TypeScriptParser.StatementContext sc = ctx.statement(); if (sc != null) { System.out.println("Class state: " + sc.getText()); @@ -705,6 +761,18 @@ public class TypeScriptCustomListener extends TypeScriptParserBaseListener imple public void enterAbstractMemberDeclaration(TypeScriptParser.AbstractMemberDeclarationContext ctx) { super.enterAbstractMemberDeclaration(ctx); System.out.println("find abstract member Declare: " + ctx.getText()); + TypeScriptParser.AbstractDeclarationContext adc = ctx.abstractDeclaration(); + TypeScriptParser.VariableStatementContext vsc = adc.variableStatement(); + if (vsc != null) { + TypeScriptParser.VariableDeclarationListContext vdl = vsc.variableDeclarationList(); + String paramName = vdl.start.getText(); + String paramType = vdl.stop.getText(); + + if (this.currentObject instanceof ClassObj co) { + co.addParam(paramName, paramType); + } + } + } @Override 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 e8a0ab54..aad55140 100644 --- a/src/intellij_plugin/ohosgen/src/main/java/grammar/ParamObj.java +++ b/src/intellij_plugin/ohosgen/src/main/java/grammar/ParamObj.java @@ -25,6 +25,7 @@ package grammar; * @since 2025-02-28 */ public class ParamObj extends GBaseObject { + private String qualifier; private String type; private String name; private int arraySize; @@ -50,6 +51,24 @@ public class ParamObj extends GBaseObject { this.asList = asl; } + /** + * 确定参数关键字 + * + * @return 参数关键字 + */ + public String getQualifier() { + return qualifier; + } + + /** + * 设置参数关键字 + * + * @param qualifier 关键字 + */ + public void setQualifier(String qualifier) { + this.qualifier = qualifier; + } + /** * 获取名字 * 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 20ff1fb0..1b4dc317 100644 --- a/src/intellij_plugin/ohosgen/src/test/java/parse/ParseTsTest.java +++ b/src/intellij_plugin/ohosgen/src/test/java/parse/ParseTsTest.java @@ -124,7 +124,7 @@ class ParseTsTest { " this.name = name;\n" + " }\n" + "\n" + - " abstract find(string): Person;\n" + + " abstract find(target: string): Person;\n" + " abstract nameAbs: string;\n" + "}"; CodePointCharStream cStream = CharStreams.fromString(testClass); @@ -143,7 +143,7 @@ class ParseTsTest { assertEquals("nameAbs", poItem.getName()); assertEquals("string", poItem.getType()); List fol = co.getFuncList(); - assertEquals(3, fol.size()); + assertEquals(2, fol.size()); FuncObj foItem = fol.get(0); assertEquals("constructor", foItem.getName()); List pol = foItem.getParamList(); @@ -153,11 +153,151 @@ class ParseTsTest { assertEquals("string", poItem.getType()); foItem = fol.get(1); assertEquals("find", foItem.getName()); + assertEquals("Person", foItem.getRetValue()); + pol = foItem.getParamList(); + assertEquals(1, pol.size()); + poItem = pol.get(0); + assertEquals("string", poItem.getType()); + + } + + @Test + void parseCStreamClass_3() { + String testClass = "abstract class Person {\n" + + " name: string;\n" + + " value: int;\n" + + "\n" + + " constructor(name: string, value: int) {\n" + + " this.name = name;\n" + + " this.value = value;\n" + + " }\n" + + "\n" + + " abstract find(target: string, cnt: int): Person;\n" + + " abstract add(int, string): Person;\n" + + " abstract nameAbs: string;\n" + + "}"; + CodePointCharStream cStream = CharStreams.fromString(testClass); + ParseBase parser = ParseFactory.getParser("ts"); + ParseObj po = parser.parseCStream(cStream); + List eol = po.getClassList(); + assertEquals(1, eol.size()); + ClassObj co = eol.get(0); + assertEquals("Person", co.getName()); + List pl = co.getParamList(); + assertEquals(3, pl.size()); + ParamObj poItem = pl.get(0); + assertEquals("name", poItem.getName()); + assertEquals("string", poItem.getType()); + poItem = pl.get(1); + assertEquals("value", poItem.getName()); + assertEquals("int", poItem.getType()); + poItem = pl.get(2); + assertEquals("nameAbs", poItem.getName()); + assertEquals("string", poItem.getType()); + List fol = co.getFuncList(); + assertEquals(3, fol.size()); + FuncObj foItem = fol.get(0); + assertEquals("constructor", foItem.getName()); + List pol = foItem.getParamList(); + assertEquals(2, pol.size()); + poItem = pol.get(0); + assertEquals("name", poItem.getName()); + assertEquals("string", poItem.getType()); + poItem = pol.get(1); + assertEquals("value", poItem.getName()); + assertEquals("int", poItem.getType()); + + foItem = fol.get(1); + assertEquals("find", foItem.getName()); + assertEquals("Person", foItem.getRetValue()); + pol = foItem.getParamList(); + assertEquals(2, pol.size()); + poItem = pol.get(0); + assertEquals("target", poItem.getName()); + assertEquals("string", poItem.getType()); + poItem = pol.get(1); + assertEquals("cnt", poItem.getName()); + assertEquals("int", poItem.getType()); + + foItem = fol.get(2); + assertEquals("add", foItem.getName()); + assertEquals("Person", foItem.getRetValue()); + pol = foItem.getParamList(); + assertEquals(2, pol.size()); + poItem = pol.get(0); + assertEquals("int", poItem.getName()); + assertEquals("int", poItem.getType()); + poItem = pol.get(1); + assertEquals("string", poItem.getName()); + assertEquals("string", poItem.getType()); + } + + @Test + void parseCStreamClass_4() { + String testClass = "abstract class Person {\n" + + " name: string;\n" + + " value: int;\n" + + "\n" + + " constructor(string, int) {\n" + + " this.name = name;\n" + + " this.value = value;\n" + + " }\n" + + "\n" + + " abstract find(string): Person;\n" + + " abstract add(int, string): Person;\n" + + " abstract nameAbs: string;\n" + + "}"; + CodePointCharStream cStream = CharStreams.fromString(testClass); + ParseBase parser = ParseFactory.getParser("ts"); + ParseObj po = parser.parseCStream(cStream); + List eol = po.getClassList(); + assertEquals(1, eol.size()); + ClassObj co = eol.get(0); + assertEquals("Person", co.getName()); + List pl = co.getParamList(); + assertEquals(3, pl.size()); + ParamObj poItem = pl.get(0); + assertEquals("name", poItem.getName()); + assertEquals("string", poItem.getType()); + poItem = pl.get(1); + assertEquals("value", poItem.getName()); + assertEquals("int", poItem.getType()); + poItem = pl.get(2); + assertEquals("nameAbs", poItem.getName()); + assertEquals("string", poItem.getType()); + List fol = co.getFuncList(); + assertEquals(3, fol.size()); + FuncObj foItem = fol.get(0); + assertEquals("constructor", foItem.getName()); + List pol = foItem.getParamList(); + assertEquals(2, pol.size()); + poItem = pol.get(0); + assertEquals("string", poItem.getName()); + assertEquals("string", poItem.getType()); + poItem = pol.get(1); + assertEquals("int", poItem.getName()); + assertEquals("int", poItem.getType()); + + foItem = fol.get(1); + assertEquals("find", foItem.getName()); + assertEquals("Person", foItem.getRetValue()); pol = foItem.getParamList(); assertEquals(1, pol.size()); poItem = pol.get(0); + assertEquals("string", poItem.getName()); assertEquals("string", poItem.getType()); + foItem = fol.get(2); + assertEquals("add", foItem.getName()); + assertEquals("Person", foItem.getRetValue()); + pol = foItem.getParamList(); + assertEquals(2, pol.size()); + poItem = pol.get(0); + assertEquals("int", poItem.getName()); + assertEquals("int", poItem.getType()); + poItem = pol.get(1); + assertEquals("string", poItem.getName()); + assertEquals("string", poItem.getType()); } @Test -- Gitee From fc7e568a315151bf0587b965666daf27efe84ce9 Mon Sep 17 00:00:00 2001 From: wangshi Date: Fri, 14 Mar 2025 16:58:27 +0800 Subject: [PATCH 2/3] fix code check Signed-off-by: wangshi --- .../typescript/TypeScriptCustomListener.java | 55 +++++++++--------- .../src/test/java/parse/ParseTsTest.java | 56 ++++++++++--------- 2 files changed, 59 insertions(+), 52 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 422ae200..66b2f5b5 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 @@ -19,10 +19,8 @@ import antlr.ParseBaseListener; import com.google.gson.Gson; import com.google.gson.GsonBuilder; import grammar.*; -import it.unimi.dsi.fastutil.bytes.F; import org.antlr.v4.runtime.tree.ParseTree; import org.antlr.v4.runtime.ParserRuleContext; -import org.apache.groovy.parser.antlr4.GroovyParser; import utils.Constants; import utils.TsToken; @@ -396,32 +394,37 @@ public class TypeScriptCustomListener extends TypeScriptParserBaseListener imple String res = ctx.formalParameterList().getText(); System.out.println("Construct: " + res); TypeScriptParser.FormalParameterListContext fplc = ctx.formalParameterList(); - if (fplc != null && (this.currentObject instanceof ClassObj co)) { - int cnt = fplc.getChildCount(); - FuncObj fo = new FuncObj(); - fo.setName("constructor"); - fo.setRetValue("void"); - co.addFunc(fo); - for (int i = 0; i < cnt; i++) { - ParseTree pt = fplc.getChild(i); - if (pt instanceof TypeScriptParser.FormalParameterArgContext fpac) { - String type = ""; - if (fpac.typeAnnotation() != null && fpac.typeAnnotation().stop != null) { - type = fpac.typeAnnotation().stop.getText(); - } - - String name = ""; - if (fpac.assignable() != null) { - name = fpac.assignable().getText(); - } - - if (type.isEmpty()) { - type = name; - } - fo.addParam(name, type); - } + if (fplc == null || !(this.currentObject instanceof ClassObj co)) { + return; + } + + int cnt = fplc.getChildCount(); + FuncObj fo = new FuncObj(); + fo.setName("constructor"); + fo.setRetValue("void"); + co.addFunc(fo); + for (int i = 0; i < cnt; i++) { + ParseTree pt = fplc.getChild(i); + if (!(pt instanceof TypeScriptParser.FormalParameterArgContext fpac)) { + continue; } + + String type = ""; + if (fpac.typeAnnotation() != null && fpac.typeAnnotation().stop != null) { + type = fpac.typeAnnotation().stop.getText(); + } + + String name = ""; + if (fpac.assignable() != null) { + name = fpac.assignable().getText(); + } + + if (type.isEmpty()) { + type = name; + } + fo.addParam(name, type); } + } @Override 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 1b4dc317..ad141702 100644 --- a/src/intellij_plugin/ohosgen/src/test/java/parse/ParseTsTest.java +++ b/src/intellij_plugin/ohosgen/src/test/java/parse/ParseTsTest.java @@ -35,6 +35,34 @@ import static org.junit.jupiter.api.Assertions.*; */ class ParseTsTest { + String testClass_3 = "abstract class Person {\n" + + " name: string;\n" + + " value: int;\n" + + "\n" + + " constructor(name: string, value: int) {\n" + + " this.name = name;\n" + + " this.value = value;\n" + + " }\n" + + "\n" + + " abstract find(target: string, cnt: int): Person;\n" + + " abstract add(int, string): Person;\n" + + " abstract nameAbs: string;\n" + + "}"; + + String testClass_4 = "abstract class Person {\n" + + " name: string;\n" + + " value: int;\n" + + "\n" + + " constructor(string, int) {\n" + + " this.name = name;\n" + + " this.value = value;\n" + + " }\n" + + "\n" + + " abstract find(string): Person;\n" + + " abstract add(int, string): Person;\n" + + " abstract nameAbs: string;\n" + + "}"; + @Test void parseFile() { } @@ -163,19 +191,7 @@ class ParseTsTest { @Test void parseCStreamClass_3() { - String testClass = "abstract class Person {\n" + - " name: string;\n" + - " value: int;\n" + - "\n" + - " constructor(name: string, value: int) {\n" + - " this.name = name;\n" + - " this.value = value;\n" + - " }\n" + - "\n" + - " abstract find(target: string, cnt: int): Person;\n" + - " abstract add(int, string): Person;\n" + - " abstract nameAbs: string;\n" + - "}"; + String testClass = this.testClass_3; CodePointCharStream cStream = CharStreams.fromString(testClass); ParseBase parser = ParseFactory.getParser("ts"); ParseObj po = parser.parseCStream(cStream); @@ -234,19 +250,7 @@ class ParseTsTest { @Test void parseCStreamClass_4() { - String testClass = "abstract class Person {\n" + - " name: string;\n" + - " value: int;\n" + - "\n" + - " constructor(string, int) {\n" + - " this.name = name;\n" + - " this.value = value;\n" + - " }\n" + - "\n" + - " abstract find(string): Person;\n" + - " abstract add(int, string): Person;\n" + - " abstract nameAbs: string;\n" + - "}"; + String testClass = testClass_4; CodePointCharStream cStream = CharStreams.fromString(testClass); ParseBase parser = ParseFactory.getParser("ts"); ParseObj po = parser.parseCStream(cStream); -- Gitee From 8364dc4e23b24b44162303dde76a05374d48d3ef Mon Sep 17 00:00:00 2001 From: wangshi Date: Fri, 14 Mar 2025 17:29:43 +0800 Subject: [PATCH 3/3] fix code check Signed-off-by: wangshi --- .../src/test/java/parse/ParseTsTest.java | 39 ++++++++----------- 1 file changed, 17 insertions(+), 22 deletions(-) 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 ad141702..a8e7ac3f 100644 --- a/src/intellij_plugin/ohosgen/src/test/java/parse/ParseTsTest.java +++ b/src/intellij_plugin/ohosgen/src/test/java/parse/ParseTsTest.java @@ -35,7 +35,18 @@ import static org.junit.jupiter.api.Assertions.*; */ class ParseTsTest { - String testClass_3 = "abstract class Person {\n" + + String testClass2 = "abstract class Person {\n" + + " name: string;\n" + + "\n" + + " constructor(name: string) {\n" + + " this.name = name;\n" + + " }\n" + + "\n" + + " abstract find(target: string): Person;\n" + + " abstract nameAbs: string;\n" + + "}"; + + String testClass3 = "abstract class Person {\n" + " name: string;\n" + " value: int;\n" + "\n" + @@ -49,7 +60,7 @@ class ParseTsTest { " abstract nameAbs: string;\n" + "}"; - String testClass_4 = "abstract class Person {\n" + + String testClass4 = "abstract class Person {\n" + " name: string;\n" + " value: int;\n" + "\n" + @@ -145,16 +156,7 @@ class ParseTsTest { @Test void parseCStreamClass_2() { - String testClass = "abstract class Person {\n" + - " name: string;\n" + - "\n" + - " constructor(name: string) {\n" + - " this.name = name;\n" + - " }\n" + - "\n" + - " abstract find(target: string): Person;\n" + - " abstract nameAbs: string;\n" + - "}"; + String testClass = testClass2; CodePointCharStream cStream = CharStreams.fromString(testClass); ParseBase parser = ParseFactory.getParser("ts"); ParseObj po = parser.parseCStream(cStream); @@ -191,20 +193,16 @@ class ParseTsTest { @Test void parseCStreamClass_3() { - String testClass = this.testClass_3; + String testClass = this.testClass3; CodePointCharStream cStream = CharStreams.fromString(testClass); ParseBase parser = ParseFactory.getParser("ts"); ParseObj po = parser.parseCStream(cStream); List eol = po.getClassList(); assertEquals(1, eol.size()); ClassObj co = eol.get(0); - assertEquals("Person", co.getName()); List pl = co.getParamList(); assertEquals(3, pl.size()); - ParamObj poItem = pl.get(0); - assertEquals("name", poItem.getName()); - assertEquals("string", poItem.getType()); - poItem = pl.get(1); + ParamObj poItem = pl.get(1); assertEquals("value", poItem.getName()); assertEquals("int", poItem.getType()); poItem = pl.get(2); @@ -244,20 +242,18 @@ class ParseTsTest { assertEquals("int", poItem.getName()); assertEquals("int", poItem.getType()); poItem = pol.get(1); - assertEquals("string", poItem.getName()); assertEquals("string", poItem.getType()); } @Test void parseCStreamClass_4() { - String testClass = testClass_4; + String testClass = testClass4; CodePointCharStream cStream = CharStreams.fromString(testClass); ParseBase parser = ParseFactory.getParser("ts"); ParseObj po = parser.parseCStream(cStream); List eol = po.getClassList(); assertEquals(1, eol.size()); ClassObj co = eol.get(0); - assertEquals("Person", co.getName()); List pl = co.getParamList(); assertEquals(3, pl.size()); ParamObj poItem = pl.get(0); @@ -279,7 +275,6 @@ class ParseTsTest { assertEquals("string", poItem.getName()); assertEquals("string", poItem.getType()); poItem = pol.get(1); - assertEquals("int", poItem.getName()); assertEquals("int", poItem.getType()); foItem = fol.get(1); -- Gitee