Report DSL errors from RulesDsl.makeRules as a DslAggregateException

This commit is contained in:
Riccardo Azzolini 2019-01-30 21:42:35 +01:00
parent f930242ee8
commit 0d89711772
4 changed files with 39 additions and 11 deletions

View File

@ -21,6 +21,7 @@ import it.cavallium.warppi.math.MathContext;
import it.cavallium.warppi.math.functions.Expression;
import it.cavallium.warppi.math.functions.Variable;
import it.cavallium.warppi.math.functions.Variable.V_TYPE;
import it.cavallium.warppi.math.rules.dsl.DslAggregateException;
import it.cavallium.warppi.math.rules.dsl.RulesDsl;
import it.cavallium.warppi.math.solver.MathSolver;
import it.cavallium.warppi.util.Error;
@ -152,13 +153,13 @@ public class RulesManager {
if (cacheFileStream != null) {
cacheFileStream.close();
}
} catch (URISyntaxException | IOException | RuntimeException e) {
} catch (URISyntaxException | IOException | DslAggregateException e) {
e.printStackTrace();
Engine.getPlatform().exit(1);
}
}
private static void loadDslRules() throws IOException {
private static void loadDslRules() throws IOException, DslAggregateException {
final StorageUtils storageUtils = Engine.getPlatform().getStorageUtils();
final File dslRulesPath = storageUtils.get("rules/dsl/");

View File

@ -0,0 +1,27 @@
package it.cavallium.warppi.math.rules.dsl;
import java.util.List;
/**
* Thrown when processing DSL code which contains one or more errors.
*
* Contains a list of {@link DslException}s, which should not be empty.
*/
public class DslAggregateException extends Exception {
private final List<DslException> exceptions;
/**
* Constructs a <code>DslAggregateException</code> containing the specified list of exceptions.
* @param exceptions The list of exceptions. Should not be empty.
*/
public DslAggregateException(final List<DslException> exceptions) {
this.exceptions = exceptions;
}
/**
* @return The list of errors detected in the DSL code.
*/
public List<DslException> getExceptions() {
return exceptions;
}
}

View File

@ -10,7 +10,7 @@ import java.util.*;
public class RulesDsl {
private RulesDsl() {}
public static List<Rule> makeRules(final String source) {
public static List<Rule> makeRules(final String source) throws DslAggregateException {
final List<DslException> errors = new ArrayList<>();
final Lexer lexer = new Lexer(source, errors::add);
@ -25,7 +25,7 @@ public class RulesDsl {
}
if (!errors.isEmpty()) {
throw new RuntimeException("Errors in DSL source code");
throw new DslAggregateException(errors);
}
return Collections.unmodifiableList(rules);

View File

@ -16,7 +16,7 @@ import static org.junit.Assert.*;
public class RulesDslTest {
@Test
public void validRules() {
public void validRules() throws DslAggregateException {
final List<Rule> rules = RulesDsl.makeRules(
"reduction test1: x -> x\n" +
"expansion test2:\n" +
@ -52,18 +52,18 @@ public class RulesDslTest {
assertEquals(expected, rules);
}
@Test(expected = RuntimeException.class)
public void lexerError() {
@Test(expected = DslAggregateException.class)
public void lexerError() throws DslAggregateException {
RulesDsl.makeRules("reduction test: 2. 5 -> 1");
}
@Test(expected = RuntimeException.class)
public void parserError() {
@Test(expected = DslAggregateException.class)
public void parserError() throws DslAggregateException {
RulesDsl.makeRules("existence test: x + y ->");
}
@Test(expected = RuntimeException.class)
public void undefinedSubFunction() {
@Test(expected = DslAggregateException.class)
public void undefinedSubFunction() throws DslAggregateException {
RulesDsl.makeRules("expansion test: x -> x + y");
}
}