diff --git a/src/intellij_plugin/ohosgen/src/main/java/antlr/typescript/TypeScriptCustomListener.java b/src/intellij_plugin/ohosgen/src/main/java/antlr/typescript/TypeScriptCustomListener.java index 2c4ee043778dba2b965264bd77daced3f47cea20..75b6e2a1805090833ba25c5004dfb288b82a286d 100644 --- a/src/intellij_plugin/ohosgen/src/main/java/antlr/typescript/TypeScriptCustomListener.java +++ b/src/intellij_plugin/ohosgen/src/main/java/antlr/typescript/TypeScriptCustomListener.java @@ -15,9 +15,14 @@ package antlr.typescript; +import grammar.*; import org.antlr.v4.runtime.tree.ParseTree; +import org.antlr.v4.runtime.ParserRuleContext; +import utils.Constants; +import utils.TsToken; import java.util.List; +import java.util.concurrent.CopyOnWriteArrayList; /** *

类名:该类用于xxx

@@ -29,6 +34,27 @@ import java.util.List; * @since 2025-02-28 */ public class TypeScriptCustomListener extends TypeScriptParserBaseListener { + private final int currentLanguage = Constants.PARSE_TS_LANGUAGE; + private String currentToken = ""; + private GBaseObject currentObject; + private List enumObjList; + private List classObjList; + private List funcObjList; + private List structObjList; + private List typeObjList; + private List unionObjList; + private List interfaceObjList; + + public TypeScriptCustomListener() { + enumObjList = new CopyOnWriteArrayList<>(); + classObjList = new CopyOnWriteArrayList<>(); + funcObjList = new CopyOnWriteArrayList<>(); + structObjList = new CopyOnWriteArrayList<>(); + typeObjList = new CopyOnWriteArrayList<>(); + unionObjList = new CopyOnWriteArrayList<>(); + interfaceObjList = new CopyOnWriteArrayList<>(); + } + @Override public void enterVariableDeclaration(TypeScriptParser.VariableDeclarationContext ctx) { String varName = ctx.identifierOrKeyWord().getText(); @@ -36,21 +62,88 @@ public class TypeScriptCustomListener extends TypeScriptParserBaseListener { System.out.println("var : " + ctx.getText()); String typeAnno = ctx.typeAnnotation() != null ? ctx.typeAnnotation().getText() : ""; System.out.println("type : " + typeAnno); - List secList = ctx.singleExpression(); - for (TypeScriptParser.SingleExpressionContext sec : secList) { - String value = sec.getText(); - System.out.println("single : " + value); - int cnt = sec.getChildCount(); - System.out.println("single child cnt: " + cnt); - for (int i = 0; i < cnt; i++) { - ParseTree pt = sec.getChild(i); - System.out.println("single child pt: " + pt.getText()); + if (varName.equals(TsToken.TS_TOKEN_TYPE)) { + TypeObj to = new TypeObj(); + List secList = ctx.singleExpression(); + for (TypeScriptParser.SingleExpressionContext sec : secList) { + String value = sec.getText(); + System.out.println("single : " + value); + int cnt = sec.getChildCount(); + System.out.println("single child cnt: " + cnt); + if (cnt == 3) { + ParseTree pt = sec.getChild(0); + to.setName(pt.getText()); + + ParseTree pt2 = sec.getChild(2); + to.addTypeValue(pt2.getText()); + } + for (int i = 0; i < cnt; i++) { + ParseTree pt = sec.getChild(i); + System.out.println("single child pt: " + pt.getText()); + } } + this.typeObjList.add(to); + System.out.println("type: " + to.toJsonString()); + } + System.out.println("------------------------------"); } + @Override + public void enterExpressionStatement(TypeScriptParser.ExpressionStatementContext ctx) { + super.enterExpressionStatement(ctx); + + } + + @Override + public void enterIdentifierOrPattern(TypeScriptParser.IdentifierOrPatternContext ctx) { + super.enterIdentifierOrPattern(ctx); + } + + @Override + public void enterObjectLiteralExpression(TypeScriptParser.ObjectLiteralExpressionContext ctx) { + super.enterObjectLiteralExpression(ctx); + ParserRuleContext prc = ctx.getParent(); + ParserRuleContext fprc = prc.getParent(); + String tokenStr = fprc.getStart().getText(); + if (tokenStr.equals(TsToken.TS_TOKEN_ENUM)) { + EnumObj eo = new EnumObj(); + eo.setName(ctx.getParent().getStart().getText()); + this.enumObjList.add(eo); + this.currentToken = TsToken.TS_TOKEN_ENUM; + this.currentObject = eo; + this.enumObjList.add(eo); + } + + } + + @Override + public void enterLiteralExpression(TypeScriptParser.LiteralExpressionContext ctx) { + super.enterLiteralExpression(ctx); + + String memName = ctx.getParent().getStart().getText(); + String memValue = ctx.getParent().getStop().getText(); + if (this.currentToken.equals(TsToken.TS_TOKEN_ENUM)) { + if (this.currentObject instanceof EnumObj) { + EnumObj eo = (EnumObj) this.currentObject; + eo.addMemberItem(memName); + eo.addMemberValue(memValue); + int lastIndex = this.enumObjList.size() - 1; + this.enumObjList.set(lastIndex, eo); + System.out.println("enum: " + eo.toJsonString()); + } + } + + } + + @Override + public void enterExpressionSequence(TypeScriptParser.ExpressionSequenceContext ctx) { + super.enterExpressionSequence(ctx); + + } + @Override public void enterConstructorDeclaration(TypeScriptParser.ConstructorDeclarationContext ctx) { // 提取构造函数参数列表 @@ -77,17 +170,22 @@ public class TypeScriptCustomListener extends TypeScriptParserBaseListener { System.out.println("Function callSign: " + callSign); String typeAnno = ctx.callSignature().typeAnnotation().getText(); System.out.println("Function typeAnno: " + typeAnno); + FuncObj fo = new FuncObj(); + fo.setName(funcName); + fo.setRetValue(typeAnno); if (ctx.callSignature().parameterList() != null) { List plc = ctx.callSignature().parameterList().parameter(); for (TypeScriptParser.ParameterContext pc : plc) { System.out.println("Function param: " + pc.getText()); TypeScriptParser. RequiredParameterContext rpc = pc.requiredParameter(); - String ta = rpc.typeAnnotation().getText(); - String iop = rpc.identifierOrPattern().getText(); - System.out.println("Function type: " + ta + " name: " + iop); + String type = rpc.typeAnnotation().getText(); + String name = rpc.identifierOrPattern().getText(); + System.out.println("Function type: " + type + " name: " + name); + fo.addParam(name, type); } } - System.out.println("--------------------"); + System.out.println("--------------------" + fo.toJsonString()); + this.funcObjList.add(fo); } @Override @@ -97,6 +195,11 @@ public class TypeScriptCustomListener extends TypeScriptParserBaseListener { String className = ctx.identifier().getText(); System.out.println("Class: " + className); + ClassObj co = new ClassObj(); + co.setName(className); + this.currentObject = co; + this.currentToken = TsToken.TS_TOKEN_CLASS; + this.classObjList.add(co); // 获取修饰符(如public/abstract) TypeScriptParser.DecoratorListContext dlc = ctx.decoratorList(); @@ -133,14 +236,33 @@ public class TypeScriptCustomListener extends TypeScriptParserBaseListener { System.out.println("Method callSign: " + callSign); String typeAnno = ctx.callSignature().typeAnnotation().getText(); System.out.println("Method typeAnno: " + typeAnno); - List plc = ctx.callSignature().parameterList().parameter(); - for (TypeScriptParser.ParameterContext pc : plc) { - System.out.println("Method param: " + pc.getText()); - TypeScriptParser. RequiredParameterContext rpc = pc.requiredParameter(); - String ta = rpc.typeAnnotation().getText(); - String iop = rpc.identifierOrPattern().getText(); - System.out.println("Method type: " + ta + " name: " + iop); + TypeScriptParser.ParameterListContext plc = ctx.callSignature().parameterList(); + + FuncObj fo = new FuncObj(); + fo.setType(typeAnno); + fo.setName(propertyName); + + if (plc != null) { + List plcList = ctx.callSignature().parameterList().parameter(); + for (TypeScriptParser.ParameterContext pc : plcList) { + System.out.println("Method param: " + pc.getText()); + TypeScriptParser. RequiredParameterContext rpc = pc.requiredParameter(); + String ta = rpc.typeAnnotation().getText(); + String iop = rpc.identifierOrPattern().getText(); + System.out.println("Method type: " + ta + " name: " + iop); + fo.addParam(iop, ta); + } } + + if ((this.currentObject != null) && (this.currentObject instanceof ClassObj)) { + ClassObj co = (ClassObj) this.currentObject; + co.addFunc(fo); + + int lastIndex = this.classObjList.size() - 1; + this.classObjList.set(lastIndex, co); + System.out.println("class: " + co.toJsonString()); + } + int cnt = ctx.getChildCount(); System.out.println("Method param cnt: " + cnt); } @@ -148,10 +270,19 @@ public class TypeScriptCustomListener extends TypeScriptParserBaseListener { @Override public void enterPropertyDeclarationExpression(TypeScriptParser.PropertyDeclarationExpressionContext ctx) { super.enterPropertyDeclarationExpression(ctx); + System.out.println("Property property: " + ctx.getText()); String propertyName = ctx.propertyName().getText(); String typeName = ctx.typeAnnotation().getText(); System.out.println("Property name: " + propertyName + " type: " + typeName); + if ((this.currentObject != null) && (this.currentObject instanceof ClassObj)) { + ClassObj co = (ClassObj) this.currentObject; + co.addParam(propertyName, typeName); + + int lastIndex = this.classObjList.size() - 1; + this.classObjList.set(lastIndex, co); + System.out.println("class: " + co.toJsonString()); + } } @Override @@ -239,11 +370,17 @@ public class TypeScriptCustomListener extends TypeScriptParserBaseListener { TypeScriptParser.TypeBodyContext tbc = otc.typeBody(); TypeScriptParser.TypeMemberListContext tlc = tbc.typeMemberList(); List tmcList = tlc.typeMember(); + InterfaceObject io = new InterfaceObject(); + io.setName(interfaceName); + for (TypeScriptParser.TypeMemberContext tmc : tmcList) { String callSign = tmc.callSignature().getText(); System.out.println("interface callSign: " + callSign); String typeAnno = tmc.callSignature().typeAnnotation().getText(); System.out.println("interface typeAnno: " + typeAnno); + FuncObj fo = new FuncObj(); + fo.setName(""); + fo.setType(typeAnno); List plc = tmc.callSignature().parameterList().parameter(); for (TypeScriptParser.ParameterContext pc : plc) { System.out.println("interface param: " + pc.getText()); @@ -251,9 +388,15 @@ public class TypeScriptCustomListener extends TypeScriptParserBaseListener { String ta = rpc.typeAnnotation().getText(); String iop = rpc.identifierOrPattern().getText(); System.out.println("interface type: " + ta + " name: " + iop); + fo.addParam(iop, ta); } + io.addFunc(fo); } - System.out.println("----------------"); + this.interfaceObjList.add(io); + this.currentObject = io; + this.currentToken = TsToken.TS_TOKEN_INTERFACE; + + System.out.println("----------------" + io.toJsonString()); } @Override diff --git a/src/intellij_plugin/ohosgen/src/main/java/antlr/typescript/TypeScriptParserVisitor.java b/src/intellij_plugin/ohosgen/src/main/java/antlr/typescript/TypeScriptParserVisitor.java index 260c10c5ed166bc2aac484c8ffaa8254bee0ea75..9b1e0d71839a3d86d41af5137de7b466d80dfd82 100644 --- a/src/intellij_plugin/ohosgen/src/main/java/antlr/typescript/TypeScriptParserVisitor.java +++ b/src/intellij_plugin/ohosgen/src/main/java/antlr/typescript/TypeScriptParserVisitor.java @@ -13,7 +13,7 @@ * limitations under the License. */ -package antlr.typescript;// Generated from TypeScriptParser.g4 by ANTLR 4.13.2 +package antlr.typescript; // Generated from TypeScriptParser.g4 by ANTLR 4.13.2 import org.antlr.v4.runtime.tree.ParseTreeVisitor; /** diff --git a/src/intellij_plugin/ohosgen/src/main/java/event/CustomEvent.java b/src/intellij_plugin/ohosgen/src/main/java/event/CustomEvent.java new file mode 100644 index 0000000000000000000000000000000000000000..9d3a9a91fd6986e9277ac69f34b876c9a538563d --- /dev/null +++ b/src/intellij_plugin/ohosgen/src/main/java/event/CustomEvent.java @@ -0,0 +1,63 @@ +/* + * 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 event; + +import java.util.EventObject; + +/** + *

类名:该类用于xxx

+ * description custom event + * + * @author Administrator + * date 2025-02-28 + * @version 1.0 + * @since 2025-02-28 + */ +public class CustomEvent extends EventObject { + private String message; + + /** + * Constructs a prototypical Event. + * + * @param source + * the object on which the Event initially occurred + * @param message 消息 + * @throws IllegalArgumentException + * if source is null + */ + public CustomEvent(Object source, String message) { + super(source); + this.message = message; + } + + /** + * 获取消息 + * + * @return message 消息 + */ + public String getMessage() { + return message; + } + + /** + * 设置消息 + * + * @param message 消息 + */ + public void setMessage(String message) { + this.message = message; + } +} diff --git a/src/intellij_plugin/ohosgen/src/main/java/event/CustomEventListener.java b/src/intellij_plugin/ohosgen/src/main/java/event/CustomEventListener.java new file mode 100644 index 0000000000000000000000000000000000000000..31c97b4ff00b9be2cb98c9e06e3490e36b63a176 --- /dev/null +++ b/src/intellij_plugin/ohosgen/src/main/java/event/CustomEventListener.java @@ -0,0 +1,36 @@ +/* + * 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 event; + +import java.util.EventListener; + +/** + *

类名:该类用于xxx

+ * description event listener + * + * @author Administrator + * date 2025-02-28 + * @version 1.0 + * @since 2025-02-28 + */ +public interface CustomEventListener extends EventListener { + /** + * 处理事件 + * + * @param event 事件 + */ + void handleEvent(CustomEvent event); +} diff --git a/src/intellij_plugin/ohosgen/src/main/java/event/EventEmitter.java b/src/intellij_plugin/ohosgen/src/main/java/event/EventEmitter.java new file mode 100644 index 0000000000000000000000000000000000000000..b6bbb6e68cb22db21440e712497c1f3f18c56835 --- /dev/null +++ b/src/intellij_plugin/ohosgen/src/main/java/event/EventEmitter.java @@ -0,0 +1,86 @@ +/* + * 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 event; + +import java.util.concurrent.ConcurrentHashMap; +import java.util.concurrent.CopyOnWriteArrayList; + +/** + *

类名:该类用于xxx

+ * description event emitter + * + * @author Administrator + * date 2025-02-28 + * @version 1.0 + * @since 2025-02-28 + */ +public class EventEmitter { + // 存储事件类型与对应的监听器列表(线程安全) + private ConcurrentHashMap> eventMap = new ConcurrentHashMap<>(); + + /** + * 注册监听器(on) + * + * @param eventType 事件类型 + * @param listener 事件监听 + */ + public void on(String eventType, CustomEventListener listener) { + eventMap.computeIfAbsent(eventType, k -> new CopyOnWriteArrayList<>()).add(listener); + } + + /** + * 单次监听 + * + * @param eventType 事件类型 + * @param listener 事件监听 + */ + public void once(String eventType, CustomEventListener listener) { + CustomEventListener wrapper = new CustomEventListener() { + @Override + public void handleEvent(CustomEvent event) { + listener.handleEvent(event); + off(eventType, this); // 执行后自动移除监听 + } + }; + on(eventType, wrapper); + } + + /** + * 触发事件(emit) + * + * @param eventType 事件类型 + * @param event 事件 + */ + public void emit(String eventType, CustomEvent event) { + CopyOnWriteArrayList listeners = eventMap.get(eventType); + if (listeners != null) { + listeners.forEach(listener -> listener.handleEvent(event)); + } + } + + /** + * 移除监听器(off) + * + * @param eventType 事件类型 + * @param listener 事件监听 + */ + public void off(String eventType, CustomEventListener listener) { + CopyOnWriteArrayList listeners = eventMap.get(eventType); + if (listeners != null) { + listeners.remove(listener); + } + } +} diff --git a/src/intellij_plugin/ohosgen/src/main/java/grammar/ClassObj.java b/src/intellij_plugin/ohosgen/src/main/java/grammar/ClassObj.java index ada22c3b2e81e3ecd7adec524633ffc06d52da80..c725e178bad74277a870db3821608aa4496c66ff 100644 --- a/src/intellij_plugin/ohosgen/src/main/java/grammar/ClassObj.java +++ b/src/intellij_plugin/ohosgen/src/main/java/grammar/ClassObj.java @@ -15,6 +15,11 @@ package grammar; +import utils.TsToken; + +import java.util.List; +import java.util.concurrent.CopyOnWriteArrayList; + /** *

类名:该类用于xxx

* description class grammar @@ -24,21 +29,32 @@ package grammar; * @version 1.0 * @since 2025-02-28 */ -public class ClassObj { +public class ClassObj extends GBaseObject { private String name; private String alias; - private ParamObj[] paramList; - private FuncObj[] funcList; + private List paramList; + private List funcList; /** * 构造函数 */ - public ClassObj() {} + public ClassObj() { + this.token = TsToken.TS_TOKEN_CLASS; + + this.paramList = new CopyOnWriteArrayList<>(); + this.funcList = new CopyOnWriteArrayList<>(); + } /** * 构造函数 + * + * @param nv 名字 + * @param av 别名 + * @param pl 参数 + * @param fl 方法 */ - public ClassObj(String nv, String av, ParamObj[] pl, FuncObj[] fl) { + public ClassObj(String nv, String av, List pl, List fl) { + this(); this.name = nv; this.alias = av; this.paramList = pl; @@ -86,7 +102,7 @@ public class ClassObj { * * @param funcList 方法列表 */ - public void setFuncList(FuncObj[] funcList) { + public void setFuncList(List funcList) { this.funcList = funcList; } @@ -95,7 +111,7 @@ public class ClassObj { * * @return 方法列表 */ - public FuncObj[] getFuncList() { + public List getFuncList() { return funcList; } @@ -104,7 +120,7 @@ public class ClassObj { * * @param paramList 参数列表 */ - public void setParamList(ParamObj[] paramList) { + public void setParamList(List paramList) { this.paramList = paramList; } @@ -113,7 +129,52 @@ public class ClassObj { * * @return 方法列表 */ - public ParamObj[] getParamList() { + public List getParamList() { return paramList; } + + /** + * 增加param + * + * @param po param + */ + public void addParam(ParamObj po) { + this.paramList.add(po); + } + + /** + * 增加param + * + * @param name 名字 + * @param type 类型 + */ + public void addParam(String name, String type) { + ParamObj po = new ParamObj(); + po.setType(type); + po.setName(name); + this.paramList.add(po); + } + + /** + * 增加方法 + * + * @param fo 方法 + */ + public void addFunc(FuncObj fo) { + this.funcList.add(fo); + } + + /** + * 增加方法 + * + * @param name 名字 + * @param ret 返回值 + * @param poList 参数 + */ + public void addFunc(String name, String ret, List poList) { + FuncObj fo = new FuncObj(); + fo.setName(name); + fo.setRetValue(ret); + fo.setParamList(poList); + } } diff --git a/src/intellij_plugin/ohosgen/src/main/java/grammar/EnumObj.java b/src/intellij_plugin/ohosgen/src/main/java/grammar/EnumObj.java index 4dcc9fec639336589da486a86f1843730f97c0cb..fb7a85096eae44163fe30070735464a1f81f4e9e 100644 --- a/src/intellij_plugin/ohosgen/src/main/java/grammar/EnumObj.java +++ b/src/intellij_plugin/ohosgen/src/main/java/grammar/EnumObj.java @@ -15,6 +15,11 @@ package grammar; +import utils.TsToken; + +import java.util.List; +import java.util.concurrent.CopyOnWriteArrayList; + /** *

类名:该类用于xxx

* description enum grammar @@ -24,16 +29,20 @@ package grammar; * @version 1.0 * @since 2025-02-28 */ -public class EnumObj { +public class EnumObj extends GBaseObject { private String name; private String alias; - private String[] memberList; - private String[] valueList; + private List memberList; + private List valueList; /** * 构造函数 */ - public EnumObj() {} + public EnumObj() { + this.token = TsToken.TS_TOKEN_ENUM; + this.memberList = new CopyOnWriteArrayList<>(); + this.valueList = new CopyOnWriteArrayList<>(); + } /** * 构造函数 @@ -43,7 +52,7 @@ public class EnumObj { * @param ml 成员 * @param vl 值 */ - public EnumObj(String nv, String av, String[] ml, String[] vl) { + public EnumObj(String nv, String av, List ml, List vl) { this.name = nv; this.alias = av; this.memberList = ml; @@ -91,7 +100,7 @@ public class EnumObj { * * @return 成员列表 */ - public String[] getMemberList() { + public List getMemberList() { return memberList; } @@ -100,7 +109,7 @@ public class EnumObj { * * @param memberList 成员列表 */ - public void setMemberList(String[] memberList) { + public void setMemberList(List memberList) { this.memberList = memberList; } @@ -109,7 +118,7 @@ public class EnumObj { * * @return 值列表 */ - public String[] getValueList() { + public List getValueList() { return valueList; } @@ -118,7 +127,27 @@ public class EnumObj { * * @param valueList 值列表 */ - public void setValueList(String[] valueList) { + public void setValueList(List valueList) { this.valueList = valueList; } + + /** + * 添加成员 + * + * @param memName 成员名称 + */ + public void addMemberItem(String memName) { + this.memberList.add(memName); + } + + /** + * 添加成员值 + * + * @param memValue 成员值 + */ + public void addMemberValue(String memValue) { + this.valueList.add(memValue); + } + + } 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 76174fa676ac563645237193322db6732a242187..b16e66feb92ceb91af91e4c17c188d02c9f78a58 100644 --- a/src/intellij_plugin/ohosgen/src/main/java/grammar/FuncObj.java +++ b/src/intellij_plugin/ohosgen/src/main/java/grammar/FuncObj.java @@ -15,6 +15,11 @@ package grammar; +import utils.TsToken; + +import java.util.List; +import java.util.concurrent.CopyOnWriteArrayList; + /** *

类名:该类用于xxx

* description function of grammar @@ -24,16 +29,20 @@ package grammar; * @version 1.0 * @since 2025-02-28 */ -public class FuncObj { +public class FuncObj extends GBaseObject { private String type; private String name; private String retValue; - private ParamObj[] paramList; + private List paramList; /** * 构造函数 */ - public FuncObj() {} + public FuncObj() { + this.token = TsToken.TS_TOKEN_FUNCTION; + + this.paramList = new CopyOnWriteArrayList<>(); + } /** * 构造函数 @@ -43,7 +52,9 @@ public class FuncObj { * @param rv 返回值 * @param pl 参数 */ - public FuncObj(String tv, String nv, String rv, ParamObj[] pl) { + public FuncObj(String tv, String nv, String rv, List pl) { + this(); + this.type = tv; this.name = nv; this.retValue = rv; @@ -109,7 +120,7 @@ public class FuncObj { * * @param paramList 参数 */ - public void setParamList(ParamObj[] paramList) { + public void setParamList(List paramList) { this.paramList = paramList; } @@ -118,7 +129,29 @@ public class FuncObj { * * @return 参数 */ - public ParamObj[] getParamList() { + public List getParamList() { return paramList; } + + /** + * 增加函数参数 + * + * @param po 参数 + */ + public void addParam(ParamObj po) { + this.paramList.add(po); + } + + /** + * 增加函数参数 + * + * @param name 参数名称 + * @param type 参数类型 + */ + public void addParam(String name, String type) { + ParamObj po = new ParamObj(); + po.setName(name); + po.setType(type); + this.paramList.add(po); + } } diff --git a/src/intellij_plugin/ohosgen/src/main/java/grammar/GBaseObject.java b/src/intellij_plugin/ohosgen/src/main/java/grammar/GBaseObject.java new file mode 100644 index 0000000000000000000000000000000000000000..850b9219217cb6c609c50b345b1d620891704ad5 --- /dev/null +++ b/src/intellij_plugin/ohosgen/src/main/java/grammar/GBaseObject.java @@ -0,0 +1,89 @@ +/* + * 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 grammar; + +import com.google.gson.Gson; +import com.google.gson.GsonBuilder; +import utils.Constants; + +/** + *

类名:该类用于xxx

+ * description grammer base object + * + * @author Administrator + * date 2025-02-28 + * @version 1.0 + * @since 2025-02-28 + */ +public class GBaseObject { + /** + * 解析未知代码 + */ + protected int languageType = Constants.PARSE_UNKNOWN_LANGUAGE; + + /** + * 关键字 + */ + protected String token = ""; + + /** + * 获取 language type + * + * @return 解析语言类型 + */ + public int getLanguageType() { + return languageType; + } + + /** + * 设置 language type + * + * @param languageType 解析语言类型 + */ + public void setLanguageType(int languageType) { + this.languageType = languageType; + } + + /** + * 获取 token + * + * @return token 关键字 + */ + public String getToken() { + return token; + } + + /** + * 设置 token + * + * @param token 关键字 + */ + public void setToken(String token) { + this.token = token; + } + + /** + * 转JSON字符串 + * + * @return json 字符串 + */ + public String toJsonString() { + // 创建 Gson 实例并启用格式化 + Gson gson = new GsonBuilder().setPrettyPrinting().create(); + + return gson.toJson(this); + } +} diff --git a/src/intellij_plugin/ohosgen/src/main/java/grammar/InterfaceObject.java b/src/intellij_plugin/ohosgen/src/main/java/grammar/InterfaceObject.java new file mode 100644 index 0000000000000000000000000000000000000000..d9a5a8cb7f19130a69d8c426958cf3f6583ac9ab --- /dev/null +++ b/src/intellij_plugin/ohosgen/src/main/java/grammar/InterfaceObject.java @@ -0,0 +1,179 @@ +/* + * 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 grammar; + +import utils.TsToken; + +import java.util.List; +import java.util.concurrent.CopyOnWriteArrayList; + +/** + *

类名:该类用于xxx

+ * description interface grammar + * + * @author Administrator + * date 2025-02-28 + * @version 1.0 + * @since 2025-02-28 + */ +public class InterfaceObject extends GBaseObject { + private String name; + private String alias; + private List paramList; + private List funcList; + + /** + * 构造函数 + */ + public InterfaceObject() { + this.token = TsToken.TS_TOKEN_INTERFACE; + + this.paramList = new CopyOnWriteArrayList<>(); + this.funcList = new CopyOnWriteArrayList<>(); + } + + /** + * 构造函数 + * + * @param nv 名字 + * @param av 别名 + * @param pl 参数 + * @param fl 方法 + */ + public InterfaceObject(String nv, String av, List pl, List fl) { + this.name = nv; + this.alias = av; + this.paramList = pl; + this.funcList = fl; + } + + /** + * 设名字 + * + * @param name 名字 + */ + public void setName(String name) { + this.name = name; + } + + /** + * 读名字 + * + * @return name 名字 + */ + public String getName() { + return name; + } + + /** + * 设置别名 + * + * @param alias 别名 + */ + public void setAlias(String alias) { + this.alias = alias; + } + + /** + * 读取别名 + * + * @return 别名 + */ + public String getAlias() { + return alias; + } + + /** + * 设置方法 + * + * @param funcList 方法列表 + */ + public void setFuncList(List funcList) { + this.funcList = funcList; + } + + /** + * 获取方法列表 + * + * @return 方法列表 + */ + public List getFuncList() { + return funcList; + } + + /** + * 设置参数 + * + * @param paramList 参数列表 + */ + public void setParamList(List paramList) { + this.paramList = paramList; + } + + /** + * 读取方法 + * + * @return 方法列表 + */ + public List getParamList() { + return paramList; + } + + /** + * 增加参数 + * + * @param po 参数 + */ + public void addParam(ParamObj po) { + this.paramList.add(po); + } + + /** + * 增加参数 + * + * @param name 名称 + * @param type 类型 + */ + public void addParam(String name, String type) { + ParamObj po = new ParamObj(); + po.setType(type); + po.setName(name); + this.paramList.add(po); + } + + /** + * 增加方法 + * + * @param fo 方法 + */ + public void addFunc(FuncObj fo) { + this.funcList.add(fo); + } + + /** + * 增加方法 + * + * @param name 名称 + * @param ret 返回值 + * @param poList 参数 + */ + public void addFunc(String name, String ret, List poList) { + FuncObj fo = new FuncObj(); + fo.setName(name); + fo.setRetValue(ret); + fo.setParamList(poList); + } +} diff --git a/src/intellij_plugin/ohosgen/src/main/java/grammar/ParamObj.java b/src/intellij_plugin/ohosgen/src/main/java/grammar/ParamObj.java index 68328928376cbfaacec0f96b3e9fcd4530a14c0d..e8a0ab546f1a20a00b9ecea43405fa5d34d64d98 100644 --- a/src/intellij_plugin/ohosgen/src/main/java/grammar/ParamObj.java +++ b/src/intellij_plugin/ohosgen/src/main/java/grammar/ParamObj.java @@ -24,7 +24,7 @@ package grammar; * @version 1.0 * @since 2025-02-28 */ -public class ParamObj { +public class ParamObj extends GBaseObject { private String type; private String name; private int arraySize; diff --git a/src/intellij_plugin/ohosgen/src/main/java/grammar/ParseObj.java b/src/intellij_plugin/ohosgen/src/main/java/grammar/ParseObj.java index 6f35ae50f70fe6c9bc00c74c9e2f07c600418a58..62d6bfcf352e36ae57ff124c3413c8decb0c7248 100644 --- a/src/intellij_plugin/ohosgen/src/main/java/grammar/ParseObj.java +++ b/src/intellij_plugin/ohosgen/src/main/java/grammar/ParseObj.java @@ -24,7 +24,7 @@ package grammar; * @version 1.0 * @since 2025-02-28 */ -public class ParseObj { +public class ParseObj extends GBaseObject { private EnumObj[] enumList; private UnionObj[] unionList; private StructObj[] structList; 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 1661708c4cb8c0c06cefff62771b18bab4442e56..c3760d2699dbb458dc3a4e2c1f904eaa5bb58797 100644 --- a/src/intellij_plugin/ohosgen/src/main/java/grammar/StructObj.java +++ b/src/intellij_plugin/ohosgen/src/main/java/grammar/StructObj.java @@ -24,7 +24,7 @@ package grammar; * @version 1.0 * @since 2025-02-28 */ -public class StructObj { +public class StructObj extends GBaseObject { private String name; private String alias; private ParamObj[] memberList; diff --git a/src/intellij_plugin/ohosgen/src/main/java/grammar/TypeObj.java b/src/intellij_plugin/ohosgen/src/main/java/grammar/TypeObj.java index 8233722e535e5ec76dc13aa376a102892c9e3c09..559a5d74c0570617a5bd621f9f7e5385cf0b1cce 100644 --- a/src/intellij_plugin/ohosgen/src/main/java/grammar/TypeObj.java +++ b/src/intellij_plugin/ohosgen/src/main/java/grammar/TypeObj.java @@ -15,6 +15,11 @@ package grammar; +import utils.TsToken; + +import java.util.List; +import java.util.concurrent.CopyOnWriteArrayList; + /** *

类名:该类用于xxx

* description type grammar @@ -24,17 +29,23 @@ package grammar; * @version 1.0 * @since 2025-02-28 */ -public class TypeObj { +public class TypeObj extends GBaseObject { private String name; private String alias; - private ParamObj[] paramList; - private FuncObj[] funcList; - private String[] typeList; + private List paramList; + private List funcList; + private List typeList; /** * 构造函数 */ - public TypeObj() {} + public TypeObj() { + this.token = TsToken.TS_TOKEN_TYPE; + + this.paramList = new CopyOnWriteArrayList<>(); + this.funcList = new CopyOnWriteArrayList<>(); + this.typeList = new CopyOnWriteArrayList<>(); + } /** * 构造函数 @@ -45,7 +56,11 @@ public class TypeObj { * @param fl 方法 * @param tl 类型 */ - public TypeObj(String nv, String av, ParamObj[] pl, FuncObj[] fl, String[] tl) { + public TypeObj(String nv, + String av, + List pl, + List fl, + List tl) { this.name = nv; this.alias = av; this.paramList = pl; @@ -94,7 +109,7 @@ public class TypeObj { * * @return 参数 */ - public ParamObj[] getParamList() { + public List getParamList() { return paramList; } @@ -103,7 +118,7 @@ public class TypeObj { * * @param paramList 参数 */ - public void setParamList(ParamObj[] paramList) { + public void setParamList(List paramList) { this.paramList = paramList; } @@ -112,7 +127,7 @@ public class TypeObj { * * @return 类型 */ - public String[] getTypeList() { + public List getTypeList() { return typeList; } @@ -120,7 +135,7 @@ public class TypeObj { * 设置类型 * @param typeList 类型 */ - public void setTypeList(String[] typeList) { + public void setTypeList(List typeList) { this.typeList = typeList; } @@ -129,7 +144,7 @@ public class TypeObj { * * @return 方法 */ - public FuncObj[] getFuncList() { + public List getFuncList() { return funcList; } @@ -138,7 +153,16 @@ public class TypeObj { * * @param funcList 方法 */ - public void setFuncList(FuncObj[] funcList) { + public void setFuncList(List funcList) { this.funcList = funcList; } + + /** + * 设置value + * + * @param value 定义值 + */ + public void addTypeValue(String value) { + this.typeList.add(value); + } } 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 47d50e3dd8c26bd0a375b0841dfe7a5f12cee717..73f8dfeffe36965f2f736a3be5d86e2a4f148c87 100644 --- a/src/intellij_plugin/ohosgen/src/main/java/grammar/UnionObj.java +++ b/src/intellij_plugin/ohosgen/src/main/java/grammar/UnionObj.java @@ -26,7 +26,7 @@ import kotlinx.html.S; * @version 1.0 * @since 2025-02-28 */ -public class UnionObj { +public class UnionObj extends GBaseObject { private String name; private String alias; private ParamObj[] memList; 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 0be7e4e83862d51083ffb8d311c07360dadc19c4..b31e7d30bde3bc2d892821c152d5936f40da4043 100644 --- a/src/intellij_plugin/ohosgen/src/main/java/parse/ParseBase.java +++ b/src/intellij_plugin/ohosgen/src/main/java/parse/ParseBase.java @@ -147,18 +147,22 @@ public abstract class ParseBase { /** * 解析enum * + * @param pi2 解析结果 * @return enum */ - protected EnumObj[] parseEnum() { + protected EnumObj[] parseEnum(ParseTaskInfo pi2) { + System.out.println("parse enum: " + pi2.toString()); return new EnumObj[0]; }; /** * 解析union * + * @param pi2 解析任务 * @return union */ - protected UnionObj[] parseUnion() { + protected UnionObj[] parseUnion(ParseTaskInfo pi2) { + System.out.println("parse union: " + pi2.toString()); return new UnionObj[0]; } @@ -167,7 +171,8 @@ public abstract class ParseBase { * * @return struct */ - protected StructObj[] parseStruct() { + protected StructObj[] parseStruct(ParseTaskInfo pi2) { + System.out.println("parse struct: " + pi2.toString()); return new StructObj[0]; } @@ -176,7 +181,8 @@ public abstract class ParseBase { * * @return class */ - protected ClassObj[] parseClass() { + protected ClassObj[] parseClass(ParseTaskInfo pi2) { + System.out.println("parse class: " + pi2.toString()); return new ClassObj[0]; } @@ -185,16 +191,19 @@ public abstract class ParseBase { * * @return func */ - protected FuncObj[] parseFunc() { + protected FuncObj[] parseFunc(ParseTaskInfo pi2) { + System.out.println("parse function: " + pi2.toString()); return new FuncObj[0]; } /** * 解析type * + * @param pi2 解析进程 * @return type */ - protected TypeObj[] parseType() { + protected TypeObj[] parseType(ParseTaskInfo pi2) { + System.out.println("parse type: " + pi2.toString()); return new TypeObj[0]; } diff --git a/src/intellij_plugin/ohosgen/src/main/java/parse/ParseC.java b/src/intellij_plugin/ohosgen/src/main/java/parse/ParseC.java index ed81bef6ce262b11b94808ae9b65c40a3aded17f..f03f0b448f81d63d49433ab2bd06b235b45ee4cc 100644 --- a/src/intellij_plugin/ohosgen/src/main/java/parse/ParseC.java +++ b/src/intellij_plugin/ohosgen/src/main/java/parse/ParseC.java @@ -95,7 +95,7 @@ public class ParseC extends ParseBase { public void parseCStream(CharStream fileCStream) { System.out.println("c/cpp parse char stream"); this.fcStream = fileCStream; - SendEvent(Constants.START_STATUS, Constants.C_CPP_START_MSG, 50); + sendEvent(Constants.START_STATUS, Constants.C_CPP_START_MSG, 50); try { // 初始化词法分析器 CPP14Lexer lexer = new CPP14Lexer(this.fcStream); @@ -114,36 +114,36 @@ public class ParseC extends ParseBase { System.out.println("parse cstream e.printStackTrace(): " + e.getMessage()); } - SendEvent(Constants.COMPLETE_STATUS, Constants.C_CPP_COMPLETE_MSG, 50); + sendEvent(Constants.COMPLETE_STATUS, Constants.C_CPP_COMPLETE_MSG, 50); } @Override - protected EnumObj[] parseEnum() { - return super.parseEnum(); + protected EnumObj[] parseEnum(ParseTaskInfo pi2) { + return super.parseEnum(pi2); } @Override - protected UnionObj[] parseUnion() { - return super.parseUnion(); + protected UnionObj[] parseUnion(ParseTaskInfo pi2) { + return super.parseUnion(pi2); } @Override - protected StructObj[] parseStruct() { - return super.parseStruct(); + protected StructObj[] parseStruct(ParseTaskInfo pi2) { + return super.parseStruct(pi2); } @Override - protected ClassObj[] parseClass() { - return super.parseClass(); + protected ClassObj[] parseClass(ParseTaskInfo pi2) { + return super.parseClass(pi2); } @Override - protected FuncObj[] parseFunc() { - return super.parseFunc(); + protected FuncObj[] parseFunc(ParseTaskInfo pi2) { + return super.parseFunc(pi2); } @Override - protected TypeObj[] parseType() { - return super.parseType(); + protected TypeObj[] parseType(ParseTaskInfo pi2) { + return super.parseType(pi2); } } 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 03e5b8504880b034b62aa12d9faaee18cde8361b..3e8604d26a004e58ea584492b2267314da592d12 100644 --- a/src/intellij_plugin/ohosgen/src/main/java/parse/ParseTask.java +++ b/src/intellij_plugin/ohosgen/src/main/java/parse/ParseTask.java @@ -132,6 +132,7 @@ public class ParseTask extends Task.Backgroundable implements BaseListener { ObjectMapper mapper = new ObjectMapper(); try { ParseTaskInfo pi2 = mapper.readValue(jsonStr, ParseTaskInfo.class); + parser.receive(pi2); } catch (JsonProcessingException e) { System.out.println("Test fromJson catch: " + e.getMessage()); } diff --git a/src/intellij_plugin/ohosgen/src/main/java/parse/ParseTaskInfo.java b/src/intellij_plugin/ohosgen/src/main/java/parse/ParseTaskInfo.java index 6b1c6d1c077ad7cc3174925f2ab459a94b1806b8..df868d3310a01d2ed254963586251aa75ef4b0df 100644 --- a/src/intellij_plugin/ohosgen/src/main/java/parse/ParseTaskInfo.java +++ b/src/intellij_plugin/ohosgen/src/main/java/parse/ParseTaskInfo.java @@ -27,7 +27,8 @@ package parse; public class ParseTaskInfo { private String status; private String message; - private int currentType; + private int lanType; + private int parseType; private String jsonData; private int progress; private int total; @@ -55,7 +56,7 @@ public class ParseTaskInfo { public ParseTaskInfo(String vs, String vm, int ct, String jd) { this.status = vs; this.message = vm; - this.currentType = ct; + this.lanType = ct; this.jsonData = jd; } @@ -95,6 +96,42 @@ public class ParseTaskInfo { return message; } + /** + * 获取解析类型 + * + * @return 解析类型 + */ + public int getLanType() { + return lanType; + } + + /** + * 设置 解析类型 + * + * @param lanType 解析类型 + */ + public void setLanType(int lanType) { + this.lanType = lanType; + } + + /** + * 获取 json data + * + * @return jsonData 解析数据 + */ + public String getJsonData() { + return jsonData; + } + + /** + * 设置 json data + * + * @param jsonData 解析数据 + */ + public void setJsonData(String jsonData) { + this.jsonData = jsonData; + } + /** * 设置进度 * @@ -130,4 +167,22 @@ public class ParseTaskInfo { public int getTotal() { return total; } + + /** + * 读取解析类型 + * + * @return 解析类型 + */ + public int getParseType() { + return parseType; + } + + /** + * 读取解析类型 + * + * @param parseType 解析类型 + */ + public void setParseType(int parseType) { + this.parseType = parseType; + } } 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 ed985e4549809888d3f14b0c2f8f041a7e386035..091a3fc4d0d4d690421c08fccb81782dd97f180b 100644 --- a/src/intellij_plugin/ohosgen/src/main/java/parse/ParseTs.java +++ b/src/intellij_plugin/ohosgen/src/main/java/parse/ParseTs.java @@ -21,6 +21,10 @@ import antlr.typescript.TypeScriptLexer; import antlr.typescript.TypeScriptParser; import com.fasterxml.jackson.core.JsonProcessingException; import com.fasterxml.jackson.databind.ObjectMapper; +import com.google.gson.Gson; +import com.google.gson.GsonBuilder; +import event.CustomEvent; +import event.CustomEventListener; import grammar.*; import org.antlr.v4.runtime.CharStream; import org.antlr.v4.runtime.CommonTokenStream; @@ -39,7 +43,7 @@ import utils.Constants; * @since 2025-02-28 * @version 1.0 */ -public class ParseTs extends ParseBase { +public class ParseTs extends ParseBase implements CustomEventListener { /** * 根据文件名解析 * @@ -72,7 +76,7 @@ public class ParseTs extends ParseBase { public void parseContent(String fileContent) { System.out.println("ts parseContent"); this.fileContent = fileContent; - SendEvent(Constants.COMPLETE_STATUS, Constants.TS_COMPLETE_MSG, 50); + sendEvent(Constants.COMPLETE_STATUS, Constants.TS_COMPLETE_MSG, 50); } /** @@ -85,7 +89,7 @@ public class ParseTs extends ParseBase { System.out.println("ts parse char stream start"); this.fcStream = fileCStream; - SendEvent(Constants.START_STATUS, Constants.TS_START_MSG, 0); + sendEvent(Constants.START_STATUS, Constants.TS_START_MSG, 0); try { // 初始化词法分析器 TypeScriptLexer lexer = new TypeScriptLexer(this.fcStream); @@ -99,41 +103,107 @@ public class ParseTs extends ParseBase { ParseTreeWalker walker = new ParseTreeWalker(); walker.walk(tsc, tree); + Gson gson = new GsonBuilder().setPrettyPrinting().create(); + + String json = gson.toJson(tsc); + System.out.println("ts parse result: " + json); + System.out.println("ts parse char stream finish"); } catch (RecognitionException e) { System.out.println("parse cstream e.printStackTrace(): " + e.getMessage()); } - SendEvent(Constants.COMPLETE_STATUS, Constants.TS_COMPLETE_MSG, 50); + sendEvent(Constants.COMPLETE_STATUS, Constants.TS_COMPLETE_MSG, 50); } + /** + * 接收解析结果 + * + * @param pi2 解析结构 + */ + @Override + public void receive(ParseTaskInfo pi2) { + super.receive(pi2); + if (pi2.getLanType() != Constants.PARSE_TS_LANGUAGE) { + System.err.println("Language type is not ts language"); + return; + } + + } + + /** + * 解析枚举 + * + * @param pi2 解析结果 + * @return 解析结果 + */ + @Override + protected EnumObj[] parseEnum(ParseTaskInfo pi2) { + return super.parseEnum(pi2); + } + + /** + * 解析联合 + * + * @param pi2 解析结果 + * @return 解析结果 + */ @Override - protected EnumObj[] parseEnum() { - return super.parseEnum(); + protected UnionObj[] parseUnion(ParseTaskInfo pi2) { + return super.parseUnion(pi2); } + /** + * 解析结构体 + * + * @param pi2 解析结果 + * @return 解析结果 + */ @Override - protected UnionObj[] parseUnion() { - return super.parseUnion(); + protected StructObj[] parseStruct(ParseTaskInfo pi2) { + return super.parseStruct(pi2); } + /** + * 解析类 + * + * @param pi2 解析结果 + * @return 解析结果 + */ @Override - protected StructObj[] parseStruct() { - return super.parseStruct(); + protected ClassObj[] parseClass(ParseTaskInfo pi2) { + return super.parseClass(pi2); } + /** + * 解析方法 + * + * @param pi2 解析结果 + * @return 解析结果 + */ @Override - protected ClassObj[] parseClass() { - return super.parseClass(); + protected FuncObj[] parseFunc(ParseTaskInfo pi2) { + return super.parseFunc(pi2); } + /** + * 解析type + * + * @param pi2 解析结果 + * @return 解析结果 + */ @Override - protected FuncObj[] parseFunc() { - return super.parseFunc(); + protected TypeObj[] parseType(ParseTaskInfo pi2) { + return super.parseType(pi2); } + /** + * 处理事件 + * + * @param event 事件 + */ @Override - protected TypeObj[] parseType() { - return super.parseType(); + public void handleEvent(CustomEvent event) { + System.out.println("parse ts handle: " + event.toString()); } } diff --git a/src/intellij_plugin/ohosgen/src/main/java/utils/Constants.java b/src/intellij_plugin/ohosgen/src/main/java/utils/Constants.java index af26815dd0dd7b2aee7f69f261c4b4b8bc084522..42eb455c2b9a4a4e9a285d98d66d8934cce4244b 100644 --- a/src/intellij_plugin/ohosgen/src/main/java/utils/Constants.java +++ b/src/intellij_plugin/ohosgen/src/main/java/utils/Constants.java @@ -25,6 +25,11 @@ package utils; * @version 1.0 */ public class Constants { + /** + * 解析未知代码 + */ + public static final int PARSE_UNKNOWN_LANGUAGE = -1; + /** * 解析c/cpp代码 */ diff --git a/src/intellij_plugin/ohosgen/src/main/java/utils/TsToken.java b/src/intellij_plugin/ohosgen/src/main/java/utils/TsToken.java new file mode 100644 index 0000000000000000000000000000000000000000..19907897d40ed05ab92cd349e7d9341b1166ed8f --- /dev/null +++ b/src/intellij_plugin/ohosgen/src/main/java/utils/TsToken.java @@ -0,0 +1,364 @@ +/* + * 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; + +/** + *

类名:该类用于xxx

+ * description typescript token + * + * @author Administrator + * date 2025-02-28 + * @version 1.0 + * @since 2025-02-28 + */ + +public class TsToken { + /** + * enum token + */ + public static final String TS_TOKEN_ENUM = "enum"; + + /** + * class token + */ + public static final String TS_TOKEN_CLASS = "class"; + + /** + * extends token + */ + public static final String TS_TOKEN_EXTENDS = "extends"; + + /** + * super token + */ + public static final String TS_TOKEN_SUPER = "super"; + + /** + * const token + */ + public static final String TS_TOKEN_CONST = "const"; + + /** + * export token + */ + public static final String TS_TOKEN_EXPORT = "export"; + + /** + * import token + */ + public static final String TS_TOKEN_IMPORT = "import"; + + /** + * implements token + */ + public static final String TS_TOKEN_IMPLEMENT = "implements"; + + /** + * let token + */ + public static final String TS_TOKEN_LET = "let"; + + /** + * private token + */ + public static final String TS_TOKEN_PRIVATE = "private"; + + /** + * public token + */ + public static final String TS_TOKEN_PUBLIC = "public"; + + /** + * interface token + */ + public static final String TS_TOKEN_INTERFACE = "interface"; + + /** + * package token + */ + public static final String TS_TOKEN_PACKAGE = "package"; + + /** + * protected token + */ + public static final String TS_TOKEN_PROTECTED = "protected"; + + /** + * static token + */ + public static final String TS_TOKEN_STATIC = "static"; + + /** + * any token + */ + public static final String TS_TOKEN_ANY = "any"; + + /** + * number token + */ + public static final String TS_TOKEN_NUMBER = "number"; + + /** + * never token + */ + public static final String TS_TOKEN_NEVER = "never"; + + /** + * boolean token + */ + public static final String TS_TOKEN_BOOLEAN = "boolean"; + + /** + * string token + */ + public static final String TS_TOKEN_STRING = "string"; + + /** + * unique token + */ + public static final String TS_TOKEN_UNIQUE = "unique"; + + /** + * symbol token + */ + public static final String TS_TOKEN_SYMBOL = "symbol"; + + /** + * undefined token + */ + public static final String TS_TOKEN_UNDEFINED = "undefined"; + + /** + * object token + */ + public static final String TS_TOKEN_OBJECT = "object"; + + /** + * of token + */ + public static final String TS_TOKEN_OF = "of"; + + /** + * keyof token + */ + public static final String TS_TOKEN_KEYOF = "keyof"; + + /** + * type token + */ + public static final String TS_TOKEN_TYPE = "type"; + + /** + * constructor token + */ + public static final String TS_TOKEN_CONSTRUCTOR = "constructor"; + + /** + * namespace token + */ + public static final String TS_TOKEN_NAMESPACE = "namespace"; + + /** + * require token + */ + public static final String TS_TOKEN_REQUIRE = "require"; + + /** + * module token + */ + public static final String TS_TOKEN_MODULE = "module"; + + /** + * declare token + */ + public static final String TS_TOKEN_DECLARE = "declare"; + + /** + * abstract token + */ + public static final String TS_TOKEN_ABSTRACT = "abstract"; + + /** + * IS token + */ + public static final String TS_TOKEN_IS = "is"; + + /** + * null token + */ + public static final String TS_TOKEN_NULL = "null"; + + /** + * break token + */ + public static final String TS_TOKEN_BREAK = "break"; + + /** + * do token + */ + public static final String TS_TOKEN_DO = "do"; + + /** + * instanceof token + */ + public static final String TS_TOKEN_INSTANCEOF = "instanceof"; + + /** + * typeof token + */ + public static final String TS_TOKEN_TYPEOF = "typeof"; + + /** + * case token + */ + public static final String TS_TOKEN_CASE = "case"; + + /** + * else token + */ + public static final String TS_TOKEN_ELSE = "else"; + + /** + * new token + */ + public static final String TS_TOKEN_NEW = "new"; + + /** + * var token + */ + public static final String TS_TOKEN_VAR = "var"; + + /** + * catch token + */ + public static final String TS_TOKEN_CATCH = "catch"; + + /** + * finally token + */ + public static final String TS_TOKEN_FINALLY = "finally"; + + /** + * return token + */ + public static final String TS_TOKEN_RETURN = "return"; + + /** + * void token + */ + public static final String TS_TOKEN_VOID = "void"; + + /** + * continue token + */ + public static final String TS_TOKEN_CONTINUE = "continue"; + + /** + * for token + */ + public static final String TS_TOKEN_FOR = "for"; + + /** + * switch token + */ + public static final String TS_TOKEN_SWITCH = "switch"; + + /** + * while token + */ + public static final String TS_TOKEN_WHILE = "while"; + + /** + * debugger token + */ + public static final String TS_TOKEN_DEBUGGER = "debugger"; + + /** + * function token + */ + public static final String TS_TOKEN_FUNCTION = "function"; + + /** + * this token + */ + public static final String TS_TOKEN_THIS = "this"; + + /** + * with token + */ + public static final String TS_TOKEN_WITH = "with"; + + /** + * default token + */ + public static final String TS_TOKEN_DEFAULT = "default"; + + /** + * if token + */ + public static final String TS_TOKEN_IF = "if"; + + /** + * throw token + */ + public static final String TS_TOKEN_THROW = "throw"; + + /** + * delete token + */ + public static final String TS_TOKEN_DELETE = "delete"; + + /** + * in token + */ + public static final String TS_TOKEN_IN = "in"; + + /** + * try token + */ + public static final String TS_TOKEN_TRY = "try"; + + /** + * as token + */ + public static final String TS_TOKEN_AS = "as"; + + /** + * from token + */ + public static final String TS_TOKEN_FROM = "from"; + + /** + * readonly token + */ + public static final String TS_TOKEN_READONLY = "readonly"; + + /** + * async token + */ + public static final String TS_TOKEN_ASYNC = "async"; + + /** + * await token + */ + public static final String TS_TOKEN_AWAIT = "await"; + + /** + * yield token + */ + public static final String TS_TOKEN_YIELD = "yield"; + +} diff --git a/src/intellij_plugin/ohosgen/src/test/java/event/CustomEventTest.java b/src/intellij_plugin/ohosgen/src/test/java/event/CustomEventTest.java new file mode 100644 index 0000000000000000000000000000000000000000..9a07c8c31716c0edfc37e96b44200cc3e889521a --- /dev/null +++ b/src/intellij_plugin/ohosgen/src/test/java/event/CustomEventTest.java @@ -0,0 +1,50 @@ +/* + * 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 event; + +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.*; + +/** + *

类名:该类用于xxx

+ * description + * + * @author Administrator + * date 2025-02-28 + * @version 1.0 + * @since 2025-02-28 + */ +class CustomEventTest { + + @BeforeEach + void setUp() { + } + + @AfterEach + void tearDown() { + } + + @Test + void getMessage() { + } + + @Test + void setMessage() { + } +} \ No newline at end of file diff --git a/src/intellij_plugin/ohosgen/src/test/java/event/EventEmitterTest.java b/src/intellij_plugin/ohosgen/src/test/java/event/EventEmitterTest.java new file mode 100644 index 0000000000000000000000000000000000000000..50b8fdbda6e73d9540594ed1920e1d1551b85607 --- /dev/null +++ b/src/intellij_plugin/ohosgen/src/test/java/event/EventEmitterTest.java @@ -0,0 +1,78 @@ +/* + * 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 event; + +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +import java.util.EventListener; + +import static org.junit.jupiter.api.Assertions.*; + +/** + *

类名:该类用于xxx

+ * description + * + * @author Administrator + * date 2025-02-28 + * @version 1.0 + * @since 2025-02-28 + */ +class EventEmitterTest { + + @BeforeEach + void setUp() { + } + + @AfterEach + void tearDown() { + } + + @Test + void testEmit() { + EventEmitter emitter = new EventEmitter(); + + // 注册监听器 + emitter.on("message", +event -> { + System.out.println("收到消息:" + event.getMessage()); + assertTrue(true); + } + ); + + // 触发事件 + CustomEvent event = new CustomEvent(emitter, "Hello World"); + emitter.emit("message", event); + } + + @Test + void on() { + + } + + @Test + void once() { + } + + @Test + void emit() { + } + + @Test + void off() { + } +} \ No newline at end of file