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.stmt;
18
19 import com.github.javaparser.ast.stmt.Statement;
20 import io.github.tonywasher.joceanus.oceanus.base.OceanusException;
21 import io.github.tonywasher.joceanus.themis.parser.base.ThemisInstance.ThemisId;
22 import io.github.tonywasher.joceanus.themis.parser.base.ThemisParserDef;
23
24 import java.util.function.Predicate;
25
26 /**
27 * Analysis StatementType.
28 */
29 public enum ThemisStatement
30 implements ThemisId {
31 /**
32 * Assert.
33 */
34 ASSERT(Statement::isAssertStmt),
35
36 /**
37 * Block.
38 */
39 BLOCK(Statement::isBlockStmt),
40
41 /**
42 * Break.
43 */
44 BREAK(Statement::isBreakStmt),
45
46 /**
47 * Continue.
48 */
49 CONTINUE(Statement::isContinueStmt),
50
51 /**
52 * Constructor.
53 */
54 CONSTRUCTOR(Statement::isExplicitConstructorInvocationStmt),
55
56 /**
57 * Do.
58 */
59 DO(Statement::isDoStmt),
60
61 /**
62 * Empty.
63 */
64 EMPTY(Statement::isEmptyStmt),
65
66 /**
67 * Expression.
68 */
69 EXPRESSION(Statement::isExpressionStmt),
70
71 /**
72 * For.
73 */
74 FOR(Statement::isForStmt),
75
76 /**
77 * ForEach.
78 */
79 FOREACH(Statement::isForEachStmt),
80
81 /**
82 * If.
83 */
84 IF(Statement::isIfStmt),
85
86 /**
87 * Labeled.
88 */
89 LABELED(Statement::isLabeledStmt),
90
91 /**
92 * LocalClass.
93 */
94 LOCALCLASS(Statement::isLocalClassDeclarationStmt),
95
96 /**
97 * LocalRecord.
98 */
99 LOCALRECORD(Statement::isLocalRecordDeclarationStmt),
100
101 /**
102 * Return.
103 */
104 RETURN(Statement::isReturnStmt),
105
106 /**
107 * ForEach.
108 */
109 SWITCH(Statement::isSwitchStmt),
110
111 /**
112 * Synchronized.
113 */
114 SYNCHRONIZED(Statement::isSynchronizedStmt),
115
116 /**
117 * Throw.
118 */
119 THROW(Statement::isThrowStmt),
120
121 /**
122 * Try.
123 */
124 TRY(Statement::isTryStmt),
125
126 /**
127 * While.
128 */
129 WHILE(Statement::isWhileStmt),
130
131 /**
132 * Yield.
133 */
134 YIELD(Statement::isYieldStmt);
135
136 /**
137 * The test.
138 */
139 private final Predicate<Statement> theTester;
140
141 /**
142 * Constructor.
143 *
144 * @param pTester the test method
145 */
146 ThemisStatement(final Predicate<Statement> pTester) {
147 theTester = pTester;
148 }
149
150 /**
151 * Determine type of statement.
152 *
153 * @param pParser the parser
154 * @param pStatement the statement
155 * @return the StatementType
156 * @throws OceanusException on error
157 */
158 public static ThemisStatement determineStatement(final ThemisParserDef pParser,
159 final Statement pStatement) throws OceanusException {
160 /* Loop testing each statement type */
161 for (ThemisStatement myType : values()) {
162 if (myType.theTester.test(pStatement)) {
163 return myType;
164 }
165 }
166
167 /* Unrecognised statementType */
168 throw pParser.buildException("Unexpected Statement", pStatement);
169 }
170 }