11package com .relogiclabs .jschema .internal .engine ;
22
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 ;
65import com .relogiclabs .jschema .internal .antlr .SchemaParser .ArrayLiteralContext ;
76import com .relogiclabs .jschema .internal .antlr .SchemaParser .BlockStatementContext ;
87import com .relogiclabs .jschema .internal .antlr .SchemaParser .BreakStatementContext ;
4746
4847import static com .relogiclabs .jschema .internal .antlr .SchemaLexer .G_STRING ;
4948import static com .relogiclabs .jschema .internal .engine .ScriptErrorHelper .failOnInvalidReturnType ;
50- import static com .relogiclabs .jschema .internal .engine .ScriptErrorHelper .failOnRuntime ;
5149import static com .relogiclabs .jschema .internal .engine .ScriptErrorHelper .failOnSystemException ;
5250import static com .relogiclabs .jschema .internal .engine .ScriptTreeHelper .dereference ;
5351import static com .relogiclabs .jschema .internal .engine .ScriptTreeHelper .getFunctionMode ;
5856import static com .relogiclabs .jschema .internal .script .GControl .BREAK ;
5957import static com .relogiclabs .jschema .internal .util .CollectionHelper .subList ;
6058import 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 ;
7876import static com .relogiclabs .jschema .type .ENull .NULL ;
7977import static com .relogiclabs .jschema .type .EUndefined .UNDEFINED ;
8078import static com .relogiclabs .jschema .type .EValue .VOID ;
@@ -106,7 +104,7 @@ public Evaluator visitCompleteSchema(CompleteSchemaContext ctx) {
106104 return tryCatch (scope -> {
107105 for (var s : scripts ) s .evaluate (scope );
108106 return VOID ;
109- }, SRPT01 , ctx );
107+ }, SRPTSE01 , ctx );
110108 }
111109
112110 private Evaluator processScript (ParserRuleContext context ) {
@@ -126,7 +124,7 @@ public Evaluator visitScriptNode(ScriptNodeContext ctx) {
126124 return tryCatch (scope -> {
127125 for (var s : statements ) s .evaluate (scope );
128126 return VOID ;
129- }, SRPT02 , ctx );
127+ }, SRPTSE02 , ctx );
130128 }
131129
132130 @ Override
@@ -145,7 +143,7 @@ public Evaluator visitFunctionDeclaration(FunctionDeclarationContext ctx) {
145143 if (constraint ) runtime .getFunctions ()
146144 .addFunction (new ScriptFunction (baseName , function ));
147145 return VOID ;
148- }, FUND03 , ctx );
146+ }, FNSDEC01 , ctx );
149147 }
150148
151149 @ Override
@@ -154,7 +152,7 @@ public Evaluator visitVarStatement(VarStatementContext ctx) {
154152 return tryCatch (scope -> {
155153 for (var d : varDeclarations ) d .evaluate (scope );
156154 return VOID ;
157- }, VARD03 , ctx );
155+ }, VARDEC02 , ctx );
158156 }
159157
160158 @ Override
@@ -164,7 +162,7 @@ public Evaluator visitVarDeclaration(VarDeclarationContext ctx) {
164162 return tryCatch (scope -> {
165163 scope .addVariable (varName , dereference (expression .evaluate (scope )));
166164 return VOID ;
167- }, VARD02 , ctx );
165+ }, VARDEC01 , ctx );
168166 }
169167
170168 @ Override
@@ -173,7 +171,7 @@ public Evaluator visitExpressionStatement(ExpressionStatementContext ctx) {
173171 return tryCatch (scope -> {
174172 expression .evaluate (scope );
175173 return VOID ;
176- }, EXPR01 , ctx );
174+ }, EXPRSE01 , ctx );
177175 }
178176
179177 @ Override
@@ -184,14 +182,14 @@ public Evaluator visitIfStatement(IfStatementContext ctx) {
184182 if (condition .evaluate (scope ).toBoolean ())
185183 return thenStatement .evaluate (scope );
186184 return VOID ;
187- }, IFST01 , ctx );
185+ }, IFSTMT01 , ctx );
188186
189187 var elseStatement = visit (ctx .statement (1 ));
190188 return tryCatch (scope -> {
191189 if (condition .evaluate (scope ).toBoolean ())
192190 return thenStatement .evaluate (scope );
193191 else return elseStatement .evaluate (scope );
194- }, IFST02 , ctx );
192+ }, IFSTMT02 , ctx );
195193 }
196194
197195 @ Override
@@ -204,7 +202,7 @@ public Evaluator visitWhileStatement(WhileStatementContext ctx) {
204202 if (result instanceof GControl ctrl ) return ctrl .toIteration ();
205203 }
206204 return VOID ;
207- }, WHIL01 , ctx );
205+ }, WHILSE01 , ctx );
208206 }
209207
210208 @ Override
@@ -223,7 +221,7 @@ public Evaluator visitForStatement(ForStatementContext ctx) {
223221 if (result instanceof GControl ctrl ) return ctrl .toIteration ();
224222 }
225223 return VOID ;
226- }, FORS01 , ctx );
224+ }, FORSTM01 , ctx );
227225 }
228226
229227 @ Override
@@ -232,7 +230,7 @@ public Evaluator visitExpressionList(ExpressionListContext ctx) {
232230 return tryCatch (scope -> {
233231 for (var e : expressions ) e .evaluate (scope );
234232 return VOID ;
235- }, EXPR02 , ctx );
233+ }, EXPRSE02 , ctx );
236234 }
237235
238236 @ Override
@@ -249,21 +247,21 @@ public Evaluator visitForeachStatement(ForeachStatementContext ctx) {
249247 if (result instanceof GControl ctrl ) return ctrl .toIteration ();
250248 }
251249 return VOID ;
252- }, FREC01 , ctx );
250+ }, FORECH01 , ctx );
253251 }
254252
255253 @ Override
256254 public Evaluator visitReturnStatement (ReturnStatementContext ctx ) {
257255 var expression = visit (ctx .expression ());
258256 if (returnType == null ) return tryCatch (scope -> GControl .ofReturn (
259- expression .evaluate (scope )), RETN02 , ctx );
257+ expression .evaluate (scope )), RETNSE01 , ctx );
260258 var thisReturnType = returnType ;
261259 return tryCatch (scope -> {
262260 var v1 = expression .evaluate (scope );
263261 if (!thisReturnType .isInstance (v1 ))
264262 throw failOnInvalidReturnType (v1 , ctx .expression ().getStart ());
265263 return GControl .ofReturn (v1 );
266- }, RETN03 , ctx );
264+ }, RETNSE03 , ctx );
267265 }
268266
269267 @ Override
@@ -281,7 +279,7 @@ public Evaluator visitBlockStatement(BlockStatementContext ctx) {
281279 if (result instanceof GControl ctrl ) return ctrl ;
282280 }
283281 return VOID ;
284- }, BLOK01 , ctx );
282+ }, BLOKSE01 , ctx );
285283 }
286284
287285 @ Override
@@ -326,7 +324,7 @@ public Evaluator visitStringLiteral(StringLiteralContext ctx) {
326324 public Evaluator visitArrayLiteral (ArrayLiteralContext ctx ) {
327325 var list = ctx .expression ().stream ().map (this ::visit ).toList ();
328326 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 );
330328 }
331329
332330 @ Override
@@ -335,19 +333,19 @@ public Evaluator visitObjectLiteral(ObjectLiteralContext ctx) {
335333 ? toEncoded (k .getText ()) : k .getText ()).toList ();
336334 var values = ctx .values .stream ().map (this ::visit ).toList ();
337335 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 );
339337 }
340338
341339 static Evaluator tryCatch (Evaluator evaluator , String code , ParserRuleContext ctx ) {
342340 return scope -> {
343341 try {
344342 return evaluator .evaluate (scope );
345- } catch (ScriptRuntimeException | ScriptTemplateException e ) {
343+ } catch (MultilevelRuntimeException e ) {
344+ throw e .translate (ctx .getStart ());
345+ } catch (BaseRuntimeException e ) {
346346 throw e ;
347- } catch (CommonException e ) {
348- throw failOnRuntime (e .getCode (), e .getMessage (), ctx .start , e );
349347 } catch (Exception e ) {
350- throw failOnSystemException (code , e , ctx .start );
348+ throw failOnSystemException (code , e , ctx .getStart () );
351349 }
352350 };
353351 }
0 commit comments