• 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.cpp.CPP14CustomListener;
20 import antlr.cpp.CPP14ErrorListener;
21 import antlr.cpp.CPP14Lexer;
22 import antlr.cpp.CPP14Parser;
23 import com.fasterxml.jackson.core.JsonProcessingException;
24 import com.fasterxml.jackson.databind.ObjectMapper;
25 import grammar.*;
26 import org.antlr.v4.runtime.CharStream;
27 import org.antlr.v4.runtime.CommonTokenStream;
28 import org.antlr.v4.runtime.RecognitionException;
29 import org.antlr.v4.runtime.tree.ParseTree;
30 import org.antlr.v4.runtime.tree.ParseTreeWalker;
31 import utils.Constants;
32 import utils.BaseEvent;
33 
34 /**
35  * <h3>类名:该类用于xxx</h3>
36  * description ${description}
37  *
38  * @author ${USER}
39  * date 2025-02-28
40  * @since 2025-02-28
41  * @version 1.0
42  */
43 public class ParseC extends ParseBase {
44     /**
45      * 根据名字解析文件
46      *
47      * @param filePath 文件路径
48      */
49     @Override
parseFile(String filePath)50     public void parseFile(String filePath) {
51         System.out.println("parseFile: " + filePath);
52         BaseEvent pcEvent = new BaseEvent(this);
53         pcEvent.setEventMsg("parsec complete");
54         ParseTaskInfo pi = new ParseTaskInfo("start", "parse c starting", 0, 100);
55         ObjectMapper mapper = new ObjectMapper();
56         try {
57             String jsonStr = mapper.writeValueAsString(pi);
58             pcEvent.setEventMsg(jsonStr);
59         } catch (JsonProcessingException e) {
60             System.out.println("json process error: " + e.getMessage());
61         }
62         listeners.forEach(listener -> {
63             listener.onEvent(pcEvent);
64         });
65     }
66 
67     /**
68      * 根据内容解析文件
69      *
70      * @param fileContent 文件内容
71      */
72     @Override
parseContent(String fileContent)73     public void parseContent(String fileContent) {
74         System.out.println("c parseContent");
75         BaseEvent pcEvent = new BaseEvent(this);
76         pcEvent.setEventMsg("parsec complete");
77         ParseTaskInfo pi = new ParseTaskInfo("start", "parse c content starting", 0, 100);
78         ObjectMapper mapper = new ObjectMapper();
79         try {
80             String jsonStr = mapper.writeValueAsString(pi);
81             pcEvent.setEventMsg(jsonStr);
82         } catch (JsonProcessingException e) {
83             System.out.println("json process error: " + e.getMessage());
84         }
85         listeners.forEach(listener -> {
86             listener.onEvent(pcEvent);
87         });
88     }
89 
90     /**
91      * 处理内容
92      *
93      * @param fileCStream
94      *         文件内容
95      * @return 解析结果
96      */
97     @Override
parseCStream(CharStream fileCStream)98     public ParseObj parseCStream(CharStream fileCStream) {
99         System.out.println("c/cpp parse char stream");
100         this.fcStream = fileCStream;
101         sendEvent(Constants.START_STATUS, Constants.C_CPP_START_MSG, 50);
102         try {
103             // 初始化词法分析器
104             CPP14Lexer lexer = new CPP14Lexer(this.fcStream);
105             CommonTokenStream tokens = new CommonTokenStream(lexer);
106             // 初始化语法分析器并生成 AST
107             CPP14Parser parser = new CPP14Parser(tokens);
108             parser.removeErrorListeners();
109             parser.addErrorListener(new CPP14ErrorListener());
110             ParseTree tree = parser.translationUnit();
111             CPP14CustomListener tsc = new CPP14CustomListener();
112             ParseTreeWalker walker = new ParseTreeWalker();
113             walker.walk(tsc, tree);
114 
115             System.out.println();
116         } catch (RecognitionException e) {
117             System.out.println("parse cstream e.printStackTrace(): " + e.getMessage());
118         }
119 
120         sendEvent(Constants.COMPLETE_STATUS, Constants.C_CPP_COMPLETE_MSG, 50);
121         return null;
122     }
123 
124     /**
125      * 生成解析结果
126      *
127      * @param pbl 解析监听
128      * @return 解析结果
129      */
130     @Override
genParseResult(ParseBaseListener pbl)131     protected ParseObj genParseResult(ParseBaseListener pbl) {
132         return null;
133     }
134 
135     /**
136      * 解析枚举
137      *
138      * @param pi2 解析结果
139      * @return 枚举
140      */
141     @Override
parseEnum(ParseTaskInfo pi2)142     protected EnumObj[] parseEnum(ParseTaskInfo pi2) {
143         return super.parseEnum(pi2);
144     }
145 
146     /**
147      * 解析枚举
148      *
149      * @param pi2 解析结果
150      * @return 联合
151      */
152     @Override
parseUnion(ParseTaskInfo pi2)153     protected UnionObj[] parseUnion(ParseTaskInfo pi2) {
154         return super.parseUnion(pi2);
155     }
156 
157     /**
158      * 解析枚举
159      *
160      * @param pi2 解析结果
161      * @return 结构体
162      */
163     @Override
parseStruct(ParseTaskInfo pi2)164     protected StructObj[] parseStruct(ParseTaskInfo pi2) {
165         return super.parseStruct(pi2);
166     }
167 
168     /**
169      * 解析枚举
170      *
171      * @param pi2 解析结果
172      * @return173      */
174     @Override
parseClass(ParseTaskInfo pi2)175     protected ClassObj[] parseClass(ParseTaskInfo pi2) {
176         return super.parseClass(pi2);
177     }
178 
179     /**
180      * 解析枚举
181      *
182      * @param pi2 解析结果
183      * @return 方法
184      */
185     @Override
parseFunc(ParseTaskInfo pi2)186     protected FuncObj[] parseFunc(ParseTaskInfo pi2) {
187         return super.parseFunc(pi2);
188     }
189 
190     /**
191      * 解析枚举
192      *
193      * @param pi2 解析结果
194      * @return 类型
195      */
196     @Override
parseType(ParseTaskInfo pi2)197     protected TypeObj[] parseType(ParseTaskInfo pi2) {
198         return super.parseType(pi2);
199     }
200 }
201