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 c7a19b09213f00fa7b05efbaee702862df9b3d50..66b2f5b58dc888804c9095feaf5669d45f28e98a 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 @@ -40,6 +40,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 +273,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,6 +393,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)) { + 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); + } } @@ -457,6 +490,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 +558,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 +764,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 e8a0ab546f1a20a00b9ecea43405fa5d34d64d98..aad5514037d2a601a8c5451b8fd9815324320cad 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 20ff1fb08a22cb3b4fa39862000a25210d924253..a8e7ac3f7bfca1125d8c7b80beb47b7406b045ad 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,45 @@ import static org.junit.jupiter.api.Assertions.*; */ class ParseTsTest { + 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" + + " 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 testClass4 = "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() { } @@ -117,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(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); @@ -143,7 +173,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 +183,120 @@ 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 = 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); + List pl = co.getParamList(); + assertEquals(3, pl.size()); + ParamObj 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.getType()); + } + + @Test + void parseCStreamClass_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); + 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.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