View Javadoc
1   /*
2    * Themis: Java Project Framework
3    * Copyright 2012-2026. Tony Washer
4    *
5    * Licensed under the Apache License, Version 2.0 (the "License"); you may not
6    * use this file except in compliance with the License.  You may obtain a copy
7    * of the License at
8    *
9    *   http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
13   * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the
14   * License for the specific language governing permissions and limitations under
15   * the License.
16   */
17  package io.github.tonywasher.joceanus.themis.parser.base;
18  
19  import com.github.javaparser.ast.Node;
20  import com.github.javaparser.ast.NodeList;
21  import com.github.javaparser.ast.PackageDeclaration;
22  import com.github.javaparser.ast.body.BodyDeclaration;
23  import com.github.javaparser.ast.expr.Expression;
24  import com.github.javaparser.ast.modules.ModuleDirective;
25  import com.github.javaparser.ast.stmt.Statement;
26  import com.github.javaparser.ast.type.Type;
27  import io.github.tonywasher.joceanus.oceanus.base.OceanusException;
28  import io.github.tonywasher.joceanus.tethys.api.thread.TethysUIThreadStatusReport;
29  import io.github.tonywasher.joceanus.themis.parser.base.ThemisInstance.ThemisClassInstance;
30  import io.github.tonywasher.joceanus.themis.parser.base.ThemisInstance.ThemisDeclarationInstance;
31  import io.github.tonywasher.joceanus.themis.parser.base.ThemisInstance.ThemisExpressionInstance;
32  import io.github.tonywasher.joceanus.themis.parser.base.ThemisInstance.ThemisModuleInstance;
33  import io.github.tonywasher.joceanus.themis.parser.base.ThemisInstance.ThemisNodeInstance;
34  import io.github.tonywasher.joceanus.themis.parser.base.ThemisInstance.ThemisStatementInstance;
35  import io.github.tonywasher.joceanus.themis.parser.base.ThemisInstance.ThemisTypeInstance;
36  
37  import java.io.File;
38  import java.util.ArrayList;
39  import java.util.Collections;
40  import java.util.List;
41  
42  /**
43   * Parser interface.
44   */
45  public interface ThemisParserDef {
46      /**
47       * Obtain the reporter.
48       *
49       * @return the reporter
50       */
51      TethysUIThreadStatusReport getReporter();
52  
53      /**
54       * Process the file as javaCode.
55       *
56       * @return the parsed compilation unit
57       * @throws OceanusException on error
58       */
59      ThemisNodeInstance parseJavaFile() throws OceanusException;
60  
61      /**
62       * Process the file as a module-info instance.
63       *
64       * @param pInfoFile the module-info file
65       * @return the parsed moduleInfo
66       * @throws OceanusException on error
67       */
68      ThemisModuleInstance parseModuleInfo(File pInfoFile) throws OceanusException;
69  
70      /**
71       * Set the current package.
72       *
73       * @param pPackage the package
74       */
75      void setCurrentPackage(String pPackage);
76  
77      /**
78       * Set the current file.
79       *
80       * @param pFile the file
81       */
82      void setCurrentFile(File pFile);
83  
84      /**
85       * Obtain the classList.
86       *
87       * @return the classList
88       */
89      List<ThemisClassInstance> getClasses();
90  
91      /**
92       * Build exception.
93       *
94       * @param pMessage the message
95       * @param pNode    the failing node
96       * @return the built exception
97       */
98      OceanusException buildException(String pMessage,
99                                      Node pNode);
100 
101     /**
102      * Check the package name.
103      *
104      * @param pPackage the package name
105      * @throws OceanusException on error
106      */
107     void checkPackage(PackageDeclaration pPackage) throws OceanusException;
108 
109     /**
110      * Register Instance.
111      *
112      * @param pInstance the instance
113      * @return the parent node
114      * @throws OceanusException on error
115      */
116     ThemisInstance registerInstance(ThemisInstance pInstance) throws OceanusException;
117 
118     /**
119      * Register Class.
120      *
121      * @param pClass the class
122      * @return the class name
123      */
124     String registerClass(ThemisClassInstance pClass) throws OceanusException;
125 
126     /**
127      * Parse a declaration.
128      *
129      * @param pDecl the declaration
130      * @return the parsed declaration
131      * @throws OceanusException on error
132      */
133     ThemisDeclarationInstance parseDeclaration(BodyDeclaration<?> pDecl) throws OceanusException;
134 
135     /**
136      * parse a list of declarations.
137      *
138      * @param pDeclList the list of Declarations
139      * @return the list of parsed declarations
140      * @throws OceanusException on error
141      */
142     default List<ThemisDeclarationInstance> parseDeclarationList(final NodeList<? extends BodyDeclaration<?>> pDeclList) throws OceanusException {
143         /* Handle null list */
144         if (pDeclList == null) {
145             return Collections.emptyList();
146         }
147 
148         /* Create list of declarations */
149         final List<ThemisDeclarationInstance> myList = new ArrayList<>();
150         for (BodyDeclaration<?> myDecl : pDeclList) {
151             final ThemisDeclarationInstance myParsed = parseDeclaration(myDecl);
152             myList.add(myParsed);
153         }
154         return myList;
155     }
156 
157     /**
158      * Parse a node.
159      *
160      * @param pNode the node
161      * @return the parsed node
162      * @throws OceanusException on error
163      */
164     ThemisNodeInstance parseNode(Node pNode) throws OceanusException;
165 
166     /**
167      * parse a list of nodes.
168      *
169      * @param pNodeList the list of Nodes
170      * @return the list of parsed nodes
171      * @throws OceanusException on error
172      */
173     default List<ThemisNodeInstance> parseNodeList(final NodeList<? extends Node> pNodeList) throws OceanusException {
174         /* Handle null list */
175         if (pNodeList == null) {
176             return Collections.emptyList();
177         }
178 
179         /* Create list of nodes */
180         final List<ThemisNodeInstance> myList = new ArrayList<>();
181         for (Node myNode : pNodeList) {
182             final ThemisNodeInstance myParsed = parseNode(myNode);
183             myList.add(myParsed);
184         }
185         return myList;
186     }
187 
188     /**
189      * parse a list of modifiers.
190      *
191      * @param pNodeList the list of Modifiers
192      * @return the list of parsed modifiers
193      * @throws OceanusException on error
194      */
195     ThemisModifierList parseModifierList(NodeList<? extends Node> pNodeList) throws OceanusException;
196 
197     /**
198      * Parse a type.
199      *
200      * @param pType the type
201      * @return the parsed type
202      * @throws OceanusException on error
203      */
204     ThemisTypeInstance parseType(Type pType) throws OceanusException;
205 
206     /**
207      * parse a list of types.
208      *
209      * @param pTypeList the list of Types
210      * @return the list of parsed types
211      * @throws OceanusException on error
212      */
213     default List<ThemisTypeInstance> parseTypeList(final NodeList<? extends Type> pTypeList) throws OceanusException {
214         /* Handle null list */
215         if (pTypeList == null) {
216             return Collections.emptyList();
217         }
218 
219         /* Create list of nodes */
220         final List<ThemisTypeInstance> myList = new ArrayList<>();
221         for (Type myType : pTypeList) {
222             final ThemisTypeInstance myParsed = parseType(myType);
223             myList.add(myParsed);
224         }
225         return myList;
226     }
227 
228     /**
229      * Parse a statement.
230      *
231      * @param pStatement the statement
232      * @return the parsed statement
233      * @throws OceanusException on error
234      */
235     ThemisStatementInstance parseStatement(Statement pStatement) throws OceanusException;
236 
237     /**
238      * parse a list of statements.
239      *
240      * @param pStatementList the list of Statements
241      * @return the list of parsed statements
242      * @throws OceanusException on error
243      */
244     default List<ThemisStatementInstance> parseStatementList(final NodeList<? extends Statement> pStatementList) throws OceanusException {
245         /* Handle null list */
246         if (pStatementList == null) {
247             return Collections.emptyList();
248         }
249 
250         /* Create list of statements */
251         final List<ThemisStatementInstance> myList = new ArrayList<>();
252         for (Statement myStatement : pStatementList) {
253             final ThemisStatementInstance myParsed = parseStatement(myStatement);
254             myList.add(myParsed);
255         }
256         return myList;
257     }
258 
259     /**
260      * Parse an expression.
261      *
262      * @param pExpression the expression
263      * @return the parsed expression
264      * @throws OceanusException on error
265      */
266     ThemisExpressionInstance parseExpression(Expression pExpression) throws OceanusException;
267 
268     /**
269      * parse a list of expressions.
270      *
271      * @param pExprList the list of Expressions
272      * @return the list of parsed expressions
273      * @throws OceanusException on error
274      */
275     default List<ThemisExpressionInstance> parseExprList(final NodeList<? extends Expression> pExprList) throws OceanusException {
276         /* Handle null list */
277         if (pExprList == null) {
278             return Collections.emptyList();
279         }
280 
281         /* Create list of expressions */
282         final List<ThemisExpressionInstance> myList = new ArrayList<>();
283         for (Expression myExpr : pExprList) {
284             final ThemisExpressionInstance myParsed = parseExpression(myExpr);
285             myList.add(myParsed);
286         }
287         return myList;
288     }
289 
290     /**
291      * Parse a module.
292      *
293      * @param pDeclaration the module declaration/directive
294      * @return the parsed declaration/directive
295      * @throws OceanusException on error
296      */
297     ThemisModuleInstance parseModule(Node pDeclaration) throws OceanusException;
298 
299     /**
300      * parse a list of module directives.
301      *
302      * @param pDirList the list of Directives
303      * @return the list of parsed directives
304      * @throws OceanusException on error
305      */
306     default List<ThemisModuleInstance> parseModuleList(final NodeList<? extends ModuleDirective> pDirList) throws OceanusException {
307         /* Handle null list */
308         if (pDirList == null) {
309             return Collections.emptyList();
310         }
311 
312         /* Create list of directives */
313         final List<ThemisModuleInstance> myList = new ArrayList<>();
314         for (ModuleDirective myDir : pDirList) {
315             final ThemisModuleInstance myParsed = parseModule(myDir);
316             myList.add(myParsed);
317         }
318         return myList;
319     }
320 }