From 99ba6e40eb43ef5fdb3cbcaae0e5adad29fce310 Mon Sep 17 00:00:00 2001 From: wangshi Date: Tue, 25 Mar 2025 21:01:34 +0800 Subject: [PATCH 01/10] add func test Signed-off-by: wangshi --- .../java/antlr/cpp/CPP14CustomListener.java | 71 ++ .../src/main/java/grammar/FuncObj.java | 15 + .../ohosgen/src/main/java/utils/CppToken.java | 5 + .../src/test/java/parse/ParseCppTest.java | 605 ++++++++++++++++++ 4 files changed, 696 insertions(+) diff --git a/src/intellij_plugin/ohosgen/src/main/java/antlr/cpp/CPP14CustomListener.java b/src/intellij_plugin/ohosgen/src/main/java/antlr/cpp/CPP14CustomListener.java index e771211d..946755da 100644 --- a/src/intellij_plugin/ohosgen/src/main/java/antlr/cpp/CPP14CustomListener.java +++ b/src/intellij_plugin/ohosgen/src/main/java/antlr/cpp/CPP14CustomListener.java @@ -19,6 +19,7 @@ import antlr.ParseBaseListener; import com.google.gson.Gson; import com.google.gson.GsonBuilder; import grammar.*; +import it.unimi.dsi.fastutil.bytes.F; import utils.Constants; import utils.CppToken; @@ -252,6 +253,14 @@ public class CPP14CustomListener extends CPP14ParserBaseListener implements Pars this.currentObject = so; this.currentToken = CppToken.CPP_TOKEN_STRUCT; this.structObjList.add(so); + } else if (key.equals(CppToken.CPP_TOKEN_CLASS)) { + ClassObj co = new ClassObj(); + String name = (ctx.classHead().classHeadName() != null) ? + ctx.classHead().classHeadName().getText() : ""; + co.setName(name); + this.currentObject = co; + this.currentToken = CppToken.CPP_TOKEN_CLASS; + this.classObjList.add(co); } } else if (ctx.classHead() != null && ctx.classHead().Union() != null) { String key = ctx.classHead().Union().getText(); @@ -297,6 +306,16 @@ public class CPP14CustomListener extends CPP14ParserBaseListener implements Pars po.setType(type); uo.addMember(po); } + } else if (this.currentObject instanceof ClassObj co) { + String type = ctx.declSpecifierSeq().getText(); + String name = ctx.memberDeclaratorList().getText(); + List mdcl = ctx.memberDeclaratorList().memberDeclarator(); + for (CPP14Parser.MemberDeclaratorContext mdc : mdcl) { + ParamObj po = new ParamObj(); + po.setName(mdc.getText()); + po.setType(type); + co.addParam(po); + } } } @@ -324,6 +343,7 @@ public class CPP14CustomListener extends CPP14ParserBaseListener implements Pars public void enterDeclaration(CPP14Parser.DeclarationContext ctx) { super.enterDeclaration(ctx); System.out.println("c/cpp declaration: " + ctx.getText()); + } @Override @@ -336,6 +356,47 @@ public class CPP14CustomListener extends CPP14ParserBaseListener implements Pars public void enterSimpleDeclaration(CPP14Parser.SimpleDeclarationContext ctx) { super.enterSimpleDeclaration(ctx); System.out.println("c/cpp enterSimpleDeclaration: " + ctx.getText()); + + if (ctx.initDeclaratorList() != null) { + CPP14Parser.NoPointerDeclaratorContext npdc = ctx.initDeclaratorList().initDeclarator(0). + declarator().pointerDeclarator().noPointerDeclarator(); + if (npdc.noPointerDeclarator() == null) { + return; + } + String name = npdc.noPointerDeclarator().getText(); + FuncObj fo = new FuncObj(); + fo.setName(name); + List dscl = ctx.declSpecifierSeq().declSpecifier(); + for (CPP14Parser.DeclSpecifierContext dsc : dscl) { + fo.addDecl(dsc.getText()); + } + if (npdc.parametersAndQualifiers().parameterDeclarationClause() != null) { + List pdcl = npdc.parametersAndQualifiers(). + parameterDeclarationClause().parameterDeclarationList().parameterDeclaration(); + for (CPP14Parser.ParameterDeclarationContext pdc : pdcl) { + String varName = pdc.declarator() != null ? pdc.declarator().getText() : ""; + String typeName = ""; + + List dscList = pdc.declSpecifierSeq().declSpecifier(); + for (CPP14Parser.DeclSpecifierContext dscItem : dscList) { + typeName += dscItem.getText() + " "; + } + if (dscList.size() > 0) { + typeName = typeName.substring(0, typeName.length() - 1); + } + + ParamObj po = new ParamObj(); + po.setName(varName); + po.setType(typeName); + fo.addParam(po); + } + } + + this.currentObject = fo; + this.currentToken = CppToken.CPP_TOKEN_FUNCTION; + this.funcObjList.add(fo); + } + } @Override @@ -354,10 +415,14 @@ public class CPP14CustomListener extends CPP14ParserBaseListener implements Pars so.setAlias(ctx.getText()); } else if (this.currentObject instanceof UnionObj uo) { uo.setAlias(ctx.getText()); + } else if (this.currentObject instanceof ClassObj co) { + co.setAlias(ctx.getText()); } } + + @Override public void enterAsmDefinition(CPP14Parser.AsmDefinitionContext ctx) { super.enterAsmDefinition(ctx); @@ -542,6 +607,12 @@ public class CPP14CustomListener extends CPP14ParserBaseListener implements Pars System.out.println("c/cpp exit translation unit: " + ctx.getText()); } + @Override + public void enterFunctionSpecifier(CPP14Parser.FunctionSpecifierContext ctx) { + super.enterFunctionSpecifier(ctx); + System.out.println("c/cpp enterFunctionSpecifier: " + ctx.getText()); + } + @Override public String dump2JsonStr() { Gson gson = new GsonBuilder().setPrettyPrinting().create(); diff --git a/src/intellij_plugin/ohosgen/src/main/java/grammar/FuncObj.java b/src/intellij_plugin/ohosgen/src/main/java/grammar/FuncObj.java index 4dff2959..4a23ad8d 100644 --- a/src/intellij_plugin/ohosgen/src/main/java/grammar/FuncObj.java +++ b/src/intellij_plugin/ohosgen/src/main/java/grammar/FuncObj.java @@ -70,6 +70,11 @@ public class FuncObj extends GBaseObject { */ private List tempList; + /** + * 声明类型 + */ + private List decList; + /** * 构造函数 */ @@ -80,6 +85,7 @@ public class FuncObj extends GBaseObject { this.retValue = TsToken.TS_TOKEN_VOID; this.paramList = new CopyOnWriteArrayList<>(); this.tempList = new CopyOnWriteArrayList<>(); + this.decList = new CopyOnWriteArrayList<>(); } /** @@ -298,4 +304,13 @@ public class FuncObj extends GBaseObject { po.setDecorator(decorator); this.paramList.add(po); } + + /** + * 添加声明类型 + * + * @param decName + */ + public void addDecl(String decName) { + this.decList.add(decName); + } } diff --git a/src/intellij_plugin/ohosgen/src/main/java/utils/CppToken.java b/src/intellij_plugin/ohosgen/src/main/java/utils/CppToken.java index 90e918b6..bf196137 100644 --- a/src/intellij_plugin/ohosgen/src/main/java/utils/CppToken.java +++ b/src/intellij_plugin/ohosgen/src/main/java/utils/CppToken.java @@ -25,6 +25,11 @@ package utils; * @since 2025-02-28 */ public class CppToken { + /** + * func token + */ + public static final String CPP_TOKEN_FUNCTION = "function"; + /** * alignas token */ diff --git a/src/intellij_plugin/ohosgen/src/test/java/parse/ParseCppTest.java b/src/intellij_plugin/ohosgen/src/test/java/parse/ParseCppTest.java index 1c629555..1f198f2b 100644 --- a/src/intellij_plugin/ohosgen/src/test/java/parse/ParseCppTest.java +++ b/src/intellij_plugin/ohosgen/src/test/java/parse/ParseCppTest.java @@ -362,6 +362,611 @@ class ParseCppTest { } + @Test + void parseCStreamClass1() { + ParseBase parser = ParseFactory.getParser("cpp"); + String testEnum = "class tree_el {\n" + + " int val;\n" + + " struct tree_el * right, * left;\n" + + "};"; + CodePointCharStream cStream = CharStreams.fromString(testEnum); + ParseObj po = parser.parseCStream(cStream); + List col = po.getClassList(); + assertEquals(1, col.size()); + ClassObj co = col.get(0); + assertEquals("tree_el", co.getName()); + + List pl = co.getParamList(); + assertEquals(3, pl.size()); + assertEquals("val", pl.get(0).getName()); + assertEquals("int", pl.get(0).getType()); + assertEquals("structtree_el", pl.get(1).getType()); + assertEquals("*right", pl.get(1).getName()); + assertEquals("structtree_el", pl.get(2).getType()); + assertEquals("*left", pl.get(2).getName()); + + } + + @Test + void parseCStreamClass2() { + ParseBase parser = ParseFactory.getParser("cpp"); + String testEnum = "class {\n" + + " int val;\n" + + " struct tree_el * right, * left;\n" + + "};"; + CodePointCharStream cStream = CharStreams.fromString(testEnum); + ParseObj po = parser.parseCStream(cStream); + List col = po.getClassList(); + assertEquals(1, col.size()); + ClassObj co = col.get(0); + + List pl = co.getParamList(); + assertEquals(3, pl.size()); + assertEquals("val", pl.get(0).getName()); + assertEquals("int", pl.get(0).getType()); + assertEquals("structtree_el", pl.get(1).getType()); + assertEquals("*right", pl.get(1).getName()); + assertEquals("structtree_el", pl.get(2).getType()); + assertEquals("*left", pl.get(2).getName()); + + } + @Test + void parseCStreamClass3() { + ParseBase parser = ParseFactory.getParser("cpp"); + String testEnum = "typedef class {\n" + + " int val;\n" + + " struct tree_el * right, * left;\n" + + "} tree_el_T;"; + CodePointCharStream cStream = CharStreams.fromString(testEnum); + ParseObj po = parser.parseCStream(cStream); + List col = po.getClassList(); + assertEquals(1, col.size()); + ClassObj co = col.get(0); + assertEquals("tree_el_T", co.getAlias()); + + List pl = co.getParamList(); + assertEquals(3, pl.size()); + assertEquals("val", pl.get(0).getName()); + assertEquals("int", pl.get(0).getType()); + assertEquals("structtree_el", pl.get(1).getType()); + assertEquals("*right", pl.get(1).getName()); + assertEquals("structtree_el", pl.get(2).getType()); + assertEquals("*left", pl.get(2).getName()); + + } + + @Test + void parseCStreamClass4() { + ParseBase parser = ParseFactory.getParser("cpp"); + String testEnum = "typedef class tree_el {\n" + + " int val;\n" + + " struct tree_el * right, * left;\n" + + "} tree_el_T;"; + CodePointCharStream cStream = CharStreams.fromString(testEnum); + ParseObj po = parser.parseCStream(cStream); + List col = po.getClassList(); + assertEquals(1, col.size()); + ClassObj co = col.get(0); + assertEquals("tree_el", co.getName()); + assertEquals("tree_el_T", co.getAlias()); + + List pl = co.getParamList(); + assertEquals(3, pl.size()); + assertEquals("val", pl.get(0).getName()); + assertEquals("int", pl.get(0).getType()); + assertEquals("structtree_el", pl.get(1).getType()); + assertEquals("*right", pl.get(1).getName()); + assertEquals("structtree_el", pl.get(2).getType()); + assertEquals("*left", pl.get(2).getName()); + + } + + @Test + void parseCStreamFunction1() { + ParseBase parser = ParseFactory.getParser("cpp"); + String testEnum = "int foo();"; + CodePointCharStream cStream = CharStreams.fromString(testEnum); + ParseObj po = parser.parseCStream(cStream); + List fol = po.getFuncList(); + assertEquals(1, fol.size()); + FuncObj fo = fol.get(0); + assertEquals("foo", fo.getName()); + + } + + @Test + void parseCStreamFunction2() { + ParseBase parser = ParseFactory.getParser("cpp"); + String testEnum = "int foo( short s );"; + CodePointCharStream cStream = CharStreams.fromString(testEnum); + ParseObj po = parser.parseCStream(cStream); + List fol = po.getFuncList(); + assertEquals(1, fol.size()); + FuncObj fo = fol.get(0); + assertEquals("foo", fo.getName()); + + List pl = fo.getParamList(); + assertEquals(1, pl.size()); + assertEquals("s", pl.get(0).getName()); + assertEquals("short", pl.get(0).getType()); + } + + @Test + void parseCStreamFunction3() { + ParseBase parser = ParseFactory.getParser("cpp"); + String testEnum = "int foo( short int si );"; + CodePointCharStream cStream = CharStreams.fromString(testEnum); + ParseObj po = parser.parseCStream(cStream); + List fol = po.getFuncList(); + assertEquals(1, fol.size()); + FuncObj fo = fol.get(0); + assertEquals("foo", fo.getName()); + + List pl = fo.getParamList(); + assertEquals(1, pl.size()); + assertEquals("si", pl.get(0).getName()); + assertEquals("short int", pl.get(0).getType()); + } + + @Test + void parseCStreamFunction4() { + ParseBase parser = ParseFactory.getParser("cpp"); + String testEnum = "int foo( signed short ss );"; + CodePointCharStream cStream = CharStreams.fromString(testEnum); + ParseObj po = parser.parseCStream(cStream); + List fol = po.getFuncList(); + assertEquals(1, fol.size()); + FuncObj fo = fol.get(0); + assertEquals("foo", fo.getName()); + + List pl = fo.getParamList(); + assertEquals(1, pl.size()); + assertEquals("ss", pl.get(0).getName()); + assertEquals("signed short", pl.get(0).getType()); + } + + @Test + void parseCStreamFunction5() { + ParseBase parser = ParseFactory.getParser("cpp"); + String testEnum = "int foo( signed short int ssi);"; + CodePointCharStream cStream = CharStreams.fromString(testEnum); + ParseObj po = parser.parseCStream(cStream); + List fol = po.getFuncList(); + assertEquals(1, fol.size()); + FuncObj fo = fol.get(0); + assertEquals("foo", fo.getName()); + + List pl = fo.getParamList(); + assertEquals(1, pl.size()); + assertEquals("ssi", pl.get(0).getName()); + assertEquals("signed short int", pl.get(0).getType()); + } + + @Test + void parseCStreamFunction6() { + ParseBase parser = ParseFactory.getParser("cpp"); + String testEnum = "int foo( unsigned short us );"; + CodePointCharStream cStream = CharStreams.fromString(testEnum); + ParseObj po = parser.parseCStream(cStream); + List fol = po.getFuncList(); + assertEquals(1, fol.size()); + FuncObj fo = fol.get(0); + assertEquals("foo", fo.getName()); + + List pl = fo.getParamList(); + assertEquals(1, pl.size()); + assertEquals("us", pl.get(0).getName()); + assertEquals("unsigned short", pl.get(0).getType()); + } + + @Test + void parseCStreamFunction7() { + ParseBase parser = ParseFactory.getParser("cpp"); + String testEnum = "int foo( unsigned short int usi );"; + CodePointCharStream cStream = CharStreams.fromString(testEnum); + ParseObj po = parser.parseCStream(cStream); + List fol = po.getFuncList(); + assertEquals(1, fol.size()); + FuncObj fo = fol.get(0); + assertEquals("foo", fo.getName()); + + List pl = fo.getParamList(); + assertEquals(1, pl.size()); + assertEquals("usi", pl.get(0).getName()); + assertEquals("unsigned short int", pl.get(0).getType()); + } + + @Test + void parseCStreamFunction8() { + ParseBase parser = ParseFactory.getParser("cpp"); + String testEnum = "int foo( int i);"; + CodePointCharStream cStream = CharStreams.fromString(testEnum); + ParseObj po = parser.parseCStream(cStream); + List fol = po.getFuncList(); + assertEquals(1, fol.size()); + FuncObj fo = fol.get(0); + assertEquals("foo", fo.getName()); + + List pl = fo.getParamList(); + assertEquals(1, pl.size()); + assertEquals("i", pl.get(0).getName()); + assertEquals("int", pl.get(0).getType()); + } + + @Test + void parseCStreamFunction9() { + ParseBase parser = ParseFactory.getParser("cpp"); + String testEnum = "int foo( signed s );"; + CodePointCharStream cStream = CharStreams.fromString(testEnum); + ParseObj po = parser.parseCStream(cStream); + List fol = po.getFuncList(); + assertEquals(1, fol.size()); + FuncObj fo = fol.get(0); + assertEquals("foo", fo.getName()); + + List pl = fo.getParamList(); + assertEquals(1, pl.size()); + assertEquals("s", pl.get(0).getName()); + assertEquals("signed", pl.get(0).getType()); + } + + @Test + void parseCStreamFunction10() { + ParseBase parser = ParseFactory.getParser("cpp"); + String testEnum = "int foo( signed int si );"; + CodePointCharStream cStream = CharStreams.fromString(testEnum); + ParseObj po = parser.parseCStream(cStream); + List fol = po.getFuncList(); + assertEquals(1, fol.size()); + FuncObj fo = fol.get(0); + assertEquals("foo", fo.getName()); + + List pl = fo.getParamList(); + assertEquals(1, pl.size()); + assertEquals("si", pl.get(0).getName()); + assertEquals("signed int", pl.get(0).getType()); + } + + @Test + void parseCStreamFunction11() { + ParseBase parser = ParseFactory.getParser("cpp"); + String testEnum = "int foo( unsigned u );"; + CodePointCharStream cStream = CharStreams.fromString(testEnum); + ParseObj po = parser.parseCStream(cStream); + List fol = po.getFuncList(); + assertEquals(1, fol.size()); + FuncObj fo = fol.get(0); + assertEquals("foo", fo.getName()); + + List pl = fo.getParamList(); + assertEquals(1, pl.size()); + assertEquals("u", pl.get(0).getName()); + assertEquals("unsigned", pl.get(0).getType()); + } + + @Test + void parseCStreamFunction12() { + ParseBase parser = ParseFactory.getParser("cpp"); + String testEnum = "int foo( unsigned int ui );"; + CodePointCharStream cStream = CharStreams.fromString(testEnum); + ParseObj po = parser.parseCStream(cStream); + List fol = po.getFuncList(); + assertEquals(1, fol.size()); + FuncObj fo = fol.get(0); + assertEquals("foo", fo.getName()); + + List pl = fo.getParamList(); + assertEquals(1, pl.size()); + assertEquals("ui", pl.get(0).getName()); + assertEquals("unsigned int", pl.get(0).getType()); + } + + @Test + void parseCStreamFunction13() { + ParseBase parser = ParseFactory.getParser("cpp"); + String testEnum = "int foo( long l );"; + CodePointCharStream cStream = CharStreams.fromString(testEnum); + ParseObj po = parser.parseCStream(cStream); + List fol = po.getFuncList(); + assertEquals(1, fol.size()); + FuncObj fo = fol.get(0); + assertEquals("foo", fo.getName()); + + List pl = fo.getParamList(); + assertEquals(1, pl.size()); + assertEquals("l", pl.get(0).getName()); + assertEquals("long", pl.get(0).getType()); + } + + @Test + void parseCStreamFunction14() { + ParseBase parser = ParseFactory.getParser("cpp"); + String testEnum = "int foo( long int li );"; + CodePointCharStream cStream = CharStreams.fromString(testEnum); + ParseObj po = parser.parseCStream(cStream); + List fol = po.getFuncList(); + assertEquals(1, fol.size()); + FuncObj fo = fol.get(0); + assertEquals("foo", fo.getName()); + + List pl = fo.getParamList(); + assertEquals(1, pl.size()); + assertEquals("li", pl.get(0).getName()); + assertEquals("long int", pl.get(0).getType()); + } + + @Test + void parseCStreamFunction15() { + ParseBase parser = ParseFactory.getParser("cpp"); + String testEnum = "int foo( signed long sl );"; + CodePointCharStream cStream = CharStreams.fromString(testEnum); + ParseObj po = parser.parseCStream(cStream); + List fol = po.getFuncList(); + assertEquals(1, fol.size()); + FuncObj fo = fol.get(0); + assertEquals("foo", fo.getName()); + + List pl = fo.getParamList(); + assertEquals(1, pl.size()); + assertEquals("sl", pl.get(0).getName()); + assertEquals("signed long", pl.get(0).getType()); + } + + @Test + void parseCStreamFunction16() { + ParseBase parser = ParseFactory.getParser("cpp"); + String testEnum = "int foo( signed long int sli );"; + CodePointCharStream cStream = CharStreams.fromString(testEnum); + ParseObj po = parser.parseCStream(cStream); + List fol = po.getFuncList(); + assertEquals(1, fol.size()); + FuncObj fo = fol.get(0); + assertEquals("foo", fo.getName()); + + List pl = fo.getParamList(); + assertEquals(1, pl.size()); + assertEquals("sli", pl.get(0).getName()); + assertEquals("signed long int", pl.get(0).getType()); + } + + @Test + void parseCStreamFunction17() { + ParseBase parser = ParseFactory.getParser("cpp"); + String testEnum = "int foo( unsigned long ul );"; + CodePointCharStream cStream = CharStreams.fromString(testEnum); + ParseObj po = parser.parseCStream(cStream); + List fol = po.getFuncList(); + assertEquals(1, fol.size()); + FuncObj fo = fol.get(0); + assertEquals("foo", fo.getName()); + + List pl = fo.getParamList(); + assertEquals(1, pl.size()); + assertEquals("ul", pl.get(0).getName()); + assertEquals("unsigned long", pl.get(0).getType()); + } + + @Test + void parseCStreamFunction18() { + ParseBase parser = ParseFactory.getParser("cpp"); + String testEnum = "int foo( unsigned long int uli );"; + CodePointCharStream cStream = CharStreams.fromString(testEnum); + ParseObj po = parser.parseCStream(cStream); + List fol = po.getFuncList(); + assertEquals(1, fol.size()); + FuncObj fo = fol.get(0); + assertEquals("foo", fo.getName()); + + List pl = fo.getParamList(); + assertEquals(1, pl.size()); + assertEquals("uli", pl.get(0).getName()); + assertEquals("unsigned long int", pl.get(0).getType()); + } + + @Test + void parseCStreamFunction19() { + ParseBase parser = ParseFactory.getParser("cpp"); + String testEnum = "int foo( long long ll );"; + CodePointCharStream cStream = CharStreams.fromString(testEnum); + ParseObj po = parser.parseCStream(cStream); + List fol = po.getFuncList(); + assertEquals(1, fol.size()); + FuncObj fo = fol.get(0); + assertEquals("foo", fo.getName()); + + List pl = fo.getParamList(); + assertEquals(1, pl.size()); + assertEquals("ll", pl.get(0).getName()); + assertEquals("long long", pl.get(0).getType()); + } + + @Test + void parseCStreamFunction20() { + ParseBase parser = ParseFactory.getParser("cpp"); + String testEnum = "int foo( long long int lli );"; + CodePointCharStream cStream = CharStreams.fromString(testEnum); + ParseObj po = parser.parseCStream(cStream); + List fol = po.getFuncList(); + assertEquals(1, fol.size()); + FuncObj fo = fol.get(0); + assertEquals("foo", fo.getName()); + + List pl = fo.getParamList(); + assertEquals(1, pl.size()); + assertEquals("lli", pl.get(0).getName()); + assertEquals("long long int", pl.get(0).getType()); + } + + @Test + void parseCStreamFunction21() { + ParseBase parser = ParseFactory.getParser("cpp"); + String testEnum = "int foo( signed long long sll );"; + CodePointCharStream cStream = CharStreams.fromString(testEnum); + ParseObj po = parser.parseCStream(cStream); + List fol = po.getFuncList(); + assertEquals(1, fol.size()); + FuncObj fo = fol.get(0); + assertEquals("foo", fo.getName()); + + List pl = fo.getParamList(); + assertEquals(1, pl.size()); + assertEquals("sll", pl.get(0).getName()); + assertEquals("signed long long", pl.get(0).getType()); + } + + @Test + void parseCStreamFunction22() { + ParseBase parser = ParseFactory.getParser("cpp"); + String testEnum = "int foo( signed long long int slli );"; + CodePointCharStream cStream = CharStreams.fromString(testEnum); + ParseObj po = parser.parseCStream(cStream); + List fol = po.getFuncList(); + assertEquals(1, fol.size()); + FuncObj fo = fol.get(0); + assertEquals("foo", fo.getName()); + + List pl = fo.getParamList(); + assertEquals(1, pl.size()); + assertEquals("slli", pl.get(0).getName()); + assertEquals("signed long long int", pl.get(0).getType()); + } + + @Test + void parseCStreamFunction23() { + ParseBase parser = ParseFactory.getParser("cpp"); + String testEnum = "int foo( unsigned long long ull );"; + CodePointCharStream cStream = CharStreams.fromString(testEnum); + ParseObj po = parser.parseCStream(cStream); + List fol = po.getFuncList(); + assertEquals(1, fol.size()); + FuncObj fo = fol.get(0); + assertEquals("foo", fo.getName()); + + List pl = fo.getParamList(); + assertEquals(1, pl.size()); + assertEquals("ull", pl.get(0).getName()); + assertEquals("unsigned long long", pl.get(0).getType()); + } + + @Test + void parseCStreamFunction24() { + ParseBase parser = ParseFactory.getParser("cpp"); + String testEnum = "int foo( unsigned long long int ulli );"; + CodePointCharStream cStream = CharStreams.fromString(testEnum); + ParseObj po = parser.parseCStream(cStream); + List fol = po.getFuncList(); + assertEquals(1, fol.size()); + FuncObj fo = fol.get(0); + assertEquals("foo", fo.getName()); + + List pl = fo.getParamList(); + assertEquals(1, pl.size()); + assertEquals("ulli", pl.get(0).getName()); + assertEquals("unsigned long long int", pl.get(0).getType()); + } + + @Test + void parseCStreamFunction25() { + ParseBase parser = ParseFactory.getParser("cpp"); + String testEnum = "int foo( double d );"; + CodePointCharStream cStream = CharStreams.fromString(testEnum); + ParseObj po = parser.parseCStream(cStream); + List fol = po.getFuncList(); + assertEquals(1, fol.size()); + FuncObj fo = fol.get(0); + assertEquals("foo", fo.getName()); + + List pl = fo.getParamList(); + assertEquals(1, pl.size()); + assertEquals("d", pl.get(0).getName()); + assertEquals("double", pl.get(0).getType()); + } + + @Test + void parseCStreamFunction26() { + ParseBase parser = ParseFactory.getParser("cpp"); + String testEnum = "int foo( long double ld );"; + CodePointCharStream cStream = CharStreams.fromString(testEnum); + ParseObj po = parser.parseCStream(cStream); + List fol = po.getFuncList(); + assertEquals(1, fol.size()); + FuncObj fo = fol.get(0); + assertEquals("foo", fo.getName()); + + List pl = fo.getParamList(); + assertEquals(1, pl.size()); + assertEquals("ld", pl.get(0).getName()); + assertEquals("long double", pl.get(0).getType()); + } + + @Test + void parseCStreamFunction27() { + ParseBase parser = ParseFactory.getParser("cpp"); + String testEnum = "int foo( unsigned long );"; + CodePointCharStream cStream = CharStreams.fromString(testEnum); + ParseObj po = parser.parseCStream(cStream); + List fol = po.getFuncList(); + assertEquals(1, fol.size()); + FuncObj fo = fol.get(0); + assertEquals("foo", fo.getName()); + + List pl = fo.getParamList(); + assertEquals(1, pl.size()); + assertEquals("", pl.get(0).getName()); + assertEquals("unsigned long", pl.get(0).getType()); + } + + @Test + void parseCStreamFunction28() { + ParseBase parser = ParseFactory.getParser("cpp"); + String testEnum = "int foo( unsigned long int );"; + CodePointCharStream cStream = CharStreams.fromString(testEnum); + ParseObj po = parser.parseCStream(cStream); + List fol = po.getFuncList(); + assertEquals(1, fol.size()); + FuncObj fo = fol.get(0); + assertEquals("foo", fo.getName()); + + List pl = fo.getParamList(); + assertEquals(1, pl.size()); + assertEquals("", pl.get(0).getName()); + assertEquals("unsigned long int", pl.get(0).getType()); + } + + @Test + void parseCStreamFunction29() { + ParseBase parser = ParseFactory.getParser("cpp"); + String testEnum = "int foo( long long );"; + CodePointCharStream cStream = CharStreams.fromString(testEnum); + ParseObj po = parser.parseCStream(cStream); + List fol = po.getFuncList(); + assertEquals(1, fol.size()); + FuncObj fo = fol.get(0); + assertEquals("foo", fo.getName()); + + List pl = fo.getParamList(); + assertEquals(1, pl.size()); + assertEquals("", pl.get(0).getName()); + assertEquals("long long", pl.get(0).getType()); + } + + @Test + void parseCStreamFunction30() { + ParseBase parser = ParseFactory.getParser("cpp"); + String testEnum = "int foo( long long int );"; + CodePointCharStream cStream = CharStreams.fromString(testEnum); + ParseObj po = parser.parseCStream(cStream); + List fol = po.getFuncList(); + assertEquals(1, fol.size()); + FuncObj fo = fol.get(0); + assertEquals("foo", fo.getName()); + + List pl = fo.getParamList(); + assertEquals(1, pl.size()); + assertEquals("", pl.get(0).getName()); + assertEquals("long long int", pl.get(0).getType()); + } + @Test void genParseResult() { } -- Gitee From 3df9fe0e6691c630dd13fe813422fb55a7832aa9 Mon Sep 17 00:00:00 2001 From: wangshi Date: Wed, 2 Apr 2025 14:12:45 +0800 Subject: [PATCH 02/10] add gen Signed-off-by: wangshi --- .../ohosgen/src/main/java/H2dtsAction.java | 2 +- .../main/java/antlr/ParseBaseListener.java | 13 + .../java/antlr/cpp/CPP14CustomListener.java | 393 ++++++++++++++++-- 3 files changed, 361 insertions(+), 47 deletions(-) diff --git a/src/intellij_plugin/ohosgen/src/main/java/H2dtsAction.java b/src/intellij_plugin/ohosgen/src/main/java/H2dtsAction.java index bd289f66..641001c0 100644 --- a/src/intellij_plugin/ohosgen/src/main/java/H2dtsAction.java +++ b/src/intellij_plugin/ohosgen/src/main/java/H2dtsAction.java @@ -58,7 +58,7 @@ public class H2dtsAction extends AnAction { * @param file 文件 */ private void doProgress(Project project, VirtualFile file) { - ParseTask pt = new ParseTask(project, "CPP", true); + ParseTask pt = new ParseTask(project, "H2DTS", true); pt.setFile(file); ProgressManager.getInstance().run(pt); } diff --git a/src/intellij_plugin/ohosgen/src/main/java/antlr/ParseBaseListener.java b/src/intellij_plugin/ohosgen/src/main/java/antlr/ParseBaseListener.java index 9f0c208d..202e23e7 100644 --- a/src/intellij_plugin/ohosgen/src/main/java/antlr/ParseBaseListener.java +++ b/src/intellij_plugin/ohosgen/src/main/java/antlr/ParseBaseListener.java @@ -26,6 +26,19 @@ package antlr; */ public interface ParseBaseListener { + /** + * removeLastSpace + * + * @param str 删除字符串最后一个空格 + * @return 返回字符串 + */ + public static String removeLastSpace(String str) { + if (str != null && !str.isEmpty() && str.charAt(str.length() - 1) == ' ') { + return str.substring(0, str.length() - 1); + } + return str; + } + /** * 打印 json str * diff --git a/src/intellij_plugin/ohosgen/src/main/java/antlr/cpp/CPP14CustomListener.java b/src/intellij_plugin/ohosgen/src/main/java/antlr/cpp/CPP14CustomListener.java index 946755da..0003190d 100644 --- a/src/intellij_plugin/ohosgen/src/main/java/antlr/cpp/CPP14CustomListener.java +++ b/src/intellij_plugin/ohosgen/src/main/java/antlr/cpp/CPP14CustomListener.java @@ -19,9 +19,9 @@ import antlr.ParseBaseListener; import com.google.gson.Gson; import com.google.gson.GsonBuilder; import grammar.*; -import it.unimi.dsi.fastutil.bytes.F; import utils.Constants; import utils.CppToken; +import utils.StringUtils; import java.util.List; import java.util.concurrent.CopyOnWriteArrayList; @@ -47,7 +47,7 @@ public class CPP14CustomListener extends CPP14ParserBaseListener implements Pars private List typeObjList; private List unionObjList; private List interfaceObjList; - + private List constList; /** * 构造函数 */ @@ -59,6 +59,7 @@ public class CPP14CustomListener extends CPP14ParserBaseListener implements Pars typeObjList = new CopyOnWriteArrayList<>(); unionObjList = new CopyOnWriteArrayList<>(); interfaceObjList = new CopyOnWriteArrayList<>(); + constList = new CopyOnWriteArrayList<>(); } /** @@ -233,6 +234,24 @@ public class CPP14CustomListener extends CPP14ParserBaseListener implements Pars this.unionObjList = unionObjList; } + /** + * 获取常量列表 + * + * @return 返回常量列表 + */ + public List getConstList() { + return constList; + } + + /** + * 设置常量列表 + * + * @param constList 常量列表 + */ + public void setConstList(List constList) { + this.constList = constList; + } + @Override public void enterAbstractDeclarator(CPP14Parser.AbstractDeclaratorContext ctx) { super.enterAbstractDeclarator(ctx); @@ -288,33 +307,165 @@ public class CPP14CustomListener extends CPP14ParserBaseListener implements Pars System.out.println("c/cpp Memberdeclaration: " + ctx.getText()); if (this.currentObject instanceof StructObj so) { String type = ctx.declSpecifierSeq().getText(); + List dscl = ctx.declSpecifierSeq().declSpecifier(); + type = !dscl.isEmpty() ? "" : type; + for (CPP14Parser.DeclSpecifierContext dscItem : dscl) { + if (dscItem.typeSpecifier() != null && + dscItem.typeSpecifier().trailingTypeSpecifier().elaboratedTypeSpecifier() != null) { + CPP14Parser.ElaboratedTypeSpecifierContext esc = + dscItem.typeSpecifier().trailingTypeSpecifier().elaboratedTypeSpecifier(); + type += esc.Enum() != null ? esc.Enum().getText() + " " : ""; + type += esc.classKey() != null ? esc.classKey().getText() + " " : ""; + type += esc.Identifier(); + } else { + type += dscItem.getText() + " "; + } + } + if (!dscl.isEmpty()) { + type = StringUtils.removeLastSpace(type); + } String name = ctx.memberDeclaratorList().getText(); List mdcl = ctx.memberDeclaratorList().memberDeclarator(); for (CPP14Parser.MemberDeclaratorContext mdc : mdcl) { - ParamObj po = new ParamObj(); - po.setName(mdc.getText()); - po.setType(type); - so.addMember(po); + if (mdc.declarator() != null && mdc.declarator().pointerDeclarator() != null && + mdc.declarator().pointerDeclarator().noPointerDeclarator() != null && + mdc.declarator().pointerDeclarator().noPointerDeclarator().constantExpression() == null && + mdc.declarator().pointerDeclarator().noPointerDeclarator().noPointerDeclarator() != null) { + CPP14Parser.NoPointerDeclaratorContext npdcItem = + mdc.declarator().pointerDeclarator().noPointerDeclarator(); + FuncObj fo = new FuncObj(); + fo.setName(npdcItem.noPointerDeclarator().getText()); + fo.setRetValue(type); + + if (npdcItem.parametersAndQualifiers() != null && + npdcItem.parametersAndQualifiers().parameterDeclarationClause() != null && + npdcItem.parametersAndQualifiers().parameterDeclarationClause().parameterDeclarationList() != null) { + List pdcList = + npdcItem.parametersAndQualifiers().parameterDeclarationClause(). + parameterDeclarationList().parameterDeclaration(); + for (CPP14Parser.ParameterDeclarationContext pdcItem : pdcList) { + ParamObj poItem = new ParamObj(); + poItem.setName(pdcItem.declarator().getText()); + poItem.setType(pdcItem.declSpecifierSeq().getText()); + fo.addParam(poItem); + } + } + + so.addFunc(fo); + } else { + ParamObj po = new ParamObj(); + po.setName(mdc.declarator().getText()); + if (mdc.braceOrEqualInitializer() != null) { + po.setStrValue(mdc.braceOrEqualInitializer().initializerClause().getText()); + } + po.setType(type); + so.addMember(po); + } } } else if (this.currentObject instanceof UnionObj uo) { String type = ctx.declSpecifierSeq().getText(); + List dscl = ctx.declSpecifierSeq().declSpecifier(); + type = !dscl.isEmpty() ? "" : type; + for (CPP14Parser.DeclSpecifierContext dscItem : dscl) { + type += dscItem.getText() + " "; + } + if (!dscl.isEmpty()) { + type = StringUtils.removeLastSpace(type); + } String name = ctx.memberDeclaratorList().getText(); List mdcl = ctx.memberDeclaratorList().memberDeclarator(); for (CPP14Parser.MemberDeclaratorContext mdc : mdcl) { - ParamObj po = new ParamObj(); - po.setName(mdc.getText()); - po.setType(type); - uo.addMember(po); + if (mdc.declarator() != null && mdc.declarator().pointerDeclarator() != null && + mdc.declarator().pointerDeclarator().noPointerDeclarator() != null && + mdc.declarator().pointerDeclarator().noPointerDeclarator().constantExpression() == null && + mdc.declarator().pointerDeclarator().noPointerDeclarator().noPointerDeclarator() != null) { + CPP14Parser.NoPointerDeclaratorContext npdcItem = + mdc.declarator().pointerDeclarator().noPointerDeclarator(); + FuncObj fo = new FuncObj(); + fo.setName(npdcItem.noPointerDeclarator().getText()); + fo.setRetValue(type); + + if (npdcItem.parametersAndQualifiers() != null && + npdcItem.parametersAndQualifiers().parameterDeclarationClause() != null && + npdcItem.parametersAndQualifiers().parameterDeclarationClause().parameterDeclarationList() != null) { + List pdcList = + npdcItem.parametersAndQualifiers().parameterDeclarationClause(). + parameterDeclarationList().parameterDeclaration(); + for (CPP14Parser.ParameterDeclarationContext pdcItem : pdcList) { + ParamObj poItem = new ParamObj(); + poItem.setName(pdcItem.declarator().getText()); + poItem.setType(pdcItem.declSpecifierSeq().getText()); + fo.addParam(poItem); + } + } + + uo.addFunc(fo); + } else { + ParamObj po = new ParamObj(); + po.setName(mdc.declarator().getText()); + if (mdc.braceOrEqualInitializer() != null) { + po.setStrValue(mdc.braceOrEqualInitializer().initializerClause().getText()); + } + po.setType(type); + uo.addMember(po); + } } } else if (this.currentObject instanceof ClassObj co) { String type = ctx.declSpecifierSeq().getText(); + List dscl = ctx.declSpecifierSeq().declSpecifier(); + type = !dscl.isEmpty() ? "" : type; + for (CPP14Parser.DeclSpecifierContext dscItem : dscl) { + if (dscItem.typeSpecifier() != null && + dscItem.typeSpecifier().trailingTypeSpecifier().elaboratedTypeSpecifier() != null) { + CPP14Parser.ElaboratedTypeSpecifierContext esc = + dscItem.typeSpecifier().trailingTypeSpecifier().elaboratedTypeSpecifier(); + type += esc.Enum() != null ? esc.Enum().getText() + " " : ""; + type += esc.classKey() != null ? esc.classKey().getText() + " " : ""; + type += esc.Identifier(); + } else { + type += dscItem.getText() + " "; + } + } + if (!dscl.isEmpty()) { + type = StringUtils.removeLastSpace(type); + } String name = ctx.memberDeclaratorList().getText(); List mdcl = ctx.memberDeclaratorList().memberDeclarator(); for (CPP14Parser.MemberDeclaratorContext mdc : mdcl) { - ParamObj po = new ParamObj(); - po.setName(mdc.getText()); - po.setType(type); - co.addParam(po); + if (mdc.declarator() != null && mdc.declarator().pointerDeclarator() != null && + mdc.declarator().pointerDeclarator().noPointerDeclarator() != null && + mdc.declarator().pointerDeclarator().noPointerDeclarator().constantExpression() == null && + mdc.declarator().pointerDeclarator().noPointerDeclarator().noPointerDeclarator() != null) { + CPP14Parser.NoPointerDeclaratorContext npdcItem = + mdc.declarator().pointerDeclarator().noPointerDeclarator(); + FuncObj fo = new FuncObj(); + fo.setName(npdcItem.noPointerDeclarator().getText()); + fo.setRetValue(type); + + if (npdcItem.parametersAndQualifiers() != null && + npdcItem.parametersAndQualifiers().parameterDeclarationClause() != null && + npdcItem.parametersAndQualifiers().parameterDeclarationClause().parameterDeclarationList() != null) { + List pdcList = + npdcItem.parametersAndQualifiers().parameterDeclarationClause(). + parameterDeclarationList().parameterDeclaration(); + for (CPP14Parser.ParameterDeclarationContext pdcItem : pdcList) { + ParamObj poItem = new ParamObj(); + poItem.setName(pdcItem.declarator().getText()); + poItem.setType(pdcItem.declSpecifierSeq().getText()); + fo.addParam(poItem); + } + } + + co.addFunc(fo); + } else { + ParamObj po = new ParamObj(); + po.setName(mdc.declarator().getText()); + if (mdc.braceOrEqualInitializer() != null) { + po.setStrValue(mdc.braceOrEqualInitializer().initializerClause().getText()); + } + po.setType(type); + co.addParam(po); + } } } @@ -330,7 +481,30 @@ public class CPP14CustomListener extends CPP14ParserBaseListener implements Pars public void enterDeclarationseq(CPP14Parser.DeclarationseqContext ctx) { super.enterDeclarationseq(ctx); System.out.println("c/cpp enterDeclarationseq: " + ctx.getText()); + } + @Override + public void enterInclusiveOrExpression(CPP14Parser.InclusiveOrExpressionContext ctx) { + super.enterInclusiveOrExpression(ctx); + System.out.println("c/cpp enterInclusiveOrExpression: " + ctx.getText()); + } + + @Override + public void enterAbstractPackDeclarator(CPP14Parser.AbstractPackDeclaratorContext ctx) { + super.enterAbstractPackDeclarator(ctx); + System.out.println("c/cpp enterAbstractPackDeclarator: " + ctx.getText()); + } + + @Override + public void enterBaseTypeSpecifier(CPP14Parser.BaseTypeSpecifierContext ctx) { + super.enterBaseTypeSpecifier(ctx); + System.out.println("c/cpp enterBaseTypeSpecifier: " + ctx.getText()); + } + + @Override + public void enterHandler(CPP14Parser.HandlerContext ctx) { + super.enterHandler(ctx); + System.out.println("c/cpp enterHandler: " + ctx.getText()); } @Override @@ -360,43 +534,58 @@ public class CPP14CustomListener extends CPP14ParserBaseListener implements Pars if (ctx.initDeclaratorList() != null) { CPP14Parser.NoPointerDeclaratorContext npdc = ctx.initDeclaratorList().initDeclarator(0). declarator().pointerDeclarator().noPointerDeclarator(); - if (npdc.noPointerDeclarator() == null) { - return; - } - String name = npdc.noPointerDeclarator().getText(); - FuncObj fo = new FuncObj(); - fo.setName(name); - List dscl = ctx.declSpecifierSeq().declSpecifier(); - for (CPP14Parser.DeclSpecifierContext dsc : dscl) { - fo.addDecl(dsc.getText()); - } - if (npdc.parametersAndQualifiers().parameterDeclarationClause() != null) { - List pdcl = npdc.parametersAndQualifiers(). - parameterDeclarationClause().parameterDeclarationList().parameterDeclaration(); - for (CPP14Parser.ParameterDeclarationContext pdc : pdcl) { - String varName = pdc.declarator() != null ? pdc.declarator().getText() : ""; - String typeName = ""; - - List dscList = pdc.declSpecifierSeq().declSpecifier(); - for (CPP14Parser.DeclSpecifierContext dscItem : dscList) { - typeName += dscItem.getText() + " "; + if (npdc.noPointerDeclarator() != null) { + String name = npdc.noPointerDeclarator().getText(); + FuncObj fo = new FuncObj(); + fo.setName(name); + List dscl = ctx.declSpecifierSeq().declSpecifier(); + for (CPP14Parser.DeclSpecifierContext dsc : dscl) { + fo.addDecl(dsc.getText()); + } + if (npdc.parametersAndQualifiers().parameterDeclarationClause() != null) { + List pdcl = npdc.parametersAndQualifiers(). + parameterDeclarationClause().parameterDeclarationList().parameterDeclaration(); + for (CPP14Parser.ParameterDeclarationContext pdc : pdcl) { + String varName = pdc.declarator() != null ? pdc.declarator().getText() : ""; + String typeName = ""; + + List dscList = pdc.declSpecifierSeq().declSpecifier(); + for (CPP14Parser.DeclSpecifierContext dscItem : dscList) { + typeName += dscItem.getText() + " "; + } + if (dscList.size() > 0) { + typeName = typeName.substring(0, typeName.length() - 1); + } + + ParamObj po = new ParamObj(); + po.setName(varName); + po.setType(typeName); + fo.addParam(po); } - if (dscList.size() > 0) { - typeName = typeName.substring(0, typeName.length() - 1); + } + + this.currentObject = fo; + this.currentToken = CppToken.CPP_TOKEN_FUNCTION; + this.funcObjList.add(fo); + } else { + if (ctx.initDeclaratorList().initDeclarator(0) != null && ctx.initDeclaratorList().initDeclarator(0).initializer() != null) { + String paType = ""; + List dscl = ctx.declSpecifierSeq().declSpecifier(); + for (CPP14Parser.DeclSpecifierContext item: dscl) { + paType += item.getText() + " "; } + paType = StringUtils.removeLastSpace(paType); + String paName = ctx.initDeclaratorList().initDeclarator(0).declarator().getText(); + String initValue = ctx.initDeclaratorList().initDeclarator(0).initializer().braceOrEqualInitializer().initializerClause().getText(); ParamObj po = new ParamObj(); - po.setName(varName); - po.setType(typeName); - fo.addParam(po); + po.setName(paName); + po.setType(paType); + po.setStrValue(initValue); + this.constList.add(po); } } - - this.currentObject = fo; - this.currentToken = CppToken.CPP_TOKEN_FUNCTION; - this.funcObjList.add(fo); } - } @Override @@ -418,16 +607,48 @@ public class CPP14CustomListener extends CPP14ParserBaseListener implements Pars } else if (this.currentObject instanceof ClassObj co) { co.setAlias(ctx.getText()); } + } + @Override + public void enterExpressionStatement(CPP14Parser.ExpressionStatementContext ctx) { + super.enterExpressionStatement(ctx); + System.out.println("c/cpp enterExpressionStatement: " + ctx.getText()); } + @Override + public void enterDeclarationStatement(CPP14Parser.DeclarationStatementContext ctx) { + super.enterDeclarationStatement(ctx); + System.out.println("c/cpp enterDeclarationStatement: " + ctx.getText()); + } + @Override + public void enterCompoundStatement(CPP14Parser.CompoundStatementContext ctx) { + super.enterCompoundStatement(ctx); + System.out.println("c/cpp enterCompoundStatement: " + ctx.getText()); + } + + @Override + public void enterDeclarator(CPP14Parser.DeclaratorContext ctx) { + super.enterDeclarator(ctx); + System.out.println("c/cpp enterDeclarator: " + ctx.getText()); + } + + @Override + public void enterEmptyDeclaration_(CPP14Parser.EmptyDeclaration_Context ctx) { + super.enterEmptyDeclaration_(ctx); + System.out.println("c/cpp enterEmptyDeclaration_: " + ctx.getText()); + } + + @Override + public void enterTranslationUnit(CPP14Parser.TranslationUnitContext ctx) { + super.enterTranslationUnit(ctx); + System.out.println("c/cpp enterTranslationUnit: " + ctx.getText()); + } @Override public void enterAsmDefinition(CPP14Parser.AsmDefinitionContext ctx) { super.enterAsmDefinition(ctx); System.out.println("c/cpp enterAsmDefinition: " + ctx.getText()); - } @Override @@ -520,10 +741,90 @@ public class CPP14CustomListener extends CPP14ParserBaseListener implements Pars System.out.println("c/cpp enterPseudoDestructorName: " + ctx.getText()); } + @Override + public void enterPrimaryExpression(CPP14Parser.PrimaryExpressionContext ctx) { + super.enterPrimaryExpression(ctx); + System.out.println("c/cpp enterPrimaryExpression: " + ctx.getText()); + } + + @Override + public void enterPostfixExpression(CPP14Parser.PostfixExpressionContext ctx) { + super.enterPostfixExpression(ctx); + System.out.println("c/cpp enterPostfixExpression: " + ctx.getText()); + } + + @Override + public void enterConstantExpression(CPP14Parser.ConstantExpressionContext ctx) { + super.enterConstantExpression(ctx); + System.out.println("c/cpp enterConstantExpression: " + ctx.getText()); + } + + @Override + public void enterVirtualSpecifier(CPP14Parser.VirtualSpecifierContext ctx) { + super.enterVirtualSpecifier(ctx); + System.out.println("c/cpp enterVirtualSpecifier: " + ctx.getText()); + } + + @Override + public void enterBalancedtoken(CPP14Parser.BalancedtokenContext ctx) { + super.enterBalancedtoken(ctx); + System.out.println("c/cpp enterBalancedtoken: " + ctx.getText()); + } + + @Override + public void enterBalancedTokenSeq(CPP14Parser.BalancedTokenSeqContext ctx) { + super.enterBalancedTokenSeq(ctx); + System.out.println("c/cpp enterBalancedtoken: " + ctx.getText()); + } + + @Override + public void enterBaseSpecifier(CPP14Parser.BaseSpecifierContext ctx) { + super.enterBaseSpecifier(ctx); + System.out.println("c/cpp enterBaseSpecifier: " + ctx.getText()); + } + + @Override + public void enterBaseClause(CPP14Parser.BaseClauseContext ctx) { + super.enterBaseClause(ctx); + System.out.println("c/cpp enterBaseClause: " + ctx.getText()); + } + @Override public void enterFunctionDefinition(CPP14Parser.FunctionDefinitionContext ctx) { super.enterFunctionDefinition(ctx); System.out.println("c/cpp function: " + ctx.getText()); + + String typeName = ctx.declSpecifierSeq().getText(); + List dscl = ctx.declSpecifierSeq().declSpecifier(); + if (!dscl.isEmpty()) { + typeName = ""; + } + for (CPP14Parser.DeclSpecifierContext dscItem : dscl) { + typeName += dscItem.getText() + " "; + } + typeName = ParseBaseListener.removeLastSpace(typeName); + String funcName = ctx.declarator().pointerDeclarator().noPointerDeclarator().noPointerDeclarator().getText(); + + FuncObj fo = new FuncObj(); + fo.setName(funcName); + fo.setRetValue(typeName); + + this.currentToken = CppToken.CPP_TOKEN_FUNCTION; + this.currentObject = fo; + this.funcObjList.add(fo); + + CPP14Parser.ParameterDeclarationListContext pdl = ctx.declarator().pointerDeclarator().noPointerDeclarator(). + parametersAndQualifiers().parameterDeclarationClause().parameterDeclarationList(); + List pdcl = pdl.parameterDeclaration(); + for (CPP14Parser.ParameterDeclarationContext pdcItem : pdcl) { + ParamObj po = new ParamObj(); + String type = pdcItem.declSpecifierSeq().getText(); + String name = pdcItem.declarator() != null ? pdcItem.declarator().getText() : ""; + po.setType(type); + po.setName(name); + fo.addParam(po); + } + } @Override @@ -544,7 +845,7 @@ public class CPP14CustomListener extends CPP14ParserBaseListener implements Pars List edcl = ctx.enumeratorList().enumeratorDefinition(); for (CPP14Parser.EnumeratorDefinitionContext edc : edcl) { String memName = edc.enumerator().getText(); - String memValue = edc.constantExpression().getText(); + String memValue = edc.constantExpression() != null ? edc.constantExpression().getText() : ""; eo.addMemberItem(memName); eo.addMemberValue(memValue); } -- Gitee From fd9c2ea0caf9d5a24e1f8997e1fb7dc236c88f3c Mon Sep 17 00:00:00 2001 From: wangshi Date: Wed, 2 Apr 2025 14:13:02 +0800 Subject: [PATCH 03/10] add cpp gen Signed-off-by: wangshi --- .../ohosgen/src/main/java/gen/GenCppFile.java | 90 ++++++++++++++++++- 1 file changed, 89 insertions(+), 1 deletion(-) 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 2420afb2..818fbfc2 100644 --- a/src/intellij_plugin/ohosgen/src/main/java/gen/GenCppFile.java +++ b/src/intellij_plugin/ohosgen/src/main/java/gen/GenCppFile.java @@ -15,6 +15,10 @@ package gen; +import grammar.*; + +import java.util.List; + /** *

类名:该类用于xxx

* description ${description} @@ -24,5 +28,89 @@ package gen; * @since 2025-02-28 * @version 1.0 */ -public class GenCppFile { +public class GenCppFile extends GeneratorBase { + /** + * 构造函数 + */ + GenCppFile() { + + } + + /** + * 生成输出内容 + * @param po + */ + @Override + public void genContent(ParseObj po) { + genInterfaceList(po.getInterfaceList()); + genEnumList(po.getEnumList()); + genClassList(po.getClassList()); + genFuncList(po.getFuncList()); + genStructList(po.getStructList()); + genTypeList(po.getTypeList()); + genUnionList(po.getUnionList()); + } + + /** + * 生成输出内容 + * @param iol + */ + @Override + public void genInterfaceList(List iol) { + + }; + + /** + * 生成输出内容 + * @param eol + */ + @Override + public void genEnumList(List eol) { + + }; + + /** + * 生成输出内容 + * @param col + */ + @Override + public void genClassList(List col) { + + }; + + /** + * 生成输出内容 + * @param fol + */ + @Override + public void genFuncList(List fol) { + + }; + + /** + * 生成输出内容 + * @param sol + */ + @Override + public void genStructList(List sol) { + + }; + + /** + * 生成输出内容 + * @param tol + */ + @Override + public void genTypeList(List tol) { + + }; + + /** + * 生成输出内容 + * @param uol + */ + @Override + public void genUnionList(List uol) { + + }; } -- Gitee From eef15d3a6eb81a3b1431a0cae27c5c83273e73da Mon Sep 17 00:00:00 2001 From: wangshi Date: Wed, 2 Apr 2025 14:13:15 +0800 Subject: [PATCH 04/10] add dts gen Signed-off-by: wangshi --- .../ohosgen/src/main/java/gen/GenDtsFile.java | 533 +++++++++++++++++- 1 file changed, 532 insertions(+), 1 deletion(-) diff --git a/src/intellij_plugin/ohosgen/src/main/java/gen/GenDtsFile.java b/src/intellij_plugin/ohosgen/src/main/java/gen/GenDtsFile.java index 1e9c5c9a..35fd9f50 100644 --- a/src/intellij_plugin/ohosgen/src/main/java/gen/GenDtsFile.java +++ b/src/intellij_plugin/ohosgen/src/main/java/gen/GenDtsFile.java @@ -15,6 +15,14 @@ package gen; +import grammar.*; +import utils.FileUtils; +import utils.StringUtils; + +import java.io.File; +import java.util.List; +import java.util.Map; + /** *

类名:该类用于xxx

* description ${description} @@ -24,5 +32,528 @@ package gen; * @since 2025-02-28 * @version 1.0 */ -public class GenDtsFile { +public class GenDtsFile extends GeneratorBase { + private final String TS_ENUM_TOKEN = "enum"; + private final String TS_CLASS_TOKEN = "class"; + private final String TS_EXPORT_TOKEN = "export"; + private final String TS_IMPLEMENTS_TOKEN = "implements"; + private final String TS_EXTENDS_TOKEN = "extends"; + private final String TS_CONST_TOKEN = "const"; + private final String TS_PRIVATE_TOKEN = "private"; + private final String TS_PUBLIC_TOKEN = "public"; + private final String TS_INTERFACE_TOKEN = "interface"; + private final String TS_PROTECTED_TOKEN = "protected"; + private final String TS_STATIC_TOKEN = "static"; + private final String TS_ANY_TOKEN = "any"; + private final String TS_NUMBER_TOKEN = "number"; + private final String TS_NEVER_TOKEN = "never"; + private final String TS_BOOLEAN_TOKEN = "boolean"; + private final String TS_STRING_TOKEN = "string"; + private final String TS_UNIQUE_TOKEN = "unique"; + private final String TS_SYMBOL_TOKEN = "symbol"; + private final String TS_UNDEFINED_TOKEN = "undefined"; + private final String TS_OBJECT_TOKEN = "object"; + private final String TS_OF_TOKEN = "of"; + private final String TS_KEYOF_TOKEN = "keyof"; + private final String TS_TYPE_TOKEN = "type"; + private final String TS_CONSTRUCTOR_TOKEN = "constructor"; + private final String TS_NAMESPACE_TOKEN = "namespace"; + private final String TS_REQUIRE_TOKEN = "require"; + private final String TS_MODULE_TOKEN = "module"; + private final String TS_DECLARE_TOKEN = "declare"; + private final String TS_ABSTRACT_TOKEN = "abstract"; + private final String TS_DEBUGGER_TOKEN = "debugger"; + private final String TS_FUNCTION_TOKEN = "function"; + private final String TS_THIS_TOKEN = "this"; + private final String TS_WITH_TOKEN = "with"; + private final String TS_DEFAULT_TOKEN = "default"; + private final String TS_READONLY_TOKEN = "readonly"; + private final String TS_ASYNC_TOKEN = "async"; + private final String TS_AWAIT_TOKEN = "await"; + private final String TS_YIELD_TOKEN = "yield"; + private final String TS_NEW_LINE = "\n"; + private final String TS_TAB_SPACE = "\t"; + private final String TS_BLANK_SPACE = " "; + private final String TS_SPLIT = " | "; + private final String TS_EQUAL = " = "; + private final String TS_COMMA = ","; + private final String TS_SEMICOLON = ";"; + private final String TS_COLON = ":"; + private final String TS_LEFT_BRACE = "{"; + private final String TS_RIGHT_BRACE = "}"; + private final String TS_LEFT_PARENTHESES = "("; + private final String TS_RIGHT_PARENTHESES = ")"; + private final String TS_LEFT_SQUARE_BRACKET = "["; + private final String TS_RIGHT_SQUARE_BRACKET = "]"; + private final String TS_LEFT_ANGLE_BRACKET = "<"; + private final String TS_RIGHT_ANGLE_BRACKET = ">"; + + private final String TS_FILE_PREFIX = "ag_"; + private final String TS_FILE_SUFFIX = ".d.ts"; + + private String interfaceContent = ""; + private String enumContent = ""; + private String classContent = ""; + private String funcContent = ""; + private String structContent = ""; + private String typeContent = ""; + private String unionContent = ""; + private String constContent = ""; + + private final Map cpp2tsMap = Map.ofEntries( + Map.entry("auto", "any"), + Map.entry("bool", "boolean"), + Map.entry("char", "string"), + Map.entry("char16_t", "string"), + Map.entry("char32_t", "string"), + Map.entry("double", "number"), + Map.entry("float", "number"), + Map.entry("int", "number"), + Map.entry("long", "number"), + Map.entry("wchar_t", "number"), + Map.entry("size_t", "number"), + Map.entry("void", "void"), + Map.entry("volatile", ""), + Map.entry("extern", ""), + Map.entry("dynamic_cast", ""), + Map.entry("static_cast", ""), + Map.entry("const_cast", ""), + Map.entry("constexpr", ""), + Map.entry("final", ""), + Map.entry("nullptr", ""), + Map.entry("inline", ""), + Map.entry("override", ""), + Map.entry("register", ""), + Map.entry("reinterpret_cast", ""), + Map.entry("thread_local", ""), + Map.entry("asm", ""), + Map.entry("export", "") + ); + + private final Map cppTokenMap = Map.ofEntries( + Map.entry("*", ""), + Map.entry("&", ""), + Map.entry("(", ""), + Map.entry(")", "") + ); + + /** + * 将 cpp key 转换成 ts key + * + * @param cppKey 枚举对象列表 + * @return ts key + */ + private String cpp2TsKey(String cppKey) { + String retKey = cppKey; + for (Map.Entry entry : cpp2tsMap.entrySet()) { + String key = entry.getKey(); + String value = entry.getValue(); + int ret = cppKey.indexOf(key); + if (ret >= 0) { + return value; + } + } + return retKey; + } + + /** + * 替换cpp token + * + * @param cppKey cpp token + * @return 替换后字符串 + */ + private String replaceCppToken(String cppKey) { + String retKey = cppKey; + for (Map.Entry entry : cppTokenMap.entrySet()) { + String key = entry.getKey(); + String value = entry.getValue(); + int ret = retKey.indexOf(key); + if (ret >= 0) { + retKey = retKey.replace(key, value); + } + } + return retKey; + } + + /** + * 构造函数 + */ + GenDtsFile() { + + } + + /** + * 获得接口内容 + * + * @return 接口内容 + */ + public String getInterfaceContent() { + return interfaceContent; + } + + /** + * 获得枚举内容 + * + * @return 枚举内容 + */ + public String getEnumContent() { + return enumContent; + } + + /** + * 获得类内容 + * + * @return 类内容 + */ + public String getClassContent() { + return classContent; + } + + /** + * 获得方法内容 + * + * @return 方法内容 + */ + public String getFuncContent() { + return funcContent; + } + + /** + * 获得结构体内容 + * + * @return 结构体内容 + */ + public String getStructContent() { + return structContent; + } + + /** + * 获得type内容 + * + * @return type内容 + */ + public String getTypeContent() { + return typeContent; + } + + /** + * 获得联合体内容 + * + * @return 联合体内容 + */ + public String getUnionContent() { + return unionContent; + } + + /** + * 获得常量内容 + * + * @return 常量内容 + */ + public String getConstContent() { + return constContent; + } + + /** + * 生成输出内容 + * @param po 解析对象 + */ + @Override + public void genContent(ParseObj po) { + genInterfaceList(po.getInterfaceList()); + genEnumList(po.getEnumList()); + genClassList(po.getClassList()); + genFuncList(po.getFuncList()); + genStructList(po.getStructList()); + genTypeList(po.getTypeList()); + genUnionList(po.getUnionList()); + genVarList(po.getVarList()); + } + + /** + * 生成文件 + */ + @Override + public void genFile(String filePath, String fileName) { + System.out.println("genFile : " + filePath + fileName); + String outFileName = filePath + File.separator + TS_FILE_PREFIX + + fileName.replace(".", "_") + TS_FILE_SUFFIX; + System.out.println("outFileName : " + outFileName); + + FileUtils.createFile(outFileName); + FileUtils.appendText(outFileName, this.constContent); + FileUtils.appendText(outFileName, this.enumContent); + FileUtils.appendText(outFileName, this.typeContent); + FileUtils.appendText(outFileName, this.interfaceContent); + FileUtils.appendText(outFileName, this.unionContent); + FileUtils.appendText(outFileName, this.funcContent); + FileUtils.appendText(outFileName, this.structContent); + FileUtils.appendText(outFileName, this.classContent); + + } + + /** + * 生成输出内容 + * @param iol 接口对象列表 + */ + @Override + public void genInterfaceList(List iol) { + System.out.println("genInterfaceList" + iol.toString()); + }; + + /** + * 生成输出内容 + * @param eol 枚举对象列表 + */ + @Override + public void genEnumList(List eol) { + System.out.println("genEnumList" + eol.toString()); + + String resContent = ""; + for (EnumObj eo : eol) { +// System.out.println("Enum jsonStr: " + eo.toJsonString()); + String enumName = eo.getName(); + enumName = !enumName.isEmpty() ? enumName : eo.getAlias(); + List memList = eo.getMemberList(); + List vaList = eo.getValueList(); + int i = 0; + resContent += TS_NEW_LINE + TS_EXPORT_TOKEN + TS_BLANK_SPACE + TS_ENUM_TOKEN + + TS_BLANK_SPACE + enumName + TS_BLANK_SPACE + TS_LEFT_BRACE; + for (String memItem : memList) { + resContent += TS_NEW_LINE + TS_TAB_SPACE + memItem; + if (vaList.size() > i && !vaList.get(i).isEmpty()) { + resContent += TS_EQUAL + vaList.get(i) + TS_COMMA; + } else { + resContent += TS_COMMA; + } + i++; + } + resContent = StringUtils.removeLastSpace(resContent); + resContent += TS_NEW_LINE + TS_RIGHT_BRACE + TS_SEMICOLON + TS_NEW_LINE; + } + this.enumContent = resContent; +// System.out.println("genEnumList : " + resContent); + }; + + /** + * 生成输出内容 + * @param col 类对象列表 + */ + @Override + public void genClassList(List col) { + System.out.println("genClassList" + col.toString()); + + String resContent = ""; + for (ClassObj co : col) { +// System.out.println("Class jsonStr: " + co.toJsonString()); + String className = co.getName(); + className = !className.isEmpty() ? className : co.getAlias(); + List funcList = co.getFuncList(); + List paList = co.getParamList(); + int i = 0; + resContent += TS_NEW_LINE + TS_EXPORT_TOKEN + TS_BLANK_SPACE + TS_CLASS_TOKEN + + TS_BLANK_SPACE + className + TS_BLANK_SPACE + TS_LEFT_BRACE; + + for (ParamObj paItem : paList) { + String paType = paItem.getType(); + resContent += TS_NEW_LINE + TS_TAB_SPACE + replaceCppToken(paItem.getName()) + + TS_COLON + TS_BLANK_SPACE + cpp2TsKey(paType); + List initVList = paItem.getvList(); + int vaSize = initVList.size(); + if (vaSize > 0) { + resContent += TS_EQUAL + initVList.get(0) + TS_SEMICOLON; + } else { + resContent += TS_SEMICOLON; + } + i++; + } + + i = 0; + for (FuncObj funcItem : funcList) { + resContent += TS_NEW_LINE + TS_TAB_SPACE + replaceCppToken(funcItem.getName()) + TS_LEFT_PARENTHESES; + List pol = funcItem.getParamList(); + for (ParamObj poItem : pol) { + String retType = cpp2TsKey(poItem.getType()); + resContent += replaceCppToken(poItem.getName()) + TS_COLON + + TS_BLANK_SPACE + retType + TS_COMMA + TS_BLANK_SPACE; + } + if (pol.size() > 0) { + resContent = StringUtils.removeLastCharacter(resContent, 2); + } + + String retValue = funcItem.getRetValue(); + resContent += TS_RIGHT_PARENTHESES + TS_BLANK_SPACE + TS_COLON + + TS_BLANK_SPACE + cpp2TsKey(retValue) + TS_SEMICOLON; + + i++; + } + + resContent = StringUtils.removeLastSpace(resContent); + resContent += TS_NEW_LINE + TS_RIGHT_BRACE + TS_SEMICOLON + TS_NEW_LINE; + } + this.classContent = resContent; +// System.out.println("genClassList : " + resContent); + }; + + /** + * 生成输出内容 + * @param fol 方法对象列表 + */ + @Override + public void genFuncList(List fol) { + System.out.println("genFuncList : " + fol.toString()); + String resContent = ""; + for (FuncObj fo : fol) { +// System.out.println("Func jsonStr: " + fo.toJsonString()); + String funcName = fo.getName(); + funcName = !funcName.isEmpty() ? funcName : fo.getAlias(); + List paList = fo.getParamList(); + int i = 0; + resContent += TS_NEW_LINE + TS_EXPORT_TOKEN + TS_BLANK_SPACE + TS_FUNCTION_TOKEN + + TS_BLANK_SPACE + replaceCppToken(funcName) + TS_LEFT_PARENTHESES; + + for (ParamObj poItem : paList) { + String paType = cpp2TsKey(poItem.getType()); + String paName = poItem.getName(); + + resContent += !paName.isEmpty() ? replaceCppToken(paName) + TS_COLON + + TS_BLANK_SPACE + paType + TS_COMMA + TS_BLANK_SPACE : + paType + TS_COMMA + TS_BLANK_SPACE; + } + if (paList.size() > 0) { + resContent = StringUtils.removeLastCharacter(resContent, 2); + } + + String retValue = fo.getRetValue(); + resContent += TS_RIGHT_PARENTHESES + TS_BLANK_SPACE + TS_COLON + + TS_BLANK_SPACE + cpp2TsKey(retValue) + TS_SEMICOLON; + } + this.funcContent = resContent; + System.out.println("genFuncList : " + resContent); + }; + + /** + * 生成输出内容 + * @param sol 结构体对象列表 + */ + @Override + public void genStructList(List sol) { + System.out.println("genStructList" + sol.toString()); + + String resContent = ""; + for (StructObj so : sol) { +// System.out.println("Struct jsonStr: " + so.toJsonString()); + String structName = so.getName(); + structName = !structName.isEmpty() ? structName : so.getAlias(); + List funcList = so.getFuncList(); + List paList = so.getMemberList(); + int i = 0; + resContent += TS_NEW_LINE + TS_EXPORT_TOKEN + TS_BLANK_SPACE + TS_CLASS_TOKEN + + TS_BLANK_SPACE + structName + TS_BLANK_SPACE + TS_LEFT_BRACE; + + for (ParamObj paItem : paList) { + String paType = paItem.getType(); + resContent += TS_NEW_LINE + TS_TAB_SPACE + paItem.getName() + + TS_COLON + TS_BLANK_SPACE + cpp2TsKey(paType); + List initVList = paItem.getvList(); + int vaSize = initVList.size(); + if (vaSize > 0) { + resContent += TS_EQUAL + initVList.get(0) + TS_SEMICOLON; + } else { + resContent += TS_SEMICOLON; + } + i++; + } + + i = 0; + for (FuncObj funcItem : funcList) { + resContent += TS_NEW_LINE + TS_TAB_SPACE + replaceCppToken(funcItem.getName()) + TS_LEFT_PARENTHESES; + List pol = funcItem.getParamList(); + for (ParamObj poItem : pol) { + String retType = cpp2TsKey(poItem.getType()); + resContent += replaceCppToken(poItem.getName()) + TS_COLON + + TS_BLANK_SPACE + retType + TS_COMMA + TS_BLANK_SPACE; + } + if (!pol.isEmpty()) { + resContent = StringUtils.removeLastCharacter(resContent, 2); + } + + String retValue = funcItem.getRetValue(); + resContent += TS_RIGHT_PARENTHESES + TS_BLANK_SPACE + TS_COLON + + TS_BLANK_SPACE + cpp2TsKey(retValue) + TS_SEMICOLON; + + i++; + } + + resContent = StringUtils.removeLastSpace(resContent); + resContent += TS_NEW_LINE + TS_RIGHT_BRACE + TS_SEMICOLON + TS_NEW_LINE; + } + this.structContent = resContent; +// System.out.println("genStructList : " + resContent); + }; + + /** + * 生成输出内容 + * @param tol 类型对象列表 + */ + @Override + public void genTypeList(List tol) { + System.out.println("genTypeList : " + tol.toString()); + }; + + /** + * 生成输出内容 + * @param uol 联合体对象列表 + */ + @Override + public void genUnionList(List uol) { + System.out.println("genUnionList : " + uol.toString()); + + String resContent = ""; + for (UnionObj uo : uol) { +// System.out.println("Union jsonStr: " + uo.toJsonString()); + String unionName = uo.getName(); + unionName = !unionName.isEmpty() ? unionName : uo.getAlias(); + List paList = uo.getMemList(); + int i = 0; + resContent += TS_NEW_LINE + TS_EXPORT_TOKEN + TS_BLANK_SPACE + TS_TYPE_TOKEN + + TS_BLANK_SPACE + unionName + TS_EQUAL; + + for (ParamObj paItem : paList) { + String paType = paItem.getType(); + resContent += cpp2TsKey(paType) + TS_SPLIT; + + i++; + } + + resContent = StringUtils.removeLastCharacter(resContent, 3); + resContent += TS_SEMICOLON + TS_NEW_LINE; + } + this.unionContent = resContent; +// System.out.println("genUnionList : " + resContent); + }; + + /** + * 生成输出内容 + * @param pol 常量列表 + */ + @Override + public void genVarList(List pol) { + System.out.println("genVarList : " + pol.toString()); + + String resContent = ""; + for (ParamObj po : pol) { +// System.out.println("Var jsonStr: " + uo.toJsonString()); + String paName = po.getName(); + String paType = cpp2TsKey(po.getType()); + String paValue = po.getStrValue(0); + int i = 0; + resContent += TS_NEW_LINE + TS_EXPORT_TOKEN + TS_BLANK_SPACE + TS_CONST_TOKEN + + TS_BLANK_SPACE + paName + TS_COLON + paType + TS_EQUAL + paValue; + + resContent += TS_SEMICOLON + TS_NEW_LINE; + } + this.constContent = resContent; + System.out.println("genVarList : " + resContent); + } } -- Gitee From 5f772e0464dcbb9ee6b8671c9af1a5cd79b6c49e Mon Sep 17 00:00:00 2001 From: sunlian Date: Wed, 2 Apr 2025 14:16:31 +0800 Subject: [PATCH 05/10] add dts gen & test Signed-off-by: sunlian --- .../src/main/java/grammar/StructObj.java | 13 +- .../src/main/java/grammar/UnionObj.java | 28 ++ .../src/main/java/parse/ParseBase.java | 14 + .../ohosgen/src/main/java/parse/ParseCpp.java | 14 +- .../src/main/java/parse/ParseFactory.java | 13 +- .../src/main/java/parse/ParseTask.java | 20 + .../ohosgen/src/main/java/parse/ParseTs.java | 9 + .../src/main/java/utils/FileUtils.java | 2 +- .../src/test/java/parse/ParseCppTest.java | 342 ++++++++++++++++++ 9 files changed, 441 insertions(+), 14 deletions(-) diff --git a/src/intellij_plugin/ohosgen/src/main/java/grammar/StructObj.java b/src/intellij_plugin/ohosgen/src/main/java/grammar/StructObj.java index b685e1ee..5f8dd087 100644 --- a/src/intellij_plugin/ohosgen/src/main/java/grammar/StructObj.java +++ b/src/intellij_plugin/ohosgen/src/main/java/grammar/StructObj.java @@ -129,15 +129,6 @@ public class StructObj extends GBaseObject { this.memberList = memberList; } - /** - * 增加方法 - * - * @param fo 方法 - */ - public void addFunction(FuncObj fo) { - this.funcList.add(fo); - } - /** * 增加属性 * @@ -147,4 +138,8 @@ public class StructObj extends GBaseObject { this.memberList.add(po); } + public void addFunc(FuncObj fo) { + this.funcList.add(fo); + } + } diff --git a/src/intellij_plugin/ohosgen/src/main/java/grammar/UnionObj.java b/src/intellij_plugin/ohosgen/src/main/java/grammar/UnionObj.java index 428ca9e5..36de6c94 100644 --- a/src/intellij_plugin/ohosgen/src/main/java/grammar/UnionObj.java +++ b/src/intellij_plugin/ohosgen/src/main/java/grammar/UnionObj.java @@ -33,6 +33,7 @@ public class UnionObj extends GBaseObject { private String name; private String alias; private List memList; + private List funcList; /** * 构造函数 @@ -108,6 +109,24 @@ public class UnionObj extends GBaseObject { this.memList = memList; } + /** + * 获取方法 + * + * @return 方法列表 + */ + public List getFuncList() { + return funcList; + } + + /** + * 设置方法 + * + * @param funcList 方法列表 + */ + public void setFuncList(List funcList) { + this.funcList = funcList; + } + /** * 增加属性 * @@ -116,4 +135,13 @@ public class UnionObj extends GBaseObject { public void addMember(ParamObj po) { this.memList.add(po); } + + /** + * 增加方法 + * + * @param fo 方法 + */ + public void addFunc(FuncObj fo) { + this.funcList.add(fo); + } } diff --git a/src/intellij_plugin/ohosgen/src/main/java/parse/ParseBase.java b/src/intellij_plugin/ohosgen/src/main/java/parse/ParseBase.java index 4ef224b9..75d8c527 100644 --- a/src/intellij_plugin/ohosgen/src/main/java/parse/ParseBase.java +++ b/src/intellij_plugin/ohosgen/src/main/java/parse/ParseBase.java @@ -38,6 +38,11 @@ import java.util.concurrent.CopyOnWriteArrayList; */ public abstract class ParseBase { + /** + * 生成类型 + */ + protected String genType; + /** * 文件内容 */ @@ -87,6 +92,15 @@ public abstract class ParseBase { listeners.add(listener); } + /** + * 返回生成类型 + * + * @return 生成类型 + */ + public String getGenType() { + return genType; + } + /** * send event * diff --git a/src/intellij_plugin/ohosgen/src/main/java/parse/ParseCpp.java b/src/intellij_plugin/ohosgen/src/main/java/parse/ParseCpp.java index f5fde90e..023fe237 100644 --- a/src/intellij_plugin/ohosgen/src/main/java/parse/ParseCpp.java +++ b/src/intellij_plugin/ohosgen/src/main/java/parse/ParseCpp.java @@ -22,6 +22,8 @@ import antlr.cpp.CPP14Lexer; import antlr.cpp.CPP14Parser; import com.fasterxml.jackson.core.JsonProcessingException; import com.fasterxml.jackson.databind.ObjectMapper; +import gen.GenerateFactory; +import gen.GeneratorBase; import grammar.*; import org.antlr.v4.runtime.CharStream; import org.antlr.v4.runtime.CommonTokenStream; @@ -41,6 +43,15 @@ import utils.BaseEvent; * @version 1.0 */ public class ParseCpp extends ParseBase { + /** + * 构造函数 + * + * @param genType 生成类型 + */ + ParseCpp(String genType) { + this.genType = genType; + } + /** * 根据名字解析文件 * @@ -116,8 +127,8 @@ public class ParseCpp extends ParseBase { System.out.println("cpp parse result: " + json); ParseObj po = genParseResult(tsc); - System.out.println("cpp parse char stream finish"); + return po; } catch (RecognitionException e) { System.out.println("parse cstream e.printStackTrace(): " + e.getMessage()); @@ -148,6 +159,7 @@ public class ParseCpp extends ParseBase { po.setStructList(tcl.getStructObjList()); po.setTypeList(tcl.getTypeObjList()); po.setUnionList(tcl.getUnionObjList()); + po.setVarList(tcl.getConstList()); return po; } diff --git a/src/intellij_plugin/ohosgen/src/main/java/parse/ParseFactory.java b/src/intellij_plugin/ohosgen/src/main/java/parse/ParseFactory.java index 016bd3f9..9ebeef7f 100644 --- a/src/intellij_plugin/ohosgen/src/main/java/parse/ParseFactory.java +++ b/src/intellij_plugin/ohosgen/src/main/java/parse/ParseFactory.java @@ -35,9 +35,16 @@ public class ParseFactory { * @throws IllegalArgumentException 非法参数异常 */ public static ParseBase getParser(String type) { - return switch (type.toUpperCase(Locale.ROOT)) { - case "CPP" -> new ParseCpp(); - case "TS" -> new ParseTs(); + String[] sList = type.split("2"); + if (sList.length <= 0) { + System.out.println("type split error: " + type); + return null; + } + String paseType = sList[0]; + String genType = sList[1]; + return switch (paseType.toUpperCase(Locale.ROOT)) { + case "H" -> new ParseCpp(genType); + case "TS" -> new ParseTs(genType); default -> { System.out.println("Unsupported parser type: " + type); throw new IllegalArgumentException("Unsupported parser type: " + type); diff --git a/src/intellij_plugin/ohosgen/src/main/java/parse/ParseTask.java b/src/intellij_plugin/ohosgen/src/main/java/parse/ParseTask.java index 87bc1ae8..d440ec23 100644 --- a/src/intellij_plugin/ohosgen/src/main/java/parse/ParseTask.java +++ b/src/intellij_plugin/ohosgen/src/main/java/parse/ParseTask.java @@ -17,6 +17,8 @@ package parse; import com.fasterxml.jackson.core.JsonProcessingException; import com.fasterxml.jackson.databind.ObjectMapper; +import gen.GenerateFactory; +import gen.GeneratorBase; import grammar.ParseObj; import org.antlr.v4.runtime.CharStream; import org.antlr.v4.runtime.CharStreams; @@ -68,6 +70,19 @@ public class ParseTask extends Task.Backgroundable implements BaseListener { parseFile = file; } + /** + * 返回解析文件名称 + * + * @return 解析文件名称 + */ + public String getFileName() { + return this.parseFile.getName(); + }; + + public String getFilePath() { + return this.parseFile.getParent().getPath(); + } + /** * 运行 * @@ -94,6 +109,11 @@ public class ParseTask extends Task.Backgroundable implements BaseListener { ParseObj po = parser.parseCStream(ics); System.out.println("parseContent finish"); + + GeneratorBase gb = GenerateFactory.getGenerator(parser.genType); + gb.genContent(po); + gb.genFile(getFilePath(), getFileName()); + System.out.println("genContent finish"); String[] lines = content.split("\n"); for (int i = 0; i < lines.length; i++) { // 模拟处理每一行 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 eac7c2ad..6110b474 100644 --- a/src/intellij_plugin/ohosgen/src/main/java/parse/ParseTs.java +++ b/src/intellij_plugin/ohosgen/src/main/java/parse/ParseTs.java @@ -43,6 +43,15 @@ import utils.Constants; * @version 1.0 */ public class ParseTs extends ParseBase implements CustomEventListener { + /** + * 构造函数 + * + * @param genType 生成类型 + */ + ParseTs(String genType) { + this.genType = genType; + } + /** * 根据文件名解析 * diff --git a/src/intellij_plugin/ohosgen/src/main/java/utils/FileUtils.java b/src/intellij_plugin/ohosgen/src/main/java/utils/FileUtils.java index 2afc8286..ae7fc601 100644 --- a/src/intellij_plugin/ohosgen/src/main/java/utils/FileUtils.java +++ b/src/intellij_plugin/ohosgen/src/main/java/utils/FileUtils.java @@ -120,7 +120,7 @@ public class FileUtils { try (BufferedWriter writer = new BufferedWriter(new FileWriter(filePath, true))) { // true 表示追加模式‌:ml-citation{ref="6" data="citationList"} writer.write(content); - writer.newLine(); // 换行追加 +// writer.newLine(); // 换行追加 } catch (IOException e) { System.out.println("appendText error: " + e.getMessage()); } diff --git a/src/intellij_plugin/ohosgen/src/test/java/parse/ParseCppTest.java b/src/intellij_plugin/ohosgen/src/test/java/parse/ParseCppTest.java index 1f198f2b..73722dcb 100644 --- a/src/intellij_plugin/ohosgen/src/test/java/parse/ParseCppTest.java +++ b/src/intellij_plugin/ohosgen/src/test/java/parse/ParseCppTest.java @@ -967,6 +967,348 @@ class ParseCppTest { assertEquals("long long int", pl.get(0).getType()); } + @Test + void parseCStreamFile1() { + ParseBase parser = ParseFactory.getParser("h2dts"); + String testEnum = "#ifndef COAP_ADDRESS_H_\n" + + "#define COAP_ADDRESS_H_\n" + + "\n" + + "#include \n" + + "#include \n" + + "#include \n" + + "#include \n" + + "#include \"libcoap.h\"\n" + + "\n" + + "#include \"coap3/coap_pdu.h\"\n" + + "\n" + + "#if defined(WITH_LWIP)\n" + + "\n" + + "#include \n" + + "\n" + + "struct coap_address_t {\n" + + " uint16_t port;\n" + + " ip_addr_t addr;\n" + + "};\n" + + "\n" + + "/**\n" + + " * Returns the port from @p addr in host byte order.\n" + + " */\n" + + "COAP_STATIC_INLINE uint16_t\n" + + "coap_address_get_port(const coap_address_t *addr) {\n" + + " return addr->port;\n" + + "}\n" + + "\n" + + "/**\n" + + " * Sets the port field of @p addr to @p port (in host byte order).\n" + + " */\n" + + "COAP_STATIC_INLINE void\n" + + "coap_address_set_port(coap_address_t *addr, uint16_t port) {\n" + + " addr->port = port;\n" + + "}\n" + + "\n" + + "#define _coap_address_equals_impl(A, B) \\\n" + + " ((A)->port == (B)->port && \\\n" + + " (!!ip_addr_cmp(&(A)->addr,&(B)->addr)))\n" + + "\n" + + "#define _coap_address_isany_impl(A) ip_addr_isany(&(A)->addr)\n" + + "\n" + + "#define _coap_is_mcast_impl(Address) ip_addr_ismulticast(&(Address)->addr)\n" + + "\n" + + "#ifdef COAP_SUPPORT_SOCKET_BROADCAST\n" + + "#define _coap_is_bcast_impl(Address) ip_addr_isbroadcast(&(Address)->addr)\n" + + "#endif\n" + + "\n" + + "#elif defined(WITH_CONTIKI)\n" + + "\n" + + "#include \"uip.h\"\n" + + "\n" + + "struct coap_address_t {\n" + + " uip_ipaddr_t addr;\n" + + " uint16_t port;\n" + + "};"; + CodePointCharStream cStream = CharStreams.fromString(testEnum); + ParseObj po = parser.parseCStream(cStream); + List fol = po.getFuncList(); + assertEquals(2, fol.size()); + + FuncObj fo = fol.get(0); + assertEquals("coap_address_get_port", fo.getName()); + assertEquals("COAP_STATIC_INLINE uint16_t", fo.getRetValue()); + List pol = fo.getParamList(); + ParamObj poItem = pol.get(0); + assertEquals("constcoap_address_t", poItem.getType()); + assertEquals("*addr", poItem.getName()); + + fo = fol.get(1); + assertEquals("coap_address_set_port", fo.getName()); + assertEquals("COAP_STATIC_INLINE void", fo.getRetValue()); + pol = fo.getParamList(); + poItem = pol.get(0); + assertEquals("coap_address_t", poItem.getType()); + assertEquals("*addr", poItem.getName()); + poItem = pol.get(1); + assertEquals("uint16_t", poItem.getType()); + assertEquals("port", poItem.getName()); + + List sol = po.getStructList(); + assertEquals(2, sol.size()); + + StructObj soItem = sol.get(0); + assertEquals("coap_address_t", soItem.getName()); + pol = soItem.getMemberList(); + poItem = pol.get(0); + assertEquals("uint16_t", poItem.getType()); + assertEquals("port", poItem.getName()); + poItem = pol.get(1); + assertEquals("ip_addr_t", poItem.getType()); + assertEquals("addr", poItem.getName()); + + soItem = sol.get(1); + assertEquals("coap_address_t", soItem.getName()); + pol = soItem.getMemberList(); + poItem = pol.get(0); + assertEquals("uip_ipaddr_t", poItem.getType()); + assertEquals("addr", poItem.getName()); + poItem = pol.get(1); + assertEquals("uint16_t", poItem.getType()); + assertEquals("port", poItem.getName()); + + } + + @Test + void parseCStreamFile2() { + ParseBase parser = ParseFactory.getParser("h2dts"); + String testEnum = "/*-------------------------------------------------------------------------\n" + + " * dEQP glslang integration\n" + + " * ------------------------\n" + + " *\n" + + " * Copyright 2015 The Android Open Source Project\n" + + " *\n" + + " * Licensed under the Apache License, Version 2.0 (the \"License\");\n" + + " * you may not use this file except in compliance with the License.\n" + + " * You may obtain a copy of the License at\n" + + " *\n" + + " * http://www.apache.org/licenses/LICENSE-2.0\n" + + " *\n" + + " * Unless required by applicable law or agreed to in writing, software\n" + + " * distributed under the License is distributed on an \"AS IS\" BASIS,\n" + + " * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n" + + " * See the License for the specific language governing permissions and\n" + + " * limitations under the License.\n" + + " *\n" + + " *//*!\n" + + " * \\file\n" + + " * \\brief glslang OS interface.\n" + + " *//*--------------------------------------------------------------------*/\n" + + "\n" + + "#include \"osinclude.h\"\n" + + "\n" + + "#include \"deThread.h\"\n" + + "#include \"deThreadLocal.h\"\n" + + "#include \"deMutex.h\"\n" + + "\n" + + "namespace glslang\n" + + "{\n" + + "\n" + + "DE_STATIC_ASSERT(sizeof(deThreadLocal)\t== sizeof(OS_TLSIndex));\n" + + "DE_STATIC_ASSERT(sizeof(deThread)\t\t== sizeof(void*));\n" + + "\n" + + "// Thread-local\n" + + "\n" + + "OS_TLSIndex OS_AllocTLSIndex (void)\n" + + "{\n" + + "\treturn (OS_TLSIndex)deThreadLocal_create();\n" + + "}\n" + + "\n" + + "bool OS_SetTLSValue (OS_TLSIndex nIndex, void* lpvValue)\n" + + "{\n" + + "\tdeThreadLocal_set((deThreadLocal)nIndex, lpvValue);\n" + + "\treturn true;\n" + + "}\n" + + "\n" + + "bool OS_FreeTLSIndex (OS_TLSIndex nIndex)\n" + + "{\n" + + "\tdeThreadLocal_destroy((deThreadLocal)nIndex);\n" + + "\treturn true;\n" + + "}\n" + + "\n" + + "void* OS_GetTLSValue (OS_TLSIndex nIndex)\n" + + "{\n" + + "\treturn deThreadLocal_get((deThreadLocal)nIndex);\n" + + "}\n" + + "\n" + + "// Global lock\n" + + "\n" + + "static deMutex s_globalLock = 0;\n" + + "\n" + + "void InitGlobalLock (void)\n" + + "{\n" + + "\tDE_ASSERT(s_globalLock == 0);\n" + + "\ts_globalLock = deMutex_create(DE_NULL);\n" + + "}\n" + + "\n" + + "void GetGlobalLock (void)\n" + + "{\n" + + "\tdeMutex_lock(s_globalLock);\n" + + "}\n" + + "\n" + + "void ReleaseGlobalLock (void)\n" + + "{\n" + + "\tdeMutex_unlock(s_globalLock);\n" + + "}\n" + + "\n" + + "// Threading\n" + + "\n" + + "DE_STATIC_ASSERT(sizeof(void*) >= sizeof(deThread));\n" + + "\n" + + "static void EnterGenericThread (void* entry)\n" + + "{\n" + + "\t((TThreadEntrypoint)entry)(DE_NULL);\n" + + "}\n" + + "\n" + + "void* OS_CreateThread (TThreadEntrypoint entry)\n" + + "{\n" + + "\treturn (void*)(deUintptr)deThread_create(EnterGenericThread, (void*)entry, DE_NULL);\n" + + "}\n" + + "\n" + + "void OS_WaitForAllThreads (void* threads, int numThreads)\n" + + "{\n" + + "\tfor (int ndx = 0; ndx < numThreads; ndx++)\n" + + "\t{\n" + + "\t\tconst deThread thread = (deThread)(deUintptr)((void**)threads)[ndx];\n" + + "\t\tdeThread_join(thread);\n" + + "\t\tdeThread_destroy(thread);\n" + + "\t}\n" + + "}\n" + + "\n" + + "void OS_Sleep (int milliseconds)\n" + + "{\n" + + "\tdeSleep(milliseconds);\n" + + "}\n" + + "\n" + + "void OS_DumpMemoryCounters (void)\n" + + "{\n" + + "\t// Not used\n" + + "}\n" + + "\n" + + "} // glslang\n"; + CodePointCharStream cStream = CharStreams.fromString(testEnum); + ParseObj po = parser.parseCStream(cStream); + List fol = po.getFuncList(); + assertEquals(12, fol.size()); + FuncObj fo = fol.get(0); + assertEquals("OS_AllocTLSIndex", fo.getName()); + assertEquals("OS_TLSIndex", fo.getRetValue()); + + List pl = fo.getParamList(); + assertEquals(1, pl.size()); + assertEquals("", pl.get(0).getName()); + assertEquals("void", pl.get(0).getType()); + + fo = fol.get(1); + assertEquals("OS_SetTLSValue", fo.getName()); + assertEquals("bool", fo.getRetValue()); + + pl = fo.getParamList(); + assertEquals(2, pl.size()); + assertEquals("nIndex", pl.get(0).getName()); + assertEquals("OS_TLSIndex", pl.get(0).getType()); + assertEquals("*lpvValue", pl.get(1).getName()); + assertEquals("void", pl.get(1).getType()); + + fo = fol.get(2); + assertEquals("OS_FreeTLSIndex", fo.getName()); + assertEquals("bool", fo.getRetValue()); + + pl = fo.getParamList(); + assertEquals(1, pl.size()); + assertEquals("nIndex", pl.get(0).getName()); + assertEquals("OS_TLSIndex", pl.get(0).getType()); + + fo = fol.get(3); + assertEquals("OS_GetTLSValue", fo.getName()); + assertEquals("void", fo.getRetValue()); + + pl = fo.getParamList(); + assertEquals(1, pl.size()); + assertEquals("nIndex", pl.get(0).getName()); + assertEquals("OS_TLSIndex", pl.get(0).getType()); + + fo = fol.get(4); + assertEquals("InitGlobalLock", fo.getName()); + assertEquals("void", fo.getRetValue()); + + pl = fo.getParamList(); + assertEquals(1, pl.size()); + assertEquals("", pl.get(0).getName()); + assertEquals("void", pl.get(0).getType()); + + fo = fol.get(5); + assertEquals("GetGlobalLock", fo.getName()); + assertEquals("void", fo.getRetValue()); + + pl = fo.getParamList(); + assertEquals(1, pl.size()); + assertEquals("", pl.get(0).getName()); + assertEquals("void", pl.get(0).getType()); + + fo = fol.get(6); + assertEquals("ReleaseGlobalLock", fo.getName()); + assertEquals("void", fo.getRetValue()); + + pl = fo.getParamList(); + assertEquals(1, pl.size()); + assertEquals("", pl.get(0).getName()); + assertEquals("void", pl.get(0).getType()); + + fo = fol.get(7); + assertEquals("EnterGenericThread", fo.getName()); + assertEquals("static void", fo.getRetValue()); + + pl = fo.getParamList(); + assertEquals(1, pl.size()); + assertEquals("*entry", pl.get(0).getName()); + assertEquals("void", pl.get(0).getType()); + + fo = fol.get(8); + assertEquals("OS_CreateThread", fo.getName()); + assertEquals("void", fo.getRetValue()); + + pl = fo.getParamList(); + assertEquals(1, pl.size()); + assertEquals("entry", pl.get(0).getName()); + assertEquals("TThreadEntrypoint", pl.get(0).getType()); + + fo = fol.get(9); + assertEquals("OS_WaitForAllThreads", fo.getName()); + assertEquals("void", fo.getRetValue()); + + pl = fo.getParamList(); + assertEquals(2, pl.size()); + assertEquals("*threads", pl.get(0).getName()); + assertEquals("void", pl.get(0).getType()); + assertEquals("numThreads", pl.get(1).getName()); + assertEquals("int", pl.get(1).getType()); + + fo = fol.get(10); + assertEquals("OS_Sleep", fo.getName()); + assertEquals("void", fo.getRetValue()); + + pl = fo.getParamList(); + assertEquals(1, pl.size()); + assertEquals("milliseconds", pl.get(0).getName()); + assertEquals("int", pl.get(0).getType()); + + fo = fol.get(11); + assertEquals("OS_DumpMemoryCounters", fo.getName()); + assertEquals("void", fo.getRetValue()); + + pl = fo.getParamList(); + assertEquals(1, pl.size()); + assertEquals("", pl.get(0).getName()); + assertEquals("void", pl.get(0).getType()); + } + @Test void genParseResult() { } -- Gitee From c8fab7e0e7ffad6ddb8c3e78f618529b501b16d3 Mon Sep 17 00:00:00 2001 From: sunlian Date: Wed, 2 Apr 2025 14:17:30 +0800 Subject: [PATCH 06/10] add dts gen & test Signed-off-by: sunlian --- .../src/test/java/gen/GenDtsFileTest.java | 94 +++++++++++++++++++ 1 file changed, 94 insertions(+) create mode 100644 src/intellij_plugin/ohosgen/src/test/java/gen/GenDtsFileTest.java diff --git a/src/intellij_plugin/ohosgen/src/test/java/gen/GenDtsFileTest.java b/src/intellij_plugin/ohosgen/src/test/java/gen/GenDtsFileTest.java new file mode 100644 index 00000000..f11e442c --- /dev/null +++ b/src/intellij_plugin/ohosgen/src/test/java/gen/GenDtsFileTest.java @@ -0,0 +1,94 @@ +package gen; + +import grammar.EnumObj; +import grammar.ParseObj; +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +import java.util.List; +import java.util.concurrent.CopyOnWriteArrayList; + +import static org.junit.jupiter.api.Assertions.*; + +/** + *

类名:该类用于xxx

+ * description + * + * @author Administrator + * date 2025-02-28 + * @version 1.0 + * @since 2025-02-28 + */ +class GenDtsFileTest { + + @BeforeEach + void setUp() { + } + + @AfterEach + void tearDown() { + } + + @Test + void genContent() { + } + + @Test + void genFile() { + } + + @Test + void genInterfaceList() { + } + + @Test + void genEnumList() { + GeneratorBase gb = GenerateFactory.getGenerator("DTS"); + ParseObj po = new ParseObj(); + EnumObj eo = new EnumObj(); + eo.setName("TestEnum"); + List ml = new CopyOnWriteArrayList<>(); + ml.add("ONE"); + ml.add("TWO"); + eo.setMemberList(ml); + List eol = new CopyOnWriteArrayList<>(); + eol.add(eo); + po.setEnumList(eol); + gb.genEnumList(po.getEnumList()); + + if (gb instanceof GenDtsFile gdf) { + String enumContent = gdf.getEnumContent(); + System.out.println("genEnum: " + enumContent); + String expect = "\nexport enum TestEnum {\n" + + "\tONE,\n" + + "\tTWO,\n" + + "};\n"; + assertEquals(expect, enumContent); + } + } + + @Test + void genClassList() { + } + + @Test + void genFuncList() { + } + + @Test + void genStructList() { + } + + @Test + void genTypeList() { + } + + @Test + void genUnionList() { + } + + @Test + void genVarList() { + } +} \ No newline at end of file -- Gitee From 73a4851c624370bfdab40a8856198b1bc94e4bc8 Mon Sep 17 00:00:00 2001 From: sunlian Date: Wed, 2 Apr 2025 14:18:16 +0800 Subject: [PATCH 07/10] add dts gen & test Signed-off-by: sunlian --- .../ohosgen/src/main/java/gen/GenHdfFile.java | 31 ++++++++++ .../ohosgen/src/main/java/gen/GenSaFile.java | 31 ++++++++++ .../src/main/java/gen/GenerateFactory.java | 36 ++++++++++++ .../src/main/java/gen/GeneratorBase.java | 56 ++++++++++++++++++ .../src/main/java/utils/StringUtils.java | 58 +++++++++++++++++++ 5 files changed, 212 insertions(+) create mode 100644 src/intellij_plugin/ohosgen/src/main/java/gen/GenHdfFile.java create mode 100644 src/intellij_plugin/ohosgen/src/main/java/gen/GenSaFile.java create mode 100644 src/intellij_plugin/ohosgen/src/main/java/gen/GenerateFactory.java create mode 100644 src/intellij_plugin/ohosgen/src/main/java/gen/GeneratorBase.java create mode 100644 src/intellij_plugin/ohosgen/src/main/java/utils/StringUtils.java diff --git a/src/intellij_plugin/ohosgen/src/main/java/gen/GenHdfFile.java b/src/intellij_plugin/ohosgen/src/main/java/gen/GenHdfFile.java new file mode 100644 index 00000000..3f7223fe --- /dev/null +++ b/src/intellij_plugin/ohosgen/src/main/java/gen/GenHdfFile.java @@ -0,0 +1,31 @@ +/* + * Copyright (c) 2025 Shenzhen Kaihong Digital. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package gen; + +/** + *

类名:该类用于xxx

+ * description ${description} + * + * @author ${USER} + * date 2025-02-28 + * @since 2025-02-28 + * @version 1.0 + */ +public class GenHdfFile extends GeneratorBase { + GenHdfFile() { + + } +} diff --git a/src/intellij_plugin/ohosgen/src/main/java/gen/GenSaFile.java b/src/intellij_plugin/ohosgen/src/main/java/gen/GenSaFile.java new file mode 100644 index 00000000..cd90eb8e --- /dev/null +++ b/src/intellij_plugin/ohosgen/src/main/java/gen/GenSaFile.java @@ -0,0 +1,31 @@ +/* + * Copyright (c) 2025 Shenzhen Kaihong Digital. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package gen; + +/** + *

类名:该类用于xxx

+ * description ${description} + * + * @author ${USER} + * date 2025-02-28 + * @since 2025-02-28 + * @version 1.0 + */ +public class GenSaFile extends GeneratorBase { + GenSaFile() { + + } +} diff --git a/src/intellij_plugin/ohosgen/src/main/java/gen/GenerateFactory.java b/src/intellij_plugin/ohosgen/src/main/java/gen/GenerateFactory.java new file mode 100644 index 00000000..4fad781f --- /dev/null +++ b/src/intellij_plugin/ohosgen/src/main/java/gen/GenerateFactory.java @@ -0,0 +1,36 @@ +package gen; + +import parse.ParseBase; +import parse.ParseCpp; +import parse.ParseTs; + +import java.util.Locale; + +/** + *

类名:该类用于xxx

+ * description create generator + * + * @author Administrator + * date 2025-02-28 + * @version 1.0 + * @since 2025-02-28 + */ +public class GenerateFactory { + /** + * 获取解析类 + * + * @param type 解析类型 + * @return 解析类 (类型不存在时候抛出异常) + * @throws IllegalArgumentException 非法参数异常 + */ + public static GeneratorBase getGenerator(String type) { + return switch (type.toUpperCase(Locale.ROOT)) { + case "CPP" -> new GenCppFile(); + case "DTS" -> new GenDtsFile(); + default -> { + System.out.println("Unsupported parser type: " + type); + throw new IllegalArgumentException("Unsupported parser type: " + type); + } + }; + } +} diff --git a/src/intellij_plugin/ohosgen/src/main/java/gen/GeneratorBase.java b/src/intellij_plugin/ohosgen/src/main/java/gen/GeneratorBase.java new file mode 100644 index 00000000..be20ae00 --- /dev/null +++ b/src/intellij_plugin/ohosgen/src/main/java/gen/GeneratorBase.java @@ -0,0 +1,56 @@ +package gen; + +import grammar.*; + +import java.util.List; + +/** + *

类名:该类用于xxx

+ * description base of generator + * + * @author Administrator + * date 2025-02-28 + * @version 1.0 + * @since 2025-02-28 + */ +public class GeneratorBase { + public void genContent(ParseObj po) { + System.out.println("GeneratorBase: genContent"); + } + + public void genFile(String filePath, String fileName) { + System.out.println("GeneratorBase: path is " + filePath + ", file is " + fileName); + } + + public void genInterfaceList(List iol) { + System.out.println("GeneratorBase: genInterfaceList"); + }; + + public void genEnumList(List eol) { + System.out.println("GeneratorBase: genEnumList"); + + }; + + public void genClassList(List col) { + System.out.println("GeneratorBase: genClassList"); + }; + + public void genFuncList(List fol) { + System.out.println("GeneratorBase: genFuncList"); + }; + + public void genStructList(List sol) { + System.out.println("GeneratorBase: genStructList"); + }; + + public void genTypeList(List tol) { + System.out.println("GeneratorBase: genTypeList"); }; + + public void genUnionList(List uol) { + System.out.println("GeneratorBase: genUnionList"); + }; + + public void genVarList(List pol) { + System.out.println("GeneratorBase: genVarList"); + } +} diff --git a/src/intellij_plugin/ohosgen/src/main/java/utils/StringUtils.java b/src/intellij_plugin/ohosgen/src/main/java/utils/StringUtils.java new file mode 100644 index 00000000..ca17f51e --- /dev/null +++ b/src/intellij_plugin/ohosgen/src/main/java/utils/StringUtils.java @@ -0,0 +1,58 @@ +package utils; + +import java.util.List; + +/** + *

类名:该类用于xxx

+ * description string utils class + * + * @author Administrator + * date 2025-02-28 + * @version 1.0 + * @since 2025-02-28 + */ +public class StringUtils { + /** + * split string by char + * + * @param str string need to split + * @param delimiter the split char + */ + public static void splitByChar(String str, char delimiter) { + int index = str.indexOf(delimiter); + if (index != -1) { + String prefix = str.substring(0, index); + String suffix = str.substring(index + 1).toLowerCase(); + System.out.println("前缀: " + prefix); + System.out.println("后缀: " + suffix); + } else { + System.out.println("未找到分隔符 '" + delimiter + "'"); + } + } + + /** + * removeLastSpace + * + * @param str 删除字符串最后一个空格 + * @return 返回字符串 + */ + public static String removeLastSpace(String str) { + if (str != null && !str.isEmpty() && str.charAt(str.length() - 1) == ' ') { + return str.substring(0, str.length() - 1); + } + return str; + } + + /** + * removeLastSpace + * + * @param str 删除字符串最后一个空格 + * @return 返回字符串 + */ + public static String removeLastCharacter(String str, int n) { + if (str != null && !str.isEmpty() && str.length() > n) { + return str.substring(0, str.length() - n); + } + return str; + } +} -- Gitee From 1566f3ca763e5e32c17ccf7127913be959a18b79 Mon Sep 17 00:00:00 2001 From: sunlian Date: Wed, 2 Apr 2025 15:01:53 +0800 Subject: [PATCH 08/10] fix code check Signed-off-by: sunlian --- .../main/java/antlr/ParseBaseListener.java | 2 +- .../java/antlr/cpp/CPP14CustomListener.java | 431 ++++++++++-------- .../ohosgen/src/main/java/gen/GenDtsFile.java | 124 +++-- .../src/main/java/gen/GenerateFactory.java | 19 +- .../src/main/java/gen/GeneratorBase.java | 15 + .../ohosgen/src/main/java/parse/ParseCpp.java | 2 - .../src/main/java/utils/FileUtils.java | 1 - .../src/main/java/utils/StringUtils.java | 19 +- .../src/test/java/parse/ParseCppTest.java | 343 +++++++------- 9 files changed, 507 insertions(+), 449 deletions(-) diff --git a/src/intellij_plugin/ohosgen/src/main/java/antlr/ParseBaseListener.java b/src/intellij_plugin/ohosgen/src/main/java/antlr/ParseBaseListener.java index 202e23e7..8305d6f6 100644 --- a/src/intellij_plugin/ohosgen/src/main/java/antlr/ParseBaseListener.java +++ b/src/intellij_plugin/ohosgen/src/main/java/antlr/ParseBaseListener.java @@ -32,7 +32,7 @@ public interface ParseBaseListener { * @param str 删除字符串最后一个空格 * @return 返回字符串 */ - public static String removeLastSpace(String str) { + static String removeLastSpace(String str) { if (str != null && !str.isEmpty() && str.charAt(str.length() - 1) == ' ') { return str.substring(0, str.length() - 1); } diff --git a/src/intellij_plugin/ohosgen/src/main/java/antlr/cpp/CPP14CustomListener.java b/src/intellij_plugin/ohosgen/src/main/java/antlr/cpp/CPP14CustomListener.java index 0003190d..a4b5f78c 100644 --- a/src/intellij_plugin/ohosgen/src/main/java/antlr/cpp/CPP14CustomListener.java +++ b/src/intellij_plugin/ohosgen/src/main/java/antlr/cpp/CPP14CustomListener.java @@ -301,173 +301,185 @@ public class CPP14CustomListener extends CPP14ParserBaseListener implements Pars System.out.println("c/cpp attribute: " + ctx.getText()); } - @Override - public void enterMemberdeclaration(CPP14Parser.MemberdeclarationContext ctx) { - super.enterMemberdeclaration(ctx); - System.out.println("c/cpp Memberdeclaration: " + ctx.getText()); - if (this.currentObject instanceof StructObj so) { - String type = ctx.declSpecifierSeq().getText(); - List dscl = ctx.declSpecifierSeq().declSpecifier(); - type = !dscl.isEmpty() ? "" : type; - for (CPP14Parser.DeclSpecifierContext dscItem : dscl) { - if (dscItem.typeSpecifier() != null && - dscItem.typeSpecifier().trailingTypeSpecifier().elaboratedTypeSpecifier() != null) { - CPP14Parser.ElaboratedTypeSpecifierContext esc = - dscItem.typeSpecifier().trailingTypeSpecifier().elaboratedTypeSpecifier(); - type += esc.Enum() != null ? esc.Enum().getText() + " " : ""; - type += esc.classKey() != null ? esc.classKey().getText() + " " : ""; - type += esc.Identifier(); - } else { - type += dscItem.getText() + " "; - } - } - if (!dscl.isEmpty()) { - type = StringUtils.removeLastSpace(type); + private void setStructMember(CPP14Parser.MemberdeclarationContext ctx, StructObj so) { + String type = ctx.declSpecifierSeq().getText(); + List dscl = ctx.declSpecifierSeq().declSpecifier(); + type = !dscl.isEmpty() ? "" : type; + for (CPP14Parser.DeclSpecifierContext dscItem : dscl) { + if (dscItem.typeSpecifier() != null && + dscItem.typeSpecifier().trailingTypeSpecifier().elaboratedTypeSpecifier() != null) { + CPP14Parser.ElaboratedTypeSpecifierContext esc = + dscItem.typeSpecifier().trailingTypeSpecifier().elaboratedTypeSpecifier(); + type += esc.Enum() != null ? esc.Enum().getText() + " " : ""; + type += esc.classKey() != null ? esc.classKey().getText() + " " : ""; + type += esc.Identifier(); + } else { + type += dscItem.getText() + " "; } - String name = ctx.memberDeclaratorList().getText(); - List mdcl = ctx.memberDeclaratorList().memberDeclarator(); - for (CPP14Parser.MemberDeclaratorContext mdc : mdcl) { - if (mdc.declarator() != null && mdc.declarator().pointerDeclarator() != null && - mdc.declarator().pointerDeclarator().noPointerDeclarator() != null && - mdc.declarator().pointerDeclarator().noPointerDeclarator().constantExpression() == null && - mdc.declarator().pointerDeclarator().noPointerDeclarator().noPointerDeclarator() != null) { - CPP14Parser.NoPointerDeclaratorContext npdcItem = - mdc.declarator().pointerDeclarator().noPointerDeclarator(); - FuncObj fo = new FuncObj(); - fo.setName(npdcItem.noPointerDeclarator().getText()); - fo.setRetValue(type); - - if (npdcItem.parametersAndQualifiers() != null && - npdcItem.parametersAndQualifiers().parameterDeclarationClause() != null && - npdcItem.parametersAndQualifiers().parameterDeclarationClause().parameterDeclarationList() != null) { - List pdcList = - npdcItem.parametersAndQualifiers().parameterDeclarationClause(). - parameterDeclarationList().parameterDeclaration(); - for (CPP14Parser.ParameterDeclarationContext pdcItem : pdcList) { - ParamObj poItem = new ParamObj(); - poItem.setName(pdcItem.declarator().getText()); - poItem.setType(pdcItem.declSpecifierSeq().getText()); - fo.addParam(poItem); - } - } + } + if (!dscl.isEmpty()) { + type = StringUtils.removeLastSpace(type); + } + String name = ctx.memberDeclaratorList().getText(); + List mdcl = ctx.memberDeclaratorList().memberDeclarator(); + for (CPP14Parser.MemberDeclaratorContext mdc : mdcl) { + if (mdc.declarator() != null && mdc.declarator().pointerDeclarator() != null && + mdc.declarator().pointerDeclarator().noPointerDeclarator() != null && + mdc.declarator().pointerDeclarator().noPointerDeclarator().constantExpression() == null && + mdc.declarator().pointerDeclarator().noPointerDeclarator().noPointerDeclarator() != null) { + CPP14Parser.NoPointerDeclaratorContext npdcItem = + mdc.declarator().pointerDeclarator().noPointerDeclarator(); + FuncObj fo = new FuncObj(); + fo.setName(npdcItem.noPointerDeclarator().getText()); + fo.setRetValue(type); - so.addFunc(fo); - } else { - ParamObj po = new ParamObj(); - po.setName(mdc.declarator().getText()); - if (mdc.braceOrEqualInitializer() != null) { - po.setStrValue(mdc.braceOrEqualInitializer().initializerClause().getText()); + if (npdcItem.parametersAndQualifiers() != null && + npdcItem.parametersAndQualifiers().parameterDeclarationClause() != null && + npdcItem.parametersAndQualifiers().parameterDeclarationClause().parameterDeclarationList() != null) { + List pdcList = + npdcItem.parametersAndQualifiers().parameterDeclarationClause(). + parameterDeclarationList().parameterDeclaration(); + for (CPP14Parser.ParameterDeclarationContext pdcItem : pdcList) { + ParamObj poItem = new ParamObj(); + poItem.setName(pdcItem.declarator().getText()); + poItem.setType(pdcItem.declSpecifierSeq().getText()); + fo.addParam(poItem); } - po.setType(type); - so.addMember(po); } + + so.addFunc(fo); + } else { + ParamObj po = new ParamObj(); + po.setName(mdc.declarator().getText()); + if (mdc.braceOrEqualInitializer() != null) { + po.setStrValue(mdc.braceOrEqualInitializer().initializerClause().getText()); + } + po.setType(type); + so.addMember(po); } - } else if (this.currentObject instanceof UnionObj uo) { - String type = ctx.declSpecifierSeq().getText(); - List dscl = ctx.declSpecifierSeq().declSpecifier(); - type = !dscl.isEmpty() ? "" : type; - for (CPP14Parser.DeclSpecifierContext dscItem : dscl) { - type += dscItem.getText() + " "; - } - if (!dscl.isEmpty()) { - type = StringUtils.removeLastSpace(type); - } - String name = ctx.memberDeclaratorList().getText(); - List mdcl = ctx.memberDeclaratorList().memberDeclarator(); - for (CPP14Parser.MemberDeclaratorContext mdc : mdcl) { - if (mdc.declarator() != null && mdc.declarator().pointerDeclarator() != null && - mdc.declarator().pointerDeclarator().noPointerDeclarator() != null && - mdc.declarator().pointerDeclarator().noPointerDeclarator().constantExpression() == null && - mdc.declarator().pointerDeclarator().noPointerDeclarator().noPointerDeclarator() != null) { - CPP14Parser.NoPointerDeclaratorContext npdcItem = - mdc.declarator().pointerDeclarator().noPointerDeclarator(); - FuncObj fo = new FuncObj(); - fo.setName(npdcItem.noPointerDeclarator().getText()); - fo.setRetValue(type); - - if (npdcItem.parametersAndQualifiers() != null && - npdcItem.parametersAndQualifiers().parameterDeclarationClause() != null && - npdcItem.parametersAndQualifiers().parameterDeclarationClause().parameterDeclarationList() != null) { - List pdcList = - npdcItem.parametersAndQualifiers().parameterDeclarationClause(). - parameterDeclarationList().parameterDeclaration(); - for (CPP14Parser.ParameterDeclarationContext pdcItem : pdcList) { - ParamObj poItem = new ParamObj(); - poItem.setName(pdcItem.declarator().getText()); - poItem.setType(pdcItem.declSpecifierSeq().getText()); - fo.addParam(poItem); - } - } + } + } - uo.addFunc(fo); - } else { - ParamObj po = new ParamObj(); - po.setName(mdc.declarator().getText()); - if (mdc.braceOrEqualInitializer() != null) { - po.setStrValue(mdc.braceOrEqualInitializer().initializerClause().getText()); + private void setUnionMember(CPP14Parser.MemberdeclarationContext ctx, UnionObj uo) { + String type = ctx.declSpecifierSeq().getText(); + List dscl = ctx.declSpecifierSeq().declSpecifier(); + type = !dscl.isEmpty() ? "" : type; + for (CPP14Parser.DeclSpecifierContext dscItem : dscl) { + type += dscItem.getText() + " "; + } + if (!dscl.isEmpty()) { + type = StringUtils.removeLastSpace(type); + } + String name = ctx.memberDeclaratorList().getText(); + List mdcl = ctx.memberDeclaratorList().memberDeclarator(); + for (CPP14Parser.MemberDeclaratorContext mdc : mdcl) { + if (mdc.declarator() != null && mdc.declarator().pointerDeclarator() != null && + mdc.declarator().pointerDeclarator().noPointerDeclarator() != null && + mdc.declarator().pointerDeclarator().noPointerDeclarator().constantExpression() == null && + mdc.declarator().pointerDeclarator().noPointerDeclarator().noPointerDeclarator() != null) { + CPP14Parser.NoPointerDeclaratorContext npdcItem = + mdc.declarator().pointerDeclarator().noPointerDeclarator(); + FuncObj fo = new FuncObj(); + fo.setName(npdcItem.noPointerDeclarator().getText()); + fo.setRetValue(type); + + if (npdcItem.parametersAndQualifiers() != null && + npdcItem.parametersAndQualifiers().parameterDeclarationClause() != null && + npdcItem.parametersAndQualifiers().parameterDeclarationClause().parameterDeclarationList() != null) { + List pdcList = + npdcItem.parametersAndQualifiers().parameterDeclarationClause(). + parameterDeclarationList().parameterDeclaration(); + for (CPP14Parser.ParameterDeclarationContext pdcItem : pdcList) { + ParamObj poItem = new ParamObj(); + poItem.setName(pdcItem.declarator().getText()); + poItem.setType(pdcItem.declSpecifierSeq().getText()); + fo.addParam(poItem); } - po.setType(type); - uo.addMember(po); } - } - } else if (this.currentObject instanceof ClassObj co) { - String type = ctx.declSpecifierSeq().getText(); - List dscl = ctx.declSpecifierSeq().declSpecifier(); - type = !dscl.isEmpty() ? "" : type; - for (CPP14Parser.DeclSpecifierContext dscItem : dscl) { - if (dscItem.typeSpecifier() != null && - dscItem.typeSpecifier().trailingTypeSpecifier().elaboratedTypeSpecifier() != null) { - CPP14Parser.ElaboratedTypeSpecifierContext esc = - dscItem.typeSpecifier().trailingTypeSpecifier().elaboratedTypeSpecifier(); - type += esc.Enum() != null ? esc.Enum().getText() + " " : ""; - type += esc.classKey() != null ? esc.classKey().getText() + " " : ""; - type += esc.Identifier(); - } else { - type += dscItem.getText() + " "; + + uo.addFunc(fo); + } else { + ParamObj po = new ParamObj(); + po.setName(mdc.declarator().getText()); + if (mdc.braceOrEqualInitializer() != null) { + po.setStrValue(mdc.braceOrEqualInitializer().initializerClause().getText()); } + po.setType(type); + uo.addMember(po); } - if (!dscl.isEmpty()) { - type = StringUtils.removeLastSpace(type); + } + } + + private void setClassMember(CPP14Parser.MemberdeclarationContext ctx, ClassObj co) { + String type = ctx.declSpecifierSeq().getText(); + List dscl = ctx.declSpecifierSeq().declSpecifier(); + type = !dscl.isEmpty() ? "" : type; + for (CPP14Parser.DeclSpecifierContext dscItem : dscl) { + if (dscItem.typeSpecifier() != null && + dscItem.typeSpecifier().trailingTypeSpecifier().elaboratedTypeSpecifier() != null) { + CPP14Parser.ElaboratedTypeSpecifierContext esc = + dscItem.typeSpecifier().trailingTypeSpecifier().elaboratedTypeSpecifier(); + type += esc.Enum() != null ? esc.Enum().getText() + " " : ""; + type += esc.classKey() != null ? esc.classKey().getText() + " " : ""; + type += esc.Identifier(); + } else { + type += dscItem.getText() + " "; } - String name = ctx.memberDeclaratorList().getText(); - List mdcl = ctx.memberDeclaratorList().memberDeclarator(); - for (CPP14Parser.MemberDeclaratorContext mdc : mdcl) { - if (mdc.declarator() != null && mdc.declarator().pointerDeclarator() != null && - mdc.declarator().pointerDeclarator().noPointerDeclarator() != null && - mdc.declarator().pointerDeclarator().noPointerDeclarator().constantExpression() == null && - mdc.declarator().pointerDeclarator().noPointerDeclarator().noPointerDeclarator() != null) { - CPP14Parser.NoPointerDeclaratorContext npdcItem = - mdc.declarator().pointerDeclarator().noPointerDeclarator(); - FuncObj fo = new FuncObj(); - fo.setName(npdcItem.noPointerDeclarator().getText()); - fo.setRetValue(type); - - if (npdcItem.parametersAndQualifiers() != null && + } + if (!dscl.isEmpty()) { + type = StringUtils.removeLastSpace(type); + } + String name = ctx.memberDeclaratorList().getText(); + List mdcl = ctx.memberDeclaratorList().memberDeclarator(); + for (CPP14Parser.MemberDeclaratorContext mdc : mdcl) { + if (mdc.declarator() != null && mdc.declarator().pointerDeclarator() != null && + mdc.declarator().pointerDeclarator().noPointerDeclarator() != null && + mdc.declarator().pointerDeclarator().noPointerDeclarator().constantExpression() == null && + mdc.declarator().pointerDeclarator().noPointerDeclarator().noPointerDeclarator() != null) { + CPP14Parser.NoPointerDeclaratorContext npdcItem = + mdc.declarator().pointerDeclarator().noPointerDeclarator(); + FuncObj fo = new FuncObj(); + fo.setName(npdcItem.noPointerDeclarator().getText()); + fo.setRetValue(type); + + if (npdcItem.parametersAndQualifiers() != null && npdcItem.parametersAndQualifiers().parameterDeclarationClause() != null && npdcItem.parametersAndQualifiers().parameterDeclarationClause().parameterDeclarationList() != null) { - List pdcList = - npdcItem.parametersAndQualifiers().parameterDeclarationClause(). - parameterDeclarationList().parameterDeclaration(); - for (CPP14Parser.ParameterDeclarationContext pdcItem : pdcList) { - ParamObj poItem = new ParamObj(); - poItem.setName(pdcItem.declarator().getText()); - poItem.setType(pdcItem.declSpecifierSeq().getText()); - fo.addParam(poItem); - } + List pdcList = + npdcItem.parametersAndQualifiers().parameterDeclarationClause(). + parameterDeclarationList().parameterDeclaration(); + for (CPP14Parser.ParameterDeclarationContext pdcItem : pdcList) { + ParamObj poItem = new ParamObj(); + poItem.setName(pdcItem.declarator().getText()); + poItem.setType(pdcItem.declSpecifierSeq().getText()); + fo.addParam(poItem); } + } - co.addFunc(fo); - } else { - ParamObj po = new ParamObj(); - po.setName(mdc.declarator().getText()); - if (mdc.braceOrEqualInitializer() != null) { - po.setStrValue(mdc.braceOrEqualInitializer().initializerClause().getText()); - } - po.setType(type); - co.addParam(po); + co.addFunc(fo); + } else { + ParamObj po = new ParamObj(); + po.setName(mdc.declarator().getText()); + if (mdc.braceOrEqualInitializer() != null) { + po.setStrValue(mdc.braceOrEqualInitializer().initializerClause().getText()); } + po.setType(type); + co.addParam(po); } } + } + + @Override + public void enterMemberdeclaration(CPP14Parser.MemberdeclarationContext ctx) { + super.enterMemberdeclaration(ctx); + System.out.println("c/cpp Memberdeclaration: " + ctx.getText()); + if (this.currentObject instanceof StructObj so) { + setStructMember(ctx, so); + } else if (this.currentObject instanceof UnionObj uo) { + setUnionMember(ctx, uo); + } else if (this.currentObject instanceof ClassObj co) { + setClassMember(ctx, co); + } } @@ -526,66 +538,81 @@ public class CPP14CustomListener extends CPP14ParserBaseListener implements Pars System.out.println("c/cpp enterBlockDeclaration: " + ctx.getText()); } + private void parseConstant(CPP14Parser.SimpleDeclarationContext ctx) { + if (ctx.initDeclaratorList().initDeclarator(0) != null && + ctx.initDeclaratorList().initDeclarator(0).initializer() != null && + ctx.declSpecifierSeq() != null) { + String paType = ""; + List dscl = ctx.declSpecifierSeq().declSpecifier(); + for (CPP14Parser.DeclSpecifierContext item: dscl) { + paType += item.getText() + " "; + } + paType = StringUtils.removeLastSpace(paType); + String paName = ctx.initDeclaratorList().initDeclarator(0).declarator().getText(); + String initValue = ctx.initDeclaratorList().initDeclarator(0).initializer().braceOrEqualInitializer().initializerClause().getText(); + + ParamObj po = new ParamObj(); + po.setName(paName); + po.setType(paType); + po.setStrValue(initValue); + this.constList.add(po); + } + } + + private void parseFunction(CPP14Parser.SimpleDeclarationContext ctx) { + CPP14Parser.NoPointerDeclaratorContext npdc = ctx.initDeclaratorList().initDeclarator(0). + declarator().pointerDeclarator().noPointerDeclarator(); + String name = npdc.noPointerDeclarator().getText(); + FuncObj fo = new FuncObj(); + fo.setName(name); + List dscl = ctx.declSpecifierSeq().declSpecifier(); + for (CPP14Parser.DeclSpecifierContext dsc : dscl) { + fo.addDecl(dsc.getText()); + } + if (npdc.parametersAndQualifiers().parameterDeclarationClause() != null) { + List pdcl = npdc.parametersAndQualifiers(). + parameterDeclarationClause().parameterDeclarationList().parameterDeclaration(); + for (CPP14Parser.ParameterDeclarationContext pdc : pdcl) { + String varName = pdc.declarator() != null ? pdc.declarator().getText() : ""; + String typeName = ""; + + List dscList = pdc.declSpecifierSeq().declSpecifier(); + for (CPP14Parser.DeclSpecifierContext dscItem : dscList) { + typeName += dscItem.getText() + " "; + } + if (dscList.size() > 0) { + typeName = typeName.substring(0, typeName.length() - 1); + } + + ParamObj po = new ParamObj(); + po.setName(varName); + po.setType(typeName); + fo.addParam(po); + } + } + + this.currentObject = fo; + this.currentToken = CppToken.CPP_TOKEN_FUNCTION; + this.funcObjList.add(fo); + } + @Override public void enterSimpleDeclaration(CPP14Parser.SimpleDeclarationContext ctx) { super.enterSimpleDeclaration(ctx); System.out.println("c/cpp enterSimpleDeclaration: " + ctx.getText()); - if (ctx.initDeclaratorList() != null) { - CPP14Parser.NoPointerDeclaratorContext npdc = ctx.initDeclaratorList().initDeclarator(0). - declarator().pointerDeclarator().noPointerDeclarator(); - if (npdc.noPointerDeclarator() != null) { - String name = npdc.noPointerDeclarator().getText(); - FuncObj fo = new FuncObj(); - fo.setName(name); - List dscl = ctx.declSpecifierSeq().declSpecifier(); - for (CPP14Parser.DeclSpecifierContext dsc : dscl) { - fo.addDecl(dsc.getText()); - } - if (npdc.parametersAndQualifiers().parameterDeclarationClause() != null) { - List pdcl = npdc.parametersAndQualifiers(). - parameterDeclarationClause().parameterDeclarationList().parameterDeclaration(); - for (CPP14Parser.ParameterDeclarationContext pdc : pdcl) { - String varName = pdc.declarator() != null ? pdc.declarator().getText() : ""; - String typeName = ""; - - List dscList = pdc.declSpecifierSeq().declSpecifier(); - for (CPP14Parser.DeclSpecifierContext dscItem : dscList) { - typeName += dscItem.getText() + " "; - } - if (dscList.size() > 0) { - typeName = typeName.substring(0, typeName.length() - 1); - } - - ParamObj po = new ParamObj(); - po.setName(varName); - po.setType(typeName); - fo.addParam(po); - } - } + if (ctx.initDeclaratorList() == null) { + return; + } - this.currentObject = fo; - this.currentToken = CppToken.CPP_TOKEN_FUNCTION; - this.funcObjList.add(fo); - } else { - if (ctx.initDeclaratorList().initDeclarator(0) != null && ctx.initDeclaratorList().initDeclarator(0).initializer() != null) { - String paType = ""; - List dscl = ctx.declSpecifierSeq().declSpecifier(); - for (CPP14Parser.DeclSpecifierContext item: dscl) { - paType += item.getText() + " "; - } - paType = StringUtils.removeLastSpace(paType); - String paName = ctx.initDeclaratorList().initDeclarator(0).declarator().getText(); - String initValue = ctx.initDeclaratorList().initDeclarator(0).initializer().braceOrEqualInitializer().initializerClause().getText(); - - ParamObj po = new ParamObj(); - po.setName(paName); - po.setType(paType); - po.setStrValue(initValue); - this.constList.add(po); - } - } + CPP14Parser.NoPointerDeclaratorContext npdc = ctx.initDeclaratorList().initDeclarator(0). + declarator().pointerDeclarator().noPointerDeclarator(); + if (npdc.noPointerDeclarator() != null) { + parseFunction(ctx); + } else { + parseConstant(ctx); } + } @Override diff --git a/src/intellij_plugin/ohosgen/src/main/java/gen/GenDtsFile.java b/src/intellij_plugin/ohosgen/src/main/java/gen/GenDtsFile.java index 35fd9f50..e93ccdcb 100644 --- a/src/intellij_plugin/ohosgen/src/main/java/gen/GenDtsFile.java +++ b/src/intellij_plugin/ohosgen/src/main/java/gen/GenDtsFile.java @@ -33,63 +33,63 @@ import java.util.Map; * @version 1.0 */ public class GenDtsFile extends GeneratorBase { - private final String TS_ENUM_TOKEN = "enum"; - private final String TS_CLASS_TOKEN = "class"; - private final String TS_EXPORT_TOKEN = "export"; - private final String TS_IMPLEMENTS_TOKEN = "implements"; - private final String TS_EXTENDS_TOKEN = "extends"; - private final String TS_CONST_TOKEN = "const"; - private final String TS_PRIVATE_TOKEN = "private"; - private final String TS_PUBLIC_TOKEN = "public"; - private final String TS_INTERFACE_TOKEN = "interface"; - private final String TS_PROTECTED_TOKEN = "protected"; - private final String TS_STATIC_TOKEN = "static"; - private final String TS_ANY_TOKEN = "any"; - private final String TS_NUMBER_TOKEN = "number"; - private final String TS_NEVER_TOKEN = "never"; - private final String TS_BOOLEAN_TOKEN = "boolean"; - private final String TS_STRING_TOKEN = "string"; - private final String TS_UNIQUE_TOKEN = "unique"; - private final String TS_SYMBOL_TOKEN = "symbol"; - private final String TS_UNDEFINED_TOKEN = "undefined"; - private final String TS_OBJECT_TOKEN = "object"; - private final String TS_OF_TOKEN = "of"; - private final String TS_KEYOF_TOKEN = "keyof"; - private final String TS_TYPE_TOKEN = "type"; - private final String TS_CONSTRUCTOR_TOKEN = "constructor"; - private final String TS_NAMESPACE_TOKEN = "namespace"; - private final String TS_REQUIRE_TOKEN = "require"; - private final String TS_MODULE_TOKEN = "module"; - private final String TS_DECLARE_TOKEN = "declare"; - private final String TS_ABSTRACT_TOKEN = "abstract"; - private final String TS_DEBUGGER_TOKEN = "debugger"; - private final String TS_FUNCTION_TOKEN = "function"; - private final String TS_THIS_TOKEN = "this"; - private final String TS_WITH_TOKEN = "with"; - private final String TS_DEFAULT_TOKEN = "default"; - private final String TS_READONLY_TOKEN = "readonly"; - private final String TS_ASYNC_TOKEN = "async"; - private final String TS_AWAIT_TOKEN = "await"; - private final String TS_YIELD_TOKEN = "yield"; - private final String TS_NEW_LINE = "\n"; - private final String TS_TAB_SPACE = "\t"; - private final String TS_BLANK_SPACE = " "; - private final String TS_SPLIT = " | "; - private final String TS_EQUAL = " = "; - private final String TS_COMMA = ","; - private final String TS_SEMICOLON = ";"; - private final String TS_COLON = ":"; - private final String TS_LEFT_BRACE = "{"; - private final String TS_RIGHT_BRACE = "}"; - private final String TS_LEFT_PARENTHESES = "("; - private final String TS_RIGHT_PARENTHESES = ")"; - private final String TS_LEFT_SQUARE_BRACKET = "["; - private final String TS_RIGHT_SQUARE_BRACKET = "]"; - private final String TS_LEFT_ANGLE_BRACKET = "<"; - private final String TS_RIGHT_ANGLE_BRACKET = ">"; - - private final String TS_FILE_PREFIX = "ag_"; - private final String TS_FILE_SUFFIX = ".d.ts"; + private static final String TS_ENUM_TOKEN = "enum"; + private static final String TS_CLASS_TOKEN = "class"; + private static final String TS_EXPORT_TOKEN = "export"; + private static final String TS_IMPLEMENTS_TOKEN = "implements"; + private static final String TS_EXTENDS_TOKEN = "extends"; + private static final String TS_CONST_TOKEN = "const"; + private static final String TS_PRIVATE_TOKEN = "private"; + private static final String TS_PUBLIC_TOKEN = "public"; + private static final String TS_INTERFACE_TOKEN = "interface"; + private static final String TS_PROTECTED_TOKEN = "protected"; + private static final String TS_STATIC_TOKEN = "static"; + private static final String TS_ANY_TOKEN = "any"; + private static final String TS_NUMBER_TOKEN = "number"; + private static final String TS_NEVER_TOKEN = "never"; + private static final String TS_BOOLEAN_TOKEN = "boolean"; + private static final String TS_STRING_TOKEN = "string"; + private static final String TS_UNIQUE_TOKEN = "unique"; + private static final String TS_SYMBOL_TOKEN = "symbol"; + private static final String TS_UNDEFINED_TOKEN = "undefined"; + private static final String TS_OBJECT_TOKEN = "object"; + private static final String TS_OF_TOKEN = "of"; + private static final String TS_KEYOF_TOKEN = "keyof"; + private static final String TS_TYPE_TOKEN = "type"; + private static final String TS_CONSTRUCTOR_TOKEN = "constructor"; + private static final String TS_NAMESPACE_TOKEN = "namespace"; + private static final String TS_REQUIRE_TOKEN = "require"; + private static final String TS_MODULE_TOKEN = "module"; + private static final String TS_DECLARE_TOKEN = "declare"; + private static final String TS_ABSTRACT_TOKEN = "abstract"; + private static final String TS_DEBUGGER_TOKEN = "debugger"; + private static final String TS_FUNCTION_TOKEN = "function"; + private static final String TS_THIS_TOKEN = "this"; + private static final String TS_WITH_TOKEN = "with"; + private static final String TS_DEFAULT_TOKEN = "default"; + private static final String TS_READONLY_TOKEN = "readonly"; + private static final String TS_ASYNC_TOKEN = "async"; + private static final String TS_AWAIT_TOKEN = "await"; + private static final String TS_YIELD_TOKEN = "yield"; + private static final String TS_NEW_LINE = "\n"; + private static final String TS_TAB_SPACE = "\t"; + private static final String TS_BLANK_SPACE = " "; + private static final String TS_SPLIT = " | "; + private static final String TS_EQUAL = " = "; + private static final String TS_COMMA = ","; + private static final String TS_SEMICOLON = ";"; + private static final String TS_COLON = ":"; + private static final String TS_LEFT_BRACE = "{"; + private static final String TS_RIGHT_BRACE = "}"; + private static final String TS_LEFT_PARENTHESES = "("; + private static final String TS_RIGHT_PARENTHESES = ")"; + private static final String TS_LEFT_SQUARE_BRACKET = "["; + private static final String TS_RIGHT_SQUARE_BRACKET = "]"; + private static final String TS_LEFT_ANGLE_BRACKET = "<"; + private static final String TS_RIGHT_ANGLE_BRACKET = ">"; + + private static final String TS_FILE_PREFIX = "ag_"; + private static final String TS_FILE_SUFFIX = ".d.ts"; private String interfaceContent = ""; private String enumContent = ""; @@ -311,7 +311,6 @@ public class GenDtsFile extends GeneratorBase { String resContent = ""; for (EnumObj eo : eol) { -// System.out.println("Enum jsonStr: " + eo.toJsonString()); String enumName = eo.getName(); enumName = !enumName.isEmpty() ? enumName : eo.getAlias(); List memList = eo.getMemberList(); @@ -332,7 +331,6 @@ public class GenDtsFile extends GeneratorBase { resContent += TS_NEW_LINE + TS_RIGHT_BRACE + TS_SEMICOLON + TS_NEW_LINE; } this.enumContent = resContent; -// System.out.println("genEnumList : " + resContent); }; /** @@ -345,7 +343,6 @@ public class GenDtsFile extends GeneratorBase { String resContent = ""; for (ClassObj co : col) { -// System.out.println("Class jsonStr: " + co.toJsonString()); String className = co.getName(); className = !className.isEmpty() ? className : co.getAlias(); List funcList = co.getFuncList(); @@ -392,7 +389,6 @@ public class GenDtsFile extends GeneratorBase { resContent += TS_NEW_LINE + TS_RIGHT_BRACE + TS_SEMICOLON + TS_NEW_LINE; } this.classContent = resContent; -// System.out.println("genClassList : " + resContent); }; /** @@ -404,7 +400,6 @@ public class GenDtsFile extends GeneratorBase { System.out.println("genFuncList : " + fol.toString()); String resContent = ""; for (FuncObj fo : fol) { -// System.out.println("Func jsonStr: " + fo.toJsonString()); String funcName = fo.getName(); funcName = !funcName.isEmpty() ? funcName : fo.getAlias(); List paList = fo.getParamList(); @@ -442,7 +437,6 @@ public class GenDtsFile extends GeneratorBase { String resContent = ""; for (StructObj so : sol) { -// System.out.println("Struct jsonStr: " + so.toJsonString()); String structName = so.getName(); structName = !structName.isEmpty() ? structName : so.getAlias(); List funcList = so.getFuncList(); @@ -489,7 +483,6 @@ public class GenDtsFile extends GeneratorBase { resContent += TS_NEW_LINE + TS_RIGHT_BRACE + TS_SEMICOLON + TS_NEW_LINE; } this.structContent = resContent; -// System.out.println("genStructList : " + resContent); }; /** @@ -511,7 +504,6 @@ public class GenDtsFile extends GeneratorBase { String resContent = ""; for (UnionObj uo : uol) { -// System.out.println("Union jsonStr: " + uo.toJsonString()); String unionName = uo.getName(); unionName = !unionName.isEmpty() ? unionName : uo.getAlias(); List paList = uo.getMemList(); @@ -530,7 +522,6 @@ public class GenDtsFile extends GeneratorBase { resContent += TS_SEMICOLON + TS_NEW_LINE; } this.unionContent = resContent; -// System.out.println("genUnionList : " + resContent); }; /** @@ -543,7 +534,6 @@ public class GenDtsFile extends GeneratorBase { String resContent = ""; for (ParamObj po : pol) { -// System.out.println("Var jsonStr: " + uo.toJsonString()); String paName = po.getName(); String paType = cpp2TsKey(po.getType()); String paValue = po.getStrValue(0); 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 4fad781f..5759fd71 100644 --- a/src/intellij_plugin/ohosgen/src/main/java/gen/GenerateFactory.java +++ b/src/intellij_plugin/ohosgen/src/main/java/gen/GenerateFactory.java @@ -1,8 +1,19 @@ -package gen; +/* + * Copyright (c) 2025 Shenzhen Kaihong Digital. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ -import parse.ParseBase; -import parse.ParseCpp; -import parse.ParseTs; +package gen; import java.util.Locale; diff --git a/src/intellij_plugin/ohosgen/src/main/java/gen/GeneratorBase.java b/src/intellij_plugin/ohosgen/src/main/java/gen/GeneratorBase.java index be20ae00..434bf8fb 100644 --- a/src/intellij_plugin/ohosgen/src/main/java/gen/GeneratorBase.java +++ b/src/intellij_plugin/ohosgen/src/main/java/gen/GeneratorBase.java @@ -1,3 +1,18 @@ +/* + * Copyright (c) 2025 Shenzhen Kaihong Digital. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + package gen; import grammar.*; diff --git a/src/intellij_plugin/ohosgen/src/main/java/parse/ParseCpp.java b/src/intellij_plugin/ohosgen/src/main/java/parse/ParseCpp.java index 023fe237..30fa6c89 100644 --- a/src/intellij_plugin/ohosgen/src/main/java/parse/ParseCpp.java +++ b/src/intellij_plugin/ohosgen/src/main/java/parse/ParseCpp.java @@ -22,8 +22,6 @@ import antlr.cpp.CPP14Lexer; import antlr.cpp.CPP14Parser; import com.fasterxml.jackson.core.JsonProcessingException; import com.fasterxml.jackson.databind.ObjectMapper; -import gen.GenerateFactory; -import gen.GeneratorBase; import grammar.*; import org.antlr.v4.runtime.CharStream; import org.antlr.v4.runtime.CommonTokenStream; diff --git a/src/intellij_plugin/ohosgen/src/main/java/utils/FileUtils.java b/src/intellij_plugin/ohosgen/src/main/java/utils/FileUtils.java index ae7fc601..f0a48213 100644 --- a/src/intellij_plugin/ohosgen/src/main/java/utils/FileUtils.java +++ b/src/intellij_plugin/ohosgen/src/main/java/utils/FileUtils.java @@ -120,7 +120,6 @@ public class FileUtils { try (BufferedWriter writer = new BufferedWriter(new FileWriter(filePath, true))) { // true 表示追加模式‌:ml-citation{ref="6" data="citationList"} writer.write(content); -// writer.newLine(); // 换行追加 } catch (IOException e) { System.out.println("appendText error: " + e.getMessage()); } 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 ca17f51e..94b2c212 100644 --- a/src/intellij_plugin/ohosgen/src/main/java/utils/StringUtils.java +++ b/src/intellij_plugin/ohosgen/src/main/java/utils/StringUtils.java @@ -1,6 +1,21 @@ +/* + * Copyright (c) 2025 Shenzhen Kaihong Digital. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + package utils; -import java.util.List; +import java.util.Locale; /** *

类名:该类用于xxx

@@ -22,7 +37,7 @@ public class StringUtils { int index = str.indexOf(delimiter); if (index != -1) { String prefix = str.substring(0, index); - String suffix = str.substring(index + 1).toLowerCase(); + String suffix = str.substring(index + 1).toLowerCase(Locale.ROOT); System.out.println("前缀: " + prefix); System.out.println("后缀: " + suffix); } else { diff --git a/src/intellij_plugin/ohosgen/src/test/java/parse/ParseCppTest.java b/src/intellij_plugin/ohosgen/src/test/java/parse/ParseCppTest.java index 73722dcb..f2399a48 100644 --- a/src/intellij_plugin/ohosgen/src/test/java/parse/ParseCppTest.java +++ b/src/intellij_plugin/ohosgen/src/test/java/parse/ParseCppTest.java @@ -37,6 +37,177 @@ import static org.junit.jupiter.api.Assertions.*; */ class ParseCppTest { + private final String testCStreamFile1 = "#ifndef COAP_ADDRESS_H_\n" + + "#define COAP_ADDRESS_H_\n" + + "\n" + + "#include \n" + + "#include \n" + + "#include \n" + + "#include \n" + + "#include \"libcoap.h\"\n" + + "\n" + + "#include \"coap3/coap_pdu.h\"\n" + + "\n" + + "#if defined(WITH_LWIP)\n" + + "\n" + + "#include \n" + + "\n" + + "struct coap_address_t {\n" + + " uint16_t port;\n" + + " ip_addr_t addr;\n" + + "};\n" + + "\n" + + "/**\n" + + " * Returns the port from @p addr in host byte order.\n" + + " */\n" + + "COAP_STATIC_INLINE uint16_t\n" + + "coap_address_get_port(const coap_address_t *addr) {\n" + + " return addr->port;\n" + + "}\n" + + "\n" + + "/**\n" + + " * Sets the port field of @p addr to @p port (in host byte order).\n" + + " */\n" + + "COAP_STATIC_INLINE void\n" + + "coap_address_set_port(coap_address_t *addr, uint16_t port) {\n" + + " addr->port = port;\n" + + "}\n" + + "\n" + + "#define _coap_address_equals_impl(A, B) \\\n" + + " ((A)->port == (B)->port && \\\n" + + " (!!ip_addr_cmp(&(A)->addr,&(B)->addr)))\n" + + "\n" + + "#define _coap_address_isany_impl(A) ip_addr_isany(&(A)->addr)\n" + + "\n" + + "#define _coap_is_mcast_impl(Address) ip_addr_ismulticast(&(Address)->addr)\n" + + "\n" + + "#ifdef COAP_SUPPORT_SOCKET_BROADCAST\n" + + "#define _coap_is_bcast_impl(Address) ip_addr_isbroadcast(&(Address)->addr)\n" + + "#endif\n" + + "\n" + + "#elif defined(WITH_CONTIKI)\n" + + "\n" + + "#include \"uip.h\"\n" + + "\n" + + "struct coap_address_t {\n" + + " uip_ipaddr_t addr;\n" + + " uint16_t port;\n" + + "};"; + + private final String testCStreamFile2 = "/*-------------------------------------------------------------------------\n" + + " * dEQP glslang integration\n" + + " * ------------------------\n" + + " *\n" + + " *\n" + + " * Licensed under the Apache License, Version 2.0 (the \"License\");\n" + + " * you may not use this file except in compliance with the License.\n" + + " * You may obtain a copy of the License at\n" + + " *\n" + + " * http://www.apache.org/licenses/LICENSE-2.0\n" + + " *\n" + + " * Unless required by applicable law or agreed to in writing, software\n" + + " * distributed under the License is distributed on an \"AS IS\" BASIS,\n" + + " * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n" + + " * See the License for the specific language governing permissions and\n" + + " * limitations under the License.\n" + + " *\n" + + " *//*!\n" + + " * \\file\n" + + " * \\brief glslang OS interface.\n" + + " *//*--------------------------------------------------------------------*/\n" + + "\n" + + "#include \"osinclude.h\"\n" + + "\n" + + "#include \"deThread.h\"\n" + + "#include \"deThreadLocal.h\"\n" + + "#include \"deMutex.h\"\n" + + "\n" + + "namespace glslang\n" + + "{\n" + + "\n" + + "DE_STATIC_ASSERT(sizeof(deThreadLocal)\t== sizeof(OS_TLSIndex));\n" + + "DE_STATIC_ASSERT(sizeof(deThread)\t\t== sizeof(void*));\n" + + "\n" + + "// Thread-local\n" + + "\n" + + "OS_TLSIndex OS_AllocTLSIndex (void)\n" + + "{\n" + + "\treturn (OS_TLSIndex)deThreadLocal_create();\n" + + "}\n" + + "\n" + + "bool OS_SetTLSValue (OS_TLSIndex nIndex, void* lpvValue)\n" + + "{\n" + + "\tdeThreadLocal_set((deThreadLocal)nIndex, lpvValue);\n" + + "\treturn true;\n" + + "}\n" + + "\n" + + "bool OS_FreeTLSIndex (OS_TLSIndex nIndex)\n" + + "{\n" + + "\tdeThreadLocal_destroy((deThreadLocal)nIndex);\n" + + "\treturn true;\n" + + "}\n" + + "\n" + + "void* OS_GetTLSValue (OS_TLSIndex nIndex)\n" + + "{\n" + + "\treturn deThreadLocal_get((deThreadLocal)nIndex);\n" + + "}\n" + + "\n" + + "// Global lock\n" + + "\n" + + "static deMutex s_globalLock = 0;\n" + + "\n" + + "void InitGlobalLock (void)\n" + + "{\n" + + "\tDE_ASSERT(s_globalLock == 0);\n" + + "\ts_globalLock = deMutex_create(DE_NULL);\n" + + "}\n" + + "\n" + + "void GetGlobalLock (void)\n" + + "{\n" + + "\tdeMutex_lock(s_globalLock);\n" + + "}\n" + + "\n" + + "void ReleaseGlobalLock (void)\n" + + "{\n" + + "\tdeMutex_unlock(s_globalLock);\n" + + "}\n" + + "\n" + + "// Threading\n" + + "\n" + + "DE_STATIC_ASSERT(sizeof(void*) >= sizeof(deThread));\n" + + "\n" + + "static void EnterGenericThread (void* entry)\n" + + "{\n" + + "\t((TThreadEntrypoint)entry)(DE_NULL);\n" + + "}\n" + + "\n" + + "void* OS_CreateThread (TThreadEntrypoint entry)\n" + + "{\n" + + "\treturn (void*)(deUintptr)deThread_create(EnterGenericThread, (void*)entry, DE_NULL);\n" + + "}\n" + + "\n" + + "void OS_WaitForAllThreads (void* threads, int numThreads)\n" + + "{\n" + + "\tfor (int ndx = 0; ndx < numThreads; ndx++)\n" + + "\t{\n" + + "\t\tconst deThread thread = (deThread)(deUintptr)((void**)threads)[ndx];\n" + + "\t\tdeThread_join(thread);\n" + + "\t\tdeThread_destroy(thread);\n" + + "\t}\n" + + "}\n" + + "\n" + + "void OS_Sleep (int milliseconds)\n" + + "{\n" + + "\tdeSleep(milliseconds);\n" + + "}\n" + + "\n" + + "void OS_DumpMemoryCounters (void)\n" + + "{\n" + + "\t// Not used\n" + + "}\n" + + "\n" + + "} // glslang\n"; + @BeforeEach void setUp() { } @@ -970,62 +1141,7 @@ class ParseCppTest { @Test void parseCStreamFile1() { ParseBase parser = ParseFactory.getParser("h2dts"); - String testEnum = "#ifndef COAP_ADDRESS_H_\n" + - "#define COAP_ADDRESS_H_\n" + - "\n" + - "#include \n" + - "#include \n" + - "#include \n" + - "#include \n" + - "#include \"libcoap.h\"\n" + - "\n" + - "#include \"coap3/coap_pdu.h\"\n" + - "\n" + - "#if defined(WITH_LWIP)\n" + - "\n" + - "#include \n" + - "\n" + - "struct coap_address_t {\n" + - " uint16_t port;\n" + - " ip_addr_t addr;\n" + - "};\n" + - "\n" + - "/**\n" + - " * Returns the port from @p addr in host byte order.\n" + - " */\n" + - "COAP_STATIC_INLINE uint16_t\n" + - "coap_address_get_port(const coap_address_t *addr) {\n" + - " return addr->port;\n" + - "}\n" + - "\n" + - "/**\n" + - " * Sets the port field of @p addr to @p port (in host byte order).\n" + - " */\n" + - "COAP_STATIC_INLINE void\n" + - "coap_address_set_port(coap_address_t *addr, uint16_t port) {\n" + - " addr->port = port;\n" + - "}\n" + - "\n" + - "#define _coap_address_equals_impl(A, B) \\\n" + - " ((A)->port == (B)->port && \\\n" + - " (!!ip_addr_cmp(&(A)->addr,&(B)->addr)))\n" + - "\n" + - "#define _coap_address_isany_impl(A) ip_addr_isany(&(A)->addr)\n" + - "\n" + - "#define _coap_is_mcast_impl(Address) ip_addr_ismulticast(&(Address)->addr)\n" + - "\n" + - "#ifdef COAP_SUPPORT_SOCKET_BROADCAST\n" + - "#define _coap_is_bcast_impl(Address) ip_addr_isbroadcast(&(Address)->addr)\n" + - "#endif\n" + - "\n" + - "#elif defined(WITH_CONTIKI)\n" + - "\n" + - "#include \"uip.h\"\n" + - "\n" + - "struct coap_address_t {\n" + - " uip_ipaddr_t addr;\n" + - " uint16_t port;\n" + - "};"; + String testEnum = testCStreamFile1; CodePointCharStream cStream = CharStreams.fromString(testEnum); ParseObj po = parser.parseCStream(cStream); List fol = po.getFuncList(); @@ -1078,120 +1194,7 @@ class ParseCppTest { @Test void parseCStreamFile2() { ParseBase parser = ParseFactory.getParser("h2dts"); - String testEnum = "/*-------------------------------------------------------------------------\n" + - " * dEQP glslang integration\n" + - " * ------------------------\n" + - " *\n" + - " * Copyright 2015 The Android Open Source Project\n" + - " *\n" + - " * Licensed under the Apache License, Version 2.0 (the \"License\");\n" + - " * you may not use this file except in compliance with the License.\n" + - " * You may obtain a copy of the License at\n" + - " *\n" + - " * http://www.apache.org/licenses/LICENSE-2.0\n" + - " *\n" + - " * Unless required by applicable law or agreed to in writing, software\n" + - " * distributed under the License is distributed on an \"AS IS\" BASIS,\n" + - " * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n" + - " * See the License for the specific language governing permissions and\n" + - " * limitations under the License.\n" + - " *\n" + - " *//*!\n" + - " * \\file\n" + - " * \\brief glslang OS interface.\n" + - " *//*--------------------------------------------------------------------*/\n" + - "\n" + - "#include \"osinclude.h\"\n" + - "\n" + - "#include \"deThread.h\"\n" + - "#include \"deThreadLocal.h\"\n" + - "#include \"deMutex.h\"\n" + - "\n" + - "namespace glslang\n" + - "{\n" + - "\n" + - "DE_STATIC_ASSERT(sizeof(deThreadLocal)\t== sizeof(OS_TLSIndex));\n" + - "DE_STATIC_ASSERT(sizeof(deThread)\t\t== sizeof(void*));\n" + - "\n" + - "// Thread-local\n" + - "\n" + - "OS_TLSIndex OS_AllocTLSIndex (void)\n" + - "{\n" + - "\treturn (OS_TLSIndex)deThreadLocal_create();\n" + - "}\n" + - "\n" + - "bool OS_SetTLSValue (OS_TLSIndex nIndex, void* lpvValue)\n" + - "{\n" + - "\tdeThreadLocal_set((deThreadLocal)nIndex, lpvValue);\n" + - "\treturn true;\n" + - "}\n" + - "\n" + - "bool OS_FreeTLSIndex (OS_TLSIndex nIndex)\n" + - "{\n" + - "\tdeThreadLocal_destroy((deThreadLocal)nIndex);\n" + - "\treturn true;\n" + - "}\n" + - "\n" + - "void* OS_GetTLSValue (OS_TLSIndex nIndex)\n" + - "{\n" + - "\treturn deThreadLocal_get((deThreadLocal)nIndex);\n" + - "}\n" + - "\n" + - "// Global lock\n" + - "\n" + - "static deMutex s_globalLock = 0;\n" + - "\n" + - "void InitGlobalLock (void)\n" + - "{\n" + - "\tDE_ASSERT(s_globalLock == 0);\n" + - "\ts_globalLock = deMutex_create(DE_NULL);\n" + - "}\n" + - "\n" + - "void GetGlobalLock (void)\n" + - "{\n" + - "\tdeMutex_lock(s_globalLock);\n" + - "}\n" + - "\n" + - "void ReleaseGlobalLock (void)\n" + - "{\n" + - "\tdeMutex_unlock(s_globalLock);\n" + - "}\n" + - "\n" + - "// Threading\n" + - "\n" + - "DE_STATIC_ASSERT(sizeof(void*) >= sizeof(deThread));\n" + - "\n" + - "static void EnterGenericThread (void* entry)\n" + - "{\n" + - "\t((TThreadEntrypoint)entry)(DE_NULL);\n" + - "}\n" + - "\n" + - "void* OS_CreateThread (TThreadEntrypoint entry)\n" + - "{\n" + - "\treturn (void*)(deUintptr)deThread_create(EnterGenericThread, (void*)entry, DE_NULL);\n" + - "}\n" + - "\n" + - "void OS_WaitForAllThreads (void* threads, int numThreads)\n" + - "{\n" + - "\tfor (int ndx = 0; ndx < numThreads; ndx++)\n" + - "\t{\n" + - "\t\tconst deThread thread = (deThread)(deUintptr)((void**)threads)[ndx];\n" + - "\t\tdeThread_join(thread);\n" + - "\t\tdeThread_destroy(thread);\n" + - "\t}\n" + - "}\n" + - "\n" + - "void OS_Sleep (int milliseconds)\n" + - "{\n" + - "\tdeSleep(milliseconds);\n" + - "}\n" + - "\n" + - "void OS_DumpMemoryCounters (void)\n" + - "{\n" + - "\t// Not used\n" + - "}\n" + - "\n" + - "} // glslang\n"; + String testEnum = testCStreamFile2; CodePointCharStream cStream = CharStreams.fromString(testEnum); ParseObj po = parser.parseCStream(cStream); List fol = po.getFuncList(); -- Gitee From 7cecd92a3cad5f909d40bf0ccaf0e0983193026c Mon Sep 17 00:00:00 2001 From: sunlian Date: Wed, 2 Apr 2025 15:39:25 +0800 Subject: [PATCH 09/10] fix code check Signed-off-by: sunlian --- .../java/antlr/cpp/CPP14CustomListener.java | 91 +++++++++++-------- .../ohosgen/src/main/java/gen/GenCppFile.java | 24 +++-- .../ohosgen/src/main/java/gen/GenDtsFile.java | 20 ++-- .../src/main/java/gen/GeneratorBase.java | 55 ++++++++++- .../src/test/java/parse/ParseCppTest.java | 21 ++++- 5 files changed, 156 insertions(+), 55 deletions(-) diff --git a/src/intellij_plugin/ohosgen/src/main/java/antlr/cpp/CPP14CustomListener.java b/src/intellij_plugin/ohosgen/src/main/java/antlr/cpp/CPP14CustomListener.java index a4b5f78c..c7410f1c 100644 --- a/src/intellij_plugin/ohosgen/src/main/java/antlr/cpp/CPP14CustomListener.java +++ b/src/intellij_plugin/ohosgen/src/main/java/antlr/cpp/CPP14CustomListener.java @@ -48,6 +48,7 @@ public class CPP14CustomListener extends CPP14ParserBaseListener implements Pars private List unionObjList; private List interfaceObjList; private List constList; + /** * 构造函数 */ @@ -301,26 +302,7 @@ public class CPP14CustomListener extends CPP14ParserBaseListener implements Pars System.out.println("c/cpp attribute: " + ctx.getText()); } - private void setStructMember(CPP14Parser.MemberdeclarationContext ctx, StructObj so) { - String type = ctx.declSpecifierSeq().getText(); - List dscl = ctx.declSpecifierSeq().declSpecifier(); - type = !dscl.isEmpty() ? "" : type; - for (CPP14Parser.DeclSpecifierContext dscItem : dscl) { - if (dscItem.typeSpecifier() != null && - dscItem.typeSpecifier().trailingTypeSpecifier().elaboratedTypeSpecifier() != null) { - CPP14Parser.ElaboratedTypeSpecifierContext esc = - dscItem.typeSpecifier().trailingTypeSpecifier().elaboratedTypeSpecifier(); - type += esc.Enum() != null ? esc.Enum().getText() + " " : ""; - type += esc.classKey() != null ? esc.classKey().getText() + " " : ""; - type += esc.Identifier(); - } else { - type += dscItem.getText() + " "; - } - } - if (!dscl.isEmpty()) { - type = StringUtils.removeLastSpace(type); - } - String name = ctx.memberDeclaratorList().getText(); + private void setStructFunc(CPP14Parser.MemberdeclarationContext ctx, StructObj so, String type) { List mdcl = ctx.memberDeclaratorList().memberDeclarator(); for (CPP14Parser.MemberDeclaratorContext mdc : mdcl) { if (mdc.declarator() != null && mdc.declarator().pointerDeclarator() != null && @@ -360,17 +342,31 @@ public class CPP14CustomListener extends CPP14ParserBaseListener implements Pars } } - private void setUnionMember(CPP14Parser.MemberdeclarationContext ctx, UnionObj uo) { + private void setStructMember(CPP14Parser.MemberdeclarationContext ctx, StructObj so) { String type = ctx.declSpecifierSeq().getText(); List dscl = ctx.declSpecifierSeq().declSpecifier(); type = !dscl.isEmpty() ? "" : type; for (CPP14Parser.DeclSpecifierContext dscItem : dscl) { - type += dscItem.getText() + " "; + if (dscItem.typeSpecifier() != null && + dscItem.typeSpecifier().trailingTypeSpecifier().elaboratedTypeSpecifier() != null) { + CPP14Parser.ElaboratedTypeSpecifierContext esc = + dscItem.typeSpecifier().trailingTypeSpecifier().elaboratedTypeSpecifier(); + type += esc.Enum() != null ? esc.Enum().getText() + " " : ""; + type += esc.classKey() != null ? esc.classKey().getText() + " " : ""; + type += esc.Identifier(); + } else { + type += dscItem.getText() + " "; + } } if (!dscl.isEmpty()) { type = StringUtils.removeLastSpace(type); } - String name = ctx.memberDeclaratorList().getText(); + setStructFunc(ctx, so, type); + + } + + private void setUnionFunc(CPP14Parser.MemberdeclarationContext ctx, UnionObj uo, String type) { + List mdcl = ctx.memberDeclaratorList().memberDeclarator(); for (CPP14Parser.MemberDeclaratorContext mdc : mdcl) { if (mdc.declarator() != null && mdc.declarator().pointerDeclarator() != null && @@ -410,26 +406,20 @@ public class CPP14CustomListener extends CPP14ParserBaseListener implements Pars } } - private void setClassMember(CPP14Parser.MemberdeclarationContext ctx, ClassObj co) { + private void setUnionMember(CPP14Parser.MemberdeclarationContext ctx, UnionObj uo) { String type = ctx.declSpecifierSeq().getText(); List dscl = ctx.declSpecifierSeq().declSpecifier(); type = !dscl.isEmpty() ? "" : type; for (CPP14Parser.DeclSpecifierContext dscItem : dscl) { - if (dscItem.typeSpecifier() != null && - dscItem.typeSpecifier().trailingTypeSpecifier().elaboratedTypeSpecifier() != null) { - CPP14Parser.ElaboratedTypeSpecifierContext esc = - dscItem.typeSpecifier().trailingTypeSpecifier().elaboratedTypeSpecifier(); - type += esc.Enum() != null ? esc.Enum().getText() + " " : ""; - type += esc.classKey() != null ? esc.classKey().getText() + " " : ""; - type += esc.Identifier(); - } else { - type += dscItem.getText() + " "; - } + type += dscItem.getText() + " "; } if (!dscl.isEmpty()) { type = StringUtils.removeLastSpace(type); } - String name = ctx.memberDeclaratorList().getText(); + setUnionFunc(ctx, uo, type); + } + + private void setClassFunc(CPP14Parser.MemberdeclarationContext ctx, ClassObj co, String type) { List mdcl = ctx.memberDeclaratorList().memberDeclarator(); for (CPP14Parser.MemberDeclaratorContext mdc : mdcl) { if (mdc.declarator() != null && mdc.declarator().pointerDeclarator() != null && @@ -443,8 +433,9 @@ public class CPP14CustomListener extends CPP14ParserBaseListener implements Pars fo.setRetValue(type); if (npdcItem.parametersAndQualifiers() != null && - npdcItem.parametersAndQualifiers().parameterDeclarationClause() != null && - npdcItem.parametersAndQualifiers().parameterDeclarationClause().parameterDeclarationList() != null) { + npdcItem.parametersAndQualifiers().parameterDeclarationClause() != null && + npdcItem.parametersAndQualifiers().parameterDeclarationClause(). + parameterDeclarationList() != null) { List pdcList = npdcItem.parametersAndQualifiers().parameterDeclarationClause(). parameterDeclarationList().parameterDeclaration(); @@ -469,6 +460,29 @@ public class CPP14CustomListener extends CPP14ParserBaseListener implements Pars } } + private void setClassMember(CPP14Parser.MemberdeclarationContext ctx, ClassObj co) { + String type = ctx.declSpecifierSeq().getText(); + List dscl = ctx.declSpecifierSeq().declSpecifier(); + type = !dscl.isEmpty() ? "" : type; + for (CPP14Parser.DeclSpecifierContext dscItem : dscl) { + if (dscItem.typeSpecifier() != null && + dscItem.typeSpecifier().trailingTypeSpecifier().elaboratedTypeSpecifier() != null) { + CPP14Parser.ElaboratedTypeSpecifierContext esc = + dscItem.typeSpecifier().trailingTypeSpecifier().elaboratedTypeSpecifier(); + type += esc.Enum() != null ? esc.Enum().getText() + " " : ""; + type += esc.classKey() != null ? esc.classKey().getText() + " " : ""; + type += esc.Identifier(); + } else { + type += dscItem.getText() + " "; + } + } + if (!dscl.isEmpty()) { + type = StringUtils.removeLastSpace(type); + } + setClassFunc(ctx, co, type); + + } + @Override public void enterMemberdeclaration(CPP14Parser.MemberdeclarationContext ctx) { super.enterMemberdeclaration(ctx); @@ -549,7 +563,8 @@ public class CPP14CustomListener extends CPP14ParserBaseListener implements Pars } paType = StringUtils.removeLastSpace(paType); String paName = ctx.initDeclaratorList().initDeclarator(0).declarator().getText(); - String initValue = ctx.initDeclaratorList().initDeclarator(0).initializer().braceOrEqualInitializer().initializerClause().getText(); + String initValue = ctx.initDeclaratorList().initDeclarator(0).initializer(). + braceOrEqualInitializer().initializerClause().getText(); ParamObj po = new ParamObj(); po.setName(paName); 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 818fbfc2..967ed190 100644 --- a/src/intellij_plugin/ohosgen/src/main/java/gen/GenCppFile.java +++ b/src/intellij_plugin/ohosgen/src/main/java/gen/GenCppFile.java @@ -38,7 +38,8 @@ public class GenCppFile extends GeneratorBase { /** * 生成输出内容 - * @param po + * + * @param po 解析类 */ @Override public void genContent(ParseObj po) { @@ -53,7 +54,8 @@ public class GenCppFile extends GeneratorBase { /** * 生成输出内容 - * @param iol + * + * @param iol 接口列表 */ @Override public void genInterfaceList(List iol) { @@ -62,7 +64,8 @@ public class GenCppFile extends GeneratorBase { /** * 生成输出内容 - * @param eol + * + * @param eol 枚举列表 */ @Override public void genEnumList(List eol) { @@ -71,7 +74,8 @@ public class GenCppFile extends GeneratorBase { /** * 生成输出内容 - * @param col + * + * @param col 类列表 */ @Override public void genClassList(List col) { @@ -80,7 +84,8 @@ public class GenCppFile extends GeneratorBase { /** * 生成输出内容 - * @param fol + * + * @param fol 方法列表 */ @Override public void genFuncList(List fol) { @@ -89,7 +94,8 @@ public class GenCppFile extends GeneratorBase { /** * 生成输出内容 - * @param sol + * + * @param sol 结构体列表 */ @Override public void genStructList(List sol) { @@ -98,7 +104,8 @@ public class GenCppFile extends GeneratorBase { /** * 生成输出内容 - * @param tol + * + * @param tol 类型列表 */ @Override public void genTypeList(List tol) { @@ -107,7 +114,8 @@ public class GenCppFile extends GeneratorBase { /** * 生成输出内容 - * @param uol + * + * @param uol 联合体列表 */ @Override public void genUnionList(List uol) { diff --git a/src/intellij_plugin/ohosgen/src/main/java/gen/GenDtsFile.java b/src/intellij_plugin/ohosgen/src/main/java/gen/GenDtsFile.java index e93ccdcb..b0e8528b 100644 --- a/src/intellij_plugin/ohosgen/src/main/java/gen/GenDtsFile.java +++ b/src/intellij_plugin/ohosgen/src/main/java/gen/GenDtsFile.java @@ -256,6 +256,7 @@ public class GenDtsFile extends GeneratorBase { /** * 生成输出内容 + * * @param po 解析对象 */ @Override @@ -272,6 +273,9 @@ public class GenDtsFile extends GeneratorBase { /** * 生成文件 + * + * @param fileName 文件名 + * @param filePath 文件路径 */ @Override public void genFile(String filePath, String fileName) { @@ -294,6 +298,7 @@ public class GenDtsFile extends GeneratorBase { /** * 生成输出内容 + * * @param iol 接口对象列表 */ @Override @@ -303,6 +308,7 @@ public class GenDtsFile extends GeneratorBase { /** * 生成输出内容 + * * @param eol 枚举对象列表 */ @Override @@ -335,6 +341,7 @@ public class GenDtsFile extends GeneratorBase { /** * 生成输出内容 + * * @param col 类对象列表 */ @Override @@ -393,6 +400,7 @@ public class GenDtsFile extends GeneratorBase { /** * 生成输出内容 + * * @param fol 方法对象列表 */ @Override @@ -429,6 +437,7 @@ public class GenDtsFile extends GeneratorBase { /** * 生成输出内容 + * * @param sol 结构体对象列表 */ @Override @@ -439,9 +448,8 @@ public class GenDtsFile extends GeneratorBase { for (StructObj so : sol) { String structName = so.getName(); structName = !structName.isEmpty() ? structName : so.getAlias(); - List funcList = so.getFuncList(); + List paList = so.getMemberList(); - int i = 0; resContent += TS_NEW_LINE + TS_EXPORT_TOKEN + TS_BLANK_SPACE + TS_CLASS_TOKEN + TS_BLANK_SPACE + structName + TS_BLANK_SPACE + TS_LEFT_BRACE; @@ -456,10 +464,9 @@ public class GenDtsFile extends GeneratorBase { } else { resContent += TS_SEMICOLON; } - i++; } - i = 0; + List funcList = so.getFuncList(); for (FuncObj funcItem : funcList) { resContent += TS_NEW_LINE + TS_TAB_SPACE + replaceCppToken(funcItem.getName()) + TS_LEFT_PARENTHESES; List pol = funcItem.getParamList(); @@ -475,8 +482,6 @@ public class GenDtsFile extends GeneratorBase { String retValue = funcItem.getRetValue(); resContent += TS_RIGHT_PARENTHESES + TS_BLANK_SPACE + TS_COLON + TS_BLANK_SPACE + cpp2TsKey(retValue) + TS_SEMICOLON; - - i++; } resContent = StringUtils.removeLastSpace(resContent); @@ -487,6 +492,7 @@ public class GenDtsFile extends GeneratorBase { /** * 生成输出内容 + * * @param tol 类型对象列表 */ @Override @@ -496,6 +502,7 @@ public class GenDtsFile extends GeneratorBase { /** * 生成输出内容 + * * @param uol 联合体对象列表 */ @Override @@ -526,6 +533,7 @@ public class GenDtsFile extends GeneratorBase { /** * 生成输出内容 + * * @param pol 常量列表 */ @Override diff --git a/src/intellij_plugin/ohosgen/src/main/java/gen/GeneratorBase.java b/src/intellij_plugin/ohosgen/src/main/java/gen/GeneratorBase.java index 434bf8fb..4c1bd1d5 100644 --- a/src/intellij_plugin/ohosgen/src/main/java/gen/GeneratorBase.java +++ b/src/intellij_plugin/ohosgen/src/main/java/gen/GeneratorBase.java @@ -29,42 +29,93 @@ import java.util.List; * @since 2025-02-28 */ public class GeneratorBase { + /** + * 生成内容 + * + * @param po 解析类 + */ public void genContent(ParseObj po) { System.out.println("GeneratorBase: genContent"); } + /** + * 生成文件 + * + * @param filePath 文件路径 + * @param fileName 文件名 + */ public void genFile(String filePath, String fileName) { System.out.println("GeneratorBase: path is " + filePath + ", file is " + fileName); } + /** + * 生成接口 + * + * @param iol 接口列表 + */ public void genInterfaceList(List iol) { System.out.println("GeneratorBase: genInterfaceList"); }; + /** + * 生成枚举 + * + * @param eol 枚举列表 + */ public void genEnumList(List eol) { System.out.println("GeneratorBase: genEnumList"); - }; + /** + * 生成类 + * + * @param col 类列表 + */ public void genClassList(List col) { System.out.println("GeneratorBase: genClassList"); }; + /** + * 生成方法 + * + * @param fol 方法列表 + */ public void genFuncList(List fol) { System.out.println("GeneratorBase: genFuncList"); }; + /** + * 生成结构体 + * + * @param sol 结构体列表 + */ public void genStructList(List sol) { System.out.println("GeneratorBase: genStructList"); }; + /** + * 生成类型 + * + * @param tol 类型列表 + */ public void genTypeList(List tol) { - System.out.println("GeneratorBase: genTypeList"); }; + System.out.println("GeneratorBase: genTypeList"); + }; + /** + * 生成联合体 + * + * @param uol 联合体列表 + */ public void genUnionList(List uol) { System.out.println("GeneratorBase: genUnionList"); }; + /** + * 生成变量 + * + * @param pol 变量列表 + */ public void genVarList(List pol) { System.out.println("GeneratorBase: genVarList"); } diff --git a/src/intellij_plugin/ohosgen/src/test/java/parse/ParseCppTest.java b/src/intellij_plugin/ohosgen/src/test/java/parse/ParseCppTest.java index f2399a48..a6c22bdc 100644 --- a/src/intellij_plugin/ohosgen/src/test/java/parse/ParseCppTest.java +++ b/src/intellij_plugin/ohosgen/src/test/java/parse/ParseCppTest.java @@ -94,7 +94,7 @@ class ParseCppTest { " uint16_t port;\n" + "};"; - private final String testCStreamFile2 = "/*-------------------------------------------------------------------------\n" + + private final String testCStreamFile2 = "/*---------------------------------------\n" + " * dEQP glslang integration\n" + " * ------------------------\n" + " *\n" + @@ -1272,6 +1272,25 @@ class ParseCppTest { assertEquals(1, pl.size()); assertEquals("*entry", pl.get(0).getName()); assertEquals("void", pl.get(0).getType()); + } + + @Test + void parseCStreamFile3() { + ParseBase parser = ParseFactory.getParser("h2dts"); + String testEnum = testCStreamFile2; + CodePointCharStream cStream = CharStreams.fromString(testEnum); + ParseObj po = parser.parseCStream(cStream); + List fol = po.getFuncList(); + assertEquals(12, fol.size()); + + FuncObj fo = fol.get(0); + assertEquals("OS_AllocTLSIndex", fo.getName()); + assertEquals("OS_TLSIndex", fo.getRetValue()); + + List pl = fo.getParamList(); + assertEquals(1, pl.size()); + assertEquals("", pl.get(0).getName()); + assertEquals("void", pl.get(0).getType()); fo = fol.get(8); assertEquals("OS_CreateThread", fo.getName()); -- Gitee From 6e3b5203aa38e654faff10f66f2c908ba130a7f3 Mon Sep 17 00:00:00 2001 From: sunlian Date: Wed, 2 Apr 2025 16:06:14 +0800 Subject: [PATCH 10/10] fix code check Signed-off-by: sunlian --- .../java/antlr/cpp/CPP14CustomListener.java | 88 +++++++++---------- .../ohosgen/src/main/java/gen/GenDtsFile.java | 16 ++-- .../src/main/java/grammar/FuncObj.java | 2 +- .../src/main/java/grammar/StructObj.java | 5 ++ .../src/main/java/parse/ParseTask.java | 5 ++ .../src/main/java/utils/StringUtils.java | 5 +- .../src/test/java/gen/GenDtsFileTest.java | 15 ++++ .../src/test/java/parse/ParseCppTest.java | 31 +------ 8 files changed, 82 insertions(+), 85 deletions(-) diff --git a/src/intellij_plugin/ohosgen/src/main/java/antlr/cpp/CPP14CustomListener.java b/src/intellij_plugin/ohosgen/src/main/java/antlr/cpp/CPP14CustomListener.java index c7410f1c..ab88d4c0 100644 --- a/src/intellij_plugin/ohosgen/src/main/java/antlr/cpp/CPP14CustomListener.java +++ b/src/intellij_plugin/ohosgen/src/main/java/antlr/cpp/CPP14CustomListener.java @@ -314,22 +314,22 @@ public class CPP14CustomListener extends CPP14ParserBaseListener implements Pars FuncObj fo = new FuncObj(); fo.setName(npdcItem.noPointerDeclarator().getText()); fo.setRetValue(type); - - if (npdcItem.parametersAndQualifiers() != null && - npdcItem.parametersAndQualifiers().parameterDeclarationClause() != null && - npdcItem.parametersAndQualifiers().parameterDeclarationClause().parameterDeclarationList() != null) { - List pdcList = - npdcItem.parametersAndQualifiers().parameterDeclarationClause(). - parameterDeclarationList().parameterDeclaration(); - for (CPP14Parser.ParameterDeclarationContext pdcItem : pdcList) { - ParamObj poItem = new ParamObj(); - poItem.setName(pdcItem.declarator().getText()); - poItem.setType(pdcItem.declSpecifierSeq().getText()); - fo.addParam(poItem); - } - } - so.addFunc(fo); + if (!(npdcItem.parametersAndQualifiers() != null && + npdcItem.parametersAndQualifiers().parameterDeclarationClause() != null && + npdcItem.parametersAndQualifiers().parameterDeclarationClause(). + parameterDeclarationList() != null)) { + continue; + } + List pdcList = + npdcItem.parametersAndQualifiers().parameterDeclarationClause(). + parameterDeclarationList().parameterDeclaration(); + for (CPP14Parser.ParameterDeclarationContext pdcItem : pdcList) { + ParamObj poItem = new ParamObj(); + poItem.setName(pdcItem.declarator().getText()); + poItem.setType(pdcItem.declSpecifierSeq().getText()); + fo.addParam(poItem); + } } else { ParamObj po = new ParamObj(); po.setName(mdc.declarator().getText()); @@ -378,22 +378,23 @@ public class CPP14CustomListener extends CPP14ParserBaseListener implements Pars FuncObj fo = new FuncObj(); fo.setName(npdcItem.noPointerDeclarator().getText()); fo.setRetValue(type); + uo.addFunc(fo); - if (npdcItem.parametersAndQualifiers() != null && - npdcItem.parametersAndQualifiers().parameterDeclarationClause() != null && - npdcItem.parametersAndQualifiers().parameterDeclarationClause().parameterDeclarationList() != null) { - List pdcList = - npdcItem.parametersAndQualifiers().parameterDeclarationClause(). - parameterDeclarationList().parameterDeclaration(); - for (CPP14Parser.ParameterDeclarationContext pdcItem : pdcList) { - ParamObj poItem = new ParamObj(); - poItem.setName(pdcItem.declarator().getText()); - poItem.setType(pdcItem.declSpecifierSeq().getText()); - fo.addParam(poItem); - } + if (!(npdcItem.parametersAndQualifiers() != null && + npdcItem.parametersAndQualifiers().parameterDeclarationClause() != null && + npdcItem.parametersAndQualifiers().parameterDeclarationClause(). + parameterDeclarationList() != null)) { + continue; + } + List pdcList = + npdcItem.parametersAndQualifiers().parameterDeclarationClause(). + parameterDeclarationList().parameterDeclaration(); + for (CPP14Parser.ParameterDeclarationContext pdcItem : pdcList) { + ParamObj poItem = new ParamObj(); + poItem.setName(pdcItem.declarator().getText()); + poItem.setType(pdcItem.declSpecifierSeq().getText()); + fo.addParam(poItem); } - - uo.addFunc(fo); } else { ParamObj po = new ParamObj(); po.setName(mdc.declarator().getText()); @@ -431,23 +432,22 @@ public class CPP14CustomListener extends CPP14ParserBaseListener implements Pars FuncObj fo = new FuncObj(); fo.setName(npdcItem.noPointerDeclarator().getText()); fo.setRetValue(type); - - if (npdcItem.parametersAndQualifiers() != null && + co.addFunc(fo); + if (!(npdcItem.parametersAndQualifiers() != null && npdcItem.parametersAndQualifiers().parameterDeclarationClause() != null && npdcItem.parametersAndQualifiers().parameterDeclarationClause(). - parameterDeclarationList() != null) { - List pdcList = - npdcItem.parametersAndQualifiers().parameterDeclarationClause(). - parameterDeclarationList().parameterDeclaration(); - for (CPP14Parser.ParameterDeclarationContext pdcItem : pdcList) { - ParamObj poItem = new ParamObj(); - poItem.setName(pdcItem.declarator().getText()); - poItem.setType(pdcItem.declSpecifierSeq().getText()); - fo.addParam(poItem); - } + parameterDeclarationList() != null)) { + continue; + } + List pdcList = + npdcItem.parametersAndQualifiers().parameterDeclarationClause(). + parameterDeclarationList().parameterDeclaration(); + for (CPP14Parser.ParameterDeclarationContext pdcItem : pdcList) { + ParamObj poItem = new ParamObj(); + poItem.setName(pdcItem.declarator().getText()); + poItem.setType(pdcItem.declSpecifierSeq().getText()); + fo.addParam(poItem); } - - co.addFunc(fo); } else { ParamObj po = new ParamObj(); po.setName(mdc.declarator().getText()); @@ -558,7 +558,7 @@ public class CPP14CustomListener extends CPP14ParserBaseListener implements Pars ctx.declSpecifierSeq() != null) { String paType = ""; List dscl = ctx.declSpecifierSeq().declSpecifier(); - for (CPP14Parser.DeclSpecifierContext item: dscl) { + for (CPP14Parser.DeclSpecifierContext item : dscl) { paType += item.getText() + " "; } paType = StringUtils.removeLastSpace(paType); diff --git a/src/intellij_plugin/ohosgen/src/main/java/gen/GenDtsFile.java b/src/intellij_plugin/ohosgen/src/main/java/gen/GenDtsFile.java index b0e8528b..0c507dff 100644 --- a/src/intellij_plugin/ohosgen/src/main/java/gen/GenDtsFile.java +++ b/src/intellij_plugin/ohosgen/src/main/java/gen/GenDtsFile.java @@ -157,9 +157,9 @@ public class GenDtsFile extends GeneratorBase { } /** - * 替换cpp token + * 替换cpptoken * - * @param cppKey cpp token + * @param cppKey 语言关键字 * @return 替换后字符串 */ private String replaceCppToken(String cppKey) { @@ -352,9 +352,8 @@ public class GenDtsFile extends GeneratorBase { for (ClassObj co : col) { String className = co.getName(); className = !className.isEmpty() ? className : co.getAlias(); - List funcList = co.getFuncList(); + List paList = co.getParamList(); - int i = 0; resContent += TS_NEW_LINE + TS_EXPORT_TOKEN + TS_BLANK_SPACE + TS_CLASS_TOKEN + TS_BLANK_SPACE + className + TS_BLANK_SPACE + TS_LEFT_BRACE; @@ -369,17 +368,16 @@ public class GenDtsFile extends GeneratorBase { } else { resContent += TS_SEMICOLON; } - i++; } - i = 0; + List funcList = co.getFuncList(); for (FuncObj funcItem : funcList) { resContent += TS_NEW_LINE + TS_TAB_SPACE + replaceCppToken(funcItem.getName()) + TS_LEFT_PARENTHESES; List pol = funcItem.getParamList(); for (ParamObj poItem : pol) { String retType = cpp2TsKey(poItem.getType()); resContent += replaceCppToken(poItem.getName()) + TS_COLON + - TS_BLANK_SPACE + retType + TS_COMMA + TS_BLANK_SPACE; + TS_BLANK_SPACE + retType + TS_COMMA + TS_BLANK_SPACE; } if (pol.size() > 0) { resContent = StringUtils.removeLastCharacter(resContent, 2); @@ -387,9 +385,7 @@ public class GenDtsFile extends GeneratorBase { String retValue = funcItem.getRetValue(); resContent += TS_RIGHT_PARENTHESES + TS_BLANK_SPACE + TS_COLON + - TS_BLANK_SPACE + cpp2TsKey(retValue) + TS_SEMICOLON; - - i++; + TS_BLANK_SPACE + cpp2TsKey(retValue) + TS_SEMICOLON; } resContent = StringUtils.removeLastSpace(resContent); diff --git a/src/intellij_plugin/ohosgen/src/main/java/grammar/FuncObj.java b/src/intellij_plugin/ohosgen/src/main/java/grammar/FuncObj.java index 4a23ad8d..1f9579b2 100644 --- a/src/intellij_plugin/ohosgen/src/main/java/grammar/FuncObj.java +++ b/src/intellij_plugin/ohosgen/src/main/java/grammar/FuncObj.java @@ -308,7 +308,7 @@ public class FuncObj extends GBaseObject { /** * 添加声明类型 * - * @param decName + * @param decName 声明 */ public void addDecl(String decName) { this.decList.add(decName); diff --git a/src/intellij_plugin/ohosgen/src/main/java/grammar/StructObj.java b/src/intellij_plugin/ohosgen/src/main/java/grammar/StructObj.java index 5f8dd087..bd076475 100644 --- a/src/intellij_plugin/ohosgen/src/main/java/grammar/StructObj.java +++ b/src/intellij_plugin/ohosgen/src/main/java/grammar/StructObj.java @@ -138,6 +138,11 @@ public class StructObj extends GBaseObject { this.memberList.add(po); } + /** + * 增加方法 + * + * @param fo 方法 + */ public void addFunc(FuncObj fo) { this.funcList.add(fo); } diff --git a/src/intellij_plugin/ohosgen/src/main/java/parse/ParseTask.java b/src/intellij_plugin/ohosgen/src/main/java/parse/ParseTask.java index d440ec23..78ff9eb4 100644 --- a/src/intellij_plugin/ohosgen/src/main/java/parse/ParseTask.java +++ b/src/intellij_plugin/ohosgen/src/main/java/parse/ParseTask.java @@ -79,6 +79,11 @@ public class ParseTask extends Task.Backgroundable implements BaseListener { return this.parseFile.getName(); }; + /** + * 获得文件路径 + * + * @return 文件路径 + */ public String getFilePath() { return this.parseFile.getParent().getPath(); } 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 94b2c212..81cdb08e 100644 --- a/src/intellij_plugin/ohosgen/src/main/java/utils/StringUtils.java +++ b/src/intellij_plugin/ohosgen/src/main/java/utils/StringUtils.java @@ -46,7 +46,7 @@ public class StringUtils { } /** - * removeLastSpace + * 删除最后的一个空格 * * @param str 删除字符串最后一个空格 * @return 返回字符串 @@ -59,9 +59,10 @@ public class StringUtils { } /** - * removeLastSpace + * 删除最后的几个字符 * * @param str 删除字符串最后一个空格 + * @param n 需要删除字符的个数 * @return 返回字符串 */ public static String removeLastCharacter(String str, int n) { diff --git a/src/intellij_plugin/ohosgen/src/test/java/gen/GenDtsFileTest.java b/src/intellij_plugin/ohosgen/src/test/java/gen/GenDtsFileTest.java index f11e442c..e9c7f49d 100644 --- a/src/intellij_plugin/ohosgen/src/test/java/gen/GenDtsFileTest.java +++ b/src/intellij_plugin/ohosgen/src/test/java/gen/GenDtsFileTest.java @@ -1,3 +1,18 @@ +/* + * Copyright (c) 2025 Shenzhen Kaihong Digital. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + package gen; import grammar.EnumObj; diff --git a/src/intellij_plugin/ohosgen/src/test/java/parse/ParseCppTest.java b/src/intellij_plugin/ohosgen/src/test/java/parse/ParseCppTest.java index a6c22bdc..44e61758 100644 --- a/src/intellij_plugin/ohosgen/src/test/java/parse/ParseCppTest.java +++ b/src/intellij_plugin/ohosgen/src/test/java/parse/ParseCppTest.java @@ -1246,32 +1246,7 @@ class ParseCppTest { assertEquals("", pl.get(0).getName()); assertEquals("void", pl.get(0).getType()); - fo = fol.get(5); - assertEquals("GetGlobalLock", fo.getName()); - assertEquals("void", fo.getRetValue()); - - pl = fo.getParamList(); - assertEquals(1, pl.size()); - assertEquals("", pl.get(0).getName()); - assertEquals("void", pl.get(0).getType()); - - fo = fol.get(6); - assertEquals("ReleaseGlobalLock", fo.getName()); - assertEquals("void", fo.getRetValue()); - pl = fo.getParamList(); - assertEquals(1, pl.size()); - assertEquals("", pl.get(0).getName()); - assertEquals("void", pl.get(0).getType()); - - fo = fol.get(7); - assertEquals("EnterGenericThread", fo.getName()); - assertEquals("static void", fo.getRetValue()); - - pl = fo.getParamList(); - assertEquals(1, pl.size()); - assertEquals("*entry", pl.get(0).getName()); - assertEquals("void", pl.get(0).getType()); } @Test @@ -1283,9 +1258,9 @@ class ParseCppTest { List fol = po.getFuncList(); assertEquals(12, fol.size()); - FuncObj fo = fol.get(0); - assertEquals("OS_AllocTLSIndex", fo.getName()); - assertEquals("OS_TLSIndex", fo.getRetValue()); + FuncObj fo = fol.get(5); + assertEquals("GetGlobalLock", fo.getName()); + assertEquals("void", fo.getRetValue()); List pl = fo.getParamList(); assertEquals(1, pl.size()); -- Gitee