allows translated panics from hgs

This commit is contained in:
hneemann 2018-03-25 17:23:34 +02:00
parent d25c92c105
commit 53ad33d6de
2 changed files with 30 additions and 2 deletions

View File

@ -8,6 +8,7 @@ package de.neemann.digital.hdl.hgs;
import de.neemann.digital.hdl.hgs.function.Func;
import de.neemann.digital.hdl.hgs.function.Function;
import de.neemann.digital.hdl.hgs.function.InnerFunction;
import de.neemann.digital.lang.Lang;
import java.util.ArrayList;
import java.util.HashMap;
@ -276,12 +277,27 @@ public class Context {
private static final class FunctionPanic extends Function {
private FunctionPanic() {
super(1);
super(-1);
}
@Override
protected Object f(Object... args) throws HGSEvalException {
throw new HGSEvalException(args[0].toString());
if (args.length == 0)
throw new HGSEvalException("panic");
String message = args[0].toString();
if (message.startsWith("err_")) {
if (args.length == 1)
message = Lang.get(message);
else {
String[] ar = new String[args.length - 1];
for (int i = 0; i < args.length - 1; i++)
ar[i] = args[i + 1].toString();
message = Lang.get(message, ar);
}
}
throw new HGSEvalException(message);
}
}

View File

@ -495,6 +495,18 @@ public class ParserTest extends TestCase {
}
}
public void testPanic2() throws IOException, ParserException, HGSEvalException {
Statement s = new Parser("<? panic(\"err_varNotDefined_N\",\"hello\"); ?>").parse();
try {
exec(s);
fail();
} catch (HGSEvalException e) {
assertTrue(e.getMessage().contains("hello"));
assertFalse(e.getMessage().contains("err_"));
}
}
public void testTrim() throws IOException, ParserException, HGSEvalException {
assertEquals(" 5", exec(" <?=5;-?> ").toString());
assertEquals("5 ", exec(" <?-=5?> ").toString());