• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2025 Shenzhen Kaihong Digital.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  * http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 package parse;
17 
18 import antlr.ParseBaseListener;
19 import antlr.typescript.TypeScriptCustomListener;
20 import antlr.typescript.TypeScriptErrorListener;
21 import antlr.typescript.TypeScriptLexer;
22 import antlr.typescript.TypeScriptParser;
23 import com.fasterxml.jackson.core.JsonProcessingException;
24 import com.fasterxml.jackson.databind.ObjectMapper;
25 import event.CustomEvent;
26 import event.CustomEventListener;
27 import grammar.*;
28 import org.antlr.v4.runtime.CharStream;
29 import org.antlr.v4.runtime.CommonTokenStream;
30 import org.antlr.v4.runtime.RecognitionException;
31 import org.antlr.v4.runtime.tree.ParseTree;
32 import org.antlr.v4.runtime.tree.ParseTreeWalker;
33 import utils.BaseEvent;
34 import utils.Constants;
35 
36 /**
37  * <h3>类名:该类用于xxx</h3>
38  * description ${description}
39  *
40  * @author ${USER}
41  * date 2025-02-28
42  * @since 2025-02-28
43  * @version 1.0
44  */
45 public class ParseTs extends ParseBase implements CustomEventListener {
46     /**
47      * 根据文件名解析
48      *
49      * @param filePath 文件路径
50      */
51     @Override
parseFile(String filePath)52     public void parseFile(String filePath) {
53         System.out.println("parseFile: " + filePath);
54         BaseEvent pcEvent = new BaseEvent(this);
55         pcEvent.setEventMsg("parsec complete");
56         ParseTaskInfo pi = new ParseTaskInfo("start", "parse ts starting", 0, 100);
57         ObjectMapper mapper = new ObjectMapper();
58         try {
59             String jsonStr = mapper.writeValueAsString(pi);
60             pcEvent.setEventMsg(jsonStr);
61         } catch (JsonProcessingException e) {
62             System.out.println("json process error: " + e.getMessage());
63         }
64         listeners.forEach(listener -> {
65             listener.onEvent(pcEvent);
66         });
67     }
68 
69     /**
70      * 处理内容
71      *
72      * @param fileContent 文件内容
73      */
74     @Override
parseContent(String fileContent)75     public void parseContent(String fileContent) {
76         System.out.println("ts parseContent");
77         this.fileContent = fileContent;
78         sendEvent(Constants.COMPLETE_STATUS, Constants.TS_COMPLETE_MSG, 50);
79     }
80 
81     /**
82      * 处理内容
83      *
84      * @param fileCStream
85      *         文件内容
86      * @return 解析结果
87      */
88     @Override
parseCStream(CharStream fileCStream)89     public ParseObj parseCStream(CharStream fileCStream) {
90         System.out.println("ts parse char stream start");
91         this.fcStream = fileCStream;
92 
93         sendEvent(Constants.START_STATUS, Constants.TS_START_MSG, 0);
94         try {
95             // 初始化词法分析器
96             TypeScriptLexer lexer = new TypeScriptLexer(this.fcStream);
97             CommonTokenStream tokens = new CommonTokenStream(lexer);
98             // 初始化语法分析器并生成 AST
99             TypeScriptParser parser = new TypeScriptParser(tokens);
100             parser.removeErrorListeners();
101             parser.addErrorListener(new TypeScriptErrorListener());
102             ParseTree tree = parser.program();
103             TypeScriptCustomListener tsc = new TypeScriptCustomListener();
104             ParseTreeWalker walker = new ParseTreeWalker();
105             walker.walk(tsc, tree);
106 
107             String json = tsc.dump2JsonStr();
108             System.out.println("ts parse result: " + json);
109 
110             ParseObj po = genParseResult(tsc);
111 
112             System.out.println("ts parse char stream finish");
113             return po;
114         } catch (RecognitionException e) {
115             System.out.println("parse cstream e.printStackTrace(): " + e.getMessage());
116         } finally {
117             sendEvent(Constants.COMPLETE_STATUS, Constants.TS_COMPLETE_MSG, 50);
118         }
119 
120         return null;
121     }
122 
123     /**
124      * 接收解析结果
125      *
126      * @param pi2 解析结构
127      */
128     @Override
receive(ParseTaskInfo pi2)129     public void receive(ParseTaskInfo pi2) {
130         super.receive(pi2);
131         if (pi2.getLanType() != Constants.PARSE_TS_LANGUAGE) {
132             System.err.println("Language type is not ts language");
133             return;
134         }
135 
136     }
137 
138     /**
139      * 生成解析结果
140      *
141      * @param pbl 解析监听
142      * @return 解析结果
143      */
144     @Override
genParseResult(ParseBaseListener pbl)145     protected ParseObj genParseResult(ParseBaseListener pbl) {
146         if (!(pbl instanceof TypeScriptCustomListener tcl)) {
147             return null;
148         }
149 
150         ParseObj po = new ParseObj();
151 
152         po.setInterfaceList(tcl.getInterfaceObjList());
153         po.setEnumList(tcl.getEnumObjList());
154         po.setClassList(tcl.getClassObjList());
155         po.setFuncList(tcl.getFuncObjList());
156         po.setStructList(tcl.getStructObjList());
157         po.setTypeList(tcl.getTypeObjList());
158         po.setUnionList(tcl.getUnionObjList());
159 
160         return po;
161     }
162 
163     /**
164      * 解析枚举
165      *
166      * @param pi2 解析结果
167      * @return 解析结果
168      */
169     @Override
parseEnum(ParseTaskInfo pi2)170     protected EnumObj[] parseEnum(ParseTaskInfo pi2) {
171         return super.parseEnum(pi2);
172     }
173 
174     /**
175      * 解析联合
176      *
177      * @param pi2 解析结果
178      * @return 解析结果
179      */
180     @Override
parseUnion(ParseTaskInfo pi2)181     protected UnionObj[] parseUnion(ParseTaskInfo pi2) {
182         return super.parseUnion(pi2);
183     }
184 
185     /**
186      * 解析结构体
187      *
188      * @param pi2 解析结果
189      * @return 解析结果
190      */
191     @Override
parseStruct(ParseTaskInfo pi2)192     protected StructObj[] parseStruct(ParseTaskInfo pi2) {
193         return super.parseStruct(pi2);
194     }
195 
196     /**
197      * 解析类
198      *
199      * @param pi2 解析结果
200      * @return 解析结果
201      */
202     @Override
parseClass(ParseTaskInfo pi2)203     protected ClassObj[] parseClass(ParseTaskInfo pi2) {
204         return super.parseClass(pi2);
205     }
206 
207     /**
208      * 解析方法
209      *
210      * @param pi2 解析结果
211      * @return 解析结果
212      */
213     @Override
parseFunc(ParseTaskInfo pi2)214     protected FuncObj[] parseFunc(ParseTaskInfo pi2) {
215         return super.parseFunc(pi2);
216     }
217 
218     /**
219      * 解析type
220      *
221      * @param pi2 解析结果
222      * @return 解析结果
223      */
224     @Override
parseType(ParseTaskInfo pi2)225     protected TypeObj[] parseType(ParseTaskInfo pi2) {
226         return super.parseType(pi2);
227     }
228 
229     /**
230      * 处理事件
231      *
232      * @param event 事件
233      */
234     @Override
handleEvent(CustomEvent event)235     public void handleEvent(CustomEvent event) {
236         System.out.println("parse ts handle: " + event.toString());
237     }
238 }
239