Skip to content

Commit 779acd1

Browse files
committed
Update exception handling for script
1 parent c2c3943 commit 779acd1

22 files changed

+890
-753
lines changed

src/main/java/com/relogiclabs/jschema/internal/engine/ScriptErrorHelper.java

+86-78
Large diffs are not rendered by default.

src/main/java/com/relogiclabs/jschema/internal/engine/ScriptScope.java

+17-17
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
package com.relogiclabs.jschema.internal.engine;
22

3-
import com.relogiclabs.jschema.exception.ScriptFunctionException;
4-
import com.relogiclabs.jschema.exception.ScriptVariableException;
3+
import com.relogiclabs.jschema.exception.DuplicateFunctionException;
4+
import com.relogiclabs.jschema.exception.DuplicateVariableException;
55
import com.relogiclabs.jschema.internal.script.GFunction;
66
import com.relogiclabs.jschema.internal.script.GLeftValue;
77
import com.relogiclabs.jschema.tree.RuntimeContext;
@@ -11,11 +11,11 @@
1111
import java.util.HashMap;
1212
import java.util.Map;
1313

14-
import static com.relogiclabs.jschema.internal.library.ScriptLibrary3.resolveStatic;
14+
import static com.relogiclabs.jschema.internal.library.LibraryFunctions3.resolveStatic;
1515
import static com.relogiclabs.jschema.internal.tree.EFunction.CONSTRAINT_PREFIX;
16-
import static com.relogiclabs.jschema.message.ErrorCode.FUND01;
17-
import static com.relogiclabs.jschema.message.ErrorCode.FUND02;
18-
import static com.relogiclabs.jschema.message.ErrorCode.VARD01;
16+
import static com.relogiclabs.jschema.message.ErrorCode.FNSDUP01;
17+
import static com.relogiclabs.jschema.message.ErrorCode.FNSDUP02;
18+
import static com.relogiclabs.jschema.message.ErrorCode.VARDUP01;
1919
import static org.apache.commons.lang3.StringUtils.substringBefore;
2020

2121
public class ScriptScope {
@@ -31,22 +31,22 @@ public GLeftValue addVariable(String name, EValue value) {
3131
var lvalue = new GLeftValue(value);
3232
var oldValue = symbols.put(name, lvalue);
3333
if(oldValue == null) return lvalue;
34-
throw new ScriptVariableException(VARD01,
35-
"Variable '" + name + "' already defined in the scope");
34+
throw new DuplicateVariableException(VARDUP01,
35+
"Variable '" + name + "' already declared in the scope");
3636
}
3737

38-
public void addFunction(String name, GFunction function) {
39-
var oldValue = symbols.put(name, function);
38+
public void addFunction(String functionId, GFunction function) {
39+
var oldValue = symbols.put(functionId, function);
4040
if(oldValue == null) return;
41-
if(name.startsWith(CONSTRAINT_PREFIX))
42-
throw failOnDuplicateDefinition(FUND01, "Constraint", name);
43-
else throw failOnDuplicateDefinition(FUND02, "Subroutine", name);
41+
if(functionId.startsWith(CONSTRAINT_PREFIX))
42+
throw failOnDuplicateFunction(FNSDUP01, "Constraint", functionId);
43+
else throw failOnDuplicateFunction(FNSDUP02, "Subroutine", functionId);
4444
}
4545

46-
private static ScriptFunctionException failOnDuplicateDefinition(String code,
47-
String functionType, String name) {
48-
return new ScriptFunctionException(code, functionType + " function '"
49-
+ substringBefore(name, '#') + "' with matching parameter(s) already defined");
46+
private static DuplicateFunctionException failOnDuplicateFunction(String code,
47+
String functionType, String functionId) {
48+
return new DuplicateFunctionException(code, functionType + " function '"
49+
+ substringBefore(functionId, '#') + "' with matching parameter(s) already defined");
5050
}
5151

5252
public EValue resolve(String name) {

src/main/java/com/relogiclabs/jschema/internal/engine/ScriptTreeVisitor1.java

+40-42
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
package 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;
65
import com.relogiclabs.jschema.internal.antlr.SchemaParser.ArrayLiteralContext;
76
import com.relogiclabs.jschema.internal.antlr.SchemaParser.BlockStatementContext;
87
import com.relogiclabs.jschema.internal.antlr.SchemaParser.BreakStatementContext;
@@ -47,7 +46,6 @@
4746

4847
import static com.relogiclabs.jschema.internal.antlr.SchemaLexer.G_STRING;
4948
import static com.relogiclabs.jschema.internal.engine.ScriptErrorHelper.failOnInvalidReturnType;
50-
import static com.relogiclabs.jschema.internal.engine.ScriptErrorHelper.failOnRuntime;
5149
import static com.relogiclabs.jschema.internal.engine.ScriptErrorHelper.failOnSystemException;
5250
import static com.relogiclabs.jschema.internal.engine.ScriptTreeHelper.dereference;
5351
import static com.relogiclabs.jschema.internal.engine.ScriptTreeHelper.getFunctionMode;
@@ -58,23 +56,23 @@
5856
import static com.relogiclabs.jschema.internal.script.GControl.BREAK;
5957
import static com.relogiclabs.jschema.internal.util.CollectionHelper.subList;
6058
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;
7876
import static com.relogiclabs.jschema.type.ENull.NULL;
7977
import static com.relogiclabs.jschema.type.EUndefined.UNDEFINED;
8078
import 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

Comments
 (0)