1
1
package com .relogiclabs .jschema .internal .engine ;
2
2
3
- import com .relogiclabs .jschema .exception .CommonException ;
4
- import com .relogiclabs .jschema .exception .ScriptRuntimeException ;
5
- import com .relogiclabs .jschema .exception .ScriptTemplateException ;
3
+ import com .relogiclabs .jschema .exception .BaseRuntimeException ;
4
+ import com .relogiclabs .jschema .exception .MultilevelRuntimeException ;
6
5
import com .relogiclabs .jschema .internal .antlr .SchemaParser .ArrayLiteralContext ;
7
6
import com .relogiclabs .jschema .internal .antlr .SchemaParser .BlockStatementContext ;
8
7
import com .relogiclabs .jschema .internal .antlr .SchemaParser .BreakStatementContext ;
47
46
48
47
import static com .relogiclabs .jschema .internal .antlr .SchemaLexer .G_STRING ;
49
48
import static com .relogiclabs .jschema .internal .engine .ScriptErrorHelper .failOnInvalidReturnType ;
50
- import static com .relogiclabs .jschema .internal .engine .ScriptErrorHelper .failOnRuntime ;
51
49
import static com .relogiclabs .jschema .internal .engine .ScriptErrorHelper .failOnSystemException ;
52
50
import static com .relogiclabs .jschema .internal .engine .ScriptTreeHelper .dereference ;
53
51
import static com .relogiclabs .jschema .internal .engine .ScriptTreeHelper .getFunctionMode ;
58
56
import static com .relogiclabs .jschema .internal .script .GControl .BREAK ;
59
57
import static com .relogiclabs .jschema .internal .util .CollectionHelper .subList ;
60
58
import static com .relogiclabs .jschema .internal .util .StringHelper .toEncoded ;
61
- import static com .relogiclabs .jschema .message .ErrorCode .ARRL01 ;
62
- import static com .relogiclabs .jschema .message .ErrorCode .BLOK01 ;
63
- import static com .relogiclabs .jschema .message .ErrorCode .EXPR01 ;
64
- import static com .relogiclabs .jschema .message .ErrorCode .EXPR02 ;
65
- import static com .relogiclabs .jschema .message .ErrorCode .FORS01 ;
66
- import static com .relogiclabs .jschema .message .ErrorCode .FREC01 ;
67
- import static com .relogiclabs .jschema .message .ErrorCode .FUND03 ;
68
- import static com .relogiclabs .jschema .message .ErrorCode .IFST01 ;
69
- import static com .relogiclabs .jschema .message .ErrorCode .IFST02 ;
70
- import static com .relogiclabs .jschema .message .ErrorCode .OBJL01 ;
71
- import static com .relogiclabs .jschema .message .ErrorCode .RETN02 ;
72
- import static com .relogiclabs .jschema .message .ErrorCode .RETN03 ;
73
- import static com .relogiclabs .jschema .message .ErrorCode .SRPT01 ;
74
- import static com .relogiclabs .jschema .message .ErrorCode .SRPT02 ;
75
- import static com .relogiclabs .jschema .message .ErrorCode .VARD02 ;
76
- import static com .relogiclabs .jschema .message .ErrorCode .VARD03 ;
77
- import static com .relogiclabs .jschema .message .ErrorCode .WHIL01 ;
59
+ import static com .relogiclabs .jschema .message .ErrorCode .ARRLIT01 ;
60
+ import static com .relogiclabs .jschema .message .ErrorCode .BLOKSE01 ;
61
+ import static com .relogiclabs .jschema .message .ErrorCode .EXPRSE01 ;
62
+ import static com .relogiclabs .jschema .message .ErrorCode .EXPRSE02 ;
63
+ import static com .relogiclabs .jschema .message .ErrorCode .FNSDEC01 ;
64
+ import static com .relogiclabs .jschema .message .ErrorCode .FORECH01 ;
65
+ import static com .relogiclabs .jschema .message .ErrorCode .FORSTM01 ;
66
+ import static com .relogiclabs .jschema .message .ErrorCode .IFSTMT01 ;
67
+ import static com .relogiclabs .jschema .message .ErrorCode .IFSTMT02 ;
68
+ import static com .relogiclabs .jschema .message .ErrorCode .OBJLIT01 ;
69
+ import static com .relogiclabs .jschema .message .ErrorCode .RETNSE01 ;
70
+ import static com .relogiclabs .jschema .message .ErrorCode .RETNSE03 ;
71
+ import static com .relogiclabs .jschema .message .ErrorCode .SRPTSE01 ;
72
+ import static com .relogiclabs .jschema .message .ErrorCode .SRPTSE02 ;
73
+ import static com .relogiclabs .jschema .message .ErrorCode .VARDEC01 ;
74
+ import static com .relogiclabs .jschema .message .ErrorCode .VARDEC02 ;
75
+ import static com .relogiclabs .jschema .message .ErrorCode .WHILSE01 ;
78
76
import static com .relogiclabs .jschema .type .ENull .NULL ;
79
77
import static com .relogiclabs .jschema .type .EUndefined .UNDEFINED ;
80
78
import static com .relogiclabs .jschema .type .EValue .VOID ;
@@ -106,7 +104,7 @@ public Evaluator visitCompleteSchema(CompleteSchemaContext ctx) {
106
104
return tryCatch (scope -> {
107
105
for (var s : scripts ) s .evaluate (scope );
108
106
return VOID ;
109
- }, SRPT01 , ctx );
107
+ }, SRPTSE01 , ctx );
110
108
}
111
109
112
110
private Evaluator processScript (ParserRuleContext context ) {
@@ -126,7 +124,7 @@ public Evaluator visitScriptNode(ScriptNodeContext ctx) {
126
124
return tryCatch (scope -> {
127
125
for (var s : statements ) s .evaluate (scope );
128
126
return VOID ;
129
- }, SRPT02 , ctx );
127
+ }, SRPTSE02 , ctx );
130
128
}
131
129
132
130
@ Override
@@ -145,7 +143,7 @@ public Evaluator visitFunctionDeclaration(FunctionDeclarationContext ctx) {
145
143
if (constraint ) runtime .getFunctions ()
146
144
.addFunction (new ScriptFunction (baseName , function ));
147
145
return VOID ;
148
- }, FUND03 , ctx );
146
+ }, FNSDEC01 , ctx );
149
147
}
150
148
151
149
@ Override
@@ -154,7 +152,7 @@ public Evaluator visitVarStatement(VarStatementContext ctx) {
154
152
return tryCatch (scope -> {
155
153
for (var d : varDeclarations ) d .evaluate (scope );
156
154
return VOID ;
157
- }, VARD03 , ctx );
155
+ }, VARDEC02 , ctx );
158
156
}
159
157
160
158
@ Override
@@ -164,7 +162,7 @@ public Evaluator visitVarDeclaration(VarDeclarationContext ctx) {
164
162
return tryCatch (scope -> {
165
163
scope .addVariable (varName , dereference (expression .evaluate (scope )));
166
164
return VOID ;
167
- }, VARD02 , ctx );
165
+ }, VARDEC01 , ctx );
168
166
}
169
167
170
168
@ Override
@@ -173,7 +171,7 @@ public Evaluator visitExpressionStatement(ExpressionStatementContext ctx) {
173
171
return tryCatch (scope -> {
174
172
expression .evaluate (scope );
175
173
return VOID ;
176
- }, EXPR01 , ctx );
174
+ }, EXPRSE01 , ctx );
177
175
}
178
176
179
177
@ Override
@@ -184,14 +182,14 @@ public Evaluator visitIfStatement(IfStatementContext ctx) {
184
182
if (condition .evaluate (scope ).toBoolean ())
185
183
return thenStatement .evaluate (scope );
186
184
return VOID ;
187
- }, IFST01 , ctx );
185
+ }, IFSTMT01 , ctx );
188
186
189
187
var elseStatement = visit (ctx .statement (1 ));
190
188
return tryCatch (scope -> {
191
189
if (condition .evaluate (scope ).toBoolean ())
192
190
return thenStatement .evaluate (scope );
193
191
else return elseStatement .evaluate (scope );
194
- }, IFST02 , ctx );
192
+ }, IFSTMT02 , ctx );
195
193
}
196
194
197
195
@ Override
@@ -204,7 +202,7 @@ public Evaluator visitWhileStatement(WhileStatementContext ctx) {
204
202
if (result instanceof GControl ctrl ) return ctrl .toIteration ();
205
203
}
206
204
return VOID ;
207
- }, WHIL01 , ctx );
205
+ }, WHILSE01 , ctx );
208
206
}
209
207
210
208
@ Override
@@ -223,7 +221,7 @@ public Evaluator visitForStatement(ForStatementContext ctx) {
223
221
if (result instanceof GControl ctrl ) return ctrl .toIteration ();
224
222
}
225
223
return VOID ;
226
- }, FORS01 , ctx );
224
+ }, FORSTM01 , ctx );
227
225
}
228
226
229
227
@ Override
@@ -232,7 +230,7 @@ public Evaluator visitExpressionList(ExpressionListContext ctx) {
232
230
return tryCatch (scope -> {
233
231
for (var e : expressions ) e .evaluate (scope );
234
232
return VOID ;
235
- }, EXPR02 , ctx );
233
+ }, EXPRSE02 , ctx );
236
234
}
237
235
238
236
@ Override
@@ -249,21 +247,21 @@ public Evaluator visitForeachStatement(ForeachStatementContext ctx) {
249
247
if (result instanceof GControl ctrl ) return ctrl .toIteration ();
250
248
}
251
249
return VOID ;
252
- }, FREC01 , ctx );
250
+ }, FORECH01 , ctx );
253
251
}
254
252
255
253
@ Override
256
254
public Evaluator visitReturnStatement (ReturnStatementContext ctx ) {
257
255
var expression = visit (ctx .expression ());
258
256
if (returnType == null ) return tryCatch (scope -> GControl .ofReturn (
259
- expression .evaluate (scope )), RETN02 , ctx );
257
+ expression .evaluate (scope )), RETNSE01 , ctx );
260
258
var thisReturnType = returnType ;
261
259
return tryCatch (scope -> {
262
260
var v1 = expression .evaluate (scope );
263
261
if (!thisReturnType .isInstance (v1 ))
264
262
throw failOnInvalidReturnType (v1 , ctx .expression ().getStart ());
265
263
return GControl .ofReturn (v1 );
266
- }, RETN03 , ctx );
264
+ }, RETNSE03 , ctx );
267
265
}
268
266
269
267
@ Override
@@ -281,7 +279,7 @@ public Evaluator visitBlockStatement(BlockStatementContext ctx) {
281
279
if (result instanceof GControl ctrl ) return ctrl ;
282
280
}
283
281
return VOID ;
284
- }, BLOK01 , ctx );
282
+ }, BLOKSE01 , ctx );
285
283
}
286
284
287
285
@ Override
@@ -326,7 +324,7 @@ public Evaluator visitStringLiteral(StringLiteralContext ctx) {
326
324
public Evaluator visitArrayLiteral (ArrayLiteralContext ctx ) {
327
325
var list = ctx .expression ().stream ().map (this ::visit ).toList ();
328
326
return tryCatch (scope -> new GArray (list .stream ().map (e
329
- -> dereference (e .evaluate (scope ))).toList ()), ARRL01 , ctx );
327
+ -> dereference (e .evaluate (scope ))).toList ()), ARRLIT01 , ctx );
330
328
}
331
329
332
330
@ Override
@@ -335,19 +333,19 @@ public Evaluator visitObjectLiteral(ObjectLiteralContext ctx) {
335
333
? toEncoded (k .getText ()) : k .getText ()).toList ();
336
334
var values = ctx .values .stream ().map (this ::visit ).toList ();
337
335
return tryCatch (scope -> new GObject (keys , values .stream ().map (v
338
- -> dereference (v .evaluate (scope ))).toList ()), OBJL01 , ctx );
336
+ -> dereference (v .evaluate (scope ))).toList ()), OBJLIT01 , ctx );
339
337
}
340
338
341
339
static Evaluator tryCatch (Evaluator evaluator , String code , ParserRuleContext ctx ) {
342
340
return scope -> {
343
341
try {
344
342
return evaluator .evaluate (scope );
345
- } catch (ScriptRuntimeException | ScriptTemplateException e ) {
343
+ } catch (MultilevelRuntimeException e ) {
344
+ throw e .translate (ctx .getStart ());
345
+ } catch (BaseRuntimeException e ) {
346
346
throw e ;
347
- } catch (CommonException e ) {
348
- throw failOnRuntime (e .getCode (), e .getMessage (), ctx .start , e );
349
347
} catch (Exception e ) {
350
- throw failOnSystemException (code , e , ctx .start );
348
+ throw failOnSystemException (code , e , ctx .getStart () );
351
349
}
352
350
};
353
351
}
0 commit comments